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