-
Notifications
You must be signed in to change notification settings - Fork 15
/
folderlist.cpp
290 lines (236 loc) · 7.14 KB
/
folderlist.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* Implementation of a list of folders */
#include <QApplication>
#include <QHeaderView>
#include <QKeyEvent>
#include <QMessageBox>
#include <QScrollBar>
#include <QStandardItemModel>
#include <QTimer>
#include "folderlist.h"
#include "foldersel.h"
#include "mainwidget.h"
Folderlist::Folderlist(Foldersel *foldersel, QWidget *parent)
: QTableView(parent), _foldersel(foldersel),
_parent(parent)
{
_parent = parent;
_main = nullptr; // not known yet
_model = new QStandardItemModel(1, 1, parent);
setModel(_model);
_valid = false;
_awaiting_user = false;
_searching = false;
horizontalHeader()->hide();
verticalHeader()->hide();
// Set the default row height to 0 so that it will be as small as possible
// while still making the text visible
verticalHeader()->setDefaultSectionSize(0);
setShowGrid(false);
setSelectionBehavior(QTableView::SelectRows);
setSelectionMode(QTableView::SingleSelection);
setEditTriggers(QTableView::NoEditTriggers);
hide();
connect(this, SIGNAL(keypressReceived(QKeyEvent *)),
this, SLOT(keypressFromFolderList(QKeyEvent *)));
connect(this, SIGNAL(selectItem(const QModelIndex&)),
this, SLOT(selectDir(const QModelIndex&)));
connect(_foldersel, SIGNAL(textChanged(const QString&)),
this, SLOT(foldersel_textChanged(const QString&)));
_timer = new QTimer (this);
connect (_timer, SIGNAL(timeout()), this, SLOT(checkFolders()));
_timer->start(200); // check 5 times a second
}
Folderlist::~Folderlist()
{
}
void Folderlist::setMainwidget(Mainwidget *main)
{
_main = main;
}
void Folderlist::keypressFromFolderList(QKeyEvent *evt)
{
QApplication::postEvent(_foldersel, evt);
}
void Folderlist::keyPressEvent(QKeyEvent *old)
{
bool send = true; // send to folderName field
bool pass_on = old->key() != Qt::Key_Tab; // pass on to QTableView
if (send) {
QKeyEvent *evt = new QKeyEvent(old->type(), old->key(), old->modifiers(),
old->text(), old->isAutoRepeat(),
old->count());
// Send the keypress to the folderName field
emit keypressReceived(evt);
}
if (pass_on)
QTableView::keyPressEvent(old);
}
void Folderlist::mousePressEvent(QMouseEvent *evt)
{
QTableView::mousePressEvent(evt);
// show the folder
QPoint point = evt->pos() + QPoint(horizontalScrollBar()->value(),
verticalScrollBar()->value());
QModelIndex ind = indexAt(point);
if (ind != QModelIndex())
emit selectItem(ind);
}
QModelIndex Folderlist::selected()
{
QModelIndexList sel = selectedIndexes();
if (sel.size())
return sel[0];
return QModelIndex();
}
void Folderlist::showFolders()
{
// Get a rectangle the same size as folderName but starting below it
QRect rect = _foldersel->geometry();
rect.translate(QPoint(0, rect.height()));
// extend it to the bottom of the dialog
rect.setBottom(_parent->geometry().bottom());
// place the folders list in the right place
move(rect.topLeft());
resize(rect.size());
horizontalHeader()->resizeSection(0, rect.width());
show();
}
void Folderlist::searchForFolders(const QString& match)
{
QStringList folders;
bool valid = false;
int row;
// We want at least three characters for a match
if (match.length() >= 3) {
folders = _main->findFolders(match, _path, _missing);
if (_path.isEmpty())
return;
valid = true;
}
// put the folder list into the model
_model->removeRows(0, _model->rowCount(QModelIndex()), QModelIndex());
for (row = 0; row < _missing.size(); row++) {
_model->insertRows(row, 1, QModelIndex());
_model->setData(_model->index(row, 0, QModelIndex()),
QString("<Add: %1>").arg(_missing[row]));
}
for (int i = 0; i < folders.size(); i++) {
_model->insertRows(row + i, 1, QModelIndex());
_model->setData(_model->index(row + i, 0, QModelIndex()),
folders[i]);
}
if (folders.size()) {
selectRow(0);
} else {
_model->insertRows(0, 1, QModelIndex());
_model->setData(_model->index(0, 0, QModelIndex()),
valid ? "<no match>" : "<too short>");
}
// make sure the column is wide enough
resizeColumnToContents(0);
showFolders();
if (folders.size())
setFocus();
_valid = folders.size() > 0;
}
void Folderlist::checkFolders()
{
// close the folder list if the focus is in anything other than the two
// fields dealing with the list of folders
if (!_awaiting_user && QApplication::focusWidget() != _foldersel &&
QApplication::focusWidget() != this && isVisible()) {
hide();
if (_parent->focusWidget() == this)
_foldersel->setFocus(Qt::OtherFocusReason);
}
// show the folder list if focus is in folderName
if (_valid && QApplication::focusWidget() == _foldersel &&
!_main->isScanning()) {
showFolders();
setFocus(Qt::OtherFocusReason);
}
}
bool Folderlist::createMissingDir(int item, QModelIndex& ind)
{
const QString& fname = _missing[item];
bool ok;
// Adding a directory
_awaiting_user = true;
ok = QMessageBox::question(
0,
tr("Confirmation -- Paperman"),
tr("Do you want to add directory %1").arg(fname),
QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok;
_awaiting_user = false;
if (!ok)
return false;
_main->newDir(_path + "/" + fname, ind);
// regenerate the folder list
_searching = true;
searchForFolders(_foldersel->text());
_searching = false;
return true;
}
void Folderlist::selectDir(const QModelIndex& target)
{
if (target.row() < _missing.size()) {
QModelIndex ind;
QString fname;
createMissingDir(target.row(), ind);
} else {
QString dir_path = _path + "/" + _model->data(target).toString();
_main->selectDir(dir_path);
}
}
bool Folderlist::getSelected(QModelIndex& ind, bool hiddenOk)
{
if (_awaiting_user)
return false;
bool ok = true;
ind = QModelIndex();
if (_valid && (hiddenOk || isVisible())) {
QModelIndex sel_ind = selected();
if (sel_ind != QModelIndex()) {
if (sel_ind.row() < _missing.size()) {
ok = createMissingDir(sel_ind.row(), ind);
} else {
QString dir_path;
dir_path = _path + "/" + _model->data(sel_ind).toString();
ind = _main->getDirIndex(dir_path);
}
}
}
return ok;
}
void Folderlist::foldersel_textChanged(const QString &text)
{
if (_searching) {
_next_search = text;
return;
}
QString match = text;
do {
_searching = true;
searchForFolders(match);
_searching = false;
match = _next_search;
_next_search = QString();
} while (match.size());
}
void Folderlist::scanStarting()
{
// close the folders list
if (!_awaiting_user)
hide();
}
Foldersel::Foldersel(QWidget* parent)
: QLineEdit(QString(), parent)
{
}
Foldersel::~Foldersel()
{
}
void Foldersel::focusOutEvent(QFocusEvent *)
{
// Do nothing here, so that any selected text remains selected
}