-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarkersWidget.cpp
482 lines (409 loc) · 10.9 KB
/
MarkersWidget.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
473
474
475
476
477
478
479
480
481
482
#include <QObject>
#include <QFileDialog>
#include <QMessageBox>
#include <QMenu>
#include <vector>
#include <algorithm>
#include "MarkersWidget.h"
/*! \brief Create and setup the markers widget
*
* Create and setup the markers widget
*
* @param parent parent
* @param mainwin pointer to the mainwindow so we can use s&s
* @param markersList pointer to the markerslist component of the UI
*/
MarkersWidget::MarkersWidget(
QWidget *parent,
QWidget *mainwin,
QTableWidget *markersList
) : QWidget(parent), _markersList(markersList)
{
_inputFile = "";
_markerStarted = false;
_inputFileModified = false;
// S&S to mainwin
connect(this, SIGNAL(jumpToFrame(qint64)), mainwin, SLOT(jumpToFrame(qint64)));
connect(this, SIGNAL(startBtnToggle(bool)), mainwin, SLOT(changeStartEndBtn(bool)));
connect(this, SIGNAL(startBtnToggle(bool)), mainwin, SLOT(changeStartEndBtn(bool)));
}
/*! \brief Destroyer
*
* Destroyer
*/
MarkersWidget::~MarkersWidget()
{}
/***************************************
*********** EXTERNALS ************
***************************************/
/*! \brief Start/end a marker
*
* Start/end a marker.
*
* @param endVal end marker value
* @param startVal start marker value, skipped if -1
*/
void MarkersWidget::endAndStartMarker(const qint64 endVal, const qint64 startVal)
{
// already started marker present?
if (_markerStarted) {
if (_markers[_currMarker]._start >= endVal) {
QMessageBox::critical(NULL, "Error", "Marker range is not valid: start must be > of end value");
return;
}
endMarker(endVal);
sort();
checkMarkersOverlaps();
clearUIList();
printListToUI();
// start a new marker?
if (startVal != -1) {
startMarker(startVal);
}
}
else {
startMarker(startVal);
}
emit startBtnToggle(_markerStarted);
}
/*! \brief Custom context menu
*
* Custom context menu. This function overwrite the original context menu.
*
* @param globalPos click position
*/
void MarkersWidget::showContextMenu(const QPoint& globalPos)
{
QMenu myMenu;
myMenu.addAction("Remove");
myMenu.addAction("Jump to frame 'From'");
myMenu.addAction("Jump to frame 'To'");
QList<QTableWidgetItem*> sel = _markersList->selectedItems();
if (sel.length() == 0) {// no sel
return;
}
QAction* selectedAction = myMenu.exec(globalPos);
// operations
if (selectedAction) {
if (selectedAction->text() == "Remove") {
int row = sel[0]->row();
removeMarker(row);
}
else if (selectedAction->text() == "Jump to frame 'From'") {
qint64 frameNum = sel[0]->text().toInt();
emit jumpToFrame(frameNum);
}
else if (selectedAction->text() == "Jump to frame 'To'") {
qint64 frameNum = sel[1]->text().toInt();
emit jumpToFrame(frameNum);
}
}
}
/*! \brief Change a marker value
*
* Change a marker value.
*
* @param row row index
* @param col column index (0=start, 1=end)
* @param val value to set
*/
void MarkersWidget::markerChanged(const int row, const int col, const qint64 val)
{
_inputFileModified = true;
// 1 = end, 0 = start
col ? _markers[row]._end = val : _markers[row]._start = val;
sort();
checkMarkersOverlaps();
clearUIList();
printListToUI();
}
/***************************************
******** MARKERS ACTIONS *********
***************************************/
/*! \brief Start a marker
*
* Start a marker
*
* @param startVal start marker value
*/
void MarkersWidget::startMarker(const qint64 startVal)
{
_currMarker = _markers.size();
// update internal list
_markers.push_back(*new Marker(startVal));
// update UI list
QTableWidgetItem *start = new QTableWidgetItem(QObject::tr("%1").arg(startVal));
_markersList->insertRow(_currMarker);
_markersList->setItem(_currMarker, 0, start);
_inputFileModified = true;
_markerStarted = true;
}
/*! \brief End a marker
*
* End a marker
*
* @param endVal end marker value
*/
void MarkersWidget::endMarker(const qint64 endVal)
{
// update internal list
_markers[_currMarker]._end = endVal;
// update UI list
QTableWidgetItem *end = new QTableWidgetItem(QObject::tr("%1").arg(endVal));
_markersList->setItem(_currMarker, 1, end);
_inputFileModified = true;
_markerStarted = false;
}
/*! \brief Add a marker with given start and end value
*
* Add a marker with given start and end value
*
* @param startVal start marker value
* @param endVal end marker value
*/
void MarkersWidget::addMarker(const qint64 startVal, const qint64 endVal)
{
// update internal list
_markers.push_back(*new Marker(startVal, endVal));
// update UI list
addMarkerToUIList(QString::number(startVal), QString::number(endVal));
}
/*! \brief Add a marker to the list
*
* Add a marker to the list
*
* @param startVal start marker value
* @param endVal end marker value
* @param overlap overlap with another marker
*/
void MarkersWidget::addMarkerToUIList(const QString startVal, const QString endVal, const bool overlap)
{
int pos = _markersList->rowCount();
QTableWidgetItem *start = new QTableWidgetItem(QObject::tr("%1").arg(startVal));
QTableWidgetItem *end = new QTableWidgetItem(QObject::tr("%1").arg(endVal));
if (overlap) {
start->setBackgroundColor(QColor(186, 33, 33, 255));
end->setBackgroundColor(QColor(186, 33, 33, 255));
}
else {
start->setBackgroundColor(QColor(23, 23, 23, 255));
end->setBackgroundColor(QColor(23, 23, 23, 255));
}
_markersList->insertRow(pos);
_markersList->setItem(pos, 0, start);
_markersList->setItem(pos, 1, end);
}
/*! \brief Remove a marker by row index
*
* Remove a marker by row index
*
* @param row row index
*/
void MarkersWidget::removeMarker(const int row)
{
_inputFileModified = true;
// if the curr marker is "new" we have to reset variables
if (_currMarker == row && _markerStarted) {
_markerStarted = false;
// _currMarker = NULL;
emit startBtnToggle(_markerStarted);
}
// update internal list
_markers.erase(_markers.begin() + row, _markers.begin() + row + 1);
// update UI list
_markersList->removeRow(row);
}
/***************************************
********** I/O METHODS ***********
***************************************/
/*! \brief Load markers from a file
*
* Load markers from a file
*
* @return file's name if successful, NULL if not
*/
QString MarkersWidget::loadFile()
{
QString temp = _inputFile;
_inputFile = QFileDialog::getOpenFileName(NULL, QObject::tr("Open a markers file"), "", QObject::tr("Text Files (*)"));
if (_inputFile=="") {
_inputFile = temp;
return "";
}
// open file
QFile file(_inputFile);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::critical(NULL, "Error", "Cannot read from the file! Check folders and files permissions.");
_inputFile = temp;
return "";
}
_markerStarted = false;
clearListAndUI();
// read line x line and fill the internal vector
while (!file.atEnd()) {
QString line = QString(file.readLine());
if (line != "") { // avoid last empty line of the stream
line.truncate(line.length() - 1); // remove '\n'
QStringList list = line.split(" ");
// check errors
if (list.length() != 2) {
QMessageBox::critical(NULL, "Error", "Error while parsing the file: expected a line with 2 markers.");
_inputFile = "";
return "";
}
bool ok1, ok2;
qint64 start = list[0].toInt(&ok1);
qint64 end = list[1].toInt(&ok2);
// check errors
if (!ok1 || !ok2) {
QMessageBox::critical(NULL, "Error", "Error while parsing the file: found a non numeric marker.");
_inputFile = "";
return "";
}
// check errors
if (start >= end) {
QMessageBox::critical(NULL, "Error", "Error while parsing the file: found a marker with start >= end.");
_inputFile = "";
return "";
}
_markers.push_back(*new Marker(start, end));
}
}
sort();
checkMarkersOverlaps();
printListToUI();
_inputFileModified = false;
return _inputFile;
}
/*! \brief Save markers to a file
*
* Save markers to a file
*
* @return file's name if successful, NULL if not
*/
QString MarkersWidget::saveFile()
{
_markerStarted = false;
// new markers file?
if (_inputFile == "") {
_inputFile = QFileDialog::getSaveFileName(NULL, QObject::tr("Save markers"), "", QObject::tr("Text files (*.txt)"));
}
// open file
QFile file(_inputFile);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::critical(NULL, "Error", "Cannot create the file or write to it! Check folders and files permissions.");
return false;
}
// write line x line
for (auto m : _markers) {
file.write(QString("%1 %2\n").arg(m._start).arg(m._end).toLatin1());
}
_inputFileModified = false;
return _inputFile;
}
/*! \brief Create a new markers file
*
* Create a new markers file
*
* @return success or not
*/
bool MarkersWidget::newFile()
{
_inputFileModified = false;
_inputFile = "";
clearListAndUI();
return true;
}
/***************************************
************ HELPERS *************
***************************************/
/*! \brief Check overlaps between markers's ranges
*
* Set overlap=true if there are overlaps between markers's ranges
*/
void MarkersWidget::checkMarkersOverlaps()
{
int len = _markers.size();
int flag_n = -1;
for (int i = 0; i < len - 1; ++i) {
bool out = false;
int j = i + 1;
while (!out && (j < len)) {
if (_markers[i]._end < _markers[j]._start) { // if they are sorted i don't have to check forward
out = true;
}
else { // _markers[i]._end >= _markers[j]._start
_markers[i]._overlap = _markers[j]._overlap = true;
if (j > flag_n)
flag_n = j;
}
++j;
}
// no overlap found, i have to use this flag 'cause i can't rely
// on .overlap (this could be true or false from before)
if (flag_n < i) {
_markers[i]._overlap = false;
}
}
// no overlap found on the last element? reset
if (flag_n < len - 1) {
_markers[len - 1]._overlap = false;
}
}
/*! \brief Write all markers onto the UI list
*
* Write all markers onto the UI list
*/
void MarkersWidget::printListToUI()
{
for (auto m : _markers) {
addMarkerToUIList(QString::number(m._start), QString::number(m._end), m._overlap);
}
}
/*! \brief Sort markers by start value first and end value after
*
* Sort markers by start value first and end value after
*/
void MarkersWidget::sort()
{
std::sort(_markers.begin(), _markers.end());
}
/*! \brief Clear markers list + UI list
*
* Clear markers list + UI list
*/
void MarkersWidget::clearListAndUI()
{
clearUIList();
_markers.clear();
}
/*! \brief Remove all markers from the UI list
*
* Remove all markers from the UI list
*/
void MarkersWidget::clearUIList()
{
while (_markersList->rowCount() > 0)
_markersList->removeRow(_markersList->rowCount() - 1);
}
/***************************************
************ GETTERS *************
***************************************/
/*! \brief Get input file string
*
* Get input file string
*
* @return input file string
*/
QString MarkersWidget::getInputFile() {
return _inputFile;
}
/*! \brief Input file has been modified but not saved?
*
* Input file has been modified but not saved?
*
* @return yes or no
*/
bool MarkersWidget::fileNotSaved() {
return _inputFileModified;
}