00001
00002
00003
00004
00005
00006
00007 #include "CmdEditPointAxis.h"
00008 #include "CmdEditPointGraph.h"
00009 #include "CmdMediator.h"
00010 #include "CmdMoveBy.h"
00011 #include "DataKey.h"
00012 #include "DigitizeStateContext.h"
00013 #include "DigitizeStateSelect.h"
00014 #include "DlgEditPointAxis.h"
00015 #include "DlgEditPointGraph.h"
00016 #include "EngaugeAssert.h"
00017 #include "GraphicsItemsExtractor.h"
00018 #include "GraphicsItemType.h"
00019 #include "GraphicsScene.h"
00020 #include "GraphicsView.h"
00021 #include "Logger.h"
00022 #include "MainWindow.h"
00023 #include <QCursor>
00024 #include <QGraphicsItem>
00025 #include <QImage>
00026 #include <QMessageBox>
00027 #include <QObject>
00028 #include <QtToString.h>
00029 #include "Version.h"
00030
00031 const QString MOVE_TEXT_DOWN (QObject::tr ("Move down"));
00032 const QString MOVE_TEXT_LEFT (QObject::tr ("Move left"));
00033 const QString MOVE_TEXT_RIGHT (QObject::tr ("Move right"));
00034 const QString MOVE_TEXT_UP (QObject::tr ("Move up"));
00035
00036 DigitizeStateSelect::DigitizeStateSelect (DigitizeStateContext &context) :
00037 DigitizeStateAbstractBase (context)
00038 {
00039 }
00040
00041 DigitizeStateSelect::~DigitizeStateSelect ()
00042 {
00043 }
00044
00045 QString DigitizeStateSelect::activeCurve () const
00046 {
00047 return context().mainWindow().selectedGraphCurve();
00048 }
00049
00050 void DigitizeStateSelect::addHoverHighlighting()
00051 {
00052 LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSelect::addHoverHighlighting";
00053
00054 QList<QGraphicsItem*> items = context().mainWindow().scene().items();
00055 QList<QGraphicsItem*>::iterator itr;
00056 for (itr = items.begin (); itr != items.end (); itr++) {
00057
00058 QGraphicsItem *item = *itr;
00059 if (item->data (DATA_KEY_GRAPHICS_ITEM_TYPE) == GRAPHICS_ITEM_TYPE_POINT) {
00060 item->setAcceptHoverEvents(true);
00061 }
00062 }
00063 }
00064
00065 void DigitizeStateSelect::begin (CmdMediator *cmdMediator,
00066 DigitizeState )
00067 {
00068 LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSelect::begin";
00069
00070 setCursor(cmdMediator);
00071 context().setDragMode(QGraphicsView::RubberBandDrag);
00072
00073 addHoverHighlighting();
00074 context().mainWindow().updateViewsOfSettings(activeCurve ());
00075 }
00076
00077 QCursor DigitizeStateSelect::cursor(CmdMediator * ) const
00078 {
00079 LOG4CPP_DEBUG_S ((*mainCat)) << "DigitizeStateSelect::cursor";
00080
00081 return QCursor (Qt::ArrowCursor);
00082 }
00083
00084 void DigitizeStateSelect::end ()
00085 {
00086 LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSelect::end";
00087
00088 removeHoverHighlighting();
00089 }
00090
00091 void DigitizeStateSelect::handleContextMenuEventAxis (CmdMediator *cmdMediator,
00092 const QString &pointIdentifier)
00093 {
00094 LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSelect::handleContextMenuEventAxis "
00095 << " point=" << pointIdentifier.toLatin1 ().data ();
00096
00097 QPointF posScreen = cmdMediator->document().positionScreen (pointIdentifier);
00098 QPointF posGraphBefore = cmdMediator->document().positionGraph (pointIdentifier);
00099 bool isXOnly = cmdMediator->document().isXOnly (pointIdentifier);
00100
00101
00102 double x = posGraphBefore.x();
00103 double y = posGraphBefore.y();
00104
00105 DlgEditPointAxis *dlg = new DlgEditPointAxis (context().mainWindow(),
00106 cmdMediator->document().modelCoords(),
00107 cmdMediator->document().modelGeneral(),
00108 context().mainWindow().modelMainWindow(),
00109 context().mainWindow().transformation(),
00110 cmdMediator->document().documentAxesPointsRequired(),
00111 isXOnly,
00112 &x,
00113 &y);
00114 int rtn = dlg->exec ();
00115
00116 QPointF posGraphAfter = dlg->posGraph (isXOnly);
00117 delete dlg;
00118
00119 if (rtn == QDialog::Accepted) {
00120
00121
00122
00123 bool isError;
00124 QString errorMessage;
00125
00126 context().mainWindow().cmdMediator()->document().checkEditPointAxis(pointIdentifier,
00127 posScreen,
00128 posGraphAfter,
00129 isError,
00130 errorMessage);
00131
00132 if (isError) {
00133
00134 QMessageBox::warning (0,
00135 engaugeWindowTitle(),
00136 errorMessage);
00137
00138 } else {
00139
00140
00141 CmdEditPointAxis *cmd = new CmdEditPointAxis (context().mainWindow(),
00142 cmdMediator->document(),
00143 pointIdentifier,
00144 posGraphBefore,
00145 posGraphAfter,
00146 isXOnly);
00147 context().appendNewCmd(cmdMediator,
00148 cmd);
00149 }
00150 }
00151 }
00152
00153 void DigitizeStateSelect::handleContextMenuEventGraph (CmdMediator *cmdMediator,
00154 const QStringList &pointIdentifiers)
00155 {
00156 LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSelect::handleContextMenuEventGraph "
00157 << "points=" << pointIdentifiers.join(",").toLatin1 ().data ();
00158
00159 double *x = 0, *y = 0;
00160
00161 if (pointIdentifiers.count() == 1) {
00162
00163
00164 x = new double;
00165 y = new double;
00166
00167 QPointF posScreenBefore = cmdMediator->document().positionScreen (pointIdentifiers.first());
00168 QPointF posGraphBefore;
00169 context().mainWindow().transformation().transformScreenToRawGraph (posScreenBefore,
00170 posGraphBefore);
00171
00172
00173 *x = posGraphBefore.x();
00174 *y = posGraphBefore.y();
00175 }
00176
00177 DlgEditPointGraph *dlg = new DlgEditPointGraph (context().mainWindow(),
00178 cmdMediator->document().modelCoords(),
00179 cmdMediator->document().modelGeneral(),
00180 context().mainWindow().modelMainWindow(),
00181 context().mainWindow().transformation(),
00182 x,
00183 y);
00184 if (x != 0) {
00185 delete x;
00186 x = 0;
00187 }
00188
00189 if (y != 0) {
00190 delete y;
00191 y = 0;
00192 }
00193
00194 int rtn = dlg->exec ();
00195
00196 bool isXGiven, isYGiven;
00197 double xGiven, yGiven;
00198 dlg->posGraph (isXGiven, xGiven, isYGiven, yGiven);
00199 delete dlg;
00200
00201 if (rtn == QDialog::Accepted) {
00202
00203
00204 CmdEditPointGraph *cmd = new CmdEditPointGraph (context().mainWindow(),
00205 cmdMediator->document(),
00206 pointIdentifiers,
00207 isXGiven,
00208 isYGiven,
00209 xGiven,
00210 yGiven);
00211 context().appendNewCmd(cmdMediator,
00212 cmd);
00213 }
00214 }
00215
00216 void DigitizeStateSelect::handleCurveChange(CmdMediator * )
00217 {
00218 LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSelect::handleCurveChange";
00219 }
00220
00221 void DigitizeStateSelect::handleKeyPress (CmdMediator *cmdMediator,
00222 Qt::Key key,
00223 bool atLeastOneSelectedItem)
00224 {
00225 LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSelect::handleKeyPress"
00226 << " key=" << QKeySequence (key).toString ().toLatin1 ().data ();
00227
00228 if (atLeastOneSelectedItem) {
00229
00230 if (key == Qt::Key_Down ||
00231 key == Qt::Key_Up ||
00232 key == Qt::Key_Left ||
00233 key == Qt::Key_Right) {
00234
00235 keyPressArrow (cmdMediator,
00236 key);
00237
00238 }
00239 }
00240 }
00241
00242 void DigitizeStateSelect::handleMouseMove (CmdMediator * ,
00243 QPointF )
00244 {
00245
00246 }
00247
00248 void DigitizeStateSelect::handleMousePress (CmdMediator * ,
00249 QPointF posScreen)
00250 {
00251 LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSelect::handleMousePress"
00252 << " posScreen=" << QPointFToString (posScreen).toLatin1 ().data ();
00253
00254
00255
00256 m_movingStart = posScreen;
00257 }
00258
00259 void DigitizeStateSelect::handleMouseRelease (CmdMediator *cmdMediator,
00260 QPointF posScreen)
00261 {
00262 LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSelect::handleMouseRelease"
00263 << " posScreen=" << QPointFToString (posScreen).toLatin1 ().data ();
00264
00265 QPointF deltaScreen = posScreen - m_movingStart;
00266 QStringList positionHasChangedIdentifers = context().mainWindow().scene().positionHasChangedPointIdentifiers();
00267
00268 bool positionHasChanged = (positionHasChangedIdentifers.count () > 0);
00269
00270 if (positionHasChanged && (
00271 deltaScreen.x () != 0 ||
00272 deltaScreen.y () != 0)) {
00273
00274 QString moveText = moveTextFromDeltaScreen (deltaScreen);
00275
00276
00277 CmdMoveBy *cmd = new CmdMoveBy (context().mainWindow(),
00278 cmdMediator->document(),
00279 deltaScreen,
00280 moveText,
00281 positionHasChangedIdentifers);
00282 context().appendNewCmd (cmdMediator,
00283 cmd);
00284
00285 } else {
00286
00287
00288 context().mainWindow().updateAfterMouseRelease();
00289
00290 }
00291 }
00292
00293 void DigitizeStateSelect::keyPressArrow (CmdMediator *cmdMediator,
00294 Qt::Key key)
00295 {
00296 QPointF deltaScreen;
00297 QString moveText;
00298 switch (key) {
00299 case Qt::Key_Down:
00300 deltaScreen = QPointF (0, zoomedToUnzoomedScreenY ());
00301 moveText = MOVE_TEXT_DOWN;
00302 break;
00303
00304 case Qt::Key_Left:
00305 deltaScreen = QPointF (-1 * zoomedToUnzoomedScreenX (), 0);
00306 moveText = MOVE_TEXT_LEFT;
00307 break;
00308
00309 case Qt::Key_Right:
00310 deltaScreen = QPointF (zoomedToUnzoomedScreenX (), 0);
00311 moveText = MOVE_TEXT_RIGHT;
00312 break;
00313
00314 case Qt::Key_Up:
00315 deltaScreen = QPointF (0, -1 * zoomedToUnzoomedScreenY ());
00316 moveText = MOVE_TEXT_UP;
00317 break;
00318
00319 default:
00320 ENGAUGE_ASSERT (false);
00321 }
00322
00323
00324 GraphicsItemsExtractor graphicsItemsExtractor;
00325 const QList<QGraphicsItem*> &items = context().mainWindow().scene ().selectedItems();
00326 CmdMoveBy *cmd = new CmdMoveBy (context().mainWindow(),
00327 cmdMediator->document(),
00328 deltaScreen,
00329 moveText,
00330 graphicsItemsExtractor.selectedPointIdentifiers (items));
00331 context().appendNewCmd (cmdMediator,
00332 cmd);
00333 }
00334
00335 QString DigitizeStateSelect::moveTextFromDeltaScreen (const QPointF &deltaScreen)
00336 {
00337 QString moveText;
00338
00339
00340
00341
00342
00343
00344 bool downOrRight = (deltaScreen.y () > -1.0 * deltaScreen.x ());
00345 bool upOrRight = (deltaScreen.y () < deltaScreen.x ());
00346 if (downOrRight && upOrRight) {
00347 moveText = MOVE_TEXT_RIGHT;
00348 } else if (downOrRight && !upOrRight) {
00349 moveText = MOVE_TEXT_DOWN;
00350 } else if (!downOrRight && upOrRight) {
00351 moveText = MOVE_TEXT_UP;
00352 } else {
00353 moveText = MOVE_TEXT_LEFT;
00354 }
00355
00356 return moveText;
00357 }
00358
00359 void DigitizeStateSelect::removeHoverHighlighting()
00360 {
00361 LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSelect::removeHoverHighlighting";
00362
00363 QList<QGraphicsItem*> items = context().mainWindow().scene().items();
00364 QList<QGraphicsItem*>::iterator itr;
00365 for (itr = items.begin (); itr != items.end (); itr++) {
00366
00367 QGraphicsItem *item = *itr;
00368 if (item->data (DATA_KEY_GRAPHICS_ITEM_TYPE) == GRAPHICS_ITEM_TYPE_POINT) {
00369 item->setAcceptHoverEvents(false);
00370 }
00371 }
00372 }
00373
00374 void DigitizeStateSelect::setHoverHighlighting(const MainWindowModel &modelMainWindow)
00375 {
00376 LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSelect::addHoverHighlighting";
00377
00378
00379 QList<QGraphicsItem*> items = context().mainWindow().scene().items();
00380 QList<QGraphicsItem*>::iterator itr;
00381 for (itr = items.begin (); itr != items.end (); itr++) {
00382
00383 QGraphicsItem *item = *itr;
00384 if (item->data (DATA_KEY_GRAPHICS_ITEM_TYPE) == GRAPHICS_ITEM_TYPE_POINT) {
00385 item->setOpacity (modelMainWindow.highlightOpacity());
00386 }
00387 }
00388 }
00389
00390 QString DigitizeStateSelect::state() const
00391 {
00392 return "DigitizeStateSelect";
00393 }
00394
00395 void DigitizeStateSelect::updateAfterPointAddition ()
00396 {
00397 LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSelect::updateAfterPointAddition";
00398
00399 addHoverHighlighting ();
00400 }
00401
00402 void DigitizeStateSelect::updateModelDigitizeCurve (CmdMediator * ,
00403 const DocumentModelDigitizeCurve & )
00404 {
00405 LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSelect::updateModelDigitizeCurve";
00406 }
00407
00408 void DigitizeStateSelect::updateModelSegments(const DocumentModelSegments & )
00409 {
00410 LOG4CPP_INFO_S ((*mainCat)) << "DigitizeStateSelect::updateModelSegments";
00411 }
00412
00413 double DigitizeStateSelect::zoomedToUnzoomedScreenX () const
00414 {
00415 double m11 = context().mainWindow ().view ().transform().m11 ();
00416 return 1.0 / m11;
00417 }
00418
00419 double DigitizeStateSelect::zoomedToUnzoomedScreenY () const
00420 {
00421 double m22 = context().mainWindow ().view ().transform().m22 ();
00422 return 1.0 / m22;
00423 }