From: Shamus Hammons Date: Mon, 7 Nov 2011 18:14:46 +0000 (+0000) Subject: Preliminary Add Line tool work... X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fc498c685147e3173130e2df8953cc52705553e9;p=architektonas Preliminary Add Line tool work... --- diff --git a/architektonas.pro b/architektonas.pro index 5d00725..e16b0db 100644 --- a/architektonas.pro +++ b/architektonas.pro @@ -42,12 +42,14 @@ DEPENDPATH = \ HEADERS = \ src/about.h \ + src/action.h \ src/applicationwindow.h \ src/arc.h \ src/circle.h \ src/container.h \ src/dimension.h \ src/drawingview.h \ + src/drawlineaction.h \ src/generaltab.h \ src/line.h \ src/main.h \ @@ -59,12 +61,14 @@ HEADERS = \ SOURCES = \ src/about.cpp \ + src/action.cpp \ src/applicationwindow.cpp \ src/arc.cpp \ src/circle.cpp \ src/container.cpp \ src/dimension.cpp \ src/drawingview.cpp \ + src/drawlineaction.cpp \ src/generaltab.cpp \ src/line.cpp \ src/main.cpp \ diff --git a/src/action.cpp b/src/action.cpp new file mode 100644 index 0000000..d286299 --- /dev/null +++ b/src/action.cpp @@ -0,0 +1,22 @@ +// action.cpp: Action base class +// +// Part of the Architektonas Project +// (C) 2011 Underground Software +// See the README and GPLv3 files for licensing and warranty information +// +// JLH = James L. Hammons +// +// WHO WHEN WHAT +// --- ---------- ------------------------------------------------------------ +// JLH 10/04/2011 Created this file +// + +#include "action.h" + +Action::Action(): QObject() +{ +} + +Action::~Action() +{ +} diff --git a/src/action.h b/src/action.h new file mode 100644 index 0000000..6565f69 --- /dev/null +++ b/src/action.h @@ -0,0 +1,29 @@ +#ifndef __ACTION_H__ +#define __ACTION_H__ + +#include +#include "vector.h" + +class Object; +class Painter; + +class Action: public QObject +{ + Q_OBJECT + + public: + Action(); + ~Action(); + + // These are all pure virtual functions: Derived classes must define + // ALL of them. + virtual void Draw(Painter *) = 0; + virtual void MouseDown(Vector) = 0; + virtual void MouseMoved(Vector) = 0; + virtual void MouseReleased(void) = 0; + + signals: + void ObjectReady(Object *); +}; + +#endif // __ACTION_H__ diff --git a/src/applicationwindow.cpp b/src/applicationwindow.cpp index 3ade284..2337417 100644 --- a/src/applicationwindow.cpp +++ b/src/applicationwindow.cpp @@ -103,6 +103,15 @@ void ApplicationWindow::RotateTool(void) drawing->SetRotateToolActive(rotateAct->isChecked()); } +void ApplicationWindow::AddLineTool(void) +{ + addCircleAct->setChecked(false); + addArcAct->setChecked(false); + rotateAct->setChecked(false); + RotateTool(); + drawing->SetAddLineToolActive(addLineAct->isChecked()); +} + void ApplicationWindow::ZoomInTool(void) { double zoomFactor = 2.0; @@ -204,11 +213,12 @@ void ApplicationWindow::CreateActions(void) addDimensionAct = CreateAction(tr("Add &Dimension"), tr("Add Dimension"), tr("Adds a dimension to the drawing."), QIcon(":/res/dimension-tool.png"), QKeySequence("D,I"), true); connect(addDimensionAct, SIGNAL(triggered()), this, SLOT(DimensionTool())); - addLineAct = CreateAction(tr("Add &Line"), tr("Add Line"), tr("Adds a line to the drawing."), QIcon(":/res/generic-tool.png"), QKeySequence(), true); + addLineAct = CreateAction(tr("Add &Line"), tr("Add Line"), tr("Adds lines to the drawing."), QIcon(":/res/generic-tool.png"), QKeySequence("A,L"), true); + connect(addLineAct, SIGNAL(triggered()), this, SLOT(AddLineTool())); - addCircleAct = CreateAction(tr("Add &Circle"), tr("Add Circle"), tr("Adds a circle to the drawing."), QIcon(":/res/generic-tool.png"), QKeySequence(), true); + addCircleAct = CreateAction(tr("Add &Circle"), tr("Add Circle"), tr("Adds circles to the drawing."), QIcon(":/res/generic-tool.png"), QKeySequence("A,C"), true); - addArcAct = CreateAction(tr("Add &Arc"), tr("Add Arc"), tr("Adds an arc to the drawing."), QIcon(":/res/generic-tool.png"), QKeySequence(), true); + addArcAct = CreateAction(tr("Add &Arc"), tr("Add Arc"), tr("Adds arcs to the drawing."), QIcon(":/res/generic-tool.png"), QKeySequence("A,A"), true); aboutAct = CreateAction(tr("About &Architektonas"), tr("About Architektonas"), tr("Gives information about this program."), QIcon(":/res/generic-tool.png"), QKeySequence()); connect(aboutAct, SIGNAL(triggered()), this, SLOT(HelpAbout())); @@ -216,7 +226,7 @@ void ApplicationWindow::CreateActions(void) rotateAct = CreateAction(tr("&Rotate Objects"), tr("Rotate"), tr("Rotate object(s) around an arbitrary center."), QIcon(":/res/generic-tool.png"), QKeySequence(tr("R,O")), true); connect(rotateAct, SIGNAL(triggered()), this, SLOT(RotateTool())); - zoomInAct = CreateAction2(tr("Zoom &In"), tr("Zoom In"), tr("Zoom in on the document."), QIcon(":/res/zoom-in.png"), QKeySequence(tr("+")), QKeySequence(tr("="))); + zoomInAct = CreateAction(tr("Zoom &In"), tr("Zoom In"), tr("Zoom in on the document."), QIcon(":/res/zoom-in.png"), QKeySequence(tr("+")), QKeySequence(tr("="))); connect(zoomInAct, SIGNAL(triggered()), this, SLOT(ZoomInTool())); zoomOutAct = CreateAction(tr("Zoom &Out"), tr("Zoom Out"), tr("Zoom out of the document."), QIcon(":/res/zoom-out.png"), QKeySequence(tr("-"))); @@ -263,7 +273,7 @@ QAction * ApplicationWindow::CreateAction(QString name, QString tooltip, QString // This is essentially the same as the previous function, but this allows more // than one key sequence to be added as key shortcuts. // -QAction * ApplicationWindow::CreateAction2(QString name, QString tooltip, QString statustip, +QAction * ApplicationWindow::CreateAction(QString name, QString tooltip, QString statustip, QIcon icon, QKeySequence key1, QKeySequence key2, bool checkable/*= false*/) { QAction * action = new QAction(icon, name, this); diff --git a/src/applicationwindow.h b/src/applicationwindow.h index 199c4b2..a68cee9 100644 --- a/src/applicationwindow.h +++ b/src/applicationwindow.h @@ -28,6 +28,7 @@ class ApplicationWindow: public QMainWindow void DeleteTool(void); void DimensionTool(void); void RotateTool(void); + void AddLineTool(void); void ZoomInTool(void); void ZoomOutTool(void); void HelpAbout(void); @@ -37,7 +38,7 @@ class ApplicationWindow: public QMainWindow void CreateActions(void); QAction * CreateAction(QString name, QString tooltip, QString statustip, QIcon icon, QKeySequence key, bool checkable = false); - QAction * CreateAction2(QString name, QString tooltip, QString statustip, + QAction * CreateAction(QString name, QString tooltip, QString statustip, QIcon icon, QKeySequence key1, QKeySequence key2, bool checkable = false); void CreateMenus(void); void CreateToolbars(void); diff --git a/src/drawingview.cpp b/src/drawingview.cpp index 4a71fa6..a214f02 100644 --- a/src/drawingview.cpp +++ b/src/drawingview.cpp @@ -34,6 +34,7 @@ #include "arc.h" #include "circle.h" #include "dimension.h" +#include "drawlineaction.h" #include "line.h" #include "painter.h" @@ -44,7 +45,7 @@ DrawingView::DrawingView(QWidget * parent/*= NULL*/): QWidget(parent), scale(1.0), offsetX(-10), offsetY(-10), document(Vector(0, 0)), gridSpacing(32.0), collided(false), rotateTool(false), rx(150.0), ry(150.0), - scrollDrag(false) + scrollDrag(false), addLineTool(false), toolAction(NULL) { setBackgroundRole(QPalette::Base); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); @@ -70,6 +71,8 @@ DrawingView::DrawingView(QWidget * parent/*= NULL*/): QWidget(parent), // Alternate way to do the above... line->SetDimensionOnLine(); #endif +// connect(toolAction, SIGNAL(ObjectReady(Object *)), this, +// SLOT(AddNewObjectToDocument(Object *))); } void DrawingView::SetRotateToolActive(bool state/*= true*/) @@ -78,6 +81,36 @@ void DrawingView::SetRotateToolActive(bool state/*= true*/) update(); } +void DrawingView::SetAddLineToolActive(bool state/*= true*/) +{ + if (state && toolAction == NULL) + { + toolAction = new DrawLineAction(); + connect(toolAction, SIGNAL(ObjectReady(Object *)), this, + SLOT(AddNewObjectToDocument(Object *))); + } + else if (!state && toolAction) + { + delete toolAction; + toolAction = NULL; + } + + addLineTool = state; + update(); +//printf("DrawingView::SetAddLineToolActive(). toolAction=%08X\n", toolAction); +} + +void DrawingView::AddNewObjectToDocument(Object * object) +{ + if (object) + { + object->Reparent(&document); + document.Add(object); + update(); + } +//printf("DrawingView::AddNewObjectToDocument(). object=%08X\n", object); +} + QPoint DrawingView::GetAdjustedMousePosition(QMouseEvent * event) { // This is undoing the transform, e.g. going from client coords to local coords. @@ -144,6 +177,9 @@ void DrawingView::paintEvent(QPaintEvent * /*event*/) // The top level document takes care of rendering for us... document.Draw(&painter); + + if (toolAction) + toolAction->Draw(&painter); } void DrawingView::mousePressEvent(QMouseEvent * event) @@ -155,6 +191,9 @@ void DrawingView::mousePressEvent(QMouseEvent * event) if (collided) update(); // Do an update if collided with at least *one* object in the document + + if (toolAction) + toolAction->MouseDown(point); } else if (event->button() == Qt::MiddleButton) { @@ -173,7 +212,7 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event) { point = Vector(event->x(), event->y()); // Since we're using Qt coords for scrolling, we have to adjust them here to - // conform to Cartesian coords, since the origin is using them. :-) + // conform to Cartesian coords, since the origin is using Cartesian. :-) Vector delta(point, oldPoint); delta /= Painter::zoom; delta.y = -delta.y; @@ -207,6 +246,12 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event) if (document.NeedsUpdate()) update(); + + if (toolAction) + { + toolAction->MouseMoved(point); + update(); + } } void DrawingView::mouseReleaseEvent(QMouseEvent * event) @@ -221,6 +266,9 @@ void DrawingView::mouseReleaseEvent(QMouseEvent * event) // if (document.NeedsUpdate()) // if (collided) update(); // Do an update if collided with at least *one* object in the document + + if (toolAction) + toolAction->MouseReleased(); } else if (event->button() == Qt::MiddleButton) { diff --git a/src/drawingview.h b/src/drawingview.h index 7d4301e..e8dc3af 100644 --- a/src/drawingview.h +++ b/src/drawingview.h @@ -3,6 +3,7 @@ #include #include +#include "action.h" #include "container.h" class DrawingView: public QWidget @@ -14,6 +15,10 @@ class DrawingView: public QWidget public: void SetRotateToolActive(bool state = true); + void SetAddLineToolActive(bool state = true); + + public slots: + void AddNewObjectToDocument(Object *); protected: void paintEvent(QPaintEvent * event); @@ -39,6 +44,8 @@ class DrawingView: public QWidget double rx, ry; bool scrollDrag; Vector oldPoint; + bool addLineTool; + Action * toolAction; /* QSize minimumSizeHint() const; QSize sizeHint() const; diff --git a/src/drawlineaction.cpp b/src/drawlineaction.cpp new file mode 100644 index 0000000..867970d --- /dev/null +++ b/src/drawlineaction.cpp @@ -0,0 +1,76 @@ +// drawlineaction.cpp: Action class for drawing lines +// +// Part of the Architektonas Project +// (C) 2011 Underground Software +// See the README and GPLv3 files for licensing and warranty information +// +// JLH = James L. Hammons +// +// WHO WHEN WHAT +// --- ---------- ------------------------------------------------------------ +// JLH 10/04/2011 Created this file +// JLH 11/07/2011 Made it so this class actually does something ;-) +// + +#include "drawlineaction.h" +#include "line.h" +#include "painter.h" +//#include "vector.h" + + +#define FIRST_POINT 0 +#define NEXT_POINT 1 + + +DrawLineAction::DrawLineAction(): state(0), line(NULL) +{ +} + +DrawLineAction::~DrawLineAction() +{ +} + + +/*virtual*/ void DrawLineAction::Draw(Painter * painter) +{ + // Need to fix pen colors, etc... + if (state == FIRST_POINT) + { + painter->DrawPoint(p1.x, p1.y); + } + else + { + painter->DrawLine(p1, p2); + } +} + +/*virtual*/ void DrawLineAction::MouseDown(Vector point) +{ + if (state == FIRST_POINT) + p1 = point; + else + p2 = point; +} + +/*virtual*/ void DrawLineAction::MouseMoved(Vector point) +{ + if (state == FIRST_POINT) + p1 = point; + else + p2 = point; +} + +/*virtual*/ void DrawLineAction::MouseReleased(void) +{ + if (state == FIRST_POINT) + state = NEXT_POINT; + else if (state == NEXT_POINT) + { + // We create the new object here, and then pass it off to the document. + line = new Line(p1, p2); + // We don't need no stinkin' sentinels, when we have signals & slots! + emit ObjectReady(line); + + p1 = p2; + } +} diff --git a/src/drawlineaction.h b/src/drawlineaction.h new file mode 100644 index 0000000..0736d30 --- /dev/null +++ b/src/drawlineaction.h @@ -0,0 +1,25 @@ +#ifndef __DRAWLINEACTION_H__ +#define __DRAWLINEACTION_H__ + +#include "action.h" + +class Line; + +class DrawLineAction: public Action +{ + public: + DrawLineAction(); + ~DrawLineAction(); + + virtual void Draw(Painter *); + virtual void MouseDown(Vector); + virtual void MouseMoved(Vector); + virtual void MouseReleased(void); + + private: + int state; + Line * line; + Vector p1, p2; +}; + +#endif // __DRAWLINEACTION_H__ diff --git a/src/object.cpp b/src/object.cpp index ae20db6..e38e661 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -86,6 +86,11 @@ ObjectState Object::GetState(void) return state; } +void Object::Reparent(Object * newParent) +{ + parent = newParent; +} + // Class methods... void Object::SetFixedAngle(bool state/*= true*/) diff --git a/src/object.h b/src/object.h index 0c89654..b6f9558 100644 --- a/src/object.h +++ b/src/object.h @@ -27,6 +27,7 @@ class Object virtual Object * GetParent(void); virtual void Add(Object *); ObjectState GetState(void); + void Reparent(Object *); //Hm. Object * Connect(Object *); // Class methods diff --git a/src/painter.cpp b/src/painter.cpp index c0d2c4d..8fe59ab 100644 --- a/src/painter.cpp +++ b/src/painter.cpp @@ -203,4 +203,3 @@ void Painter::DrawText(QRectF rect, int type, QString text) painter->drawText(rect, (Qt::AlignmentFlag)type, text); } -