File: FancyMainWindow.cpp

package info (click to toggle)
camitk 5.2.0-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 358,388 kB
  • sloc: cpp: 86,984; xml: 1,295; sh: 1,280; ansic: 142; makefile: 112; perl: 84; sed: 20
file content (384 lines) | stat: -rw-r--r-- 13,579 bytes parent folder | download
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
/*****************************************************************************
 * $CAMITK_LICENCE_BEGIN$
 *
 * CamiTK - Computer Assisted Medical Intervention ToolKit
 * (c) 2001-2024 Univ. Grenoble Alpes, CNRS, Grenoble INP - UGA, TIMC, 38000 Grenoble, France
 *
 * Visit http://camitk.imag.fr for more information
 *
 * This file is part of CamiTK.
 *
 * CamiTK is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * CamiTK 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 Lesser General Public License version 3 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with CamiTK.  If not, see <http://www.gnu.org/licenses/>.
 *
 * $CAMITK_LICENCE_END$
 ****************************************************************************/


// -- Application Fancy stuff
#include "FancyMainWindow.h"

#include <Application.h>
#include <Action.h>
#include <InteractiveViewer.h>
#include <ImageComponent.h>
#include <SingleImageComponent.h>
#include <ArbitrarySingleImageComponent.h>
#include <Log.h>

// -- Qt stuff
#include <SliderSpinBoxWidget.h>
#include <QFrame>
#include <QLayout>
#include <QStackedLayout>
#include <QToolBar>

using namespace camitk;

// ------------- constructor -----------------
FancyMainWindow::FancyMainWindow() : MainWindow("Fancy") {
    comp = nullptr;
    mainWidget = new QWidget;
    ui.setupUi(mainWidget);

    //-- actions of the File menu
    ui.openButton->setIcon(QPixmap(":/fileOpen"));
    ui.openButton->setShortcut(QKeySequence::Open);
    ui.dial->show();

    // add the medical image viewer
    visibility = VIEWER_AXIAL;

    // for this application, force the medical image viewer to be embedded
    Viewer* medicalImageViewer = Application::getViewer("Medical Image Viewer");
    if (medicalImageViewer != nullptr) {
        medicalImageViewer->setEmbedder(ui.viewer);
    }
    else {
        CAMITK_ERROR(tr("Cannot find \"Medical Image Viewer\". This viewer is mandatory for running camitk-fancy."))
    }

    // modify default slice viewer colors
    QStringList viewerNames;
    viewerNames << "Axial Viewer" << "Sagittal Viewer" << "Coronal Viewer" << "Arbitrary Viewer";

    for (auto& viewerName : qAsConst(viewerNames)) {
        // hide slide bar and modify the color and background
        InteractiveViewer* sliceViewer = dynamic_cast<InteractiveViewer*>(Application::getViewer(viewerName));

        if (sliceViewer != nullptr) {
            sliceViewer->setSideFrameVisible(false);
            sliceViewer->setGradientBackground(false);
            sliceViewer->setBackgroundColor(ui.dial->palette().window().color());
        }
        else {
            CAMITK_ERROR(tr("Cannot find \"%1\". This viewer is mandatory for running camitk-fancy.").arg(viewerName))
        }
    }

    // add a 3D small little viewer (note that we need to set the visibility of this viewer
    // to true during refresh, as by default, the component does not know about it
    // and its visibility is false)
    viewer3D = dynamic_cast<InteractiveViewer*>(Application::getNewViewer("Small 3D Viewer", "InteractiveGeometryViewer"));
    Application::registerViewer(viewer3D);
    viewer3D->setEmbedder(ui.viewer3DLayout);
    viewer3D->setSideFrameVisible(false);
    viewer3D->setGradientBackground(false);
    viewer3D->setBackgroundColor(ui.dial->palette().window().color());
    viewer3D->getToolBar()->hide();

    updateAngleSlider(ui.xAngledial, ui.xAngleValue);
    updateAngleSlider(ui.yAngledial, ui.yAngleValue);
    updateAngleSlider(ui.zAngledial, ui.zAngleValue);
    showAngleDials(false);

    connect(ui.openButton, SIGNAL(clicked()),         this, SLOT(fancyFileOpen()));
    connect(ui.axial,      SIGNAL(clicked()),         this, SLOT(layoutChanged()));
    connect(ui.coronal,    SIGNAL(clicked()),         this, SLOT(layoutChanged()));
    connect(ui.saggital,   SIGNAL(clicked()),         this, SLOT(layoutChanged()));
    connect(ui.abstract,   SIGNAL(clicked()),         this, SLOT(layoutChanged()));
    connect(ui.xAngledial, SIGNAL(valueChanged(int)), this, SLOT(xAngleDialValueChanged(int)));
    connect(ui.yAngledial, SIGNAL(valueChanged(int)), this, SLOT(yAngleDialValueChanged(int)));
    connect(ui.zAngledial, SIGNAL(valueChanged(int)), this, SLOT(zAngleDialValueChanged(int)));
    connect(ui.dial,       SIGNAL(valueChanged(int)), this, SLOT(dialValueChanged(int)));

    layoutChanged();

    setCentralWidget(mainWidget);
}

// ------------- destructor -----------------
FancyMainWindow::~FancyMainWindow() {
    delete mainWidget;
}

// ------------- destructor -----------------
void FancyMainWindow::closeEvent(QCloseEvent* event) {
    // the current component is going to be closed, avoid dangling pointer
    comp = nullptr;
    MainWindow::closeEvent(event);
}

// ------------- updateComponent -----------------
void FancyMainWindow::updateComponent() {
    // get the current state from the current selected component top level
    comp = nullptr;

    if (Application::getTopLevelComponents().size() > 0) {
        comp = dynamic_cast<ImageComponent*>(Application::getTopLevelComponents().last());
    }

    viewer3D->resetCamera();
}

// ------------- fancyFileOpen -----------------
void FancyMainWindow::fancyFileOpen() {
    // the current component is going to be closed, avoid dangling pointer
    comp = nullptr;
    Application::getAction("Close All")->trigger(this);
    Application::getAction("Open")->trigger(this);

    updateComponent();
    refresh();
}

// -------------------- updateAngleSlider --------------------
void FancyMainWindow::updateAngleSlider(QDial* dial, QLabel* label) {
    dial->blockSignals(true);
    dial->setMinimum(0);
    dial->setMaximum(360);
    dial->blockSignals(false);

    if (label == ui.xAngleValue) {
        label->setText("X : <tt>" + QString("%1").arg(dial->value(), 3) + "</tt>" + 0x00B0);
    }
    else {
        if (label == ui.yAngleValue) {
            label->setText("Y : <tt>" + QString("%1").arg(dial->value(), 3) + "</tt>" + 0x00B0);
        }
        else {
            if (label == ui.zAngleValue) {
                label->setText("Z : <tt>" + QString("%1").arg(dial->value(), 3) + "</tt>" + 0x00B0);
            }
        }
    }

    label->update();

}

// -------------------- updateDialSlider --------------------
void FancyMainWindow::updateDialSlider() {
    int currentSliceId, minSliceId, maxSliceId;
    currentSliceId = minSliceId = maxSliceId = 0;

    updateComponent();

    if (comp != nullptr) {
        switch (visibility) {
            case VIEWER_AXIAL:
                currentSliceId = comp->getAxialSlices()->getSlice();
                maxSliceId = comp->getAxialSlices()->getNumberOfSlices();
                break;

            case VIEWER_CORONAL:
                currentSliceId = comp->getCoronalSlices()->getSlice();
                maxSliceId = comp->getCoronalSlices()->getNumberOfSlices();
                break;

            case VIEWER_SAGITTAL:
                currentSliceId = comp->getSagittalSlices()->getSlice();
                maxSliceId = comp->getSagittalSlices()->getNumberOfSlices();
                break;

            case VIEWER_ARBITRARY:
                // this is a percentage of translation (50% = half way through the volume)
                currentSliceId = comp->getArbitrarySlices()->getTranslationInVolume() * 100.0;
                maxSliceId = 100;
                break;

            default:
                break;

        }
    }

    ui.dial->blockSignals(true);
    ui.dial->setMinimum(minSliceId);
    ui.dial->setMaximum(maxSliceId);
    ui.dial->setValue(currentSliceId);
    ui.dial->blockSignals(false);

    ui.slideValue->setText(QString("%1").arg((currentSliceId + 1), 3) + "/" + QString("%1").arg(maxSliceId, 3));
    ui.slideValue->update();
}

// -------------------- showAngleDials --------------------
void FancyMainWindow::showAngleDials(bool isShown) {
    ui.xAngledial->setVisible(isShown);
    ui.xAngleValue->setVisible(isShown);
    ui.yAngledial->setVisible(isShown);
    ui.yAngleValue->setVisible(isShown);
    ui.zAngledial->setVisible(isShown);
    ui.zAngleValue->setVisible(isShown);
}

// -------------------- layoutChanged --------------------
void FancyMainWindow::layoutChanged() {
    if (ui.axial->isChecked()) {
        visibility = VIEWER_AXIAL;
    }
    else {
        if (ui.saggital->isChecked()) {
            visibility = VIEWER_SAGITTAL;
        }
        else {
            if (ui.coronal->isChecked()) {
                visibility = VIEWER_CORONAL;
            }
            else {
                visibility = VIEWER_ARBITRARY;
            }
        }
    }

    // switch off all viewers
    Application::getViewer("3D Viewer")->setVisible(false);
    Application::getViewer("Axial Viewer")->setVisible(false);
    Application::getViewer("Coronal Viewer")->setVisible(false);
    Application::getViewer("Sagittal Viewer")->setVisible(false);
    Application::getViewer("Arbitrary Viewer")->setVisible(false);
    // show the visible viewer
    getVisibleViewer()->setVisible(true);

    ui.dial->show();

    ui.slideValue->show();

    showAngleDials(visibility == VIEWER_ARBITRARY);

    refresh();
}

// -------------------- xAngleDialValueChanged --------------------
void FancyMainWindow::xAngleDialValueChanged(int value) {
    if (comp != nullptr) {
        if (visibility == VIEWER_ARBITRARY && comp != nullptr && comp->getArbitrarySlices() != nullptr) {
            comp->getArbitrarySlices()->setTransformRotation(ui.xAngledial->value(), ui.yAngledial->value(), ui.zAngledial->value());
        }
        else {
            dynamic_cast<InteractiveViewer*>(Application::getViewer("Arbitrary Viewer"))->xAngleChanged(value);
        }
    }

    updateAngleSlider(ui.xAngledial, ui.xAngleValue);
    refresh();
}

// -------------------- yAngleDialValueChanged --------------------
void FancyMainWindow::yAngleDialValueChanged(int value) {
    if (comp != nullptr) {
        if (visibility == VIEWER_ARBITRARY && comp != nullptr && comp->getArbitrarySlices() != nullptr) {
            comp->getArbitrarySlices()->setTransformRotation(ui.xAngledial->value(), ui.yAngledial->value(), ui.zAngledial->value());
        }
        else {
            dynamic_cast<InteractiveViewer*>(Application::getViewer("Arbitrary Viewer"))->yAngleChanged(value);
        }
    }

    updateAngleSlider(ui.yAngledial, ui.yAngleValue);
    refresh();
}

// -------------------- zAngleDialValueChanged --------------------
void FancyMainWindow::zAngleDialValueChanged(int value) {
    if (comp != nullptr) {
        if (visibility == VIEWER_ARBITRARY && comp != nullptr && comp->getArbitrarySlices() != nullptr) {
            comp->getArbitrarySlices()->setTransformRotation(ui.xAngledial->value(), ui.yAngledial->value(), ui.zAngledial->value());
        }
        else {
            dynamic_cast<InteractiveViewer*>(Application::getViewer("Arbitrary Viewer"))->zAngleChanged(value);
        }
    }

    updateAngleSlider(ui.zAngledial, ui.zAngleValue);
    refresh();
}

// -------------------- dialValueChanged --------------------
void FancyMainWindow::dialValueChanged(int value) {
    if (comp != nullptr) {
        if (visibility == VIEWER_ARBITRARY && comp->getArbitrarySlices() != nullptr) {
            comp->getArbitrarySlices()->setTransformTranslation(0.0, 0.0, ((double) value) / 100.0);
        }
        else {
            getVisibleViewer()->sliderChanged(value);
        }

        updateDialSlider();
        refresh();
    }
}

// -------------------- refreshView --------------------
void FancyMainWindow::refresh() {
    getVisibleViewer()->refresh();

    // force visualization of arbitrary slice in 3D
    // We need to set the visibility of our viewer3D to true during as by default
    // the component does not know about it and the visibility in viewer3D is false
    if (comp != nullptr) {
        if (comp->getAxialSlices() != nullptr) {
            comp->getAxialSlices()->setVisibility(viewer3D->getName(), true);
        }

        if (comp->getSagittalSlices() != nullptr) {
            comp->getSagittalSlices()->setVisibility(viewer3D->getName(), true);
        }

        if (comp->getCoronalSlices() != nullptr) {
            comp->getCoronalSlices()->setVisibility(viewer3D->getName(), true);
        }

        if (comp->getArbitrarySlices() != nullptr) {
            comp->getArbitrarySlices()->setVisibility(viewer3D->getName(), true);
        }
    }

    viewer3D->refresh();
    updateDialSlider();
}

// -------------------- getVisibleViewer --------------------
InteractiveViewer* FancyMainWindow::getVisibleViewer() {
    Viewer* visibleViewer = nullptr;

    switch (visibility) {
        case VIEWER_AXIAL:
            visibleViewer = Application::getViewer("Axial Viewer");
            break;

        case VIEWER_CORONAL:
            visibleViewer = Application::getViewer("Coronal Viewer");
            break;

        case VIEWER_SAGITTAL:
            visibleViewer = Application::getViewer("Sagittal Viewer");
            break;

        default: // arbitrary (and... 3D!)
            visibleViewer = Application::getViewer("Arbitrary Viewer");
            break;
    }

    return dynamic_cast<InteractiveViewer*>(visibleViewer);
}