Cppcheck
mainwindow.h
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2023 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef MAINWINDOW_H
20 #define MAINWINDOW_H
21 
22 #include "library.h"
23 #include "platforms.h"
24 
25 #include <QFileDialog>
26 #include <QMainWindow>
27 #include <QObject>
28 #include <QPair>
29 #include <QString>
30 #include <QStringList>
31 
32 class ThreadHandler;
33 class TranslationHandler;
34 class ScratchPad;
35 class ProjectFile;
36 class ApplicationList;
37 class QAction;
38 class QActionGroup;
39 class QSettings;
40 class QTimer;
41 class QLineEdit;
42 class ImportProject;
43 class QCloseEvent;
44 class QNetworkAccessManager;
45 class QNetworkReply;
46 class Settings;
47 namespace Ui {
48  class MainWindow;
49 }
50 
51 /// @addtogroup GUI
52 /// @{
53 
54 /**
55  * @brief Main window for cppcheck-gui
56  *
57  */
58 class MainWindow : public QMainWindow {
59  Q_OBJECT
60 public:
61 
62  /**
63  * @brief Maximum number of MRU project items in File-menu.
64  */
65  enum { MaxRecentProjects = 5 };
66 
67  MainWindow(TranslationHandler* th, QSettings* settings);
68  MainWindow(const MainWindow &) = delete;
69  ~MainWindow() override;
70  MainWindow &operator=(const MainWindow &) = delete;
71 
72  /**
73  * List of checked platforms.
74  */
76 
77  /**
78  * @brief Analyze given code
79  *
80  * @param code Content of the (virtual) file to be analyzed
81  * @param filename Name of the (virtual) file to be analyzed - determines language.
82  */
83  void analyzeCode(const QString& code, const QString& filename);
84 
85 public slots:
86  /** @brief Slot for analyze files menu item */
87  void analyzeFiles();
88 
89  /** @brief Slot to reanalyze all files */
90  void reAnalyzeAll();
91 
92  /** @brief Slot to reanalyze with checking library configuration */
93  void checkLibrary();
94 
95  /** @brief Slot to check configuration */
96  void checkConfiguration();
97 
98  /**
99  * @brief Slot to reanalyze selected files
100  * @param selectedFilesList list of selected files
101  */
102  void performSelectedFilesCheck(const QStringList &selectedFilesList);
103 
104  /** @brief Slot to reanalyze modified files */
105  void reAnalyzeModified();
106 
107  /** @brief Slot to clear all search results */
108  void clearResults();
109 
110  /** @brief Slot to open XML report file */
111  void openResults();
112 
113  /**
114  * @brief Show errors with type "style"
115  * @param checked Should errors be shown (true) or hidden (false)
116  */
117  void showStyle(bool checked);
118 
119  /**
120  * @brief Show errors with type "error"
121  * @param checked Should errors be shown (true) or hidden (false)
122  */
123  void showErrors(bool checked);
124 
125  /**
126  * @brief Show errors with type "warning"
127  * @param checked Should errors be shown (true) or hidden (false)
128  */
129  void showWarnings(bool checked);
130 
131  /**
132  * @brief Show errors with type "portability"
133  * @param checked Should errors be shown (true) or hidden (false)
134  */
135  void showPortability(bool checked);
136 
137  /**
138  * @brief Show errors with type "performance"
139  * @param checked Should errors be shown (true) or hidden (false)
140  */
141  void showPerformance(bool checked);
142 
143  /**
144  * @brief Show errors with type "information"
145  * @param checked Should errors be shown (true) or hidden (false)
146  */
147  void showInformation(bool checked);
148 
149  /** @brief Slot to check all "Show errors" menu items */
150  void checkAll();
151 
152  /** @brief Slot to uncheck all "Show errors" menu items */
153  void uncheckAll();
154 
155  /** @brief Slot for analyze directory menu item */
156  void analyzeDirectory();
157 
158  /** @brief Slot to open program's settings dialog */
159  void programSettings();
160 
161  /** @brief Slot to open program's about dialog */
162  void about();
163 
164  /** @brief Slot to to show license text */
165  void showLicense();
166 
167  /** @brief Slot to to show authors list */
168  void showAuthors();
169 
170  /** @brief Slot to save results */
171  void save();
172 
173  /** @brief Slot to generate compliance report */
174  void complianceReport();
175 
176  /** @brief Slot to create new project file */
177  void newProjectFile();
178 
179  /** @brief Slot to open project file and start analyzing contained paths. */
180  void openProjectFile();
181 
182  /** @brief Slot to show scratchpad. */
183  void showScratchpad();
184 
185  /** @brief Slot to close open project file. */
186  void closeProjectFile();
187 
188  /** @brief Slot to edit project file. */
189  void editProjectFile();
190 
191  /** @brief Slot for showing the scan and project statistics. */
192  void showStatistics();
193 
194  /** @brief Slot for showing the library editor */
195  void showLibraryEditor();
196 
197 private slots:
198 
199  /** @brief Slot for checkthread's done signal */
200  void analysisDone();
201 
202  /** @brief Lock down UI while analyzing */
203  void checkLockDownUI();
204 
205  /** @brief Slot for enabling save and clear button */
206  void resultsAdded();
207 
208  /** @brief Slot for showing/hiding standard toolbar */
209  void toggleMainToolBar();
210 
211  /** @brief Slot for showing/hiding Categories toolbar */
212  void toggleViewToolBar();
213 
214  /** @brief Slot for showing/hiding Filter toolbar */
215  void toggleFilterToolBar();
216 
217  /** @brief Slot for updating View-menu before it is shown. */
218  void aboutToShowViewMenu();
219 
220  /** @brief Slot when stop analysis button is pressed */
221  void stopAnalysis();
222 
223  /** @brief Open help file contents */
224  void openHelpContents();
225 
226  /** @brief Filters the results in the result list. */
227  void filterResults();
228 
229  /** @brief Opens recently opened project file. */
230  void openRecentProject();
231 
232  /** @brief Selects the platform as analyzed platform. */
233  void selectPlatform();
234 
235  /** Suppress error ids */
236  void suppressIds(QStringList ids);
237 
238 private slots:
239  void replyFinished(QNetworkReply *reply);
240 
241  void hideInformation();
242 private:
243 
244  bool isCppcheckPremium() const;
245 
246  /** Get filename for last results */
247  QString getLastResults() const;
248 
249  /** @brief Reanalyzes files */
250  void reAnalyze(bool all);
251 
252  /**
253  * @brief Reanalyze selected files
254  * @param files list of selected files
255  */
256  void reAnalyzeSelected(const QStringList& files);
257 
258  /**
259  * @brief Analyze the project.
260  * @param projectFile Pointer to the project to analyze.
261  * @param checkLibrary Flag to indicate if the library should be checked.
262  * @param checkConfiguration Flag to indicate if the configuration should be checked.
263  */
264  void analyzeProject(const ProjectFile *projectFile, const bool checkLibrary = false, const bool checkConfiguration = false);
265 
266  /**
267  * @brief Set current language
268  * @param code Language code of the language to set (e.g. "en").
269  */
270  void setLanguage(const QString &code);
271 
272  /** @brief Event coming when application is about to close. */
273  void closeEvent(QCloseEvent *event) override;
274 
275  /**
276  * @brief Helper function to toggle all show error menu items
277  * @param checked Should all errors be shown (true) or hidden (false)
278  */
279  void toggleAllChecked(bool checked);
280 
281  /** @brief Helper function to enable/disable all check,recheck buttons */
282  void enableCheckButtons(bool enable);
283 
284  /** @brief Helper function to enable/disable results buttons (clear,save,print) */
285  void enableResultsButtons();
286 
287  /**
288  * @brief Select files/or directory to analyze.
289  * Helper function to open a dialog to ask user to select files or
290  * directory to analyze. Use native dialogs instead of Qt:s own dialogs.
291  *
292  * @param mode Dialog open mode (files or directories)
293  * @return QStringList of files or directories that were selected to analyze
294  */
295  QStringList selectFilesToAnalyze(QFileDialog::FileMode mode);
296 
297  /**
298  * @brief Analyze project
299  * @param p imported project
300  * @param checkLibrary Flag to indicate if library should be checked
301  * @param checkConfiguration Flag to indicate if the configuration should be checked.
302  */
303  void doAnalyzeProject(ImportProject p, const bool checkLibrary = false, const bool checkConfiguration = false);
304 
305  /**
306  * @brief Analyze all files specified in parameter files
307  *
308  * @param files List of files and/or directories to analyze
309  * @param checkLibrary Flag to indicate if library should be checked
310  * @param checkConfiguration Flag to indicate if the configuration should be checked.
311  */
312  void doAnalyzeFiles(const QStringList &files, const bool checkLibrary = false, const bool checkConfiguration = false);
313 
314  /**
315  * @brief Get our default cppcheck settings and read project file.
316  *
317  * @return Default cppcheck settings
318  */
319  QPair<bool, Settings> getCppcheckSettings();
320 
321  /** @brief Load program settings */
322  void loadSettings();
323 
324  /** @brief Save program settings */
325  void saveSettings() const;
326 
327  /**
328  * @brief Format main window title.
329  * @param text Text added to end of the title.
330  */
331  void formatAndSetTitle(const QString &text = QString());
332 
333  /** @brief Show help contents */
334  static void openOnlineHelp();
335 
336  /**
337  * @brief Enable or disable project file actions.
338  * Project editing and closing actions should be only enabled when project is
339  * open and we are not analyzing files.
340  * @param enable If true then actions are enabled.
341  */
342  void enableProjectActions(bool enable);
343 
344  /**
345  * @brief Enable or disable project file actions.
346  * Project opening and creating actions should be disabled when analyzing.
347  * @param enable If true then actions are enabled.
348  */
349  void enableProjectOpenActions(bool enable);
350 
351  /**
352  * @brief Add include directories.
353  * @param includeDirs List of include directories to add.
354  * @param result Settings class where include directories are added.
355  */
356  void addIncludeDirs(const QStringList &includeDirs, Settings &result);
357 
358  /**
359  * @brief Handle command line parameters given to GUI.
360  * @param params List of string given to command line.
361  */
362  void handleCLIParams(const QStringList &params);
363 
364  /**
365  * @brief Load XML file to the GUI.
366  * @param selectedFile Filename (inc. path) of XML file to load.
367  */
368  void loadResults(const QString &selectedFile);
369 
370  /**
371  * @brief Load XML file to the GUI.
372  * @param selectedFile Filename (inc. path) of XML file to load.
373  * @param sourceDirectory Path to the directory that the results were generated for.
374  */
375  void loadResults(const QString &selectedFile, const QString &sourceDirectory);
376 
377  /**
378  * @brief Load last project results to the GUI.
379  * @return Returns true if last results was loaded
380  */
381  bool loadLastResults();
382 
383  /**
384  * @brief Load project file to the GUI.
385  * @param filePath Filename (inc. path) of project file to load.
386  */
387  void loadProjectFile(const QString &filePath);
388 
389  /**
390  * @brief Load library file
391  * @param library library to use
392  * @param filename filename (no path)
393  * @return error code
394  */
395  Library::Error loadLibrary(Library &library, const QString &filename);
396 
397  /**
398  * @brief Tries to load library file, prints message on error
399  * @param library library to use
400  * @param filename filename (no path)
401  * @return True if no error
402  */
403  bool tryLoadLibrary(Library &library, const QString& filename);
404 
405  QString loadAddon(Settings &settings, const QString &filesDir, const QString &pythonCmd, const QString& addon);
406 
407  /**
408  * @brief Update project MRU items in File-menu.
409  */
410  void updateMRUMenuItems();
411 
412  /**
413  * @brief Add project file (path) to the MRU list.
414  * @param project Full path to the project file to add.
415  */
416  void addProjectMRU(const QString &project);
417 
418  /**
419  * @brief Remove project file (path) from the MRU list.
420  * @param project Full path of the project file to remove.
421  */
422  void removeProjectMRU(const QString &project);
423 
424  /** @brief Program settings */
425  QSettings *mSettings;
426 
427  /** @brief Thread to analyze files */
429 
430  /** @brief List of user defined applications to open errors with */
432 
433  /** @brief Class to handle translation changes */
435 
436  /** @brief Class holding all UI components */
437  Ui::MainWindow *mUI;
438 
439  /** @brief Current analyzed directory. */
441 
442  /** @brief Scratchpad. */
444 
445  /** @brief Project (file). */
447 
448  /** @brief Filter field in the Filter toolbar. */
449  QLineEdit* mLineEditFilter;
450 
451  /** @brief Timer to delay filtering while typing. */
452  QTimer* mFilterTimer;
453 
454  /** @brief GUI actions for selecting the analyzed platform. */
455  QActionGroup *mPlatformActions;
456 
457  /** @brief GUI actions for selecting the coding standard. */
459 
460  /** @brief GUI actions for selecting language. */
461  QActionGroup *mSelectLanguageActions;
462 
463  /**
464  * @brief Are we exiting the cppcheck?
465  * If this is true then the cppcheck is waiting for check threads to exit
466  * so that the application can be closed.
467  */
468  bool mExiting{};
469 
470  /** @brief Set to true in case of loading log file. */
472 
473  /**
474  * @brief Project MRU menu actions.
475  * List of MRU menu actions. Needs also to store the separator.
476  */
478 
481 
482  QNetworkAccessManager *mNetworkAccessManager = nullptr;
483 };
484 /// @}
485 #endif // MAINWINDOW_H
List of applications user has specified to open errors with.
Importing project settings.
Definition: importproject.h:52
Library definitions handling.
Definition: library.h:52
Main window for cppcheck-gui.
Definition: mainwindow.h:58
void filterResults()
Filters the results in the result list.
void newProjectFile()
Slot to create new project file.
void enableProjectActions(bool enable)
Enable or disable project file actions.
void reAnalyzeAll()
Slot to reanalyze all files.
void closeEvent(QCloseEvent *event) override
Event coming when application is about to close.
bool loadLastResults()
Load last project results to the GUI.
QNetworkAccessManager * mNetworkAccessManager
Definition: mainwindow.h:482
ThreadHandler * mThread
Thread to analyze files.
Definition: mainwindow.h:428
void loadResults(const QString &selectedFile)
Load XML file to the GUI.
QStringList selectFilesToAnalyze(QFileDialog::FileMode mode)
Select files/or directory to analyze.
Definition: mainwindow.cpp:660
void showStatistics()
Slot for showing the scan and project statistics.
void showErrors(bool checked)
Show errors with type "error".
~MainWindow() override
Definition: mainwindow.cpp:313
void setLanguage(const QString &code)
Set current language.
bool mExiting
Are we exiting the cppcheck? If this is true then the cppcheck is waiting for check threads to exit s...
Definition: mainwindow.h:468
void uncheckAll()
Slot to uncheck all "Show errors" menu items.
void handleCLIParams(const QStringList &params)
Handle command line parameters given to GUI.
Definition: mainwindow.cpp:320
void showScratchpad()
Slot to show scratchpad.
void hideInformation()
void toggleMainToolBar()
Slot for showing/hiding standard toolbar.
void closeProjectFile()
Slot to close open project file.
void loadProjectFile(const QString &filePath)
Load project file to the GUI.
void enableProjectOpenActions(bool enable)
Enable or disable project file actions.
void analyzeCode(const QString &code, const QString &filename)
Analyze given code.
Definition: mainwindow.cpp:625
void save()
Slot to save results.
void checkAll()
Slot to check all "Show errors" menu items.
void clearResults()
Slot to clear all search results.
void doAnalyzeFiles(const QStringList &files, const bool checkLibrary=false, const bool checkConfiguration=false)
Analyze all files specified in parameter files.
Definition: mainwindow.cpp:557
void enableCheckButtons(bool enable)
Helper function to enable/disable all check,recheck buttons.
QAction * mRecentProjectActs[MaxRecentProjects+1]
Project MRU menu actions.
Definition: mainwindow.h:477
void replyFinished(QNetworkReply *reply)
void openHelpContents()
Open help file contents.
QString mCppcheckCfgProductName
Definition: mainwindow.h:480
void addProjectMRU(const QString &project)
Add project file (path) to the MRU list.
bool tryLoadLibrary(Library &library, const QString &filename)
Tries to load library file, prints message on error.
Definition: mainwindow.cpp:874
void checkLibrary()
Slot to reanalyze with checking library configuration.
void showLicense()
Slot to to show license text.
void toggleViewToolBar()
Slot for showing/hiding Categories toolbar.
bool mIsLogfileLoaded
Set to true in case of loading log file.
Definition: mainwindow.h:471
void reAnalyzeSelected(const QStringList &files)
Reanalyze selected files.
void analysisDone()
Slot for checkthread's done signal.
void analyzeDirectory()
Slot for analyze directory menu item.
Definition: mainwindow.cpp:753
Library::Error loadLibrary(Library &library, const QString &filename)
Load library file.
Definition: mainwindow.cpp:826
void updateMRUMenuItems()
Update project MRU items in File-menu.
ScratchPad * mScratchPad
Scratchpad.
Definition: mainwindow.h:443
void showInformation(bool checked)
Show errors with type "information".
QActionGroup * mCppStandardActions
Definition: mainwindow.h:458
QTimer * mFilterTimer
Timer to delay filtering while typing.
Definition: mainwindow.h:452
void removeProjectMRU(const QString &project)
Remove project file (path) from the MRU list.
QActionGroup * mCStandardActions
GUI actions for selecting the coding standard.
Definition: mainwindow.h:458
void programSettings()
Slot to open program's settings dialog.
void about()
Slot to open program's about dialog.
void showStyle(bool checked)
Show errors with type "style".
void analyzeFiles()
Slot for analyze files menu item.
Definition: mainwindow.cpp:716
void openRecentProject()
Opens recently opened project file.
void showLibraryEditor()
Slot for showing the library editor.
void addIncludeDirs(const QStringList &includeDirs, Settings &result)
Add include directories.
Definition: mainwindow.cpp:810
QLineEdit * mLineEditFilter
Filter field in the Filter toolbar.
Definition: mainwindow.h:449
Platforms mPlatforms
List of checked platforms.
Definition: mainwindow.h:75
QString loadAddon(Settings &settings, const QString &filesDir, const QString &pythonCmd, const QString &addon)
Definition: mainwindow.cpp:926
ApplicationList * mApplications
List of user defined applications to open errors with.
Definition: mainwindow.h:431
void saveSettings() const
Save program settings.
Definition: mainwindow.cpp:433
@ MaxRecentProjects
Definition: mainwindow.h:65
void openResults()
Slot to open XML report file.
void loadSettings()
Load program settings.
Definition: mainwindow.cpp:351
void openProjectFile()
Slot to open project file and start analyzing contained paths.
void enableResultsButtons()
Helper function to enable/disable results buttons (clear,save,print)
QActionGroup * mSelectLanguageActions
GUI actions for selecting language.
Definition: mainwindow.h:461
MainWindow & operator=(const MainWindow &)=delete
static void openOnlineHelp()
Show help contents.
Ui::MainWindow * mUI
Class holding all UI components.
Definition: mainwindow.h:437
void aboutToShowViewMenu()
Slot for updating View-menu before it is shown.
void analyzeProject(const ProjectFile *projectFile, const bool checkLibrary=false, const bool checkConfiguration=false)
Analyze the project.
bool isCppcheckPremium() const
ProjectFile * mProjectFile
Project (file).
Definition: mainwindow.h:446
void showPerformance(bool checked)
Show errors with type "performance".
TranslationHandler * mTranslation
Class to handle translation changes.
Definition: mainwindow.h:434
void resultsAdded()
Slot for enabling save and clear button.
QString mCurrentDirectory
Current analyzed directory.
Definition: mainwindow.h:440
QActionGroup * mPlatformActions
GUI actions for selecting the analyzed platform.
Definition: mainwindow.h:455
QSettings * mSettings
Program settings.
Definition: mainwindow.h:425
QPair< bool, Settings > getCppcheckSettings()
Get our default cppcheck settings and read project file.
Definition: mainwindow.cpp:963
void doAnalyzeProject(ImportProject p, const bool checkLibrary=false, const bool checkConfiguration=false)
Analyze project.
Definition: mainwindow.cpp:489
void toggleFilterToolBar()
Slot for showing/hiding Filter toolbar.
void toggleAllChecked(bool checked)
Helper function to toggle all show error menu items.
void stopAnalysis()
Slot when stop analysis button is pressed.
MainWindow(TranslationHandler *th, QSettings *settings)
Definition: mainwindow.cpp:113
void showPortability(bool checked)
Show errors with type "portability".
QString mCppcheckCfgAbout
Definition: mainwindow.h:479
QString getLastResults() const
Get filename for last results.
void complianceReport()
Slot to generate compliance report.
void showWarnings(bool checked)
Show errors with type "warning".
MainWindow(const MainWindow &)=delete
void reAnalyzeModified()
Slot to reanalyze modified files.
void checkConfiguration()
Slot to check configuration.
void formatAndSetTitle(const QString &text=QString())
Format main window title.
void suppressIds(QStringList ids)
Suppress error ids.
void showAuthors()
Slot to to show authors list.
void editProjectFile()
Slot to edit project file.
void selectPlatform()
Selects the platform as analyzed platform.
void reAnalyze(bool all)
Reanalyzes files.
void checkLockDownUI()
Lock down UI while analyzing.
void performSelectedFilesCheck(const QStringList &selectedFilesList)
Slot to reanalyze selected files.
List of checked platforms.
Definition: platforms.h:45
A class that reads and writes project files.
Definition: projectfile.h:46
A window with a text field that .
Definition: scratchpad.h:37
This is just a container for general settings so that we don't need to pass individual values to func...
Definition: settings.h:95
This class handles creating threadresult and starting threads.
Definition: threadhandler.h:50
A class handling the available translations.
Definition: aboutdialog.h:27