Cppcheck
resultstree.h
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2024 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 
20 #ifndef RESULTSTREE_H
21 #define RESULTSTREE_H
22 
23 #include "showtypes.h"
24 
25 #include <QObject>
26 #include <QStandardItemModel>
27 #include <QString>
28 #include <QStringList>
29 #include <QTreeView>
30 
31 class ApplicationList;
32 class Report;
33 class ErrorItem;
34 class ErrorLine;
35 class QModelIndex;
36 class QWidget;
37 class QItemSelectionModel;
38 class ThreadHandler;
39 class QContextMenuEvent;
40 class QKeyEvent;
41 class QSettings;
42 enum class Severity;
43 
44 /// @addtogroup GUI
45 /// @{
46 
47 
48 /**
49  * @brief Cppcheck's results are shown in this tree
50  *
51  */
52 class ResultsTree : public QTreeView {
53  Q_OBJECT
54 public:
55  explicit ResultsTree(QWidget * parent = nullptr);
56 
57  void initialize(QSettings *settings, ApplicationList *list, ThreadHandler *checkThreadHandler);
58 
59  /**
60  * @brief Add a new item to the tree
61  *
62  * @param item Error item data
63  */
64  bool addErrorItem(const ErrorItem &item);
65 
66  /**
67  * @brief Clear all errors from the tree
68  *
69  */
70  void clear();
71 
72  /**
73  * @brief Clear errors for a specific file from the tree
74  */
75  void clear(const QString &filename);
76 
77  /**
78  * @brief Clear errors of a file selected for recheck
79  */
80  void clearRecheckFile(const QString &filename);
81 
82  /**
83  * @brief Function to filter the displayed list of errors.
84  * Refreshes the tree.
85  *
86  * @param filter String that must be found in the summary, description, file or id
87  */
88  void filterResults(const QString& filter);
89 
90  /**
91  * @brief Function to show results that were previous hidden with HideResult()
92  */
93  void showHiddenResults();
94 
95  /**
96  * @brief Refresh tree by checking which of the items should be shown
97  * and which should be hidden
98  */
99  void refreshTree();
100 
101  /**
102  * @brief Save results to a text stream
103  *
104  */
105  void saveResults(Report *report) const;
106 
107  /**
108  * @brief Update items from old report (tag, sinceDate)
109  */
110  void updateFromOldReport(const QString &filename);
111 
112  /**
113  * @brief Update tree settings
114  *
115  * @param showFullPath Show full path of files in the tree
116  * @param saveFullPath Save full path of files in reports
117  * @param saveAllErrors Save all visible errors
118  * @param showErrorId Show error id
119  * @param showInconclusive Show inconclusive column
120  */
121  void updateSettings(bool showFullPath, bool saveFullPath, bool saveAllErrors, bool showErrorId, bool showInconclusive);
122 
123  /**
124  * @brief Set the directory we are checking
125  *
126  * This is used to split error file path to relative if necessary
127  * @param dir Directory we are checking
128  */
129  void setCheckDirectory(const QString &dir);
130 
131  /**
132  * @brief Get the directory we are checking
133  *
134  * @return Directory containing source files
135  */
136 
137  const QString& getCheckDirectory();
138 
139  /**
140  * @brief Check if there are any visible results in view.
141  * @return true if there is at least one visible warning/error.
142  */
143  bool hasVisibleResults() const;
144 
145  /**
146  * @brief Do we have results from check?
147  * @return true if there is at least one warning/error, hidden or visible.
148  */
149  bool hasResults() const;
150 
151  /**
152  * @brief Save all settings
153  * Column widths
154  */
155  void saveSettings() const;
156 
157  /**
158  * @brief Change all visible texts language
159  *
160  */
161  void translate();
162 
163  /**
164  * @brief Show optional column "Id"
165  */
166  void showIdColumn(bool show);
167 
168  /**
169  * @brief Show optional column "Inconclusve"
170  */
171  void showInconclusiveColumn(bool show);
172 
173  /**
174  * @brief Returns true if column "Id" is shown
175  */
176  bool showIdColumn() const {
177  return mShowErrorId;
178  }
179 
180  /**
181  * @brief GUI severities.
182  */
184 
185  void keyPressEvent(QKeyEvent *event) override;
186 
187 signals:
188  /**
189  * @brief Signal that results have been hidden or shown
190  *
191  * @param hidden true if there are some hidden results, or false if there are not
192  */
193  // NOLINTNEXTLINE(readability-inconsistent-declaration-parameter-name) - caused by generated MOC code
194  void resultsHidden(bool hidden);
195 
196  /**
197  * @brief Signal to perform selected files recheck
198  *
199  * @param selectedItems list of selected files
200  */
201  // NOLINTNEXTLINE(readability-inconsistent-declaration-parameter-name) - caused by generated MOC code
202  void checkSelected(QStringList selectedItems);
203 
204  /**
205  * @brief Signal for selection change in result tree.
206  *
207  * @param current Model index to specify new selected item.
208  */
209  // NOLINTNEXTLINE(readability-inconsistent-declaration-parameter-name) - caused by generated MOC code
210  void treeSelectionChanged(const QModelIndex &current);
211 
212  /** Suppress Ids */
213  // NOLINTNEXTLINE(readability-inconsistent-declaration-parameter-name) - caused by generated MOC code
214  void suppressIds(QStringList ids);
215 
216 public slots:
217 
218  /**
219  * @brief Function to show/hide certain type of errors
220  * Refreshes the tree.
221  *
222  * @param type Type of error to show/hide
223  * @param show Should specified errors be shown (true) or hidden (false)
224  */
225  void showResults(ShowTypes::ShowType type, bool show);
226 
227 
228  /**
229  * @brief Show/hide cppcheck errors.
230  * Refreshes the tree.
231  *
232  * @param show Should specified errors be shown (true) or hidden (false)
233  */
234  void showCppcheckResults(bool show);
235 
236  /**
237  * @brief Show/hide clang-tidy/clang-analyzer errors.
238  * Refreshes the tree.
239  *
240  * @param show Should specified errors be shown (true) or hidden (false)
241  */
242  void showClangResults(bool show);
243 
244 protected slots:
245  /**
246  * @brief Slot to quickstart an error with default application
247  *
248  * @param index Model index to specify which error item to open
249  */
250  void quickStartApplication(const QModelIndex &index);
251 
252  /**
253  * @brief Slot for context menu item to open an error with specified application
254  *
255  * @param application Index of the application to open the error
256  */
257  void context(int application);
258 
259  /**
260  * @brief Slot for context menu item to copy selection to clipboard
261  */
262  void copy();
263 
264  /**
265  * @brief Slot for context menu item to hide the current error message
266  *
267  */
268  void hideResult();
269 
270  /**
271  * @brief Slot for rechecking selected files
272  *
273  */
274  void recheckSelectedFiles();
275 
276  /**
277  * @brief Slot for context menu item to hide all messages with the current message Id
278  *
279  */
280  void hideAllIdResult();
281 
282  /** Slot for context menu item to suppress all messages with the current message id */
283  void suppressSelectedIds();
284 
285  /** Slot for context menu item to suppress message with hash */
286  void suppressHash();
287 
288  /**
289  * @brief Slot for context menu item to open the folder containing the current file.
290  */
291  void openContainingFolder();
292 
293  /**
294  * @brief Slot for selection change in the results tree.
295  *
296  * @param current Model index to specify new selected item.
297  * @param previous Model index to specify previous selected item.
298  */
299  void currentChanged(const QModelIndex &current, const QModelIndex &previous) override;
300 
301 protected:
302 
303  /**
304  * @brief Hides/shows full file path on all error file items according to mShowFullPath
305  *
306  */
307  void refreshFilePaths();
308 
309  /**
310  * @brief Hides/shows full file path on all error file items according to mShowFullPath
311  * @param item Parent item whose children's paths to change
312  */
313  void refreshFilePaths(QStandardItem *item);
314 
315 
316  /**
317  * @brief Removes checking directory from given path if mShowFullPath is false
318  *
319  * @param path Path to remove checking directory
320  * @param saving are we saving? Check mSaveFullPath instead
321  * @return Path that has checking directory removed
322  */
323  QString stripPath(const QString &path, bool saving) const;
324 
325 
326  /**
327  * @brief Save all errors under specified item
328  * @param report Report that errors are saved to
329  * @param fileItem Item whose errors to save
330  */
331  void saveErrors(Report *report, const QStandardItem *fileItem) const;
332 
333  /**
334  * @brief Convert a severity string to a icon filename
335  *
336  * @param severity Severity
337  */
338  static QString severityToIcon(Severity severity);
339 
340  /**
341  * @brief Helper function to open an error within target with application*
342  *
343  * @param target Error tree item to open
344  * @param application Index of the application to open with. Giving -1
345  * (default value) will open the default application.
346  */
347  void startApplication(const QStandardItem *target, int application = -1);
348 
349  /**
350  * @brief Helper function returning the filename/full path of the error tree item \a target.
351  *
352  * @param target The error tree item containing the filename/full path
353  * @param fullPath Whether or not to retrieve the full path or only the filename.
354  */
355  static QString getFilePath(const QStandardItem *target, bool fullPath);
356 
357  /**
358  * @brief Context menu event (user right clicked on the tree)
359  *
360  * @param e Event
361  */
362  void contextMenuEvent(QContextMenuEvent * e) override;
363 
364  /**
365  * @brief Add a new error item beneath a file or a backtrace item beneath an error
366  *
367  * @param parent Parent for the item. Either a file item or an error item
368  * @param item Error line data
369  * @param hide Should this be hidden (true) or shown (false)
370  * @param icon Should a default backtrace item icon be added
371  * @param childOfMessage Is this a child element of a message?
372  * @return newly created QStandardItem *
373  */
374  QStandardItem *addBacktraceFiles(QStandardItem *parent,
375  const ErrorLine &item,
376  const bool hide,
377  const QString &icon,
378  bool childOfMessage);
379 
380  /**
381  * @brief Convert Severity to translated string for GUI.
382  * @param severity Severity to convert
383  * @return Severity as translated string
384  */
385  static QString severityToTranslatedString(Severity severity);
386 
387  /**
388  * @brief Load all settings
389  * Column widths
390  */
391  void loadSettings();
392 
393  /**
394  * @brief Ask directory where file is located.
395  * @param file File name.
396  * @return Directory user chose.
397  */
398  QString askFileDir(const QString &file);
399 
400  /**
401  * @brief Create new normal item.
402  *
403  * Normal item has left alignment and text set also as tooltip.
404  * @param name name for the item
405  * @return new QStandardItem
406  */
407  static QStandardItem *createNormalItem(const QString &name);
408 
409  /**
410  * @brief Create new normal item.
411  *
412  * Normal item has left alignment and text set also as tooltip.
413  * @param checked checked
414  * @return new QStandardItem
415  */
416  static QStandardItem *createCheckboxItem(bool checked);
417 
418  /**
419  * @brief Create new line number item.
420  *
421  * Line number item has right align and text set as tooltip.
422  * @param linenumber name for the item
423  * @return new QStandardItem
424  */
425  static QStandardItem *createLineNumberItem(const QString &linenumber);
426 
427  /**
428  * @brief Finds a file item
429  *
430  * @param name name of the file item to find
431  * @return pointer to file item or null if none found
432  */
433  QStandardItem *findFileItem(const QString &name) const;
434 
435 
436  /**
437  * @brief Ensures there's a item in the model for the specified file
438  *
439  * @param fullpath Full path to the file item.
440  * @param file0 Source file
441  * @param hide is the error (we want this file item for) hidden?
442  * @return QStandardItem to be used as a parent for all errors for specified file
443  */
444  QStandardItem *ensureFileItem(const QString &fullpath, const QString &file0, bool hide);
445 
446  /**
447  * @brief Item model for tree
448  *
449  */
450  QStandardItemModel mModel;
451 
452  /**
453  * @brief Program settings
454  *
455  */
456  QSettings* mSettings{};
457 
458  /**
459  * @brief A string used to filter the results for display.
460  *
461  */
462  QString mFilter;
463 
464  /**
465  * @brief List of applications to open errors with
466  *
467  */
469 
470  /**
471  * @brief Right clicked item (used by context menu slots)
472  *
473  */
474  QStandardItem* mContextItem{};
475 
476  /**
477  * @brief Should full path of files be shown (true) or relative (false)
478  *
479  */
481 
482  /**
483  * @brief Should full path of files be saved
484  *
485  */
487 
488  /**
489  * @brief Save all errors (true) or only visible (false)
490  *
491  */
492  bool mSaveAllErrors = true;
493 
494  /**
495  * @brief true if optional column "Id" is shown
496  *
497  */
498  bool mShowErrorId{};
499 
500  /**
501  * @brief Path we are currently checking
502  *
503  */
504  QString mCheckPath;
505 
506  /**
507  * @brief Are there any visible errors
508  *
509  */
511 
512 private:
513  /** tag selected items */
514  void tagSelectedItems(const QString &tag);
515 
516  /** @brief Convert GUI error item into data error item */
517  void readErrorItem(const QStandardItem *error, ErrorItem *item) const;
518 
519  QStringList mHiddenMessageId;
520 
521  QItemSelectionModel* mSelectionModel{};
523 
524  bool mShowCppcheck = true;
525  bool mShowClang = true;
526 };
527 /// @}
528 #endif // RESULTSTREE_H
List of applications user has specified to open errors with.
A class containing error data for one error.
Definition: erroritem.h:72
A class containing error data for one shown error line.
Definition: erroritem.h:111
A base class for reports.
Definition: report.h:34
Cppcheck's results are shown in this tree.
Definition: resultstree.h:52
void hideResult()
Slot for context menu item to hide the current error message.
bool mSaveFullPath
Should full path of files be saved.
Definition: resultstree.h:486
void updateFromOldReport(const QString &filename)
Update items from old report (tag, sinceDate)
bool mShowCppcheck
Definition: resultstree.h:524
void treeSelectionChanged(const QModelIndex &current)
Signal for selection change in result tree.
static QString severityToIcon(Severity severity)
Convert a severity string to a icon filename.
void hideAllIdResult()
Slot for context menu item to hide all messages with the current message Id.
QString stripPath(const QString &path, bool saving) const
Removes checking directory from given path if mShowFullPath is false.
QString mFilter
A string used to filter the results for display.
Definition: resultstree.h:462
void openContainingFolder()
Slot for context menu item to open the folder containing the current file.
void suppressSelectedIds()
Slot for context menu item to suppress all messages with the current message id.
QStandardItem * addBacktraceFiles(QStandardItem *parent, const ErrorLine &item, const bool hide, const QString &icon, bool childOfMessage)
Add a new error item beneath a file or a backtrace item beneath an error.
void tagSelectedItems(const QString &tag)
tag selected items
void showHiddenResults()
Function to show results that were previous hidden with HideResult()
void updateSettings(bool showFullPath, bool saveFullPath, bool saveAllErrors, bool showErrorId, bool showInconclusive)
Update tree settings.
void resultsHidden(bool hidden)
Signal that results have been hidden or shown.
void clearRecheckFile(const QString &filename)
Clear errors of a file selected for recheck.
void clear()
Clear all errors from the tree.
static QStandardItem * createCheckboxItem(bool checked)
Create new normal item.
void showClangResults(bool show)
Show/hide clang-tidy/clang-analyzer errors.
bool showIdColumn() const
Returns true if column "Id" is shown.
Definition: resultstree.h:176
ShowTypes mShowSeverities
GUI severities.
Definition: resultstree.h:183
static QStandardItem * createLineNumberItem(const QString &linenumber)
Create new line number item.
bool mVisibleErrors
Are there any visible errors.
Definition: resultstree.h:510
bool mShowClang
Definition: resultstree.h:525
void saveSettings() const
Save all settings Column widths.
static QString getFilePath(const QStandardItem *target, bool fullPath)
Helper function returning the filename/full path of the error tree item target.
bool hasVisibleResults() const
Check if there are any visible results in view.
void checkSelected(QStringList selectedItems)
Signal to perform selected files recheck.
void translate()
Change all visible texts language.
QSettings * mSettings
Program settings.
Definition: resultstree.h:456
void loadSettings()
Load all settings Column widths.
void refreshTree()
Refresh tree by checking which of the items should be shown and which should be hidden.
bool addErrorItem(const ErrorItem &item)
Add a new item to the tree.
QStandardItemModel mModel
Item model for tree.
Definition: resultstree.h:450
void filterResults(const QString &filter)
Function to filter the displayed list of errors.
void readErrorItem(const QStandardItem *error, ErrorItem *item) const
Convert GUI error item into data error item.
QString mCheckPath
Path we are currently checking.
Definition: resultstree.h:504
bool mShowErrorId
true if optional column "Id" is shown
Definition: resultstree.h:498
ApplicationList * mApplications
List of applications to open errors with.
Definition: resultstree.h:468
void refreshFilePaths()
Hides/shows full file path on all error file items according to mShowFullPath.
ResultsTree(QWidget *parent=nullptr)
Definition: resultstree.cpp:86
void suppressHash()
Slot for context menu item to suppress message with hash.
bool mShowFullPath
Should full path of files be shown (true) or relative (false)
Definition: resultstree.h:480
QStandardItem * findFileItem(const QString &name) const
Finds a file item.
void saveResults(Report *report) const
Save results to a text stream.
QStandardItem * ensureFileItem(const QString &fullpath, const QString &file0, bool hide)
Ensures there's a item in the model for the specified file.
bool mSaveAllErrors
Save all errors (true) or only visible (false)
Definition: resultstree.h:492
void recheckSelectedFiles()
Slot for rechecking selected files.
void suppressIds(QStringList ids)
Suppress Ids.
QStringList mHiddenMessageId
Definition: resultstree.h:519
bool hasResults() const
Do we have results from check?
void currentChanged(const QModelIndex &current, const QModelIndex &previous) override
Slot for selection change in the results tree.
void startApplication(const QStandardItem *target, int application=-1)
Helper function to open an error within target with application*.
void initialize(QSettings *settings, ApplicationList *list, ThreadHandler *checkThreadHandler)
QString askFileDir(const QString &file)
Ask directory where file is located.
void saveErrors(Report *report, const QStandardItem *fileItem) const
Save all errors under specified item.
ThreadHandler * mThread
Definition: resultstree.h:522
QItemSelectionModel * mSelectionModel
Definition: resultstree.h:521
void showResults(ShowTypes::ShowType type, bool show)
Function to show/hide certain type of errors Refreshes the tree.
QStandardItem * mContextItem
Right clicked item (used by context menu slots)
Definition: resultstree.h:474
static QStandardItem * createNormalItem(const QString &name)
Create new normal item.
void context(int application)
Slot for context menu item to open an error with specified application.
void keyPressEvent(QKeyEvent *event) override
Definition: resultstree.cpp:97
void copy()
Slot for context menu item to copy selection to clipboard.
static QString severityToTranslatedString(Severity severity)
Convert Severity to translated string for GUI.
void showInconclusiveColumn(bool show)
Show optional column "Inconclusve".
void showCppcheckResults(bool show)
Show/hide cppcheck errors.
void setCheckDirectory(const QString &dir)
Set the directory we are checking.
void quickStartApplication(const QModelIndex &index)
Slot to quickstart an error with default application.
const QString & getCheckDirectory()
Get the directory we are checking.
void contextMenuEvent(QContextMenuEvent *e) override
Context menu event (user right clicked on the tree)
A class for different show types we have.
Definition: showtypes.h:38
ShowType
Show types we have (i.e.
Definition: showtypes.h:44
This class handles creating threadresult and starting threads.
Definition: threadhandler.h:50
Severity
enum class for severity.
Definition: errortypes.h:63
@ error
Programming error.