From 4a0fc5926124d5362a9860533fceb71bc955a105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20O=2E=20Cordero=20P=C3=A9rez?= Date: Mon, 3 Mar 2025 17:23:50 -0400 Subject: [PATCH 1/3] feat: allow opening a different project Opening a new project closes and replaces the current project. --- src/gui/mainwindow.cpp | 27 ++++++++++++++++++++------- src/gui/mainwindow.h | 2 +- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 0681bf9d..61738de1 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -364,6 +364,10 @@ void MainWindow::openProject() void MainWindow::initProject(const QString &path) { + const int tabCount = ui->tabWidget->tabBar()->count(); + for (int i = tabCount; i >= 0; i--) + closeDocument(i); + setWindowTitle(generateWindowTitle(path)); // Update recent list QSettings settings; @@ -378,10 +382,6 @@ void MainWindow::initProject(const QString &path) auto index = m_fileModel->setRootPath(path); m_projectView->setRootIndex(index); connect(m_projectView->selectionModel(), &QItemSelectionModel::currentChanged, this, &MainWindow::openDocument); - - // Disable menus, we can only load one project - restart Knut if needed - ui->actionOpen->setEnabled(false); - ui->actionRecentProjects->setEnabled(false); } void MainWindow::updateRecentProjects() @@ -513,12 +513,25 @@ void MainWindow::saveDocument() document->save(); } -void MainWindow::closeDocument() +void MainWindow::closeDocument(int closeIndex) { + const int currentIndex = ui->tabWidget->currentIndex(); + bool differentIndex = false; + if (closeIndex == 0) + closeIndex = currentIndex; + else + differentIndex = currentIndex != closeIndex; + if (differentIndex) + ui->tabWidget->setCurrentIndex(closeIndex); + auto document = Core::Project::instance()->currentDocument(); - if (document) + if (document) { document->close(); - ui->tabWidget->removeTab(ui->tabWidget->currentIndex()); + ui->tabWidget->removeTab(closeIndex); + } + + if (differentIndex) + ui->tabWidget->setCurrentIndex(currentIndex - (closeIndex < currentIndex ? 1 : 0)); } void MainWindow::createQrc() diff --git a/src/gui/mainwindow.h b/src/gui/mainwindow.h index dce61804..7dfc50f0 100644 --- a/src/gui/mainwindow.h +++ b/src/gui/mainwindow.h @@ -59,7 +59,7 @@ class MainWindow : public QMainWindow void openProject(); void saveDocument(); void saveAllDocuments(); - void closeDocument(); + void closeDocument(int); void openOptions(); void returnToEditor(); From 0068cae22eb55f5a1e2e5d9053919a6b6b729fc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20O=2E=20Cordero=20P=C3=A9rez?= Date: Mon, 3 Mar 2025 17:01:57 -0400 Subject: [PATCH 2/3] feat: save and reload app settings when opening a project Saving only takes place if it's applicable. --- src/core/settings.cpp | 14 +++++++++++--- src/core/settings.h | 1 + 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/core/settings.cpp b/src/core/settings.cpp index d5ccaed1..33136b60 100644 --- a/src/core/settings.cpp +++ b/src/core/settings.cpp @@ -49,9 +49,7 @@ Settings::Settings(Mode mode, QObject *parent) Q_ASSERT(m_instance == nullptr); m_instance = this; - loadKnutSettings(); - if (!isTesting()) // Only load if not testing - loadUserSettings(); + loadBaseSettings(); m_saveTimer->callOnTimeout(this, &Settings::saveSettings); m_saveTimer->setSingleShot(true); @@ -63,6 +61,13 @@ Settings::~Settings() m_instance = nullptr; } +void Settings::loadBaseSettings() +{ + loadKnutSettings(); + if (!isTesting()) // Only load if not testing + loadUserSettings(); +} + Settings *Settings::instance() { Q_ASSERT(m_instance); @@ -85,6 +90,9 @@ void Settings::loadUserSettings() void Settings::loadProjectSettings(const QString &rootDir) { + saveOnExit(); + loadBaseSettings(); + m_projectPath = rootDir; const QString fileName = projectFilePath(); diff --git a/src/core/settings.h b/src/core/settings.h index 766e1836..e5fea4ed 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -133,6 +133,7 @@ public slots: void loadKnutSettings(); void loadUserSettings(); + void loadBaseSettings(); void updatePaths(const QString &path, const std::string &json_path, bool add); void saveSettings(); void saveOnExit(); From 6fc496274f47f80ec14937cd6209a2111304f771 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20O=2E=20Cordero=20P=C3=A9rez?= Date: Mon, 3 Mar 2025 17:19:41 -0400 Subject: [PATCH 3/3] feat: save and reload app settings when opening a project Saving only takes place if it's applicable. --- src/core/project.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/core/project.cpp b/src/core/project.cpp index 7aef9f24..617a8668 100644 --- a/src/core/project.cpp +++ b/src/core/project.cpp @@ -94,12 +94,11 @@ bool Project::setRoot(const QString &newRoot) if (m_root == dir.absolutePath()) return true; - if (m_root.isEmpty()) { - spdlog::info("{}: {}", FUNCTION_NAME, dir.absolutePath()); - } else { - spdlog::error("{}: can't open a new project", FUNCTION_NAME); - return false; - } + if (!m_root.isEmpty()) + for (auto client : m_lspClients | std::views::values) + client->closeProject(m_root); + + spdlog::info("{}: {}", FUNCTION_NAME, dir.absolutePath()); m_root = dir.absolutePath(); Settings::instance()->loadProjectSettings(m_root);