cppcheck/gui/mainwindow.cpp

1614 lines
57 KiB
C++
Raw Normal View History

/*
* Cppcheck - A tool for static C/C++ code analysis
2016-01-01 14:34:45 +01:00
* Copyright (C) 2007-2016 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/>.
*/
#include <QApplication>
#include <QDebug>
#include <QMessageBox>
#include <QFileInfo>
#include <QDir>
#include <QDesktopServices>
#include <QUrl>
#include <QAction>
#include <QActionGroup>
#include <QFile>
#include <QInputDialog>
#include "mainwindow.h"
#include "cppcheck.h"
#include "applicationlist.h"
#include "aboutdialog.h"
#include "common.h"
#include "threadhandler.h"
#include "fileviewdialog.h"
#include "projectfile.h"
#include "project.h"
#include "report.h"
#include "scratchpad.h"
#include "statsdialog.h"
#include "settingsdialog.h"
#include "threadresult.h"
#include "translationhandler.h"
#include "logview.h"
#include "filelist.h"
#include "showtypes.h"
2015-09-04 20:46:26 +02:00
#include "librarydialog.h"
static const QString OnlineHelpURL("http://cppcheck.net/manual.html");
static const QString compile_commands_json("compile_commands.json");
MainWindow::MainWindow(TranslationHandler* th, QSettings* settings) :
mSettings(settings),
2010-04-15 20:08:51 +02:00
mApplications(new ApplicationList(this)),
mTranslation(th),
mLogView(NULL),
mScratchPad(NULL),
mProject(NULL),
mPlatformActions(new QActionGroup(this)),
mCStandardActions(new QActionGroup(this)),
mCppStandardActions(new QActionGroup(this)),
mSelectLanguageActions(new QActionGroup(this)),
mExiting(false),
mIsLogfileLoaded(false)
{
mUI.setupUi(this);
mThread = new ThreadHandler(this);
2017-07-28 12:10:10 +02:00
mUI.mResults->initialize(mSettings, mApplications, mThread);
// Filter timer to delay filtering results slightly while typing
mFilterTimer = new QTimer(this);
mFilterTimer->setInterval(500);
mFilterTimer->setSingleShot(true);
2017-07-28 12:22:15 +02:00
connect(mFilterTimer, SIGNAL(timeout()), this, SLOT(filterResults()));
// "Filter" toolbar
mLineEditFilter = new QLineEdit(mUI.mToolBarFilter);
mLineEditFilter->setPlaceholderText(tr("Quick Filter:"));
mUI.mToolBarFilter->addWidget(mLineEditFilter);
connect(mLineEditFilter, SIGNAL(textChanged(const QString&)), mFilterTimer, SLOT(start()));
2017-07-28 12:22:15 +02:00
connect(mLineEditFilter, SIGNAL(returnPressed()), this, SLOT(filterResults()));
2017-07-28 12:22:15 +02:00
connect(mUI.mActionPrint, SIGNAL(triggered()), mUI.mResults, SLOT(print()));
connect(mUI.mActionPrintPreview, SIGNAL(triggered()), mUI.mResults, SLOT(printPreview()));
connect(mUI.mActionQuit, SIGNAL(triggered()), this, SLOT(close()));
connect(mUI.mActionAnalyzeFiles, SIGNAL(triggered()), this, SLOT(analyzeFiles()));
connect(mUI.mActionAnalyzeDirectory, SIGNAL(triggered()), this, SLOT(analyzeDirectory()));
2017-07-28 12:22:15 +02:00
connect(mUI.mActionSettings, SIGNAL(triggered()), this, SLOT(programSettings()));
connect(mUI.mActionClearResults, SIGNAL(triggered()), this, SLOT(clearResults()));
connect(mUI.mActionOpenXML, SIGNAL(triggered()), this, SLOT(openResults()));
connect(mUI.mActionShowStyle, SIGNAL(toggled(bool)), this, SLOT(showStyle(bool)));
connect(mUI.mActionShowErrors, SIGNAL(toggled(bool)), this, SLOT(showErrors(bool)));
connect(mUI.mActionShowWarnings, SIGNAL(toggled(bool)), this, SLOT(showWarnings(bool)));
connect(mUI.mActionShowPortability, SIGNAL(toggled(bool)), this, SLOT(showPortability(bool)));
connect(mUI.mActionShowPerformance, SIGNAL(toggled(bool)), this, SLOT(showPerformance(bool)));
connect(mUI.mActionShowInformation, SIGNAL(toggled(bool)), this, SLOT(showInformation(bool)));
connect(mUI.mActionCheckAll, SIGNAL(triggered()), this, SLOT(checkAll()));
connect(mUI.mActionUncheckAll, SIGNAL(triggered()), this, SLOT(uncheckAll()));
connect(mUI.mActionCollapseAll, SIGNAL(triggered()), mUI.mResults, SLOT(collapseAllResults()));
connect(mUI.mActionExpandAll, SIGNAL(triggered()), mUI.mResults, SLOT(expandAllResults()));
connect(mUI.mActionShowHidden, SIGNAL(triggered()), mUI.mResults, SLOT(showHiddenResults()));
connect(mUI.mActionViewLog, SIGNAL(triggered()), this, SLOT(showLogView()));
connect(mUI.mActionViewStats, SIGNAL(triggered()), this, SLOT(showStatistics()));
connect(mUI.mActionLibraryEditor, SIGNAL(triggered()), this, SLOT(showLibraryEditor()));
connect(mUI.mActionReanalyzeModified, SIGNAL(triggered()), this, SLOT(reAnalyzeModified()));
connect(mUI.mActionReanalyzeAll, SIGNAL(triggered()), this, SLOT(reAnalyzeAll()));
2017-07-28 12:22:15 +02:00
connect(mUI.mActionStop, SIGNAL(triggered()), this, SLOT(stopAnalysis()));
2017-07-28 12:22:15 +02:00
connect(mUI.mActionSave, SIGNAL(triggered()), this, SLOT(save()));
// About menu
2017-07-28 12:22:15 +02:00
connect(mUI.mActionAbout, SIGNAL(triggered()), this, SLOT(about()));
connect(mUI.mActionLicense, SIGNAL(triggered()), this, SLOT(showLicense()));
// View > Toolbar menu
2017-07-28 12:22:15 +02:00
connect(mUI.mActionToolBarMain, SIGNAL(toggled(bool)), this, SLOT(toggleMainToolBar()));
connect(mUI.mActionToolBarView, SIGNAL(toggled(bool)), this, SLOT(toggleViewToolBar()));
connect(mUI.mActionToolBarFilter, SIGNAL(toggled(bool)), this, SLOT(toggleFilterToolBar()));
2017-07-28 12:22:15 +02:00
connect(mUI.mActionAuthors, SIGNAL(triggered()), this, SLOT(showAuthors()));
2017-07-28 12:39:28 +02:00
connect(mThread, SIGNAL(done()), this, SLOT(checkDone()));
2017-07-28 12:22:15 +02:00
connect(mUI.mResults, SIGNAL(gotResults()), this, SLOT(resultsAdded()));
connect(mUI.mResults, SIGNAL(resultsHidden(bool)), mUI.mActionShowHidden, SLOT(setEnabled(bool)));
connect(mUI.mResults, SIGNAL(checkSelected(QStringList)), this, SLOT(performSelectedFilesCheck(QStringList)));
connect(mUI.mMenuView, SIGNAL(aboutToShow()), this, SLOT(aboutToShowViewMenu()));
// File menu
2017-07-28 12:22:15 +02:00
connect(mUI.mActionNewProjectFile, SIGNAL(triggered()), this, SLOT(newProjectFile()));
connect(mUI.mActionOpenProjectFile, SIGNAL(triggered()), this, SLOT(openProjectFile()));
connect(mUI.mActionShowScratchpad, SIGNAL(triggered()), this, SLOT(showScratchpad()));
connect(mUI.mActionCloseProjectFile, SIGNAL(triggered()), this, SLOT(closeProjectFile()));
connect(mUI.mActionEditProjectFile, SIGNAL(triggered()), this, SLOT(editProjectFile()));
2017-07-28 12:22:15 +02:00
connect(mUI.mActionHelpContents, SIGNAL(triggered()), this, SLOT(openHelpContents()));
2017-07-28 05:44:15 +02:00
loadSettings();
2017-07-28 12:39:28 +02:00
mThread->initialize(mUI.mResults);
if (mProject)
formatAndSetTitle(tr("Project:") + ' ' + mProject->getFilename());
else
formatAndSetTitle();
2017-07-28 05:44:15 +02:00
enableCheckButtons(true);
mUI.mActionPrint->setShortcut(QKeySequence::Print);
mUI.mActionPrint->setEnabled(false);
mUI.mActionPrintPreview->setEnabled(false);
mUI.mActionClearResults->setEnabled(false);
mUI.mActionSave->setEnabled(false);
2017-07-30 09:46:01 +02:00
mUI.mActionReanalyzeModified->setEnabled(false);
mUI.mActionReanalyzeAll->setEnabled(false);
2017-07-28 05:44:15 +02:00
enableProjectOpenActions(true);
enableProjectActions(false);
// Must setup MRU menu before CLI param handling as it can load a
// project file and update MRU menu.
2011-10-13 20:53:06 +02:00
for (int i = 0; i < MaxRecentProjects; ++i) {
mRecentProjectActs[i] = new QAction(this);
mRecentProjectActs[i]->setVisible(false);
connect(mRecentProjectActs[i], SIGNAL(triggered()),
2017-07-28 12:22:15 +02:00
this, SLOT(openRecentProject()));
}
mRecentProjectActs[MaxRecentProjects] = NULL; // The separator
mUI.mActionProjectMRU->setVisible(false);
2017-07-28 05:44:15 +02:00
updateMRUMenuItems();
QStringList args = QCoreApplication::arguments();
//Remove the application itself
args.removeFirst();
if (!args.isEmpty()) {
2017-07-28 05:44:15 +02:00
handleCLIParams(args);
}
mUI.mActionCloseProjectFile->setEnabled(mProject != nullptr);
mUI.mActionEditProjectFile->setEnabled(mProject != nullptr);
2011-10-13 20:53:06 +02:00
for (int i = 0; i < mPlatforms.getCount(); i++) {
Platform plat = mPlatforms.mPlatforms[i];
QAction *act = new QAction(this);
plat.mActMainWindow = act;
mPlatforms.mPlatforms[i] = plat;
act->setText(plat.mTitle);
act->setData(plat.mType);
act->setCheckable(true);
act->setActionGroup(mPlatformActions);
2017-07-30 09:46:01 +02:00
mUI.mMenuAnalyze->insertAction(mUI.mActionPlatforms, act);
2017-07-28 12:22:15 +02:00
connect(act, SIGNAL(triggered()), this, SLOT(selectPlatform()));
}
mUI.mActionC89->setActionGroup(mCStandardActions);
mUI.mActionC99->setActionGroup(mCStandardActions);
mUI.mActionC11->setActionGroup(mCStandardActions);
mUI.mActionCpp03->setActionGroup(mCppStandardActions);
mUI.mActionCpp11->setActionGroup(mCppStandardActions);
mUI.mActionEnforceC->setActionGroup(mSelectLanguageActions);
mUI.mActionEnforceCpp->setActionGroup(mSelectLanguageActions);
mUI.mActionAutoDetectLanguage->setActionGroup(mSelectLanguageActions);
// For Windows platforms default to Win32 checked platform.
// For other platforms default to unspecified/default which means the
// platform Cppcheck GUI was compiled on.
#if defined(_WIN32)
const Settings::PlatformType defaultPlat = Settings::Win32W;
#else
2014-06-14 18:47:19 +02:00
const Settings::PlatformType defaultPlat = Settings::Unspecified;
#endif
2014-06-14 18:47:19 +02:00
Platform &plat = mPlatforms.get((Settings::PlatformType)mSettings->value(SETTINGS_CHECKED_PLATFORM, defaultPlat).toInt());
plat.mActMainWindow->setChecked(true);
}
MainWindow::~MainWindow()
{
delete mLogView;
delete mProject;
delete mScratchPad;
}
2017-07-28 05:44:15 +02:00
void MainWindow::handleCLIParams(const QStringList &params)
{
int index;
2011-10-13 20:53:06 +02:00
if (params.contains("-p")) {
index = params.indexOf("-p");
if ((index + 1) < params.length())
2017-07-28 05:44:15 +02:00
loadProjectFile(params[index + 1]);
} else if (params.contains("-l")) {
QString logFile;
index = params.indexOf("-l");
if ((index + 1) < params.length())
logFile = params[index + 1];
if (params.contains("-d")) {
QString checkedDir;
index = params.indexOf("-d");
if ((index + 1) < params.length())
checkedDir = params[index + 1];
2017-07-28 05:44:15 +02:00
loadResults(logFile, checkedDir);
2013-02-10 23:53:01 +01:00
} else {
2017-07-28 05:44:15 +02:00
loadResults(logFile);
}
} else if ((index = params.indexOf(QRegExp(".*\\.cppcheck$", Qt::CaseInsensitive), 0)) >= 0 && index < params.length() && QFile(params[index]).exists()) {
2017-07-28 05:44:15 +02:00
loadProjectFile(params[index]);
} else if ((index = params.indexOf(QRegExp(".*\\.xml$", Qt::CaseInsensitive), 0)) >= 0 && index < params.length() && QFile(params[index]).exists()) {
2017-07-28 05:44:15 +02:00
loadResults(params[index],QDir::currentPath());
2011-10-13 20:53:06 +02:00
} else
doAnalyzeFiles(params);
}
2017-07-28 05:44:15 +02:00
void MainWindow::loadSettings()
{
// Window/dialog sizes
2011-10-13 20:53:06 +02:00
if (mSettings->value(SETTINGS_WINDOW_MAXIMIZED, false).toBool()) {
showMaximized();
2011-10-13 20:53:06 +02:00
} else {
resize(mSettings->value(SETTINGS_WINDOW_WIDTH, 800).toInt(),
mSettings->value(SETTINGS_WINDOW_HEIGHT, 600).toInt());
}
2009-03-22 18:39:44 +01:00
2017-07-28 12:10:10 +02:00
ShowTypes *types = mUI.mResults->getShowTypes();
mUI.mActionShowStyle->setChecked(types->isShown(ShowTypes::ShowStyle));
mUI.mActionShowErrors->setChecked(types->isShown(ShowTypes::ShowErrors));
mUI.mActionShowWarnings->setChecked(types->isShown(ShowTypes::ShowWarnings));
mUI.mActionShowPortability->setChecked(types->isShown(ShowTypes::ShowPortability));
mUI.mActionShowPerformance->setChecked(types->isShown(ShowTypes::ShowPerformance));
mUI.mActionShowInformation->setChecked(types->isShown(ShowTypes::ShowInformation));
const bool stdCpp03 = mSettings->value(SETTINGS_STD_CPP03, false).toBool();
mUI.mActionCpp03->setChecked(stdCpp03);
const bool stdCpp11 = mSettings->value(SETTINGS_STD_CPP11, true).toBool();
mUI.mActionCpp11->setChecked(stdCpp11 || !stdCpp03);
const bool stdC89 = mSettings->value(SETTINGS_STD_C89, false).toBool();
mUI.mActionC89->setChecked(stdC89);
const bool stdC11 = mSettings->value(SETTINGS_STD_C11, false).toBool();
mUI.mActionC11->setChecked(stdC11);
const bool stdC99 = mSettings->value(SETTINGS_STD_C99, true).toBool();
mUI.mActionC99->setChecked(stdC99 || (!stdC89 && !stdC11));
const bool stdPosix = mSettings->value(SETTINGS_STD_POSIX, false).toBool();
mUI.mActionPosix->setChecked(stdPosix);
// Main window settings
const bool showMainToolbar = mSettings->value(SETTINGS_TOOLBARS_MAIN_SHOW, true).toBool();
mUI.mActionToolBarMain->setChecked(showMainToolbar);
mUI.mToolBarMain->setVisible(showMainToolbar);
const bool showViewToolbar = mSettings->value(SETTINGS_TOOLBARS_VIEW_SHOW, true).toBool();
mUI.mActionToolBarView->setChecked(showViewToolbar);
mUI.mToolBarView->setVisible(showViewToolbar);
const bool showFilterToolbar = mSettings->value(SETTINGS_TOOLBARS_FILTER_SHOW, true).toBool();
mUI.mActionToolBarFilter->setChecked(showFilterToolbar);
mUI.mToolBarFilter->setVisible(showFilterToolbar);
Settings::Language enforcedLanguage = (Settings::Language)mSettings->value(SETTINGS_ENFORCED_LANGUAGE, 0).toInt();
if (enforcedLanguage == Settings::CPP)
mUI.mActionEnforceCpp->setChecked(true);
else if (enforcedLanguage == Settings::C)
mUI.mActionEnforceC->setChecked(true);
else
mUI.mActionAutoDetectLanguage->setChecked(true);
2017-07-28 05:18:43 +02:00
bool succeeded = mApplications->loadSettings();
2011-10-13 20:53:06 +02:00
if (!succeeded) {
2011-08-22 21:37:38 +02:00
const QString msg = tr("There was a problem with loading the editor application settings.\n\n"
2011-08-23 02:32:35 +02:00
"This is probably because the settings were changed between the Cppcheck versions. "
"Please check (and fix) the editor application settings, otherwise the editor "
"program might not start correctly.");
QMessageBox msgBox(QMessageBox::Warning,
tr("Cppcheck"),
msg,
QMessageBox::Ok,
this);
msgBox.exec();
}
const QString project = mSettings->value(SETTINGS_OPEN_PROJECT, QString()).toString();
if (!project.isEmpty() && QCoreApplication::arguments().size()==1) {
QFileInfo inf(project);
if (inf.exists() && inf.isReadable()) {
delete mProject;
mProject = new Project(project, this);
mProject->open();
if (!mProject->getProjectFile()->getBuildDir().isEmpty()) {
const QString buildDir = QFileInfo(project).absolutePath() + '/' + mProject->getProjectFile()->getBuildDir();
const QString lastResults = buildDir + "/lastResults.xml";
if (QFileInfo(lastResults).exists())
mUI.mResults->readErrorsXml(lastResults);
}
}
}
}
2017-07-28 05:44:15 +02:00
void MainWindow::saveSettings() const
{
// Window/dialog sizes
mSettings->setValue(SETTINGS_WINDOW_WIDTH, size().width());
mSettings->setValue(SETTINGS_WINDOW_HEIGHT, size().height());
mSettings->setValue(SETTINGS_WINDOW_MAXIMIZED, isMaximized());
2009-03-22 18:39:44 +01:00
// Show * states
mSettings->setValue(SETTINGS_SHOW_STYLE, mUI.mActionShowStyle->isChecked());
mSettings->setValue(SETTINGS_SHOW_ERRORS, mUI.mActionShowErrors->isChecked());
mSettings->setValue(SETTINGS_SHOW_WARNINGS, mUI.mActionShowWarnings->isChecked());
mSettings->setValue(SETTINGS_SHOW_PORTABILITY, mUI.mActionShowPortability->isChecked());
mSettings->setValue(SETTINGS_SHOW_PERFORMANCE, mUI.mActionShowPerformance->isChecked());
mSettings->setValue(SETTINGS_SHOW_INFORMATION, mUI.mActionShowInformation->isChecked());
mSettings->setValue(SETTINGS_STD_CPP03, mUI.mActionCpp03->isChecked());
mSettings->setValue(SETTINGS_STD_CPP11, mUI.mActionCpp11->isChecked());
mSettings->setValue(SETTINGS_STD_C89, mUI.mActionC89->isChecked());
mSettings->setValue(SETTINGS_STD_C99, mUI.mActionC99->isChecked());
mSettings->setValue(SETTINGS_STD_C11, mUI.mActionC11->isChecked());
mSettings->setValue(SETTINGS_STD_POSIX, mUI.mActionPosix->isChecked());
// Main window settings
mSettings->setValue(SETTINGS_TOOLBARS_MAIN_SHOW, mUI.mToolBarMain->isVisible());
mSettings->setValue(SETTINGS_TOOLBARS_VIEW_SHOW, mUI.mToolBarView->isVisible());
mSettings->setValue(SETTINGS_TOOLBARS_FILTER_SHOW, mUI.mToolBarFilter->isVisible());
if (mUI.mActionEnforceCpp->isChecked())
mSettings->setValue(SETTINGS_ENFORCED_LANGUAGE, Settings::CPP);
else if (mUI.mActionEnforceC->isChecked())
mSettings->setValue(SETTINGS_ENFORCED_LANGUAGE, Settings::C);
else
mSettings->setValue(SETTINGS_ENFORCED_LANGUAGE, Settings::None);
2017-07-28 05:18:43 +02:00
mApplications->saveSettings();
2017-07-28 13:43:49 +02:00
mSettings->setValue(SETTINGS_LANGUAGE, mTranslation->getCurrentLanguage());
mSettings->setValue(SETTINGS_OPEN_PROJECT, mProject ? mProject->getFilename() : QString());
2017-07-28 12:10:10 +02:00
mUI.mResults->saveSettings(mSettings);
}
void MainWindow::doAnalyzeProject(ImportProject p)
2016-08-18 21:58:50 +02:00
{
2017-07-28 10:41:03 +02:00
clearResults();
2016-08-18 21:58:50 +02:00
mIsLogfileLoaded = false;
if (mProject) {
std::vector<std::string> v;
2017-07-28 05:08:59 +02:00
foreach (const QString &s, mProject->getProjectFile()->getExcludedPaths()) {
2016-08-18 21:58:50 +02:00
v.push_back(s.toStdString());
}
p.ignorePaths(v);
} else {
2017-07-28 05:44:15 +02:00
enableProjectActions(false);
2016-08-18 21:58:50 +02:00
}
2017-07-28 12:10:10 +02:00
mUI.mResults->clear(true);
2017-07-28 12:39:28 +02:00
mThread->clearFiles();
2016-08-18 21:58:50 +02:00
2017-07-28 12:10:10 +02:00
mUI.mResults->checkingStarted(p.fileSettings.size());
2016-08-18 21:58:50 +02:00
QDir inf(mCurrentDirectory);
const QString checkPath = inf.canonicalPath();
2017-07-28 11:20:35 +02:00
setPath(SETTINGS_LAST_CHECK_PATH, checkPath);
2016-08-18 21:58:50 +02:00
2017-07-28 10:41:03 +02:00
checkLockDownUI(); // lock UI while checking
2016-08-18 21:58:50 +02:00
2017-07-28 12:10:10 +02:00
mUI.mResults->setCheckDirectory(checkPath);
2017-07-28 05:44:15 +02:00
Settings checkSettings = getCppcheckSettings();
checkSettings.force = false;
2016-08-18 21:58:50 +02:00
if (mProject)
2017-07-28 05:08:59 +02:00
qDebug() << "Checking project file" << mProject->getProjectFile()->getFilename();
2016-08-18 21:58:50 +02:00
2016-12-09 20:48:32 +01:00
if (!checkSettings.buildDir.empty()) {
std::list<std::string> sourcefiles;
AnalyzerInformation::writeFilesTxt(checkSettings.buildDir, sourcefiles, p.fileSettings);
}
//mThread->SetanalyzeProject(true);
2017-07-28 12:39:28 +02:00
mThread->setProject(p);
mThread->check(checkSettings, true);
2016-08-18 21:58:50 +02:00
}
void MainWindow::doAnalyzeFiles(const QStringList &files)
{
2011-10-13 20:53:06 +02:00
if (files.isEmpty()) {
return;
}
2017-07-28 10:41:03 +02:00
clearResults();
mIsLogfileLoaded = false;
FileList pathList;
pathList.addPathList(files);
if (mProject) {
pathList.addExcludeList(mProject->getProjectFile()->getExcludedPaths());
} else {
2017-07-28 05:44:15 +02:00
enableProjectActions(false);
}
QStringList fileNames = pathList.getFileList();
2017-07-28 12:10:10 +02:00
mUI.mResults->clear(true);
2017-07-28 12:39:28 +02:00
mThread->clearFiles();
2011-10-13 20:53:06 +02:00
if (fileNames.isEmpty()) {
QMessageBox msg(QMessageBox::Warning,
tr("Cppcheck"),
tr("No suitable files found to check!"),
QMessageBox::Ok,
this);
msg.exec();
return;
}
2017-07-28 12:10:10 +02:00
mUI.mResults->checkingStarted(fileNames.count());
2017-07-28 12:39:28 +02:00
mThread->setFiles(fileNames);
QDir inf(mCurrentDirectory);
const QString checkPath = inf.canonicalPath();
2017-07-28 11:20:35 +02:00
setPath(SETTINGS_LAST_CHECK_PATH, checkPath);
2017-07-28 10:41:03 +02:00
checkLockDownUI(); // lock UI while checking
2017-07-28 12:10:10 +02:00
mUI.mResults->setCheckDirectory(checkPath);
2017-07-28 05:44:15 +02:00
Settings checkSettings = getCppcheckSettings();
if (mProject)
2017-07-28 05:08:59 +02:00
qDebug() << "Checking project file" << mProject->getProjectFile()->getFilename();
2016-11-19 20:38:50 +01:00
if (!checkSettings.buildDir.empty()) {
2016-12-09 20:48:32 +01:00
std::list<std::string> sourcefiles;
foreach (QString s, fileNames)
sourcefiles.push_back(s.toStdString());
AnalyzerInformation::writeFilesTxt(checkSettings.buildDir, sourcefiles, checkSettings.project.fileSettings);
2016-11-19 20:38:50 +01:00
}
2017-07-28 12:39:28 +02:00
mThread->setCheckFiles(true);
mThread->check(checkSettings, true);
}
void MainWindow::analyzeCode(const QString& code, const QString& filename)
{
// Initialize dummy ThreadResult as ErrorLogger
ThreadResult result;
2017-07-28 12:39:28 +02:00
result.setFiles(QStringList(filename));
2017-07-28 13:43:49 +02:00
connect(&result, SIGNAL(progress(int, const QString&)),
mUI.mResults, SLOT(progress(int, const QString&)));
connect(&result, SIGNAL(error(const ErrorItem &)),
mUI.mResults, SLOT(error(const ErrorItem &)));
connect(&result, SIGNAL(log(const QString &)),
this, SLOT(log(const QString &)));
connect(&result, SIGNAL(debugError(const ErrorItem &)),
this, SLOT(debugError(const ErrorItem &)));
// Create CppCheck instance
CppCheck cppcheck(result, true);
2017-07-28 05:44:15 +02:00
cppcheck.settings() = getCppcheckSettings();
// Check
2017-07-28 10:41:03 +02:00
checkLockDownUI();
clearResults();
2017-07-28 12:10:10 +02:00
mUI.mResults->checkingStarted(1);
cppcheck.check(filename.toStdString(), code.toStdString());
analysisDone();
}
QStringList MainWindow::selectFilesToAnalyze(QFileDialog::FileMode mode)
{
2011-10-13 20:53:06 +02:00
if (mProject) {
QMessageBox msgBox(this);
msgBox.setWindowTitle(tr("Cppcheck"));
const QString msg(tr("You must close the project file before selecting new files or directories!"));
msgBox.setText(msg);
msgBox.setIcon(QMessageBox::Critical);
msgBox.exec();
return QStringList();
}
QStringList selected;
// NOTE: we use QFileDialog::getOpenFileNames() and
// QFileDialog::getExistingDirectory() because they show native Windows
2012-09-09 13:51:34 +02:00
// selection dialog which is a lot more usable than Qt:s own dialog.
2011-10-13 20:53:06 +02:00
if (mode == QFileDialog::ExistingFiles) {
selected = QFileDialog::getOpenFileNames(this,
tr("Select files to check"),
2017-07-28 11:20:35 +02:00
getPath(SETTINGS_LAST_CHECK_PATH),
tr("C/C++ Source, Compile database, Visual Studio (%1 %2 *.sln *.vcxproj)")
.arg(FileList::getDefaultFilters().join(" "))
2016-10-01 12:04:13 +02:00
.arg(compile_commands_json));
if (selected.isEmpty())
mCurrentDirectory.clear();
2011-10-13 20:53:06 +02:00
else {
QFileInfo inf(selected[0]);
mCurrentDirectory = inf.absolutePath();
}
2017-07-28 05:44:15 +02:00
formatAndSetTitle();
2011-10-13 20:53:06 +02:00
} else if (mode == QFileDialog::DirectoryOnly) {
QString dir = QFileDialog::getExistingDirectory(this,
tr("Select directory to check"),
2017-07-28 11:20:35 +02:00
getPath(SETTINGS_LAST_CHECK_PATH));
if (!dir.isEmpty()) {
qDebug() << "Setting current directory to: " << dir;
mCurrentDirectory = dir;
selected.append(dir);
dir = QDir::toNativeSeparators(dir);
2017-07-28 05:44:15 +02:00
formatAndSetTitle(dir);
}
}
2013-02-19 21:39:50 +01:00
2017-07-28 11:20:35 +02:00
setPath(SETTINGS_LAST_CHECK_PATH, mCurrentDirectory);
2013-02-19 21:39:50 +01:00
return selected;
}
void MainWindow::analyzeFiles()
{
QStringList selected = selectFilesToAnalyze(QFileDialog::ExistingFiles);
const QString file0 = (selected.size() ? selected[0].toLower() : "");
if (file0.endsWith(".sln") || file0.endsWith(".vcxproj") || file0.endsWith(compile_commands_json)) {
ImportProject p;
p.import(selected[0].toStdString());
if (file0.endsWith(".sln")) {
QStringList configs;
for (std::list<ImportProject::FileSettings>::const_iterator it = p.fileSettings.begin(); it != p.fileSettings.end(); ++it) {
const QString cfg(QString::fromStdString(it->cfg));
if (!configs.contains(cfg))
configs.push_back(cfg);
}
configs.sort();
bool ok = false;
const QString cfg = QInputDialog::getItem(this, tr("Select configuration"), tr("Select the configuration that will be checked"), configs, 0, false, &ok);
if (!ok)
return;
p.ignoreOtherConfigs(cfg.toStdString());
}
doAnalyzeProject(p);
return;
}
doAnalyzeFiles(selected);
}
void MainWindow::analyzeDirectory()
{
QStringList dir = selectFilesToAnalyze(QFileDialog::DirectoryOnly);
if (dir.isEmpty())
return;
QDir checkDir(dir[0]);
QStringList filters;
filters << "*.cppcheck";
checkDir.setFilter(QDir::Files | QDir::Readable);
checkDir.setNameFilters(filters);
QStringList projFiles = checkDir.entryList();
2011-10-13 20:53:06 +02:00
if (!projFiles.empty()) {
if (projFiles.size() == 1) {
// If one project file found, suggest loading it
QMessageBox msgBox(this);
msgBox.setWindowTitle(tr("Cppcheck"));
const QString msg(tr("Found project file: %1\n\nDo you want to "
"load this project file instead?").arg(projFiles[0]));
msgBox.setText(msg);
msgBox.setIcon(QMessageBox::Warning);
msgBox.addButton(QMessageBox::Yes);
msgBox.addButton(QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::Yes);
int dlgResult = msgBox.exec();
2011-10-13 20:53:06 +02:00
if (dlgResult == QMessageBox::Yes) {
QString path = checkDir.canonicalPath();
if (!path.endsWith("/"))
path += "/";
path += projFiles[0];
2017-07-28 05:44:15 +02:00
loadProjectFile(path);
2011-10-13 20:53:06 +02:00
} else {
doAnalyzeFiles(dir);
}
2011-10-13 20:53:06 +02:00
} else {
// If multiple project files found inform that there are project
// files also available.
QMessageBox msgBox(this);
msgBox.setWindowTitle(tr("Cppcheck"));
const QString msg(tr("Found project files from the directory.\n\n"
"Do you want to proceed checking without "
"using any of these project files?"));
msgBox.setText(msg);
msgBox.setIcon(QMessageBox::Warning);
msgBox.addButton(QMessageBox::Yes);
msgBox.addButton(QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::Yes);
int dlgResult = msgBox.exec();
2011-10-13 20:53:06 +02:00
if (dlgResult == QMessageBox::Yes) {
doAnalyzeFiles(dir);
}
}
2011-10-13 20:53:06 +02:00
} else {
doAnalyzeFiles(dir);
}
}
2017-07-28 05:44:15 +02:00
void MainWindow::addIncludeDirs(const QStringList &includeDirs, Settings &result)
2010-11-26 21:51:38 +01:00
{
QString dir;
foreach (dir, includeDirs) {
2010-11-26 21:51:38 +01:00
QString incdir;
if (!QDir::isAbsolutePath(dir))
incdir = mCurrentDirectory + "/";
incdir += dir;
incdir = QDir::cleanPath(incdir);
// include paths must end with '/'
if (!incdir.endsWith("/"))
incdir += "/";
result.includePaths.push_back(incdir.toStdString());
2010-11-26 21:51:38 +01:00
}
}
2017-07-28 05:44:15 +02:00
Library::Error MainWindow::loadLibrary(Library *library, QString filename)
{
Library::Error ret;
// Try to load the library from the project folder..
if (mProject) {
2017-07-28 05:08:59 +02:00
QString path = QFileInfo(mProject->getProjectFile()->getFilename()).canonicalPath();
ret = library->load(NULL, (path+"/"+filename).toLatin1());
if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND)
return ret;
}
// Try to load the library from the application folder..
const QString appPath = QFileInfo(QCoreApplication::applicationFilePath()).canonicalPath();
ret = library->load(NULL, (appPath+"/"+filename).toLatin1());
if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND)
return ret;
ret = library->load(NULL, (appPath+"/cfg/"+filename).toLatin1());
if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND)
return ret;
#ifdef CFGDIR
// Try to load the library from CFGDIR..
const QString cfgdir = CFGDIR;
if (!cfgdir.isEmpty()) {
ret = library->load(NULL, (cfgdir+"/"+filename).toLatin1());
if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND)
return ret;
ret = library->load(NULL, (cfgdir+"/cfg/"+filename).toLatin1());
if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND)
return ret;
}
#endif
// Try to load the library from the cfg subfolder..
const QString datadir = mSettings->value("DATADIR", QString()).toString();
if (!datadir.isEmpty()) {
ret = library->load(NULL, (datadir+"/"+filename).toLatin1());
if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND)
return ret;
ret = library->load(NULL, (datadir+"/cfg/"+filename).toLatin1());
if (ret.errorcode != Library::ErrorCode::FILE_NOT_FOUND)
return ret;
}
return ret;
}
2017-07-28 05:44:15 +02:00
bool MainWindow::tryLoadLibrary(Library *library, QString filename)
{
2017-07-28 05:44:15 +02:00
const Library::Error error = loadLibrary(library, filename);
if (error.errorcode != Library::ErrorCode::OK) {
if (error.errorcode == Library::UNKNOWN_ELEMENT) {
QMessageBox::information(this, tr("Information"), tr("The library '%1' contains unknown elements:\n%2").arg(filename).arg(error.reason.c_str()));
return true;
}
QString errmsg;
switch (error.errorcode) {
case Library::ErrorCode::OK:
break;
case Library::ErrorCode::FILE_NOT_FOUND:
errmsg = tr("File not found");
break;
case Library::ErrorCode::BAD_XML:
errmsg = tr("Bad XML");
break;
case Library::ErrorCode::MISSING_ATTRIBUTE:
errmsg = tr("Missing attribute");
break;
case Library::ErrorCode::BAD_ATTRIBUTE_VALUE:
errmsg = tr("Bad attribute value");
break;
case Library::ErrorCode::UNSUPPORTED_FORMAT:
errmsg = tr("Unsupported format");
break;
case Library::ErrorCode::DUPLICATE_PLATFORM_TYPE:
errmsg = tr("Duplicate platform type");
break;
case Library::ErrorCode::PLATFORM_TYPE_REDEFINED:
errmsg = tr("Platform type redefined");
break;
2015-12-07 11:24:54 +01:00
case Library::ErrorCode::UNKNOWN_ELEMENT:
errmsg = tr("Unknown element");
break;
default:
errmsg = tr("Unknown issue");
break;
}
if (!error.reason.empty())
errmsg += " '" + QString::fromStdString(error.reason) + "'";
QMessageBox::information(this, tr("Information"), tr("Failed to load the selected library '%1'.\n%2").arg(filename).arg(errmsg));
return false;
}
return true;
}
2017-07-28 05:44:15 +02:00
Settings MainWindow::getCppcheckSettings()
{
2017-07-28 05:44:15 +02:00
saveSettings(); // Save settings
Settings result;
// If project file loaded, read settings from it
2011-10-13 20:53:06 +02:00
if (mProject) {
2017-07-28 04:57:28 +02:00
ProjectFile *pfile = mProject->getProjectFile();
2017-07-28 05:08:59 +02:00
QStringList dirs = pfile->getIncludeDirs();
2017-07-28 05:44:15 +02:00
addIncludeDirs(dirs, result);
2010-11-26 21:51:38 +01:00
2017-07-28 05:08:59 +02:00
const QStringList defines = pfile->getDefines();
foreach (QString define, defines) {
if (!result.userDefines.empty())
result.userDefines += ";";
result.userDefines += define.toStdString();
}
2017-07-28 05:08:59 +02:00
const QStringList libraries = pfile->getLibraries();
foreach (QString library, libraries) {
const QString filename = library + ".cfg";
2017-07-28 05:44:15 +02:00
tryLoadLibrary(&result.library, filename);
2013-12-29 18:06:31 +01:00
}
2017-07-28 05:08:59 +02:00
const QStringList suppressions = pfile->getSuppressions();
foreach (QString suppression, suppressions) {
2013-12-30 22:32:50 +01:00
result.nomsg.addSuppressionLine(suppression.toStdString());
}
// Only check the given -D configuration
if (!defines.isEmpty())
result.maxConfigs = 1;
2016-11-19 20:38:50 +01:00
2017-07-28 05:08:59 +02:00
QString buildDir = pfile->getBuildDir();
2016-11-19 20:38:50 +01:00
if (!buildDir.isEmpty()) {
2017-07-28 05:08:59 +02:00
QString prjpath = QFileInfo(pfile->getFilename()).absolutePath();
2016-11-19 20:38:50 +01:00
result.buildDir = (prjpath + '/' + buildDir).toStdString();
}
}
// Include directories (and files) are searched in listed order.
// Global include directories must be added AFTER the per project include
// directories so per project include directories can override global ones.
const QString globalIncludes = mSettings->value(SETTINGS_GLOBAL_INCLUDE_PATHS).toString();
2011-10-13 20:53:06 +02:00
if (!globalIncludes.isEmpty()) {
QStringList includes = globalIncludes.split(";");
2017-07-28 05:44:15 +02:00
addIncludeDirs(includes, result);
}
result.addEnabled("warning");
result.addEnabled("style");
result.addEnabled("performance");
result.addEnabled("portability");
result.addEnabled("information");
result.addEnabled("missingInclude");
2016-11-19 20:38:50 +01:00
if (!result.buildDir.empty())
result.addEnabled("unusedFunction");
2010-08-28 08:16:01 +02:00
result.debug = false;
result.debugwarnings = mSettings->value(SETTINGS_SHOW_DEBUG_WARNINGS, false).toBool();
2015-07-25 19:17:24 +02:00
result.quiet = false;
result.verbose = true;
result.force = mSettings->value(SETTINGS_CHECK_FORCE, 1).toBool();
result.xml = false;
result.jobs = mSettings->value(SETTINGS_CHECK_THREADS, 1).toInt();
result.inlineSuppressions = mSettings->value(SETTINGS_INLINE_SUPPRESSIONS, false).toBool();
result.inconclusive = mSettings->value(SETTINGS_INCONCLUSIVE_ERRORS, false).toBool();
result.platformType = (Settings::PlatformType) mSettings->value(SETTINGS_CHECKED_PLATFORM, 0).toInt();
result.standards.cpp = mSettings->value(SETTINGS_STD_CPP11, true).toBool() ? Standards::CPP11 : Standards::CPP03;
result.standards.c = mSettings->value(SETTINGS_STD_C99, true).toBool() ? Standards::C99 : (mSettings->value(SETTINGS_STD_C11, false).toBool() ? Standards::C11 : Standards::C89);
result.standards.posix = mSettings->value(SETTINGS_STD_POSIX, false).toBool();
result.enforcedLang = (Settings::Language)mSettings->value(SETTINGS_ENFORCED_LANGUAGE, 0).toInt();
2017-07-28 05:44:15 +02:00
const bool std = tryLoadLibrary(&result.library, "std.cfg");
bool posix = true;
if (result.standards.posix)
2017-07-28 05:44:15 +02:00
posix = tryLoadLibrary(&result.library, "posix.cfg");
bool windows = true;
if (result.platformType == Settings::Win32A || result.platformType == Settings::Win32W || result.platformType == Settings::Win64)
2017-07-28 05:44:15 +02:00
windows = tryLoadLibrary(&result.library, "windows.cfg");
if (!std || !posix || !windows)
2017-05-03 21:44:59 +02:00
QMessageBox::critical(this, tr("Error"), tr("Failed to load %1. Your Cppcheck installation is broken. You can use --data-dir=<directory> at the command line to specify where this file is located. Please note that --data-dir is supposed to be used by installation scripts and therefore the GUI does not start when it is used, all that happens is that the setting is configured.").arg(!std ? "std.cfg" : !posix ? "posix.cfg" : "windows.cfg"));
if (result.jobs <= 1) {
result.jobs = 1;
}
result.terminate(false);
return result;
}
void MainWindow::analysisDone()
{
2011-10-13 20:53:06 +02:00
if (mExiting) {
close();
return;
}
2017-07-28 12:10:10 +02:00
mUI.mResults->checkingFinished();
2017-07-28 05:44:15 +02:00
enableCheckButtons(true);
mUI.mActionSettings->setEnabled(true);
mUI.mActionOpenXML->setEnabled(true);
if (mProject) {
2017-07-28 05:44:15 +02:00
enableProjectActions(true);
} else if (mIsLogfileLoaded) {
2017-07-30 09:46:01 +02:00
mUI.mActionReanalyzeModified->setEnabled(false);
mUI.mActionReanalyzeAll->setEnabled(false);
}
2017-07-28 05:44:15 +02:00
enableProjectOpenActions(true);
mPlatformActions->setEnabled(true);
mCStandardActions->setEnabled(true);
mCppStandardActions->setEnabled(true);
mSelectLanguageActions->setEnabled(true);
mUI.mActionPosix->setEnabled(true);
if (mScratchPad)
mScratchPad->setEnabled(true);
if (mProject && !mProject->getProjectFile()->getBuildDir().isEmpty()) {
const QString prjpath = QFileInfo(mProject->getProjectFile()->getFilename()).absolutePath();
const QString buildDir = prjpath + '/' + mProject->getProjectFile()->getBuildDir();
if (QDir(buildDir).exists()) {
mUI.mResults->saveStatistics(buildDir + "/statistics.txt");
mUI.mResults->save(buildDir + "/lastResults.xml", Report::XMLV2);
}
}
2017-07-28 12:10:10 +02:00
if (mUI.mResults->hasResults()) {
mUI.mActionClearResults->setEnabled(true);
mUI.mActionSave->setEnabled(true);
mUI.mActionPrint->setEnabled(true);
mUI.mActionPrintPreview->setEnabled(true);
}
for (int i = 0; i < MaxRecentProjects + 1; i++) {
if (mRecentProjectActs[i] != NULL)
mRecentProjectActs[i]->setEnabled(true);
}
// Notify user - if the window is not active - that check is ready
QApplication::alert(this, 3000);
if (mSettings->value(SETTINGS_SHOW_STATISTICS, false).toBool())
2017-07-28 10:41:03 +02:00
showStatistics();
}
2017-07-28 10:41:03 +02:00
void MainWindow::checkLockDownUI()
{
2017-07-28 05:44:15 +02:00
enableCheckButtons(false);
mUI.mActionSettings->setEnabled(false);
mUI.mActionOpenXML->setEnabled(false);
2017-07-28 05:44:15 +02:00
enableProjectActions(false);
enableProjectOpenActions(false);
mPlatformActions->setEnabled(false);
mCStandardActions->setEnabled(false);
mCppStandardActions->setEnabled(false);
mSelectLanguageActions->setEnabled(false);
mUI.mActionPosix->setEnabled(false);
if (mScratchPad)
mScratchPad->setEnabled(false);
for (int i = 0; i < MaxRecentProjects + 1; i++) {
if (mRecentProjectActs[i] != NULL)
mRecentProjectActs[i]->setEnabled(false);
}
}
2017-07-28 10:41:03 +02:00
void MainWindow::programSettings()
{
SettingsDialog dialog(mApplications, mTranslation, this);
2011-10-13 20:53:06 +02:00
if (dialog.exec() == QDialog::Accepted) {
2017-07-28 13:43:49 +02:00
dialog.saveSettingValues();
mUI.mResults->updateSettings(dialog.showFullPath(),
dialog.saveFullPath(),
dialog.saveAllErrors(),
dialog.showNoErrorsMessage(),
dialog.showErrorId(),
dialog.showInconclusive());
const QString newLang = mSettings->value(SETTINGS_LANGUAGE, "en").toString();
2017-07-28 05:44:15 +02:00
setLanguage(newLang);
}
}
void MainWindow::reAnalyzeModified()
{
reAnalyze(false);
}
void MainWindow::reAnalyzeAll()
{
reAnalyze(true);
}
void MainWindow::reAnalyzeSelected(QStringList files, bool all)
{
if (files.empty())
return;
2017-07-28 12:39:28 +02:00
if (mThread->isChecking())
return;
// Clear details, statistics and progress
2017-07-28 12:10:10 +02:00
mUI.mResults->clear(false);
for (int i = 0; i < files.size(); ++i)
2017-07-28 12:10:10 +02:00
mUI.mResults->clearRecheckFile(files[i]);
2017-07-28 12:10:10 +02:00
mCurrentDirectory = mUI.mResults->getCheckDirectory();
FileList pathList;
pathList.addPathList(files);
if (mProject)
pathList.addExcludeList(mProject->getProjectFile()->getExcludedPaths());
QStringList fileNames = pathList.getFileList();
2017-07-28 10:41:03 +02:00
checkLockDownUI(); // lock UI while checking
2017-07-28 12:10:10 +02:00
mUI.mResults->checkingStarted(fileNames.size());
2017-07-28 12:39:28 +02:00
mThread->setCheckFiles(fileNames);
// Saving last check start time, otherwise unchecked modified files will not be
// considered in "Modified Files Check" performed after "Selected Files Check"
// TODO: Should we store per file CheckStartTime?
2017-07-28 12:39:28 +02:00
QDateTime saveCheckStartTime = mThread->getCheckStartTime();
mThread->check(getCppcheckSettings(), all);
mThread->setCheckStartTime(saveCheckStartTime);
}
void MainWindow::reAnalyze(bool all)
{
2017-07-28 12:39:28 +02:00
const QStringList files = mThread->getReCheckFiles(all);
if (files.empty())
return;
// Clear details, statistics and progress
2017-07-28 12:10:10 +02:00
mUI.mResults->clear(all);
// Clear results for changed files
for (int i = 0; i < files.size(); ++i)
2017-07-28 12:10:10 +02:00
mUI.mResults->clear(files[i]);
2017-07-28 10:41:03 +02:00
checkLockDownUI(); // lock UI while checking
2017-07-28 12:10:10 +02:00
mUI.mResults->checkingStarted(files.size());
if (mProject)
2017-07-28 05:08:59 +02:00
qDebug() << "Rechecking project file" << mProject->getProjectFile()->getFilename();
2017-07-28 12:39:28 +02:00
mThread->setCheckFiles(all);
mThread->check(getCppcheckSettings(), all);
}
2017-07-28 10:41:03 +02:00
void MainWindow::clearResults()
{
2017-07-28 12:10:10 +02:00
mUI.mResults->clear(true);
mUI.mActionClearResults->setEnabled(false);
mUI.mActionSave->setEnabled(false);
mUI.mActionPrint->setEnabled(false);
mUI.mActionPrintPreview->setEnabled(false);
}
2017-07-28 10:41:03 +02:00
void MainWindow::openResults()
{
2017-07-28 12:10:10 +02:00
if (mUI.mResults->hasResults()) {
QMessageBox msgBox(this);
msgBox.setWindowTitle(tr("Cppcheck"));
const QString msg(tr("Current results will be cleared.\n\n"
"Opening a new XML file will clear current results."
"Do you want to proceed?"));
msgBox.setText(msg);
msgBox.setIcon(QMessageBox::Warning);
msgBox.addButton(QMessageBox::Yes);
msgBox.addButton(QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::Yes);
int dlgResult = msgBox.exec();
if (dlgResult == QMessageBox::No) {
return;
}
}
QString selectedFilter;
2011-08-22 21:37:38 +02:00
const QString filter(tr("XML files (*.xml)"));
QString selectedFile = QFileDialog::getOpenFileName(this,
tr("Open the report file"),
2017-07-28 11:20:35 +02:00
getPath(SETTINGS_LAST_RESULT_PATH),
filter,
&selectedFilter);
if (!selectedFile.isEmpty()) {
2017-07-28 05:44:15 +02:00
loadResults(selectedFile);
}
}
2017-07-28 05:44:15 +02:00
void MainWindow::loadResults(const QString selectedFile)
{
2011-10-13 20:53:06 +02:00
if (!selectedFile.isEmpty()) {
if (mProject)
2017-07-28 10:41:03 +02:00
closeProjectFile();
mIsLogfileLoaded = true;
2017-07-28 12:10:10 +02:00
mUI.mResults->clear(true);
2017-07-30 09:46:01 +02:00
mUI.mActionReanalyzeModified->setEnabled(false);
mUI.mActionReanalyzeAll->setEnabled(false);
2017-07-28 12:10:10 +02:00
mUI.mResults->readErrorsXml(selectedFile);
2017-07-28 11:20:35 +02:00
setPath(SETTINGS_LAST_RESULT_PATH, selectedFile);
}
}
2017-07-28 05:44:15 +02:00
void MainWindow::loadResults(const QString selectedFile, const QString sourceDirectory)
{
2017-07-28 05:44:15 +02:00
loadResults(selectedFile);
2017-07-28 12:10:10 +02:00
mUI.mResults->setCheckDirectory(sourceDirectory);
}
2017-07-28 05:44:15 +02:00
void MainWindow::enableCheckButtons(bool enable)
{
mUI.mActionStop->setEnabled(!enable);
2017-07-30 09:46:01 +02:00
mUI.mActionAnalyzeFiles->setEnabled(enable);
if (!enable || mProject || mThread->hasPreviousFiles()) {
mUI.mActionReanalyzeModified->setEnabled(!mProject && enable);
2017-07-30 09:46:01 +02:00
mUI.mActionReanalyzeAll->setEnabled(enable);
}
2017-07-30 09:46:01 +02:00
mUI.mActionAnalyzeDirectory->setEnabled(enable);
}
2017-07-28 10:41:03 +02:00
void MainWindow::showStyle(bool checked)
2009-03-22 18:39:44 +01:00
{
2017-07-28 12:10:10 +02:00
mUI.mResults->showResults(ShowTypes::ShowStyle, checked);
2009-03-22 18:39:44 +01:00
}
2017-07-28 10:41:03 +02:00
void MainWindow::showErrors(bool checked)
2009-03-22 18:39:44 +01:00
{
2017-07-28 12:10:10 +02:00
mUI.mResults->showResults(ShowTypes::ShowErrors, checked);
2009-03-22 18:39:44 +01:00
}
2017-07-28 10:41:03 +02:00
void MainWindow::showWarnings(bool checked)
{
2017-07-28 12:10:10 +02:00
mUI.mResults->showResults(ShowTypes::ShowWarnings, checked);
}
2017-07-28 10:41:03 +02:00
void MainWindow::showPortability(bool checked)
{
2017-07-28 12:10:10 +02:00
mUI.mResults->showResults(ShowTypes::ShowPortability, checked);
}
2017-07-28 10:41:03 +02:00
void MainWindow::showPerformance(bool checked)
{
2017-07-28 12:10:10 +02:00
mUI.mResults->showResults(ShowTypes::ShowPerformance, checked);
}
2017-07-28 10:41:03 +02:00
void MainWindow::showInformation(bool checked)
{
2017-07-28 12:10:10 +02:00
mUI.mResults->showResults(ShowTypes::ShowInformation, checked);
}
2017-07-28 10:41:03 +02:00
void MainWindow::checkAll()
{
2017-07-28 05:44:15 +02:00
toggleAllChecked(true);
}
2017-07-28 10:41:03 +02:00
void MainWindow::uncheckAll()
{
2017-07-28 05:44:15 +02:00
toggleAllChecked(false);
}
void MainWindow::closeEvent(QCloseEvent *event)
{
// Check that we aren't checking files
2017-07-28 12:39:28 +02:00
if (!mThread->isChecking()) {
2017-07-28 05:44:15 +02:00
saveSettings();
event->accept();
2011-10-13 20:53:06 +02:00
} else {
2011-08-22 21:37:38 +02:00
const QString text(tr("Checking is running.\n\n" \
2015-03-07 08:48:16 +01:00
"Do you want to stop the checking and exit Cppcheck?"));
QMessageBox msg(QMessageBox::Warning,
tr("Cppcheck"),
text,
QMessageBox::Yes | QMessageBox::No,
this);
msg.setDefaultButton(QMessageBox::No);
int rv = msg.exec();
2011-10-13 20:53:06 +02:00
if (rv == QMessageBox::Yes) {
// This isn't really very clean way to close threads but since the app is
// exiting it doesn't matter.
2017-07-28 12:39:28 +02:00
mThread->stop();
2017-07-28 05:44:15 +02:00
saveSettings();
mExiting = true;
}
event->ignore();
}
}
2017-07-28 05:44:15 +02:00
void MainWindow::toggleAllChecked(bool checked)
{
mUI.mActionShowStyle->setChecked(checked);
2017-07-28 10:41:03 +02:00
showStyle(checked);
mUI.mActionShowErrors->setChecked(checked);
2017-07-28 10:41:03 +02:00
showErrors(checked);
mUI.mActionShowWarnings->setChecked(checked);
2017-07-28 10:41:03 +02:00
showWarnings(checked);
mUI.mActionShowPortability->setChecked(checked);
2017-07-28 10:41:03 +02:00
showPortability(checked);
mUI.mActionShowPerformance->setChecked(checked);
2017-07-28 10:41:03 +02:00
showPerformance(checked);
mUI.mActionShowInformation->setChecked(checked);
2017-07-28 10:41:03 +02:00
showInformation(checked);
}
2017-07-28 10:41:03 +02:00
void MainWindow::about()
{
AboutDialog *dlg = new AboutDialog(CppCheck::version(), CppCheck::extraVersion(), this);
dlg->exec();
}
2017-07-28 10:41:03 +02:00
void MainWindow::showLicense()
{
FileViewDialog *dlg = new FileViewDialog(":COPYING", tr("License"), this);
dlg->resize(570, 400);
dlg->exec();
}
2017-07-28 10:41:03 +02:00
void MainWindow::showAuthors()
{
FileViewDialog *dlg = new FileViewDialog(":AUTHORS", tr("Authors"), this);
2009-06-14 22:22:19 +02:00
dlg->resize(350, 400);
dlg->exec();
}
2017-07-28 10:41:03 +02:00
void MainWindow::performSelectedFilesCheck(QStringList selectedFilesList)
{
reAnalyzeSelected(selectedFilesList, true);
}
2017-07-28 10:41:03 +02:00
void MainWindow::save()
{
QString selectedFilter;
2017-07-29 19:36:01 +02:00
const QString filter(tr("XML files (*.xml);;Text files (*.txt);;CSV files (*.csv)"));
QString selectedFile = QFileDialog::getSaveFileName(this,
tr("Save the report file"),
2017-07-28 11:20:35 +02:00
getPath(SETTINGS_LAST_RESULT_PATH),
filter,
&selectedFilter);
2011-10-13 20:53:06 +02:00
if (!selectedFile.isEmpty()) {
Report::Type type = Report::TXT;
2017-07-29 19:36:01 +02:00
if (selectedFilter == tr("XML files (*.xml)")) {
type = Report::XMLV2;
if (!selectedFile.endsWith(".xml", Qt::CaseInsensitive))
selectedFile += ".xml";
2011-10-13 20:53:06 +02:00
} else if (selectedFilter == tr("Text files (*.txt)")) {
type = Report::TXT;
if (!selectedFile.endsWith(".txt", Qt::CaseInsensitive))
selectedFile += ".txt";
2011-10-13 20:53:06 +02:00
} else if (selectedFilter == tr("CSV files (*.csv)")) {
type = Report::CSV;
if (!selectedFile.endsWith(".csv", Qt::CaseInsensitive))
selectedFile += ".csv";
2011-10-13 20:53:06 +02:00
} else {
if (selectedFile.endsWith(".xml", Qt::CaseInsensitive))
2017-07-29 19:36:01 +02:00
type = Report::XMLV2;
else if (selectedFile.endsWith(".txt", Qt::CaseInsensitive))
type = Report::TXT;
else if (selectedFile.endsWith(".csv", Qt::CaseInsensitive))
type = Report::CSV;
}
2017-07-28 12:10:10 +02:00
mUI.mResults->save(selectedFile, type);
2017-07-28 11:20:35 +02:00
setPath(SETTINGS_LAST_RESULT_PATH, selectedFile);
}
}
2017-07-28 10:41:03 +02:00
void MainWindow::resultsAdded()
{
}
2017-07-28 10:41:03 +02:00
void MainWindow::toggleMainToolBar()
{
mUI.mToolBarMain->setVisible(mUI.mActionToolBarMain->isChecked());
}
2017-07-28 10:41:03 +02:00
void MainWindow::toggleViewToolBar()
{
mUI.mToolBarView->setVisible(mUI.mActionToolBarView->isChecked());
}
2017-07-28 10:41:03 +02:00
void MainWindow::toggleFilterToolBar()
{
mUI.mToolBarFilter->setVisible(mUI.mActionToolBarFilter->isChecked());
mLineEditFilter->clear(); // Clearing the filter also disables filtering
}
2017-07-28 05:44:15 +02:00
void MainWindow::formatAndSetTitle(const QString &text)
{
QString title;
if (text.isEmpty())
title = tr("Cppcheck");
else
title = QString(tr("Cppcheck - %1")).arg(text);
setWindowTitle(title);
}
2017-07-28 05:44:15 +02:00
void MainWindow::setLanguage(const QString &code)
{
2017-07-28 13:43:49 +02:00
const QString currentLang = mTranslation->getCurrentLanguage();
if (currentLang == code)
return;
2017-07-28 13:43:49 +02:00
if (mTranslation->setLanguage(code)) {
2009-07-02 12:48:32 +02:00
//Translate everything that is visible here
mUI.retranslateUi(this);
2017-07-28 12:10:10 +02:00
mUI.mResults->translate();
delete mLogView;
mLogView = 0;
}
}
2017-07-28 10:41:03 +02:00
void MainWindow::aboutToShowViewMenu()
{
mUI.mActionToolBarMain->setChecked(mUI.mToolBarMain->isVisible());
mUI.mActionToolBarView->setChecked(mUI.mToolBarView->isVisible());
mUI.mActionToolBarFilter->setChecked(mUI.mToolBarFilter->isVisible());
}
void MainWindow::stopAnalysis()
{
2017-07-28 12:39:28 +02:00
mThread->stop();
2017-07-28 12:10:10 +02:00
mUI.mResults->disableProgressbar();
}
2017-07-28 10:41:03 +02:00
void MainWindow::openHelpContents()
{
2017-07-28 05:44:15 +02:00
openOnlineHelp();
}
2017-07-28 05:44:15 +02:00
void MainWindow::openOnlineHelp()
{
QDesktopServices::openUrl(QUrl(OnlineHelpURL));
}
2017-07-28 10:41:03 +02:00
void MainWindow::openProjectFile()
{
const QString lastPath = mSettings->value(SETTINGS_LAST_PROJECT_PATH, QString()).toString();
const QString filter = tr("Project files (*.cppcheck);;All files(*.*)");
const QString filepath = QFileDialog::getOpenFileName(this,
tr("Select Project File"),
2017-07-28 11:20:35 +02:00
getPath(SETTINGS_LAST_PROJECT_PATH),
filter);
2011-10-13 20:53:06 +02:00
if (!filepath.isEmpty()) {
const QFileInfo fi(filepath);
if (fi.exists() && fi.isFile() && fi.isReadable()) {
2017-07-28 11:20:35 +02:00
setPath(SETTINGS_LAST_PROJECT_PATH, filepath);
2017-07-28 05:44:15 +02:00
loadProjectFile(filepath);
}
}
}
2017-07-28 10:41:03 +02:00
void MainWindow::showScratchpad()
{
if (!mScratchPad)
mScratchPad = new ScratchPad(*this);
mScratchPad->show();
if (!mScratchPad->isActiveWindow())
mScratchPad->activateWindow();
}
2017-07-28 05:44:15 +02:00
void MainWindow::loadProjectFile(const QString &filePath)
{
QFileInfo inf(filePath);
const QString filename = inf.fileName();
formatAndSetTitle(tr("Project:") + ' ' + filename);
2017-07-28 05:44:15 +02:00
addProjectMRU(filePath);
mIsLogfileLoaded = false;
mUI.mActionCloseProjectFile->setEnabled(true);
mUI.mActionEditProjectFile->setEnabled(true);
delete mProject;
mProject = new Project(filePath, this);
analyzeProject(mProject);
}
void MainWindow::analyzeProject(Project *project)
{
2017-07-28 04:57:28 +02:00
if (!project->isOpen()) {
if (!project->open()) {
2012-10-14 17:40:51 +02:00
delete mProject;
mProject = 0;
return;
}
}
2017-07-28 04:57:28 +02:00
QFileInfo inf(project->getFilename());
2017-07-28 05:08:59 +02:00
const QString rootpath = project->getProjectFile()->getRootPath();
// If the root path is not given or is not "current dir", use project
// file's location directory as root path
if (rootpath.isEmpty() || rootpath == ".")
mCurrentDirectory = inf.canonicalPath();
else if (rootpath.startsWith("."))
mCurrentDirectory = inf.canonicalPath() + rootpath.mid(1);
else
mCurrentDirectory = rootpath;
if (!project->getProjectFile()->getBuildDir().isEmpty()) {
QString buildDir = project->getProjectFile()->getBuildDir();
if (!QDir::isAbsolutePath(buildDir))
buildDir = mCurrentDirectory + '/' + buildDir;
if (!QDir(buildDir).exists()) {
QMessageBox msg(QMessageBox::Critical,
tr("Cppcheck"),
tr("Build dir '%1' does not exist, create it?").arg(buildDir),
QMessageBox::Yes | QMessageBox::No,
this);
if (msg.exec() == QMessageBox::Yes) {
QDir().mkdir(buildDir);
}
}
}
2017-07-28 05:08:59 +02:00
if (!project->getProjectFile()->getImportProject().isEmpty()) {
2016-08-18 21:58:50 +02:00
ImportProject p;
2017-07-28 05:08:59 +02:00
QString prjfile = inf.canonicalPath() + '/' + project->getProjectFile()->getImportProject();
2016-08-18 21:58:50 +02:00
p.import(prjfile.toStdString());
doAnalyzeProject(p);
2016-08-18 21:58:50 +02:00
return;
}
2017-07-28 05:08:59 +02:00
QStringList paths = project->getProjectFile()->getCheckPaths();
// If paths not given then check the root path (which may be the project
// file's location, see above). This is to keep the compatibility with
// old "silent" project file loading when we checked the director where the
// project file was located.
2011-10-13 20:53:06 +02:00
if (paths.isEmpty()) {
paths << mCurrentDirectory;
}
// Convert relative paths to absolute paths
2011-10-13 20:53:06 +02:00
for (int i = 0; i < paths.size(); i++) {
if (!QDir::isAbsolutePath(paths[i])) {
QString path = mCurrentDirectory + "/";
path += paths[i];
paths[i] = QDir::cleanPath(path);
}
}
doAnalyzeFiles(paths);
}
2017-07-28 10:41:03 +02:00
void MainWindow::newProjectFile()
{
const QString filter = tr("Project files (*.cppcheck);;All files(*.*)");
QString filepath = QFileDialog::getSaveFileName(this,
tr("Select Project Filename"),
2017-07-28 11:20:35 +02:00
getPath(SETTINGS_LAST_PROJECT_PATH),
filter);
if (filepath.isEmpty())
return;
2013-02-09 19:36:48 +01:00
2017-07-28 11:20:35 +02:00
setPath(SETTINGS_LAST_PROJECT_PATH, filepath);
2013-02-19 21:39:50 +01:00
2017-07-28 05:44:15 +02:00
enableProjectActions(true);
QFileInfo inf(filepath);
const QString filename = inf.fileName();
2017-07-28 05:44:15 +02:00
formatAndSetTitle(tr("Project:") + QString(" ") + filename);
delete mProject;
mProject = new Project(filepath, this);
2017-07-28 04:57:28 +02:00
mProject->create();
if (mProject->edit()) {
2017-07-28 05:44:15 +02:00
addProjectMRU(filepath);
analyzeProject(mProject);
}
}
2017-07-28 10:41:03 +02:00
void MainWindow::closeProjectFile()
{
delete mProject;
mProject = NULL;
2017-07-28 05:44:15 +02:00
enableProjectActions(false);
enableProjectOpenActions(true);
formatAndSetTitle();
}
2017-07-28 10:41:03 +02:00
void MainWindow::editProjectFile()
{
2011-10-13 20:53:06 +02:00
if (!mProject) {
QMessageBox msg(QMessageBox::Critical,
tr("Cppcheck"),
QString(tr("No project file loaded")),
QMessageBox::Ok,
this);
msg.exec();
return;
}
2017-07-28 04:57:28 +02:00
if (mProject->edit())
analyzeProject(mProject);
}
2017-07-28 10:41:03 +02:00
void MainWindow::showLogView()
{
if (mLogView == NULL)
mLogView = new LogView;
mLogView->show();
if (!mLogView->isActiveWindow())
mLogView->activateWindow();
}
2017-07-28 10:41:03 +02:00
void MainWindow::showStatistics()
{
StatsDialog statsDialog(this);
// Show a dialog with the previous scan statistics and project information
2011-10-13 20:53:06 +02:00
if (mProject) {
statsDialog.setProject(*mProject);
}
statsDialog.setPathSelected(mCurrentDirectory);
2017-07-28 12:39:28 +02:00
statsDialog.setNumberOfFilesScanned(mThread->getPreviousFilesCount());
statsDialog.setScanDuration(mThread->getPreviousScanDuration() / 1000.0);
2017-07-28 12:10:10 +02:00
statsDialog.setStatistics(mUI.mResults->getStatistics());
statsDialog.exec();
}
2017-07-28 10:41:03 +02:00
void MainWindow::showLibraryEditor()
2015-08-16 17:36:10 +02:00
{
2015-09-04 20:46:26 +02:00
LibraryDialog libraryDialog(this);
libraryDialog.exec();
2015-08-16 17:36:10 +02:00
}
2017-07-28 10:41:03 +02:00
void MainWindow::log(const QString &logline)
{
2011-10-13 20:53:06 +02:00
if (mLogView) {
2017-07-28 11:37:17 +02:00
mLogView->appendLine(logline);
}
}
2017-07-28 10:41:03 +02:00
void MainWindow::debugError(const ErrorItem &item)
{
2011-10-13 20:53:06 +02:00
if (mLogView) {
2017-07-28 11:37:17 +02:00
mLogView->appendLine(item.ToString());
}
}
2017-07-28 10:41:03 +02:00
void MainWindow::filterResults()
{
2017-07-28 12:10:10 +02:00
mUI.mResults->filterResults(mLineEditFilter->text());
}
2017-07-28 05:44:15 +02:00
void MainWindow::enableProjectActions(bool enable)
{
mUI.mActionCloseProjectFile->setEnabled(enable);
mUI.mActionEditProjectFile->setEnabled(enable);
}
2017-07-28 05:44:15 +02:00
void MainWindow::enableProjectOpenActions(bool enable)
{
mUI.mActionNewProjectFile->setEnabled(enable);
mUI.mActionOpenProjectFile->setEnabled(enable);
}
2017-07-28 10:41:03 +02:00
void MainWindow::openRecentProject()
{
QAction *action = qobject_cast<QAction *>(sender());
2011-10-13 20:53:06 +02:00
if (action) {
const QString project = action->data().toString();
QFileInfo inf(project);
2011-10-13 20:53:06 +02:00
if (inf.exists()) {
2017-07-28 05:44:15 +02:00
loadProjectFile(project);
2011-10-13 20:53:06 +02:00
} else {
2011-08-22 21:37:38 +02:00
const QString text(tr("The project file\n\n%1\n\n could not be found!\n\n"
2011-08-23 02:32:35 +02:00
"Do you want to remove the file from the recently "
"used projects -list?").arg(project));
QMessageBox msg(QMessageBox::Warning,
tr("Cppcheck"),
text,
QMessageBox::Yes | QMessageBox::No,
this);
msg.setDefaultButton(QMessageBox::No);
int rv = msg.exec();
2011-10-13 20:53:06 +02:00
if (rv == QMessageBox::Yes) {
2017-07-28 05:44:15 +02:00
removeProjectMRU(project);
}
}
}
}
2017-07-28 05:44:15 +02:00
void MainWindow::updateMRUMenuItems()
{
2011-10-13 20:53:06 +02:00
for (int i = 0; i < MaxRecentProjects + 1; i++) {
if (mRecentProjectActs[i] != NULL)
mUI.mMenuFile->removeAction(mRecentProjectActs[i]);
}
QStringList projects = mSettings->value(SETTINGS_MRU_PROJECTS).toStringList();
// Do a sanity check - remove duplicates and empty or space only items
int removed = projects.removeDuplicates();
for (int i = projects.size() - 1; i >= 0; i--) {
QString text = projects[i].trimmed();
if (text.isEmpty()) {
projects.removeAt(i);
removed++;
}
}
if (removed)
mSettings->setValue(SETTINGS_MRU_PROJECTS, projects);
const int numRecentProjects = qMin(projects.size(), (int)MaxRecentProjects);
2011-10-13 20:53:06 +02:00
for (int i = 0; i < numRecentProjects; i++) {
const QString filename = QFileInfo(projects[i]).fileName();
const QString text = QString("&%1 %2").arg(i + 1).arg(filename);
mRecentProjectActs[i]->setText(text);
mRecentProjectActs[i]->setData(projects[i]);
mRecentProjectActs[i]->setVisible(true);
mUI.mMenuFile->insertAction(mUI.mActionProjectMRU, mRecentProjectActs[i]);
}
if (numRecentProjects > 1)
mRecentProjectActs[numRecentProjects] = mUI.mMenuFile->insertSeparator(mUI.mActionProjectMRU);
}
2017-07-28 05:44:15 +02:00
void MainWindow::addProjectMRU(const QString &project)
{
QStringList files = mSettings->value(SETTINGS_MRU_PROJECTS).toStringList();
files.removeAll(project);
files.prepend(project);
while (files.size() > MaxRecentProjects)
files.removeLast();
mSettings->setValue(SETTINGS_MRU_PROJECTS, files);
2017-07-28 05:44:15 +02:00
updateMRUMenuItems();
}
2017-07-28 05:44:15 +02:00
void MainWindow::removeProjectMRU(const QString &project)
{
QStringList files = mSettings->value(SETTINGS_MRU_PROJECTS).toStringList();
files.removeAll(project);
mSettings->setValue(SETTINGS_MRU_PROJECTS, files);
2017-07-28 05:44:15 +02:00
updateMRUMenuItems();
}
2017-07-28 10:41:03 +02:00
void MainWindow::selectPlatform()
{
QAction *action = qobject_cast<QAction *>(sender());
2011-10-13 20:53:06 +02:00
if (action) {
const Settings::PlatformType platform = (Settings::PlatformType) action->data().toInt();
mSettings->setValue(SETTINGS_CHECKED_PLATFORM, platform);
}
}