From 2a996467f9ca00e8eee24376b31b9cb919f7fbf7 Mon Sep 17 00:00:00 2001 From: Pavel Pisa Date: Wed, 17 Jul 2019 23:37:14 +0200 Subject: Update editor search algorithm to prefer current editor for unnamed buffers. Signed-off-by: Pavel Pisa --- qtmips_gui/mainwindow.cpp | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/qtmips_gui/mainwindow.cpp b/qtmips_gui/mainwindow.cpp index a201b24..210b8de 100644 --- a/qtmips_gui/mainwindow.cpp +++ b/qtmips_gui/mainwindow.cpp @@ -540,28 +540,42 @@ void MainWindow::update_open_file_list() { settings->setValue("openSrcFiles", open_src_files); } +static int compare_filenames(const QString &filename1, const QString &filename2) { + QFileInfo fi1(filename1); + QFileInfo fi2(filename2); + QString canon1 = fi1.canonicalFilePath(); + QString canon2 = fi2.canonicalFilePath(); + if (!canon1.isEmpty() && (canon1 == canon2)) + return 2; + if (filename1 == filename2) + return 1; + return 0; +} + SrcEditor *MainWindow::source_editor_for_file(QString filename, bool open) { - if ((central_window == nullptr) || (settings == nullptr)) + if (central_window == nullptr) return nullptr; - bool found = false; - SrcEditor *editor = nullptr; + int found_match = 0; + SrcEditor *found_editor; for (int i = 0; i < central_window->count(); i++) { QWidget *w = central_window->widget(i); - editor = dynamic_cast(w); + SrcEditor *editor = dynamic_cast(w); if (editor == nullptr) continue; - if (editor->filename() == filename) { - found = true; - break; + int match = compare_filenames(filename, editor->filename()); + if ((match > found_match) || + ((editor == current_srceditor) && (match >= found_match))) { + found_editor = editor; + found_match = match; } } - if (found) - return editor; + if (found_match > 0) + return found_editor; if (!open) return nullptr; - editor = new SrcEditor(); + SrcEditor *editor = new SrcEditor(); if (!editor->loadFile(filename)) { delete editor; return nullptr; -- cgit v1.2.3