2009-03-01 08:38:21 +01:00
/*
* Cppcheck - A tool for static C / C + + code analysis
2015-11-18 20:04:50 +01:00
* Copyright ( C ) 2007 - 2015 Cppcheck team .
2009-03-01 08:38:21 +01:00
*
* 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
2009-09-27 17:08:31 +02:00
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
2009-03-01 08:38:21 +01:00
*/
2009-06-20 22:23:54 +02:00
# include <QApplication>
2009-03-01 08:38:21 +01:00
# include <QDebug>
2009-05-24 10:53:29 +02:00
# include <QMessageBox>
2009-06-17 21:35:24 +02:00
# include <QFileInfo>
2010-01-06 00:40:26 +01:00
# include <QDir>
2011-03-26 15:06:11 +01:00
# include <QDesktopServices>
# include <QUrl>
2011-12-27 11:46:21 +01:00
# include <QAction>
# include <QActionGroup>
2014-01-11 08:38:26 +01:00
# include <QFile>
2011-03-26 15:06:11 +01:00
# include "mainwindow.h"
2012-10-07 12:25:34 +02:00
# include "cppcheck.h"
# include "applicationlist.h"
2009-06-04 14:35:41 +02:00
# include "aboutdialog.h"
2012-10-07 12:25:34 +02:00
# include "common.h"
2009-06-20 18:55:23 +02:00
# include "threadhandler.h"
2009-06-14 11:57:43 +02:00
# include "fileviewdialog.h"
2009-06-22 10:57:17 +02:00
# include "projectfile.h"
2010-07-12 19:21:45 +02:00
# include "project.h"
2009-07-06 11:30:49 +02:00
# include "report.h"
2012-10-20 20:14:52 +02:00
# include "scratchpad.h"
2010-09-01 08:12:24 +02:00
# include "statsdialog.h"
2012-10-07 12:25:34 +02:00
# include "settingsdialog.h"
2012-10-20 20:14:52 +02:00
# include "threadresult.h"
2012-10-07 12:25:34 +02:00
# include "translationhandler.h"
2010-07-16 16:48:13 +02:00
# include "logview.h"
2010-08-11 16:22:39 +02:00
# include "filelist.h"
2011-10-11 21:14:15 +02:00
# include "showtypes.h"
2015-09-04 20:46:26 +02:00
# include "librarydialog.h"
2011-03-26 15:06:11 +01:00
2011-03-26 19:37:28 +01:00
static const QString OnlineHelpURL ( " http://cppcheck.sourceforge.net/manual.html " ) ;
2009-03-01 08:38:21 +01:00
2013-03-01 19:11:27 +01:00
MainWindow : : MainWindow ( TranslationHandler * th , QSettings * settings ) :
mSettings ( settings ) ,
2010-04-15 20:08:51 +02:00
mApplications ( new ApplicationList ( this ) ) ,
2013-03-01 19:11:27 +01:00
mTranslation ( th ) ,
2010-07-17 22:52:50 +02:00
mLogView ( NULL ) ,
2012-10-20 20:14:52 +02:00
mScratchPad ( NULL ) ,
2010-08-24 23:56:45 +02:00
mProject ( NULL ) ,
2011-12-27 11:46:21 +01:00
mPlatformActions ( new QActionGroup ( this ) ) ,
2013-03-02 13:01:21 +01:00
mCStandardActions ( new QActionGroup ( this ) ) ,
mCppStandardActions ( new QActionGroup ( this ) ) ,
2015-10-14 14:31:52 +02:00
mSelectLanguageActions ( new QActionGroup ( this ) ) ,
2010-08-24 23:56:45 +02:00
mExiting ( false )
2009-07-02 10:32:29 +02:00
{
mUI . setupUi ( this ) ;
2009-06-20 18:55:23 +02:00
mThread = new ThreadHandler ( this ) ;
2015-12-23 10:28:07 +01:00
mUI . mResults - > Initialize ( mSettings , mApplications , mThread ) ;
2009-05-24 10:53:29 +02:00
2011-05-04 07:30:54 +02:00
// Filter timer to delay filtering results slightly while typing
mFilterTimer = new QTimer ( this ) ;
mFilterTimer - > setInterval ( 500 ) ;
mFilterTimer - > setSingleShot ( true ) ;
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 ( ) ) ) ;
connect ( mLineEditFilter , SIGNAL ( returnPressed ( ) ) , this , SLOT ( FilterResults ( ) ) ) ;
2015-04-18 17:37:13 +02:00
connect ( mUI . mActionPrint , SIGNAL ( triggered ( ) ) , mUI . mResults , SLOT ( Print ( ) ) ) ;
connect ( mUI . mActionPrintPreview , SIGNAL ( triggered ( ) ) , mUI . mResults , SLOT ( PrintPreview ( ) ) ) ;
2009-07-02 10:32:29 +02:00
connect ( mUI . mActionQuit , SIGNAL ( triggered ( ) ) , this , SLOT ( close ( ) ) ) ;
connect ( mUI . mActionCheckFiles , SIGNAL ( triggered ( ) ) , this , SLOT ( CheckFiles ( ) ) ) ;
connect ( mUI . mActionCheckDirectory , SIGNAL ( triggered ( ) ) , this , SLOT ( CheckDirectory ( ) ) ) ;
connect ( mUI . mActionSettings , SIGNAL ( triggered ( ) ) , this , SLOT ( ProgramSettings ( ) ) ) ;
connect ( mUI . mActionClearResults , SIGNAL ( triggered ( ) ) , this , SLOT ( ClearResults ( ) ) ) ;
2013-02-10 07:48:09 +01:00
connect ( mUI . mActionOpenXML , SIGNAL ( triggered ( ) ) , this , SLOT ( OpenResults ( ) ) ) ;
2009-07-02 10:32:29 +02:00
connect ( mUI . mActionShowStyle , SIGNAL ( toggled ( bool ) ) , this , SLOT ( ShowStyle ( bool ) ) ) ;
connect ( mUI . mActionShowErrors , SIGNAL ( toggled ( bool ) ) , this , SLOT ( ShowErrors ( bool ) ) ) ;
2010-10-18 19:50:34 +02:00
connect ( mUI . mActionShowWarnings , SIGNAL ( toggled ( bool ) ) , this , SLOT ( ShowWarnings ( bool ) ) ) ;
2010-12-27 10:06:31 +01:00
connect ( mUI . mActionShowPortability , SIGNAL ( toggled ( bool ) ) , this , SLOT ( ShowPortability ( bool ) ) ) ;
2010-10-18 19:50:34 +02:00
connect ( mUI . mActionShowPerformance , SIGNAL ( toggled ( bool ) ) , this , SLOT ( ShowPerformance ( bool ) ) ) ;
2010-12-26 13:36:24 +01:00
connect ( mUI . mActionShowInformation , SIGNAL ( toggled ( bool ) ) , this , SLOT ( ShowInformation ( bool ) ) ) ;
2009-07-02 10:32:29 +02:00
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 ( ) ) ) ;
2010-11-21 19:55:34 +01:00
connect ( mUI . mActionShowHidden , SIGNAL ( triggered ( ) ) , mUI . mResults , SLOT ( ShowHiddenResults ( ) ) ) ;
2010-07-16 16:48:13 +02:00
connect ( mUI . mActionViewLog , SIGNAL ( triggered ( ) ) , this , SLOT ( ShowLogView ( ) ) ) ;
2010-09-01 08:12:24 +02:00
connect ( mUI . mActionViewStats , SIGNAL ( triggered ( ) ) , this , SLOT ( ShowStatistics ( ) ) ) ;
2015-08-16 17:36:10 +02:00
connect ( mUI . mActionLibraryEditor , SIGNAL ( triggered ( ) ) , this , SLOT ( ShowLibraryEditor ( ) ) ) ;
2009-03-22 18:39:44 +01:00
2015-10-14 19:07:20 +02:00
connect ( mUI . mActionRecheckModified , SIGNAL ( triggered ( ) ) , this , SLOT ( ReCheckModified ( ) ) ) ;
connect ( mUI . mActionRecheckAll , SIGNAL ( triggered ( ) ) , this , SLOT ( ReCheckAll ( ) ) ) ;
2009-05-24 10:53:29 +02:00
2009-07-02 19:23:44 +02:00
connect ( mUI . mActionStop , SIGNAL ( triggered ( ) ) , this , SLOT ( StopChecking ( ) ) ) ;
2009-07-02 10:32:29 +02:00
connect ( mUI . mActionSave , SIGNAL ( triggered ( ) ) , this , SLOT ( Save ( ) ) ) ;
2009-05-24 10:53:29 +02:00
2011-05-04 07:30:54 +02:00
// About menu
2009-07-02 10:32:29 +02:00
connect ( mUI . mActionAbout , SIGNAL ( triggered ( ) ) , this , SLOT ( About ( ) ) ) ;
connect ( mUI . mActionLicense , SIGNAL ( triggered ( ) ) , this , SLOT ( ShowLicense ( ) ) ) ;
2011-05-04 07:30:54 +02:00
// View > Toolbar menu
2010-07-03 19:03:53 +02:00
connect ( mUI . mActionToolBarMain , SIGNAL ( toggled ( bool ) ) , this , SLOT ( ToggleMainToolBar ( ) ) ) ;
2010-07-03 18:37:09 +02:00
connect ( mUI . mActionToolBarView , SIGNAL ( toggled ( bool ) ) , this , SLOT ( ToggleViewToolBar ( ) ) ) ;
2011-05-04 07:30:54 +02:00
connect ( mUI . mActionToolBarFilter , SIGNAL ( toggled ( bool ) ) , this , SLOT ( ToggleFilterToolBar ( ) ) ) ;
2009-06-14 23:48:16 +02:00
2009-07-02 10:46:26 +02:00
connect ( mUI . mActionAuthors , SIGNAL ( triggered ( ) ) , this , SLOT ( ShowAuthors ( ) ) ) ;
2009-06-20 18:55:23 +02:00
connect ( mThread , SIGNAL ( Done ( ) ) , this , SLOT ( CheckDone ( ) ) ) ;
2009-07-02 10:32:29 +02:00
connect ( mUI . mResults , SIGNAL ( GotResults ( ) ) , this , SLOT ( ResultsAdded ( ) ) ) ;
2010-11-24 09:48:07 +01:00
connect ( mUI . mResults , SIGNAL ( ResultsHidden ( bool ) ) , mUI . mActionShowHidden , SLOT ( setEnabled ( bool ) ) ) ;
2015-12-23 10:28:07 +01:00
connect ( mUI . mResults , SIGNAL ( CheckSelected ( QStringList ) ) , this , SLOT ( PerformSelectedFilesCheck ( QStringList ) ) ) ;
2009-07-02 19:11:47 +02:00
connect ( mUI . mMenuView , SIGNAL ( aboutToShow ( ) ) , this , SLOT ( AboutToShowViewMenu ( ) ) ) ;
2009-05-26 17:21:39 +02:00
2011-05-04 07:30:54 +02:00
// File menu
2010-07-13 11:33:24 +02:00
connect ( mUI . mActionNewProjectFile , SIGNAL ( triggered ( ) ) , this , SLOT ( NewProjectFile ( ) ) ) ;
connect ( mUI . mActionOpenProjectFile , SIGNAL ( triggered ( ) ) , this , SLOT ( OpenProjectFile ( ) ) ) ;
2012-10-20 20:14:52 +02:00
connect ( mUI . mActionShowScratchpad , SIGNAL ( triggered ( ) ) , this , SLOT ( ShowScratchpad ( ) ) ) ;
2010-08-15 11:12:28 +02:00
connect ( mUI . mActionCloseProjectFile , SIGNAL ( triggered ( ) ) , this , SLOT ( CloseProjectFile ( ) ) ) ;
2010-08-15 15:12:32 +02:00
connect ( mUI . mActionEditProjectFile , SIGNAL ( triggered ( ) ) , this , SLOT ( EditProjectFile ( ) ) ) ;
2010-07-07 20:18:42 +02:00
2010-02-23 16:29:49 +01:00
connect ( mUI . mActionHelpContents , SIGNAL ( triggered ( ) ) , this , SLOT ( OpenHelpContents ( ) ) ) ;
2009-03-01 08:38:21 +01:00
LoadSettings ( ) ;
2009-07-02 10:32:29 +02:00
mThread - > Initialize ( mUI . mResults ) ;
2009-06-22 17:45:48 +02:00
FormatAndSetTitle ( ) ;
2009-05-26 17:21:39 +02:00
EnableCheckButtons ( true ) ;
2009-06-03 20:18:22 +02:00
2015-04-17 16:33:52 +02:00
mUI . mActionPrint - > setShortcut ( QKeySequence : : Print ) ;
mUI . mActionPrint - > setEnabled ( false ) ;
mUI . mActionPrintPreview - > setEnabled ( false ) ;
2009-07-02 10:32:29 +02:00
mUI . mActionClearResults - > setEnabled ( false ) ;
mUI . mActionSave - > setEnabled ( false ) ;
2015-10-14 19:07:20 +02:00
mUI . mActionRecheckModified - > setEnabled ( false ) ;
mUI . mActionRecheckAll - > setEnabled ( false ) ;
2010-08-25 19:59:30 +02:00
EnableProjectOpenActions ( true ) ;
2010-08-25 17:54:45 +02:00
EnableProjectActions ( false ) ;
2009-07-02 10:32:29 +02:00
2012-01-10 20:16:47 +01:00
// 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 ) {
2011-05-12 11:34:23 +02:00
mRecentProjectActs [ i ] = new QAction ( this ) ;
mRecentProjectActs [ i ] - > setVisible ( false ) ;
connect ( mRecentProjectActs [ i ] , SIGNAL ( triggered ( ) ) ,
this , SLOT ( OpenRecentProject ( ) ) ) ;
}
2011-08-24 22:14:28 +02:00
mRecentProjectActs [ MaxRecentProjects ] = NULL ; // The separator
2011-05-12 11:34:23 +02:00
mUI . mActionProjectMRU - > setVisible ( false ) ;
UpdateMRUMenuItems ( ) ;
2011-10-02 21:08:12 +02:00
2012-01-10 20:16:47 +01:00
QStringList args = QCoreApplication : : arguments ( ) ;
//Remove the application itself
args . removeFirst ( ) ;
if ( ! args . isEmpty ( ) ) {
HandleCLIParams ( args ) ;
}
2011-10-13 20:53:06 +02:00
for ( int i = 0 ; i < mPlatforms . getCount ( ) ; i + + ) {
2011-10-02 21:08:12 +02:00
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 ) ;
2011-12-27 11:46:21 +01:00
act - > setActionGroup ( mPlatformActions ) ;
2011-10-02 21:08:12 +02:00
mUI . mMenuCheck - > insertAction ( mUI . mActionPlatforms , act ) ;
connect ( act , SIGNAL ( triggered ( ) ) , this , SLOT ( SelectPlatform ( ) ) ) ;
}
2013-03-02 13:01:21 +01:00
mUI . mActionC89 - > setActionGroup ( mCStandardActions ) ;
mUI . mActionC99 - > setActionGroup ( mCStandardActions ) ;
mUI . mActionC11 - > setActionGroup ( mCStandardActions ) ;
mUI . mActionCpp03 - > setActionGroup ( mCppStandardActions ) ;
mUI . mActionCpp11 - > setActionGroup ( mCppStandardActions ) ;
2015-10-14 14:31:52 +02:00
mUI . mActionEnforceC - > setActionGroup ( mSelectLanguageActions ) ;
mUI . mActionEnforceCpp - > setActionGroup ( mSelectLanguageActions ) ;
mUI . mActionAutoDetectLanguage - > setActionGroup ( mSelectLanguageActions ) ;
2011-10-02 22:45:11 +02:00
// 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)
2014-06-16 06:30:42 +02:00
const Settings : : PlatformType defaultPlat = Settings : : Win32W ;
2011-10-02 22:45:11 +02:00
# else
2014-06-14 18:47:19 +02:00
const Settings : : PlatformType defaultPlat = Settings : : Unspecified ;
2011-10-02 22:45:11 +02:00
# endif
2014-06-14 18:47:19 +02:00
Platform & plat = mPlatforms . get ( ( Settings : : PlatformType ) mSettings - > value ( SETTINGS_CHECKED_PLATFORM , defaultPlat ) . toInt ( ) ) ;
2011-10-02 21:08:12 +02:00
plat . mActMainWindow - > setChecked ( true ) ;
2009-03-01 08:38:21 +01:00
}
MainWindow : : ~ MainWindow ( )
{
2010-07-16 16:48:13 +02:00
delete mLogView ;
2010-08-15 10:35:46 +02:00
delete mProject ;
2012-10-20 20:14:52 +02:00
delete mScratchPad ;
2009-03-01 08:38:21 +01:00
}
2011-04-07 14:34:15 +02:00
void MainWindow : : HandleCLIParams ( const QStringList & params )
{
2015-12-26 11:47:07 +01:00
int index ;
2011-10-13 20:53:06 +02:00
if ( params . contains ( " -p " ) ) {
2015-12-26 11:47:07 +01:00
index = params . indexOf ( " -p " ) ;
if ( ( index + 1 ) < params . length ( ) )
LoadProjectFile ( params [ index + 1 ] ) ;
} else if ( ( index = params . indexOf ( QRegExp ( " .* \\ .cppcheck$ " , Qt : : CaseInsensitive ) , 0 ) ) > = 0 & & index < params . length ( ) & & QFile ( params [ index ] ) . exists ( ) ) {
LoadProjectFile ( params [ index ] ) ;
} else if ( ( index = params . indexOf ( QRegExp ( " .* \\ .xml$ " , Qt : : CaseInsensitive ) , 0 ) ) > = 0 & & index < params . length ( ) & & QFile ( params [ index ] ) . exists ( ) ) {
LoadResults ( params [ index ] ) ;
2013-02-10 07:48:09 +01:00
} else if ( params . contains ( " -l " ) ) {
QString logFile ;
2015-12-26 11:47:07 +01:00
index = params . indexOf ( " -l " ) ;
if ( ( index + 1 ) < params . length ( ) )
logFile = params [ index + 1 ] ;
2013-02-10 07:48:09 +01:00
if ( params . contains ( " -d " ) ) {
QString checkedDir ;
2015-12-26 11:47:07 +01:00
index = params . indexOf ( " -d " ) ;
if ( ( index + 1 ) < params . length ( ) )
checkedDir = params [ index + 1 ] ;
2013-02-10 07:48:09 +01:00
LoadResults ( logFile , checkedDir ) ;
2013-02-10 23:53:01 +01:00
} else {
2013-02-10 07:48:09 +01:00
LoadResults ( logFile ) ;
}
2011-10-13 20:53:06 +02:00
} else
2011-04-07 14:34:15 +02:00
DoCheckFiles ( params ) ;
}
2009-03-01 08:38:21 +01:00
void MainWindow : : LoadSettings ( )
{
2011-05-04 07:30:54 +02:00
// Window/dialog sizes
2011-10-13 20:53:06 +02:00
if ( mSettings - > value ( SETTINGS_WINDOW_MAXIMIZED , false ) . toBool ( ) ) {
2009-03-01 08:38:21 +01:00
showMaximized ( ) ;
2011-10-13 20:53:06 +02:00
} else {
2009-07-02 10:32:29 +02:00
resize ( mSettings - > value ( SETTINGS_WINDOW_WIDTH , 800 ) . toInt ( ) ,
mSettings - > value ( SETTINGS_WINDOW_HEIGHT , 600 ) . toInt ( ) ) ;
2009-03-01 08:38:21 +01:00
}
2009-03-22 18:39:44 +01:00
2011-10-11 21:14:15 +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 ) ) ;
2009-07-01 23:05:09 +02:00
2013-03-02 13:01:21 +01:00
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 ) ) ;
2011-12-27 21:12:52 +01:00
const bool stdPosix = mSettings - > value ( SETTINGS_STD_POSIX , false ) . toBool ( ) ;
mUI . mActionPosix - > setChecked ( stdPosix ) ;
2011-05-04 07:30:54 +02:00
// 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 ) ;
2009-07-02 10:32:29 +02:00
2015-10-14 14:31:52 +02:00
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 ) ;
2011-06-16 15:03:25 +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. " ) ;
2011-04-01 23:53:26 +02:00
QMessageBox msgBox ( QMessageBox : : Warning ,
tr ( " Cppcheck " ) ,
msg ,
QMessageBox : : Ok ,
this ) ;
msgBox . exec ( ) ;
}
2009-03-01 08:38:21 +01:00
}
2012-10-27 12:26:51 +02:00
void MainWindow : : SaveSettings ( ) const
2009-03-01 08:38:21 +01:00
{
2011-05-04 07:30:54 +02:00
// Window/dialog sizes
2009-07-02 10:32:29 +02:00
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
2011-05-04 07:30:54 +02:00
// Show * states
2009-07-02 10:32:29 +02:00
mSettings - > setValue ( SETTINGS_SHOW_STYLE , mUI . mActionShowStyle - > isChecked ( ) ) ;
mSettings - > setValue ( SETTINGS_SHOW_ERRORS , mUI . mActionShowErrors - > isChecked ( ) ) ;
2010-10-18 19:50:34 +02:00
mSettings - > setValue ( SETTINGS_SHOW_WARNINGS , mUI . mActionShowWarnings - > isChecked ( ) ) ;
2010-12-27 10:06:31 +01:00
mSettings - > setValue ( SETTINGS_SHOW_PORTABILITY , mUI . mActionShowPortability - > isChecked ( ) ) ;
2010-10-18 19:50:34 +02:00
mSettings - > setValue ( SETTINGS_SHOW_PERFORMANCE , mUI . mActionShowPerformance - > isChecked ( ) ) ;
2010-12-26 13:36:24 +01:00
mSettings - > setValue ( SETTINGS_SHOW_INFORMATION , mUI . mActionShowInformation - > isChecked ( ) ) ;
2011-05-04 07:30:54 +02:00
2013-03-02 13:01:21 +01:00
mSettings - > setValue ( SETTINGS_STD_CPP03 , mUI . mActionCpp03 - > isChecked ( ) ) ;
mSettings - > setValue ( SETTINGS_STD_CPP11 , mUI . mActionCpp11 - > isChecked ( ) ) ;
mSettings - > setValue ( SETTINGS_STD_C89 , mUI . mActionC89 - > isChecked ( ) ) ;
2011-12-27 21:12:52 +01:00
mSettings - > setValue ( SETTINGS_STD_C99 , mUI . mActionC99 - > isChecked ( ) ) ;
2013-03-02 13:01:21 +01:00
mSettings - > setValue ( SETTINGS_STD_C11 , mUI . mActionC11 - > isChecked ( ) ) ;
2011-12-27 21:12:52 +01:00
mSettings - > setValue ( SETTINGS_STD_POSIX , mUI . mActionPosix - > isChecked ( ) ) ;
2011-05-04 07:30:54 +02:00
// Main window settings
2010-07-03 19:03:53 +02:00
mSettings - > setValue ( SETTINGS_TOOLBARS_MAIN_SHOW , mUI . mToolBarMain - > isVisible ( ) ) ;
2010-07-03 18:37:09 +02:00
mSettings - > setValue ( SETTINGS_TOOLBARS_VIEW_SHOW , mUI . mToolBarView - > isVisible ( ) ) ;
2011-05-04 07:30:54 +02:00
mSettings - > setValue ( SETTINGS_TOOLBARS_FILTER_SHOW , mUI . mToolBarFilter - > isVisible ( ) ) ;
2009-07-01 23:05:09 +02:00
2015-10-14 14:31:52 +02:00
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 ) ;
2011-06-16 15:03:25 +02:00
mApplications - > SaveSettings ( ) ;
2009-07-02 10:32:29 +02:00
mSettings - > setValue ( SETTINGS_LANGUAGE , mTranslation - > GetCurrentLanguage ( ) ) ;
2010-11-23 20:57:16 +01:00
mUI . mResults - > SaveSettings ( mSettings ) ;
2009-03-01 08:38:21 +01:00
}
2009-07-16 07:20:29 +02:00
void MainWindow : : DoCheckFiles ( const QStringList & files )
{
2011-10-13 20:53:06 +02:00
if ( files . isEmpty ( ) ) {
2009-07-16 07:20:29 +02:00
return ;
}
ClearResults ( ) ;
2010-08-09 21:15:05 +02:00
FileList pathList ;
pathList . AddPathList ( files ) ;
2011-02-27 23:13:28 +01:00
if ( mProject )
2011-08-23 18:54:53 +02:00
pathList . AddExcludeList ( mProject - > GetProjectFile ( ) - > GetExcludedPaths ( ) ) ;
2010-08-09 21:15:05 +02:00
QStringList fileNames = pathList . GetFileList ( ) ;
2009-07-16 07:20:29 +02:00
2012-09-05 21:01:50 +02:00
mUI . mResults - > Clear ( true ) ;
2009-07-16 07:20:29 +02:00
mThread - > ClearFiles ( ) ;
2011-10-13 20:53:06 +02:00
if ( fileNames . isEmpty ( ) ) {
2009-07-16 07:20:29 +02:00
QMessageBox msg ( QMessageBox : : Warning ,
tr ( " Cppcheck " ) ,
tr ( " No suitable files found to check! " ) ,
QMessageBox : : Ok ,
this ) ;
msg . exec ( ) ;
return ;
}
2010-07-04 17:54:41 +02:00
mUI . mResults - > CheckingStarted ( fileNames . count ( ) ) ;
2009-07-16 07:20:29 +02:00
2010-08-09 21:15:05 +02:00
mThread - > SetFiles ( fileNames ) ;
2013-02-10 08:15:32 +01:00
QDir inf ( mCurrentDirectory ) ;
const QString checkPath = inf . canonicalPath ( ) ;
2013-02-17 19:59:16 +01:00
SetPath ( SETTINGS_LAST_CHECK_PATH , checkPath ) ;
2009-07-16 07:20:29 +02:00
2011-02-11 20:08:37 +01:00
CheckLockDownUI ( ) ; // lock UI while checking
mUI . mResults - > SetCheckDirectory ( checkPath ) ;
2009-07-16 07:20:29 +02:00
Settings checkSettings = GetCppcheckSettings ( ) ;
2011-06-04 14:38:51 +02:00
2011-06-07 00:30:44 +02:00
if ( mProject )
qDebug ( ) < < " Checking project file " < < mProject - > GetProjectFile ( ) - > GetFilename ( ) ;
2011-06-04 14:38:51 +02:00
2015-12-23 10:28:07 +01:00
mThread - > SetCheckFiles ( true ) ;
mThread - > Check ( checkSettings , true ) ;
2009-07-16 07:20:29 +02:00
}
2012-10-20 20:14:52 +02:00
void MainWindow : : CheckCode ( const QString & code , const QString & filename )
{
// Initialize dummy ThreadResult as ErrorLogger
ThreadResult result ;
result . SetFiles ( QStringList ( filename ) ) ;
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 ) ;
cppcheck . settings ( ) = GetCppcheckSettings ( ) ;
// Check
CheckLockDownUI ( ) ;
ClearResults ( ) ;
mUI . mResults - > CheckingStarted ( 1 ) ;
cppcheck . check ( filename . toStdString ( ) , code . toStdString ( ) ) ;
CheckDone ( ) ;
}
2009-07-16 07:20:29 +02:00
QStringList MainWindow : : SelectFilesToCheck ( QFileDialog : : FileMode mode )
2009-03-01 08:38:21 +01:00
{
2011-10-13 20:53:06 +02:00
if ( mProject ) {
2010-08-19 21:28:56 +02:00
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 ( ) ;
}
2009-06-17 21:35:24 +02:00
QStringList selected ;
2009-03-22 13:32:07 +01:00
2009-06-17 21:35:24 +02:00
// 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 ) {
2015-10-18 17:19:05 +02:00
2013-01-04 10:23:16 +01:00
selected = QFileDialog : : getOpenFileNames ( this ,
tr ( " Select files to check " ) ,
2015-10-18 17:19:05 +02:00
GetPath ( SETTINGS_LAST_CHECK_PATH ) ,
tr ( " C/C++ Source (%1) " ) . arg ( FileList : : GetDefaultFilters ( ) . join ( " " ) ) ) ;
2010-04-02 07:30:58 +02:00
if ( selected . isEmpty ( ) )
2009-06-22 10:57:17 +02:00
mCurrentDirectory . clear ( ) ;
2011-10-13 20:53:06 +02:00
else {
2010-01-06 10:34:02 +01:00
QFileInfo inf ( selected [ 0 ] ) ;
mCurrentDirectory = inf . absolutePath ( ) ;
}
2009-06-22 17:45:48 +02:00
FormatAndSetTitle ( ) ;
2011-10-13 20:53:06 +02:00
} else if ( mode = = QFileDialog : : DirectoryOnly ) {
2013-09-07 19:38:40 +02:00
QString dir = QFileDialog : : getExistingDirectory ( this ,
tr ( " Select directory to check " ) ,
GetPath ( SETTINGS_LAST_CHECK_PATH ) ) ;
if ( ! dir . isEmpty ( ) ) {
qDebug ( ) < < " Setting current directory to: " < < dir ;
2009-06-22 10:57:17 +02:00
mCurrentDirectory = dir ;
2009-06-17 21:35:24 +02:00
selected . append ( dir ) ;
2009-08-02 13:08:04 +02:00
dir = QDir : : toNativeSeparators ( dir ) ;
2009-06-22 17:45:48 +02:00
FormatAndSetTitle ( dir ) ;
2009-06-22 10:57:17 +02:00
}
2009-06-17 21:35:24 +02:00
}
2013-02-19 21:39:50 +01:00
2013-02-08 16:18:05 +01:00
SetPath ( SETTINGS_LAST_CHECK_PATH , mCurrentDirectory ) ;
2013-02-19 21:39:50 +01:00
2009-07-16 07:20:29 +02:00
return selected ;
2009-03-01 08:38:21 +01:00
}
2009-03-22 13:32:07 +01:00
void MainWindow : : CheckFiles ( )
{
2009-07-16 07:20:29 +02:00
DoCheckFiles ( SelectFilesToCheck ( QFileDialog : : ExistingFiles ) ) ;
2009-03-22 13:32:07 +01:00
}
void MainWindow : : CheckDirectory ( )
{
2011-06-07 15:12:21 +02:00
QStringList dir = SelectFilesToCheck ( QFileDialog : : DirectoryOnly ) ;
if ( dir . isEmpty ( ) )
return ;
QDir checkDir ( dir [ 0 ] ) ;
QStringList filters ;
filters < < " *.cppcheck " ;
2011-06-07 22:56:48 +02:00
checkDir . setFilter ( QDir : : Files | QDir : : Readable ) ;
2011-06-07 15:12:21 +02:00
checkDir . setNameFilters ( filters ) ;
QStringList projFiles = checkDir . entryList ( ) ;
2011-10-13 20:53:06 +02:00
if ( ! projFiles . empty ( ) ) {
if ( projFiles . size ( ) = = 1 ) {
2011-06-07 15:12:21 +02:00
// If one project file found, suggest loading it
QMessageBox msgBox ( this ) ;
msgBox . setWindowTitle ( tr ( " Cppcheck " ) ) ;
const QString msg ( tr ( " Found project file: %1 \n \n Do 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 ) {
2011-06-08 09:40:17 +02:00
QString path = checkDir . canonicalPath ( ) ;
if ( ! path . endsWith ( " / " ) )
path + = " / " ;
path + = projFiles [ 0 ] ;
LoadProjectFile ( path ) ;
2011-10-13 20:53:06 +02:00
} else {
2011-06-07 15:12:21 +02:00
DoCheckFiles ( dir ) ;
}
2011-10-13 20:53:06 +02:00
} else {
2011-06-07 15:12:21 +02:00
// 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 ) {
2011-06-07 15:12:21 +02:00
DoCheckFiles ( dir ) ;
}
}
2011-10-13 20:53:06 +02:00
} else {
2011-06-07 15:12:21 +02:00
DoCheckFiles ( dir ) ;
}
2009-03-22 13:32:07 +01:00
}
2010-11-26 21:51:38 +01:00
void MainWindow : : AddIncludeDirs ( const QStringList & includeDirs , Settings & result )
{
QString dir ;
2015-12-17 15:40:54 +01:00
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 ( ) ) ;
}
}
2014-05-26 20:16:19 +02:00
Library : : Error MainWindow : : LoadLibrary ( Library * library , QString filename )
2013-12-31 10:46:44 +01:00
{
2014-05-26 20:16:19 +02:00
Library : : Error ret ;
2013-12-31 10:46:44 +01:00
// Try to load the library from the project folder..
if ( mProject ) {
QString path = QFileInfo ( mProject - > GetProjectFile ( ) - > GetFilename ( ) ) . canonicalPath ( ) ;
2014-05-26 20:16:19 +02:00
ret = library - > load ( NULL , ( path + " / " + filename ) . toLatin1 ( ) ) ;
if ( ret . errorcode ! = Library : : ErrorCode : : FILE_NOT_FOUND )
return ret ;
2013-12-31 10:46:44 +01:00
}
// Try to load the library from the application folder..
2014-03-19 19:34:20 +01:00
const QString appPath = QFileInfo ( QCoreApplication : : applicationFilePath ( ) ) . canonicalPath ( ) ;
2014-05-26 20:16:19 +02:00
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 ;
2013-12-31 10:46:44 +01:00
2014-03-19 19:34:20 +01:00
// Try to load the library from the cfg subfolder..
const QString datadir = mSettings - > value ( " DATADIR " , QString ( ) ) . toString ( ) ;
if ( ! datadir . isEmpty ( ) ) {
2014-05-26 20:16:19 +02:00
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 ;
2014-01-11 08:38:26 +01:00
}
2014-05-26 20:16:19 +02:00
return ret ;
2013-12-31 10:46:44 +01:00
}
2015-01-10 22:51:45 +01:00
bool MainWindow : : TryLoadLibrary ( Library * library , QString filename )
{
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 ;
2015-01-10 22:51:45 +01:00
}
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 ;
}
2010-08-24 23:47:05 +02:00
Settings MainWindow : : GetCppcheckSettings ( )
{
2015-10-14 14:31:52 +02:00
SaveSettings ( ) ; // Save settings
2010-08-24 23:47:05 +02:00
Settings result ;
2010-11-26 21:35:45 +01:00
2011-05-12 12:53:59 +02:00
// If project file loaded, read settings from it
2011-10-13 20:53:06 +02:00
if ( mProject ) {
2010-08-24 23:47:05 +02:00
ProjectFile * pfile = mProject - > GetProjectFile ( ) ;
2010-08-15 10:35:46 +02:00
QStringList dirs = pfile - > GetIncludeDirs ( ) ;
2010-11-26 21:51:38 +01:00
AddIncludeDirs ( dirs , result ) ;
2015-01-06 18:10:19 +01:00
const QStringList defines = pfile - > GetDefines ( ) ;
2010-08-21 21:59:55 +02:00
QString define ;
2015-12-17 15:40:54 +01:00
foreach ( define , defines ) {
2010-08-21 21:59:55 +02:00
if ( ! result . userDefines . empty ( ) )
result . userDefines + = " ; " ;
result . userDefines + = define . toStdString ( ) ;
2009-06-22 10:57:17 +02:00
}
2013-06-18 14:14:33 +02:00
2015-01-06 18:10:19 +01:00
const QStringList libraries = pfile - > GetLibraries ( ) ;
2015-12-17 15:40:54 +01:00
foreach ( QString library , libraries ) {
2013-12-30 00:23:42 +01:00
const QString filename = library + " .cfg " ;
2015-01-10 22:51:45 +01:00
TryLoadLibrary ( & result . library , filename ) ;
2013-12-29 18:06:31 +01:00
}
2015-01-06 18:10:19 +01:00
const QStringList suppressions = pfile - > GetSuppressions ( ) ;
2015-12-17 15:40:54 +01:00
foreach ( QString suppression , suppressions ) {
2013-12-30 22:32:50 +01:00
result . nomsg . addSuppressionLine ( suppression . toStdString ( ) ) ;
}
2013-06-18 14:14:33 +02:00
// Only check the given -D configuration
if ( ! defines . isEmpty ( ) )
result . _maxConfigs = 1 ;
2009-06-22 10:57:17 +02:00
}
2011-08-26 09:34:01 +02:00
// 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 ( ) ) {
2011-08-26 09:34:01 +02:00
QStringList includes = globalIncludes . split ( " ; " ) ;
AddIncludeDirs ( includes , result ) ;
}
2013-02-10 23:19:07 +01:00
result . addEnabled ( " warning " ) ;
2010-09-06 22:28:41 +02:00
result . addEnabled ( " style " ) ;
2011-09-04 20:26:31 +02:00
result . addEnabled ( " performance " ) ;
result . addEnabled ( " portability " ) ;
2011-01-05 22:33:53 +01:00
result . addEnabled ( " information " ) ;
2011-01-26 22:44:15 +01:00
result . addEnabled ( " missingInclude " ) ;
2010-08-28 08:16:01 +02:00
result . debug = false ;
2010-12-26 17:37:11 +01:00
result . debugwarnings = mSettings - > value ( SETTINGS_SHOW_DEBUG_WARNINGS , false ) . toBool ( ) ;
2015-07-25 19:17:24 +02:00
result . quiet = false ;
2009-03-22 13:32:07 +01:00
result . _verbose = true ;
2009-07-02 10:32:29 +02:00
result . _force = mSettings - > value ( SETTINGS_CHECK_FORCE , 1 ) . toBool ( ) ;
2009-03-22 13:32:07 +01:00
result . _xml = false ;
2009-07-02 10:32:29 +02:00
result . _jobs = mSettings - > value ( SETTINGS_CHECK_THREADS , 1 ) . toInt ( ) ;
2010-12-26 17:29:43 +01:00
result . _inlineSuppressions = mSettings - > value ( SETTINGS_INLINE_SUPPRESSIONS , false ) . toBool ( ) ;
2011-04-16 17:42:32 +02:00
result . inconclusive = mSettings - > value ( SETTINGS_INCONCLUSIVE_ERRORS , false ) . toBool ( ) ;
2011-09-29 21:46:19 +02:00
result . platformType = ( Settings : : PlatformType ) mSettings - > value ( SETTINGS_CHECKED_PLATFORM , 0 ) . toInt ( ) ;
2013-03-02 13:01:21 +01:00
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 ) ;
2011-12-27 21:12:52 +01:00
result . standards . posix = mSettings - > value ( SETTINGS_STD_POSIX , false ) . toBool ( ) ;
2015-10-14 14:31:52 +02:00
result . enforcedLang = ( Settings : : Language ) mSettings - > value ( SETTINGS_ENFORCED_LANGUAGE , 0 ) . toInt ( ) ;
2009-05-24 10:53:29 +02:00
2015-01-10 22:51:45 +01:00
const bool std = TryLoadLibrary ( & result . library , " std.cfg " ) ;
2013-12-26 11:15:28 +01:00
bool posix = true ;
2013-12-23 10:06:45 +01:00
if ( result . standards . posix )
2015-01-10 22:51:45 +01:00
posix = TryLoadLibrary ( & result . library , " posix.cfg " ) ;
2014-10-19 07:34:40 +02:00
bool windows = true ;
if ( result . platformType = = Settings : : Win32A | | result . platformType = = Settings : : Win32W | | result . platformType = = Settings : : Win64 )
2015-01-10 22:51:45 +01:00
windows = TryLoadLibrary ( & result . library , " windows.cfg " ) ;
2013-12-26 11:15:28 +01:00
2014-10-19 07:34:40 +02:00
if ( ! std | | ! posix | | ! windows )
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. " ) . arg ( ! std ? " std.cfg " : ! posix ? " posix.cfg " : " windows.cfg " ) ) ;
2013-12-23 10:06:45 +01:00
2013-02-04 20:38:29 +01:00
if ( result . _jobs < = 1 ) {
2009-05-24 10:53:29 +02:00
result . _jobs = 1 ;
}
2009-03-22 13:32:07 +01:00
return result ;
}
2009-03-01 08:38:21 +01:00
void MainWindow : : CheckDone ( )
{
2011-10-13 20:53:06 +02:00
if ( mExiting ) {
2010-07-17 22:52:50 +02:00
close ( ) ;
return ;
}
2010-07-04 17:54:41 +02:00
mUI . mResults - > CheckingFinished ( ) ;
2009-03-22 16:42:00 +01:00
EnableCheckButtons ( true ) ;
2009-07-02 10:32:29 +02:00
mUI . mActionSettings - > setEnabled ( true ) ;
2010-07-12 01:18:26 +02:00
mUI . mActionOpenXML - > setEnabled ( true ) ;
2010-08-25 17:54:45 +02:00
EnableProjectActions ( true ) ;
2010-08-25 19:59:30 +02:00
EnableProjectOpenActions ( true ) ;
2011-12-27 11:46:21 +01:00
mPlatformActions - > setEnabled ( true ) ;
2013-03-02 13:01:21 +01:00
mCStandardActions - > setEnabled ( true ) ;
mCppStandardActions - > setEnabled ( true ) ;
2015-10-14 14:31:52 +02:00
mSelectLanguageActions - > setEnabled ( true ) ;
2011-12-27 21:12:52 +01:00
mUI . mActionPosix - > setEnabled ( true ) ;
2012-10-20 20:14:52 +02:00
if ( mScratchPad )
mScratchPad - > setEnabled ( true ) ;
2010-07-13 11:46:28 +02:00
2011-10-13 20:53:06 +02:00
if ( mUI . mResults - > HasResults ( ) ) {
2009-07-02 10:32:29 +02:00
mUI . mActionClearResults - > setEnabled ( true ) ;
mUI . mActionSave - > setEnabled ( true ) ;
2015-04-17 16:33:52 +02:00
mUI . mActionPrint - > setEnabled ( true ) ;
mUI . mActionPrintPreview - > setEnabled ( true ) ;
2009-06-20 22:05:17 +02:00
}
2009-06-20 22:23:54 +02:00
2011-12-28 10:50:26 +01:00
for ( int i = 0 ; i < MaxRecentProjects + 1 ; i + + ) {
if ( mRecentProjectActs [ i ] ! = NULL )
mRecentProjectActs [ i ] - > setEnabled ( true ) ;
}
2009-06-20 22:23:54 +02:00
// Notify user - if the window is not active - that check is ready
QApplication : : alert ( this , 3000 ) ;
2015-07-14 18:48:51 +02:00
ShowStatistics ( ) ;
2009-03-22 13:32:07 +01:00
}
2011-02-11 20:08:37 +01:00
void MainWindow : : CheckLockDownUI ( )
{
EnableCheckButtons ( false ) ;
mUI . mActionSettings - > setEnabled ( false ) ;
mUI . mActionOpenXML - > setEnabled ( false ) ;
EnableProjectActions ( false ) ;
EnableProjectOpenActions ( false ) ;
2011-12-27 11:46:21 +01:00
mPlatformActions - > setEnabled ( false ) ;
2013-03-02 13:01:21 +01:00
mCStandardActions - > setEnabled ( false ) ;
mCppStandardActions - > setEnabled ( false ) ;
2015-10-14 14:31:52 +02:00
mSelectLanguageActions - > setEnabled ( false ) ;
2011-12-27 21:12:52 +01:00
mUI . mActionPosix - > setEnabled ( false ) ;
2012-10-20 20:14:52 +02:00
if ( mScratchPad )
mScratchPad - > setEnabled ( false ) ;
2011-12-28 10:50:26 +01:00
for ( int i = 0 ; i < MaxRecentProjects + 1 ; i + + ) {
if ( mRecentProjectActs [ i ] ! = NULL )
mRecentProjectActs [ i ] - > setEnabled ( false ) ;
}
2011-02-11 20:08:37 +01:00
}
2009-03-22 13:32:07 +01:00
void MainWindow : : ProgramSettings ( )
{
2011-05-13 09:25:52 +02:00
SettingsDialog dialog ( mApplications , mTranslation , this ) ;
2011-10-13 20:53:06 +02:00
if ( dialog . exec ( ) = = QDialog : : Accepted ) {
2010-11-26 21:35:45 +01:00
dialog . SaveSettingValues ( ) ;
2009-07-02 10:32:29 +02:00
mUI . mResults - > UpdateSettings ( dialog . ShowFullPath ( ) ,
dialog . SaveFullPath ( ) ,
dialog . SaveAllErrors ( ) ,
2012-10-27 11:16:52 +02:00
dialog . ShowNoErrorsMessage ( ) ,
2015-10-15 11:59:17 +02:00
dialog . ShowErrorId ( ) ,
dialog . ShowInconclusive ( ) ) ;
2011-02-07 11:30:13 +01:00
const QString newLang = mSettings - > value ( SETTINGS_LANGUAGE , " en " ) . toString ( ) ;
2011-02-08 17:22:44 +01:00
SetLanguage ( newLang ) ;
2009-03-22 13:32:07 +01:00
}
2009-03-01 08:38:21 +01:00
}
2009-03-22 13:32:07 +01:00
2015-10-14 19:07:20 +02:00
void MainWindow : : ReCheckModified ( )
2009-03-22 16:42:00 +01:00
{
2015-10-14 19:07:20 +02:00
ReCheck ( false ) ;
}
void MainWindow : : ReCheckAll ( )
{
ReCheck ( true ) ;
}
2015-12-23 10:28:07 +01:00
void MainWindow : : ReCheckSelected ( QStringList files , bool all )
{
if ( files . empty ( ) )
return ;
2015-12-23 23:05:26 +01:00
if ( mThread - > IsChecking ( ) )
2015-12-23 10:28:07 +01:00
return ;
// Clear details, statistics and progress
mUI . mResults - > Clear ( false ) ;
for ( int i = 0 ; i < files . size ( ) ; + + i )
2015-12-29 01:38:36 +01:00
mUI . mResults - > ClearRecheckFile ( files [ i ] ) ;
FileList pathList ;
pathList . AddPathList ( files ) ;
if ( mProject )
pathList . AddExcludeList ( mProject - > GetProjectFile ( ) - > GetExcludedPaths ( ) ) ;
QStringList fileNames = pathList . GetFileList ( ) ;
2015-12-23 10:28:07 +01:00
CheckLockDownUI ( ) ; // lock UI while checking
2015-12-29 01:38:36 +01:00
mUI . mResults - > CheckingStarted ( fileNames . size ( ) ) ;
mThread - > SetCheckFiles ( fileNames ) ;
2015-12-23 10:28:07 +01:00
// 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?
QDateTime saveCheckStartTime = mThread - > GetCheckStartTime ( ) ;
mThread - > Check ( GetCppcheckSettings ( ) , all ) ;
mThread - > SetCheckStartTime ( saveCheckStartTime ) ;
}
2015-10-14 19:07:20 +02:00
void MainWindow : : ReCheck ( bool all )
{
const QStringList files = mThread - > GetReCheckFiles ( all ) ;
2012-02-14 21:16:11 +01:00
if ( files . empty ( ) )
return ;
2010-07-07 11:25:47 +02:00
2012-09-05 21:01:50 +02:00
// Clear details, statistics and progress
2015-10-14 19:07:20 +02:00
mUI . mResults - > Clear ( all ) ;
2012-09-05 21:01:50 +02:00
2012-02-14 21:16:11 +01:00
// Clear results for changed files
for ( int i = 0 ; i < files . size ( ) ; + + i )
mUI . mResults - > Clear ( files [ i ] ) ;
CheckLockDownUI ( ) ; // lock UI while checking
mUI . mResults - > CheckingStarted ( files . size ( ) ) ;
2010-07-07 11:25:47 +02:00
2011-06-07 00:30:44 +02:00
if ( mProject )
qDebug ( ) < < " Rechecking project file " < < mProject - > GetProjectFile ( ) - > GetFilename ( ) ;
2011-06-04 14:38:51 +02:00
2015-12-23 10:28:07 +01:00
mThread - > SetCheckFiles ( all ) ;
mThread - > Check ( GetCppcheckSettings ( ) , all ) ;
2009-03-22 16:42:00 +01:00
}
void MainWindow : : ClearResults ( )
{
2012-09-05 21:01:50 +02:00
mUI . mResults - > Clear ( true ) ;
2009-07-02 10:32:29 +02:00
mUI . mActionClearResults - > setEnabled ( false ) ;
mUI . mActionSave - > setEnabled ( false ) ;
2015-04-17 16:33:52 +02:00
mUI . mActionPrint - > setEnabled ( false ) ;
mUI . mActionPrintPreview - > setEnabled ( false ) ;
2009-03-22 16:42:00 +01:00
}
2013-02-10 07:48:09 +01:00
void MainWindow : : OpenResults ( )
2010-07-10 15:37:36 +02:00
{
2012-08-02 14:09:08 +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 ;
}
}
2010-07-10 15:37:36 +02:00
QString selectedFilter ;
2011-08-22 21:37:38 +02:00
const QString filter ( tr ( " XML files (*.xml) " )) ;
2010-07-10 15:37:36 +02:00
QString selectedFile = QFileDialog : : getOpenFileName ( this ,
tr ( " Open the report file " ) ,
2013-02-08 16:18:05 +01:00
GetPath ( SETTINGS_LAST_RESULT_PATH ) ,
2010-07-10 15:37:36 +02:00
filter ,
& selectedFilter ) ;
2013-02-10 07:48:09 +01:00
if ( ! selectedFile . isEmpty ( ) ) {
LoadResults ( selectedFile ) ;
}
}
void MainWindow : : LoadResults ( const QString selectedFile )
{
2011-10-13 20:53:06 +02:00
if ( ! selectedFile . isEmpty ( ) ) {
2012-09-05 21:01:50 +02:00
mUI . mResults - > Clear ( true ) ;
2010-07-10 15:37:36 +02:00
mUI . mResults - > ReadErrorsXml ( selectedFile ) ;
2013-02-08 16:18:05 +01:00
SetPath ( SETTINGS_LAST_RESULT_PATH , selectedFile ) ;
2010-07-10 15:37:36 +02:00
}
}
2013-02-10 07:48:09 +01:00
void MainWindow : : LoadResults ( const QString selectedFile , const QString sourceDirectory )
{
LoadResults ( selectedFile ) ;
mUI . mResults - > SetCheckDirectory ( sourceDirectory ) ;
}
2009-03-22 16:42:00 +01:00
void MainWindow : : EnableCheckButtons ( bool enable )
{
2009-07-02 10:32:29 +02:00
mUI . mActionStop - > setEnabled ( ! enable ) ;
mUI . mActionCheckFiles - > setEnabled ( enable ) ;
2009-10-13 10:19:00 +02:00
2015-10-14 19:07:20 +02:00
if ( ! enable | | mThread - > HasPreviousFiles ( ) ) {
mUI . mActionRecheckModified - > setEnabled ( enable ) ;
mUI . mActionRecheckAll - > setEnabled ( enable ) ;
}
2009-10-13 10:19:00 +02:00
2009-07-02 10:32:29 +02:00
mUI . mActionCheckDirectory - > setEnabled ( enable ) ;
2009-03-22 16:42:00 +01:00
}
2009-03-22 18:39:44 +01:00
void MainWindow : : ShowStyle ( bool checked )
{
2011-10-11 21:14:15 +02:00
mUI . mResults - > ShowResults ( ShowTypes : : ShowStyle , checked ) ;
2009-03-22 18:39:44 +01:00
}
void MainWindow : : ShowErrors ( bool checked )
{
2011-10-11 21:14:15 +02:00
mUI . mResults - > ShowResults ( ShowTypes : : ShowErrors , checked ) ;
2009-03-22 18:39:44 +01:00
}
2009-05-23 10:17:27 +02:00
2010-10-18 19:50:34 +02:00
void MainWindow : : ShowWarnings ( bool checked )
{
2011-10-11 21:14:15 +02:00
mUI . mResults - > ShowResults ( ShowTypes : : ShowWarnings , checked ) ;
2010-10-18 19:50:34 +02:00
}
2010-12-27 10:06:31 +01:00
void MainWindow : : ShowPortability ( bool checked )
{
2011-10-11 21:14:15 +02:00
mUI . mResults - > ShowResults ( ShowTypes : : ShowPortability , checked ) ;
2010-12-27 10:06:31 +01:00
}
2010-10-18 19:50:34 +02:00
void MainWindow : : ShowPerformance ( bool checked )
{
2011-10-11 21:14:15 +02:00
mUI . mResults - > ShowResults ( ShowTypes : : ShowPerformance , checked ) ;
2010-10-18 19:50:34 +02:00
}
2010-12-26 13:36:24 +01:00
void MainWindow : : ShowInformation ( bool checked )
{
2011-10-11 21:14:15 +02:00
mUI . mResults - > ShowResults ( ShowTypes : : ShowInformation , checked ) ;
2010-12-26 13:36:24 +01:00
}
2009-05-23 10:17:27 +02:00
void MainWindow : : CheckAll ( )
{
ToggleAllChecked ( true ) ;
}
void MainWindow : : UncheckAll ( )
{
ToggleAllChecked ( false ) ;
}
2009-06-04 16:02:35 +02:00
void MainWindow : : closeEvent ( QCloseEvent * event )
{
// Check that we aren't checking files
2011-10-13 20:53:06 +02:00
if ( ! mThread - > IsChecking ( ) ) {
2009-07-04 10:06:29 +02:00
SaveSettings ( ) ;
2009-06-04 16:02:35 +02:00
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? " ) ) ;
2009-06-09 09:51:27 +02:00
QMessageBox msg ( QMessageBox : : Warning ,
tr ( " Cppcheck " ) ,
text ,
2010-07-16 11:44:11 +02:00
QMessageBox : : Yes | QMessageBox : : No ,
2009-06-09 09:51:27 +02:00
this ) ;
2010-07-16 11:44:11 +02:00
msg . setDefaultButton ( QMessageBox : : No ) ;
int rv = msg . exec ( ) ;
2011-10-13 20:53:06 +02:00
if ( rv = = QMessageBox : : Yes ) {
2010-07-16 11:44:11 +02:00
// This isn't really very clean way to close threads but since the app is
// exiting it doesn't matter.
mThread - > Stop ( ) ;
SaveSettings ( ) ;
2010-07-17 22:52:50 +02:00
mExiting = true ;
2010-07-16 11:44:11 +02:00
}
2010-07-17 22:52:50 +02:00
event - > ignore ( ) ;
2009-06-04 16:02:35 +02:00
}
}
2009-05-23 10:17:27 +02:00
void MainWindow : : ToggleAllChecked ( bool checked )
{
2009-07-02 10:32:29 +02:00
mUI . mActionShowStyle - > setChecked ( checked ) ;
2009-05-23 10:17:27 +02:00
ShowStyle ( checked ) ;
2009-07-02 10:32:29 +02:00
mUI . mActionShowErrors - > setChecked ( checked ) ;
2009-05-23 10:17:27 +02:00
ShowErrors ( checked ) ;
2010-10-18 19:50:34 +02:00
mUI . mActionShowWarnings - > setChecked ( checked ) ;
ShowWarnings ( checked ) ;
2010-12-27 10:06:31 +01:00
mUI . mActionShowPortability - > setChecked ( checked ) ;
ShowPortability ( checked ) ;
2010-10-18 19:50:34 +02:00
mUI . mActionShowPerformance - > setChecked ( checked ) ;
ShowPerformance ( checked ) ;
2010-12-26 13:36:24 +01:00
mUI . mActionShowInformation - > setChecked ( checked ) ;
ShowInformation ( checked ) ;
2009-05-23 10:17:27 +02:00
}
2009-05-24 10:53:29 +02:00
void MainWindow : : About ( )
{
2011-08-11 17:13:54 +02:00
AboutDialog * dlg = new AboutDialog ( CppCheck : : version ( ) , CppCheck : : extraVersion ( ) , this ) ;
2009-06-14 11:57:43 +02:00
dlg - > exec ( ) ;
2009-05-24 10:53:29 +02:00
}
2009-05-26 17:21:39 +02:00
2009-06-14 11:57:43 +02:00
void MainWindow : : ShowLicense ( )
{
2009-07-02 19:23:44 +02:00
FileViewDialog * dlg = new FileViewDialog ( " :COPYING " , tr ( " License " ) , this ) ;
2009-07-02 18:46:26 +02:00
dlg - > resize ( 570 , 400 ) ;
2009-06-14 11:57:43 +02:00
dlg - > exec ( ) ;
}
void MainWindow : : ShowAuthors ( )
{
2009-07-02 19:23:44 +02:00
FileViewDialog * dlg = new FileViewDialog ( " :AUTHORS " , tr ( " Authors " ) , this ) ;
2009-06-14 22:22:19 +02:00
dlg - > resize ( 350 , 400 ) ;
2009-06-14 11:57:43 +02:00
dlg - > exec ( ) ;
}
2009-05-26 17:21:39 +02:00
2015-12-23 10:28:07 +01:00
void MainWindow : : PerformSelectedFilesCheck ( QStringList selectedFilesList )
{
ReCheckSelected ( selectedFilesList , true ) ;
}
2009-05-26 17:21:39 +02:00
void MainWindow : : Save ( )
{
2009-06-17 23:34:22 +02:00
QString selectedFilter ;
2011-08-22 21:37:38 +02:00
const QString filter ( tr ( " XML files version 2 (*.xml) ; ; XML files version 1 ( * . xml ) ; ; Text files ( * . txt ) ; ; CSV files ( * . csv ) " )) ;
2009-06-17 23:34:22 +02:00
QString selectedFile = QFileDialog : : getSaveFileName ( this ,
tr ( " Save the report file " ) ,
2013-02-08 16:18:05 +01:00
GetPath ( SETTINGS_LAST_RESULT_PATH ) ,
2009-06-17 23:34:22 +02:00
filter ,
& selectedFilter ) ;
2011-10-13 20:53:06 +02:00
if ( ! selectedFile . isEmpty ( ) ) {
2009-11-19 23:19:44 +01:00
Report : : Type type = Report : : TXT ;
2011-10-13 20:53:06 +02:00
if ( selectedFilter = = tr ( " XML files version 1 (*.xml) " ) ) {
2009-07-06 11:30:49 +02:00
type = Report : : XML ;
2010-04-02 07:30:58 +02:00
if ( ! selectedFile . endsWith ( " .xml " , Qt : : CaseInsensitive ) )
2009-07-06 11:30:49 +02:00
selectedFile + = " .xml " ;
2011-10-13 20:53:06 +02:00
} else if ( selectedFilter = = tr ( " XML files version 2 (*.xml) " )) {
2011-02-03 20:40:35 +01:00
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) " )) {
2009-07-06 11:30:49 +02:00
type = Report : : TXT ;
2010-04-02 07:30:58 +02:00
if ( ! selectedFile . endsWith ( " .txt " , Qt : : CaseInsensitive ) )
2009-07-06 11:30:49 +02:00
selectedFile + = " .txt " ;
2011-10-13 20:53:06 +02:00
} else if ( selectedFilter = = tr ( " CSV files (*.csv) " )) {
2009-07-06 11:30:49 +02:00
type = Report : : CSV ;
2010-04-02 07:30:58 +02:00
if ( ! selectedFile . endsWith ( " .csv " , Qt : : CaseInsensitive ) )
2009-07-06 11:30:49 +02:00
selectedFile + = " .csv " ;
2011-10-13 20:53:06 +02:00
} else {
2010-04-02 07:30:58 +02:00
if ( selectedFile . endsWith ( " .xml " , Qt : : CaseInsensitive ) )
2009-11-19 23:19:44 +01:00
type = Report : : XML ;
2010-04-02 07:30:58 +02:00
else if ( selectedFile . endsWith ( " .txt " , Qt : : CaseInsensitive ) )
2009-11-19 23:19:44 +01:00
type = Report : : TXT ;
2010-04-02 07:30:58 +02:00
else if ( selectedFile . endsWith ( " .csv " , Qt : : CaseInsensitive ) )
2009-11-19 23:19:44 +01:00
type = Report : : CSV ;
}
2009-06-17 23:34:22 +02:00
2009-07-06 11:30:49 +02:00
mUI . mResults - > Save ( selectedFile , type ) ;
2013-02-08 16:18:05 +01:00
SetPath ( SETTINGS_LAST_RESULT_PATH , selectedFile ) ;
2009-06-03 20:18:22 +02:00
}
2009-05-26 17:21:39 +02:00
}
2009-06-03 20:18:22 +02:00
void MainWindow : : ResultsAdded ( )
{
}
2009-06-22 17:45:48 +02:00
2010-07-03 19:03:53 +02:00
void MainWindow : : ToggleMainToolBar ( )
2009-07-01 22:46:42 +02:00
{
2010-07-03 19:03:53 +02:00
mUI . mToolBarMain - > setVisible ( mUI . mActionToolBarMain - > isChecked ( ) ) ;
2009-07-01 22:46:42 +02:00
}
2010-07-03 18:37:09 +02:00
void MainWindow : : ToggleViewToolBar ( )
{
mUI . mToolBarView - > setVisible ( mUI . mActionToolBarView - > isChecked ( ) ) ;
}
2011-05-04 07:30:54 +02:00
void MainWindow : : ToggleFilterToolBar ( )
{
mUI . mToolBarFilter - > setVisible ( mUI . mActionToolBarFilter - > isChecked ( ) ) ;
mLineEditFilter - > clear ( ) ; // Clearing the filter also disables filtering
}
2009-06-22 17:45:48 +02:00
void MainWindow : : FormatAndSetTitle ( const QString & text )
{
QString title ;
2010-04-02 07:30:58 +02:00
if ( text . isEmpty ( ) )
2009-06-22 17:45:48 +02:00
title = tr ( " Cppcheck " ) ;
else
title = QString ( tr ( " Cppcheck - %1 " ) ) . arg ( text ) ;
setWindowTitle ( title ) ;
}
2009-07-01 22:46:42 +02:00
2011-02-07 11:30:13 +01:00
void MainWindow : : SetLanguage ( const QString & code )
2009-07-01 22:46:42 +02:00
{
2011-02-08 17:22:44 +01:00
const QString currentLang = mTranslation - > GetCurrentLanguage ( ) ;
if ( currentLang = = code )
2009-07-02 10:32:29 +02:00
return ;
2013-03-01 19:11:27 +01:00
if ( mTranslation - > SetLanguage ( code ) ) {
2009-07-02 12:48:32 +02:00
//Translate everything that is visible here
2009-07-02 10:32:29 +02:00
mUI . retranslateUi ( this ) ;
2009-07-02 10:46:26 +02:00
mUI . mResults - > Translate ( ) ;
2013-03-01 21:07:34 +01:00
delete mLogView ;
mLogView = 0 ;
2009-07-02 10:32:29 +02:00
}
2009-07-01 22:46:42 +02:00
}
2009-07-02 10:32:29 +02:00
2009-07-02 19:11:47 +02:00
void MainWindow : : AboutToShowViewMenu ( )
{
2010-07-03 19:03:53 +02:00
mUI . mActionToolBarMain - > setChecked ( mUI . mToolBarMain - > isVisible ( ) ) ;
2010-07-03 18:37:09 +02:00
mUI . mActionToolBarView - > setChecked ( mUI . mToolBarView - > isVisible ( ) ) ;
2011-07-20 12:35:56 +02:00
mUI . mActionToolBarFilter - > setChecked ( mUI . mToolBarFilter - > isVisible ( ) ) ;
2009-07-02 19:11:47 +02:00
}
2009-07-02 19:23:44 +02:00
void MainWindow : : StopChecking ( )
{
mThread - > Stop ( ) ;
mUI . mResults - > DisableProgressbar ( ) ;
}
2010-02-23 16:29:49 +01:00
void MainWindow : : OpenHelpContents ( )
{
2011-03-26 15:06:11 +01:00
OpenOnlineHelp ( ) ;
2010-02-23 16:29:49 +01:00
}
2011-03-26 15:06:11 +01:00
void MainWindow : : OpenOnlineHelp ( )
2010-02-23 16:29:49 +01:00
{
2011-03-26 15:06:11 +01:00
QDesktopServices : : openUrl ( QUrl ( OnlineHelpURL ) ) ;
2010-02-23 16:29:49 +01:00
}
2010-07-07 20:18:42 +02:00
2010-07-13 11:33:24 +02:00
void MainWindow : : OpenProjectFile ( )
2010-07-07 20:18:42 +02:00
{
2013-02-10 08:15:32 +01:00
const QString lastPath = mSettings - > value ( SETTINGS_LAST_PROJECT_PATH , QString ( ) ) . toString ( ) ;
2010-07-07 20:18:42 +02:00
const QString filter = tr ( " Project files (*.cppcheck);;All files(*.*) " ) ;
2012-01-10 20:40:11 +01:00
const QString filepath = QFileDialog : : getOpenFileName ( this ,
tr ( " Select Project File " ) ,
2013-02-08 16:18:05 +01:00
GetPath ( SETTINGS_LAST_PROJECT_PATH ) ,
2012-01-10 20:40:11 +01:00
filter ) ;
2010-07-07 20:18:42 +02:00
2011-10-13 20:53:06 +02:00
if ( ! filepath . isEmpty ( ) ) {
2012-01-10 20:40:11 +01:00
const QFileInfo fi ( filepath ) ;
if ( fi . exists ( ) & & fi . isFile ( ) & & fi . isReadable ( ) ) {
2013-02-08 16:18:05 +01:00
SetPath ( SETTINGS_LAST_PROJECT_PATH , filepath ) ;
2012-01-10 20:40:11 +01:00
LoadProjectFile ( filepath ) ;
}
2011-04-07 14:34:15 +02:00
}
}
2010-08-21 18:38:51 +02:00
2012-10-20 20:14:52 +02:00
void MainWindow : : ShowScratchpad ( )
{
if ( ! mScratchPad )
mScratchPad = new ScratchPad ( * this ) ;
mScratchPad - > show ( ) ;
if ( ! mScratchPad - > isActiveWindow ( ) )
mScratchPad - > activateWindow ( ) ;
}
2011-04-07 14:34:15 +02:00
void MainWindow : : LoadProjectFile ( const QString & filePath )
{
QFileInfo inf ( filePath ) ;
const QString filename = inf . fileName ( ) ;
2011-05-12 20:15:10 +02:00
FormatAndSetTitle ( tr ( " Project: " ) + QString ( " " ) + filename ) ;
2011-05-12 11:34:23 +02:00
AddProjectMRU ( filePath ) ;
2011-04-07 14:34:15 +02:00
mUI . mActionCloseProjectFile - > setEnabled ( true ) ;
mUI . mActionEditProjectFile - > setEnabled ( true ) ;
delete mProject ;
mProject = new Project ( filePath , this ) ;
2011-08-24 21:41:48 +02:00
CheckProject ( mProject ) ;
}
void MainWindow : : CheckProject ( Project * project )
{
2012-10-14 17:40:51 +02:00
if ( ! project - > IsOpen ( ) ) {
if ( ! project - > Open ( ) ) {
delete mProject ;
mProject = 0 ;
return ;
}
}
2011-08-24 21:41:48 +02:00
QFileInfo inf ( project - > Filename ( ) ) ;
const QString rootpath = project - > GetProjectFile ( ) - > GetRootPath ( ) ;
2011-04-07 14:34:15 +02:00
2011-06-07 13:29:59 +02:00
// If the root path is not given or is not "current dir", use project
// file's location directory as root path
2011-04-07 14:34:15 +02:00
if ( rootpath . isEmpty ( ) | | rootpath = = " . " )
mCurrentDirectory = inf . canonicalPath ( ) ;
2015-08-10 20:48:08 +02:00
else if ( rootpath . startsWith ( " . " ) )
mCurrentDirectory = inf . canonicalPath ( ) + rootpath . mid ( 1 ) ;
2011-04-07 14:34:15 +02:00
else
mCurrentDirectory = rootpath ;
2011-08-24 21:41:48 +02:00
QStringList paths = project - > GetProjectFile ( ) - > GetCheckPaths ( ) ;
2011-06-07 13:29:59 +02:00
// 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 ( ) ) {
2011-06-07 13:29:59 +02:00
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 ] ) ) {
2011-06-07 13:29:59 +02:00
QString path = mCurrentDirectory + " / " ;
path + = paths [ i ] ;
paths [ i ] = QDir : : cleanPath ( path ) ;
2010-08-19 21:06:45 +02:00
}
2010-07-07 20:18:42 +02:00
}
2011-06-07 13:29:59 +02:00
DoCheckFiles ( paths ) ;
2010-07-07 20:18:42 +02:00
}
2010-07-08 17:59:06 +02:00
2010-07-13 11:33:24 +02:00
void MainWindow : : NewProjectFile ( )
2010-07-08 17:59:06 +02:00
{
2010-07-12 19:21:45 +02:00
const QString filter = tr ( " Project files (*.cppcheck);;All files(*.*) " ) ;
QString filepath = QFileDialog : : getSaveFileName ( this ,
tr ( " Select Project Filename " ) ,
2013-02-08 16:18:05 +01:00
GetPath ( SETTINGS_LAST_PROJECT_PATH ) ,
2010-07-12 19:21:45 +02:00
filter ) ;
2011-11-25 07:42:16 +01:00
if ( filepath . isEmpty ( ) )
return ;
2013-02-09 19:36:48 +01:00
2013-02-08 16:18:05 +01:00
SetPath ( SETTINGS_LAST_PROJECT_PATH , filepath ) ;
2013-02-19 21:39:50 +01:00
2011-11-25 07:42:16 +01:00
EnableProjectActions ( true ) ;
QFileInfo inf ( filepath ) ;
const QString filename = inf . fileName ( ) ;
FormatAndSetTitle ( tr ( " Project: " ) + QString ( " " ) + filename ) ;
delete mProject ;
mProject = new Project ( filepath , this ) ;
mProject - > Create ( ) ;
2012-10-14 14:13:54 +02:00
if ( mProject - > Edit ( ) ) {
AddProjectMRU ( filepath ) ;
CheckProject ( mProject ) ;
}
2010-07-08 17:59:06 +02:00
}
2010-07-16 16:48:13 +02:00
2010-08-15 11:12:28 +02:00
void MainWindow : : CloseProjectFile ( )
{
delete mProject ;
mProject = NULL ;
2010-08-25 17:54:45 +02:00
EnableProjectActions ( false ) ;
2010-08-25 19:59:30 +02:00
EnableProjectOpenActions ( true ) ;
2010-08-17 17:06:17 +02:00
FormatAndSetTitle ( ) ;
2010-08-15 15:12:32 +02:00
}
void MainWindow : : EditProjectFile ( )
{
2011-10-13 20:53:06 +02:00
if ( ! mProject ) {
2010-08-15 15:12:32 +02:00
QMessageBox msg ( QMessageBox : : Critical ,
tr ( " Cppcheck " ) ,
QString ( tr ( " No project file loaded " ) ) ,
QMessageBox : : Ok ,
this ) ;
msg . exec ( ) ;
return ;
2010-07-12 19:21:45 +02:00
}
2010-08-15 15:12:32 +02:00
mProject - > Edit ( ) ;
2010-07-08 17:59:06 +02:00
}
2010-07-16 16:48:13 +02:00
void MainWindow : : ShowLogView ( )
{
if ( mLogView = = NULL )
2011-05-13 09:38:54 +02:00
mLogView = new LogView ;
2010-07-16 16:48:13 +02:00
mLogView - > show ( ) ;
if ( ! mLogView - > isActiveWindow ( ) )
mLogView - > activateWindow ( ) ;
}
2010-09-01 08:12:24 +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 ) {
2010-09-01 08:12:24 +02:00
statsDialog . setProject ( * mProject ) ;
}
statsDialog . setPathSelected ( mCurrentDirectory ) ;
statsDialog . setNumberOfFilesScanned ( mThread - > GetPreviousFilesCount ( ) ) ;
statsDialog . setScanDuration ( mThread - > GetPreviousScanDuration ( ) / 1000.0 ) ;
2010-11-29 23:01:45 +01:00
statsDialog . setStatistics ( mUI . mResults - > GetStatistics ( ) ) ;
2010-09-01 08:12:24 +02:00
statsDialog . exec ( ) ;
}
2015-08-16 17:36:10 +02:00
void MainWindow : : ShowLibraryEditor ( )
{
2015-09-04 20:46:26 +02:00
LibraryDialog libraryDialog ( this ) ;
libraryDialog . exec ( ) ;
2015-08-16 17:36:10 +02:00
}
2010-07-16 16:48:13 +02:00
void MainWindow : : Log ( const QString & logline )
{
2011-10-13 20:53:06 +02:00
if ( mLogView ) {
2010-07-16 16:48:13 +02:00
mLogView - > AppendLine ( logline ) ;
}
}
2010-08-25 17:54:45 +02:00
2010-08-28 19:37:21 +02:00
void MainWindow : : DebugError ( const ErrorItem & item )
{
2011-10-13 20:53:06 +02:00
if ( mLogView ) {
2010-08-28 19:37:21 +02:00
mLogView - > AppendLine ( item . ToString ( ) ) ;
}
}
2011-05-04 07:30:54 +02:00
void MainWindow : : FilterResults ( )
{
mUI . mResults - > FilterResults ( mLineEditFilter - > text ( ) ) ;
}
2010-08-25 17:54:45 +02:00
void MainWindow : : EnableProjectActions ( bool enable )
{
mUI . mActionCloseProjectFile - > setEnabled ( enable ) ;
mUI . mActionEditProjectFile - > setEnabled ( enable ) ;
}
2010-08-25 19:59:30 +02:00
void MainWindow : : EnableProjectOpenActions ( bool enable )
{
mUI . mActionNewProjectFile - > setEnabled ( enable ) ;
mUI . mActionOpenProjectFile - > setEnabled ( enable ) ;
}
2011-05-12 11:34:23 +02:00
void MainWindow : : OpenRecentProject ( )
{
QAction * action = qobject_cast < QAction * > ( sender ( ) ) ;
2011-10-13 20:53:06 +02:00
if ( action ) {
2011-05-12 12:30:22 +02:00
const QString project = action - > data ( ) . toString ( ) ;
QFileInfo inf ( project ) ;
2011-10-13 20:53:06 +02:00
if ( inf . exists ( ) ) {
2011-05-12 12:30:22 +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 ) ) ;
2011-05-12 12:30:22 +02:00
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 ) {
2011-05-12 12:30:22 +02:00
RemoveProjectMRU ( project ) ;
}
}
}
2011-05-12 11:34:23 +02:00
}
void MainWindow : : UpdateMRUMenuItems ( )
{
2011-10-13 20:53:06 +02:00
for ( int i = 0 ; i < MaxRecentProjects + 1 ; i + + ) {
2011-05-12 11:34:23 +02:00
if ( mRecentProjectActs [ i ] ! = NULL )
mUI . mMenuFile - > removeAction ( mRecentProjectActs [ i ] ) ;
}
QStringList projects = mSettings - > value ( SETTINGS_MRU_PROJECTS ) . toStringList ( ) ;
2011-12-27 10:46:43 +01:00
// 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 ) ;
2011-05-12 11:34:23 +02:00
const int numRecentProjects = qMin ( projects . size ( ) , ( int ) MaxRecentProjects ) ;
2011-10-13 20:53:06 +02:00
for ( int i = 0 ; i < numRecentProjects ; i + + ) {
2011-05-12 11:34:23 +02:00
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 )
2011-08-24 22:14:28 +02:00
mRecentProjectActs [ numRecentProjects ] = mUI . mMenuFile - > insertSeparator ( mUI . mActionProjectMRU ) ;
2011-05-12 11:34:23 +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 ) ;
UpdateMRUMenuItems ( ) ;
}
2011-05-12 12:30:22 +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 ) ;
UpdateMRUMenuItems ( ) ;
}
2011-09-29 21:46:19 +02:00
2011-10-02 21:08:12 +02:00
void MainWindow : : SelectPlatform ( )
2011-09-29 21:46:19 +02:00
{
2011-10-02 21:08:12 +02:00
QAction * action = qobject_cast < QAction * > ( sender ( ) ) ;
2011-10-13 20:53:06 +02:00
if ( action ) {
2011-10-02 21:08:12 +02:00
const Settings : : PlatformType platform = ( Settings : : PlatformType ) action - > data ( ) . toInt ( ) ;
mSettings - > setValue ( SETTINGS_CHECKED_PLATFORM , platform ) ;
}
2011-09-29 21:46:19 +02:00
}