1 #include "qui/motif_editor/MotifEditorDelegate.hpp"
5 #include <QColorDialog>
11 #include <QRegExpValidator>
13 MotifEditorDelegate::MotifEditorDelegate(QWidget *parent) :
18 QWidget *MotifEditorDelegate::createEditor(
20 const QStyleOptionViewItem & /* option */,
21 const QModelIndex &index) const
23 static const QRegExp no_quotes("[^\"]*");
24 // grabbed from Alphabet::nucleic_alphabet
25 static const QRegExp iupac("[AaCcGgTtUuRrYyMmKkSsWwBbDdHhVvNn-~.?]*");
26 QRegExpValidator *regex_validator;
27 QLineEdit *widget = 0;
28 switch(index.column()) {
29 // case MotifModel::EnabledCell is handled in editorEvent
30 // case MotifModel::ColorCell:
31 case MotifModel::NameCell:
32 widget = new QLineEdit(parent);
33 widget->installEventFilter(const_cast<MotifEditorDelegate*>(this));
34 regex_validator = new QRegExpValidator(no_quotes, parent);
35 widget->setValidator(regex_validator);
37 case MotifModel::SequenceCell:
38 widget = new QLineEdit(parent);
39 widget->installEventFilter(const_cast<MotifEditorDelegate*>(this));
40 regex_validator = new QRegExpValidator(iupac, parent);
41 widget->setValidator(regex_validator);
49 //! check to see if our event is something that should "activate" our cell
50 static bool is_cell_activated(QEvent *event)
52 // if a mouse releases on us we can toggle
53 if (event->type() == QEvent::MouseButtonRelease) {
55 // if we're a keypress
56 } else if (event->type() == QEvent::KeyPress) {
57 // try to convert to a KeyEvent
58 QKeyEvent *key_event = dynamic_cast<QKeyEvent *>(event);
59 // if we've gotten our key event, did the use hit the enter/return key?
61 if (key_event->key() == Qt::Key_Enter or key_event->key() == Qt::Key_Return) {
66 // all other cases don't toggle
69 bool MotifEditorDelegate::editorEvent(
71 QAbstractItemModel *model,
72 const QStyleOptionViewItem &option,
73 const QModelIndex & index )
75 switch (index.column()) {
76 case MotifModel::EnabledCell:
77 if (is_cell_activated(event)) {
78 QVariant value = index.model()->data(index, Qt::DisplayRole);
79 value = not value.toBool();
80 model->setData(index, value);
84 case MotifModel::ColorCell:
85 if (is_cell_activated(event)) {
86 QVariant value = index.model()->data(index, Qt::DisplayRole);
87 QColor old_color = value.value<QColor>();
88 QColor new_color = QColorDialog::getColor(old_color);
89 // color is not valid if user hit the cancel button on the color dialog
90 if (new_color.isValid()) {
91 model->setData(index, QVariant(new_color));
93 model->setData(index, QVariant(old_color));
101 return this->QItemDelegate::editorEvent(event, model, option, index);
104 void MotifEditorDelegate::setEditorData(
106 const QModelIndex &index) const
108 QVariant value = index.model()->data(index, Qt::DisplayRole);
109 switch(index.column()) {
110 // toggling MotifModel::EnabledCell is handled by editorEvent
111 // case MotifModel::ColorCell is handled by paint and editorEvent
113 // both of these should be text boxes
114 case MotifModel::NameCell:
115 case MotifModel::SequenceCell:
116 static_cast<QLineEdit*>(editor)->setText(value.toString());
123 void MotifEditorDelegate::setModelData(
125 QAbstractItemModel *model,
126 const QModelIndex &index) const
129 switch(index.column()) {
130 // both of these should be text boxes
131 case MotifModel::NameCell:
132 case MotifModel::SequenceCell:
133 value = static_cast<QLineEdit*>(editor)->text();
134 model->setData(index, value);
141 struct painter_save_restore {
142 painter_save_restore(QPainter *p) { painter = p; painter->save(); }
143 ~painter_save_restore() { painter->restore(); }
147 static void drawHighlightIfNeeded(QPainter *painter, const QStyleOptionViewItem &option)
149 if (option.state & QStyle::State_Selected) {
150 painter->setBrush(option.palette.highlight());
151 painter->drawRect(option.rect);
155 void MotifEditorDelegate::paint(
157 const QStyleOptionViewItem & option,
158 const QModelIndex & index ) const
160 painter_save_restore saved(painter);
162 QVariant value = index.model()->data(index, Qt::DisplayRole);
163 switch(index.column()) {
164 case MotifModel::EnabledCell:
166 painter->setPen(Qt::NoPen);
167 drawHighlightIfNeeded(painter, option);
168 Qt::CheckState state = (value.toBool()) ? Qt::Checked : Qt::Unchecked;
169 this->QItemDelegate::drawCheck(painter, option, option.rect, state);
172 case MotifModel::ColorCell:
174 painter->setPen(Qt::NoPen);
175 drawHighlightIfNeeded(painter, option);
176 QColor color(value.value<QColor>());
177 painter->setBrush(QBrush(color));
178 painter->drawRoundRect(option.rect, 60, 60);
182 this->QItemDelegate::paint(painter, option, index);
187 void MotifEditorDelegate::updateEditorGeometry(
189 const QStyleOptionViewItem &option,
190 const QModelIndex &index) const
192 editor->setGeometry(option.rect);