From 1af983dd9587a9951ed674ea11bc4071fed611f1 Mon Sep 17 00:00:00 2001 From: bwoester Date: Fri, 25 May 2018 07:09:49 +0200 Subject: [PATCH] Gui select bcb6 projects (#1258) * Allow selecting bcb6 projects in "Analyze" -> "Files..." This change also splits the filters for files which can be analyzed into multiple entries and includes a helper class to construct filter strings. * move FilterStringBuilder to its own class and document it * add new files to .pro * add missing include for Q_DECLARE_TR_FUNCTIONS macro * re-run astyle * allow to import bcb6 project when creating a new cppcheck project exchange class FilterStringBuilder with a toFilterString() helper method * add missing include --- gui/common.cpp | 24 ++++++++++++++++++++++++ gui/common.h | 28 ++++++++++++++++++++++++++++ gui/mainwindow.cpp | 19 ++++++++++++++----- gui/projectfiledialog.cpp | 6 +++++- gui/projectfiledialog.ui | 2 +- 5 files changed, 72 insertions(+), 7 deletions(-) diff --git a/gui/common.cpp b/gui/common.cpp index 58c6a1d2c..bcbb557ae 100644 --- a/gui/common.cpp +++ b/gui/common.cpp @@ -18,6 +18,7 @@ #include "common.h" +#include #include #include #include @@ -42,3 +43,26 @@ void setPath(const QString &type, const QString &value) QSettings settings; settings.setValue(type, value); } + +QString toFilterString(const QMap& filters, bool addAllSupported, bool addAll) +{ + QStringList entries; + + if (addAllSupported) { + entries << QCoreApplication::translate("toFilterString", "All supported files (%1)") + .arg(QStringList(filters.values()).join(" ")); + } + + if (addAll) { + entries << QCoreApplication::translate("toFilterString", "All files (%1)").arg("*.*"); + } + + // We're using the description of the filters as the map keys, the file + // name patterns are our values. The generated filter string list will + // thus be sorted alphabetically over the descriptions. + for (auto k: filters.keys()) { + entries << QString("%1 (%2)").arg(k).arg(filters.value(k)); + } + + return entries.join(";;"); +} diff --git a/gui/common.h b/gui/common.h index 011f2b953..8eb6e665a 100644 --- a/gui/common.h +++ b/gui/common.h @@ -19,6 +19,7 @@ #ifndef COMMON_H #define COMMON_H +#include #include /// @addtogroup GUI @@ -106,6 +107,7 @@ #define SETTINGS_LAST_INCLUDE_PATH "Last include path" #define SETTINGS_LAST_APP_PATH "Last application path" +#define SETTINGS_LAST_ANALYZE_FILES_FILTER "Last analyze files filter" /** * @brief Obtains the path of specified type @@ -124,5 +126,31 @@ QString getPath(const QString &type); */ void setPath(const QString &type, const QString &value); +/** + * @brief Creates a string suitable for passing as the filter argument to + * methods like QFileDialog::getOpenFileName. + * @param filters A map of filter descriptions to the associated file name + * patterns. + * @param addAllSupported If set to true (the default), the function will + * include a filter entry containing all the file name patterns found in + * \p filters. This entry will be the first in the resulting filter string. + * @param addAll If set to true (the default), the function will + * include a filter entry displaying all files. This entry will be placed + * after the entry for \p addAllSupported files. + * + * Example usage: + * + * @code + * QMap filters; + * filters[tr("Supported images")] = "*.bmp *.jpg *.png"; + * filters[tr("Plain text")] = "*.txt"; + * + * const QString filterString = toFilterString(filters); + * + * // filterString contains "All supported files (*.txt *.bmp *.jpg *.png);;All files (*.*);;Plain text (*.txt);;Supported images (*.bmp *.jpg *.png)" + * @endcode + */ +QString toFilterString(const QMap& filters, bool addAllSupported=true, bool addAll=true); + /// @} #endif diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 883ad3c7f..9af8122a0 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -574,13 +574,19 @@ QStringList MainWindow::selectFilesToAnalyze(QFileDialog::FileMode mode) // QFileDialog::getExistingDirectory() because they show native Windows // selection dialog which is a lot more usable than Qt:s own dialog. if (mode == QFileDialog::ExistingFiles) { - + QMap filters; + filters[tr("C/C++ Source")] = FileList::getDefaultFilters().join(" "); + filters[tr("Compile database")] = compile_commands_json; + filters[tr("Visual Studio")] = "*.sln *.vcxproj"; + filters[tr("Borland C++ Builder 6")] = "*.bpr"; + QString lastFilter = mSettings->value(SETTINGS_LAST_ANALYZE_FILES_FILTER).toString(); selected = QFileDialog::getOpenFileNames(this, tr("Select files to analyze"), getPath(SETTINGS_LAST_CHECK_PATH), - tr("C/C++ Source, Compile database, Visual Studio (%1 %2 *.sln *.vcxproj)") - .arg(FileList::getDefaultFilters().join(" ")) - .arg(compile_commands_json)); + toFilterString(filters), + &lastFilter); + mSettings->setValue(SETTINGS_LAST_ANALYZE_FILES_FILTER, lastFilter); + if (selected.isEmpty()) mCurrentDirectory.clear(); else { @@ -613,7 +619,10 @@ void MainWindow::analyzeFiles() QStringList selected = selectFilesToAnalyze(QFileDialog::ExistingFiles); const QString file0 = (selected.size() ? selected[0].toLower() : QString()); - if (file0.endsWith(".sln") || file0.endsWith(".vcxproj") || file0.endsWith(compile_commands_json)) { + if (file0.endsWith(".sln") + || file0.endsWith(".vcxproj") + || file0.endsWith(compile_commands_json) + || file0.endsWith(".bpr")) { ImportProject p; p.import(selected[0].toStdString()); diff --git a/gui/projectfiledialog.cpp b/gui/projectfiledialog.cpp index a1cc69220..bb6311b82 100644 --- a/gui/projectfiledialog.cpp +++ b/gui/projectfiledialog.cpp @@ -365,9 +365,13 @@ void ProjectFileDialog::browseImportProject() { const QFileInfo inf(mProjectFile->getFilename()); const QDir &dir = inf.absoluteDir(); + QMap filters; + filters[tr("Visual Studio")] = "*.sln *.vcxproj"; + filters[tr("Compile database")] = "compile_commands.json"; + filters[tr("Borland C++ Builder 6")] = "*.bpr"; QString fileName = QFileDialog::getOpenFileName(this, tr("Import Project"), dir.canonicalPath(), - tr("Visual Studio (*.sln *.vcxproj);;Compile database (compile_commands.json)")); + toFilterString(filters)); if (!fileName.isEmpty()) { mUI.mEditImportProject->setText(dir.relativeFilePath(fileName)); updatePathsAndDefines(); diff --git a/gui/projectfiledialog.ui b/gui/projectfiledialog.ui index 59af0330c..530155444 100644 --- a/gui/projectfiledialog.ui +++ b/gui/projectfiledialog.ui @@ -27,7 +27,7 @@ - Import Project (Visual studio / compile database) + Import Project (Visual studio / compile database/ Borland C++ Builder 6)