-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
80 lines (62 loc) · 1.88 KB
/
main.cpp
File metadata and controls
80 lines (62 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <QApplication>
#include <QTreeView>
#include <QScreen>
#include <QMainWindow>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QKeyEvent>
#include <QSortFilterProxyModel>
#include "model.hpp"
class Tree : public QTreeView
{
public:
Tree(QLineEdit* filter, QWidget* parent = nullptr);
protected:
virtual void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) override;
virtual void keyPressEvent(QKeyEvent *event) override;
private:
FileSystemModel fsModel;
QSortFilterProxyModel filterModel;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow main;
QWidget central;
QVBoxLayout* vbox = new QVBoxLayout(¢ral);
QLineEdit filter;
vbox->addWidget(&filter);
Tree view(&filter);
vbox->addWidget(&view);
main.setCentralWidget(¢ral);
main.show();
return a.exec();
}
Tree::Tree(QLineEdit* filter, QWidget* parent)
: QTreeView(parent)
{
filterModel.setSourceModel(&fsModel);
setModel(&filterModel);
QObject::connect(filter, &QLineEdit::textChanged,
&filterModel, &QSortFilterProxyModel::setFilterWildcard);
for (int column = 0; column < fsModel.columnCount(); ++column)
resizeColumnToContents(column);
setEditTriggers(EditTrigger::DoubleClicked);
}
void Tree::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
{
QTreeView::selectionChanged(selected, deselected);
QItemSelection mapped = filterModel.mapSelectionToSource(selected);
for(const auto& m : mapped.indexes())
fsModel.load(m);
}
void Tree::keyPressEvent(QKeyEvent *event)
{
QTreeView::keyPressEvent(event);
if(event->key() == Qt::Key_Delete)
{
const QModelIndex index = selectionModel()->currentIndex();
QAbstractItemModel* m = model();
m->removeRow(index.row(), index.parent());
}
}