-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.cpp
511 lines (441 loc) · 15 KB
/
window.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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#include <deque>
#include <iostream>
#include <QtGui>
#include <QDir>
//#include <QxtSpanSlider>
#include <QKeySequence>
#include <QFileDialog>
#include <QFileInfo>
#include <QFileSystemWatcher>
#include <QSignalMapper>
#include <QDebug>
#include <QTimer>
#include <QMap>
#include <QColorDialog>
#include "glwidget.h"
#include "window.h"
#include "ui_window.h"
#include "ui_preferences.h"
#include "OMFImport.h"
#include "OMFHeader.h"
#include "OMFContainer.h"
#include "qxtspanslider.h"
struct OMFImport;
Window::Window(int argc, char *argv[]) :
QMainWindow(NULL),
ui(new Ui::Window)
{
ui->setupUi(this);
// Cache size
cacheSize = 50;
cachePos = 0;
prefs = new Preferences(this);
about = new AboutDialog(this);
connect(prefs, SIGNAL(finished(int)), this, SLOT(updatePrefs()));
initSlider(ui->xSlider);
initSlider(ui->ySlider);
initSlider(ui->zSlider);
initSpanSlider(ui->xSpanSlider);
initSpanSlider(ui->ySpanSlider);
initSpanSlider(ui->zSpanSlider);
// Rotation
connect(ui->xSlider, SIGNAL(valueChanged(int)), ui->viewport, SLOT(setXRotation(int)));
connect(ui->viewport, SIGNAL(xRotationChanged(int)), ui->xSlider, SLOT(setValue(int)));
connect(ui->ySlider, SIGNAL(valueChanged(int)), ui->viewport, SLOT(setYRotation(int)));
connect(ui->viewport, SIGNAL(yRotationChanged(int)), ui->ySlider, SLOT(setValue(int)));
connect(ui->zSlider, SIGNAL(valueChanged(int)), ui->viewport, SLOT(setZRotation(int)));
connect(ui->viewport, SIGNAL(zRotationChanged(int)), ui->zSlider, SLOT(setValue(int)));
// Slicing
connect(ui->xSpanSlider, SIGNAL(lowerValueChanged(int)), ui->viewport, SLOT(setXSliceLow(int)));
connect(ui->xSpanSlider, SIGNAL(upperValueChanged(int)), ui->viewport, SLOT(setXSliceHigh(int)));
connect(ui->ySpanSlider, SIGNAL(lowerValueChanged(int)), ui->viewport, SLOT(setYSliceLow(int)));
connect(ui->ySpanSlider, SIGNAL(upperValueChanged(int)), ui->viewport, SLOT(setYSliceHigh(int)));
connect(ui->zSpanSlider, SIGNAL(lowerValueChanged(int)), ui->viewport, SLOT(setZSliceLow(int)));
connect(ui->zSpanSlider, SIGNAL(upperValueChanged(int)), ui->viewport, SLOT(setZSliceHigh(int)));
ui->animSlider->setRange(0, 10);
ui->animSlider->setSingleStep(1);
ui->animSlider->setPageStep(10);
ui->animSlider->setTickInterval(2);
ui->animSlider->setTickPosition(QSlider::TicksRight);
ui->animSlider->setEnabled(false);
// Actions
connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(openAbout()));
connect(ui->actionPreferences, SIGNAL(triggered()), this, SLOT(openSettings()));
connect(ui->actionFiles, SIGNAL(triggered()), this, SLOT(openFiles()));
connect(ui->actionDir, SIGNAL(triggered()), this, SLOT(openDir()));
connect(ui->actionCubes, SIGNAL(triggered()), this, SLOT(toggleDisplay()));
connect(ui->actionCones, SIGNAL(triggered()), this, SLOT(toggleDisplay()));
connect(ui->actionVectors, SIGNAL(triggered()), this, SLOT(toggleDisplay()));
displayType = new QActionGroup(this);
displayType->addAction(ui->actionCubes);
displayType->addAction(ui->actionCones);
displayType->addAction(ui->actionVectors);
ui->actionCubes->setChecked(true);
signalMapper = new QSignalMapper(this);
signalMapper->setMapping (ui->actionFollow, "") ;
connect (signalMapper, SIGNAL(mapped(QString)), this, SLOT(watchDir(QString))) ;
connect(ui->actionFollow, SIGNAL(triggered()), signalMapper, SLOT(map()));
ui->xSlider->setValue(15 * 16);
ui->ySlider->setValue(345 * 16);
ui->zSlider->setValue(0 * 16);
setWindowTitle(tr("MuView 0.9"));
// Data, don't connect until we are ready (probably still not ready here)...
connect(ui->animSlider, SIGNAL(valueChanged(int)), this, SLOT(updateDisplayData(int)));
// Load files from command line if supplied
if (argc > 1) {
QStringList rawList;
for (int i=1; i<argc; i++) {
rawList << argv[i];
}
if (rawList.contains(QString("-w"))) {
if (rawList.indexOf("-w") < (rawList.length() - 1)) {
watchDir(rawList[rawList.indexOf("-w")+1]);
}
} else {
QStringList allLoadedFiles;
foreach (QString item, rawList)
{
QFileInfo info(item);
if (!info.exists()) {
std::cout << "File " << item.toStdString() << " does not exist" << std::endl;
} else {
// Push our new content...
if (info.isDir()) {
QDir chosenDir(item);
dirString = chosenDir.path()+"/";
QStringList filters;
filters << "*.omf" << "*.ovf";
chosenDir.setNameFilters(filters);
QStringList files = chosenDir.entryList();
foreach (QString file, files)
{
filenames << (dirString+file);
displayNames << (dirString+item);
//omfCache.push_back(readOMF((dirString+file).toStdString(), tempHeader));
}
} else {
// just a normal file
filenames << (dirString+item);
displayNames << (dirString+item);
//omfCache.push_back(readOMF(item.toStdString(), tempHeader));
}
}
}
processFilenames();
gotoFrontOfCache();
adjustAnimSlider(false); // Refresh the animation bar
}
}
}
void Window::initSpanSlider(QxtSpanSlider *slider)
{
// QxtSpanSlider *spanSlider = new QxtSpanSlider(Qt::Vertical);
slider->setRange(0 *16, 100 * 16);
slider->setSingleStep(16);
slider->setPageStep(15 * 16);
slider->setTickInterval(15 * 16);
slider->setTickPosition(QSlider::TicksRight);
slider->setHandleMovementMode(QxtSpanSlider::NoOverlapping);
slider->setLowerValue(0*16);
slider->setUpperValue(100*16);
}
void Window::initSlider(QSlider *slider)
{
// QSlider *slider = new QSlider(Qt::Vertical);
slider->setRange(0, 360 * 16);
slider->setSingleStep(16);
slider->setPageStep(15 * 16);
slider->setTickInterval(15 * 16);
slider->setTickPosition(QSlider::TicksRight);
}
void Window::adjustAnimSlider(bool back)
{
int numFiles = filenames.size();
//qDebug() << QString("Updating Animation Slider to size") << numFiles;
if (numFiles > 1) {
ui->animSlider->setRange(0, numFiles-1);
ui->animSlider->setSingleStep(1);
ui->animSlider->setPageStep(10);
ui->animSlider->setTickInterval(2);
ui->animSlider->setTickPosition(QSlider::TicksRight);
ui->animSlider->setEnabled(true);
if (back) {
ui->animSlider->setSliderPosition(numFiles-1);
} else {
ui->animSlider->setSliderPosition(0);
}
} else {
ui->animSlider->setEnabled(false);
}
//qDebug() << QString("Updated Animation Slider to size") << numFiles;
}
void Window::keyPressEvent(QKeyEvent *e)
{
if (e->modifiers() == Qt::CTRL) {
// Close on Ctrl-Q or Ctrl-W
if (e->key() == Qt::Key_Q || e->key() == Qt::Key_W )
close();
} else if (e->modifiers() == Qt::SHIFT) {
if (e->key() == Qt::Key_Q || e->key() == Qt::Key_W )
close();
} else {
if (e->key() == Qt::Key_Escape)
close();
else
QWidget::keyPressEvent(e);
}
}
void Window::updatePrefs() {
// qDebug() << prefs->getBackgroundColor().name();
ui->viewport->setBackgroundColor(prefs->getBackgroundColor());
}
void Window::openSettings()
{
prefs->exec();
}
void Window::openAbout()
{
about->exec();
}
void Window::processFilenames() {
// Looping over files
for (int loadPos=0; loadPos<cacheSize && loadPos<filenames.size(); loadPos++) {
header_ptr header = header_ptr(new OMFHeader());
omfHeaderCache.push_back(header);
omfCache.push_back(readOMF((filenames[loadPos]).toStdString(), *header));
}
}
void Window::gotoFrontOfCache() {
ui->viewport->updateHeader(omfHeaderCache.front(), omfCache.front());
ui->statusbar->showMessage(filenames.front());
// ui->viewport->updateTopOverlay(filenames.front());
ui->viewport->updateData(omfCache.front());
cachePos = 0;
adjustAnimSlider(false); // Go to end of slider
}
void Window::gotoBackOfCache() {
ui->viewport->updateHeader(omfHeaderCache.back(), omfCache.back());
ui->statusbar->showMessage(filenames.back());
ui->viewport->updateData(omfCache.back());
cachePos = filenames.size()-1;
adjustAnimSlider(true); // Go to start of slider
}
void Window::clearHeaderCache() {
while (!omfHeaderCache.empty()) {
omfHeaderCache.pop_back();
}
}
void Window::clearOMFCache() {
while (!omfCache.empty()) {
omfCache.pop_back();
}
}
void Window::openFiles()
{
QString fileName;
fileName = QFileDialog::getOpenFileName(this,
tr("Open File"), QDir::currentPath(),
tr("OVF Files (*.omf *.ovf)"));
if (fileName != "")
{
filenames.clear();
filenames.push_back(fileName);
clearOMFCache();
clearHeaderCache();
processFilenames();
gotoFrontOfCache();
}
}
void Window::updateWatchedFiles(const QString& str) {
// Look at all of the files in the directory
// and add those which are not in the list of
// original filenames
// filenames contains the list of loaded files
// watchedFiles is a map of files to load and their modification timestamps
// When the timestamps in wathcedFiles stop changing we actually
// push the relevant files into the OMF cache.
QDir chosenDir(str);
QString dirString = chosenDir.path()+"/";
QStringList filters;
filters << "*.omf" << "*.ovf";
chosenDir.setNameFilters(filters);
QStringList dirFiles = chosenDir.entryList();
OMFHeader tempHeader = OMFHeader();
// compare to existing list of files
bool changed = false;
foreach(QString dirFile, dirFiles)
{
//qDebug() << QString("Changed File!") << dirFile;
if (!filenames.contains(dirString + dirFile)) {
// Haven't been loaded
QString fullPath = dirString + dirFile;
QFileInfo info(fullPath);
//qDebug() << QString("Not in filenames: ") << (dirFile);
if (!watchedFiles.contains(fullPath)) {
// Not on the watch list yet
watchedFiles.insert(fullPath, info.lastModified());
//qDebug() << QString("Inserted: ") << (dirFile);
} else {
// on the watch list
if (info.lastModified() == watchedFiles[fullPath]) {
// File size has stabalized
//qDebug() << QString("Stable, pushed") << dirFile;
filenames.append(fullPath);
displayNames.append(dirFile);
//omfCache.push_back(readOMF(fullPath.toStdString(), tempHeader));
changed = true;
} else {
// File still changing
//qDebug() << QSt //qDebug() << QString("Pushing Back") << filenames[loadPos];ring("Unstable") << dirFile;
watchedFiles[fullPath] = info.lastModified();
}
}
}
}
if (changed) {
clearOMFCache();
clearHeaderCache();
processFilenames();
gotoBackOfCache();
}
}
void Window::updateDisplayData(int index)
{
// Check to see if we've cached this data already.
// Add and remove elements from the front and back
// of the deque until we've caught up... if we're
// too far out of range just scratch everything and
// reload.
if ( abs(index-cachePos) >= cacheSize ) {
// Out of the realm of caching
// Clear the cache of pre-existing elements
//qDebug() << QString("Clearing the cache, too far out of range") << index << cachePos;
clearOMFCache();
clearHeaderCache();
cachePos = index;
for (int loadPos=index; loadPos<(index+cacheSize) && loadPos<filenames.size(); loadPos++) {
header_ptr header = header_ptr(new OMFHeader());
omfHeaderCache.push_back(header);
omfCache.push_back(readOMF((filenames[loadPos]).toStdString(), *header));
}
cachePos = index;
} else if ( index < cachePos ) {
// Moving backwards, regroup for fast scrubbing!
//qDebug() << QString("Moving backwards") << index << cachePos;
for (int loadPos=cachePos-1; loadPos >= index && loadPos<filenames.size(); loadPos--) {
if (omfCache.size()==uint(cacheSize)) {
omfCache.pop_back();
omfHeaderCache.pop_back();
}
header_ptr header = header_ptr(new OMFHeader());
omfHeaderCache.push_front(header);
omfCache.push_front(readOMF((filenames[loadPos]).toStdString(), *header));
}
cachePos = index;
}
// We are within the current cache
if (index < filenames.size()) {
//qDebug() << QString("In Cache Range") << index << cachePos;
// Update the top overlay
ui->statusbar->showMessage(displayNames[index]);
// Update the Display
//qDebug() << QString("Current cache size") << omfCache.size();
ui->viewport->updateHeader(omfHeaderCache.at(index-cachePos), omfCache.at(index-cachePos));
ui->viewport->updateData(omfCache.at(index-cachePos));
} else {
//qDebug() << QString("Out of Cache Range!!!!") << index << cachePos;
ui->statusbar->showMessage(QString("Don't scroll so erratically..."));
}
}
void Window::openDir()
{
QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
"/home",
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
if (dir != "")
{
QDir chosenDir(dir);
dirString = chosenDir.path()+"/";
QStringList filters;
filters << "*.omf" << "*.ovf";
chosenDir.setNameFilters(filters);
QStringList dirFiles = chosenDir.entryList();
filenames.clear();
foreach (QString file, dirFiles) {
filenames.push_back(dirString + file);
}
// persistent storage of filenames for top overlay
displayNames = dirFiles;
clearOMFCache();
clearHeaderCache();
processFilenames();
gotoBackOfCache();
}
}
void Window::watchDir(const QString& str)
{
QString dir;
// Don't show a dialog if we get this message from the command line
if (str == "") {
dir = QFileDialog::getExistingDirectory(this, tr("Watch Directory"),
"/home",
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
} else {
dir = str;
}
filenames.clear();
displayNames.clear();
clearOMFCache();
cachePos = 0; // reset position to beginning
if (dir != "")
{
// Added the dir to the watch list
watcher = new QFileSystemWatcher();
//waiter = new QFileSystemWatcher();
watcher->addPath(dir);
// Now read all of the current files
QDir chosenDir(dir);
QString dirString = chosenDir.path()+"/";
QStringList filters;
filters << "*.omf" << "*.ovf";
chosenDir.setNameFilters(filters);
QStringList dirFiles = chosenDir.entryList();
// persistent storage of filenames for top overlay
displayNames = dirFiles;
foreach (QString file, dirFiles) {
filenames.push_back(dirString + file);
}
if (filenames.length()>0) {
// Only cache the last file
header_ptr header = header_ptr(new OMFHeader());
cachePos = filenames.size()-1;
omfHeaderCache.push_back(header);
omfCache.push_back(readOMF(filenames.last().toStdString(), *header));
// Update the Display with the first element
ui->viewport->updateHeader(omfHeaderCache.back(), omfCache.back());
ui->viewport->updateData(omfCache.back());
// Update the top overlay
ui->statusbar->showMessage(displayNames.back());
// Refresh the animation bar
adjustAnimSlider(true);
}
// Now the callbacks
QObject::connect(watcher, SIGNAL(directoryChanged(QString)),
this, SLOT(updateWatchedFiles(QString)));
}
}
void Window::toggleDisplay() {
if (ui->actionCubes->isChecked()) {
ui->viewport->toggleDisplay(0);
} else if (ui->actionCones->isChecked()) {
ui->viewport->toggleDisplay(1);
} else {
ui->viewport->toggleDisplay(2);
}
}
Window::~Window()
{
delete ui;
}