]> Shamusworld >> Repos - architektonas/commitdiff
Preliminary Add Line tool work...
authorShamus Hammons <jlhamm@acm.org>
Mon, 7 Nov 2011 18:14:46 +0000 (18:14 +0000)
committerShamus Hammons <jlhamm@acm.org>
Mon, 7 Nov 2011 18:14:46 +0000 (18:14 +0000)
12 files changed:
architektonas.pro
src/action.cpp [new file with mode: 0644]
src/action.h [new file with mode: 0644]
src/applicationwindow.cpp
src/applicationwindow.h
src/drawingview.cpp
src/drawingview.h
src/drawlineaction.cpp [new file with mode: 0644]
src/drawlineaction.h [new file with mode: 0644]
src/object.cpp
src/object.h
src/painter.cpp

index 5d00725a32059260d3aa892b5d7c636f061a04cd..e16b0dbd8b490f024202f88c3c28cd5c958b1a7e 100644 (file)
@@ -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 (file)
index 0000000..d286299
--- /dev/null
@@ -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 <jlhamm@acm.org>
+//
+// 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 (file)
index 0000000..6565f69
--- /dev/null
@@ -0,0 +1,29 @@
+#ifndef __ACTION_H__
+#define __ACTION_H__
+
+#include <QtCore>
+#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__
index 3ade28491bb30af8ec1faa3c62fc800875dfc7ae..23374171066c7ac90c2aa9f4d5e87ca641b777cf 100644 (file)
@@ -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);
index 199c4b23c735a81dc9245084b149800b3a8dc077..a68cee9c1880ecf93187187bc7413d8918b05df9 100644 (file)
@@ -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);
index 4a71fa6469a7160c644dddce86bd0a394927497e..a214f02fd901d4b0d339bd3dca571be9dd810044 100644 (file)
@@ -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)
        {
index 7d4301e81ef74603fbdfd21ac0aedc1d7f90cf0c..e8dc3af86c1522d61b2244f9f4bf7d6bd55e5bc1 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <QtGui>
 #include <stdint.h>
+#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 (file)
index 0000000..867970d
--- /dev/null
@@ -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 <jlhamm@acm.org>
+//
+// 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 (file)
index 0000000..0736d30
--- /dev/null
@@ -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__
index ae20db6a8cf6a1706e4776c96a49ba43a367e74d..e38e6614ba353d4f7cd8102abdc7a19657c0b70d 100644 (file)
@@ -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*/)
index 0c8965403a3437663e0ea1dbe68faaec6dda89cf..b6f9558e452b9386a80cab88f841f49651dfa88c 100644 (file)
@@ -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
index c0d2c4d2300843e80aaaee7a2b8b55e52b037a2f..8fe59ab9ad64720bd06d901b662d602ccf052c2c 100644 (file)
@@ -203,4 +203,3 @@ void Painter::DrawText(QRectF rect, int type, QString text)
 
        painter->drawText(rect, (Qt::AlignmentFlag)type, text);
 }
-