]> Shamusworld >> Repos - architektonas/commitdiff
Added preliminary zooming and panning.
authorShamus Hammons <jlhamm@acm.org>
Thu, 29 Sep 2011 17:16:39 +0000 (17:16 +0000)
committerShamus Hammons <jlhamm@acm.org>
Thu, 29 Sep 2011 17:16:39 +0000 (17:16 +0000)
src/applicationwindow.cpp
src/applicationwindow.h
src/drawingview.cpp
src/drawingview.h
src/vector.cpp
src/vector.h

index fe0c29db9fd1e2696e91266b40411f0071c3cbad..f658a9b1d69aaa5da0c30faf0a25027b41dcce6a 100644 (file)
@@ -28,8 +28,9 @@
 
 #include "about.h"
 #include "drawingview.h"
-#include "settingsdialog.h"
 #include "generaltab.h"
+#include "painter.h"
+#include "settingsdialog.h"
 
 
 ApplicationWindow::ApplicationWindow(): settings("Underground Software", "Architektonas")
@@ -100,6 +101,24 @@ void ApplicationWindow::RotateTool(void)
        drawing->SetRotateToolActive(rotateAct->isChecked());
 }
 
+void ApplicationWindow::ZoomInTool(void)
+{
+//printf("Zoom in... level going from %02f to ", Painter::zoom);
+       // This just zooms leaving origin intact... should zoom in at the current center!
+//     drawing->ZoomIn();
+       Painter::zoom *= 2.0;
+       drawing->update();
+}
+
+void ApplicationWindow::ZoomOutTool(void)
+{
+//printf("Zoom out...\n");
+       // This just zooms leaving origin intact... should zoom out at the current center!
+//     drawing->ZoomOut();
+       Painter::zoom /= 2.0;
+       drawing->update();
+}
+
 void ApplicationWindow::HelpAbout(void)
 {
        aboutWin->show();
@@ -150,6 +169,12 @@ 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 = CreateAction(tr("Zoom &In"), tr("Zoom In"), tr("Zoom in on the document."), QIcon(":/res/generic-tool.png"), 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/generic-tool.png"), QKeySequence(tr("-")));
+       connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(ZoomOutTool()));
+
        fileNewAct = CreateAction(tr("&New Drawing"), tr("New Drawing"), tr("Creates a new drawing."), QIcon(":/res/generic-tool.png"), QKeySequence(tr("Ctrl+n")));
 
        fileOpenAct = CreateAction(tr("&Open Drawing"), tr("Open Drawing"), tr("Opens an existing drawing from a file."), QIcon(":/res/generic-tool.png"), QKeySequence(tr("Ctrl+o")));
@@ -217,6 +242,10 @@ void ApplicationWindow::CreateMenus(void)
        menu->addSeparator();
        menu->addAction(exitAct);
 
+       menu = menuBar()->addMenu(tr("&View"));
+       menu->addAction(zoomInAct);
+       menu->addAction(zoomOutAct);
+
        menu = menuBar()->addMenu(tr("&Edit"));
        menu->addAction(fixAngleAct);
        menu->addAction(fixLengthAct);
@@ -248,6 +277,10 @@ void ApplicationWindow::CreateToolbars(void)
        QToolBar * toolbar = addToolBar(tr("File"));
        toolbar->addAction(exitAct);
 
+       toolbar = addToolBar(tr("View"));
+       toolbar->addAction(zoomInAct);
+       toolbar->addAction(zoomOutAct);
+
        toolbar = addToolBar(tr("Edit"));
        toolbar->addAction(fixAngleAct);
        toolbar->addAction(fixLengthAct);
index 99b5441b73edf31f30aa65351c2f81b2f68a9586..199c4b23c735a81dc9245084b149800b3a8dc077 100644 (file)
@@ -28,6 +28,8 @@ class ApplicationWindow: public QMainWindow
                void DeleteTool(void);
                void DimensionTool(void);
                void RotateTool(void);
+               void ZoomInTool(void);
+               void ZoomOutTool(void);
                void HelpAbout(void);
                void Settings(void);
 
@@ -42,8 +44,6 @@ class ApplicationWindow: public QMainWindow
                void ReadSettings(void);
                void WriteSettings(void);
 
-//             EditWindow * editWnd;
-//             CharWindow * charWnd;
                DrawingView * drawing;
                AboutWindow * aboutWin;
 
@@ -65,6 +65,8 @@ class ApplicationWindow: public QMainWindow
                QAction * addArcAct;
                QAction * aboutAct;
                QAction * rotateAct;
+               QAction * zoomInAct;
+               QAction * zoomOutAct;
 };
 
 #endif // __APPLICATIONWINDOW_H__
index a5349d98b8721da800da1f497055dd06f435b6d4..a3a6eb13a67503dd56042600eea073349cedfdaf 100644 (file)
@@ -42,7 +42,8 @@ DrawingView::DrawingView(QWidget * parent/*= NULL*/): QWidget(parent),
        useAntialiasing(true),
        scale(1.0), offsetX(-10), offsetY(-10),
        document(Vector(0, 0)),
-       gridSpacing(32.0), collided(false), rotateTool(false), rx(150.0), ry(150.0)
+       gridSpacing(32.0), collided(false), rotateTool(false), rx(150.0), ry(150.0),
+       scrollDrag(false)
 {
        setBackgroundRole(QPalette::Base);
        setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
@@ -76,6 +77,17 @@ void DrawingView::SetRotateToolActive(bool state/*= true*/)
        update();
 }
 
+//These are not needed... :-P
+#if 0
+void DrawingView::ZoomIn(void)
+{
+}
+
+void DrawingView::ZoomOut(void);
+{
+}
+#endif
+
 QPoint DrawingView::GetAdjustedMousePosition(QMouseEvent * event)
 {
        // This is undoing the transform, e.g. going from client coords to local coords.
@@ -101,7 +113,7 @@ void DrawingView::paintEvent(QPaintEvent * /*event*/)
                qtPainter.setRenderHint(QPainter::Antialiasing);
 
        Painter::screenSize = Vector(size().width(), size().height());
-       Painter::zoom = 2.0;    // 200% zoom
+//     Painter::zoom = 2.0;    // 200% zoom
 #if 0
 #if 0
        painter.translate(QPoint(-offsetX, size.height() - (-offsetY)));
@@ -164,23 +176,39 @@ void DrawingView::mousePressEvent(QMouseEvent * event)
 {
        if (event->button() == Qt::LeftButton)
        {
-//             QPoint pt = GetAdjustedMousePosition(event);
-//             Vector point(pt.x(), pt.y());
                Vector point = Painter::QtToCartesianCoords(Vector(event->x(), event->y()));
-
                collided = document.Collided(point);
 
                if (collided)
                        update();       // Do an update if collided with at least *one* object in the document
        }
+       else if (event->button() == Qt::MiddleButton)
+       {
+               scrollDrag = true;
+               oldPoint = Vector(event->x(), event->y());
+               // Should also change the mouse pointer as well...
+               setCursor(Qt::SizeAllCursor);
+       }
 }
 
 void DrawingView::mouseMoveEvent(QMouseEvent * event)
 {
-//     QPoint pt = GetAdjustedMousePosition(event);
-//     Vector point(pt.x(), pt.y());
        Vector point = Painter::QtToCartesianCoords(Vector(event->x(), event->y()));
 
+       if (event->buttons() & Qt::MiddleButton)
+       {
+               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. :-)
+               Vector delta(point, oldPoint);
+               delta /= Painter::zoom;
+               delta.y = -delta.y;
+               Painter::origin -= delta;
+               update();
+               oldPoint = point;
+               return;
+       }
+
        // Grid processing...
 #if 1
        // This looks strange, but it's really quite simple: We want a point that's
@@ -220,6 +248,11 @@ void DrawingView::mouseReleaseEvent(QMouseEvent * event)
 //             if (collided)
                        update();       // Do an update if collided with at least *one* object in the document
        }
+       else if (event->button() == Qt::MiddleButton)
+       {
+               scrollDrag = false;
+               setCursor(Qt::ArrowCursor);
+       }
 }
 
 
index 02baa301121a2d0ad0fd9989232532aba25ee101..dfb9f8fc0b43c18dc96d5872a41415487ef7d667 100644 (file)
@@ -14,6 +14,8 @@ class DrawingView: public QWidget
 
        public:
                void SetRotateToolActive(bool state = true);
+//             void ZoomIn(void);
+//             void ZoomOut(void);
 
        protected:
                void paintEvent(QPaintEvent * event);
@@ -37,6 +39,8 @@ class DrawingView: public QWidget
 //Should this go into Object's class variables???
                bool rotateTool;
                double rx, ry;
+               bool scrollDrag;
+               Vector oldPoint;
 /*             QSize minimumSizeHint() const;
                QSize sizeHint() const;
 
index e9ad51f3427fa06ad918dd7793986369cacdef87..58228ca39f75a7cfe9bb1b834dd8f19af4d24bf0 100644 (file)
@@ -133,6 +133,24 @@ Vector& Vector::operator+=(double const v)
        return *this;\r
 }\r
 \r
+// Vector - vector, self assigned\r
+\r
+Vector& Vector::operator-=(Vector const v)\r
+{\r
+       x -= v.x, y -= v.y, z -= v.z;\r
+\r
+       return *this;\r
+}\r
+\r
+// Vector - constant, self assigned\r
+\r
+Vector& Vector::operator-=(double const v)\r
+{\r
+       x -= v, y -= v, z -= v;\r
+\r
+       return *this;\r
+}\r
+\r
 \r
 Vector Vector::Unit(void)\r
 {\r
index 81a715f33e54ab533c3c022477b6074d9edf588f..0551ea87cc8555a0309aa0ad1946d5f8d340c39b 100644 (file)
@@ -32,6 +32,8 @@ class Vector
                Vector& operator/=(double const v);             // Vector divided by constant self-assignment\r
                Vector& operator+=(Vector const v);             // Vector plus Vector self-assignment\r
                Vector& operator+=(double const v);             // Vector plus constant self-assignment\r
+               Vector& operator-=(Vector const v);             // Vector minus Vector self-assignment\r
+               Vector& operator-=(double const v);             // Vector minus constant self-assignment\r
 \r
                Vector Unit(void);\r
                double Magnitude(void);\r