Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow opening a different project #241

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/core/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 11 additions & 3 deletions src/core/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -85,6 +90,9 @@ void Settings::loadUserSettings()

void Settings::loadProjectSettings(const QString &rootDir)
{
saveOnExit();
loadBaseSettings();

m_projectPath = rootDir;

const QString fileName = projectFilePath();
Expand Down
1 change: 1 addition & 0 deletions src/core/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
27 changes: 20 additions & 7 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class MainWindow : public QMainWindow
void openProject();
void saveDocument();
void saveAllDocuments();
void closeDocument();
void closeDocument(int);
void openOptions();
void returnToEditor();

Expand Down