/
qSlicerIOManager.cxx
551 lines (494 loc) · 16 KB
/
qSlicerIOManager.cxx
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/// Qt includes
#include <QDebug>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QFileDialog>
#include <QInputDialog>
#include <QMetaProperty>
#include <QProgressDialog>
#include <QSettings>
#include <QTimer>
// CTK includes
#include "ctkScreenshotDialog.h"
/// Slicer includes
#include "qSlicerIOManager.h"
#include "qSlicerDataDialog.h"
#include "qSlicerModelsDialog.h"
#include "qSlicerSaveDataDialog.h"
#include "qSlicerApplication.h"
#include "qSlicerLayoutManager.h"
#include "qSlicerModuleManager.h"
#include "qSlicerAbstractCoreModule.h"
/// MRML includes
#include <vtkMRMLNode.h>
#include <vtkMRMLScene.h>
#include <vtkMRMLMessageCollection.h>
/// VTK includes
#include <vtkCollection.h>
//-----------------------------------------------------------------------------
class qSlicerIOManagerPrivate
{
Q_DECLARE_PUBLIC(qSlicerIOManager);
protected:
qSlicerIOManager* const q_ptr;
public:
qSlicerIOManagerPrivate(qSlicerIOManager& object);
vtkMRMLScene* currentScene()const;
/// Return true if a dialog is created, false if a dialog already existed
bool startProgressDialog(int steps = 1);
void stopProgressDialog();
void readSettings();
void writeSettings();
QString createUniqueDialogName(qSlicerIO::IOFileType,
qSlicerFileDialog::IOAction,
const qSlicerIO::IOProperties&);
qSlicerFileDialog* findDialog(qSlicerIO::IOFileType fileType,
qSlicerFileDialog::IOAction)const;
QStringList History;
QList<QUrl> Favorites;
QList<qSlicerFileDialog*> ReadDialogs;
QList<qSlicerFileDialog*> WriteDialogs;
QSharedPointer<ctkScreenshotDialog> ScreenshotDialog;
QProgressDialog* ProgressDialog{nullptr};
/// File dialog that next call of execDelayedFileDialog signal will execute
qSlicerFileDialog* DelayedFileDialog{nullptr};
};
//-----------------------------------------------------------------------------
// qSlicerIOManagerPrivate methods
//-----------------------------------------------------------------------------
qSlicerIOManagerPrivate::qSlicerIOManagerPrivate(qSlicerIOManager& object)
: q_ptr(&object)
{
}
//-----------------------------------------------------------------------------
vtkMRMLScene* qSlicerIOManagerPrivate::currentScene()const
{
return qSlicerCoreApplication::application()->mrmlScene();
}
//-----------------------------------------------------------------------------
bool qSlicerIOManagerPrivate::startProgressDialog(int steps)
{
Q_Q(qSlicerIOManager);
if (this->ProgressDialog)
{
return false;
}
int max = (steps != 1 ? steps : 100);
this->ProgressDialog = new QProgressDialog("Loading file... ", "Cancel", 0, max);
this->ProgressDialog->setWindowTitle(QString("Loading ..."));
if (steps == 1)
{
// We only support cancelling a load action if we can have control over it
this->ProgressDialog->setCancelButton(nullptr);
}
this->ProgressDialog->setWindowModality(Qt::WindowModal);
this->ProgressDialog->setMinimumDuration(1000);
this->ProgressDialog->setValue(0);
if (steps == 1)
{
q->qvtkConnect(qSlicerCoreApplication::application()->mrmlScene(),
vtkMRMLScene::NodeAddedEvent,
q, SLOT(updateProgressDialog()));
}
return true;
}
//-----------------------------------------------------------------------------
void qSlicerIOManagerPrivate::stopProgressDialog()
{
Q_Q(qSlicerIOManager);
if (!this->ProgressDialog)
{
return;
}
this->ProgressDialog->setValue(this->ProgressDialog->maximum());
q->qvtkDisconnect(qSlicerCoreApplication::application()->mrmlScene(),
vtkMRMLScene::NodeAddedEvent,
q, SLOT(updateProgressDialog()));
delete this->ProgressDialog;
this->ProgressDialog = nullptr;
}
//-----------------------------------------------------------------------------
void qSlicerIOManagerPrivate::readSettings()
{
QSettings settings;
settings.beginGroup("ioManager");
if (!settings.value("favoritesPaths").toList().isEmpty())
{
QStringList paths = qSlicerCoreApplication::application()->toSlicerHomeAbsolutePaths(settings.value("favoritesPaths").toStringList());
foreach (const QString& varUrl, paths)
{
this->Favorites << QUrl(varUrl);
}
}
else
{
this->Favorites << QUrl::fromLocalFile(QDir::homePath());
}
settings.endGroup();
}
//-----------------------------------------------------------------------------
void qSlicerIOManagerPrivate::writeSettings()
{
Q_Q(qSlicerIOManager);
QSettings settings;
settings.beginGroup("ioManager");
QStringList list;
foreach (const QUrl& url, q->favorites())
{
list << url.toString();
}
QStringList paths = qSlicerCoreApplication::application()->toSlicerHomeRelativePaths(list);
settings.setValue("favoritesPaths", QVariant(paths));
settings.endGroup();
}
//-----------------------------------------------------------------------------
QString qSlicerIOManagerPrivate::
createUniqueDialogName(qSlicerIO::IOFileType fileType,
qSlicerFileDialog::IOAction action,
const qSlicerIO::IOProperties& ioProperties)
{
QString objectName;
objectName += action == qSlicerFileDialog::Read ? "Add" : "Save";
objectName += fileType;
objectName += ioProperties["multipleFiles"].toBool() ? "s" : "";
objectName += "Dialog";
return objectName;
}
//-----------------------------------------------------------------------------
qSlicerFileDialog* qSlicerIOManagerPrivate
::findDialog(qSlicerIO::IOFileType fileType,
qSlicerFileDialog::IOAction action)const
{
const QList<qSlicerFileDialog*>& dialogs =
(action == qSlicerFileDialog::Read)? this->ReadDialogs : this->WriteDialogs;
foreach(qSlicerFileDialog* dialog, dialogs)
{
if (dialog->fileType() == fileType)
{
return dialog;
}
}
return nullptr;
}
//-----------------------------------------------------------------------------
// qSlicerIOManager methods
//-----------------------------------------------------------------------------
qSlicerIOManager::qSlicerIOManager(QObject* _parent):Superclass(_parent)
, d_ptr(new qSlicerIOManagerPrivate(*this))
{
Q_D(qSlicerIOManager);
d->readSettings();
}
//-----------------------------------------------------------------------------
qSlicerIOManager::~qSlicerIOManager()
{
Q_D(qSlicerIOManager);
d->writeSettings();
}
//-----------------------------------------------------------------------------
bool qSlicerIOManager::openLoadSceneDialog()
{
qSlicerIO::IOProperties properties;
properties["clear"] = true;
return this->openDialog(QString("SceneFile"), qSlicerFileDialog::Read, properties);
}
//-----------------------------------------------------------------------------
bool qSlicerIOManager::openAddSceneDialog()
{
qSlicerIO::IOProperties properties;
properties["clear"] = false;
return this->openDialog(QString("SceneFile"), qSlicerFileDialog::Read, properties);
}
//-----------------------------------------------------------------------------
bool qSlicerIOManager::openDialog(qSlicerIO::IOFileType fileType,
qSlicerFileDialog::IOAction action,
qSlicerIO::IOProperties properties,
vtkCollection* loadedNodes)
{
Q_D(qSlicerIOManager);
bool deleteDialog = false;
if (properties["objectName"].toString().isEmpty())
{
QString name = d->createUniqueDialogName(fileType, action, properties);
properties["objectName"] = name;
}
qSlicerFileDialog* dialog = d->findDialog(fileType, action);
if (dialog == nullptr)
{
deleteDialog = true;
qSlicerStandardFileDialog* standardDialog =
new qSlicerStandardFileDialog(this);
standardDialog->setFileType(fileType);
standardDialog->setAction(action);
dialog = standardDialog;
}
bool res = dialog->exec(properties);
if (loadedNodes)
{
foreach(const QString& nodeID, dialog->loadedNodes())
{
vtkMRMLNode* node = d->currentScene()->GetNodeByID(nodeID.toUtf8());
if (node)
{
loadedNodes->AddItem(node);
}
}
}
if (deleteDialog)
{
delete dialog;
}
return res;
}
//---------------------------------------------------------------------------
void qSlicerIOManager::dragEnterEvent(QDragEnterEvent *event)
{
Q_D(qSlicerIOManager);
foreach(qSlicerFileDialog* dialog, d->ReadDialogs)
{
if (dialog->isMimeDataAccepted(event->mimeData()))
{
event->accept();
break;
}
}
}
//-----------------------------------------------------------------------------
void qSlicerIOManager::dropEvent(QDropEvent *event)
{
Q_D(qSlicerIOManager);
QStringList supportedReaders;
QStringList genericReaders; // those must be last in the choice menu
foreach(qSlicerFileDialog* dialog, d->ReadDialogs)
{
if (dialog->isMimeDataAccepted(event->mimeData()))
{
QString supportedReader = dialog->description();
if (dialog->fileType() == "NoFile")
{
genericReaders << supportedReader;
}
else
{
supportedReaders << supportedReader;
}
}
}
supportedReaders << genericReaders;
QString selectedReader;
if (supportedReaders.size() > 1)
{
QString title = tr("Select a reader");
QString label = tr("Select a reader to use for your data?");
int current = 0;
bool editable = false;
bool ok = false;
selectedReader = QInputDialog::getItem(nullptr, title, label, supportedReaders, current, editable, &ok);
if (!ok)
{
selectedReader = QString();
}
}
else if (supportedReaders.size() ==1)
{
selectedReader = supportedReaders[0];
}
if (selectedReader.isEmpty())
{
return;
}
foreach(qSlicerFileDialog* dialog, d->ReadDialogs)
{
if (dialog->description() == selectedReader)
{
dialog->dropEvent(event);
if (event->isAccepted())
{
// If we immediately executed the dialog here then we would block the application
// that initiated the drag-and-drop operation. Therefore we return and open the dialog
// with a timer.
d->DelayedFileDialog = dialog;
QTimer::singleShot(0, this, SLOT(execDelayedFileDialog()));
break;
}
}
}
}
//-----------------------------------------------------------------------------
void qSlicerIOManager::execDelayedFileDialog()
{
Q_D(qSlicerIOManager);
if (d->DelayedFileDialog)
{
d->DelayedFileDialog->exec();
}
}
//-----------------------------------------------------------------------------
void qSlicerIOManager::addHistory(const QString& path)
{
Q_D(qSlicerIOManager);
d->History << path;
}
//-----------------------------------------------------------------------------
const QStringList& qSlicerIOManager::history()const
{
Q_D(const qSlicerIOManager);
return d->History;
}
//-----------------------------------------------------------------------------
void qSlicerIOManager::setFavorites(const QList<QUrl>& urls)
{
Q_D(qSlicerIOManager);
QList<QUrl> newFavorites;
foreach(const QUrl& url, urls)
{
newFavorites << url;
}
d->Favorites = newFavorites;
}
//-----------------------------------------------------------------------------
const QList<QUrl>& qSlicerIOManager::favorites()const
{
Q_D(const qSlicerIOManager);
return d->Favorites;
}
//-----------------------------------------------------------------------------
void qSlicerIOManager::registerDialog(qSlicerFileDialog* dialog)
{
Q_D(qSlicerIOManager);
Q_ASSERT(dialog);
qSlicerFileDialog* existingDialog =
d->findDialog(dialog->fileType(), dialog->action());
if (existingDialog)
{
d->ReadDialogs.removeAll(existingDialog);
d->WriteDialogs.removeAll(existingDialog);
existingDialog->deleteLater();
}
if (dialog->action() == qSlicerFileDialog::Read)
{
d->ReadDialogs.prepend(dialog);
}
else if (dialog->action() == qSlicerFileDialog::Write)
{
d->WriteDialogs.prepend(dialog);
}
else
{
Q_ASSERT(dialog->action() == qSlicerFileDialog::Read ||
dialog->action() == qSlicerFileDialog::Write);
}
dialog->setParent(this);
}
//-----------------------------------------------------------------------------
bool qSlicerIOManager::loadNodes(const qSlicerIO::IOFileType& fileType,
const qSlicerIO::IOProperties& parameters, vtkCollection* loadedNodes,
vtkMRMLMessageCollection* userMessages/*=nullptr*/)
{
Q_D(qSlicerIOManager);
bool needStop = d->startProgressDialog(1);
d->ProgressDialog->setLabelText(
"Loading file " + parameters.value("fileName").toString() + " ...");
if (needStop)
{
d->ProgressDialog->setValue(25);
}
bool res = this->qSlicerCoreIOManager::loadNodes(fileType, parameters, loadedNodes, userMessages);
if (needStop)
{
d->stopProgressDialog();
}
return res;
}
//-----------------------------------------------------------------------------
bool qSlicerIOManager::loadNodes(const QList<qSlicerIO::IOProperties>& files,
vtkCollection* loadedNodes, vtkMRMLMessageCollection* userMessages/*=nullptr*/)
{
Q_D(qSlicerIOManager);
bool needStop = d->startProgressDialog(files.count());
bool success = true;
foreach(qSlicerIO::IOProperties fileProperties, files)
{
int numberOfUserMessagesBefore = userMessages ? userMessages->GetNumberOfMessages() : 0;
success = this->loadNodes(
static_cast<qSlicerIO::IOFileType>(fileProperties["fileType"].toString()),
fileProperties,
loadedNodes, userMessages) && success;
if (userMessages && userMessages->GetNumberOfMessages() > numberOfUserMessagesBefore)
{
userMessages->AddSeparator();
}
this->updateProgressDialog();
if (d->ProgressDialog->wasCanceled())
{
success = false;
break;
}
}
if (needStop)
{
d->stopProgressDialog();
}
return success;
}
//-----------------------------------------------------------------------------
void qSlicerIOManager::updateProgressDialog()
{
Q_D(qSlicerIOManager);
if (!d->ProgressDialog)
{
return;
}
int progress = d->ProgressDialog->value();
d->ProgressDialog->setValue(qMin(progress + 1, d->ProgressDialog->maximum() - 1) );
// Give time to process graphic events including the progress dialog if needed
// TBD: Not needed ?
//qApp->processEvents();
}
//-----------------------------------------------------------------------------
void qSlicerIOManager::openScreenshotDialog()
{
Q_D(qSlicerIOManager);
// try opening the Annotation module's screen shot
qSlicerModuleManager *moduleManager = qSlicerApplication::application()->moduleManager();
qSlicerAbstractCoreModule *modulePointer = nullptr;
if (moduleManager)
{
modulePointer = moduleManager->module("Annotations");
}
if (modulePointer)
{
QMetaObject::invokeMethod(modulePointer, "showScreenshotDialog");
}
else
{
qWarning() << "qSlicerIOManager::openScreenshotDialog: Unable to get Annotations module (annotations), using the CTK screen shot dialog.";
// use the ctk one
if (!d->ScreenshotDialog)
{
d->ScreenshotDialog = QSharedPointer<ctkScreenshotDialog>(
new ctkScreenshotDialog());
d->ScreenshotDialog->setWidgetToGrab(
qSlicerApplication::application()->layoutManager()->viewport());
}
d->ScreenshotDialog->show();
}
}
//-----------------------------------------------------------------------------
void qSlicerIOManager::openSceneViewsDialog()
{
// Q_D(qSlicerIOManager);
qSlicerModuleManager *moduleManager = qSlicerApplication::application()->moduleManager();
if (!moduleManager)
{
qWarning() << "qSlicerIOManager::openSceneViewsDialog: unable to get module manager, can't get at the Scene Views module";
return;
}
qSlicerAbstractCoreModule *modulePointer = moduleManager->module("SceneViews");
if (modulePointer == nullptr)
{
qWarning() << "qSlicerIOManager::openSceneViewsDialog: Unable to get at the SceneViews module (sceneviews).";
return;
}
QMetaObject::invokeMethod(modulePointer, "showSceneViewDialog");
}