Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
QmitkLevelWindowPresetDefinitionDialog.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 
19 #include <QHeaderView>
20 #include <QMessageBox>
21 #include <QTableWidgetItem>
22 
24  : QDialog(parent, f), m_TableModel(nullptr), m_SortModel(this)
25 {
26  this->setupUi(this);
27 
28  QObject::connect(addButton, SIGNAL(clicked()), this, SLOT(addPreset()));
29  QObject::connect(removeButton, SIGNAL(clicked()), this, SLOT(removePreset()));
30  QObject::connect(changeButton, SIGNAL(clicked()), this, SLOT(changePreset()));
31 
32  QObject::connect(presetView->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(sortPresets(int)));
33 
34  presetView->verticalHeader()->setVisible(false);
35  presetView->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
36 
37  presetView->setModel(&m_SortModel);
38 }
39 
41 {
42  delete m_TableModel;
43 }
44 
46 {
47  static Qt::SortOrder order[3] = {Qt::AscendingOrder};
48 
49  presetView->sortByColumn(index, order[index]);
50  if (order[index] == Qt::AscendingOrder)
51  order[index] = Qt::DescendingOrder;
52  else
53  order[index] = Qt::AscendingOrder;
54 }
55 
57 {
58  QDialog::resizeEvent(event);
59 
60  this->resizeColumns();
61 }
62 
64 {
65  this->resizeColumns();
66 
67  QDialog::showEvent(event);
68 }
69 
71 {
72  int width = presetView->viewport()->size().width() - presetView->columnWidth(1) - presetView->columnWidth(2);
73  if (width < 50)
74  width = 50;
75 
76  presetView->setColumnWidth(0, width);
77 }
78 
80 {
81  std::string name(presetnameLineEdit->text().toStdString());
82  if (m_TableModel->contains(name))
83  {
84  QMessageBox::critical(this,
85  "Preset definition",
86  "Presetname already exists.\n"
87  "You have to enter another one.");
88  }
89  else if (presetnameLineEdit->text() == "")
90  {
91  QMessageBox::critical(this,
92  "Preset definition",
93  "Presetname has to be set.\n"
94  "You have to enter a Presetname.");
95  }
96  else
97  {
98  m_TableModel->addPreset(name, levelSpinBox->value(), windowSpinBox->value());
99  }
100 }
101 
103 {
104  QModelIndex index(presetView->selectionModel()->currentIndex());
105  if (!index.isValid())
106  return;
107 
108  m_TableModel->removePreset(index);
109 }
110 
112 {
113  if (presetView->selectionModel()->hasSelection())
114  {
115  std::string name(presetnameLineEdit->text().toStdString());
116  if (name == "")
117  {
118  QMessageBox::critical(this,
119  "Preset definition",
120  "Presetname has to be set.\n"
121  "You have to enter a Presetname.");
122  }
123  else if (m_TableModel->contains(name) &&
124  (m_TableModel->getPreset(presetView->selectionModel()->currentIndex()).name != name))
125  {
126  QMessageBox::critical(this,
127  "Preset definition",
128  "Presetname already exists.\n"
129  "You have to enter another one.");
130  }
131  else
132  {
134  presetView->selectionModel()->currentIndex().row(), name, levelSpinBox->value(), windowSpinBox->value());
135  }
136  }
137 }
138 
139 void QmitkLevelWindowPresetDefinitionDialog::setPresets(std::map<std::string, double> &level,
140  std::map<std::string, double> &window,
141  QString initLevel,
142  QString initWindow)
143 {
144  levelSpinBox->setValue(initLevel.toInt());
145  windowSpinBox->setValue(initWindow.toInt());
146 
147  delete m_TableModel;
148  m_TableModel = new PresetTableModel(level, window, this);
149 
150  m_SortModel.setSourceModel(m_TableModel);
151 
152  QObject::connect(presetView->selectionModel(),
153  SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
154  this,
155  SLOT(ListViewSelectionChanged(const QItemSelection &, const QItemSelection &)));
156 
157  this->sortPresets(0);
158 
159  presetView->resizeColumnsToContents();
160 }
161 
163 {
164  std::map<std::string, double> levels;
165  m_TableModel->getLevels(levels);
166  return levels;
167 }
168 
170 {
171  std::map<std::string, double> windows;
172  m_TableModel->getWindows(windows);
173  return windows;
174 }
175 
177  const QItemSelection & /*deselected*/)
178 {
179  QModelIndexList indexes(selected.indexes());
180  if (indexes.empty())
181  {
182  presetnameLineEdit->setText("");
183  levelSpinBox->setValue(0);
184  windowSpinBox->setValue(0);
185  }
186  else
187  {
188  // use the sorted index to get the entry
189  PresetTableModel::Entry entry(m_TableModel->getPreset((m_SortModel.mapToSource(indexes.first()))));
190  presetnameLineEdit->setText(QString(entry.name.c_str()));
191  levelSpinBox->setValue((int)entry.level);
192  windowSpinBox->setValue((int)entry.window);
193  }
194 }
195 
197  std::map<std::string, double> &windows,
198  QObject *parent)
199  : QAbstractTableModel(parent)
200 {
201  for (auto iter = levels.begin(); iter != levels.end(); ++iter)
202  {
203  m_Entries.push_back(Entry(iter->first, iter->second, windows[iter->first]));
204  }
205 }
206 
208 {
209  for (auto iter = m_Entries.begin(); iter != m_Entries.end(); ++iter)
210  {
211  levels[iter->name] = iter->level;
212  }
213 }
214 
216 {
217  for (auto iter = m_Entries.begin(); iter != m_Entries.end(); ++iter)
218  {
219  windows[iter->name] = iter->window;
220  }
221 }
222 
223 void QmitkLevelWindowPresetDefinitionDialog::PresetTableModel::addPreset(std::string &name, double level, double window)
224 {
225  this->beginInsertRows(QModelIndex(), (int)m_Entries.size(), (int)m_Entries.size());
226 
227  m_Entries.push_back(Entry(name, level, window));
228 
229  this->endInsertRows();
230 }
231 
233 {
234  for (auto iter = m_Entries.begin(); iter != m_Entries.end(); ++iter)
235  {
236  if (iter->name == name)
237  return true;
238  }
239 
240  return false;
241 }
242 
244 {
245  int row = index.row();
246 
247  this->beginRemoveRows(QModelIndex(), row, row);
248 
249  m_Entries.erase(m_Entries.begin() + row);
250 
251  this->endRemoveRows();
252 }
253 
255  std::string &name,
256  double level,
257  double window)
258 {
259  m_Entries[row].name = name;
260  m_Entries[row].level = level;
261  m_Entries[row].window = window;
262 
263  this->dataChanged(index(row, 0), index(row, 2));
264 }
265 
268 {
269  int row = index.row();
270 
271  if (row < 0 || (unsigned int)row >= m_Entries.size())
272  return Entry("", 0, 0);
273 
274  return m_Entries[row];
275 }
276 
278 {
279  return (int)m_Entries.size();
280 }
281 
283 {
284  return 3;
285 }
286 
287 QVariant QmitkLevelWindowPresetDefinitionDialog::PresetTableModel::data(const QModelIndex &index, int role) const
288 {
289  if (role == Qt::DisplayRole)
290  {
291  switch (index.column())
292  {
293  case 0:
294  return QVariant(QString(m_Entries[index.row()].name.c_str()));
295  case 1:
296  {
297  return QVariant(m_Entries[index.row()].level);
298  }
299  case 2:
300  return QVariant(m_Entries[index.row()].window);
301  }
302  }
303 
304  return QVariant();
305 }
306 
308  Qt::Orientation orientation,
309  int role) const
310 {
311  if (role == Qt::DisplayRole && orientation == Qt::Horizontal)
312  {
313  switch (section)
314  {
315  case 0:
316  return QVariant("Preset");
317  case 1:
318  return QVariant("Level");
319  case 2:
320  return QVariant("Window");
321  }
322  }
323 
324  return QVariant();
325 }
PresetTableModel(std::map< std::string, double > &levels, std::map< std::string, double > &windows, QObject *parent=nullptr)
void ListViewSelectionChanged(const QItemSelection &, const QItemSelection &)
void setPresets(std::map< std::string, double > &level, std::map< std::string, double > &window, QString initLevel, QString initWindow)
QmitkLevelWindowPresetDefinitionDialog(QWidget *parent=nullptr, Qt::WindowFlags f=nullptr)
void changePreset(int row, std::string &name, double level, double window)
QVariant headerData(int section, Qt::Orientation orientation, int) const override