19 #include "parameter_delegate.h" 26 namespace ParameterGui
29 : QItemDelegate(parent)
62 return QItemDelegate::sizeHint(option, index);
71 QString pattern_description = index.data(Qt::StatusTipRole).toString();
73 QRegExp rx_string(
"\\b(FileName|DirectoryName)\\b");
75 if (rx_string.indexIn (pattern_description) != -1)
77 QString value = index.model()->data(index, Qt::DisplayRole).toString();
79 QStyleOptionViewItem my_option = option;
80 my_option.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
82 drawDisplay(painter, my_option, my_option.rect, value);
83 drawFocus(painter, my_option, my_option.rect);
87 QItemDelegate::paint(painter, option, index);
91 QItemDelegate::paint(painter, option, index);
97 const QStyleOptionViewItem &option,
98 const QModelIndex &index)
const 102 QString pattern_description = index.data(Qt::StatusTipRole).toString();
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");
112 if (rx_string.indexIn (pattern_description) != -1)
114 QLineEdit * line_editor =
new QLineEdit(parent);
116 connect(line_editor, SIGNAL(editingFinished()),
121 else if (rx_filename.indexIn (pattern_description) != -1)
126 connect(filename_editor, SIGNAL(editingFinished()),
129 return filename_editor;
131 else if (rx_dirname.indexIn (pattern_description) != -1)
136 connect(dirname_editor, SIGNAL(editingFinished()),
139 return dirname_editor;
141 else if (rx_integer.indexIn (pattern_description) != -1)
143 QSpinBox * spin_box =
new QSpinBox(parent);
145 const int min_int_value = std::numeric_limits<int>::min();
146 const int max_int_value = std::numeric_limits<int>::max();
148 spin_box->setMaximum(max_int_value);
149 spin_box->setMinimum(min_int_value);
152 connect(spin_box, SIGNAL(editingFinished()),
157 else if (rx_double.indexIn (pattern_description) != -1)
159 QDoubleSpinBox * double_spin_box =
new QDoubleSpinBox(parent);
161 const double min_double_value = -std::numeric_limits<double>::max();
162 const double max_double_value = std::numeric_limits<double>::max();
164 double_spin_box->setMaximum(max_double_value);
165 double_spin_box->setMinimum(min_double_value);
169 connect(double_spin_box, SIGNAL(editingFinished()),
172 return double_spin_box;
174 else if (rx_selection.indexIn (pattern_description) != -1)
176 QComboBox * combo_box =
new QComboBox(parent);
178 std::vector<std::string> choices;
179 std::string tmp(pattern_description.toStdString());
181 if (tmp.find(
"[") != std::string::npos)
182 tmp.erase (0, tmp.find(
"[")+1);
184 if (tmp.find(
"]") != std::string::npos)
185 tmp.erase (tmp.find(
"]"),tmp.length());
187 if (tmp.find(
" ") != std::string::npos)
188 tmp.erase (0, tmp.find(
" ")+1);
190 while (tmp.find(
'|') != std::string::npos)
192 choices.push_back(std::string(tmp, 0, tmp.find(
'|')));
193 tmp.erase (0, tmp.find(
'|')+1);
196 if (tmp.find(
" ") != std::string::npos)
197 tmp.erase (tmp.find(
" "));
199 choices.push_back(tmp);
201 for (
unsigned int i=0; i<choices.size(); ++i)
202 combo_box->addItem (tr(choices[i].c_str()), tr(choices[i].c_str()));
204 combo_box->setEditable(
false);
206 connect(combo_box, SIGNAL(currentIndexChanged(
int)),
211 else if (rx_bool.indexIn (pattern_description) != -1)
213 QComboBox * combo_box =
new QComboBox(parent);
215 std::vector<std::string> choices;
216 choices.push_back(std::string(
"true"));
217 choices.push_back(std::string(
"false"));
219 for (
unsigned int i=0; i<choices.size(); ++i)
220 combo_box->addItem (tr(choices[i].c_str()), tr(choices[i].c_str()));
222 combo_box->setEditable(
false);
224 connect(combo_box, SIGNAL(currentIndexChanged(
int)),
231 return QItemDelegate::createEditor(parent, option, index);
244 QString pattern_description = index.data(Qt::StatusTipRole).toString();
246 QRegExp rx_filename(
"\\b(FileName)\\b"),
247 rx_dirname(
"\\b(DirectoryName)\\b"),
248 rx_selection(
"\\b(Selection)\\b");
250 if (rx_filename.indexIn (pattern_description) != -1)
252 QString file_name = index.data(Qt::DisplayRole).toString();
255 filename_editor->
setText(file_name);
257 else if (rx_dirname.indexIn (pattern_description) != -1)
259 QString dir_name = index.data(Qt::DisplayRole).toString();
262 dirname_editor->
setText(dir_name);
264 else if (rx_selection.indexIn (pattern_description) != -1)
266 QRegExp rx(index.data(Qt::DisplayRole).toString());
268 QComboBox * combo_box = qobject_cast<QComboBox *>(editor);
270 for (
int i=0; i<combo_box->count(); ++i)
271 if (rx.exactMatch(combo_box->itemText(i)))
272 combo_box->setCurrentIndex(i);
275 QItemDelegate::setEditorData(editor, index);
285 QWidget * editor = qobject_cast<QWidget *>(sender());
286 emit commitData(editor);
287 emit closeEditor(editor);
293 const QModelIndex &index)
const 297 QString pattern_description = index.data(Qt::StatusTipRole).toString();
300 QRegExp rx_filename(
"\\b(FileName)\\b"),
301 rx_dirname(
"\\b(DirectoryName)\\b"),
302 rx_selection(
"\\b(Selection)\\b");
304 if (rx_filename.indexIn (pattern_description) != -1)
307 QString value = filename_editor->
text();
308 model->setData(index, value);
310 else if (rx_dirname.indexIn (pattern_description) != -1)
313 QString value = dirname_editor->
text();
314 model->setData(index, value);
316 else if (rx_selection.indexIn (pattern_description) != -1)
318 QComboBox * combo_box = qobject_cast<QComboBox *>(editor);
319 QString value = combo_box->currentText();
320 model->setData(index, value);
323 QItemDelegate::setModelData(editor, model, index);
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 commit_and_close_editor()
void setText(const QString &str)
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
unsigned int double_decimals
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const