CustusX  15.8
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->getActiveGroupId();
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(), &ViewGroupData::dataViewPropertiesChanged,
124  this, &DataSelectWidget::updateDataVisibility);
125  }
126 
127  mCurrentViewGroup = group;
128 
129  if (mCurrentViewGroup)
130  {
131  connect(mCurrentViewGroup.get(), &ViewGroupData::dataViewPropertiesChanged,
132  this, &DataSelectWidget::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 
164 void DataSelectWidget::requestEraseData()
165 {
166  this->setRemoveIcon();
167 
168  if (mRemoveAction->isChecked())
169  {
170  return;
171  }
172  if (!mData->getData())
173  return;
174 
175  mPatientModelService->removeData(mData->getData()->getUid());
176 }
177 
178 void DataSelectWidget::setRemoveIcon()
179 {
180  if (mRemoveAction->isChecked())
181  {
182  mRemoveAction->setIcon(QIcon(":/icons/preset_remove.png"));
183  }
184  else
185  {
186  mRemoveAction->setIcon(QIcon(":/icons/open_icon_library/edit-delete-2.png"));
187  }
188 }
189 
190 void DataSelectWidget::setShowIcon()
191 {
192  if (mToggleShowAction->isChecked())
193  {
194  mToggleShowAction->setIcon(QIcon(":/icons/open_icon_library/eye.png.png"));
195  }
196  else
197  {
198  mToggleShowAction->setIcon(QIcon(":/icons/eye.png"));
199  }
200 }
201 
205 void DataSelectWidget::cancelRemovalSlot()
206 {
207  mRemoveAction->blockSignals(true);
208  mRemoveAction->setChecked(false);
209  this->setRemoveIcon();
210  mRemoveAction->blockSignals(false);
211 }
212 
213 void DataSelectWidget::toggleShowData()
214 {
215  if (!mData->getData())
216  return;
217 
218  if (mToggleShowAction->isChecked())
219  {
220  mCurrentViewGroup->addData(mData->getData()->getUid());
221  }
222  else
223  {
224  mCurrentViewGroup->removeData(mData->getData()->getUid());
225  }
226 }
227 
228 
229 } // namespace cx
230 
231 
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:129
boost::shared_ptr< class SelectDataStringPropertyBase > SelectDataStringPropertyBasePtr
boost::shared_ptr< class VisualizationService > VisualizationServicePtr
Definition: cxRegServices.h:43
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
void dataViewPropertiesChanged(QString uid)
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.