00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "Text.h"
00023 #include "PrefDialog.h"
00024 #include "Config.h"
00025 #include <qpainter.h>
00026 #include <qsettings.h>
00027
00028 Text::Text ()
00029 {
00030 defaultColor.setNamedColor("red");
00031 helpFile = "text.html";
00032 label = "Text";
00033 fontLabel = "Font";
00034 labelLabel = "Label";
00035 type = "Text";
00036
00037 Config config;
00038 QString s;
00039 config.getData(Config::PlotFont, s);
00040 QStringList l = QStringList::split(",", s, FALSE);
00041 QFont f(l[0], l[1].toInt(), l[2].toInt());
00042 font = f;
00043
00044 loadDefaults();
00045 }
00046
00047 Text::~Text ()
00048 {
00049 }
00050
00051 void Text::draw (QPixmap &buffer, Scaler &scaler, int startIndex, int pixelspace, int startX)
00052 {
00053 QPainter painter;
00054 painter.begin(&buffer);
00055
00056 int x2 = data->getX(date);
00057 if (x2 == -1)
00058 return;
00059
00060 int x = startX + (x2 * pixelspace) - (startIndex * pixelspace);
00061 if (x == -1)
00062 return;
00063
00064 painter.setFont(font);
00065 painter.setPen(color);
00066
00067 int y = scaler.convertToY(getValue());
00068
00069 painter.drawText(x, y, label);
00070
00071 QFontMetrics fm = painter.fontMetrics();
00072 clearSelectionArea();
00073 setSelectionArea(new QRegion(x,
00074 y - fm.height(),
00075 fm.width(label, -1),
00076 fm.height(),
00077 QRegion::Rectangle));
00078
00079 if (getStatus() == COBase::Selected)
00080 {
00081 clearGrabHandles();
00082
00083 setGrabHandle(new QRegion(x - HANDLE_WIDTH - 1,
00084 y - (fm.height() / 2),
00085 HANDLE_WIDTH,
00086 HANDLE_WIDTH,
00087 QRegion::Rectangle));
00088
00089 painter.fillRect(x - HANDLE_WIDTH - 1,
00090 y - (fm.height() / 2),
00091 HANDLE_WIDTH,
00092 HANDLE_WIDTH,
00093 getColor());
00094 }
00095
00096 painter.end();
00097 }
00098
00099 void Text::prefDialog ()
00100 {
00101 QString pl = tr("Details");
00102 QString cl = tr("Color");
00103 QString sd = tr("Set Default");
00104 QString fl = tr("Font");
00105 QString ll = tr("Label");
00106 QString vl = tr("Value");
00107
00108 PrefDialog *dialog = new PrefDialog();
00109 dialog->setCaption(tr("Edit Text"));
00110 dialog->createPage (pl);
00111 dialog->setHelpFile (helpFile);
00112 dialog->addColorPrefItem(cl, pl, color);
00113 dialog->addFontItem(fl, pl, font);
00114 dialog->addTextItem(ll, pl, label);
00115 dialog->addDoubleItem(vl, pl, getValue());
00116 dialog->addCheckItem(sd, pl, FALSE);
00117
00118 int rc = dialog->exec();
00119
00120 if (rc == QDialog::Accepted)
00121 {
00122 dialog->getColor(cl, color);
00123 dialog->getText(ll, label);
00124 dialog->getFont(fl, font);
00125 value = dialog->getDouble(vl);
00126
00127 setSaveFlag(TRUE);
00128
00129 bool f = dialog->getCheck(sd);
00130 if (f)
00131 {
00132 dialog->getColor(cl, defaultColor);
00133 dialog->getFont(fl, dfont);
00134 saveDefaults();
00135 }
00136
00137 emit signalDraw();
00138 }
00139
00140 delete dialog;
00141 }
00142
00143 void Text::newObject (QString &ind, QString &n)
00144 {
00145 indicator = ind;
00146 plot = ind;
00147 name = n;
00148 status = ClickWait;
00149 emit message(tr("Select point to place Text..."));
00150 }
00151
00152 COBase::Status Text::pointerClick (QPoint &point, QDateTime &x, double y)
00153 {
00154 switch (status)
00155 {
00156 case None:
00157 if (isSelected(point))
00158 {
00159 status = Selected;
00160 emit signalDraw();
00161 }
00162 break;
00163 case Selected:
00164 if (isGrabSelected(point))
00165 status = Moving;
00166 else
00167 {
00168 if (! isSelected(point))
00169 {
00170 status = None;
00171 emit signalDraw();
00172 }
00173 }
00174 break;
00175 case Moving:
00176 status = Selected;
00177 break;
00178 case ClickWait:
00179 setDate(x);
00180 setValue(y);
00181 setSaveFlag(TRUE);
00182 setColor(defaultColor);
00183 emit signalDraw();
00184 status = None;
00185 emit message("");
00186 emit signalSave(name);
00187 break;
00188 default:
00189 break;
00190 }
00191
00192 return status;
00193 }
00194
00195 void Text::pointerMoving (QPixmap &, QPoint &, QDateTime &x, double y)
00196 {
00197 if (status != Moving)
00198 return;
00199
00200 setDate(x);
00201 setValue(y);
00202 setSaveFlag(TRUE);
00203 emit signalDraw();
00204 QString s = x.toString("yyyy-MM-dd hh:mm:ss") + " " + QString::number(y);
00205 emit message(s);
00206 }
00207
00208 void Text::loadDefaults ()
00209 {
00210 QSettings settings;
00211
00212 QString s = "/Qtstalker/DefaultTextColor";
00213 s = settings.readEntry(s);
00214 if (s.length())
00215 defaultColor.setNamedColor(s);
00216
00217 s = "/Qtstalker/DefaultTextFont";
00218 s = settings.readEntry(s);
00219 if (s.length())
00220 {
00221 QStringList l = QStringList::split(",", s, FALSE);
00222 font = QFont(l[0], l[1].toInt(), l[2].toInt());
00223 }
00224 }
00225
00226 void Text::saveDefaults ()
00227 {
00228 QSettings settings;
00229
00230 QString s = "/Qtstalker/DefaultTextColor";
00231 settings.writeEntry(s, defaultColor.name());
00232
00233 s = "/Qtstalker/DefaultTextFont";
00234 settings.writeEntry(s, dfont.family() + "," +
00235 QString::number(dfont.pointSize()) + "," +
00236 QString::number(dfont.weight()));
00237 }
00238
00239 void Text::getSettings (Setting &set)
00240 {
00241 QString s = date.toString(dateFormat);
00242 set.setData(dateLabel, s);
00243 s = QString::number(value);
00244 set.setData(valueLabel, s);
00245 s = color.name();
00246 set.setData(colorLabel, s);
00247 set.setData(plotLabel, plot);
00248 set.setData(nameLabel, name);
00249 set.setData(labelLabel, label);
00250 set.setData(typeLabel, type);
00251
00252 s = font.family();
00253 s.append(" ");
00254 s.append(QString::number(font.pointSize()));
00255 s.append(" ");
00256 s.append(QString::number(font.weight()));
00257 set.setData(fontLabel, s);
00258 }
00259
00260 void Text::setSettings (Setting &set)
00261 {
00262 QString s;
00263 set.getData(dateLabel, s);
00264 Bar bar;
00265 bar.setDate(s);
00266 bar.getDate(date);
00267 value = set.getDouble(valueLabel);
00268 set.getData(colorLabel, s);
00269 color.setNamedColor(s);
00270 set.getData(plotLabel, plot);
00271 set.getData(nameLabel, name);
00272 set.getData(labelLabel, label);
00273
00274 set.getData(fontLabel, s);
00275 QStringList l = QStringList::split(" ", s, FALSE);
00276 if (l.count())
00277 font = QFont(l[0], l[1].toInt(), l[2].toInt());
00278 }
00279
00280 void Text::getFont (QFont &d)
00281 {
00282 d = font;
00283 }
00284
00285 void Text::getLabel (QString &d)
00286 {
00287 d = label;
00288 }
00289
00290 void Text::adjustForSplit (QDateTime &dt, double d)
00291 {
00292 if (date < dt)
00293 value = value * d;
00294 }
00295