Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #define NOMINMAX
00019
00020 #include "QmitkPropertyDelegate.h"
00021
00022 #include "QmitkCustomVariants.h"
00023
00024 #include "mitkRenderingManager.h"
00025
00026 #include <bitset>
00027 #include <QPainter>
00028 #include <QApplication>
00029 #include <QCheckBox>
00030 #include <QLabel>
00031 #include <QPushButton>
00032 #include <QColorDialog>
00033 #include <QComboBox>
00034 #include <QDoubleSpinBox>
00035 #include <QStringList>
00036 #include <QMessageBox>
00037 #include <QPen>
00038
00039 QmitkPropertyDelegate::QmitkPropertyDelegate(QObject * )
00040 {
00041 }
00042
00043 void QmitkPropertyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option
00044 , const QModelIndex &index) const
00045 {
00046
00047 QVariant data = index.data(Qt::DisplayRole);
00048
00049 QString name = data.value<QString>();
00050
00051 if(index.column() == 1 && data.type() == QVariant::Color)
00052 {
00053 QColor qcol = data.value<QColor>();
00054
00055 painter->save();
00056 painter->fillRect(option.rect, qcol);
00057 QRect rect = option.rect;
00058 rect.setWidth(rect.width()-1);
00059 rect.setHeight(rect.height()-1);
00060 QPen pen;
00061 pen.setWidth(1);
00062 painter->setPen(pen);
00063 painter->drawRect(rect);
00064 painter->restore();
00065
00066 }
00067 else
00068 {
00069 QStyledItemDelegate::paint(painter, option, index);
00070 }
00071 }
00072
00073 QWidget* QmitkPropertyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option
00074 , const QModelIndex &index) const
00075 {
00076 QVariant data = index.data(Qt::EditRole);
00077 QVariant displayData = index.data(Qt::DisplayRole);
00078 QString name = index.model()->data(index.model()->index(index.row(), index.column()-1)).value<QString>();
00079
00080 if(data.isValid())
00081 {
00082
00083 QWidget* editorWidget = NULL;
00084
00085 if(data.type() == QVariant::Color)
00086 {
00087 QPushButton* colorBtn = new QPushButton(parent);
00088 QColor color = data.value<QColor>();
00089
00090 QColor result = QColorDialog::getColor(color);
00091 if(result.isValid())
00092 {
00093 QPalette palette = colorBtn->palette();
00094 palette.setColor(QPalette::Button, result);
00095 colorBtn->setPalette(palette);
00096 colorBtn->setStyleSheet(QString("background-color: %1;foreground-color: %1; border-style: none;").arg(result.name()));
00097
00098 }
00099
00100 else
00101 {
00102 QPalette palette = colorBtn->palette();
00103 palette.setColor(QPalette::Button, color);
00104 colorBtn->setPalette(palette);
00105 colorBtn->setStyleSheet(QString("background-color: %1;foreground-color: %1; border-style: none;").arg(color.name()));
00106
00107 }
00108
00109 connect(colorBtn, SIGNAL(pressed()), this, SLOT(commitAndCloseEditor()));
00110
00111 editorWidget = colorBtn;
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125 else if(data.type() == QVariant::Int)
00126 {
00127 QSpinBox* spinBox = new QSpinBox(parent);
00128 spinBox->setSingleStep(1);
00129 spinBox->setMinimum(std::numeric_limits<int>::min());
00130 spinBox->setMaximum(std::numeric_limits<int>::max());
00131 editorWidget = spinBox;
00132 }
00133
00134
00135 else if(static_cast<QMetaType::Type>(data.type()) == QMetaType::Float)
00136 {
00137 QDoubleSpinBox* spinBox = new QDoubleSpinBox(parent);
00138 spinBox->setDecimals(2);
00139 spinBox->setSingleStep(0.1);
00140 if(name == "opacity")
00141 {
00142 spinBox->setMinimum(0.0);
00143 spinBox->setMaximum(1.0);
00144 }
00145 else
00146 {
00147 spinBox->setMinimum(std::numeric_limits<float>::min());
00148 spinBox->setMaximum(std::numeric_limits<float>::max());
00149 }
00150
00151 editorWidget = spinBox;
00152 }
00153
00154 else if(data.type() == QVariant::StringList)
00155 {
00156 QStringList entries = data.value<QStringList>();
00157 QComboBox* comboBox = new QComboBox(parent);
00158 comboBox->setEditable(false);
00159 comboBox->addItems(entries);
00160
00161 editorWidget = comboBox;
00162 }
00163
00164
00165 else
00166 {
00167 editorWidget = QStyledItemDelegate::createEditor(parent, option, index);
00168 }
00169
00170 if ( editorWidget )
00171 {
00172
00173 editorWidget->installEventFilter( const_cast<QmitkPropertyDelegate*>(this) );
00174 }
00175
00176 return editorWidget;
00177
00178 }
00179 else
00180 return new QLabel(displayData.toString(), parent);
00181
00182 }
00183
00184 void QmitkPropertyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
00185 {
00186
00187 QVariant data = index.data(Qt::EditRole);
00188 QVariant displayData = index.data(Qt::DisplayRole);
00189
00190 if(data.isValid())
00191 {
00192
00193 if(data.type() == QVariant::Color)
00194 {
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209 }
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219 if(data.type() == QVariant::Int)
00220 {
00221 QSpinBox* spinBox = qobject_cast<QSpinBox *>(editor);
00222 spinBox->setValue(data.toInt());
00223 }
00224
00225
00226 else if(static_cast<QMetaType::Type>(data.type()) == QMetaType::Float)
00227 {
00228 QDoubleSpinBox* spinBox = qobject_cast<QDoubleSpinBox *>(editor);
00229 spinBox->setValue(data.toDouble());
00230 }
00231
00232 else if(data.type() == QVariant::StringList)
00233 {
00234 QComboBox* comboBox = qobject_cast<QComboBox *>(editor);
00235 QString displayString = displayData.value<QString>();
00236 comboBox->setCurrentIndex(comboBox->findData(displayString));
00237
00238
00239
00240 }
00241
00242 else
00243 return QStyledItemDelegate::setEditorData(editor, index);
00244 }
00245 }
00246
00247 void QmitkPropertyDelegate::setModelData(QWidget *editor, QAbstractItemModel* model
00248 , const QModelIndex &index) const
00249 {
00250 QVariant data = index.data(Qt::EditRole);
00251 QVariant displayData = index.data(Qt::DisplayRole);
00252
00253 if(data.isValid())
00254 {
00255
00256 if(data.type() == QVariant::Color)
00257 {
00258 QWidget *colorBtn = qobject_cast<QWidget *>(editor);
00259 QVariant colorVariant;
00260 colorVariant.setValue<QColor>(colorBtn->palette().color(QPalette::Button));
00261 model->setData(index, colorVariant);
00262 }
00263
00264 else if(data.type() == QVariant::Int)
00265 {
00266 QSpinBox* spinBox = qobject_cast<QSpinBox *>(editor);
00267 int intValue = spinBox->value();
00268
00269 QVariant intValueVariant;
00270 intValueVariant.setValue<float>(static_cast<float>(intValue));
00271 model->setData(index, intValueVariant);
00272 }
00273
00274 else if(static_cast<QMetaType::Type>(data.type()) == QMetaType::Float)
00275 {
00276 QDoubleSpinBox* spinBox = qobject_cast<QDoubleSpinBox *>(editor);
00277 double doubleValue = spinBox->value();
00278
00279 QVariant doubleValueVariant;
00280 doubleValueVariant.setValue<float>(static_cast<float>(doubleValue));
00281 model->setData(index, doubleValueVariant);
00282 }
00283
00284 else if(data.type() == QVariant::StringList)
00285 {
00286 QString displayData = data.value<QString>();
00287
00288 QComboBox* comboBox = qobject_cast<QComboBox *>(editor);
00289 QString comboBoxValue = comboBox->currentText();
00290
00291 QVariant comboBoxValueVariant;
00292 comboBoxValueVariant.setValue<QString>(comboBoxValue);
00293 model->setData(index, comboBoxValueVariant);
00294 }
00295
00296 else
00297 QStyledItemDelegate::setModelData(editor, model, index);
00298 }
00299
00300 }
00301
00302 void QmitkPropertyDelegate::commitAndCloseEditor()
00303 {
00304 QWidget* editor = 0;
00305 if(QPushButton *pushBtn = qobject_cast<QPushButton *>(sender()))
00306 {
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316 editor = pushBtn;
00317 }
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327 if(editor)
00328 {
00329 emit commitData(editor);
00330 emit closeEditor(editor);
00331 }
00332
00333 }
00334
00335 void QmitkPropertyDelegate::updateEditorGeometry(QWidget *editor,
00336 const QStyleOptionViewItem &option,
00337 const QModelIndex & ) const
00338 {
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354 editor->setGeometry(option.rect);
00355 }
00356
00357 void QmitkPropertyDelegate::ComboBoxCurrentIndexChanged( int )
00358 {
00359 if(QComboBox *comboBox = qobject_cast<QComboBox *>(sender()))
00360 {
00361 emit commitData(comboBox);
00362 emit closeEditor(comboBox);
00363 }
00364 }
00365
00366 void QmitkPropertyDelegate::SpinBoxValueChanged( const QString& )
00367 {
00368 QAbstractSpinBox *spinBox = 0;
00369 if((spinBox = qobject_cast<QSpinBox *>(sender()))
00370 || (spinBox = qobject_cast<QDoubleSpinBox *>(sender())))
00371 {
00372 emit commitData(spinBox);
00373 emit closeEditor(spinBox);
00374 }
00375 }
00376
00377 void QmitkPropertyDelegate::showColorDialog()
00378 {
00379
00380 }
00381
00382 bool QmitkPropertyDelegate::eventFilter( QObject *o, QEvent *e )
00383 {
00384
00385
00386 switch ( e->type() )
00387 {
00388 case QEvent::KeyRelease:
00389 case QEvent::MouseButtonRelease:
00390 case QEvent::MouseButtonDblClick:
00391 case QEvent::Wheel:
00392 case QEvent::FocusIn:
00393 {
00394 if( QWidget* editor = dynamic_cast<QWidget*>(o) )
00395 {
00396 emit commitData(editor);
00397 }
00398
00399 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
00400 break;
00401 }
00402 default:
00403 {
00404 break;
00405 }
00406 }
00407
00408 return false;
00409 }