00001
00002
00003
00004
00005
00006
00007 #include "CmdMoveBy.h"
00008 #include "DataKey.h"
00009 #include "Document.h"
00010 #include "DocumentSerialize.h"
00011 #include "EngaugeAssert.h"
00012 #include "GraphicsItemType.h"
00013 #include "GraphicsView.h"
00014 #include "Logger.h"
00015 #include "MainWindow.h"
00016 #include <QGraphicsItem>
00017 #include <QtToString.h>
00018 #include <QXmlStreamReader>
00019 #include "Xml.h"
00020
00021 CmdMoveBy::CmdMoveBy(MainWindow &mainWindow,
00022 Document &document,
00023 const QPointF &deltaScreen,
00024 const QString &moveText,
00025 const QStringList &selectedPointIdentifiers) :
00026 CmdPointChangeBase (mainWindow,
00027 document,
00028 moveText),
00029 m_deltaScreen (deltaScreen)
00030 {
00031 QStringList selected;
00032 QStringList::const_iterator itr;
00033 for (itr = selectedPointIdentifiers.begin (); itr != selectedPointIdentifiers.end (); itr++) {
00034
00035 QString selectedPointIdentifier = *itr;
00036
00037 selected << selectedPointIdentifier;
00038 m_movedPoints.setKeyValue (selectedPointIdentifier, true);
00039 }
00040
00041 LOG4CPP_INFO_S ((*mainCat)) << "CmdMoveBy::CmdMoveBy"
00042 << " deltaScreen=" << QPointFToString (deltaScreen).toLatin1 ().data ()
00043 << " selected=" << selected.join (", ").toLatin1 ().data () << ")";
00044 }
00045
00046 CmdMoveBy::CmdMoveBy (MainWindow &mainWindow,
00047 Document &document,
00048 const QString &cmdDescription,
00049 QXmlStreamReader &reader) :
00050 CmdPointChangeBase (mainWindow,
00051 document,
00052 cmdDescription)
00053 {
00054 LOG4CPP_INFO_S ((*mainCat)) << "CmdMoveBy::CmdMoveBy";
00055
00056 QXmlStreamAttributes attributes = reader.attributes();
00057
00058 if (!attributes.hasAttribute(DOCUMENT_SERIALIZE_SCREEN_X_DELTA) ||
00059 !attributes.hasAttribute(DOCUMENT_SERIALIZE_SCREEN_Y_DELTA) ) {
00060 xmlExitWithError (reader,
00061 QString ("%1 %2 %3 %4")
00062 .arg (QObject::tr ("Missing attribute(s)"))
00063 .arg (DOCUMENT_SERIALIZE_SCREEN_X_DELTA)
00064 .arg (QObject::tr ("and/or"))
00065 .arg (DOCUMENT_SERIALIZE_SCREEN_Y_DELTA));
00066 }
00067
00068 m_deltaScreen.setX(attributes.value(DOCUMENT_SERIALIZE_SCREEN_X_DELTA).toDouble());
00069 m_deltaScreen.setY(attributes.value(DOCUMENT_SERIALIZE_SCREEN_Y_DELTA).toDouble());
00070 m_movedPoints.loadXml (reader);
00071 }
00072
00073 CmdMoveBy::~CmdMoveBy ()
00074 {
00075 }
00076
00077 void CmdMoveBy::cmdRedo ()
00078 {
00079 LOG4CPP_INFO_S ((*mainCat)) << "CmdMoveBy::cmdRedo"
00080 << " deltaScreen=" << QPointFToString (m_deltaScreen).toLatin1().data()
00081 << " moving=" << m_movedPoints.count ();
00082
00083 saveOrCheckPreCommandDocumentStateHash (document ());
00084 saveDocumentState (document ());
00085 moveBy (m_deltaScreen);
00086 mainWindow().updateAfterCommand();
00087 resetSelection(m_movedPoints);
00088 saveOrCheckPostCommandDocumentStateHash (document ());
00089 }
00090
00091 void CmdMoveBy::cmdUndo ()
00092 {
00093 LOG4CPP_INFO_S ((*mainCat)) << "CmdMoveBy::cmdUndo"
00094 << " deltaScreen=" << QPointFToString (-1.0 * m_deltaScreen).toLatin1().data()
00095 << " moving=" << m_movedPoints.count ();
00096
00097 saveOrCheckPostCommandDocumentStateHash (document ());
00098 restoreDocumentState (document ());
00099 mainWindow().updateAfterCommand();
00100 resetSelection(m_movedPoints);
00101 saveOrCheckPreCommandDocumentStateHash (document ());
00102 }
00103
00104 void CmdMoveBy::moveBy (const QPointF &deltaScreen)
00105 {
00106 LOG4CPP_INFO_S ((*mainCat)) << "CmdMoveBy::moveBy";
00107
00108
00109 for (int i = 0; i < m_movedPoints.count(); i++) {
00110
00111 QString pointIdentifier = m_movedPoints.getKey (i);
00112 document().movePoint (pointIdentifier, deltaScreen);
00113
00114 }
00115
00116
00117 QList<QGraphicsItem *> items = mainWindow().view().items();
00118 QList<QGraphicsItem *>::iterator itrS;
00119 for (itrS = items.begin (); itrS != items.end (); itrS++) {
00120
00121 QGraphicsItem *item = *itrS;
00122 if (item->data (DATA_KEY_GRAPHICS_ITEM_TYPE).toInt () == GRAPHICS_ITEM_TYPE_POINT) {
00123
00124 QString pointIdentifier = item->data (DATA_KEY_IDENTIFIER).toString ();
00125
00126 if (m_movedPoints.contains (pointIdentifier)) {
00127
00128
00129 QPointF posScreen = document().positionScreen (pointIdentifier);
00130
00131 if (item->pos () != posScreen) {
00132
00133
00134 item->setPos (posScreen);
00135 }
00136 }
00137 }
00138 }
00139
00140 document().updatePointOrdinals (mainWindow().transformation());
00141
00142
00143 mainWindow().updateGraphicsLinesToMatchGraphicsPoints();
00144 }
00145
00146 void CmdMoveBy::saveXml (QXmlStreamWriter &writer) const
00147 {
00148 writer.writeStartElement(DOCUMENT_SERIALIZE_CMD);
00149 writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_TYPE, DOCUMENT_SERIALIZE_CMD_MOVE_BY);
00150 writer.writeAttribute(DOCUMENT_SERIALIZE_CMD_DESCRIPTION, QUndoCommand::text ());
00151 writer.writeAttribute(DOCUMENT_SERIALIZE_SCREEN_X_DELTA, QString::number (m_deltaScreen.x()));
00152 writer.writeAttribute(DOCUMENT_SERIALIZE_SCREEN_Y_DELTA, QString::number (m_deltaScreen.y()));
00153 m_movedPoints.saveXml (writer);
00154 writer.writeEndElement();
00155 }