File indexing completed on 2024-05-05 04:39:27

0001 /*
0002     SPDX-FileCopyrightText: 2018 Anton Anikin <anton@anikin.xyz>
0003     SPDX-FileCopyrightText: 2018, 2020 Friedrich W. H. Kossebau <kossebau@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "compileanalyzeproblemmodel.h"
0009 
0010 // KDevPlatform
0011 #include <interfaces/icore.h>
0012 #include <interfaces/iproject.h>
0013 #include <interfaces/iprojectcontroller.h>
0014 #include <language/editor/documentrange.h>
0015 // KF
0016 #include <KLocalizedString>
0017 
0018 namespace KDevelop
0019 {
0020 
0021 CompileAnalyzeProblemModel::CompileAnalyzeProblemModel(const QString& toolName, QObject* parent)
0022     : KDevelop::ProblemModel(parent)
0023     , m_toolName(toolName)
0024     , m_pathLocation(KDevelop::DocumentRange::invalid())
0025 {
0026 }
0027 
0028 CompileAnalyzeProblemModel::~CompileAnalyzeProblemModel() = default;
0029 
0030 KDevelop::IProject* CompileAnalyzeProblemModel::project() const
0031 {
0032     return m_project;
0033 }
0034 
0035 void CompileAnalyzeProblemModel::setMessage(const QString& message)
0036 {
0037     setPlaceholderText(message, m_pathLocation, m_toolName);
0038 }
0039 
0040 // The code is adapted version of cppcheck::ProblemModel::problemExists()
0041 // TODO Add into KDevelop::ProblemModel class ?
0042 bool CompileAnalyzeProblemModel::problemExists(KDevelop::IProblem::Ptr newProblem)
0043 {
0044     for (const auto& problem : qAsConst(m_problems)) {
0045         if (newProblem->source() == problem->source() &&
0046             newProblem->sourceString() == problem->sourceString() &&
0047             newProblem->severity() == problem->severity() &&
0048             newProblem->finalLocation() == problem->finalLocation() &&
0049             newProblem->description() == problem->description() &&
0050             newProblem->explanation() == problem->explanation())
0051             return true;
0052     }
0053 
0054     return false;
0055 }
0056 
0057 // The code is adapted version of cppcheck::ProblemModel::addProblems()
0058 // TODO Add into KDevelop::ProblemModel class ?
0059 void CompileAnalyzeProblemModel::addProblems(const QVector<KDevelop::IProblem::Ptr>& problems)
0060 {
0061     if (m_problems.isEmpty()) {
0062         m_maxProblemDescriptionLength = 0;
0063     }
0064 
0065     for (const auto& problem : problems) {
0066         if (problemExists(problem)) {
0067             continue;
0068         }
0069 
0070         m_problems.append(problem);
0071         addProblem(problem);
0072 
0073         // This performs adjusting of columns width in the ProblemsView
0074         if (m_maxProblemDescriptionLength < problem->description().length()) {
0075             m_maxProblemDescriptionLength = problem->description().length();
0076             setProblems(m_problems);
0077         }
0078     }
0079 }
0080 
0081 void CompileAnalyzeProblemModel::finishAddProblems()
0082 {
0083     if (m_problems.isEmpty()) {
0084         setMessage(i18n("Analysis completed, no problems detected."));
0085     } else {
0086         setProblems(m_problems);
0087     }
0088 }
0089 
0090 void CompileAnalyzeProblemModel::reset()
0091 {
0092     reset(nullptr, QUrl(), false);
0093 }
0094 
0095 void CompileAnalyzeProblemModel::reset(KDevelop::IProject* project, const QUrl& path, bool allFiles)
0096 {
0097     m_project = project;
0098     m_path = path;
0099     m_allFiles = allFiles;
0100     m_pathLocation.document = KDevelop::IndexedString(path.toLocalFile());
0101 
0102     clearProblems();
0103     m_problems.clear();
0104 
0105     QString tooltip;
0106     if (m_project) {
0107         setMessage(i18n("Analysis started..."));
0108 
0109         const QString prettyPathName = KDevelop::ICore::self()->projectController()->prettyFileName(        path, KDevelop::IProjectController::FormatPlain);
0110         tooltip = i18nc("@info:tooltip %2 is the path of the file", "Re-run last %1 analysis (%2)", m_toolName, prettyPathName);
0111     } else {
0112         tooltip = i18nc("@info:tooltip", "Re-run last %1 analysis", m_toolName);
0113     }
0114 
0115     setFullUpdateTooltip(tooltip);
0116 }
0117 
0118 void CompileAnalyzeProblemModel::forceFullUpdate()
0119 {
0120     if (m_path.isValid()) {
0121         emit rerunRequested(m_path, m_allFiles);
0122     }
0123 }
0124 
0125 }
0126 
0127 #include "moc_compileanalyzeproblemmodel.cpp"