VirtualBox

Ignore:
Timestamp:
Apr 14, 2016 2:11:52 PM (9 years ago)
Author:
vboxsync
Message:

FE/Qt: ​​bugref:7456: Port Forwarding UI: Possibility to manually commit embedded-editor data to QITableView. It's used in Port Forwarding UI to prevent loosing edited data when dialog is closed with Ok while editor still opened.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/extensions/QITableView.cpp

    r60495 r60496  
    2020#else  /* !VBOX_WITH_PRECOMPILED_HEADERS */
    2121
     22/* Qt includes: */
     23# include <QStyledItemDelegate>
     24
    2225/* GUI includes: */
    2326# include "QITableView.h"
     27
     28/* Other VBox includes: */
     29# include "iprt/assert.h"
    2430
    2531#endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
    2632
    2733
     34/** QStyledItemDelegate extension used with QITableView. */
     35class QITableViewStyledItemDelegate : public QStyledItemDelegate
     36{
     37    Q_OBJECT;
     38
     39signals:
     40
     41    /** Notifies listeners about @a pEditor created for particular model @a index. */
     42    void sigEditorCreated(QWidget *pEditor, const QModelIndex &index) const;
     43
     44public:
     45
     46    /** Constructs table-view styled-item-delegate on the basis of passed @a pParent. */
     47    QITableViewStyledItemDelegate(QObject *pParent);
     48
     49private:
     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
     62QITableViewStyledItemDelegate::QITableViewStyledItemDelegate(QObject *pParent)
     63    : QStyledItemDelegate(pParent)
     64{
     65}
     66
     67QWidget* 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
    2882QITableView::QITableView(QWidget *pParent)
    2983    : QTableView(pParent)
    3084{
     85    /* Prepare all: */
     86    prepare();
     87}
     88
     89void 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
     106void 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
     122void 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
     129void 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);
    31135}
    32136
     
    39143}
    40144
     145#include "QITableView.moc"
     146
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette