Reference documentation for deal.II version 8.4.2
parameter_delegate.cpp
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2010 - 2013 by Martin Steigemann and Wolfgang Bangerth
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 
17 #include <QtGui>
18 
19 #include "parameter_delegate.h"
20 
21 #include <limits>
22 
23 
24 namespace dealii
25 {
26  namespace ParameterGui
27  {
28  ParameterDelegate::ParameterDelegate(const int value_column, QObject *parent)
29  : QItemDelegate(parent)
30  {
31  this->value_column = value_column;
32 
33  double_steps = 0.1; // any click in the editor will increase or decrease the value about double_steps
34  double_decimals = 14; // number of decimals shown in the editor
35 
36  int_steps = 1; // step value for increasing or decreasing integers
37  }
38 
39 
40 
41  QSize ParameterDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
42  {
43  if (index.column() == value_column)
44  {
45  return QSize(400,30); // we increase the height of all lines to show editors
46 
47 /*
48  QString pattern_description = index.data(Qt::StatusTipRole).toString(); // load pattern description
49  // stored in the StatusLine
50  QRegExp rx_string("\\b(FileName|DirectoryName)\\b");
51 
52  if (rx_string.indexIn (pattern_description) != -1)
53  {
54  return QSize(400,35); // we increase the height for FileName and
55  } // DirectoryName to show a "browse" button
56  else
57  return QItemDelegate::sizeHint(option, index);
58 */
59 
60  }
61  else
62  return QItemDelegate::sizeHint(option, index);
63  }
64 
65 
66 
67  void ParameterDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
68  {
69  if (index.column() == value_column)
70  {
71  QString pattern_description = index.data(Qt::StatusTipRole).toString(); // load pattern description
72  // stored in the StatusLine
73  QRegExp rx_string("\\b(FileName|DirectoryName)\\b"); // if the type is Filename
74  // or DirectoryName
75  if (rx_string.indexIn (pattern_description) != -1)
76  {
77  QString value = index.model()->data(index, Qt::DisplayRole).toString(); // take the value
78 
79  QStyleOptionViewItem my_option = option; // load options
80  my_option.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
81 
82  drawDisplay(painter, my_option, my_option.rect, value); // print the text in the display
83  drawFocus(painter, my_option, my_option.rect); // if the line has the
84  // focus, print a rectangle
85  }
86  else
87  QItemDelegate::paint(painter, option, index); // for all other types use
88  // the standard delegate
89  }
90  else
91  QItemDelegate::paint(painter, option, index);
92  }
93 
94 
95 
96  QWidget *ParameterDelegate::createEditor(QWidget *parent,
97  const QStyleOptionViewItem &option,
98  const QModelIndex &index) const
99  {
100  if (index.column() == value_column)
101  {
102  QString pattern_description = index.data(Qt::StatusTipRole).toString(); // load pattern description
103  // stored in the StatusLine
104  QRegExp rx_string("\\b(Anything|MultipleSelection|List|Map)\\b"),
105  rx_filename("\\b(FileName)\\b"),
106  rx_dirname("\\b(DirectoryName)\\b"),
107  rx_integer("\\b(Integer)\\b"),
108  rx_double("\\b(Double|Float|Floating)\\b"),
109  rx_selection("\\b(Selection)\\b"),
110  rx_bool("\\b(Bool)\\b");
111 
112  if (rx_string.indexIn (pattern_description) != -1) // if the type is "Anything"
113  {
114  QLineEdit * line_editor = new QLineEdit(parent); // choose a LineEditor
115 
116  connect(line_editor, SIGNAL(editingFinished()), // and connect editors signal
117  this, SLOT(commit_and_close_editor())); // to the closer function
118 
119  return line_editor;
120  }
121  else if (rx_filename.indexIn (pattern_description) != -1) // if the type is "FileName"
122  {
123  BrowseLineEdit * filename_editor = // choose a BrowseLineEditor
124  new BrowseLineEdit(BrowseLineEdit::file, parent);
125 
126  connect(filename_editor, SIGNAL(editingFinished()),
127  this, SLOT(commit_and_close_editor()));
128 
129  return filename_editor;
130  }
131  else if (rx_dirname.indexIn (pattern_description) != -1) // if the type is "DirectoryName"
132  {
133  BrowseLineEdit * dirname_editor = // choose a BrowseLineEditor
134  new BrowseLineEdit(BrowseLineEdit::directory, parent);
135 
136  connect(dirname_editor, SIGNAL(editingFinished()),
137  this, SLOT(commit_and_close_editor()));
138 
139  return dirname_editor;
140  }
141  else if (rx_integer.indexIn (pattern_description) != -1) // if the tpye is "Integer"
142  {
143  QSpinBox * spin_box = new QSpinBox(parent); // choose a spin box
144 
145  const int min_int_value = std::numeric_limits<int>::min();
146  const int max_int_value = std::numeric_limits<int>::max();
147 
148  spin_box->setMaximum(max_int_value); // set max and min from the limits.h class
149  spin_box->setMinimum(min_int_value);
150  spin_box->setSingleStep(int_steps); // and every klick is a SingleStep
151 
152  connect(spin_box, SIGNAL(editingFinished()), // connect editors signal to the closer function
153  this, SLOT(commit_and_close_editor()));
154 
155  return spin_box;
156  }
157  else if (rx_double.indexIn (pattern_description) != -1) // the same with "Double"
158  {
159  QDoubleSpinBox * double_spin_box = new QDoubleSpinBox(parent); // choose a spin box
160 
161  const double min_double_value = -std::numeric_limits<double>::max();
162  const double max_double_value = std::numeric_limits<double>::max();
163 
164  double_spin_box->setMaximum(max_double_value); // set max and min from the limits.h class
165  double_spin_box->setMinimum(min_double_value);
166  double_spin_box->setDecimals(double_decimals); // show "double_decimals" decimals
167  double_spin_box->setSingleStep(double_steps); // and every klick is a SingleStep
168 
169  connect(double_spin_box, SIGNAL(editingFinished()), // connect editors signal to the closer function
170  this, SLOT(commit_and_close_editor()));
171 
172  return double_spin_box;
173  }
174  else if (rx_selection.indexIn (pattern_description) != -1) // and selections
175  {
176  QComboBox * combo_box = new QComboBox(parent); // we assume, that pattern_desctiption is of the form
177  // "Type: [Selection item1|item2| ....|item ] "
178  std::vector<std::string> choices; // list with the different items
179  std::string tmp(pattern_description.toStdString());
180 
181  if (tmp.find("[") != std::string::npos) // delete all char before [
182  tmp.erase (0, tmp.find("[")+1);
183 
184  if (tmp.find("]") != std::string::npos) // delete all char after ]
185  tmp.erase (tmp.find("]"),tmp.length());
186 
187  if (tmp.find(" ") != std::string::npos) // delete all char before " "
188  tmp.erase (0, tmp.find(" ")+1);
189 
190  while (tmp.find('|') != std::string::npos) // extract items
191  {
192  choices.push_back(std::string(tmp, 0, tmp.find('|')));
193  tmp.erase (0, tmp.find('|')+1);
194  };
195 
196  if (tmp.find(" ") != std::string::npos) // delete " "
197  tmp.erase (tmp.find(" "));
198 
199  choices.push_back(tmp); // add last item
200 
201  for (unsigned int i=0; i<choices.size(); ++i) // add items to the combo box
202  combo_box->addItem (tr(choices[i].c_str()), tr(choices[i].c_str()));
203 
204  combo_box->setEditable(false);
205 
206  connect(combo_box, SIGNAL(currentIndexChanged(int)), // connect editors signal to the closer function
207  this, SLOT(commit_and_close_editor()));
208 
209  return combo_box;
210  }
211  else if (rx_bool.indexIn (pattern_description) != -1) // and booleans
212  {
213  QComboBox * combo_box = new QComboBox(parent);
214 
215  std::vector<std::string> choices; // list with the different items
216  choices.push_back(std::string("true")); // add true
217  choices.push_back(std::string("false")); // and false
218 
219  for (unsigned int i=0; i<choices.size(); ++i) // add items to the combo box
220  combo_box->addItem (tr(choices[i].c_str()), tr(choices[i].c_str()));
221 
222  combo_box->setEditable(false);
223 
224  connect(combo_box, SIGNAL(currentIndexChanged(int)), // connect editors signal to the closer function
225  this, SLOT(commit_and_close_editor()));
226 
227  return combo_box;
228  }
229  else
230  {
231  return QItemDelegate::createEditor(parent, option, index);
232  };
233  };
234 
235  return 0; // if it is not the column "parameter values", do nothing
236  }
237 
238 
239 
240  void ParameterDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
241  {
242  if (index.column() == value_column)
243  {
244  QString pattern_description = index.data(Qt::StatusTipRole).toString(); // load pattern description
245  // stored in the StatusLine
246  QRegExp rx_filename("\\b(FileName)\\b"),
247  rx_dirname("\\b(DirectoryName)\\b"),
248  rx_selection("\\b(Selection)\\b");
249 
250  if (rx_filename.indexIn (pattern_description) != -1) // if the type is "FileName"
251  {
252  QString file_name = index.data(Qt::DisplayRole).toString();
253 
254  BrowseLineEdit *filename_editor = qobject_cast<BrowseLineEdit *>(editor); // set the text of the editor
255  filename_editor->setText(file_name);
256  }
257  else if (rx_dirname.indexIn (pattern_description) != -1) // if the type is "DirectoryName"
258  {
259  QString dir_name = index.data(Qt::DisplayRole).toString();
260 
261  BrowseLineEdit *dirname_editor = qobject_cast<BrowseLineEdit *>(editor); // set the text of the editor
262  dirname_editor->setText(dir_name);
263  }
264  else if (rx_selection.indexIn (pattern_description) != -1) // if we have a combo box,
265  {
266  QRegExp rx(index.data(Qt::DisplayRole).toString());
267 
268  QComboBox * combo_box = qobject_cast<QComboBox *>(editor);
269 
270  for (int i=0; i<combo_box->count(); ++i) // we look, which index
271  if (rx.exactMatch(combo_box->itemText(i))) // the data has and set
272  combo_box->setCurrentIndex(i); // it to the combo_box
273  }
274  else
275  QItemDelegate::setEditorData(editor, index); // if it is not FileName,
276  // DirectoryName or Selection
277  // use the standard delegate
278  };
279  }
280 
281 
282 
284  {
285  QWidget * editor = qobject_cast<QWidget *>(sender());
286  emit commitData(editor);
287  emit closeEditor(editor);
288  }
289 
290 
291 
292  void ParameterDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
293  const QModelIndex &index) const
294  {
295  if (index.column() == value_column)
296  {
297  QString pattern_description = index.data(Qt::StatusTipRole).toString(); // load pattern description
298  // stored in the StatusLine
299 
300  QRegExp rx_filename("\\b(FileName)\\b"),
301  rx_dirname("\\b(DirectoryName)\\b"),
302  rx_selection("\\b(Selection)\\b");
303 
304  if (rx_filename.indexIn (pattern_description) != -1) // if the type is "FileName"
305  {
306  BrowseLineEdit * filename_editor = qobject_cast<BrowseLineEdit *>(editor); // set the text from the editor
307  QString value = filename_editor->text();
308  model->setData(index, value);
309  }
310  else if (rx_dirname.indexIn (pattern_description) != -1) // if the type is "DirectoryName"
311  {
312  BrowseLineEdit * dirname_editor = qobject_cast<BrowseLineEdit *>(editor); // set the text from the editor
313  QString value = dirname_editor->text();
314  model->setData(index, value);
315  }
316  else if (rx_selection.indexIn (pattern_description) != -1) // if the type is "Selection"
317  {
318  QComboBox * combo_box = qobject_cast<QComboBox *>(editor); // set the text from the editor
319  QString value = combo_box->currentText();
320  model->setData(index, value);
321  }
322  else
323  QItemDelegate::setModelData(editor, model, index); // if it is not FileName or DirectoryName,
324  // use the standard delegate
325  };
326  }
327  }
328 }
329 
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
ParameterDelegate(const int value_column, QObject *parent=0)
void setEditorData(QWidget *editor, const QModelIndex &index) const
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const