123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- /*=========================================================================
- Library: CTK
- Copyright (c) Kitware Inc.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.commontk.org/LICENSE
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- =========================================================================*/
- // Qt includes
- #include <QDebug>
- #include <QStringList>
- // CTK includes
- #include "ctkModalityWidget.h"
- #include "ui_ctkModalityWidget.h"
- #include "ctkLogger.h"
- #include "ctkFlowLayout.h"
- // STD includes
- #include <cmath>
- static ctkLogger logger("org.commontk.libs.widgets.ctkModalityWidget");
- QStringList sDefaultVisibleModalities;
- //-----------------------------------------------------------------------------
- class ctkModalityWidgetPrivate: public Ui_ctkModalityWidget
- {
- Q_DECLARE_PUBLIC(ctkModalityWidget);
- protected:
- ctkModalityWidget* const q_ptr;
- public:
- ctkModalityWidgetPrivate(ctkModalityWidget& object);
- void init();
- void updateAnyCheckBoxState();
-
- QStringList SelectedModalities;
- QStringList VisibleModalities;
- QMap<QString, QCheckBox*> Modalities;
- };
- // --------------------------------------------------------------------------
- ctkModalityWidgetPrivate::ctkModalityWidgetPrivate(ctkModalityWidget& object)
- :q_ptr(&object)
- {
- if (sDefaultVisibleModalities.isEmpty())
- {
- sDefaultVisibleModalities << "CT" << "MR" << "US" << "CR" << "XA" << "NM" << "PT";
- }
- }
- // --------------------------------------------------------------------------
- void ctkModalityWidgetPrivate::init()
- {
- Q_Q(ctkModalityWidget);
- this->setupUi(q);
-
- this->AnyCheckBox->setTristate(true);
- QObject::connect(this->AnyCheckBox, SIGNAL(stateChanged(int)),
- q, SLOT(onAnyChanged(int)));
- foreach(QCheckBox* box, q->findChildren<QCheckBox*>())
- {
- if (box == this->AnyCheckBox)
- {
- continue;
- }
- this->Modalities[box->text()] = box;
- QObject::connect(box, SIGNAL(toggled(bool)),
- q, SLOT(onModalityChecked(bool)));
- }
- // reparent items into a flow layout
- QLayout* layout = q->layout();
- ctkFlowLayout* flowLayout = new ctkFlowLayout(Qt::Horizontal);
- flowLayout->setContentsMargins(layout->contentsMargins());
- flowLayout->setPreferredExpandingDirections(Qt::Vertical);
- QLayoutItem* item;
- while ((item = layout->takeAt(0)))
- {
- flowLayout->addItem(item);
- }
- delete layout;
- q->setLayout(flowLayout);
-
- q->setVisibleModalities(sDefaultVisibleModalities);
- QSize sizeHint = flowLayout->sizeHint();
- double area = sizeHint.width() * sizeHint.height();
- area = sqrt(area);
- sizeHint = QSize(area, area);
- q->resize(sizeHint);
- q->selectAll();
- }
- // --------------------------------------------------------------------------
- void ctkModalityWidgetPrivate::updateAnyCheckBoxState()
- {
- if (this->SelectedModalities.isEmpty())
- {
- this->AnyCheckBox->setCheckState(Qt::Unchecked);
- }
- else if (this->SelectedModalities.count() == this->Modalities.count())
- {
- this->AnyCheckBox->setCheckState(Qt::Checked);
- }
- else
- {
- this->AnyCheckBox->setCheckState(Qt::PartiallyChecked);
- }
- }
- // --------------------------------------------------------------------------
- ctkModalityWidget::ctkModalityWidget(QWidget* parentWidget)
- : Superclass(parentWidget)
- , d_ptr(new ctkModalityWidgetPrivate(*this))
- {
- Q_D(ctkModalityWidget);
- d->init();
- }
- // --------------------------------------------------------------------------
- ctkModalityWidget::~ctkModalityWidget()
- {
- }
- // --------------------------------------------------------------------------
- QStringList ctkModalityWidget::selectedModalities()const
- {
- Q_D(const ctkModalityWidget);
- return d->SelectedModalities;
- }
- // --------------------------------------------------------------------------
- void ctkModalityWidget::setSelectedModalities(const QStringList& modalities)
- {
- if (modalities == this->selectedModalities())
- {
- return;
- }
- bool blocked = this->blockSignals(true);
- this->unselectAll();
- foreach(QString modality, modalities)
- {
- this->selectModality(modality);
- }
- this->blockSignals(blocked);
- emit this->selectedModalitiesChanged(modalities);
- }
- // --------------------------------------------------------------------------
- QStringList ctkModalityWidget::visibleModalities()const
- {
- Q_D(const ctkModalityWidget);
- return d->VisibleModalities;
- }
- // --------------------------------------------------------------------------
- void ctkModalityWidget::setVisibleModalities(const QStringList& modalities)
- {
- if (modalities == this->visibleModalities())
- {
- return;
- }
- this->hideAll();
- foreach(QString modality, modalities)
- {
- this->showModality(modality);
- }
- }
- // --------------------------------------------------------------------------
- void ctkModalityWidget::selectModality(const QString& modality, bool select)
- {
- Q_D(ctkModalityWidget);
- QCheckBox* modalityBox = d->Modalities[modality];
- modalityBox->setChecked(select);
- }
- // --------------------------------------------------------------------------
- void ctkModalityWidget::showModality(const QString& modality, bool show)
- {
- Q_D(ctkModalityWidget);
- QCheckBox* modalityBox = d->Modalities[modality];
- modalityBox->setVisible(show);
- }
- // --------------------------------------------------------------------------
- void ctkModalityWidget::selectAll()
- {
- Q_D(ctkModalityWidget);
- if (d->SelectedModalities.count() == d->Modalities.count())
- {
- return;
- }
- bool blocked = this->blockSignals(true);
- foreach(const QString& modality, d->Modalities.keys())
- {
- this->selectModality(modality, true);
- }
- this->blockSignals(blocked);
- emit this->selectedModalitiesChanged(d->SelectedModalities);
- }
- // --------------------------------------------------------------------------
- void ctkModalityWidget::unselectAll()
- {
- Q_D(ctkModalityWidget);
- if (d->SelectedModalities.count() == 0)
- {
- return;
- }
- bool blocked = this->blockSignals(true);
- foreach(const QString& modality, d->Modalities.keys())
- {
- this->selectModality(modality, false);
- }
- this->blockSignals(blocked);
- emit this->selectedModalitiesChanged(d->SelectedModalities);
- }
- // --------------------------------------------------------------------------
- void ctkModalityWidget::showAll()
- {
- Q_D(ctkModalityWidget);
- foreach(const QString& modality, d->Modalities.keys())
- {
- this->showModality(modality, true);
- }
- }
- // --------------------------------------------------------------------------
- void ctkModalityWidget::hideAll()
- {
- Q_D(ctkModalityWidget);
- foreach(const QString& modality, d->Modalities.keys())
- {
- this->showModality(modality, false);
- }
- }
- // --------------------------------------------------------------------------
- void ctkModalityWidget::onAnyChanged(int state)
- {
- if (state == Qt::Unchecked)
- {
- this->unselectAll();
- }
- else if (state == Qt::Checked)
- {
- this->selectAll();
- }
- }
- // --------------------------------------------------------------------------
- void ctkModalityWidget::onModalityChecked(bool checked)
- {
- Q_D(ctkModalityWidget);
- QCheckBox* box = qobject_cast<QCheckBox*>(this->sender());
- QString modality = box->text();
- if (checked)
- {
- d->SelectedModalities.append(modality);
- }
- else
- {
- d->SelectedModalities.removeAll(modality);
- }
- d->updateAnyCheckBoxState();
- emit this->selectedModalitiesChanged(d->SelectedModalities);
- }
|