diff edgui.cpp @ 376:40e33ad0d153

Work towards a working editor .. some day.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 17 Oct 2012 02:27:55 +0300
parents
children feaeec4c6c55
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/edgui.cpp	Wed Oct 17 02:27:55 2012 +0300
@@ -0,0 +1,446 @@
+//
+// Demo Editor -- Qt GUI setup parts and callbacks
+// (C) Copyright 2012 Matti 'ccr' Hämäläinen <ccr@tnsp.org>
+//
+#include "edmain.h"
+
+#include <QCloseEvent>
+#include <QMenuBar>
+#include <QStatusBar>
+#include <QMenu>
+#include <QProgressBar>
+#include <QFileDialog>
+#include <QHBoxLayout>
+#include <QVBoxLayout>
+
+
+
+void DemoEditor::updateMenuStates()
+{
+    // Set window title based on document filename and changed status
+    QString name;
+
+    if (!demo || demo->filename.isEmpty())
+        name = DOC_DEF_FILENAME;
+    else
+        name = demo->filename;
+
+    if (changed)
+        name = "*" + name;
+    
+    setWindowTitle(name + " - " + QCoreApplication::applicationName());
+
+    // Enable menu items based on states
+#if 0
+    menuActValidate->setEnabled(demo->get);
+    menuActSave->setEnabled(demo->changed || changed);
+    menuActSaveAs->setEnabled(demo->changed || changed);
+#else
+    menuActSave->setEnabled(false);
+    menuActSaveAs->setEnabled(false);
+#endif
+    
+    // Enable undo/redo items and set their texts based on history status
+    int historyLevels = undoHistory.size();
+    QString itemText;
+    bool itemEnabled;
+    
+    if (undoHistoryPos >= 0 && undoHistoryPos < historyLevels)
+    {
+        itemText = " " + undoHistory.at(undoHistoryPos)->state;
+        itemEnabled = true;
+    }
+    else
+    {
+        itemText = "";
+        itemEnabled = false;
+    }
+    
+    menuActRedo->setEnabled(itemEnabled);
+    menuActRedo->setText("&Redo" + itemText);
+
+    if (undoHistoryPos > 0 && historyLevels > 0)
+    {
+        itemText = " " + undoHistory.at(undoHistoryPos - 1)->state;
+        itemEnabled = true;
+    }
+    else
+    {
+        itemText = "";
+        itemEnabled = false;
+    }
+    
+    menuActUndo->setEnabled(itemEnabled);
+    menuActUndo->setText("&Undo" + itemText);
+
+    update();
+}
+
+
+//
+// Show about dialog
+//
+void DemoEditor::actionAboutBox()
+{
+    QMessageBox::about(this,
+    "About "+ QCoreApplication::applicationName(),
+    "<h1>" + QCoreApplication::applicationName() +
+    " v"+ QCoreApplication::applicationVersion() +"</h1><br>\n"
+    "(C) Copyright 2012 Matti 'ccr' H&auml;m&auml;l&auml;inen<br>\n"
+    "<br>\n"
+    "<b>A demo editor TNSP dmlib engine.</b><br>\n");
+}
+
+
+//
+// Show a dialog inquiring the user whether to save the current
+// document if it has been modified since last save/load.
+//
+QMessageBox::StandardButton DemoEditor::showDocumentModifiedDialog()
+{
+    return QMessageBox::question(this,
+        "The document has been modified.",
+        "Do you want to save your changes?",
+        QMessageBox::Discard | QMessageBox::Cancel | QMessageBox::Save,
+        QMessageBox::Save);
+}
+
+
+//
+// Generic error/warning dialog
+//
+void DemoEditor::showFileErrorDialog(QString operation, int code, QFile::FileError err)
+{
+/*
+    QString msg;
+    
+    QMessageBox::error(this,
+    "A non-critical error occured",
+    operation
+*/
+}
+
+
+void DemoEditor::actionFileNew()
+{
+    bool okToCreate = true;
+
+    if (changed)
+    {
+        okToCreate = false;
+        switch (showDocumentModifiedDialog())
+        {
+            case QMessageBox::Discard:
+                okToCreate = true;
+                break;
+            
+            case QMessageBox::Save:
+                actionFileSave();
+                if (!changed)
+                {
+                    QMessageBox::information(this, 
+                        "Document saved",
+                        "The document was saved as " + demo->filename);
+
+                    okToCreate = true;
+                }
+                break;
+
+            default:
+                break;
+        }
+    }
+    
+    if (okToCreate)
+        createNewFile();
+}
+
+
+void DemoEditor::actionFileOpen()
+{
+    bool okToOpen = true;
+
+    if (changed)
+    {
+        okToOpen = false;
+
+        switch (showDocumentModifiedDialog())
+        {
+            case QMessageBox::Discard:
+                okToOpen = true;
+                break;
+            
+            case QMessageBox::Save:
+                actionFileSave();
+                if (!changed)
+                {
+                    QMessageBox::information(this, 
+                        "Document saved",
+                        "The document was saved as " + demo->filename);
+                    
+                    okToOpen = true;
+                }
+                break;
+
+            default:
+                break;
+        }
+    }
+        
+    if (okToOpen)
+    {
+        QFileDialog fdialog(this);
+
+        fdialog.setAcceptMode(QFileDialog::AcceptOpen);
+        fdialog.setFileMode(QFileDialog::ExistingFile);
+        fdialog.setNameFilter("Demo timeline files (*.demo)");
+        fdialog.setDefaultSuffix("demo");
+
+        if (fdialog.exec())
+            readFromFile(fdialog.selectedFiles()[0]);
+    }
+}
+
+
+void DemoEditor::actionFileSaveAs()
+{
+    if (!demo)
+    {
+        qDebug() << "DemoEditor::actionFileSaveAs(): demo == null";
+        return;
+    }
+
+    QFileDialog fdialog(this);
+
+    fdialog.setAcceptMode(QFileDialog::AcceptSave);
+    fdialog.setFileMode(QFileDialog::AnyFile);
+    fdialog.setNameFilter("Demo files (*.demo)");
+    fdialog.setDefaultSuffix("demo");
+    fdialog.setConfirmOverwrite(true);
+
+    fdialog.selectFile(demo->filename.isEmpty() ? demo->filename : DOC_DEF_FILENAME);
+    
+    if (fdialog.exec())
+        saveToFile(fdialog.selectedFiles()[0]);
+}
+
+
+void DemoEditor::actionFileSave()
+{
+    if (!demo)
+    {
+        qDebug() << "DemoEditor::actionFileSave(): demo == null";
+        return;
+    }
+
+    // If filename has been set, save .. otherwise go to save as
+    if (!demo->filename.isEmpty())
+        saveToFile(demo->filename);
+    else
+        actionFileSaveAs();
+}
+
+
+void DemoEditor::closeEvent(QCloseEvent *event)
+{
+    bool okToClose = true;
+
+    if (changed)
+    {
+        okToClose = false;
+        switch (showDocumentModifiedDialog())
+        {
+            case QMessageBox::Discard:
+                okToClose = true;
+                break;
+            
+            case QMessageBox::Save:
+                actionFileSave();
+                if (!changed)
+                    okToClose = true;
+                break;
+            
+            default:
+                break;
+        }
+    }
+    
+    if (okToClose)
+        event->accept();
+}
+
+
+//
+// Various menu actions
+//
+
+
+//
+// Update statusbar message text
+//
+void DemoEditor::statusMsg(QString message)
+{
+    statusBar()->showMessage(message, 0);
+}
+
+
+//
+// Set active element of an action group based on matching the data
+//
+void DemoEditor::setActionGroupChecked(QActionGroup *group, QVariant data)
+{
+    QList<QAction *> items = group->actions();
+
+    for (int i = 0; i < items.size(); i++)
+    {
+        QAction *act = items.at(i);
+        if (act->data() == data)
+        {
+            act->setChecked(true);
+            return;
+        }
+    }
+}
+
+
+//
+// Helper functions for creating GUI elements
+//
+QAction * DemoEditor::createToolButton(QActionGroup *group, QString name, QIcon icon, QString statustip, QVariant data)
+{
+    QAction *action = new QAction(icon, name, group);
+    
+    action->setStatusTip(statustip + ".");
+    action->setCheckable(true);
+    action->setData(data);
+    
+    return action;
+}
+
+
+QAction * DemoEditor::createMenuAction(QString name, const QKeySequence &shortcut, QString tooltip)
+{
+    QAction *action = new QAction(name, this);
+    
+    if (shortcut != QKeySequence(QKeySequence::UnknownKey))
+        action->setShortcut(shortcut);
+    
+    if (!tooltip.isNull())
+        action->setStatusTip(tooltip + ".");
+
+    return action;
+}
+
+
+QAction * DemoEditor::createMenuGroupAction(QMenu *menu, QActionGroup *group, QString name, const QKeySequence &shortcut, QString tooltip, QVariant data)
+{
+    QAction *action = createMenuAction(name, shortcut, tooltip);
+    
+    action->setCheckable(true);
+    action->setData(data);
+
+    menu->addAction(action);
+    group->addAction(action);
+
+    return action;
+}
+
+
+#define MCONNECT(menu, act, slot) do { connect(act, SIGNAL(triggered()), this, SLOT(slot)); menu->addAction(act); } while(0)
+
+
+//
+// Create GUI elements
+//
+void DemoEditor::createMainGUI()
+{
+    QAction *act;
+    QMenu *fileMenu, *editMenu, *viewMenu, *helpMenu;
+
+    qDebug() << "- Constructing menus";
+
+    //
+    // File menu
+    //
+    fileMenu = menuBar()->addMenu("&File");
+
+    act = createMenuAction("&New", QKeySequence::New, "Create a new demo timeline");
+    MCONNECT(fileMenu, act, actionFileNew());
+
+    menuActOpen = createMenuAction("&Open", QKeySequence::Open, "Open a demo timeline file");
+    MCONNECT(fileMenu, menuActOpen, actionFileOpen());
+
+    menuActSave = createMenuAction("&Save", QKeySequence::Save, "Save demo timeline");
+    MCONNECT(fileMenu, menuActSave, actionFileSave());
+
+    menuActSaveAs = createMenuAction("Save &as", QKeySequence::SaveAs, "Save demo timeline as a new file");
+    MCONNECT(fileMenu, menuActSaveAs, actionFileSaveAs());
+
+    fileMenu->addSeparator();
+
+    QKeySequence qseq(Qt::CTRL + Qt::Key_Q);
+    act = createMenuAction("&Quit", qseq, "Exit application");
+    MCONNECT(fileMenu, act, close());
+
+
+    //
+    // Edit menu
+    //
+    editMenu = menuBar()->addMenu("&Edit");
+
+    menuActUndo = createMenuAction("&Undo",  QKeySequence::Undo, "Undo last change");
+    MCONNECT(editMenu, menuActUndo, performUndo());
+
+    menuActRedo = createMenuAction("&Redo",  QKeySequence::Redo, "Redo last change");
+    MCONNECT(editMenu, menuActRedo, performRedo());
+
+#if 0
+    editMenu->addSeparator();
+
+    menuActCut = createMenuAction("Cu&t",  QKeySequence::Cut, "Cut object");
+    MCONNECT(editMenu, menuActCut, actionCut());
+
+    menuActCopy = createMenuAction("&Copy",  QKeySequence::Copy, "Copy object");
+    MCONNECT(editMenu, menuActCopy, actionCopy());
+
+    menuActPaste = createMenuAction("&Paste", QKeySequence::Paste, "Paste object");
+    MCONNECT(editMenu, menuActPaste, actionPaste());
+
+    menuActDelete = createMenuAction("&Delete", QKeySequence::Delete, "Delete object");
+    MCONNECT(editMenu, menuActDelete, actionDelete());
+#endif
+
+    //
+    // Help menu
+    //
+    helpMenu = menuBar()->addMenu("&Help");
+    act = createMenuAction("About", 0, "Show information about application");
+    MCONNECT(helpMenu, act, actionAboutBox());
+
+
+    qDebug() << "- Constructing toolbars";
+
+    //
+    // Construct the main screen
+    //
+    qDebug() << "- Constructing main screen layout";
+
+    QWidget *sideVBoxContainer = new QWidget();
+    QSizePolicy sideVBoxPolicy(QSizePolicy::Fixed, QSizePolicy::Ignored);
+    sideVBoxPolicy.setHeightForWidth(sideVBoxContainer->sizePolicy().hasHeightForWidth());
+    sideVBoxContainer->setSizePolicy(sideVBoxPolicy);
+
+    QVBoxLayout *sideVBox = new QVBoxLayout(sideVBoxContainer);
+    sideVBox->setSpacing(0);
+    sideVBox->setContentsMargins(0, 0, 0, 0);
+
+//    view = new MapView();
+
+    QWidget *holder = new QWidget();
+    QHBoxLayout *viewSplitter = new QHBoxLayout(holder);
+
+    viewSplitter->addWidget(sideVBoxContainer);
+//    viewSplitter->addWidget(view);
+
+    setCentralWidget(holder);
+}