1 #include "qui/motif_editor/MotifEditorDelegate.hpp"
5 #include <QColorDialog>
11 MotifEditorDelegate::MotifEditorDelegate(QWidget *parent) :
16 QWidget *MotifEditorDelegate::createEditor(
18 const QStyleOptionViewItem & /* option */,
19 const QModelIndex &index) const
22 switch(index.column()) {
23 // case MotifModel::EnabledCell is handled in editorEvent
24 // case MotifModel::ColorCell:
25 // both of these should be text boxes
26 case MotifModel::NameCell:
27 case MotifModel::SequenceCell:
28 widget = new QLineEdit(parent);
29 widget->installEventFilter(const_cast<MotifEditorDelegate*>(this));
37 //! check to see if our event is something that should "activate" our cell
38 static bool is_cell_activated(QEvent *event)
40 // if a mouse releases on us we can toggle
41 if (event->type() == QEvent::MouseButtonRelease) {
43 // if we're a keypress
44 } else if (event->type() == QEvent::KeyPress) {
45 // try to convert to a KeyEvent
46 QKeyEvent *key_event = dynamic_cast<QKeyEvent *>(event);
47 // if we've gotten our key event, did the use hit the enter/return key?
49 if (key_event->key() == Qt::Key_Enter or key_event->key() == Qt::Key_Return) {
54 // all other cases don't toggle
57 bool MotifEditorDelegate::editorEvent(
59 QAbstractItemModel *model,
60 const QStyleOptionViewItem &option,
61 const QModelIndex & index )
63 switch (index.column()) {
64 case MotifModel::EnabledCell:
65 if (is_cell_activated(event)) {
66 QVariant value = index.model()->data(index, Qt::DisplayRole);
67 value = not value.toBool();
68 model->setData(index, value);
72 case MotifModel::ColorCell:
73 if (is_cell_activated(event)) {
74 QVariant value = index.model()->data(index, Qt::DisplayRole);
75 QColor old_color = value.value<QColor>();
76 QColor new_color = QColorDialog::getColor(old_color);
77 // color is not valid if user hit the cancel button on the color dialog
78 if (new_color.isValid()) {
79 model->setData(index, QVariant(new_color));
81 model->setData(index, QVariant(old_color));
89 return this->QItemDelegate::editorEvent(event, model, option, index);
92 void MotifEditorDelegate::setEditorData(
94 const QModelIndex &index) const
96 QVariant value = index.model()->data(index, Qt::DisplayRole);
97 switch(index.column()) {
98 // toggling MotifModel::EnabledCell is handled by editorEvent
99 // case MotifModel::ColorCell is handled by paint and editorEvent
101 // both of these should be text boxes
102 case MotifModel::NameCell:
103 case MotifModel::SequenceCell:
104 static_cast<QLineEdit*>(editor)->setText(value.toString());
111 void MotifEditorDelegate::setModelData(
113 QAbstractItemModel *model,
114 const QModelIndex &index) const
117 switch(index.column()) {
118 // both of these should be text boxes
119 case MotifModel::NameCell:
120 case MotifModel::SequenceCell:
121 value = static_cast<QLineEdit*>(editor)->text();
122 model->setData(index, value);
129 struct painter_save_restore {
130 painter_save_restore(QPainter *p) { painter = p; painter->save(); }
131 ~painter_save_restore() { painter->restore(); }
135 static void drawHighlightIfNeeded(QPainter *painter, const QStyleOptionViewItem &option)
137 if (option.state & QStyle::State_Selected) {
138 painter->setBrush(option.palette.highlight());
139 painter->drawRect(option.rect);
143 void MotifEditorDelegate::paint(
145 const QStyleOptionViewItem & option,
146 const QModelIndex & index ) const
148 painter_save_restore saved(painter);
150 QVariant value = index.model()->data(index, Qt::DisplayRole);
151 switch(index.column()) {
152 case MotifModel::EnabledCell:
154 painter->setPen(Qt::NoPen);
155 drawHighlightIfNeeded(painter, option);
156 Qt::CheckState state = (value.toBool()) ? Qt::Checked : Qt::Unchecked;
157 this->QItemDelegate::drawCheck(painter, option, option.rect, state);
160 case MotifModel::ColorCell:
162 painter->setPen(Qt::NoPen);
163 drawHighlightIfNeeded(painter, option);
164 QColor color(value.value<QColor>());
165 painter->setBrush(QBrush(color));
166 painter->drawRoundRect(option.rect, 60, 60);
170 this->QItemDelegate::paint(painter, option, index);
175 void MotifEditorDelegate::updateEditorGeometry(
177 const QStyleOptionViewItem &option,
178 const QModelIndex &index) const
180 editor->setGeometry(option.rect);