Skip to content

Commit 1af983d

Browse files
bwoesterdanmar
authored andcommitted
Gui select bcb6 projects (cppcheck-opensource#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
1 parent 742d651 commit 1af983d

5 files changed

Lines changed: 72 additions & 7 deletions

File tree

gui/common.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919

2020
#include "common.h"
21+
#include <QCoreApplication>
2122
#include <QSettings>
2223
#include <QFileInfo>
2324
#include <QDir>
@@ -42,3 +43,26 @@ void setPath(const QString &type, const QString &value)
4243
QSettings settings;
4344
settings.setValue(type, value);
4445
}
46+
47+
QString toFilterString(const QMap<QString,QString>& filters, bool addAllSupported, bool addAll)
48+
{
49+
QStringList entries;
50+
51+
if (addAllSupported) {
52+
entries << QCoreApplication::translate("toFilterString", "All supported files (%1)")
53+
.arg(QStringList(filters.values()).join(" "));
54+
}
55+
56+
if (addAll) {
57+
entries << QCoreApplication::translate("toFilterString", "All files (%1)").arg("*.*");
58+
}
59+
60+
// We're using the description of the filters as the map keys, the file
61+
// name patterns are our values. The generated filter string list will
62+
// thus be sorted alphabetically over the descriptions.
63+
for (auto k: filters.keys()) {
64+
entries << QString("%1 (%2)").arg(k).arg(filters.value(k));
65+
}
66+
67+
return entries.join(";;");
68+
}

gui/common.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#ifndef COMMON_H
2020
#define COMMON_H
2121

22+
#include <QMap>
2223
#include <QString>
2324

2425
/// @addtogroup GUI
@@ -106,6 +107,7 @@
106107
#define SETTINGS_LAST_INCLUDE_PATH "Last include path"
107108
#define SETTINGS_LAST_APP_PATH "Last application path"
108109

110+
#define SETTINGS_LAST_ANALYZE_FILES_FILTER "Last analyze files filter"
109111

110112
/**
111113
* @brief Obtains the path of specified type
@@ -124,5 +126,31 @@ QString getPath(const QString &type);
124126
*/
125127
void setPath(const QString &type, const QString &value);
126128

129+
/**
130+
* @brief Creates a string suitable for passing as the filter argument to
131+
* methods like QFileDialog::getOpenFileName.
132+
* @param filters A map of filter descriptions to the associated file name
133+
* patterns.
134+
* @param addAllSupported If set to true (the default), the function will
135+
* include a filter entry containing all the file name patterns found in
136+
* \p filters. This entry will be the first in the resulting filter string.
137+
* @param addAll If set to true (the default), the function will
138+
* include a filter entry displaying all files. This entry will be placed
139+
* after the entry for \p addAllSupported files.
140+
*
141+
* Example usage:
142+
*
143+
* @code
144+
* QMap<QString,QString> filters;
145+
* filters[tr("Supported images")] = "*.bmp *.jpg *.png";
146+
* filters[tr("Plain text")] = "*.txt";
147+
*
148+
* const QString filterString = toFilterString(filters);
149+
*
150+
* // filterString contains "All supported files (*.txt *.bmp *.jpg *.png);;All files (*.*);;Plain text (*.txt);;Supported images (*.bmp *.jpg *.png)"
151+
* @endcode
152+
*/
153+
QString toFilterString(const QMap<QString,QString>& filters, bool addAllSupported=true, bool addAll=true);
154+
127155
/// @}
128156
#endif

gui/mainwindow.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -574,13 +574,19 @@ QStringList MainWindow::selectFilesToAnalyze(QFileDialog::FileMode mode)
574574
// QFileDialog::getExistingDirectory() because they show native Windows
575575
// selection dialog which is a lot more usable than Qt:s own dialog.
576576
if (mode == QFileDialog::ExistingFiles) {
577-
577+
QMap<QString,QString> filters;
578+
filters[tr("C/C++ Source")] = FileList::getDefaultFilters().join(" ");
579+
filters[tr("Compile database")] = compile_commands_json;
580+
filters[tr("Visual Studio")] = "*.sln *.vcxproj";
581+
filters[tr("Borland C++ Builder 6")] = "*.bpr";
582+
QString lastFilter = mSettings->value(SETTINGS_LAST_ANALYZE_FILES_FILTER).toString();
578583
selected = QFileDialog::getOpenFileNames(this,
579584
tr("Select files to analyze"),
580585
getPath(SETTINGS_LAST_CHECK_PATH),
581-
tr("C/C++ Source, Compile database, Visual Studio (%1 %2 *.sln *.vcxproj)")
582-
.arg(FileList::getDefaultFilters().join(" "))
583-
.arg(compile_commands_json));
586+
toFilterString(filters),
587+
&lastFilter);
588+
mSettings->setValue(SETTINGS_LAST_ANALYZE_FILES_FILTER, lastFilter);
589+
584590
if (selected.isEmpty())
585591
mCurrentDirectory.clear();
586592
else {
@@ -613,7 +619,10 @@ void MainWindow::analyzeFiles()
613619
QStringList selected = selectFilesToAnalyze(QFileDialog::ExistingFiles);
614620

615621
const QString file0 = (selected.size() ? selected[0].toLower() : QString());
616-
if (file0.endsWith(".sln") || file0.endsWith(".vcxproj") || file0.endsWith(compile_commands_json)) {
622+
if (file0.endsWith(".sln")
623+
|| file0.endsWith(".vcxproj")
624+
|| file0.endsWith(compile_commands_json)
625+
|| file0.endsWith(".bpr")) {
617626
ImportProject p;
618627
p.import(selected[0].toStdString());
619628

gui/projectfiledialog.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,13 @@ void ProjectFileDialog::browseImportProject()
365365
{
366366
const QFileInfo inf(mProjectFile->getFilename());
367367
const QDir &dir = inf.absoluteDir();
368+
QMap<QString,QString> filters;
369+
filters[tr("Visual Studio")] = "*.sln *.vcxproj";
370+
filters[tr("Compile database")] = "compile_commands.json";
371+
filters[tr("Borland C++ Builder 6")] = "*.bpr";
368372
QString fileName = QFileDialog::getOpenFileName(this, tr("Import Project"),
369373
dir.canonicalPath(),
370-
tr("Visual Studio (*.sln *.vcxproj);;Compile database (compile_commands.json)"));
374+
toFilterString(filters));
371375
if (!fileName.isEmpty()) {
372376
mUI.mEditImportProject->setText(dir.relativeFilePath(fileName));
373377
updatePathsAndDefines();

gui/projectfiledialog.ui

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<item>
2828
<widget class="QGroupBox" name="groupBox_4">
2929
<property name="title">
30-
<string>Import Project (Visual studio / compile database)</string>
30+
<string>Import Project (Visual studio / compile database/ Borland C++ Builder 6)</string>
3131
</property>
3232
<layout class="QVBoxLayout" name="verticalLayout_4">
3333
<item>

0 commit comments

Comments
 (0)