From 724ee7e216d315ffa75c6930f4d290ccd0fe91bf Mon Sep 17 00:00:00 2001 From: Pavel Pisa Date: Tue, 20 Aug 2019 09:31:09 +0200 Subject: Ask for modified source close and handle unnamed sources close. Signed-off-by: Pavel Pisa --- qtmips_gui/mainwindow.cpp | 69 ++++++++++++++++++++++++++++++++++++---- qtmips_gui/mainwindow.h | 4 ++- qtmips_gui/savechangeddialog.cpp | 13 ++++++-- 3 files changed, 76 insertions(+), 10 deletions(-) (limited to 'qtmips_gui') diff --git a/qtmips_gui/mainwindow.cpp b/qtmips_gui/mainwindow.cpp index 932a6be..a344fc6 100644 --- a/qtmips_gui/mainwindow.cpp +++ b/qtmips_gui/mainwindow.cpp @@ -120,7 +120,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { connect(ui->actionOpen, SIGNAL(triggered(bool)), this, SLOT(open_source())); connect(ui->actionSave, SIGNAL(triggered(bool)), this, SLOT(save_source())); connect(ui->actionSaveAs, SIGNAL(triggered(bool)), this, SLOT(save_source_as())); - connect(ui->actionClose, SIGNAL(triggered(bool)), this, SLOT(close_source())); + connect(ui->actionClose, SIGNAL(triggered(bool)), this, SLOT(close_source_check())); connect(ui->actionMnemonicRegisters, SIGNAL(triggered(bool)), this, SLOT(view_mnemonics_registers(bool))); connect(ui->actionCompileSource, SIGNAL(triggered(bool)), this, SLOT(compile_source())); connect(ui->actionBuildExe, SIGNAL(triggered(bool)), this, SLOT(build_execute())); @@ -386,14 +386,15 @@ void MainWindow::show_symbol_dialog(){ return; QStringList *symnames = machine->symbol_table()->names(); GoToSymbolDialog *gotosyboldialog = new GoToSymbolDialog(this, *symnames); + delete symnames; connect(gotosyboldialog, SIGNAL(program_focus_addr(std::uint32_t)), program, SIGNAL(focus_addr_with_save(std::uint32_t))); connect(gotosyboldialog, SIGNAL(memory_focus_addr(std::uint32_t)), memory, SIGNAL(focus_addr(std::uint32_t))); connect(gotosyboldialog, SIGNAL(obtain_value_for_name(std::uint32_t&,QString)), machine->symbol_table(), SLOT(name_to_value(std::uint32_t&,QString))); - gotosyboldialog->exec(); - delete symnames; + gotosyboldialog->setAttribute(Qt::WA_DeleteOnClose); + gotosyboldialog->open(); } void MainWindow::about_qtmips() @@ -438,7 +439,7 @@ void MainWindow::closeEvent(QCloseEvent *event) { settings->sync(); QStringList list; - if (modified_file_list(list) && !ignore_unsaved) { + if (modified_file_list(list, true) && !ignore_unsaved) { SaveChnagedDialog *dialog = new SaveChnagedDialog(list, this); connect(dialog, SIGNAL(user_decision(bool,QStringList&)), this, SLOT(save_exit_or_ignore(bool,QStringList&))); @@ -448,11 +449,29 @@ void MainWindow::closeEvent(QCloseEvent *event) { } void MainWindow::save_exit_or_ignore(bool cancel, QStringList &tosavelist) { + bool save_unnamed = false; if (cancel) return; for (const auto &fname : tosavelist) { SrcEditor *editor = source_editor_for_file(fname, false); - editor->saveFile(); + if (fname.isEmpty()) { + save_unnamed = true; + } else if (editor != nullptr) { + editor->saveFile(); + } + } + if (save_unnamed && (central_window != nullptr)) { + for (int i = 0; i < central_window->count(); i++) { + QWidget *w = central_window->widget(i); + SrcEditor *editor = dynamic_cast(w); + if (editor == nullptr) + continue; + if (!editor->filename().isEmpty()) + continue; + central_window->setCurrentWidget(editor); + save_source_as(); + return; + } } ignore_unsaved = true; close(); @@ -569,7 +588,7 @@ void MainWindow::update_open_file_list() { settings->setValue("openSrcFiles", open_src_files); } -bool MainWindow::modified_file_list(QStringList &list) { +bool MainWindow::modified_file_list(QStringList &list, bool report_unnamed) { bool ret = false; list.clear(); QStringList open_src_files; @@ -580,7 +599,7 @@ bool MainWindow::modified_file_list(QStringList &list) { SrcEditor *editor = dynamic_cast(w); if (editor == nullptr) continue; - if (editor->filename() == "") + if ((editor->filename() == "") && !report_unnamed) continue; if (!editor->isModified()) continue; @@ -734,6 +753,42 @@ void MainWindow::save_source() { #endif } +void MainWindow::close_source_check() { + if (current_srceditor == nullptr) + return; + SrcEditor *editor = current_srceditor; + if (!editor->isModified()) { + close_source(); + return; + } + QMessageBox *msgbox = new QMessageBox(this); + msgbox->setWindowTitle("Close unsaved source"); + msgbox->setText("Close unsaved source."); + msgbox->setInformativeText("Do you want to save your changes?"); + msgbox->setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); + msgbox->setDefaultButton(QMessageBox::Save); + msgbox->setMinimumSize(QSize(200, 100)); + msgbox->setAttribute(Qt::WA_DeleteOnClose); + connect(msgbox, SIGNAL(finished(int)), + this, SLOT(close_source_decided(int))); + msgbox->open(); +} + +void MainWindow::close_source_decided(int result) { + if (current_srceditor == nullptr) + return; + SrcEditor *editor = current_srceditor; + if (result == QMessageBox::Save) { + if (editor->filename().isEmpty()) { + save_source_as(); + return; + } + } else if (result != QMessageBox::Discard) { + return; + } + close_source(); +} + void MainWindow::close_source() { if (current_srceditor == nullptr) return; diff --git a/qtmips_gui/mainwindow.h b/qtmips_gui/mainwindow.h index 1a854e6..ba2998e 100644 --- a/qtmips_gui/mainwindow.h +++ b/qtmips_gui/mainwindow.h @@ -87,6 +87,8 @@ public slots: void save_source(); void save_source_as(); void close_source(); + void close_source_check(); + void close_source_decided(int result); void compile_source(); void build_execute(); void build_execute_no_check(); @@ -156,7 +158,7 @@ private: void show_dockwidget(QDockWidget *w, Qt::DockWidgetArea area = Qt::RightDockWidgetArea); void add_src_editor_to_tabs(SrcEditor *editor); void update_open_file_list(); - bool modified_file_list(QStringList &list); + bool modified_file_list(QStringList &list, bool report_unnamed = false); SrcEditor *source_editor_for_file(QString filename, bool open); QPointer build_process; bool ignore_unsaved; diff --git a/qtmips_gui/savechangeddialog.cpp b/qtmips_gui/savechangeddialog.cpp index dda1949..1849245 100644 --- a/qtmips_gui/savechangeddialog.cpp +++ b/qtmips_gui/savechangeddialog.cpp @@ -51,11 +51,20 @@ SaveChnagedDialog::SaveChnagedDialog(QStringList &changedlist, QWidget *parent) setWindowTitle(tr("Save next modified files?")); model = new QStandardItemModel(this); + bool unknown_inserted = false; for ( const auto& fname : changedlist) { int row = model->rowCount(); QStandardItem* item = new QStandardItem(); - item->setText(fname); + item->setData(fname, Qt::UserRole); + if (!fname.isEmpty()) { + item->setText(fname); + } else { + if (!unknown_inserted) { + item->setText("Unknown"); + unknown_inserted = true; + } + } item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled); item->setCheckState(Qt::Checked); model->setItem(row, 0, item); @@ -109,7 +118,7 @@ void SaveChnagedDialog::save_clicked() { QStringList list; for(int r = 0; r < model->rowCount(); ++r) { if (model->item(r)->checkState() == Qt::Checked) - list.append(model->item(r)->text()); + list.append(model->item(r)->data(Qt::UserRole).toString()); } emit user_decision(false, list); close(); -- cgit v1.2.3