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
This commit is contained in:
bwoester 2018-05-25 07:09:49 +02:00 committed by Daniel Marjamäki
parent 742d6513a1
commit 1af983dd95
5 changed files with 72 additions and 7 deletions

View File

@ -18,6 +18,7 @@
#include "common.h" #include "common.h"
#include <QCoreApplication>
#include <QSettings> #include <QSettings>
#include <QFileInfo> #include <QFileInfo>
#include <QDir> #include <QDir>
@ -42,3 +43,26 @@ void setPath(const QString &type, const QString &value)
QSettings settings; QSettings settings;
settings.setValue(type, value); settings.setValue(type, value);
} }
QString toFilterString(const QMap<QString,QString>& 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(";;");
}

View File

@ -19,6 +19,7 @@
#ifndef COMMON_H #ifndef COMMON_H
#define COMMON_H #define COMMON_H
#include <QMap>
#include <QString> #include <QString>
/// @addtogroup GUI /// @addtogroup GUI
@ -106,6 +107,7 @@
#define SETTINGS_LAST_INCLUDE_PATH "Last include path" #define SETTINGS_LAST_INCLUDE_PATH "Last include path"
#define SETTINGS_LAST_APP_PATH "Last application 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 * @brief Obtains the path of specified type
@ -124,5 +126,31 @@ QString getPath(const QString &type);
*/ */
void setPath(const QString &type, const QString &value); 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<QString,QString> 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<QString,QString>& filters, bool addAllSupported=true, bool addAll=true);
/// @} /// @}
#endif #endif

View File

@ -574,13 +574,19 @@ QStringList MainWindow::selectFilesToAnalyze(QFileDialog::FileMode mode)
// QFileDialog::getExistingDirectory() because they show native Windows // QFileDialog::getExistingDirectory() because they show native Windows
// selection dialog which is a lot more usable than Qt:s own dialog. // selection dialog which is a lot more usable than Qt:s own dialog.
if (mode == QFileDialog::ExistingFiles) { if (mode == QFileDialog::ExistingFiles) {
QMap<QString,QString> 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, selected = QFileDialog::getOpenFileNames(this,
tr("Select files to analyze"), tr("Select files to analyze"),
getPath(SETTINGS_LAST_CHECK_PATH), getPath(SETTINGS_LAST_CHECK_PATH),
tr("C/C++ Source, Compile database, Visual Studio (%1 %2 *.sln *.vcxproj)") toFilterString(filters),
.arg(FileList::getDefaultFilters().join(" ")) &lastFilter);
.arg(compile_commands_json)); mSettings->setValue(SETTINGS_LAST_ANALYZE_FILES_FILTER, lastFilter);
if (selected.isEmpty()) if (selected.isEmpty())
mCurrentDirectory.clear(); mCurrentDirectory.clear();
else { else {
@ -613,7 +619,10 @@ void MainWindow::analyzeFiles()
QStringList selected = selectFilesToAnalyze(QFileDialog::ExistingFiles); QStringList selected = selectFilesToAnalyze(QFileDialog::ExistingFiles);
const QString file0 = (selected.size() ? selected[0].toLower() : QString()); 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; ImportProject p;
p.import(selected[0].toStdString()); p.import(selected[0].toStdString());

View File

@ -365,9 +365,13 @@ void ProjectFileDialog::browseImportProject()
{ {
const QFileInfo inf(mProjectFile->getFilename()); const QFileInfo inf(mProjectFile->getFilename());
const QDir &dir = inf.absoluteDir(); const QDir &dir = inf.absoluteDir();
QMap<QString,QString> 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"), QString fileName = QFileDialog::getOpenFileName(this, tr("Import Project"),
dir.canonicalPath(), dir.canonicalPath(),
tr("Visual Studio (*.sln *.vcxproj);;Compile database (compile_commands.json)")); toFilterString(filters));
if (!fileName.isEmpty()) { if (!fileName.isEmpty()) {
mUI.mEditImportProject->setText(dir.relativeFilePath(fileName)); mUI.mEditImportProject->setText(dir.relativeFilePath(fileName));
updatePathsAndDefines(); updatePathsAndDefines();

View File

@ -27,7 +27,7 @@
<item> <item>
<widget class="QGroupBox" name="groupBox_4"> <widget class="QGroupBox" name="groupBox_4">
<property name="title"> <property name="title">
<string>Import Project (Visual studio / compile database)</string> <string>Import Project (Visual studio / compile database/ Borland C++ Builder 6)</string>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_4"> <layout class="QVBoxLayout" name="verticalLayout_4">
<item> <item>