Handles document changes when exiting, bugfix: calls Save As if no document name is given

git-svn-id: http://svn.clifford.at/openscad/trunk@175 b57f626f-c46c-0410-a088-ec61d464b74c
stl_dim
kintel 2009-12-14 04:24:51 +00:00
parent 084c60bbd1
commit 4d5084ea27
2 changed files with 58 additions and 14 deletions

View File

@ -45,6 +45,9 @@ public:
MainWindow(const char *filename = 0); MainWindow(const char *filename = 0);
~MainWindow(); ~MainWindow();
protected:
void closeEvent(QCloseEvent *event);
private slots: private slots:
void updatedFps(); void updatedFps();
void updateTVal(); void updateTVal();
@ -55,6 +58,7 @@ private:
void load(); void load();
void find_root_tag(AbstractNode *n); void find_root_tag(AbstractNode *n);
void compile(bool procevents); void compile(bool procevents);
bool maybeSave();
private slots: private slots:
void actionNew(); void actionNew();

View File

@ -251,6 +251,8 @@ MainWindow::MainWindow(const char *filename)
} }
connect(editor->document(), SIGNAL(contentsChanged()), this, SLOT(animateUpdateDocChanged())); connect(editor->document(), SIGNAL(contentsChanged()), this, SLOT(animateUpdateDocChanged()));
connect(editor->document(), SIGNAL(modificationChanged(bool)), this, SLOT(setWindowModified(bool)));
connect(editor->document(), SIGNAL(modificationChanged(bool)), fileActionSave, SLOT(setEnabled(bool)));
connect(screen, SIGNAL(doAnimateUpdate()), this, SLOT(animateUpdate())); connect(screen, SIGNAL(doAnimateUpdate()), this, SLOT(animateUpdate()));
// display this window and check for OpenGL 2.0 (OpenCSG) support // display this window and check for OpenGL 2.0 (OpenCSG) support
@ -310,6 +312,7 @@ MainWindow::openFile(const QString &new_filename)
} }
#endif #endif
setFileName(new_filename); setFileName(new_filename);
load(); load();
} }
@ -379,8 +382,8 @@ void MainWindow::updateTVal()
void MainWindow::load() void MainWindow::load()
{ {
if (!this->fileName.isEmpty()) current_win = this;
{ if (!this->fileName.isEmpty()) {
QString text; QString text;
FILE *fp = fopen(this->fileName.toUtf8(), "rt"); FILE *fp = fopen(this->fileName.toUtf8(), "rt");
if (!fp) { if (!fp) {
@ -397,6 +400,7 @@ void MainWindow::load()
} }
editor->setPlainText(text); editor->setPlainText(text);
} }
current_win = this;
} }
void MainWindow::find_root_tag(AbstractNode *n) void MainWindow::find_root_tag(AbstractNode *n)
@ -671,16 +675,21 @@ void MainWindow::updateRecentFileActions()
void MainWindow::actionSave() void MainWindow::actionSave()
{ {
current_win = this; if (this->fileName.isEmpty()) {
FILE *fp = fopen(this->fileName.toUtf8(), "wt"); actionSaveAs();
if (!fp) { }
PRINTA("Failed to open file for writing: %1 (%2)", this->fileName, QString(strerror(errno))); else {
} else { current_win = this;
fprintf(fp, "%s", editor->toPlainText().toAscii().data()); FILE *fp = fopen(this->fileName.toUtf8(), "wt");
fclose(fp); if (!fp) {
PRINTA("Saved design `%1'.", this->fileName); PRINTA("Failed to open file for writing: %1 (%2)", this->fileName, QString(strerror(errno)));
} else {
fprintf(fp, "%s", editor->toPlainText().toAscii().data());
fclose(fp);
PRINTA("Saved design `%1'.", this->fileName);
}
current_win = NULL;
} }
current_win = NULL;
} }
void MainWindow::actionSaveAs() void MainWindow::actionSaveAs()
@ -694,9 +703,7 @@ void MainWindow::actionSaveAs()
void MainWindow::actionReload() void MainWindow::actionReload()
{ {
current_win = this;
load(); load();
current_win = NULL;
} }
void MainWindow::editIndent() void MainWindow::editIndent()
@ -788,10 +795,11 @@ void MainWindow::pasteViewportRotation()
void MainWindow::actionReloadCompile() void MainWindow::actionReloadCompile()
{ {
current_win = this;
console->clear(); console->clear();
load(); load();
current_win = this;
compile(true); compile(true);
#ifdef ENABLE_OPENCSG #ifdef ENABLE_OPENCSG
@ -1431,3 +1439,35 @@ MainWindow::helpManual()
QDesktopServices::openUrl(QUrl("http://en.wikibooks.org/wiki/OpenSCAD_User_Manual")); QDesktopServices::openUrl(QUrl("http://en.wikibooks.org/wiki/OpenSCAD_User_Manual"));
} }
/*!
FIXME: In SDI mode, this should be called also on New and Open
In MDI mode; also call on both reload functions?
*/
bool
MainWindow::maybeSave()
{
if (editor->document()->isModified()) {
QMessageBox::StandardButton ret;
ret = QMessageBox::warning(this, "Application",
"The document has been modified.\n"
"Do you want to save your changes?",
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
if (ret == QMessageBox::Save) {
actionSave();
return true; // FIXME: Should return false on error
}
else if (ret == QMessageBox::Cancel) {
return false;
}
}
return true;
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if (maybeSave()) {
event->accept();
} else {
event->ignore();
}
}