From 7301a3e64d8f02292d69a5f932498082d77e68e5 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Fri, 8 Feb 2013 16:18:05 +0100 Subject: [PATCH] QFileDialog last used paths storage improved --- gui/applicationdialog.cpp | 4 +++- gui/common.cpp | 49 +++++++++++++++++++++++++++++++++++++++ gui/common.h | 14 ++++++++++- gui/gui.pro | 1 + gui/mainwindow.cpp | 22 +++++++++++------- gui/resultstree.cpp | 5 ++-- gui/settingsdialog.cpp | 3 ++- 7 files changed, 85 insertions(+), 13 deletions(-) create mode 100644 gui/common.cpp diff --git a/gui/applicationdialog.cpp b/gui/applicationdialog.cpp index d1f2a7862..3adc2ec69 100644 --- a/gui/applicationdialog.cpp +++ b/gui/applicationdialog.cpp @@ -23,6 +23,7 @@ #include #include "applicationdialog.h" #include "application.h" +#include "common.h" ApplicationDialog::ApplicationDialog(const QString &title, @@ -59,10 +60,11 @@ void ApplicationDialog::Browse() #endif // Q_WS_WIN QString selectedFile = QFileDialog::getOpenFileName(this, tr("Select viewer application"), - QString(), + GetPath(SETTINGS_LAST_APP_PATH), filter); if (!selectedFile.isEmpty()) { + SetPath(SETTINGS_LAST_APP_PATH, selectedFile, false); QString path(QDir::toNativeSeparators(selectedFile)); mUI.mPath->setText(path); } diff --git a/gui/common.cpp b/gui/common.cpp new file mode 100644 index 000000000..799721738 --- /dev/null +++ b/gui/common.cpp @@ -0,0 +1,49 @@ +/* + * Cppcheck - A tool for static C/C++ code analysis + * Copyright (C) 2007-2013 Daniel Marjamäki and Cppcheck team. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "common.h" +#include +#include +#include + + +QString GetPath(const QString &type) +{ + QSettings settings; + const QString path = settings.value(type, "").toString(); + if (path.isEmpty()) + return settings.value(SETTINGS_LAST_USED_PATH, "").toString(); + return path; +} + +void SetPath(const QString &type, const QString &value, bool storeAsLastUsed /* = true */) +{ + QSettings settings; + settings.setValue(type, value); + if (storeAsLastUsed) { + // file name and especially its extension is not portable between types so strip it + const QFileInfo fi(value); + if (fi.isFile()) { + settings.setValue(SETTINGS_LAST_USED_PATH, fi.dir().path()); + } + else { + settings.setValue(SETTINGS_LAST_USED_PATH, value); + } + } +} diff --git a/gui/common.h b/gui/common.h index 64714db00..7eee98786 100644 --- a/gui/common.h +++ b/gui/common.h @@ -19,6 +19,8 @@ #ifndef COMMON_H #define COMMON_H +#include + /// @addtogroup GUI /// @{ @@ -59,7 +61,6 @@ #define SETTINGS_STD_POSIX "Platform Posix" // Other settings -#define SETTINGS_CHECK_PATH "Check path" #define SETTINGS_CHECK_FORCE "Check force" #define SETTINGS_CHECK_THREADS "Check threads" #define SETTINGS_SHOW_FULL_PATH "Show full path" @@ -82,7 +83,18 @@ #define PROGRESS_MAX 1024.0 #define SETTINGS_CHECKED_PLATFORM "Checked platform" + +#define SETTINGS_LAST_USED_PATH "Last used path" +#define SETTINGS_LAST_CHECK_PATH "Last check path" #define SETTINGS_LAST_PROJECT_PATH "Last project path" +#define SETTINGS_LAST_RESULT_PATH "Last result path" +#define SETTINGS_LAST_SOURCE_PATH "Last source path" +#define SETTINGS_LAST_INCLUDE_PATH "Last include path" +#define SETTINGS_LAST_APP_PATH "Last application path" + + +QString GetPath(const QString &type); +void SetPath(const QString &type, const QString &value, bool storeAsLastUsed = true); /// @} #endif diff --git a/gui/gui.pro b/gui/gui.pro index b380780ae..8f02a04d6 100644 --- a/gui/gui.pro +++ b/gui/gui.pro @@ -106,6 +106,7 @@ SOURCES += aboutdialog.cpp \ applicationlist.cpp \ checkstatistics.cpp \ checkthread.cpp \ + common.cpp \ csvreport.cpp \ erroritem.cpp \ filelist.cpp \ diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 954c02e78..a263993bf 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -391,7 +391,7 @@ QStringList MainWindow::SelectFilesToCheck(QFileDialog::FileMode mode) if (mode == QFileDialog::ExistingFiles) { selected = QFileDialog::getOpenFileNames(this, tr("Select files to check"), - mSettings->value(SETTINGS_CHECK_PATH, "").toString()); + GetPath(SETTINGS_LAST_CHECK_PATH)); if (selected.isEmpty()) mCurrentDirectory.clear(); else { @@ -402,7 +402,7 @@ QStringList MainWindow::SelectFilesToCheck(QFileDialog::FileMode mode) } else if (mode == QFileDialog::DirectoryOnly) { QString dir = QFileDialog::getExistingDirectory(this, tr("Select directory to check"), - mSettings->value(SETTINGS_CHECK_PATH, "").toString()); + GetPath(SETTINGS_LAST_CHECK_PATH)); if (!dir.isEmpty()) { qDebug() << "Setting current directory to: " << dir; mCurrentDirectory = dir; @@ -411,7 +411,9 @@ QStringList MainWindow::SelectFilesToCheck(QFileDialog::FileMode mode) FormatAndSetTitle(dir); } } - + + SetPath(SETTINGS_LAST_CHECK_PATH, mCurrentDirectory); + return selected; } @@ -670,7 +672,7 @@ void MainWindow::OpenResults() const QString filter(tr("XML files (*.xml)")); QString selectedFile = QFileDialog::getOpenFileName(this, tr("Open the report file"), - QString(), + GetPath(SETTINGS_LAST_RESULT_PATH), filter, &selectedFilter); @@ -684,6 +686,7 @@ void MainWindow::LoadResults(const QString selectedFile) if (!selectedFile.isEmpty()) { mUI.mResults->Clear(true); mUI.mResults->ReadErrorsXml(selectedFile); + SetPath(SETTINGS_LAST_RESULT_PATH, selectedFile); } } @@ -815,7 +818,7 @@ void MainWindow::Save() const QString filter(tr("XML files version 2 (*.xml);;XML files version 1 (*.xml);;Text files (*.txt);;CSV files (*.csv)")); QString selectedFile = QFileDialog::getSaveFileName(this, tr("Save the report file"), - QString(), + GetPath(SETTINGS_LAST_RESULT_PATH), filter, &selectedFilter); @@ -847,6 +850,7 @@ void MainWindow::Save() } mUI.mResults->Save(selectedFile, type); + SetPath(SETTINGS_LAST_RESULT_PATH, selectedFile); } } @@ -935,13 +939,13 @@ void MainWindow::OpenProjectFile() const QString filter = tr("Project files (*.cppcheck);;All files(*.*)"); const QString filepath = QFileDialog::getOpenFileName(this, tr("Select Project File"), - lastPath, + GetPath(SETTINGS_LAST_PROJECT_PATH), filter); if (!filepath.isEmpty()) { const QFileInfo fi(filepath); if (fi.exists() && fi.isFile() && fi.isReadable()) { - mSettings->setValue(SETTINGS_LAST_PROJECT_PATH, fi.path()); + SetPath(SETTINGS_LAST_PROJECT_PATH, filepath); LoadProjectFile(filepath); } } @@ -1018,12 +1022,14 @@ void MainWindow::NewProjectFile() const QString filter = tr("Project files (*.cppcheck);;All files(*.*)"); QString filepath = QFileDialog::getSaveFileName(this, tr("Select Project Filename"), - QString(), + GetPath(SETTINGS_LAST_PROJECT_PATH), filter); if (filepath.isEmpty()) return; + SetPath(SETTINGS_LAST_PROJECT_PATH, filepath); + EnableProjectActions(true); QFileInfo inf(filepath); const QString filename = inf.fileName(); diff --git a/gui/resultstree.cpp b/gui/resultstree.cpp index db89b0334..8e8675f21 100644 --- a/gui/resultstree.cpp +++ b/gui/resultstree.cpp @@ -694,11 +694,12 @@ QString ResultsTree::AskFileDir(const QString &file) msgbox.setText(text); msgbox.setIcon(QMessageBox::Warning); msgbox.exec(); - + QString dir = QFileDialog::getExistingDirectory(this, tr("Select Directory"), - "", + GetPath(SETTINGS_LAST_SOURCE_PATH), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); mCheckPath = dir; + SetPath(SETTINGS_LAST_SOURCE_PATH, dir); return dir; } diff --git a/gui/settingsdialog.cpp b/gui/settingsdialog.cpp index 204e91e19..2fb25da3b 100644 --- a/gui/settingsdialog.cpp +++ b/gui/settingsdialog.cpp @@ -310,10 +310,11 @@ void SettingsDialog::AddIncludePath() { QString selectedDir = QFileDialog::getExistingDirectory(this, tr("Select include directory"), - QString()); + GetPath(SETTINGS_LAST_INCLUDE_PATH)); if (!selectedDir.isEmpty()) { AddIncludePath(selectedDir); + SetPath(SETTINGS_LAST_INCLUDE_PATH, selectedDir); } }