42 #include <QTreeWidget> 43 #include <QTreeWidgetItem> 44 #include <QStringList> 45 #include <QVBoxLayout> 46 #include <QHeaderView> 71 BaseWidget(parent,
"metric_widget",
"Metrics/3D ruler"),
72 mVerticalLayout(new QVBoxLayout(this)),
73 mTable(new QTableWidget(this)),
78 int lowUpdateRate = 100;
79 mLocalModified =
false;
80 mDelayedUpdateTimer =
new QTimer(
this);
81 connect(mDelayedUpdateTimer, SIGNAL(timeout()),
this, SLOT(
delayedUpdate()));
82 mDelayedUpdateTimer->start(lowUpdateRate);
83 this->setToolTip(
"3D measurements");
87 mMetricManager.reset(
new MetricManager(services->view(), services->patient(), services->tracking(), services->spaceProvider()));
88 connect(mMetricManager.get(), SIGNAL(activeMetricChanged()),
this, SLOT(
setModified()));
89 connect(mMetricManager.get(), SIGNAL(metricsChanged()),
this, SLOT(
setModified()));
93 connect(mTable, SIGNAL(cellChanged(
int,
int)),
this, SLOT(
cellChangedSlot(
int,
int)));
94 connect(mTable, SIGNAL(cellClicked(
int,
int)),
this, SLOT(
cellClickedSlot(
int,
int)));
96 this->setLayout(mVerticalLayout);
98 mEditWidgets =
new QStackedWidget;
100 QActionGroup* group =
new QActionGroup(
this);
101 this->createActions(group);
106 QWidget* toolBar =
new QWidget(
this);
107 QHBoxLayout* toolLayout =
new QHBoxLayout(toolBar);
108 toolLayout->setMargin(0);
109 toolLayout->setSpacing(0);
110 QList<QAction*> actions = group->actions();
111 for (
unsigned i=0; i<actions.size(); ++i)
113 if (actions[i]->isSeparator())
115 toolLayout->addSpacing(4);
116 QFrame* frame =
new QFrame();
117 frame->setFrameStyle(QFrame::Sunken + QFrame::VLine);
118 toolLayout->addWidget(frame);
119 toolLayout->addSpacing(4);
124 button->setDefaultAction(actions[i]);
125 button->setIconSize(QSize(32,32));
126 toolLayout->addWidget(button);
130 QHBoxLayout* buttonLayout =
new QHBoxLayout;
131 buttonLayout->addWidget(toolBar);
132 buttonLayout->addStretch();
135 mVerticalLayout->addLayout(buttonLayout);
136 mVerticalLayout->addWidget(mTable, 1);
137 mVerticalLayout->addWidget(mEditWidgets, 0);
143 void MetricWidget::createActions(QActionGroup* group)
149 this->createAction(group,
":/icons/metric_angle.png",
"Angle",
"Create a new Angle Metric", SLOT(
addAngleButtonClickedSlot()));
150 this->createAction(group,
":/icons/metric_plane.png",
"Plane",
"Create a new Plane Metric", SLOT(
addPlaneButtonClickedSlot()));
151 this->createAction(group,
":/icons/metric_sphere.png",
"Sphere",
"Create a new Sphere Metric", SLOT(
addSphereButtonClickedSlot()));
152 this->createAction(group,
":/icons/metric_torus.png",
"Torus",
"Create a new Torus Metric", SLOT(
addDonutButtonClickedSlot()));
153 this->createAction(group,
":/icons/metric_custom.png",
"Custom",
"Create a new Custom Metric", SLOT(
addCustomButtonClickedSlot()));
154 this->createAction(group,
":/icons/metric.png",
"ROI",
"Create a new Region of Interest Metric", SLOT(
addROIButtonClickedSlot()));
156 this->createAction(group,
"",
"",
"", NULL)->setSeparator(
true);
157 mRemoveAction = this->createAction(group,
":/icons/metric_remove.png",
"Remove",
"Remove currently selected metric", SLOT(
removeButtonClickedSlot()));
158 mRemoveAction->setDisabled(
true);
159 mLoadReferencePointsAction = this->createAction(group,
":/icons/metric_reference.png",
"Import",
"Import reference points from reference tool", SLOT(
loadReferencePointsSlot()));
160 mLoadReferencePointsAction->setDisabled(
true);
161 this->createAction(group,
"",
"",
"", NULL)->setSeparator(
true);
167 QAction* MetricWidget::createAction(QActionGroup* group, QString iconName, QString text, QString tip,
const char* slot)
169 QAction* action =
new QAction(QIcon(iconName), text, group);
170 action->setStatusTip(tip);
171 action->setToolTip(tip);
174 connect(action, SIGNAL(triggered()),
this, slot);
183 QTableWidgetItem* item = mTable->item(row,col);
184 DataPtr data =
mServices->patient()->getData(item->data(Qt::UserRole).toString());
186 data->setName(item->text());
193 if (row < 0 || column < 0)
196 QTableWidgetItem* item = mTable->item(row,column);
197 QString uid = item->data(Qt::UserRole).toString();
198 mMetricManager->moveToMetric(uid);
203 QTableWidgetItem* item = mTable->currentItem();
205 mMetricManager->setActiveUid(item->data(Qt::UserRole).toString());
206 mEditWidgets->setCurrentIndex(mTable->currentRow());
208 mMetricManager->setSelection(this->getSelectedUids());
213 void MetricWidget::showEvent(QShowEvent* event)
215 QWidget::showEvent(event);
219 void MetricWidget::hideEvent(QHideEvent* event)
221 QWidget::hideEvent(event);
224 void MetricWidget::prePaintEvent()
234 bool rebuild = !this->checkEqual(newMetrics, mMetrics);
237 this->resetWrappersAndEditWidgets(newMetrics);
238 this->initializeTable();
241 this->updateMetricWrappers();
242 this->updateTableContents();
246 this->expensizeColumnResize();
249 this->enablebuttons();
254 void MetricWidget::expensizeColumnResize()
257 mTable->resizeColumnToContents(valueColumn);
260 void MetricWidget::initializeTable()
262 mTable->blockSignals(
true);
266 mTable->setRowCount(mMetrics.size());
267 mTable->setColumnCount(4);
268 QStringList headerItems(QStringList() <<
"Name" <<
"Value" <<
"Arguments" <<
"Type");
269 mTable->setHorizontalHeaderLabels(headerItems);
271 mTable->setSelectionBehavior(QAbstractItemView::SelectRows);
272 mTable->verticalHeader()->hide();
274 for (
unsigned i = 0; i < mMetrics.size(); ++i)
278 for (
unsigned j = 0; j < 4; ++j)
280 QTableWidgetItem* item =
new QTableWidgetItem(
"empty");
281 item->setData(Qt::UserRole, current->getData()->getUid());
282 mTable->setItem(i, j, item);
285 mTable->blockSignals(
false);
288 void MetricWidget::updateMetricWrappers()
290 for (
unsigned i = 0; i < mMetrics.size(); ++i)
292 mMetrics[i]->update();
296 void MetricWidget::updateTableContents()
298 mTable->blockSignals(
true);
303 for (
unsigned i = 0; i < mMetrics.size(); ++i)
306 if (!mTable->item(i,0))
308 std::cout <<
"no qitem for:: " << i <<
" " << current->getData()->getName() << std::endl;
311 QString name = current->getData()->getName();
312 QString value = current->getValue();
313 QString arguments = current->getArguments();
314 QString type = current->getType();
316 mTable->item(i,0)->setText(name);
317 mTable->item(i,1)->setText(value);
318 mTable->item(i,2)->setText(arguments);
319 mTable->item(i,3)->setText(type);
322 if (current->getData()->getUid() == mMetricManager->getActiveUid())
324 mTable->setCurrentCell(i,1);
325 mEditWidgets->setCurrentIndex(i);
328 mTable->blockSignals(
false);
333 mLocalModified =
true;
343 mLocalModified =
false;
347 void MetricWidget::resetWrappersAndEditWidgets(std::vector<MetricBasePtr> wrappers)
349 while (mEditWidgets->count())
351 mEditWidgets->removeWidget(mEditWidgets->widget(0));
354 for (
unsigned i=0; i<mMetrics.size(); ++i)
356 disconnect(mMetrics[i]->getData().
get(), SIGNAL(transformChanged()),
this, SLOT(
setModified()));
361 for (
unsigned i=0; i<mMetrics.size(); ++i)
363 connect(mMetrics[i]->getData().
get(), SIGNAL(transformChanged()),
this, SLOT(
setModified()));
366 for (
unsigned i=0; i<mMetrics.size(); ++i)
369 QGroupBox* groupBox =
new QGroupBox(wrapper->getData()->getName(),
this);
370 groupBox->setFlat(
true);
371 QVBoxLayout* gbLayout =
new QVBoxLayout(groupBox);
372 gbLayout->setMargin(4);
373 gbLayout->addWidget(wrapper->createWidget());
374 mEditWidgets->addWidget(groupBox);
377 mEditWidgets->setCurrentIndex(-1);
380 bool MetricWidget::checkEqual(
const std::vector<MetricBasePtr>& a,
const std::vector<MetricBasePtr>& b)
const 382 if (a.size()!=b.size())
385 for (
unsigned i=0; i<a.size(); ++i)
387 if (a[i]->getData()!=b[i]->getData())
394 void MetricWidget::enablebuttons()
396 mRemoveAction->setEnabled(!mMetricManager->getActiveUid().isEmpty());
397 mLoadReferencePointsAction->setEnabled(
mServices->tracking()->getReferenceTool() ?
true :
false);
402 mMetricManager->loadReferencePointsSlot();
406 mMetricManager->addPointButtonClickedSlot();
410 mMetricManager->addFrameButtonClickedSlot();
414 mMetricManager->addToolButtonClickedSlot();
418 mMetricManager->addPlaneButtonClickedSlot();
422 mMetricManager->addAngleButtonClickedSlot();
426 mMetricManager->addDistanceButtonClickedSlot();
430 mMetricManager->addROIButtonClickedSlot();
434 mMetricManager->addSphereButtonClickedSlot();
438 mMetricManager->addDonutButtonClickedSlot();
442 mMetricManager->addCustomButtonClickedSlot();
445 std::set<QString> MetricWidget::getSelectedUids()
447 QList<QTableWidgetItem*> selection = mTable->selectedItems();
449 std::set<QString> selectedUids;
450 for (
int i=0; i<selection.size(); ++i)
452 selectedUids.insert(selection[i]->data(Qt::UserRole).
toString());
459 int nextIndex = mTable->currentRow() + 1;
461 if (nextIndex < mTable->rowCount())
463 QTableWidgetItem* nextItem = mTable->item(nextIndex, 0);
464 nextUid = nextItem->data(Qt::UserRole).toString();
467 mServices->patient()->removeData(mMetricManager->getActiveUid());
469 if (!nextUid.isEmpty())
470 mMetricManager->setActiveUid(nextUid);
475 QString suggestion = QString(
"%1/Logs/metrics_%2.xml")
476 .arg(
mServices->patient()->getActivePatientFolder())
479 QString filename = QFileDialog::getSaveFileName(
this,
480 "Select the file to export the metrics to",
482 if(!filename.isEmpty())
483 mMetricManager->exportMetricsToXMLFile(filename);
488 QString suggestion = QString(
"%1/Logs/")
489 .arg(
mServices->patient()->getActivePatientFolder());
491 QString fileName = QFileDialog::getOpenFileName(
this,
492 "Select the file to import metrics from (can be a patient file)",
494 "XML or MNI Tag file (*.xml *.tag)");
496 if(!fileName.isEmpty() && fileName.endsWith(
".xml"))
497 mMetricManager->importMetricsFromXMLFile(fileName);
498 else if(!fileName.isEmpty() && fileName.endsWith(
".tag"))
499 mMetricManager->importMetricsFromMNITagFile(fileName);
boost::shared_ptr< class VisServices > VisServicesPtr
std::string toString(T const &value)
converts any type to a string
std::vector< MetricBasePtr > createMetricWrappers()
QString timestampSecondsFormat()
boost::shared_ptr< class Data > DataPtr
boost::shared_ptr< class MetricBase > MetricBasePtr
Namespace for all CustusX production code.