NorMIT-nav  2023.01.05-dev+develop.0da12
An IGT application
cxDataSelectWidget.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxDataSelectWidget.h"
13 
14 #include "cxHelperWidgets.h"
16 #include "cxTypeConversions.h"
17 #include "cxViewService.h"
18 #include "cxPatientModelService.h"
19 #include "cxData.h"
20 #include "cxViewGroupData.h"
21 #include "cxLogger.h"
22 
23 namespace cx
24 {
25 
26 DataSelectWidget::DataSelectWidget(ViewServicePtr viewService, PatientModelServicePtr patientModelService, QWidget *parent, SelectDataStringPropertyBasePtr data, QGridLayout* gridLayout, int row) :
27  BaseWidget(parent, "DataSelectWidget", "DataSelectWidget"),
28  mData(data),
29  mViewService(viewService),
30  mPatientModelService(patientModelService)
31 {
32 
33  QHBoxLayout* layout = new QHBoxLayout(this);
34  layout->setMargin(0);
35  layout->setSpacing(0);
36 
37  QWidget* widget = sscCreateDataWidget(this, mData, gridLayout, row);
38 
39  mToggleShowAction = this->createAction(this,
40  QIcon(":/icons/open_icon_library/eye.png.png"),
41  "Toggle show data in view", "",
42  SLOT(toggleShowData()),
43  NULL);
44  mToggleShowAction->setCheckable(true);
45  CXSmallToolButton* toggleShowButton = new CXSmallToolButton();
46  toggleShowButton->setDefaultAction(mToggleShowAction);
47 
48  mRemoveButton = new EraseDataToolButton(this);
49  connect(mRemoveButton, &EraseDataToolButton::eraseData, this, &DataSelectWidget::eraseData);
50 
51  if(gridLayout)
52  {
53  gridLayout->setMargin(0);
54  gridLayout->setSpacing(0);
55  QHBoxLayout* lay = new QHBoxLayout;
56  lay->addWidget(toggleShowButton);
57  lay->addWidget(mRemoveButton);
58  gridLayout->addLayout(lay, row, 2);
59  }else
60  {
61  layout->addWidget(widget);
62  layout->addWidget(toggleShowButton);
63  layout->addWidget(mRemoveButton);
64  }
65 
66  connect(mViewService.get(), SIGNAL(activeViewChanged()), this, SLOT(viewGroupChangedSlot()));
67  connect(mData.get(), SIGNAL(changed()), this, SLOT(updateDataVisibility()));
68 
69  this->viewGroupChangedSlot();
70 }
71 
73 {
74  disconnect(mViewService.get(), SIGNAL(activeViewChanged()), this, SLOT(viewGroupChangedSlot()));
75 }
76 
77 ViewGroupDataPtr DataSelectWidget::getActiveViewGroupData()
78 {
79  int groupIdx = mViewService->getActiveGroupId();
80  if (groupIdx<0)
81  groupIdx = 0;
82  return mViewService->getGroup(groupIdx);
83 }
84 
85 void DataSelectWidget::viewGroupChangedSlot()
86 {
87  ViewGroupDataPtr group = this->getActiveViewGroupData();
88  if (mCurrentViewGroupData==group)
89  return;
90 
91  if (mCurrentViewGroupData)
92  {
93  disconnect(mCurrentViewGroupData.get(), &ViewGroupData::dataViewPropertiesChanged,
94  this, &DataSelectWidget::updateDataVisibility);
95  }
96 
97  mCurrentViewGroupData = group;
98 
99  if (mCurrentViewGroupData)
100  {
101  connect(mCurrentViewGroupData.get(), &ViewGroupData::dataViewPropertiesChanged,
102  this, &DataSelectWidget::updateDataVisibility);
103  }
104 
105  this->updateDataVisibility();
106 }
107 
108 void DataSelectWidget::updateDataVisibility()
109 {
110  mToggleShowAction->setEnabled(mData->getData() && (mCurrentViewGroupData!=0));
111  mRemoveButton->setEnabled(mData->getData() ? true : false);
112 
113  bool visible = false;
114  if (mData->getData())
115  {
116  std::vector<DataPtr> visibleData;
117  if (mCurrentViewGroupData)
118  {
119  visibleData = mCurrentViewGroupData->getData();
120  }
121  visible = std::count(visibleData.begin(), visibleData.end(), mData->getData());
122  }
123  mToggleShowAction->blockSignals(true);
124  mToggleShowAction->setChecked(visible);
125  mToggleShowAction->blockSignals(false);
126  mRemoveButton->reset();
127  this->setShowIcon();
128 }
129 
130 void DataSelectWidget::eraseData()
131 {
132  if (!mData->getData())
133  return;
134 
135  mPatientModelService->removeData(mData->getData()->getUid());
136 }
137 
138 void DataSelectWidget::setShowIcon()
139 {
140  if (mToggleShowAction->isChecked())
141  {
142  mToggleShowAction->setIcon(QIcon(":/icons/open_icon_library/eye.png.png"));
143  }
144  else
145  {
146  mToggleShowAction->setIcon(QIcon(":/icons/eye.png"));
147  }
148 }
149 
150 void DataSelectWidget::toggleShowData()
151 {
152  if (!mData->getData())
153  return;
154 
155  if (mToggleShowAction->isChecked())
156  {
157  mCurrentViewGroupData->addData(mData->getData()->getUid());
158  }
159  else
160  {
161  mCurrentViewGroupData->removeData(mData->getData()->getUid());
162  }
163 }
164 
165 //---------------------------------------------------------
166 //---------------------------------------------------------
167 //---------------------------------------------------------
168 
170  CXSmallToolButton(parent)
171 {
172  QString tip("<html><h4>Permanently delete data.</h4><p>Press button twice to delete.<br>"
173  "Right-click after the first click to cancel.<p></html>");
174 
175  mRemoveAction = new QAction(this);
176  mRemoveAction->setToolTip(tip);
177  connect(mRemoveAction, &QAction::triggered, this, &EraseDataToolButton::requestEraseData);
178  mRemoveAction->setCheckable(true);
179  this->setDefaultAction(mRemoveAction);
180 
181  this->setRemoveIcon();
182 }
183 
185 {
186  this->cancelRemovalSlot();
187 }
188 
189 void EraseDataToolButton::mousePressEvent(QMouseEvent *e)
190 {
191  CXSmallToolButton::mousePressEvent(e);
192  if (e->button() == Qt::RightButton)
193  this->cancelRemovalSlot();
194 }
195 
199 void EraseDataToolButton::cancelRemovalSlot()
200 {
201  mRemoveAction->blockSignals(true);
202  mRemoveAction->setChecked(false);
203  this->setRemoveIcon();
204  mRemoveAction->blockSignals(false);
205 }
206 
207 void EraseDataToolButton::setRemoveIcon()
208 {
209  if (mRemoveAction->isChecked())
210  {
211  mRemoveAction->setIcon(QIcon(":/icons/preset_remove.png"));
212  }
213  else
214  {
215  mRemoveAction->setIcon(QIcon(":/icons/open_icon_library/edit-delete-2.png"));
216  }
217 }
218 
223 void EraseDataToolButton::requestEraseData()
224 {
225  this->setRemoveIcon();
226 
227  if (mRemoveAction->isChecked())
228  return;
229 
230  emit eraseData();
231 }
232 
233 
234 } // namespace cx
235 
236 
cxLogger.h
cx::ViewGroupDataPtr
boost::shared_ptr< class ViewGroupData > ViewGroupDataPtr
Definition: cxViewGroup.h:29
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::BaseWidget
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:88
CXSmallToolButton
Definition: cxBaseWidget.h:66
cx::ViewGroupData::dataViewPropertiesChanged
void dataViewPropertiesChanged(QString uid)
cxViewGroupData.h
cx::BaseWidget::createAction
QAction * createAction(QObject *parent, QIcon iconName, QString text, QString tip, T slot, QLayout *layout=NULL, QToolButton *button=new QToolButton())
Definition: cxBaseWidget.h:129
cxHelperWidgets.h
cx::EraseDataToolButton::eraseData
void eraseData()
indicates that erase has been requested by user.
cxData.h
cxViewService.h
cx::PatientModelServicePtr
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
Definition: cxLogicManager.h:25
cx::ViewServicePtr
boost::shared_ptr< class ViewService > ViewServicePtr
Definition: cxLogicManager.h:28
cxTypeConversions.h
cxPatientModelService.h
cx::EraseDataToolButton
Definition: cxDataSelectWidget.h:72
cx::DataSelectWidget::~DataSelectWidget
~DataSelectWidget()
Definition: cxDataSelectWidget.cpp:72
cx::EraseDataToolButton::reset
void reset()
reset button to initial state
Definition: cxDataSelectWidget.cpp:184
cxDataSelectWidget.h
cx::EraseDataToolButton::EraseDataToolButton
EraseDataToolButton(QWidget *parent)
Definition: cxDataSelectWidget.cpp:169
cx::sscCreateDataWidget
QWidget * sscCreateDataWidget(QWidget *parent, PropertyPtr data, QGridLayout *gridLayout, int row)
Create a widget capable of displaying the input data.
Definition: cxHelperWidgets.cpp:59
cxSelectDataStringProperty.h
cx::DataSelectWidget::DataSelectWidget
DataSelectWidget(ViewServicePtr viewService, PatientModelServicePtr patientModelService, QWidget *parent, SelectDataStringPropertyBasePtr data, QGridLayout *gridLayout=NULL, int row=0)
Definition: cxDataSelectWidget.cpp:26
cx::SelectDataStringPropertyBasePtr
boost::shared_ptr< class SelectDataStringPropertyBase > SelectDataStringPropertyBasePtr
Definition: cxMeshGlyphsWidget.h:26