fw4spl
Selector.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "uiMedDataQt/widget/Selector.hpp"
8 
9 #include <fwData/Image.hpp>
10 
11 #include <fwMedData/Equipment.hpp>
12 #include <fwMedData/ImageSeries.hpp>
13 #include <fwMedData/Patient.hpp>
14 #include <fwMedData/Series.hpp>
15 #include <fwMedData/Study.hpp>
16 
17 #include <QItemSelectionModel>
18 #include <QKeyEvent>
19 #include <QModelIndexList>
20 #include <QStandardItem>
21 #include <QString>
22 
23 namespace uiMedDataQt
24 {
25 namespace widget
26 {
27 
28 //-----------------------------------------------------------------------------
29 
30 Selector::Selector(QWidget* parent) :
31  QTreeView(parent),
32  m_allowedRemove(true)
33 {
34  m_model = new SelectorModel();
35  this->setModel(m_model);
36 
37  this->setSelectionMode(QAbstractItemView::ExtendedSelection);
38  this->setAlternatingRowColors( true );
39  this->setDragEnabled(true);
40 }
41 
42 //-----------------------------------------------------------------------------
43 
45 {
46 }
47 
48 //-----------------------------------------------------------------------------
49 
51 {
52  m_model->clear();
53 }
54 
55 //-----------------------------------------------------------------------------
56 
57 void Selector::setInsertMode(bool insert)
58 {
59  m_model->setInsertMode(insert);
60 }
61 
62 //-----------------------------------------------------------------------------
63 
64 void Selector::addSeries(::fwMedData::Series::sptr series)
65 {
66  m_model->addSeries(series);
67  QStandardItem* studyItem = m_model->findStudyItem(series->getStudy());
68  this->expand(m_model->indexFromItem(studyItem));
69 
70  for (int i = 0; i < m_model->columnCount(); ++i)
71  {
72  this->resizeColumnToContents(i);
73  }
74 }
75 
76 //-----------------------------------------------------------------------------
77 
78 void Selector::removeSeries(::fwMedData::Series::sptr series)
79 {
80  m_model->removeSeries(series);
81 }
82 
83 //-----------------------------------------------------------------------------
84 
85 void Selector::setAllowedRemove(bool allowed)
86 {
87  m_allowedRemove = allowed;
88 }
89 
90 //-----------------------------------------------------------------------------
91 
92 void Selector::selectionChanged( const QItemSelection& selected, const QItemSelection& deselected )
93 {
94  QTreeView::selectionChanged(selected, deselected);
95 
96  SeriesVectorType selectedSeries = this->getSeries(selected);
97 
98  SeriesVectorType deselectedSeries = this->getSeries(deselected);
99 
100  Q_EMIT selectSeries(selectedSeries, deselectedSeries);
101 }
102 
103 //-----------------------------------------------------------------------------
104 
105 Selector::SeriesVectorType Selector::getSeries( const QItemSelection& selection )
106 {
107  SeriesVectorType vSeries;
108 
109  QModelIndexList selectedIndexes = selection.indexes();
110  vSeries = this->getSeries(selectedIndexes);
111  return vSeries;
112 }
113 
114 //-----------------------------------------------------------------------------
115 
116 Selector::SeriesVectorType Selector::getSeries(const QModelIndexList& indexList)
117 {
118  SeriesVectorType vSeries;
119  for(QModelIndex index : indexList)
120  {
121  std::string uid = index.data(SelectorModel::UID).toString().toStdString();
122  ::fwTools::Object::sptr obj = ::fwTools::fwID::getObject(uid);
123 
125  {
126  ::fwMedData::Series::sptr series = ::fwMedData::Series::dynamicCast(obj);
127  vSeries.push_back(series);
128  }
129  }
130  return vSeries;
131 }
132 
133 //-----------------------------------------------------------------------------
134 
135 QModelIndexList Selector::getStudyIndexes(const QModelIndexList& indexList)
136 {
137  QModelIndexList studiesIndex;
138  for(QModelIndex index : indexList)
139  {
141  {
142  studiesIndex.push_back(index);
143  }
144  }
145  return studiesIndex;
146 }
147 
148 //-----------------------------------------------------------------------------
149 
150 Selector::SeriesVectorType Selector::getSeriesFromStudyIndex(const QModelIndex& index )
151 {
152  SeriesVectorType vSeries;
153  QStandardItem* item = m_model->itemFromIndex(index);
154  int nbRow = item->rowCount();
155  for(int row = 0; row < nbRow; ++row)
156  {
157  QStandardItem* child = item->child(row);
158  std::string uid = child->data(SelectorModel::UID).toString().toStdString();
159  SLM_ASSERT("UID must not be empty.", !uid.empty());
160  ::fwTools::Object::sptr obj = ::fwTools::fwID::getObject(uid);
161  ::fwMedData::Series::sptr series = ::fwMedData::Series::dynamicCast(obj);
162  vSeries.push_back(series);
163  }
164  return vSeries;
165 }
166 
167 //-----------------------------------------------------------------------------
168 
170 {
171  return m_model->getItemType(index);
172 }
173 
174 //-----------------------------------------------------------------------------
175 
176 void Selector::keyPressEvent(QKeyEvent* event)
177 {
178  if(event->matches(QKeySequence::Delete) && m_allowedRemove)
179  {
180  this->deleteSelection();
181  event->accept();
182  }
183  else
184  {
185  QTreeView::keyPressEvent(event);
186  }
187 }
188 
189 //-----------------------------------------------------------------------------
190 
192 {
193  QModelIndexList selection = this->selectionModel()->selectedRows(0);
194 
195  SeriesVectorType vSeries = this->getSeries(selection);
196  QModelIndexList studyIndexes = this->getStudyIndexes(selection);
197  for(QModelIndex index : studyIndexes)
198  {
199  SeriesVectorType series = getSeriesFromStudyIndex(index);
200  std::copy(series.begin(), series.end(), std::back_inserter(vSeries));
201  }
202 
203  Q_EMIT removeSeries(vSeries);
204 
205  // Remove item in Selector.
206  m_model->removeRows(selection);
207 }
208 
209 //-----------------------------------------------------------------------------
210 
211 void Selector::setSeriesIcons(const SeriesIconType& seriesIcons)
212 {
213  m_model->setSeriesIcons(seriesIcons);
214 }
215 
216 //-----------------------------------------------------------------------------
217 
218 } // namespace widget
219 } // namespace uiMedDataQt
The namespace uiMedDataQt contains editors for medical data.
void deleteSelection()
Deletes the selected items and notify the deleted series.
Definition: Selector.cpp:191
UIMEDDATAQT_API void addSeries(::fwMedData::Series::sptr series)
Adds the Series in the tree. If the associated study already exists in the tree, the series is added ...
Definition: Selector.cpp:64
Role for the fwID of the object.
SelectorModel::SeriesIconType SeriesIconType
Map associating icons to series (map<series classname, icon path>)
Definition: Selector.hpp:43
Type to represent Study/Patient.
ItemType
Defines item type (STUDY or SERIES), it is used in items data (ITEM_TYPE role).
UIMEDDATAQT_API ~Selector()
Destrucotr.
Definition: Selector.cpp:44
UIMEDDATAQT_API void setSeriesIcons(const SeriesIconType &seriesIcons)
Sets the specific icons for series in selector.
Definition: Selector.cpp:211
UIMEDDATAQT_API void clear()
Clear all items in the tree.
Definition: Selector.cpp:50
UIMEDDATAQT_API void removeSeries(::fwMedData::Series::sptr series)
Removes the Series from the tree. After deletion, if the study is empty, it will be removed...
Definition: Selector.cpp:78
QModelIndexList getStudyIndexes(const QModelIndexList &indexList)
Returns all the study indexes contained in the given indexList.
Definition: Selector.cpp:135
UIMEDDATAQT_API SelectorModel::ItemType getItemType(const QModelIndex &index)
Returns the type of the item (SERIES or STUDY)
Definition: Selector.cpp:169
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
SeriesVectorType getSeriesFromStudyIndex(const QModelIndex &index)
Returns all the series associated with the study index.
Definition: Selector.cpp:150
UIMEDDATAQT_API void setAllowedRemove(bool allowed)
Allows removing items or not.
Definition: Selector.cpp:85
This class represents the Selector Model.
Role for the item type (STUDY or SERIES)
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
Slot called when the selection changed. Emits a signal containing the new selected/deselected series...
Definition: Selector.cpp:92
void selectSeries(QVector< ::fwMedData::Series::sptr > selection, QVector< ::fwMedData::Series::sptr > deselection)
Signal emitted when the selection change.
UIMEDDATAQT_API void setInsertMode(bool insert)
Sets if the selector must be in insert mode.
Definition: Selector.cpp:57
UIMEDDATAQT_API void keyPressEvent(QKeyEvent *event)
Catch the delete key event and remove the selected items.
Definition: Selector.cpp:176
SeriesVectorType getSeries(const QItemSelection &selection)
Returns all the Series associated to the selection.
Definition: Selector.cpp:105
UIMEDDATAQT_API Selector(QWidget *parent=0)
Constructor. Init tree view.
Definition: Selector.cpp:30
static FWTOOLS_API std::shared_ptr< ::fwTools::Object > getObject(IDType requestID)
Retrieve the object attached to the given id. Return a null sptr if no correspondence exist...
Definition: fwID.cpp:117