]> Shamusworld >> Repos - ttedit/blobdiff - src/mainwindow.cpp
Fix zoom to zoom in/out from the center.
[ttedit] / src / mainwindow.cpp
index 076769589de113e7c8ee42200b1c64b8fd078fe8..2c9b45d1024d27a29540d537771b802ca6afa945 100644 (file)
@@ -6,7 +6,7 @@
 // JLH = James L. Hammons <jlhamm@acm.org>
 //
 // Who  When        What
-// ---  ----------  -------------------------------------------------------------
+// ---  ----------  -----------------------------------------------------------
 // JLH  04/10/2002  Created this file
 // JLH  05/10/2004  Translated file from ASM to CPP
 // JLH  05/14/2004  Added rudimentary editing capability to tool palette tools
@@ -18,7 +18,8 @@
 
 // FIXED:
 //
-// - Fix problem with tool palette not getting focus 1st time it's called up [DONE]
+// - Fix problem with tool palette not getting focus 1st time it's called up
+//   [DONE]
 // - Split out windows/classes defined here into their own files [DONE]
 //
 // STILL TO BE DONE:
@@ -37,6 +38,7 @@
 #include "mainwindow.h"
 #include "charwindow.h"
 #include "editwindow.h"
+#include "global.h"
 #include "ttedit.h"
 
 
@@ -45,8 +47,9 @@ MainWindow::MainWindow()
        ((TTEdit *)qApp)->charWnd = new CharWindow(this);
        editWnd = new EditWindow(this);
        setCentralWidget(editWnd);
+       editWnd->setFocus();
        setWindowIcon(QIcon(":/res/ttedit.png"));
-       setWindowTitle("TTEdit!");
+       setWindowTitle("TTEdit! - Untitled");
 
 #if 0
 //     createActions();
@@ -101,6 +104,8 @@ MainWindow::MainWindow()
 #endif
 
        //      Create status bar
+       scaleIndicator = new QLabel("Scale: 100%");
+       statusBar()->addPermanentWidget(scaleIndicator);
        statusBar()->showMessage(tr("Ready"));
 
        ReadSettings();
@@ -118,8 +123,8 @@ MainWindow::MainWindow()
 //
 // Consolidates action creation from a multi-step process to a single-step one.
 //
-QAction * MainWindow::CreateAction(QString name, QString tooltip, QString statustip,
-       QIcon icon, QKeySequence key, bool checkable/*= false*/)
+QAction * MainWindow::CreateAction(QString name, QString tooltip, QString
+       statustip, QIcon icon, QKeySequence key, bool checkable/*= false*/)
 {
        QAction * action = new QAction(icon, name, this);
        action->setToolTip(tooltip);
@@ -135,8 +140,9 @@ QAction * MainWindow::CreateAction(QString name, QString tooltip, QString status
 // This is essentially the same as the previous function, but this allows more
 // than one key sequence to be added as key shortcuts.
 //
-QAction * MainWindow::CreateAction(QString name, QString tooltip, QString statustip,
-       QIcon icon, QKeySequence key1, QKeySequence key2, bool checkable/*= false*/)
+QAction * MainWindow::CreateAction(QString name, QString tooltip, QString
+       statustip, QIcon icon, QKeySequence key1, QKeySequence key2, bool
+       checkable/*= false*/)
 {
        QAction * action = new QAction(icon, name, this);
        action->setToolTip(tooltip);
@@ -154,12 +160,18 @@ QAction * MainWindow::CreateAction(QString name, QString tooltip, QString status
 void MainWindow::CreateActions(void)
 {
        newGlyphAct = CreateAction("&New Glyph", "New Glyph", "Create a new glyph", QIcon(), QKeySequence());
-       openFileAct = CreateAction("&Open File", "Open File", "Open a glyph file", QIcon(), QKeySequence());
-       saveFileAct = CreateAction("&Save File", "Save File", "Save a glyph file", QIcon(), QKeySequence());
+       openFileAct = CreateAction("&Open File", "Open File", "Open a glyph file", QIcon(), QKeySequence("ctrl+o"));
+       saveFileAct = CreateAction("&Save File", "Save File", "Save a glyph file", QIcon(), QKeySequence("ctrl+s"));
+       quitAct = CreateAction("&Quit", "Quit", "Exits from the TTEdit application", QIcon(), QKeySequence("ctrl+q"));
+       zoomInAct = CreateAction("Zoom In", "Zoom In", "Zoom into the canvas", QIcon(), QKeySequence("+"), QKeySequence("="));
+       zoomOutAct = CreateAction("Zoom Out", "Zoom Out", "Zoom out of the canvas", QIcon(), QKeySequence("-"));
 
        connect(newGlyphAct, SIGNAL(triggered()), this, SLOT(NewGlyph()));
        connect(openFileAct, SIGNAL(triggered()), this, SLOT(OpenFile()));
        connect(saveFileAct, SIGNAL(triggered()), this, SLOT(SaveFile()));
+       connect(quitAct, SIGNAL(triggered()), this, SLOT(close()));
+       connect(zoomInAct, SIGNAL(triggered()), this, SLOT(ZoomIn()));
+       connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(ZoomOut()));
 }
 
 
@@ -169,8 +181,13 @@ void MainWindow::CreateMenus(void)
        menu->addAction(newGlyphAct);
        menu->addAction(openFileAct);
        menu->addAction(saveFileAct);
+       menu->addSeparator();
+       menu->addAction(quitAct);
 //     menu->addAction(fileSaveAsAct);
 //     menu->addAction(fileCloseAct);
+       menu = menuBar()->addMenu(tr("&View"));
+       menu->addAction(zoomInAct);
+       menu->addAction(zoomOutAct);
 }
 
 
@@ -191,16 +208,19 @@ void MainWindow::NewGlyph(void)
        editWnd->pts.Clear();
        ((TTEdit *)qApp)->charWnd->MakePathFromPoints(&(editWnd->pts));
        ((TTEdit *)qApp)->charWnd->update();
+       filename.clear();
 //     editWnd->polyFirstPoint = true;
        editWnd->update();
+       setWindowTitle("TTEdit! - Untitled");
+       editWnd->setFocus();
 }
 
 
 void MainWindow::OpenFile(void)
 {
-       QString filename = QFileDialog::getOpenFileName(this, tr("Open Glyph File"),
+       filename = QFileDialog::getOpenFileName(this, tr("Open Glyph File"),
                "./", tr("Glyph files (*.glyph)"));
-       FILE * file = fopen(filename.toAscii().data(), "r");
+       FILE * file = fopen(filename.toUtf8().data(), "r");
 
        //need to pop an error box here...
        if (file == 0)
@@ -212,14 +232,18 @@ void MainWindow::OpenFile(void)
        ((TTEdit *)qApp)->charWnd->MakePathFromPoints(&(editWnd->pts));
        ((TTEdit *)qApp)->charWnd->update();
        editWnd->update();
+       setWindowTitle(QString("TTEdit! - %1").arg(filename));
+       editWnd->setFocus();
 }
 
 
 void MainWindow::SaveFile(void)
 {
-       QString filename = QFileDialog::getSaveFileName(this, tr("Save Glyph File"),
+       if (filename.isEmpty())
+               filename = QFileDialog::getSaveFileName(this, tr("Save Glyph File"),
                "./", tr("Glyph files (*.glyph)"));
-       FILE * file = fopen(filename.toAscii().data(), "w");
+
+       FILE * file = fopen(filename.toUtf8().data(), "w");
 
        //need to pop an error box here...
        if (file == 0)
@@ -227,6 +251,43 @@ void MainWindow::SaveFile(void)
 
        editWnd->pts.SaveGlyphToFile(file);
        fclose(file);
+       setWindowTitle(QString("TTEdit! - %1").arg(filename));
+       statusBar()->showMessage(tr("File saved."));
+       editWnd->setFocus();
+}
+
+
+void MainWindow::ZoomIn(void)
+{
+       if (Global::zoom == 4.0)
+               return;
+
+       // Adjust the origin by the width/height by 1/4 (half of the scaled size).
+       // Also, need to adjust for the current zoom level...
+       Vector adjustment = (Global::screenSize / Global::zoom) * 0.25;
+       Global::origin += adjustment;
+
+       Global::zoom *= 2.0;
+
+       scaleIndicator->setText(QString("Scale: %1%").arg(Global::zoom * 100.0));
+       editWnd->update();
+}
+
+
+void MainWindow::ZoomOut(void)
+{
+       if (Global::zoom == 0.25)
+               return;
+
+       Global::zoom *= 0.5;
+
+       // Adjust the origin by the width/height by 1/4 (half of the scaled size).
+       // Also, need to adjust for the current zoom level...
+       Vector adjustment = (Global::screenSize / Global::zoom) * 0.25;
+       Global::origin -= adjustment;
+
+       scaleIndicator->setText(QString("Scale: %1%").arg(Global::zoom * 100.0));
+       editWnd->update();
 }