forked from Tulon/scantailor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStageListView.cpp
472 lines (394 loc) · 11.8 KB
/
StageListView.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
Scan Tailor - Interactive post-processing tool for scanned pages.
Copyright (C) Joseph Artsimovich <[email protected]>
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
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "StageListView.h"
#include "StageListView.h.moc"
#include "StageSequence.h"
#include "ChangedStateItemDelegate.h"
#include "SkinnedButton.h"
#include "BubbleAnimation.h"
#include <QAbstractTableModel>
#include <QStyledItemDelegate>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QScrollBar>
#include <QPainter>
#include <QPalette>
#include <QStyle>
#include <QColor>
#include <QTimer>
#include <QTimerEvent>
#include <QVariant>
#include <Qt>
#include <QDebug>
#include <boost/foreach.hpp>
#include <algorithm>
#include <memory>
#include <assert.h>
class StageListView::Model : public QAbstractTableModel
{
public:
Model(QObject* parent, IntrusivePtr<StageSequence> const& stages);
void updateBatchProcessingAnimation(
int selected_row, QPixmap const& animation_frame);
void disableBatchProcessingAnimation();
virtual int columnCount(QModelIndex const& parent) const;
virtual int rowCount(QModelIndex const& parent) const;
virtual QVariant data(QModelIndex const& index, int role) const;
private:
IntrusivePtr<StageSequence> m_ptrStages;
QPixmap m_curAnimationFrame;
int m_curSelectedRow;
};
class StageListView::LeftColDelegate :
public ChangedStateItemDelegate<QStyledItemDelegate>
{
public:
LeftColDelegate(StageListView* view);
virtual void paint(
QPainter* painter, QStyleOptionViewItem const& option,
QModelIndex const& index) const;
private:
typedef ChangedStateItemDelegate<QStyledItemDelegate> SuperClass;
StageListView* m_pView;
};
class StageListView::RightColDelegate :
public ChangedStateItemDelegate<QStyledItemDelegate>
{
public:
RightColDelegate(QObject* parent = 0);
virtual void paint(
QPainter* painter, QStyleOptionViewItem const& option,
QModelIndex const& index) const;
private:
typedef ChangedStateItemDelegate<QStyledItemDelegate> SuperClass;
};
StageListView::StageListView(QWidget* parent)
: QTableView(parent),
m_sizeHint(QTableView::sizeHint()),
m_pModel(0),
m_pFirstColDelegate(new LeftColDelegate(this)),
m_pSecondColDelegate(new RightColDelegate(this)),
m_curBatchAnimationFrame(0),
m_timerId(0),
m_batchProcessingPossible(false),
m_batchProcessingInProgress(false)
{
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// Prevent current item visualization. Not to be confused
// with selected items.
m_pFirstColDelegate->flagsForceDisabled(QStyle::State_HasFocus);
m_pSecondColDelegate->flagsForceDisabled(QStyle::State_HasFocus);
setItemDelegateForColumn(0, m_pFirstColDelegate);
setItemDelegateForColumn(1, m_pSecondColDelegate);
QHeaderView* h_header = horizontalHeader();
h_header->setResizeMode(QHeaderView::Stretch);
h_header->hide();
QHeaderView* v_header = verticalHeader();
v_header->setResizeMode(QHeaderView::ResizeToContents);
v_header->setMovable(false);
m_pLaunchBtn = new SkinnedButton(
":/icons/play-small.png",
":/icons/play-small-hovered.png",
":/icons/play-small-pressed.png",
viewport()
);
m_pLaunchBtn->setStatusTip(tr("Launch batch processing"));
m_pLaunchBtn->hide();
connect(
m_pLaunchBtn, SIGNAL(clicked()),
this, SIGNAL(launchBatchProcessing())
);
connect(
verticalScrollBar(), SIGNAL(rangeChanged(int, int)),
this, SLOT(ensureSelectedRowVisible()), Qt::QueuedConnection
);
}
StageListView::~StageListView()
{
}
void
StageListView::setStages(IntrusivePtr<StageSequence> const& stages)
{
if (QAbstractItemModel* m = model()) {
// Q*View classes don't own their models.
m->deleteLater();
}
m_pModel = new Model(this, stages);
setModel(m_pModel);
QHeaderView* h_header = horizontalHeader();
QHeaderView* v_header = verticalHeader();
h_header->setResizeMode(0, QHeaderView::Stretch);
h_header->setResizeMode(1, QHeaderView::Fixed);
if (v_header->count() != 0) {
// Make the cells in the last column square.
int const square_side = v_header->sectionSize(0);
h_header->resizeSection(1, square_side);
int const reduced_square_side = std::max(1, square_side - 6);
createBatchAnimationSequence(reduced_square_side);
} else {
// Just to avoid special cases elsewhere.
createBatchAnimationSequence(1);
}
m_curBatchAnimationFrame = 0;
updateRowSpans();
// Limit the vertical size to make it just enough to get
// rid of the scrollbars, but not more.
int height = verticalHeader()->length();
height += this->height() - viewport()->height();
m_sizeHint.setHeight(height);
QSizePolicy sp(QSizePolicy::Preferred, QSizePolicy::Maximum);
sp.setVerticalStretch(1);
setSizePolicy(sp);
updateGeometry();
}
void
StageListView::setBatchProcessingPossible(bool const possible)
{
if (m_batchProcessingPossible == possible) {
return;
}
m_batchProcessingPossible = possible;
if (possible) {
placeLaunchButton(selectedRow());
} else {
removeLaunchButton(selectedRow());
}
}
void
StageListView::setBatchProcessingInProgress(bool const in_progress)
{
if (m_batchProcessingInProgress == in_progress) {
return;
}
m_batchProcessingInProgress = in_progress;
if (in_progress) {
removeLaunchButton(selectedRow());
updateRowSpans(); // Join columns.
// Some styles (Oxygen) visually separate items in a selected row.
// We really don't want that, so we pretend the items are not selected.
// Same goes for hovered items.
m_pFirstColDelegate->flagsForceDisabled(QStyle::State_Selected|QStyle::State_MouseOver);
m_pSecondColDelegate->flagsForceDisabled(QStyle::State_Selected|QStyle::State_MouseOver);
initiateBatchAnimationFrameRendering();
m_timerId = startTimer(180);
} else {
updateRowSpans(); // Separate columns.
placeLaunchButton(selectedRow());
m_pFirstColDelegate->removeChanges(QStyle::State_Selected|QStyle::State_MouseOver);
m_pSecondColDelegate->removeChanges(QStyle::State_Selected|QStyle::State_MouseOver);
if (m_pModel) {
m_pModel->disableBatchProcessingAnimation();
}
killTimer(m_timerId);
m_timerId = 0;
}
}
void
StageListView::timerEvent(QTimerEvent* event)
{
if (event->timerId() != m_timerId) {
QTableView::timerEvent(event);
return;
}
initiateBatchAnimationFrameRendering();
}
void
StageListView::initiateBatchAnimationFrameRendering()
{
if (!m_pModel || !m_batchProcessingInProgress) {
return;
}
int const selected_row = selectedRow();
if (selected_row == -1) {
return;
}
m_pModel->updateBatchProcessingAnimation(
selected_row, m_batchAnimationPixmaps[m_curBatchAnimationFrame]
);
if (++m_curBatchAnimationFrame == (int)m_batchAnimationPixmaps.size()) {
m_curBatchAnimationFrame = 0;
}
}
void
StageListView::selectionChanged(
QItemSelection const& selected,
QItemSelection const& deselected)
{
// Call the base version.
QTableView::selectionChanged(selected, deselected);
if (!deselected.isEmpty()) {
removeLaunchButton(deselected.front().topLeft().row());
}
if (!selected.isEmpty()) {
placeLaunchButton(selected.front().topLeft().row());
}
}
void
StageListView::ensureSelectedRowVisible()
{
// This loop won't run more than one iteration.
BOOST_FOREACH(QModelIndex const& idx, selectionModel()->selectedRows(0)) {
scrollTo(idx, EnsureVisible);
}
}
void
StageListView::removeLaunchButton(int const row)
{
if (row == -1) {
return;
}
m_pLaunchBtn->hide();
}
void
StageListView::placeLaunchButton(int row)
{
if (row == -1) {
return;
}
QModelIndex const idx(m_pModel->index(row, 0));
QRect button_geometry(visualRect(idx));
// Place it to the right (assuming height is less than width).
button_geometry.setLeft(button_geometry.right() + 1 - button_geometry.height());
m_pLaunchBtn->setGeometry(button_geometry);
m_pLaunchBtn->show();
}
void
StageListView::createBatchAnimationSequence(int const square_side)
{
int const num_frames = 8;
m_batchAnimationPixmaps.resize(num_frames);
QColor const head_color(palette().color(QPalette::Window).darker(200));
QColor const tail_color(palette().color(QPalette::Window).darker(130));
BubbleAnimation animation(num_frames);
for (int i = 0; i < num_frames; ++i) {
QPixmap& pixmap = m_batchAnimationPixmaps[i];
if (pixmap.width() != square_side || pixmap.height() != square_side) {
pixmap = QPixmap(square_side, square_side);
}
pixmap.fill(Qt::transparent);
animation.nextFrame(head_color, tail_color, &pixmap);
}
}
void
StageListView::updateRowSpans()
{
if (!m_pModel) {
return;
}
int const count = m_pModel->rowCount(QModelIndex());
for (int i = 0; i < count; ++i) {
setSpan(i, 0, 1, m_batchProcessingInProgress ? 1 : 2);
}
}
int
StageListView::selectedRow() const
{
QModelIndexList const selection(selectionModel()->selectedRows(0));
if (selection.empty()) {
return - 1;
}
return selection.front().row();
}
/*========================= StageListView::Model ======================*/
StageListView::Model::Model(
QObject* parent, IntrusivePtr<StageSequence> const& stages)
: QAbstractTableModel(parent),
m_ptrStages(stages),
m_curSelectedRow(0)
{
assert(m_ptrStages.get());
}
void
StageListView::Model::updateBatchProcessingAnimation(
int const selected_row, QPixmap const& animation_frame)
{
int const max_row = std::max(selected_row, m_curSelectedRow);
m_curSelectedRow = selected_row;
m_curAnimationFrame = animation_frame;
emit dataChanged(index(0, 1), index(max_row, 1));
}
void
StageListView::Model::disableBatchProcessingAnimation()
{
m_curAnimationFrame = QPixmap();
emit dataChanged(index(0, 1), index(m_curSelectedRow, 1));
}
int
StageListView::Model::columnCount(QModelIndex const& parent) const
{
return 2;
}
int
StageListView::Model::rowCount(QModelIndex const& parent) const
{
return m_ptrStages->count();
}
QVariant
StageListView::Model::data(QModelIndex const& index, int const role) const
{
if (role == Qt::DisplayRole) {
if (index.column() == 0) {
return m_ptrStages->filterAt(index.row())->getName();
}
}
if (role == Qt::UserRole) {
if (index.column() == 1) {
if (index.row() <= m_curSelectedRow) {
return m_curAnimationFrame;
}
}
}
return QVariant();
}
/*================= StageListView::LeftColDelegate ===================*/
StageListView::LeftColDelegate::LeftColDelegate(StageListView* view)
: SuperClass(view),
m_pView(view)
{
}
void
StageListView::LeftColDelegate::paint(
QPainter* painter, QStyleOptionViewItem const& option,
QModelIndex const& index) const
{
SuperClass::paint(painter, option, index);
if (index.row() == m_pView->selectedRow() && m_pView->m_pLaunchBtn->isVisible()) {
QRect button_geometry(option.rect);
// Place it to the right (assuming height is less than width).
button_geometry.setLeft(button_geometry.right() + 1 - button_geometry.height());
m_pView->m_pLaunchBtn->setGeometry(button_geometry);
}
}
/*================= StageListView::RightColDelegate ===================*/
StageListView::RightColDelegate::RightColDelegate(QObject* parent)
: SuperClass(parent)
{
}
void
StageListView::RightColDelegate::paint(
QPainter* painter, QStyleOptionViewItem const& option,
QModelIndex const& index) const
{
SuperClass::paint(painter, option, index);
QVariant const var(index.data(Qt::UserRole));
if (!var.isNull()) {
QPixmap const pixmap(var.value<QPixmap>());
if (!pixmap.isNull()) {
QRect r(pixmap.rect());
r.moveCenter(option.rect.center());
painter->drawPixmap(r, pixmap);
}
}
}