CustusX  15.3.4-beta
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 
43 namespace cx
44 {
45 
46 DataSelectWidget::DataSelectWidget(VisualizationServicePtr visualizationService, PatientModelServicePtr patientModelService, QWidget *parent, SelectDataStringPropertyBasePtr data, QGridLayout* gridLayout, int row) :
47  BaseWidget(parent, "DataSelectWidget", "DataSelectWidget"),
48  mData(data),
49  mVisualizationService(visualizationService),
50  mPatientModelService(patientModelService)
51 {
52 
53  QHBoxLayout* layout = new QHBoxLayout(this);
54  layout->setMargin(0);
55  layout->setSpacing(0);
56 
57  QWidget* widget = sscCreateDataWidget(this, mData, gridLayout, row);
58 
59  mToggleShowAction = this->createAction(this,
60  QIcon(":/icons/open_icon_library/eye.png.png"),
61  "Toggle show data in view", "",
62  SLOT(toggleShowData()),
63  NULL);
64  mToggleShowAction->setCheckable(true);
65  CXSmallToolButton* toggleShowButton = new CXSmallToolButton();
66  toggleShowButton->setDefaultAction(mToggleShowAction);
67 
68  mRemoveAction = this->createAction(this,
69  QIcon(),
70  "<html><h4>Permanently delete data.</h4><p>Press button twice to delete.<br>"
71  "Right-click after the first click to cancel.<p></html>"
72  , "",
73  SLOT(requestEraseData()),
74  NULL);
75  mRemoveAction->setCheckable(true);
76  EraseDataToolButton* removeButton = new EraseDataToolButton(this);
77  connect(removeButton, SIGNAL(rightClick()), this, SLOT(cancelRemovalSlot()));
78  removeButton->setDefaultAction(mRemoveAction);
79 
80  if(gridLayout)
81  {
82  gridLayout->setMargin(0);
83  gridLayout->setSpacing(0);
84  QHBoxLayout* lay = new QHBoxLayout;
85  lay->addWidget(toggleShowButton);
86  lay->addWidget(removeButton);
87  gridLayout->addLayout(lay, row, 2);
88  }else
89  {
90  layout->addWidget(widget);
91  layout->addWidget(toggleShowButton);
92  layout->addWidget(removeButton);
93  }
94 
95  connect(mVisualizationService.get(), SIGNAL(activeViewChanged()), this, SLOT(viewGroupChangedSlot()));
96  connect(mData.get(), SIGNAL(changed()), this, SLOT(updateDataVisibility()));
97 
98  this->setRemoveIcon();
99  this->viewGroupChangedSlot();
100 }
101 
103 {
104  disconnect(mVisualizationService.get(), SIGNAL(activeViewChanged()), this, SLOT(viewGroupChangedSlot()));
105 }
106 
107 ViewGroupDataPtr DataSelectWidget::getActiveViewGroupData()
108 {
109  int groupIdx = mVisualizationService->getActiveGroup();
110  if (groupIdx<0)
111  groupIdx = 0;
112  return mVisualizationService->getGroup(groupIdx);
113 }
114 
115 void DataSelectWidget::viewGroupChangedSlot()
116 {
117  ViewGroupDataPtr group = this->getActiveViewGroupData();
118  if (mCurrentViewGroup==group)
119  return;
120 
121  if (mCurrentViewGroup)
122  {
123  disconnect(mCurrentViewGroup.get(), SIGNAL(dataViewPropertiesChanged(QString)),
124  this, SLOT(updateDataVisibility()));
125  }
126 
127  mCurrentViewGroup = group;
128 
129  if (mCurrentViewGroup)
130  {
131  connect(mCurrentViewGroup.get(), SIGNAL(dataViewPropertiesChanged(QString)),
132  this, SLOT(updateDataVisibility()));
133  }
134 
135  this->updateDataVisibility();
136 }
137 
138 void DataSelectWidget::updateDataVisibility()
139 {
140  mToggleShowAction->setEnabled(mData->getData() && (mCurrentViewGroup!=0));
141  mRemoveAction->setEnabled(mData->getData() ? true : false);
142 
143  bool visible = false;
144  if (mData->getData())
145  {
146  std::vector<DataPtr> visibleData;
147  if (mCurrentViewGroup)
148  {
149  visibleData = mCurrentViewGroup->getData();
150  }
151  visible = std::count(visibleData.begin(), visibleData.end(), mData->getData());
152  }
153  mToggleShowAction->blockSignals(true);
154  mToggleShowAction->setChecked(visible);
155  mToggleShowAction->blockSignals(false);
156  this->cancelRemovalSlot();
157  this->setShowIcon();
158 }
159 
161 {
162  return "<html></html>";
163 }
164 
169 void DataSelectWidget::requestEraseData()
170 {
171  this->setRemoveIcon();
172 
173  if (mRemoveAction->isChecked())
174  {
175  return;
176  }
177  if (!mData->getData())
178  return;
179 
180  mPatientModelService->removeData(mData->getData()->getUid());
181 }
182 
183 void DataSelectWidget::setRemoveIcon()
184 {
185  if (mRemoveAction->isChecked())
186  {
187  mRemoveAction->setIcon(QIcon(":/icons/preset_remove.png"));
188  }
189  else
190  {
191  mRemoveAction->setIcon(QIcon(":/icons/open_icon_library/edit-delete-2.png"));
192  }
193 }
194 
195 void DataSelectWidget::setShowIcon()
196 {
197  if (mToggleShowAction->isChecked())
198  {
199  mToggleShowAction->setIcon(QIcon(":/icons/open_icon_library/eye.png.png"));
200  }
201  else
202  {
203  mToggleShowAction->setIcon(QIcon(":/icons/eye.png"));
204  }
205 }
206 
210 void DataSelectWidget::cancelRemovalSlot()
211 {
212  mRemoveAction->blockSignals(true);
213  mRemoveAction->setChecked(false);
214  this->setRemoveIcon();
215  mRemoveAction->blockSignals(false);
216 }
217 
218 void DataSelectWidget::toggleShowData()
219 {
220  if (!mData->getData())
221  return;
222 
223  if (mToggleShowAction->isChecked())
224  {
225  mCurrentViewGroup->addData(mData->getData()->getUid());
226  }
227  else
228  {
229  mCurrentViewGroup->removeData(mData->getData()->getUid());
230  }
231 }
232 
233 
234 } // namespace cx
235 
236 
QString defaultWhatsThis() const
Returns a short description of what this widget will do for you.
boost::shared_ptr< class ViewGroupData > ViewGroupDataPtr
Definition: cxViewGroup.h:50
QAction * createAction(QObject *parent, QIcon iconName, QString text, QString tip, T slot, QLayout *layout=NULL, QToolButton *button=new QToolButton())
Definition: cxBaseWidget.h:130
boost::shared_ptr< class SelectDataStringPropertyBase > SelectDataStringPropertyBasePtr
boost::shared_ptr< class VisualizationService > VisualizationServicePtr
Definition: cxRegServices.h:43
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
DataSelectWidget(VisualizationServicePtr visualizationService, PatientModelServicePtr patientModelService, QWidget *parent, SelectDataStringPropertyBasePtr data, QGridLayout *gridLayout=NULL, int row=0)
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:108
QWidget * sscCreateDataWidget(QWidget *parent, PropertyPtr data, QGridLayout *gridLayout, int row)
Create a widget capable of displaying the input data.