Skip to content

Commit fb248ed

Browse files
author
Daniel Marjamäki
committed
GUI: New experimental dialog for selecting files to check
1 parent be7691c commit fb248ed

4 files changed

Lines changed: 128 additions & 5 deletions

File tree

‎gui/gui.pro‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ HEADERS += aboutdialog.h \
7979
txtreport.h \
8080
xmlreport.h \
8181
xmlreportv1.h \
82-
xmlreportv2.h
82+
xmlreportv2.h \
83+
selectfilesdialog.h
8384

8485
SOURCES += aboutdialog.cpp \
8586
application.cpp \
@@ -110,7 +111,8 @@ SOURCES += aboutdialog.cpp \
110111
txtreport.cpp \
111112
xmlreport.cpp \
112113
xmlreportv1.cpp \
113-
xmlreportv2.cpp
114+
xmlreportv2.cpp \
115+
selectfilesdialog.cpp
114116

115117
win32 {
116118
DEFINES += _CRT_SECURE_NO_WARNINGS

‎gui/mainwindow.cpp‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "logview.h"
4242
#include "filelist.h"
4343
#include "showtypes.h"
44+
#include "selectfilesdialog.h"
4445

4546
static const QString OnlineHelpURL("http://cppcheck.sourceforge.net/manual.html");
4647

@@ -347,9 +348,13 @@ QStringList MainWindow::SelectFilesToCheck(QFileDialog::FileMode mode)
347348
// QFileDialog::getExistingDirectory() because they show native Windows
348349
// selection dialog which is a lot more usable than QT:s own dialog.
349350
if (mode == QFileDialog::ExistingFiles) {
350-
selected = QFileDialog::getOpenFileNames(this,
351-
tr("Select files to check"),
352-
mSettings->value(SETTINGS_CHECK_PATH, "").toString());
351+
SelectFilesDialog *dialog = new SelectFilesDialog;
352+
dialog->exec();
353+
/*
354+
selected = QFileDialog::getOpenFileNames(this,
355+
tr("Select files to check"),
356+
mSettings->value(SETTINGS_CHECK_PATH, "").toString());
357+
*/
353358
if (selected.isEmpty())
354359
mCurrentDirectory.clear();
355360
else {

‎gui/selectfilesdialog.cpp‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
2+
#include "selectfilesdialog.h"
3+
4+
#include <QTreeView>
5+
#include <QVBoxLayout>
6+
#include <QFileSystemModel>
7+
#include <QList>
8+
#include <QString>
9+
#include <QPushButton>
10+
11+
class SelectFilesModel : public QFileSystemModel {
12+
private:
13+
QList<QString> selected;
14+
QList<QString> unselected;
15+
16+
int getindex(const QList<QString> &s, const QString &filepath) const {
17+
for (int i = 0; i < s.size(); ++i) {
18+
if (filepath.startsWith(s[i]))
19+
return i;
20+
}
21+
return -1;
22+
}
23+
24+
public:
25+
SelectFilesModel() : QFileSystemModel() {
26+
QStringList f;
27+
f << "*.cpp";
28+
setNameFilters(f);
29+
setNameFilterDisables(false);
30+
}
31+
32+
Qt::ItemFlags flags(const QModelIndex& index) const {
33+
if (index.column() == 0)
34+
return QFileSystemModel::flags(index) | Qt::ItemIsUserCheckable;
35+
return QFileSystemModel::flags(index);
36+
}
37+
38+
QVariant data(const QModelIndex& index, int role=Qt::DisplayRole) const {
39+
if (role == Qt::CheckStateRole) {
40+
const QString filepath = filePath(index);
41+
if (getindex(selected,filepath) >= 0 &&
42+
getindex(unselected,filepath) == -1)
43+
return Qt::Checked;
44+
45+
return Qt::Unchecked;
46+
}
47+
return QFileSystemModel::data(index, role);
48+
}
49+
50+
bool setData(const QModelIndex& index, const QVariant& value, int role) {
51+
if (role == Qt::CheckStateRole) {
52+
const QString filepath = filePath(index);
53+
if (unselected.indexOf(filepath) != -1) {
54+
// remove unchecked path
55+
unselected.removeAll(filepath);
56+
} else if (selected.indexOf(filepath) != -1) {
57+
// remove checked path
58+
selected.removeAll(filepath);
59+
60+
// remove child unchecked paths
61+
int i;
62+
while ((i = getindex(unselected,filepath)) != -1) {
63+
unselected.removeAt(i);
64+
}
65+
} else {
66+
if (getindex(selected,filepath) == -1)
67+
selected.append(filepath);
68+
else
69+
unselected.append(filepath);
70+
}
71+
72+
if (rowCount(index) > 0)
73+
emit(dataChanged(index, index.child(rowCount(index)-1,0)));
74+
75+
return true;
76+
}
77+
return QFileSystemModel::setData(index, value, role);
78+
}
79+
};
80+
81+
82+
83+
84+
SelectFilesDialog::SelectFilesDialog() : QDialog()
85+
{
86+
setModal(true);
87+
88+
resize(300,400);
89+
90+
QTreeView *treeView = new QTreeView(this);
91+
treeView->setModel(new SelectFilesModel);
92+
for (int i = 1; i < 4; ++i)
93+
treeView->setColumnHidden(i, true);
94+
95+
QPushButton *cancel = new QPushButton("Cancel", this);
96+
connect(cancel,SIGNAL(clicked()),this,SLOT(accept()));
97+
98+
setLayout(new QVBoxLayout(this));
99+
layout()->addWidget(treeView);
100+
layout()->addWidget(cancel);
101+
}
102+
103+

‎gui/selectfilesdialog.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
#ifndef selectfilesdialogH
3+
#define selectfilesdialogH
4+
5+
#include <QDialog>
6+
7+
class SelectFilesDialog : public QDialog {
8+
public:
9+
SelectFilesDialog();
10+
};
11+
12+
#endif
13+

0 commit comments

Comments
 (0)