# HG changeset patch # User Jacob Dawid # Date 1302676971 -7200 # Node ID cb41924c25e9f436bf4d7b27440e9a4dfd153dc8 # Parent 32732696103897706336f7d0092b14ffe5b590ee Creating a new file works. diff -r 327326961038 -r cb41924c25e9 gui//src/FileEditorMdiSubWindow.cpp --- 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 #include #include +#include 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 = ""; + 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); } diff -r 327326961038 -r cb41924c25e9 gui//src/FileEditorMdiSubWindow.h --- 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