]> Shamusworld >> Repos - ttedit/blobdiff - src/mainwindow.cpp
Added preview window to file loading dialog. :-)
[ttedit] / src / mainwindow.cpp
index 8c7524f500a1d37f9e409920cb9fd8d6df901c6f..173c977a8a04a0fe4cad8e6791d4ace2e418147b 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:
 #include "mainwindow.h"
 #include "charwindow.h"
 #include "editwindow.h"
+#include "global.h"
+#include "previewfiledialog.h"
 #include "ttedit.h"
 
 
 MainWindow::MainWindow()
 {
-       ((TTEdit *)qApp)->charWnd = new CharWindow(this);
+       Global::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();
-       newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
-       newAct->setShortcuts(QKeySequence::New);
-       newAct->setStatusTip(tr("Create a new file"));
-       connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
-
-       openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
-       openAct->setShortcuts(QKeySequence::Open);
-       openAct->setStatusTip(tr("Open an existing file"));
-       connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
-
-       aboutQtAct = new QAction(tr("About &Qt"), this);
-       aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
-       connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
-
-//     createMenus();
-       fileMenu = menuBar()->addMenu(tr("&File"));
-       fileMenu->addAction(newAct);
-       fileMenu->addAction(openAct);
-       fileMenu->addAction(saveAct);
-       fileMenu->addAction(saveAsAct);
-       fileMenu->addSeparator();
-       fileMenu->addAction(exitAct);
-
-       editMenu = menuBar()->addMenu(tr("&Edit"));
-       editMenu->addAction(cutAct);
-       editMenu->addAction(copyAct);
-       editMenu->addAction(pasteAct);
-
-       menuBar()->addSeparator();
-
-       helpMenu = menuBar()->addMenu(tr("&Help"));
-       helpMenu->addAction(aboutAct);
-       helpMenu->addAction(aboutQtAct);
-
-//     createToolBars();
-       fileToolBar = addToolBar(tr("File"));
-       fileToolBar->addAction(newAct);
-       fileToolBar->addAction(openAct);
-       fileToolBar->addAction(saveAct);
-
-       editToolBar = addToolBar(tr("Edit"));
-       editToolBar->addAction(cutAct);
-       editToolBar->addAction(copyAct);
-       editToolBar->addAction(pasteAct);
-#else
        CreateActions();
        CreateMenus();
        CreateToolbars();
-#endif
 
        //      Create status bar
+       scaleIndicator = new QLabel("Scale: 100%");
+       statusBar()->addPermanentWidget(scaleIndicator);
        statusBar()->showMessage(tr("Ready"));
 
        ReadSettings();
@@ -111,15 +69,15 @@ MainWindow::MainWindow()
 //     setCurrentFile("");
        setUnifiedTitleAndToolBarOnMac(true);
 
-       ((TTEdit *)qApp)->charWnd->show();//eh?
+       Global::charWnd->show();
 }
 
 
 //
 // 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 +93,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 +113,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 +134,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);
 }
 
 
@@ -189,17 +159,32 @@ void MainWindow::closeEvent(QCloseEvent * event)
 void MainWindow::NewGlyph(void)
 {
        editWnd->pts.Clear();
-       ((TTEdit *)qApp)->charWnd->MakePathFromPoints(&(editWnd->pts));
-       ((TTEdit *)qApp)->charWnd->update();
+       Global::charWnd->MakePathFromPoints(&(editWnd->pts));
+       Global::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"),
+#if 0
+       filename = QFileDialog::getOpenFileName(this, tr("Open Glyph File"),
                "./", tr("Glyph files (*.glyph)"));
-       FILE * file = fopen(filename.toAscii().data(), "r");
+#else
+       PreviewFileDialog * pfd = new PreviewFileDialog(this, tr("Open Glyph File"),
+               "./", tr("Glyph files (*.glyph)"));
+       pfd->setAcceptMode(QFileDialog::AcceptOpen);
+
+       if (pfd->exec() == false)
+               return;
+
+       filename = pfd->selectedFiles().at(0);
+#endif
+       FILE * file = fopen(filename.toUtf8().data(), "r");
 
        //need to pop an error box here...
        if (file == 0)
@@ -208,17 +193,21 @@ void MainWindow::OpenFile(void)
        editWnd->pts.LoadGlyphFromFile(file);
        fclose(file);
 
-       ((TTEdit *)qApp)->charWnd->MakePathFromPoints(&(editWnd->pts));
-       ((TTEdit *)qApp)->charWnd->update();
+       Global::charWnd->MakePathFromPoints(&(editWnd->pts));
+       Global::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)
@@ -226,6 +215,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();
 }
 
 
@@ -238,8 +264,8 @@ void MainWindow::ReadSettings(void)
        move(pos);
        pos = settings.value("charWndPos", QPoint(0, 0)).toPoint();
        size = settings.value("charWndSize", QSize(200, 200)).toSize();
-       ((TTEdit *)qApp)->charWnd->resize(size);
-       ((TTEdit *)qApp)->charWnd->move(pos);
+       Global::charWnd->resize(size);
+       Global::charWnd->move(pos);
 }
 
 
@@ -248,35 +274,12 @@ void MainWindow::WriteSettings(void)
        QSettings settings("Underground Software", "TTEdit");
        settings.setValue("pos", pos());
        settings.setValue("size", size());
-       settings.setValue("charWndPos", ((TTEdit *)qApp)->charWnd->pos());
-       settings.setValue("charWndSize", ((TTEdit *)qApp)->charWnd->size());
+       settings.setValue("charWndPos", Global::charWnd->pos());
+       settings.setValue("charWndSize", Global::charWnd->size());
 }
 
 
 #if 0
-#include "ttedit.h"
-#include "charwindow.h"
-#include "toolwindow.h"
-#include "editwindow.h"
-#include "tte_res.h"                                                   // Resource IDs
-#ifdef DEBUG
-#include "debug.h"
-#endif
-
-// Pixmap resouces
-
-#include "res/cur1.xpm"
-#include "res/cur2.xpm"
-#include "res/cur3.xpm"
-#include "res/cur4.xpm"
-#include "res/cur5.xpm"
-#include "res/cur6.xpm"
-#include "res/cur7.xpm"
-#include "res/cur8.xpm"
-#include "res/ttedit.xpm"                                              // *nix only, but small enough to not matter
-#include "res/tool1.xpm"
-#include "res/tool2.xpm"
-#include "res/tool3.xpm"
 
 
 IMPLEMENT_APP(TTEditApp)                                               // Run the main application loop