/* prop.cc */ #include "prop.h" #include #include #include #include #include #include #include #include #include /* ---------------------------------------------------------------------- */ class MyTreeItem : public QTreeWidgetItem { public: Field * field; MyTreeItem (QTreeWidget * view) : QTreeWidgetItem (view), field (NULL) { setFlags (flags () | Qt::ItemIsEditable); } }; /* ---------------------------------------------------------------------- */ class MyItemEditor : public QWidget { public: QLineEdit * line_edit; QComboBox * combo_box; QPushButton * button; MyItemEditor (QWidget * parent) : QWidget (parent), line_edit (NULL), combo_box (NULL), button (NULL) { } }; /* ---------------------------------------------------------------------- */ class MyItemDelegate : public QItemDelegate { private: Field * getItem (const QModelIndex & index) const; public: virtual QWidget * createEditor (QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const; virtual void setEditorData (QWidget * param_editor, const QModelIndex & index) const; virtual void setModelData (QWidget * param_editor, QAbstractItemModel * model, const QModelIndex & index) const; virtual void paint (QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex &index) const; virtual void updateEditorGeometry (QWidget * param_editor, const QStyleOptionViewItem & option, const QModelIndex &index) const; virtual QSize sizeHint (const QStyleOptionViewItem & option, const QModelIndex & index) const; }; /* ---------------------------------------------------------------------- */ Field * MyItemDelegate::getItem (const QModelIndex & index) const { Field * item = NULL; if (index.isValid ()) { MyTreeItem * list_item = static_cast < MyTreeItem * > (index.internalPointer ()); assert (list_item != NULL); item = list_item->field; } return item; } QWidget * MyItemDelegate::createEditor (QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const { MyItemEditor * editor = new MyItemEditor (parent); Field * item = getItem (index); if (item != NULL && index.column () == 1) { QHBoxLayout * layout = new QHBoxLayout (editor); layout->setMargin (0); layout->setSpacing (0); editor->setLayout (layout); if (item->ListEnabled ()) { editor->combo_box = new QComboBox (editor); layout->addWidget (editor->combo_box); int cnt = item->ListCount (); for (int inx = 0; inx < cnt; inx++) editor->combo_box->addItem (item->ListValue (inx)); QPalette p = editor->combo_box->palette (); p.setColor (QPalette::Button, p.color (QPalette::Base)); editor->combo_box->setPalette (p); #if 0 // combo box ... item changed ctrl->list_selection_changed_adapter->add (editor->combo_box, SIGNAL (activated (int))); #endif } else { editor->line_edit = new QLineEdit (editor); layout->addWidget (editor->line_edit); #if 0 // line_edit ... text changed ctrl->text_changed_adapter->add (editor->line_edit, SIGNAL (textChanged (const QString &))); // line_edit ... return preesed ctrl->return_pressed_adapter->add (editor->line_edit, SIGNAL (returnPressed())); #endif } // if (item->DialogEnabled ()) { editor->button = new QPushButton (editor); editor->button->setText ("..."); editor->button->setMaximumWidth (32); layout->addWidget (editor->button); #if 0 // connect ctrl->dialog_button_click_adapter->add (editor->button, SIGNAL (clicked())); #endif } } return editor; } void MyItemDelegate::setEditorData (QWidget * param_editor, const QModelIndex & index) const { MyItemEditor * editor = dynamic_cast < MyItemEditor * > (param_editor); assert (editor != NULL); Field * item = getItem (index); if (item != NULL) { if (editor->line_edit != NULL) { editor->line_edit->setText (item->getText ()); } } } void MyItemDelegate::setModelData (QWidget * param_editor, QAbstractItemModel * model, const QModelIndex & index) const { MyItemEditor * editor = dynamic_cast < MyItemEditor * > (param_editor); assert (editor != NULL); Field * item = getItem (index); if (item != NULL) { if (editor->line_edit != NULL) { QString txt = editor->line_edit->text (); item->setText (txt); } } } /* ---------------------------------------------------------------------- */ void MyItemDelegate::paint (QPainter * painter, const QStyleOptionViewItem & opt, const QModelIndex & index) const { QStyleOptionViewItem option = opt; if (index.column() == 0) option.rect.setX (0); const bool mask = true; // qvariant_cast(index.model()->data(index, Qt::EditRole)); if (index.column() == 0 && mask) option.font.setBold (true); QBrush br = QBrush ("lightyellow"); // qvariant_cast(index.model()->data(index, BrushRole)); if ( index.row () & 1 ) br = QBrush ("lightgreen"); painter->save(); painter->setBrushOrigin (option.rect.x(), option.rect.y()); painter->fillRect (option.rect, br); painter->restore(); QItemDelegate::paint (painter, option, index); const QColor color = static_cast (QApplication::style()->styleHint (QStyle::SH_Table_GridLineColor, &option)); const QPen oldPen = painter->pen(); painter->setPen (QPen (color)); painter->drawLine (option.rect.right(), option.rect.y(), option.rect.right(), option.rect.bottom()); painter->drawLine (option.rect.x(), option.rect.bottom(), option.rect.right(), option.rect.bottom()); painter->setPen (oldPen); } // see ColorDelegate::paint in qt-4.8.0/tools/designer/src/components/propertyeditor/paletteeditor.cpp void MyItemDelegate::updateEditorGeometry (QWidget * param_editor, const QStyleOptionViewItem & option, const QModelIndex &index) const { QItemDelegate::updateEditorGeometry (param_editor, option, index); param_editor->setGeometry (param_editor->geometry().adjusted(0, 0, -1, -1)); } QSize MyItemDelegate::sizeHint (const QStyleOptionViewItem & option, const QModelIndex & index) const { return QItemDelegate::sizeHint (option, index) + QSize (4, 4); } /* ---------------------------------------------------------------------- */ MyProp::MyProp (QWidget* parent) : QTreeWidget (parent) { setColumnCount (2); headerItem ()->setText (0, "Name"); headerItem ()->setText (1, "Value"); header ()->setStretchLastSection (true); setAlternatingRowColors (true); setEditTriggers (QAbstractItemView::CurrentChanged); #if 1 setItemDelegate (new MyItemDelegate); #endif } void MyProp::display (Area * area) { Field * field = area->getFirstField (); while (field != NULL) { MyTreeItem * item = new MyTreeItem (this); item->setText (0, field->getName ()); item->setText (1, field->getValue ()); item->field = field; field = field->getNextField (); } }