Now adds directory contents to list of files to check.

Only adds proper (.cpp,.c,.cpp,...) files to the list of files to check.
Modified checkthread to clear results after each file.
This commit is contained in:
Vesa Pikki 2009-03-02 19:56:51 +00:00
parent 20390ce655
commit e629db6818
12 changed files with 182 additions and 107 deletions

View File

@ -27,6 +27,9 @@
#include <QFileDialog>
#include <QDirIterator>
#include <QDebug>
#include <QFileInfo>
#include <QDirIterator>
#include "../src/filelister.h"
CheckDialog::CheckDialog(QSettings &programSettings) :
mSettings(programSettings)
@ -111,8 +114,54 @@ CheckDialog::~CheckDialog()
}
QStringList CheckDialog::RemoveUnacceptedFiles(const QStringList &list)
{
QStringList result;
QString str;
foreach(str, list)
{
if (FileLister::AcceptFile(str.toStdString()))
{
result << str;
}
}
return result;
}
QStringList CheckDialog::GetFiles(QModelIndex index)
{
QFileInfo info(mModel.filePath(index));
QStringList list;
if (info.isDir())
{
QDirIterator it(mModel.filePath(index), QDirIterator::Subdirectories);
while (it.hasNext())
{
list << it.next();
}
}
else
{
list << mModel.filePath(index);
}
return list;
}
QStringList CheckDialog::RemoveDuplicates(const QStringList &list)
{
QHash<QString, int> hash;
QString str;
foreach(str, list)
{
hash[str] = 0;
}
return QStringList(hash.uniqueKeys());
}
QStringList CheckDialog::GetSelectedFiles()
{
@ -124,11 +173,13 @@ QStringList CheckDialog::GetSelectedFiles()
{
if (!mModel.filePath(index).isEmpty())
{
list << mModel.filePath(index);
list << GetFiles(index);
}
}
return list;
QString str;
return RemoveUnacceptedFiles(RemoveDuplicates(list));
}

View File

@ -33,9 +33,9 @@
#include <QDirModel>
#include <QStandardItem>
/**
* @brief Dialog to select what and how to check
*
/**
* @brief Dialog to select what and how to check
*
*/
class CheckDialog : public QDialog
{
@ -44,38 +44,41 @@ public:
CheckDialog(QSettings &programSettings);
virtual ~CheckDialog();
/**
* @brief Get cppcheck settings based on user selections
*
* @return cppcheck settings
/**
* @brief Get cppcheck settings based on user selections
*
* @return cppcheck settings
*/
Settings GetSettings();
/**
* @brief Get the root path of current selection
*
* @return default path to use next time
/**
* @brief Get the root path of current selection
*
* @return default path to use next time
*/
QString GetDefaultPath();
/**
* @brief Get a list of selected files and directories
*
* @return list of selected files
/**
* @brief Get a list of selected files and directories
*
* @return list of selected files
*/
QStringList GetSelectedFiles();
/**
* @brief Save all checkbox values
*
/**
* @brief Save all checkbox values
*
*/
void SaveCheckboxValues();
protected:
QStringList RemoveUnacceptedFiles(const QStringList &list);
QStringList RemoveDuplicates(const QStringList &list);
QStringList GetFiles(QModelIndex index);
/**
* @brief Load saved values
/**
* @brief Load saved values
* Loads dialog size and column widths.
*
*
*/
void SaveSettings();
@ -86,58 +89,58 @@ protected:
*/
void LoadSettings();
/**
* @brief Save a single checkboxes value
*
* @param box checkbox to save
* @param name name for QSettings to store the value
/**
* @brief Save a single checkboxes value
*
* @param box checkbox to save
* @param name name for QSettings to store the value
*/
void SaveCheckboxValue(QCheckBox *box, const QString &name);
/**
* @brief Add a new checkbox to layout
*
* @param layout layout to add to
/**
* @brief Add a new checkbox to layout
*
* @param layout layout to add to
* @param label label for the checkbox
* @param settings QSettings name for default value
* @return newly created QCheckBox
* @param settings QSettings name for default value
* @return newly created QCheckBox
*/
QCheckBox* AddCheckbox(QVBoxLayout *layout,
const QString &label,
const QString &settings,
bool value);
/**
* @brief Convert bool to Qt::CheckState
*
* @param yes value to convert
* @return value converted to Qt::CheckState
/**
* @brief Convert bool to Qt::CheckState
*
* @param yes value to convert
* @return value converted to Qt::CheckState
*/
Qt::CheckState BoolToCheckState(bool yes);
/**
* @brief Converts Qt::CheckState to bool
*
* @param state Qt::CheckState to convert
* @return converted value
/**
* @brief Converts Qt::CheckState to bool
*
* @param state Qt::CheckState to convert
* @return converted value
*/
bool CheckStateToBool(Qt::CheckState state);
/**
* @brief Item model for mFileTree
*
/**
* @brief Item model for mFileTree
*
*/
QDirModel mModel;
/**
* @brief Filetree to select files from
*
/**
* @brief Filetree to select files from
*
*/
QTreeView *mFileTree;
/**
* @brief How many threads should cppcheck have
*
/**
* @brief How many threads should cppcheck have
*
*/
QLineEdit *mJobs;

View File

@ -46,9 +46,12 @@ void CheckThread::run()
while (!file.isEmpty())
{
qDebug() << tr("Checking file") << file;
qDebug() << "Checking file" << file;
mCppCheck.addFile(file.toStdString());
mCppCheck.check();
mCppCheck.clearFiles();
emit FileChecked(file);
file = mResult.GetNextFile();
}

View File

@ -57,6 +57,8 @@ signals:
*
*/
void Done();
void FileChecked(const QString &file);
protected:
ThreadResult &mResult;
/**

View File

@ -19,6 +19,7 @@
#include "resultstree.h"
#include <QDebug>
ResultsTree::ResultsTree(QSettings &settings) :
mSettings(settings)
@ -61,6 +62,8 @@ void ResultsTree::AddErrorItem(const QString &file,
fileitem = CreateItem(file);
}
qDebug() << "Adding error for file" << file << ". Message is" << message;
QList<QStandardItem*> list;
list << CreateItem(severity);
list << CreateItem(message);

View File

@ -51,7 +51,7 @@ void ResultsView::Clear()
void ResultsView::Progress(int value, int max)
{
qDebug() << tr("Progress:") << value << tr("/") << max;
qDebug() << "Progress:" << value << "/" << max;
mProgress->setMaximum(max);
mProgress->setValue(value);
}

View File

@ -27,9 +27,9 @@
#include "../src/errorlogger.h"
#include "resultstree.h"
/**
* @brief Widget to show cppcheck progressbar and result
*
/**
* @brief Widget to show cppcheck progressbar and result
*
*/
class ResultsView : public QWidget
{
@ -38,9 +38,9 @@ public:
ResultsView(QSettings &settings);
virtual ~ResultsView();
/**
* @brief Clear results
*
/**
* @brief Clear results
*
*/
void Clear();
public slots:
@ -54,15 +54,15 @@ public slots:
const QStringList &files,
const QList<int> &lines);
protected:
/**
* @brief Tree to show cppcheck's results
*
/**
* @brief Tree to show cppcheck's results
*
*/
ResultsTree *mTree;
/**
* @brief Progressbar to show cppcheck's progress
*
/**
* @brief Progressbar to show cppcheck's progress
*
*/
QProgressBar *mProgress;

View File

@ -21,21 +21,14 @@
#include "threadhandler.h"
#include <QDebug>
ThreadHandler::ThreadHandler() : mThreadCount(1), mRunningThreadCount(0)
ThreadHandler::ThreadHandler() : mRunningThreadCount(0)
{
SetThreadCount(1);
}
ThreadHandler::~ThreadHandler()
{
Stop();
for (int i = 0;i < mThreads.size();i++)
{
delete mThreads[i];
}
mThreads.clear();
RemoveThreads();
}
void ThreadHandler::ClearFiles()
@ -50,23 +43,23 @@ void ThreadHandler::SetFiles(const QStringList &files)
void ThreadHandler::Check(Settings settings)
{
if (mResults.GetFileCount() == 0 || mRunningThreadCount > 0)
if (mResults.GetFileCount() == 0 || mRunningThreadCount > 0 || settings._jobs <= 0)
{
qDebug() << tr("Can't start checking if there's no files to check or if check is in progress.");
qDebug() << "Can't start checking if there's no files to check or if check is in progress.";
return;
}
SetThreadCount(settings._jobs);
mRunningThreadCount = mThreadCount;
mRunningThreadCount = mThreads.size();
if (mResults.GetFileCount() < mRunningThreadCount)
{
mRunningThreadCount = mResults.GetFileCount();
}
qDebug() << tr("Starting") << mRunningThreadCount << tr("threads");
qDebug() << "Starting" << mRunningThreadCount << "threads";
qDebug() << mThreads.size();
for (int i = 0;i < mRunningThreadCount;i++)
{
mThreads[i]->Check(settings);
@ -76,41 +69,51 @@ void ThreadHandler::Check(Settings settings)
void ThreadHandler::SetThreadCount(const int count)
{
if (mRunningThreadCount > 0 ||
count == mThreadCount ||
count == mThreads.size() ||
count <= 0)
{
return;
}
qDebug() << tr("Setting thead count to") << count;
qDebug() << "Setting thead count to" << count;
mThreadCount = count;
//Remove unused old threads
if (mThreads.size() > count)
RemoveThreads();
//Create new threads
for (int i = mThreads.size();i < count;i++)
{
for (int i = count;i < mThreads.size();i++)
{
disconnect(mThreads.last(), SIGNAL(Done()),
this, SLOT(ThreadDone()));
delete mThreads.takeLast();
}
mThreads << new CheckThread(mResults);
connect(mThreads.last(), SIGNAL(Done()),
this, SLOT(ThreadDone()));
connect(mThreads.last(), SIGNAL(FileChecked(const QString &)),
&mResults, SLOT(FileChecked(const QString &)));
}
else
}
void ThreadHandler::RemoveThreads()
{
for (int i = 0;i < mThreads.size();i++)
{
//Create new threads
for (int i = mThreads.size();i < count;i++)
{
mThreads << new CheckThread(mResults);
connect(mThreads.last(), SIGNAL(Done()),
this, SLOT(ThreadDone()));
}
mThreads[i]->terminate();
disconnect(mThreads.last(), SIGNAL(Done()),
this, SLOT(ThreadDone()));
disconnect(mThreads.last(), SIGNAL(FileChecked(const QString &)),
&mResults, SLOT(FileChecked(const QString &)));
delete mThreads[i];
}
mThreads.clear();
}
void ThreadHandler::ThreadDone()
{
mRunningThreadCount--;
qDebug() << "Thread done" << mRunningThreadCount << "threads left";
if (mRunningThreadCount == 0)
{
emit Done();
@ -153,6 +156,6 @@ void ThreadHandler::LoadSettings(QSettings &settings)
void ThreadHandler::SaveSettings(QSettings &settings)
{
settings.setValue(tr("Check threads"), mThreadCount);
settings.setValue(tr("Check threads"), mThreads.size());
}

View File

@ -65,8 +65,8 @@ protected slots:
void Stop();
void ThreadDone();
protected:
void RemoveThreads();
ThreadResult mResults;
int mThreadCount;
QList<CheckThread *> mThreads;
int mRunningThreadCount;
private:

View File

@ -19,6 +19,7 @@
#include "threadresult.h"
#include <QDebug>
ThreadResult::ThreadResult() : mMaxProgress(0), mProgress(0)
{
@ -32,10 +33,16 @@ ThreadResult::~ThreadResult()
void ThreadResult::reportOut(const std::string &outmsg)
{
//emit CurrentFile(QString(outmsg.c_str()));
Q_UNUSED(outmsg);
}
void ThreadResult::FileChecked(const QString &file)
{
Q_UNUSED(file); //For later use maybe?
mProgress++;
emit Progress(mProgress, mMaxProgress);
}
void ThreadResult::reportErr(const ErrorLogger::ErrorMessage &msg)
{
QMutexLocker locker(&mutex);
@ -50,6 +57,7 @@ void ThreadResult::reportErr(const ErrorLogger::ErrorMessage &msg)
files << QString((*tok).file.c_str());
lines << (*tok).line;
}
qDebug() << "Got error for file" << QString(callStackToString(msg._callStack).c_str()) << QString(msg._msg.c_str());
emit Error(QString(callStackToString(msg._callStack).c_str()),
QString(msg._severity.c_str()),
@ -57,9 +65,7 @@ void ThreadResult::reportErr(const ErrorLogger::ErrorMessage &msg)
files,
lines);
mProgress++;
emit Progress(mProgress, mMaxProgress);
}
@ -83,8 +89,7 @@ void ThreadResult::reportStatus(unsigned int index, unsigned int max)
void ThreadResult::SetFiles(const QStringList &files)
{
//TODO we should check which of the strings in files is actually a path
//and add the path's contents
QMutexLocker locker(&mutex);
mFiles = files;
mProgress = 0;
mMaxProgress = files.size();
@ -92,11 +97,13 @@ void ThreadResult::SetFiles(const QStringList &files)
void ThreadResult::ClearFiles()
{
QMutexLocker locker(&mutex);
mFiles.clear();
}
int ThreadResult::GetFileCount()
{
QMutexLocker locker(&mutex);
return mFiles.size();
}

View File

@ -47,6 +47,8 @@ public:
void reportOut(const std::string &outmsg);
void reportErr(const ErrorLogger::ErrorMessage &msg);
void reportStatus(unsigned int index, unsigned int max);
public slots:
void FileChecked(const QString &file);
signals:
void Progress(int value, int max);
void Error(const QString &file,

View File

@ -42,8 +42,9 @@ public:
static void RecursiveAddFiles(std::vector<std::string> &filenames, const std::string &path, bool recursive);
static std::string simplifyPath(const char *originalPath);
static bool SameFileName(const char fname1[], const char fname2[]);
private:
static bool AcceptFile(const std::string &filename);
private:
};
#endif // #ifndef FILELISTER_H