Changeset 60496 in vbox for trunk/src/VBox/Frontends/VirtualBox/src/extensions/QITableView.cpp
- Timestamp:
- Apr 14, 2016 2:11:52 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VirtualBox/src/extensions/QITableView.cpp
r60495 r60496 20 20 #else /* !VBOX_WITH_PRECOMPILED_HEADERS */ 21 21 22 /* Qt includes: */ 23 # include <QStyledItemDelegate> 24 22 25 /* GUI includes: */ 23 26 # include "QITableView.h" 27 28 /* Other VBox includes: */ 29 # include "iprt/assert.h" 24 30 25 31 #endif /* !VBOX_WITH_PRECOMPILED_HEADERS */ 26 32 27 33 34 /** QStyledItemDelegate extension used with QITableView. */ 35 class QITableViewStyledItemDelegate : public QStyledItemDelegate 36 { 37 Q_OBJECT; 38 39 signals: 40 41 /** Notifies listeners about @a pEditor created for particular model @a index. */ 42 void sigEditorCreated(QWidget *pEditor, const QModelIndex &index) const; 43 44 public: 45 46 /** Constructs table-view styled-item-delegate on the basis of passed @a pParent. */ 47 QITableViewStyledItemDelegate(QObject *pParent); 48 49 private: 50 51 /** Returns the widget used to edit the item specified by @a index for editing. 52 * The @a pParent widget and style @a option are used to control how the editor widget appears. 53 * Besides that, we are notifying listener about editor was created for particular model @a index. */ 54 virtual QWidget* createEditor(QWidget *pParent, const QStyleOptionViewItem &option, const QModelIndex &index) const; 55 }; 56 57 58 /********************************************************************************************************************************* 59 * Class QITableViewStyledItemDelegate implementation. * 60 *********************************************************************************************************************************/ 61 62 QITableViewStyledItemDelegate::QITableViewStyledItemDelegate(QObject *pParent) 63 : QStyledItemDelegate(pParent) 64 { 65 } 66 67 QWidget* QITableViewStyledItemDelegate::createEditor(QWidget *pParent, const QStyleOptionViewItem &option, const QModelIndex &index) const 68 { 69 /* Call to base-class: */ 70 QWidget *pEditor = QStyledItemDelegate::createEditor(pParent, option, index); 71 /* Notify listeners about editor created: */ 72 emit sigEditorCreated(pEditor, index); 73 /* Return editor: */ 74 return pEditor; 75 } 76 77 78 /********************************************************************************************************************************* 79 * Class QITableView implementation. * 80 *********************************************************************************************************************************/ 81 28 82 QITableView::QITableView(QWidget *pParent) 29 83 : QTableView(pParent) 30 84 { 85 /* Prepare all: */ 86 prepare(); 87 } 88 89 void QITableView::makeSureEditorDataCommitted() 90 { 91 /* Do we have current editor at all? */ 92 QObject *pEditorObject = m_editors.value(currentIndex()); 93 if (pEditorObject && pEditorObject->isWidgetType()) 94 { 95 /* Cast the editor to widget type: */ 96 QWidget *pEditor = qobject_cast<QWidget*>(pEditorObject); 97 AssertPtrReturnVoid(pEditor); 98 { 99 /* Commit the editor data and closes it: */ 100 commitData(pEditor); 101 closeEditor(pEditor, QAbstractItemDelegate::SubmitModelCache); 102 } 103 } 104 } 105 106 void QITableView::prepare() 107 { 108 /* Delete old delegate: */ 109 delete itemDelegate(); 110 /* Create new delegate: */ 111 QITableViewStyledItemDelegate *pStyledItemDelegate = new QITableViewStyledItemDelegate(this); 112 AssertPtrReturnVoid(pStyledItemDelegate); 113 { 114 /* Assign newly created delegate to the table: */ 115 setItemDelegate(pStyledItemDelegate); 116 /* Connect newly created delegate to the table: */ 117 connect(pStyledItemDelegate, SIGNAL(sigEditorCreated(QWidget *, const QModelIndex &)), 118 this, SLOT(sltEditorCreated(QWidget *, const QModelIndex &))); 119 } 120 } 121 122 void QITableView::sltEditorCreated(QWidget *pEditor, const QModelIndex &index) 123 { 124 /* Connect created editor to the table and store it: */ 125 connect(pEditor, SIGNAL(destroyed(QObject *)), this, SLOT(sltEditorDestroyed(QObject *))); 126 m_editors[index] = pEditor; 127 } 128 129 void QITableView::sltEditorDestroyed(QObject *pEditor) 130 { 131 /* Clear destroyed editor from the table: */ 132 QModelIndex index = m_editors.key(pEditor); 133 AssertReturnVoid(index.isValid()); 134 m_editors.remove(index); 31 135 } 32 136 … … 39 143 } 40 144 145 #include "QITableView.moc" 146
Note:
See TracChangeset
for help on using the changeset viewer.