From 7301a3e64d8f02292d69a5f932498082d77e68e5 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Fri, 8 Feb 2013 16:18:05 +0100 Subject: [PATCH 1/3] 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); } } From c6520ffafe59e32fba735e0bb4eba0c1d8bcf754 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Sat, 9 Feb 2013 17:58:51 +0100 Subject: [PATCH 2/3] doxygen comments added --- gui/common.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/gui/common.h b/gui/common.h index 7eee98786..d5482c2ff 100644 --- a/gui/common.h +++ b/gui/common.h @@ -93,7 +93,25 @@ #define SETTINGS_LAST_APP_PATH "Last application path" +/** + * @brief Obtains the path of specified type + * Returns the path of specifed type if not empty. Otherwise returns + * common last used path set previously by any other file dialog window. + * @param type Type of path to obtain + * @return Best path fo provided type + */ QString GetPath(const QString &type); + +/** + * @brief Stores last used path of specified type + * Stores provided path as last used path for specified type and if + * @p storeAsLastUsed is enabled its directory is also set as common last used + * path that will later be used to initialize the value of paths of other types. + * @param type Type of the path to store + * @param value Path to store + * @param storeAsLastUsed Specifies if directory of that path should also be + * stored as common last used path. + */ void SetPath(const QString &type, const QString &value, bool storeAsLastUsed = true); /// @} From 2f47b82090a4221bc385c0cd2726e0cd53d0adb6 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Sun, 17 Feb 2013 19:59:16 +0100 Subject: [PATCH 3/3] Storing last path implemented according to received feedback --- gui/applicationdialog.cpp | 2 +- gui/common.cpp | 23 +++++++++-------------- gui/common.h | 13 ++++--------- gui/mainwindow.cpp | 2 +- 4 files changed, 15 insertions(+), 25 deletions(-) diff --git a/gui/applicationdialog.cpp b/gui/applicationdialog.cpp index 3adc2ec69..e9d6fe5c2 100644 --- a/gui/applicationdialog.cpp +++ b/gui/applicationdialog.cpp @@ -64,7 +64,7 @@ void ApplicationDialog::Browse() filter); if (!selectedFile.isEmpty()) { - SetPath(SETTINGS_LAST_APP_PATH, selectedFile, false); + SetPath(SETTINGS_LAST_APP_PATH, selectedFile); QString path(QDir::toNativeSeparators(selectedFile)); mUI.mPath->setText(path); } diff --git a/gui/common.cpp b/gui/common.cpp index 799721738..10b5beed9 100644 --- a/gui/common.cpp +++ b/gui/common.cpp @@ -26,24 +26,19 @@ 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(); + QString path = settings.value(type, "").toString(); + if (path.isEmpty()) { + // if not set, fallback to last check path hoping that it will be close enough + path = settings.value(SETTINGS_LAST_CHECK_PATH, "").toString(); + if (path.isEmpty()) + // if not set, return user's home directory as the best we can do for now + return QDir::homePath(); + } return path; } -void SetPath(const QString &type, const QString &value, bool storeAsLastUsed /* = true */) +void SetPath(const QString &type, const QString &value) { 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 d5482c2ff..d13eddcf9 100644 --- a/gui/common.h +++ b/gui/common.h @@ -84,7 +84,6 @@ #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" @@ -95,8 +94,8 @@ /** * @brief Obtains the path of specified type - * Returns the path of specifed type if not empty. Otherwise returns - * common last used path set previously by any other file dialog window. + * Returns the path of specified type if not empty. Otherwise returns last check + * path if valid or user's home directory. * @param type Type of path to obtain * @return Best path fo provided type */ @@ -104,15 +103,11 @@ QString GetPath(const QString &type); /** * @brief Stores last used path of specified type - * Stores provided path as last used path for specified type and if - * @p storeAsLastUsed is enabled its directory is also set as common last used - * path that will later be used to initialize the value of paths of other types. + * Stores provided path as last used path for specified type. * @param type Type of the path to store * @param value Path to store - * @param storeAsLastUsed Specifies if directory of that path should also be - * stored as common last used path. */ -void SetPath(const QString &type, const QString &value, bool storeAsLastUsed = true); +void SetPath(const QString &type, const QString &value); /// @} #endif diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index a263993bf..c94352eb9 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -332,7 +332,7 @@ void MainWindow::DoCheckFiles(const QStringList &files) mThread->SetFiles(fileNames); QDir inf(mCurrentDirectory); const QString checkPath = inf.canonicalPath(); - mSettings->setValue(SETTINGS_CHECK_PATH, checkPath); + SetPath(SETTINGS_LAST_CHECK_PATH, checkPath); CheckLockDownUI(); // lock UI while checking