35 #include <QTreeWidget>
36 #include <QTreeWidgetItem>
37 #include <QStringList>
38 #include <QVBoxLayout>
49 RegistrationHistoryWidget::RegistrationHistoryWidget(
RegServicesPtr servicesPtr, QWidget* parent,
bool compact) :
50 BaseWidget(parent,
"RegistrationHistoryWidget",
"Registration History"),
51 mServices(servicesPtr)
59 QVBoxLayout* toptopLayout =
new QVBoxLayout(
this);
60 QHBoxLayout* topLayout =
new QHBoxLayout;
61 this->createControls(topLayout);
63 toptopLayout->addWidget(mGroup);
64 mGroup->setLayout(topLayout);
67 mTextEdit =
new QTextEdit;
68 mTextEdit->setLineWrapMode(QTextEdit::NoWrap);
70 toptopLayout->addWidget(mTextEdit, 1);
71 toptopLayout->addStretch();
72 topLayout->addStretch();
75 toptopLayout->setContentsMargins(QMargins(0,0,0,0));
76 topLayout->setContentsMargins(QMargins(0,0,0,0));
88 "<h3>Registration history.</h3>"
90 "Use the registration history to rewind the system to previous time. When history is rewinded, "
91 "all registrations performed after the active time is ignored by the system."
94 "<b>NB:</b> While a previous time is active, <em>no new registrations or adding of data</em> should be performed. "
95 "This will lead to undefined behaviour!</b>"
100 void RegistrationHistoryWidget::createControls(QHBoxLayout* layout)
102 mRemoveAction = this->createAction(layout,
":/icons/open_icon_library/dialog-close.png",
"Remove",
103 "Delete all registrations after the active time", SLOT(
removeSlot()));
105 mBehindLabel =
new QLabel(
this);
106 mBehindLabel->setToolTip(
"Number of registrations before the active time");
107 layout->addWidget(mBehindLabel);
109 mRewindAction = this->createAction(layout,
":/icons/open_icon_library/arrow-left-3.png",
"Rewind",
110 "One step back in registration history, changing active time.\nThis enables looking at a previous system state,\nbut take care to not add more registrations while this state.",
113 mForwardAction = this->createAction(layout,
":/icons/open_icon_library/arrow-right-3.png",
114 "Rewind",
"One step forward in registration history", SLOT(
forwardSlot()));
116 mInFrontLabel =
new QLabel(
this);
117 mInFrontLabel->setToolTip(
"Number of registrations after active time");
118 layout->addWidget(mInFrontLabel);
120 mFastForwardAction = this->createAction(layout,
121 ":/icons/open_icon_library/arrow-right-double-3.png",
"Fast Forward",
127 QWidget::showEvent(event);
138 QWidget::closeEvent(event);
140 for (
unsigned i = 0; i < mHistories.size(); ++i)
150 for (
unsigned i = 0; i < mHistories.size(); ++i)
155 mHistories = this->getAllRegistrationHistories();
157 for (
unsigned i = 0; i < mHistories.size(); ++i)
166 std::map<QDateTime, QString> RegistrationHistoryWidget::getRegistrationTimes()
170 std::vector<RegistrationHistoryPtr> allHistories = this->getAllRegistrationHistories();
172 retval[QDateTime(QDate(2000, 1, 1))] =
"initial";
174 for (
unsigned i = 0; i < allHistories.size(); ++i)
176 std::vector<RegistrationTransform> current = allHistories[i]->getData();
177 for (
unsigned j = 0; j < current.size(); ++j)
179 retval[current[j].mTimestamp] = QString(
"%1 [f=%2, m=%3]").arg(current[j].mType).arg(current[j].mFixed).arg(
182 std::vector<ParentSpace> frames = allHistories[i]->getParentSpaces();
183 for (
unsigned j = 0; j < frames.size(); ++j)
185 retval[frames[j].mTimestamp] = QString(
"%1 [val=%2]").arg(frames[j].mType).arg(frames[j].mValue);
189 retval.erase(QDateTime());
194 RegistrationHistoryWidget::TimeMap::iterator RegistrationHistoryWidget::findCurrentActiveIter(TimeMap& times)
196 QDateTime active = this->getActiveTime();
198 if (!active.isValid())
201 for (TimeMap::iterator iter = times.begin(); iter != times.end(); ++iter)
203 if (iter->first >= active)
209 QDateTime RegistrationHistoryWidget::getActiveTime()
211 std::vector<RegistrationHistoryPtr> raw = getAllRegistrationHistories();
213 for (
unsigned i = 0; i < raw.size(); ++i)
215 if (raw[i]->isNull())
217 return raw[i]->getActiveTime();
223 void RegistrationHistoryWidget::setActiveTime(QDateTime active)
225 std::vector<RegistrationHistoryPtr> raw = getAllRegistrationHistories();
226 for (
unsigned i = 0; i < raw.size(); ++i)
228 raw[i]->setActiveTime(active);
235 std::vector<RegistrationHistoryPtr> RegistrationHistoryWidget::getAllRegistrationHistories()
237 std::vector<RegistrationHistoryPtr> retval;
238 retval.push_back(mServices->patient()->get_rMpr_History());
240 std::map<QString, DataPtr> data = mServices->patient()->getData();
241 for (std::map<QString, DataPtr>::iterator iter = data.begin(); iter != data.end(); ++iter)
243 retval.push_back(iter->second->get_rMd_History());
255 QDateTime active = this->getActiveTime();
256 if (!active.isValid())
262 std::vector<RegistrationHistoryPtr> raw = getAllRegistrationHistories();
263 for (
unsigned i = 0; i < raw.size(); ++i)
265 raw[i]->removeNewerThan(active);
271 std::vector<RegistrationTransform> RegistrationHistoryWidget::mergeHistory(
const std::vector<RegistrationHistoryPtr>& allHistories)
273 std::vector<RegistrationTransform> history;
274 for (
unsigned i = 0; i < allHistories.size(); ++i)
276 std::vector<RegistrationTransform> current = allHistories[i]->getData();
277 std::copy(current.begin(), current.end(), std::back_inserter(history));
279 std::sort(history.begin(), history.end());
290 TimeMap times = this->getRegistrationTimes();
292 if (times.size() <= 1)
296 std::map<QDateTime, QString>::iterator current = this->findCurrentActiveIter(times);
298 if (current == times.begin())
301 if (current == times.end())
307 + current->second +
"]");
308 this->setActiveTime(current->first);
311 QString RegistrationHistoryWidget::debugDump()
313 TimeMap times = this->getRegistrationTimes();
314 bool addedBreak =
false;
315 std::stringstream ss;
318 if (!this->getActiveTime().isValid())
319 ss <<
"Active time: Current \n";
324 ss <<
"<p><span style=\"color:blue\">";
325 for (TimeMap::iterator iter = times.begin(); iter != times.end(); ++iter)
327 if (iter->first > this->getActiveTime() && !addedBreak && this->getActiveTime().isValid())
329 ss <<
"</span> <span style=\"color:gray\">";
344 QAction* RegistrationHistoryWidget::createAction(QLayout* layout, QString iconName, QString text, QString tip, T slot)
346 QAction* action =
new QAction(QIcon(iconName), text,
this);
347 action->setToolTip(tip);
348 connect(action, SIGNAL(triggered()),
this, slot);
350 QToolButton* button =
new QToolButton();
351 button->setDefaultAction(action);
352 layout->addWidget(button);
361 std::map<QDateTime, QString> times = this->getRegistrationTimes();
367 TimeMap::iterator current = this->findCurrentActiveIter(times);
369 if (current == times.end())
373 if (current == times.end() || times.rbegin()->first == current->first)
375 report(
"Forward: Setting registration time to current, [" + times.rbegin()->second +
"]");
376 this->setActiveTime(QDateTime());
381 this->setActiveTime(current->first);
390 std::vector<RegistrationHistoryPtr> raw = getAllRegistrationHistories();
391 report(
"Fast Forward: Setting registration time to current.");
393 for (
unsigned i = 0; i < raw.size(); ++i)
395 raw[i]->setActiveTime(QDateTime());
405 TimeMap times = this->getRegistrationTimes();
406 std::map<QDateTime, QString>::iterator current = this->findCurrentActiveIter(times);
407 size_t behind = std::min<int>(distance(times.begin(), current), times.size() - 1);
408 size_t infront = times.size() - 1 - behind;
410 mRewindAction->setText(
"Rewind (" +
qstring_cast(behind) +
")");
411 mForwardAction->setText(
"Forward (" +
qstring_cast(infront) +
")");
413 mBehindLabel->setText(
"(" +
qstring_cast(behind) +
")");
414 mInFrontLabel->setText(
"(" +
qstring_cast(infront) +
")");
416 mRewindAction->setEnabled(behind > 0);
417 mRewindAction->setEnabled(behind > 0);
418 mRemoveAction->setEnabled(infront != 0);
419 mForwardAction->setEnabled(infront != 0);
420 mFastForwardAction->setEnabled(infront != 0);
429 color = QString(
"QFrame {background: qradialgradient(cx:0.5, cy:0.5, radius: 0.5, fx:0.5, fy:0.5, stop:0 rgb(255,30,0), stop:0.8 rgb(255,50,0), stop:1 transparent) }");
430 color += QString(
"QLabel {background-color: transparent }");
433 mGroup->setStyleSheet(color);
436 mTextEdit->setText(debugDump());
QString qstring_cast(const T &val)
boost::shared_ptr< class RegServices > RegServicesPtr
void dataAddedOrRemoved()
virtual QString defaultWhatsThis() const
QString timestampSecondsFormatNice()
virtual void hideEvent(QCloseEvent *event)
disconnects stuff
virtual ~RegistrationHistoryWidget()
virtual void prePaintEvent()
virtual void showEvent(QShowEvent *event)
updates internal info before showing the widget