Changeset 6148 for trunk/qt4-gui/src/views
- Timestamp:
- 04/13/08 23:18:08 (8 months ago)
- Location:
- trunk/qt4-gui/src/views
- Files:
-
- 3 modified
-
contactdelegate.cpp (modified) (2 diffs)
-
contactdelegate.h (modified) (1 diff)
-
userviewbase.cpp (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/qt4-gui/src/views/contactdelegate.cpp
r6008 r6148 23 23 #include "contactdelegate.h" 24 24 25 #include <QApplication> 26 #include <QKeyEvent> 27 #include <QLineEdit> 25 28 #include <QPainter> 26 29 … … 495 498 #undef EXTICON 496 499 } 500 501 QWidget* ContactDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& /* option */, const QModelIndex& /* index */) const 502 { 503 QLineEdit* editor = new QLineEdit(parent); 504 505 // Don't use the view's skinned palette 506 editor->setPalette(QApplication::palette()); 507 508 return editor; 509 } 510 511 void ContactDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const 512 { 513 QString value = index.model()->data(index, Qt::EditRole).toString(); 514 515 QLineEdit* lineedit = dynamic_cast<QLineEdit*>(editor); 516 lineedit->setText(value); 517 } 518 519 void ContactDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const 520 { 521 QLineEdit* lineedit = dynamic_cast<QLineEdit*>(editor); 522 model->setData(index, lineedit->text(), Qt::EditRole); 523 } 524 525 void ContactDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const 526 { 527 QRect r = option.rect; 528 529 // Leave space for icon in first column 530 if (index.column() == 0) 531 r.setLeft(r.left() + 18); 532 533 editor->setGeometry(r); 534 } 535 536 bool ContactDelegate::eventFilter(QObject* object, QEvent* event) 537 { 538 QWidget* editor = qobject_cast<QWidget*>(object); 539 if (editor == NULL) 540 return false; 541 542 if (event->type() == QEvent::KeyPress) 543 { 544 switch (static_cast<QKeyEvent*>(event)->key()) 545 { 546 case Qt::Key_Enter: 547 case Qt::Key_Return: 548 emit commitData(editor); 549 emit closeEditor(editor, QAbstractItemDelegate::SubmitModelCache); 550 return true; 551 case Qt::Key_Escape: 552 // don't commit data 553 emit closeEditor(editor, QAbstractItemDelegate::RevertModelCache); 554 return true; 555 } 556 } 557 else if (event->type() == QEvent::FocusOut) 558 { 559 emit commitData(editor); 560 emit closeEditor(editor, NoHint); 561 } 562 563 return QAbstractItemDelegate::eventFilter(object, event); 564 } 565 -
trunk/qt4-gui/src/views/contactdelegate.h
r5930 r6148 72 72 virtual void paint(QPainter* p, const QStyleOptionViewItem& option, const QModelIndex& index) const; 73 73 74 /** 75 * Create widget that can be used for editing data in an item 76 * 77 * @param parent Parent widget for the editor 78 * @param option Style options 79 * @param index Item to edit 80 * @return An editor widget 81 */ 82 virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const; 83 84 /** 85 * Set data for an editor widget 86 * 87 * @param editor Existing editor widget to update with data 88 * @param index Item to get data from 89 */ 90 virtual void setEditorData(QWidget* editor, const QModelIndex& index) const; 91 92 /** 93 * Get data from editor widget and update model 94 * 95 * @param editor Existing editor widget with data 96 * @param model Model to write data to 97 * @param index Index of item to update 98 */ 99 virtual void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const; 100 101 /** 102 * Update geometry of an editor widget 103 * 104 * @param editor Existing editor widget to update geometry for 105 * @param option Style options, including new rect for editor 106 * @param index Index of item being edited 107 */ 108 virtual void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const; 109 110 /** 111 * Catch events for other objects. Used to get events for editor widget 112 * 113 * @param object Object the event happened for 114 * @param event Event for that object 115 * @return True if event should not be forwarded to object 116 */ 117 virtual bool eventFilter(QObject* object, QEvent* event); 118 74 119 private: 75 120 /** -
trunk/qt4-gui/src/views/userviewbase.cpp
r6100 r6148 50 50 { 51 51 setItemDelegate(new ContactDelegate(this, this)); 52 setEditTriggers(EditKeyPressed); 52 53 53 54 // Look'n'Feel
