VirtualBox

Ignore:
Timestamp:
Apr 15, 2019 7:20:20 PM (6 years ago)
Author:
vboxsync
Message:

FE/Qt: bugref:9434: Import Appliance wizard: Form Editor model: Implement bool/string/choice editors.

Location:
trunk/src/VBox/Frontends/VirtualBox/src/wizards/importappliance
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/wizards/importappliance/UIFormEditorWidget.cpp

    r78130 r78131  
    4848
    4949
     50/** QComboBox extension used as Port editor. */
     51class ChoiceEditor : public QComboBox
     52{
     53    Q_OBJECT;
     54    Q_PROPERTY(ChoiceData choice READ choice WRITE setChoice USER true);
     55
     56public:
     57
     58    /** Constructs Port-editor passing @a pParent to the base-class. */
     59    ChoiceEditor(QWidget *pParent = 0)
     60        : QComboBox(pParent) {}
     61
     62private:
     63
     64    /** Defines the @a choice. */
     65    void setChoice(ChoiceData choice)
     66    {
     67        addItems(choice.choices().toList());
     68        setCurrentIndex(choice.selectedChoice());
     69    }
     70
     71    /** Returns the choice. */
     72    ChoiceData choice() const
     73    {
     74        QVector<QString> choices(count());
     75        for (int i = 0; i < count(); ++i)
     76            choices[i] = itemText(i);
     77        return ChoiceData(choices, currentIndex());
     78    }
     79};
     80
     81
    5082/** QITableViewCell extension used as Form Editor table-view cell. */
    5183class UIFormEditorCell : public QITableViewCell
     
    87119    /** Returns the row value as string. */
    88120    QString valueToString() const;
     121
     122    /** Returns value cast to bool. */
     123    bool toBool() const;
     124
     125    /** Returns value cast to string. */
     126    QString toString() const;
     127
     128    /** Returns value cast to choice. */
     129    ChoiceData toChoice() const;
    89130
    90131protected:
     
    215256{
    216257    return m_cells.at(UIFormEditorDataType_Value)->text();
     258}
     259
     260bool UIFormEditorRow::toBool() const
     261{
     262    AssertReturn(valueType() == KFormValueType_Boolean, false);
     263    CBooleanFormValue comValue(m_comValue);
     264    return comValue.GetSelected();
     265}
     266
     267QString UIFormEditorRow::toString() const
     268{
     269    AssertReturn(valueType() == KFormValueType_String, QString());
     270    CStringFormValue comValue(m_comValue);
     271    return comValue.GetString();
     272}
     273
     274ChoiceData UIFormEditorRow::toChoice() const
     275{
     276    AssertReturn(valueType() == KFormValueType_Choice, ChoiceData());
     277    CChoiceFormValue comValue(m_comValue);
     278    return ChoiceData(comValue.GetValues(), comValue.GetSelectedIndex());
    217279}
    218280
     
    380442    switch (iRole)
    381443    {
     444        /* Checkstate role: */
     445        case Qt::CheckStateRole:
     446        {
     447            /* Switch for different columns: */
     448            switch (index.column())
     449            {
     450                case UIFormEditorDataType_Value:
     451                    return   m_dataList[index.row()]->valueType() == KFormValueType_Boolean
     452                           ? m_dataList[index.row()]->toBool()
     453                           ? Qt::Checked
     454                           : Qt::Unchecked
     455                           : QVariant();
     456                default:
     457                    return QVariant();
     458            }
     459        }
    382460        /* Display role: */
    383461        case Qt::DisplayRole:
     
    396474            }
    397475        }
     476        /* Edit role: */
     477        case Qt::EditRole:
     478        {
     479            /* Switch for different columns: */
     480            switch (index.column())
     481            {
     482                case UIFormEditorDataType_Value:
     483                {
     484                    switch (m_dataList[index.row()]->valueType())
     485                    {
     486                        case KFormValueType_String:
     487                            return QVariant::fromValue(m_dataList[index.row()]->toString());
     488                        case KFormValueType_Choice:
     489                            return QVariant::fromValue(m_dataList[index.row()]->toChoice());
     490                        default:
     491                            return QVariant();
     492                    }
     493                }
     494                default:
     495                    return QVariant();
     496            }
     497        }
    398498        /* Alignment role: */
    399499        case Qt::TextAlignmentRole:
     
    485585                m_pTableView->setModel(m_pTableModel);
    486586
     587            /* We certainly have abstract item delegate: */
     588            QAbstractItemDelegate *pAbstractItemDelegate = m_pTableView->itemDelegate();
     589            if (pAbstractItemDelegate)
     590            {
     591                /* But is this also styled item delegate? */
     592                QStyledItemDelegate *pStyledItemDelegate = qobject_cast<QStyledItemDelegate*>(pAbstractItemDelegate);
     593                if (pStyledItemDelegate)
     594                {
     595                    /* Create new item editor factory: */
     596                    QItemEditorFactory *pNewItemEditorFactory = new QItemEditorFactory;
     597                    if (pNewItemEditorFactory)
     598                    {
     599                        /* Register ChoiceEditor as the ChoiceData editor: */
     600                        int iChoiceId = qRegisterMetaType<ChoiceData>();
     601                        QStandardItemEditorCreator<ChoiceEditor> *pChoiceEditorItemCreator = new QStandardItemEditorCreator<ChoiceEditor>();
     602                        pNewItemEditorFactory->registerEditor((QVariant::Type)iChoiceId, pChoiceEditorItemCreator);
     603
     604                        /* Set newly created item editor factory for table delegate: */
     605                        pStyledItemDelegate->setItemEditorFactory(pNewItemEditorFactory);
     606                    }
     607                }
     608            }
     609
    487610            /* Add into layout: */
    488611            pLayout->addWidget(m_pTableView);
  • trunk/src/VBox/Frontends/VirtualBox/src/wizards/importappliance/UIFormEditorWidget.h

    r78129 r78131  
    3131
    3232
     33/** Class used to hold choice data. */
     34class ChoiceData
     35{
     36public:
     37
     38    /** Constructs null choice data. */
     39    ChoiceData() {}
     40    /** Constructs choice data on the basis of passed @a choices and @a iSelectedChoice. */
     41    ChoiceData(const QVector<QString> &choices, int iSelectedChoice)
     42        : m_choices(choices), m_iSelectedChoice(iSelectedChoice) {}
     43
     44    /** Returns choice list. */
     45    QVector<QString> choices() const { return m_choices; }
     46    /** Returns current selected choice. */
     47    int selectedChoice() const { return m_iSelectedChoice; }
     48
     49private:
     50
     51    /** Holds choice list. */
     52    QVector<QString>  m_choices;
     53    /** Holds current selected choice. */
     54    int               m_iSelectedChoice;
     55};
     56Q_DECLARE_METATYPE(ChoiceData);
     57
     58
    3359/** QWidget subclass representing model/view Form Editor widget. */
    3460class UIFormEditorWidget : public QWidget
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