Fraxinus  16.5.0-fx-rc5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include "cxDataSelectWidget.h"
34 
35 #include "cxHelperWidgets.h"
37 #include "cxTypeConversions.h"
38 #include "cxViewService.h"
39 #include "cxPatientModelService.h"
40 #include "cxData.h"
41 #include "cxViewGroupData.h"
42 #include "cxLogger.h"
43 
44 namespace cx
45 {
46 
47 DataSelectWidget::DataSelectWidget(ViewServicePtr viewService, PatientModelServicePtr patientModelService, QWidget *parent, SelectDataStringPropertyBasePtr data, QGridLayout* gridLayout, int row) :
48  BaseWidget(parent, "DataSelectWidget", "DataSelectWidget"),
49  mData(data),
50  mViewService(viewService),
51  mPatientModelService(patientModelService)
52 {
53 
54  QHBoxLayout* layout = new QHBoxLayout(this);
55  layout->setMargin(0);
56  layout->setSpacing(0);
57 
58  QWidget* widget = sscCreateDataWidget(this, mData, gridLayout, row);
59 
60  mToggleShowAction = this->createAction(this,
61  QIcon(":/icons/open_icon_library/eye.png.png"),
62  "Toggle show data in view", "",
63  SLOT(toggleShowData()),
64  NULL);
65  mToggleShowAction->setCheckable(true);
66  CXSmallToolButton* toggleShowButton = new CXSmallToolButton();
67  toggleShowButton->setDefaultAction(mToggleShowAction);
68 
69  mRemoveButton = new EraseDataToolButton(this);
70  connect(mRemoveButton, &EraseDataToolButton::eraseData, this, &DataSelectWidget::eraseData);
71 
72  if(gridLayout)
73  {
74  gridLayout->setMargin(0);
75  gridLayout->setSpacing(0);
76  QHBoxLayout* lay = new QHBoxLayout;
77  lay->addWidget(toggleShowButton);
78  lay->addWidget(mRemoveButton);
79  gridLayout->addLayout(lay, row, 2);
80  }else
81  {
82  layout->addWidget(widget);
83  layout->addWidget(toggleShowButton);
84  layout->addWidget(mRemoveButton);
85  }
86 
87  connect(mViewService.get(), SIGNAL(activeViewChanged()), this, SLOT(viewGroupChangedSlot()));
88  connect(mData.get(), SIGNAL(changed()), this, SLOT(updateDataVisibility()));
89 
90  this->viewGroupChangedSlot();
91 }
92 
94 {
95  disconnect(mViewService.get(), SIGNAL(activeViewChanged()), this, SLOT(viewGroupChangedSlot()));
96 }
97 
98 ViewGroupDataPtr DataSelectWidget::getActiveViewGroupData()
99 {
100  int groupIdx = mViewService->getActiveGroupId();
101  if (groupIdx<0)
102  groupIdx = 0;
103  return mViewService->getGroup(groupIdx);
104 }
105 
106 void DataSelectWidget::viewGroupChangedSlot()
107 {
108  ViewGroupDataPtr group = this->getActiveViewGroupData();
109  if (mCurrentViewGroup==group)
110  return;
111 
112  if (mCurrentViewGroup)
113  {
114  disconnect(mCurrentViewGroup.get(), &ViewGroupData::dataViewPropertiesChanged,
115  this, &DataSelectWidget::updateDataVisibility);
116  }
117 
118  mCurrentViewGroup = group;
119 
120  if (mCurrentViewGroup)
121  {
122  connect(mCurrentViewGroup.get(), &ViewGroupData::dataViewPropertiesChanged,
123  this, &DataSelectWidget::updateDataVisibility);
124  }
125 
126  this->updateDataVisibility();
127 }
128 
129 void DataSelectWidget::updateDataVisibility()
130 {
131  mToggleShowAction->setEnabled(mData->getData() && (mCurrentViewGroup!=0));
132  mRemoveButton->setEnabled(mData->getData() ? true : false);
133 
134  bool visible = false;
135  if (mData->getData())
136  {
137  std::vector<DataPtr> visibleData;
138  if (mCurrentViewGroup)
139  {
140  visibleData = mCurrentViewGroup->getData();
141  }
142  visible = std::count(visibleData.begin(), visibleData.end(), mData->getData());
143  }
144  mToggleShowAction->blockSignals(true);
145  mToggleShowAction->setChecked(visible);
146  mToggleShowAction->blockSignals(false);
147  mRemoveButton->reset();
148  this->setShowIcon();
149 }
150 
151 void DataSelectWidget::eraseData()
152 {
153  if (!mData->getData())
154  return;
155 
156  mPatientModelService->removeData(mData->getData()->getUid());
157 }
158 
159 void DataSelectWidget::setShowIcon()
160 {
161  if (mToggleShowAction->isChecked())
162  {
163  mToggleShowAction->setIcon(QIcon(":/icons/open_icon_library/eye.png.png"));
164  }
165  else
166  {
167  mToggleShowAction->setIcon(QIcon(":/icons/eye.png"));
168  }
169 }
170 
171 void DataSelectWidget::toggleShowData()
172 {
173  if (!mData->getData())
174  return;
175 
176  if (mToggleShowAction->isChecked())
177  {
178  mCurrentViewGroup->addData(mData->getData()->getUid());
179  }
180  else
181  {
182  mCurrentViewGroup->removeData(mData->getData()->getUid());
183  }
184 }
185 
186 //---------------------------------------------------------
187 //---------------------------------------------------------
188 //---------------------------------------------------------
189 
191  CXSmallToolButton(parent)
192 {
193  QString tip("<html><h4>Permanently delete data.</h4><p>Press button twice to delete.<br>"
194  "Right-click after the first click to cancel.<p></html>");
195 
196  mRemoveAction = new QAction(this);
197  mRemoveAction->setToolTip(tip);
198  connect(mRemoveAction, &QAction::triggered, this, &EraseDataToolButton::requestEraseData);
199  mRemoveAction->setCheckable(true);
200  this->setDefaultAction(mRemoveAction);
201 
202  this->setRemoveIcon();
203 }
204 
206 {
207  this->cancelRemovalSlot();
208 }
209 
210 void EraseDataToolButton::mousePressEvent(QMouseEvent *e)
211 {
212  CXSmallToolButton::mousePressEvent(e);
213  if (e->button() == Qt::RightButton)
214  this->cancelRemovalSlot();
215 }
216 
220 void EraseDataToolButton::cancelRemovalSlot()
221 {
222  mRemoveAction->blockSignals(true);
223  mRemoveAction->setChecked(false);
224  this->setRemoveIcon();
225  mRemoveAction->blockSignals(false);
226 }
227 
228 void EraseDataToolButton::setRemoveIcon()
229 {
230  if (mRemoveAction->isChecked())
231  {
232  mRemoveAction->setIcon(QIcon(":/icons/preset_remove.png"));
233  }
234  else
235  {
236  mRemoveAction->setIcon(QIcon(":/icons/open_icon_library/edit-delete-2.png"));
237  }
238 }
239 
244 void EraseDataToolButton::requestEraseData()
245 {
246  this->setRemoveIcon();
247 
248  if (mRemoveAction->isChecked())
249  return;
250 
251  emit eraseData();
252 }
253 
254 
255 } // namespace cx
256 
257 
boost::shared_ptr< class ViewGroupData > ViewGroupDataPtr
Definition: cxViewGroup.h:50
EraseDataToolButton(QWidget *parent)
boost::shared_ptr< class ViewService > ViewServicePtr
QAction * createAction(QObject *parent, QIcon iconName, QString text, QString tip, T slot, QLayout *layout=NULL, QToolButton *button=new QToolButton())
Definition: cxBaseWidget.h:149
boost::shared_ptr< class SelectDataStringPropertyBase > SelectDataStringPropertyBasePtr
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
void dataViewPropertiesChanged(QString uid)
void reset()
reset button to initial state
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:108
void eraseData()
indicates that erase has been requested by user.
cxLogicManager_EXPORT ViewServicePtr viewService()
QWidget * sscCreateDataWidget(QWidget *parent, PropertyPtr data, QGridLayout *gridLayout, int row)
Create a widget capable of displaying the input data.
DataSelectWidget(ViewServicePtr viewService, PatientModelServicePtr patientModelService, QWidget *parent, SelectDataStringPropertyBasePtr data, QGridLayout *gridLayout=NULL, int row=0)