changeset 13419:cb41924c25e9

Creating a new file works.
author Jacob Dawid <jacob.dawid@googlemail.com>
date Wed, 13 Apr 2011 08:42:51 +0200
parents 327326961038
children 485dfb3846cb
files gui//src/FileEditorMdiSubWindow.cpp gui//src/FileEditorMdiSubWindow.h
diffstat 2 files changed, 53 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/gui//src/FileEditorMdiSubWindow.cpp	Tue Apr 12 23:17:28 2011 +0200
+++ b/gui//src/FileEditorMdiSubWindow.cpp	Wed Apr 13 08:42:51 2011 +0200
@@ -21,6 +21,7 @@
 #include <QApplication>
 #include <QFile>
 #include <QFileDialog>
+#include <QMessageBox>
 
 FileEditorMdiSubWindow::FileEditorMdiSubWindow(QWidget *parent)
     : QMdiSubWindow(parent) {
@@ -38,14 +39,49 @@
     file.close();
 }
 
+void FileEditorMdiSubWindow::newFile() {
+    if(m_modified) {
+        int decision
+                = QMessageBox::question(this,
+                                        "Open New File",
+                                        "Do you want to save the current file?",
+                                        QMessageBox::Yes, QMessageBox::No);
+
+        if(decision == QMessageBox::Yes) {
+            saveFile();
+            if(m_modified) {
+                // If the user attempted to save the file, but it's still
+                // modified, then probably something went wrong, so we quit here.
+                return;
+            }
+        }
+    }
+
+    m_fileName = "<unnamed>";
+    setWindowTitle(m_fileName);
+    m_codeEdit->setPlainText("");
+}
+
 void FileEditorMdiSubWindow::saveFile() {
     QString saveFileName = QFileDialog::getSaveFileName(this, "Save File", m_fileName);
     QFile file(saveFileName);
     file.open(QFile::WriteOnly);
-    file.write(m_codeEdit->toPlainText().toLocal8Bit());
+
+    if(file.write(m_codeEdit->toPlainText().toLocal8Bit()) == -1) {
+        QMessageBox::warning(this,
+                             "Error Saving File",
+                             QString("The file could not be saved: %1.").arg(file.errorString()));
+    } else {
+        m_codeEdit->document()->setModified(false);
+    }
+
     file.close();
 }
 
+void FileEditorMdiSubWindow::showToolTipNew() {
+    m_statusBar->showMessage("Create a new file.", 2000);
+}
+
 void FileEditorMdiSubWindow::showToolTipSave() {
     m_statusBar->showMessage("Save the file.", 2000);
 }
@@ -58,6 +94,10 @@
     m_statusBar->showMessage("Append previous changes.", 2000);
 }
 
+void FileEditorMdiSubWindow::registerModified(bool modified) {
+    m_modified = modified;
+}
+
 void FileEditorMdiSubWindow::construct() {
     QStyle *style = QApplication::style();
     setWidget(new QWidget());
@@ -90,11 +130,18 @@
     layout->setMargin(2);
     widget()->setLayout(layout);
 
+    connect(newAction, SIGNAL(triggered()), this, SLOT(newFile()));
     connect(undoAction, SIGNAL(triggered()), m_codeEdit, SLOT(undo()));
     connect(redoAction, SIGNAL(triggered()), m_codeEdit, SLOT(redo()));
     connect(saveAction, SIGNAL(triggered()), this, SLOT(saveFile()));
 
+    connect(newAction, SIGNAL(hovered()), this, SLOT(showToolTipNew()));
     connect(undoAction, SIGNAL(hovered()), this, SLOT(showToolTipUndo()));
     connect(redoAction, SIGNAL(hovered()), this, SLOT(showToolTipRedo()));
     connect(saveAction, SIGNAL(hovered()), this, SLOT(showToolTipSave()));
+
+    connect(m_codeEdit, SIGNAL(modificationChanged(bool)), this, SLOT(registerModified(bool)));
+
+    m_fileName = "";
+    setWindowTitle(m_fileName);
 }
--- a/gui//src/FileEditorMdiSubWindow.h	Tue Apr 12 23:17:28 2011 +0200
+++ b/gui//src/FileEditorMdiSubWindow.h	Wed Apr 13 08:42:51 2011 +0200
@@ -32,11 +32,15 @@
     void loadFile(QString fileName);
 
 public slots:
+    void newFile();
     void saveFile();
+
+    void showToolTipNew();
     void showToolTipSave();
     void showToolTipUndo();
     void showToolTipRedo();
 
+    void registerModified(bool modified);
 private:
     void construct();
     QToolBar *m_toolBar;
@@ -44,6 +48,7 @@
     NumberedTextView *m_numberedTextView;
     QStatusBar *m_statusBar;
     QString m_fileName;
+    bool m_modified;
 };
 
 #endif // FILEEDITORMDISUBWINDOW_H