cppcheck/gui/resultstree.h

529 lines
14 KiB
C
Raw Normal View History

/*
* Cppcheck - A tool for static C/C++ code analysis
2023-01-28 10:16:34 +01:00
* Copyright (C) 2007-2023 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef RESULTSTREE_H
#define RESULTSTREE_H
2020-05-23 07:16:49 +02:00
#include "errortypes.h"
#include "showtypes.h"
#include <QObject>
2022-02-02 16:17:28 +01:00
#include <QStandardItemModel>
#include <QString>
#include <QStringList>
2022-02-02 16:17:28 +01:00
#include <QTreeView>
class ApplicationList;
class Report;
class ErrorItem;
class ErrorLine;
class QModelIndex;
class QWidget;
class QItemSelectionModel;
class ThreadHandler;
class QContextMenuEvent;
class QKeyEvent;
class QSettings;
/// @addtogroup GUI
/// @{
/**
2021-08-07 20:51:18 +02:00
* @brief Cppcheck's results are shown in this tree
*
*/
2011-10-13 20:53:06 +02:00
class ResultsTree : public QTreeView {
Q_OBJECT
public:
explicit ResultsTree(QWidget * parent = nullptr);
2017-07-28 11:54:20 +02:00
void initialize(QSettings *settings, ApplicationList *list, ThreadHandler *checkThreadHandler);
/**
2021-08-07 20:51:18 +02:00
* @brief Add a new item to the tree
*
* @param item Error item data
*/
2017-07-28 11:54:20 +02:00
bool addErrorItem(const ErrorItem &item);
/**
2021-08-07 20:51:18 +02:00
* @brief Clear all errors from the tree
*
*/
2017-07-28 11:54:20 +02:00
void clear();
2009-03-22 18:39:44 +01:00
/**
* @brief Clear errors for a specific file from the tree
*/
2017-07-28 11:54:20 +02:00
void clear(const QString &filename);
/**
* @brief Clear errors of a file selected for recheck
*/
2017-07-28 11:54:20 +02:00
void clearRecheckFile(const QString &filename);
/**
2021-08-07 20:51:18 +02:00
* @brief Function to filter the displayed list of errors.
* Refreshes the tree.
*
* @param filter String that must be found in the summary, description, file or id
*/
2017-07-28 11:54:20 +02:00
void filterResults(const QString& filter);
2010-11-21 19:55:34 +01:00
/**
2021-08-07 20:51:18 +02:00
* @brief Function to show results that were previous hidden with HideResult()
*/
2017-07-28 11:54:20 +02:00
void showHiddenResults();
2010-11-21 19:55:34 +01:00
/**
2021-08-07 20:51:18 +02:00
* @brief Refresh tree by checking which of the items should be shown
* and which should be hidden
*/
void refreshTree();
/**
2021-08-07 20:51:18 +02:00
* @brief Save results to a text stream
*
*/
2017-07-28 11:54:20 +02:00
void saveResults(Report *report) const;
/**
* @brief Update items from old report (tag, sinceDate)
*/
void updateFromOldReport(const QString &filename);
/**
2021-08-07 20:51:18 +02:00
* @brief Update tree settings
*
* @param showFullPath Show full path of files in the tree
* @param saveFullPath Save full path of files in reports
* @param saveAllErrors Save all visible errors
* @param showErrorId Show error id
* @param showInconclusive Show inconclusive column
*/
2017-07-28 11:54:20 +02:00
void updateSettings(bool showFullPath, bool saveFullPath, bool saveAllErrors, bool showErrorId, bool showInconclusive);
/**
2021-08-07 20:51:18 +02:00
* @brief Set the directory we are checking
*
* This is used to split error file path to relative if necessary
* @param dir Directory we are checking
*/
2017-07-28 11:54:20 +02:00
void setCheckDirectory(const QString &dir);
/**
2021-08-07 20:51:18 +02:00
* @brief Get the directory we are checking
*
* @return Directory containing source files
*/
QString getCheckDirectory();
/**
2021-08-07 20:51:18 +02:00
* @brief Check if there are any visible results in view.
* @return true if there is at least one visible warning/error.
*/
2017-07-28 11:54:20 +02:00
bool hasVisibleResults() const;
/**
2021-08-07 20:51:18 +02:00
* @brief Do we have results from check?
* @return true if there is at least one warning/error, hidden or visible.
*/
2017-07-28 11:54:20 +02:00
bool hasResults() const;
/**
2021-08-07 20:51:18 +02:00
* @brief Save all settings
* Column widths
*/
2017-07-28 11:54:20 +02:00
void saveSettings() const;
/**
2021-08-07 20:51:18 +02:00
* @brief Change all visible texts language
*
*/
2017-07-28 11:54:20 +02:00
void translate();
/**
2021-08-07 20:51:18 +02:00
* @brief Show optional column "Id"
*/
2017-07-28 11:54:20 +02:00
void showIdColumn(bool show);
/**
2021-08-07 20:51:18 +02:00
* @brief Show optional column "Inconclusve"
*/
2017-07-28 11:54:20 +02:00
void showInconclusiveColumn(bool show);
/**
2021-08-07 20:51:18 +02:00
* @brief Returns true if column "Id" is shown
*/
2017-07-28 11:54:20 +02:00
bool showIdColumn() const {
return mShowErrorId;
}
/**
* @brief GUI severities.
*/
ShowTypes mShowSeverities;
void keyPressEvent(QKeyEvent *event) override;
signals:
/**
2021-08-07 20:51:18 +02:00
* @brief Signal that results have been hidden or shown
*
* @param hidden true if there are some hidden results, or false if there are not
*/
// NOLINTNEXTLINE(readability-inconsistent-declaration-parameter-name) - caused by generated MOC code
2017-07-28 11:54:20 +02:00
void resultsHidden(bool hidden);
/**
2021-08-07 20:51:18 +02:00
* @brief Signal to perform selected files recheck
*
* @param selectedItems list of selected files
*/
// NOLINTNEXTLINE(readability-inconsistent-declaration-parameter-name) - caused by generated MOC code
2017-07-28 11:54:20 +02:00
void checkSelected(QStringList selectedItems);
/**
2021-08-07 20:51:18 +02:00
* @brief Signal for selection change in result tree.
*
* @param current Model index to specify new selected item.
*/
// NOLINTNEXTLINE(readability-inconsistent-declaration-parameter-name) - caused by generated MOC code
void treeSelectionChanged(const QModelIndex &current);
2017-08-11 07:45:29 +02:00
/** Suppress Ids */
// NOLINTNEXTLINE(readability-inconsistent-declaration-parameter-name) - caused by generated MOC code
2017-08-11 07:45:29 +02:00
void suppressIds(QStringList ids);
public slots:
/**
2021-08-07 20:51:18 +02:00
* @brief Function to show/hide certain type of errors
* Refreshes the tree.
*
* @param type Type of error to show/hide
* @param show Should specified errors be shown (true) or hidden (false)
*/
void showResults(ShowTypes::ShowType type, bool show);
/**
2021-08-07 20:51:18 +02:00
* @brief Show/hide cppcheck errors.
* Refreshes the tree.
*
* @param show Should specified errors be shown (true) or hidden (false)
*/
void showCppcheckResults(bool show);
/**
2021-08-07 20:51:18 +02:00
* @brief Show/hide clang-tidy/clang-analyzer errors.
* Refreshes the tree.
*
* @param show Should specified errors be shown (true) or hidden (false)
*/
void showClangResults(bool show);
protected slots:
2009-06-01 23:52:14 +02:00
/**
2021-08-07 20:51:18 +02:00
* @brief Slot to quickstart an error with default application
*
* @param index Model index to specify which error item to open
*/
2017-07-28 11:54:20 +02:00
void quickStartApplication(const QModelIndex &index);
/**
2021-08-07 20:51:18 +02:00
* @brief Slot for context menu item to open an error with specified application
*
* @param application Index of the application to open the error
*/
2017-07-28 11:54:20 +02:00
void context(int application);
/**
2021-08-07 20:51:18 +02:00
* @brief Slot for context menu item to copy selection to clipboard
*/
2018-04-13 17:31:51 +02:00
void copy();
2010-11-21 19:55:34 +01:00
/**
2021-08-07 20:51:18 +02:00
* @brief Slot for context menu item to hide the current error message
*
*/
2017-07-28 11:54:20 +02:00
void hideResult();
2010-11-21 19:55:34 +01:00
/**
2021-08-07 20:51:18 +02:00
* @brief Slot for rechecking selected files
*
*/
2017-07-28 11:54:20 +02:00
void recheckSelectedFiles();
/**
2021-08-07 20:51:18 +02:00
* @brief Slot for context menu item to hide all messages with the current message Id
*
*/
2017-07-28 11:54:20 +02:00
void hideAllIdResult();
2017-08-11 07:45:29 +02:00
/** Slot for context menu item to suppress all messages with the current message id */
void suppressSelectedIds();
2020-07-21 11:27:03 +02:00
/** Slot for context menu item to suppress message with hash */
void suppressHash();
2020-07-15 13:03:07 +02:00
/**
2021-08-07 20:51:18 +02:00
* @brief Slot for context menu item to open the folder containing the current file.
*/
2017-07-28 11:54:20 +02:00
void openContainingFolder();
/**
2021-08-07 20:51:18 +02:00
* @brief Slot for selection change in the results tree.
*
* @param current Model index to specify new selected item.
* @param previous Model index to specify previous selected item.
*/
void currentChanged(const QModelIndex &current, const QModelIndex &previous) override;
protected:
/**
2021-08-07 20:51:18 +02:00
* @brief Hides/shows full file path on all error file items according to mShowFullPath
*
*/
2017-07-28 11:54:20 +02:00
void refreshFilePaths();
/**
2021-08-07 20:51:18 +02:00
* @brief Hides/shows full file path on all error file items according to mShowFullPath
* @param item Parent item whose children's paths to change
*/
2017-07-28 11:54:20 +02:00
void refreshFilePaths(QStandardItem *item);
/**
2021-08-07 20:51:18 +02:00
* @brief Removes checking directory from given path if mShowFullPath is false
*
* @param path Path to remove checking directory
* @param saving are we saving? Check mSaveFullPath instead
* @return Path that has checking directory removed
*/
2017-07-28 11:54:20 +02:00
QString stripPath(const QString &path, bool saving) const;
/**
2021-08-07 20:51:18 +02:00
* @brief Save all errors under specified item
* @param report Report that errors are saved to
* @param fileItem Item whose errors to save
*/
2020-07-14 18:19:03 +02:00
void saveErrors(Report *report, const QStandardItem *fileItem) const;
2009-06-01 23:52:14 +02:00
/**
2021-08-07 20:51:18 +02:00
* @brief Convert a severity string to a icon filename
*
* @param severity Severity
*/
2022-09-08 20:01:41 +02:00
static QString severityToIcon(Severity::SeverityType severity);
2009-06-01 23:52:14 +02:00
/**
2021-08-07 20:51:18 +02:00
* @brief Helper function to open an error within target with application*
*
* @param target Error tree item to open
* @param application Index of the application to open with. Giving -1
* (default value) will open the default application.
*/
void startApplication(const QStandardItem *target, int application = -1);
/**
2021-08-07 20:51:18 +02:00
* @brief Helper function returning the filename/full path of the error tree item \a target.
*
* @param target The error tree item containing the filename/full path
* @param fullPath Whether or not to retrieve the full path or only the filename.
*/
static QString getFilePath(const QStandardItem *target, bool fullPath);
2009-06-01 23:52:14 +02:00
/**
2021-08-07 20:51:18 +02:00
* @brief Context menu event (user right clicked on the tree)
*
* @param e Event
*/
void contextMenuEvent(QContextMenuEvent * e) override;
2009-06-01 23:52:14 +02:00
/**
2021-08-07 20:51:18 +02:00
* @brief Add a new error item beneath a file or a backtrace item beneath an error
*
* @param parent Parent for the item. Either a file item or an error item
* @param item Error line data
* @param hide Should this be hidden (true) or shown (false)
* @param icon Should a default backtrace item icon be added
* @param childOfMessage Is this a child element of a message?
* @return newly created QStandardItem *
*/
2017-07-28 11:54:20 +02:00
QStandardItem *addBacktraceFiles(QStandardItem *parent,
const ErrorLine &item,
const bool hide,
const QString &icon,
bool childOfMessage);
/**
2021-08-07 20:51:18 +02:00
* @brief Convert Severity to translated string for GUI.
* @param severity Severity to convert
* @return Severity as translated string
*/
2017-07-28 11:54:20 +02:00
static QString severityToTranslatedString(Severity::SeverityType severity);
/**
2021-08-07 20:51:18 +02:00
* @brief Load all settings
* Column widths
*/
2017-07-28 11:54:20 +02:00
void loadSettings();
/**
2021-08-07 20:51:18 +02:00
* @brief Ask directory where file is located.
* @param file File name.
* @return Directory user chose.
*/
2017-07-28 11:54:20 +02:00
QString askFileDir(const QString &file);
/**
2021-08-07 20:51:18 +02:00
* @brief Create new normal item.
*
* Normal item has left alignment and text set also as tooltip.
* @param name name for the item
* @return new QStandardItem
*/
2017-07-28 11:54:20 +02:00
static QStandardItem *createNormalItem(const QString &name);
/**
2021-08-07 20:51:18 +02:00
* @brief Create new normal item.
*
* Normal item has left alignment and text set also as tooltip.
* @param checked checked
* @return new QStandardItem
*/
2017-07-28 11:54:20 +02:00
static QStandardItem *createCheckboxItem(bool checked);
2010-11-19 22:11:40 +01:00
/**
2021-08-07 20:51:18 +02:00
* @brief Create new line number item.
*
* Line number item has right align and text set as tooltip.
* @param linenumber name for the item
* @return new QStandardItem
*/
2017-07-28 11:54:20 +02:00
static QStandardItem *createLineNumberItem(const QString &linenumber);
2010-11-19 22:11:40 +01:00
/**
2021-08-07 20:51:18 +02:00
* @brief Finds a file item
*
* @param name name of the file item to find
* @return pointer to file item or null if none found
*/
2017-07-28 11:54:20 +02:00
QStandardItem *findFileItem(const QString &name) const;
/**
2021-08-07 20:51:18 +02:00
* @brief Ensures there's a item in the model for the specified file
*
* @param fullpath Full path to the file item.
* @param file0 Source file
* @param hide is the error (we want this file item for) hidden?
* @return QStandardItem to be used as a parent for all errors for specified file
*/
2017-07-28 11:54:20 +02:00
QStandardItem *ensureFileItem(const QString &fullpath, const QString &file0, bool hide);
/**
2021-08-07 20:51:18 +02:00
* @brief Item model for tree
*
*/
QStandardItemModel mModel;
/**
2021-08-07 20:51:18 +02:00
* @brief Program settings
*
*/
QSettings* mSettings{};
2009-03-22 18:39:44 +01:00
/**
2021-08-07 20:51:18 +02:00
* @brief A string used to filter the results for display.
*
*/
QString mFilter;
2009-06-01 23:52:14 +02:00
/**
2021-08-07 20:51:18 +02:00
* @brief List of applications to open errors with
*
*/
ApplicationList* mApplications{};
/**
2021-08-07 20:51:18 +02:00
* @brief Right clicked item (used by context menu slots)
*
*/
QStandardItem* mContextItem{};
/**
2021-08-07 20:51:18 +02:00
* @brief Should full path of files be shown (true) or relative (false)
*
*/
bool mShowFullPath{};
/**
2021-08-07 20:51:18 +02:00
* @brief Should full path of files be saved
*
*/
bool mSaveFullPath{};
/**
2021-08-07 20:51:18 +02:00
* @brief Save all errors (true) or only visible (false)
*
*/
bool mSaveAllErrors = true;
/**
2021-08-07 20:51:18 +02:00
* @brief true if optional column "Id" is shown
*
*/
bool mShowErrorId{};
/**
2021-08-07 20:51:18 +02:00
* @brief Path we are currently checking
*
*/
QString mCheckPath;
/**
2021-08-07 20:51:18 +02:00
* @brief Are there any visible errors
*
*/
bool mVisibleErrors{};
private:
/** tag selected items */
void tagSelectedItems(const QString &tag);
2017-08-02 12:45:57 +02:00
/** @brief Convert GUI error item into data error item */
void readErrorItem(const QStandardItem *error, ErrorItem *item) const;
QStringList mHiddenMessageId;
QItemSelectionModel* mSelectionModel{};
ThreadHandler *mThread{};
bool mShowCppcheck = true;
bool mShowClang = true;
};
/// @}
#endif // RESULTSTREE_H