VirtualBox

Changeset 91252 in vbox


Ignore:
Timestamp:
Sep 15, 2021 12:50:23 PM (3 years ago)
Author:
vboxsync
Message:

FE/Qt: bugref:9996. Refactoring UIDiskFormatsGroupBox. part 1.

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

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/wizards/editors/UIWizardDiskEditors.cpp

    r91220 r91252  
    148148}
    149149
     150/* static */
     151QString UIDiskEditorGroupBox::defaultExtension(const CMediumFormat &mediumFormatRef, KDeviceType enmDeviceType)
     152{
     153    if (!mediumFormatRef.isNull())
     154    {
     155        /* Load extension / device list: */
     156        QVector<QString> fileExtensions;
     157        QVector<KDeviceType> deviceTypes;
     158        CMediumFormat mediumFormat(mediumFormatRef);
     159        mediumFormat.DescribeFileExtensions(fileExtensions, deviceTypes);
     160        for (int i = 0; i < fileExtensions.size(); ++i)
     161            if (deviceTypes[i] == enmDeviceType)
     162                return fileExtensions[i].toLower();
     163    }
     164    AssertMsgFailed(("Extension can't be NULL!\n"));
     165    return QString();
     166}
     167
    150168
    151169/*********************************************************************************************************************************
     
    155173UIDiskFormatsGroupBox::UIDiskFormatsGroupBox(bool fExpertMode, KDeviceType enmDeviceType, QWidget *pParent /* = 0 */)
    156174    : UIDiskEditorGroupBox(fExpertMode, pParent)
     175    , m_enmDeviceType(enmDeviceType)
    157176    , m_pFormatButtonGroup(0)
    158 {
    159     prepare(enmDeviceType);
     177    , m_pMainLayout(0)
     178{
     179    prepare();
     180
    160181}
    161182
    162183CMediumFormat UIDiskFormatsGroupBox::mediumFormat() const
    163184{
    164     return m_pFormatButtonGroup && m_pFormatButtonGroup->checkedButton() ? m_formats[m_pFormatButtonGroup->checkedId()] : CMediumFormat();
     185    return m_pFormatButtonGroup &&
     186           m_pFormatButtonGroup->checkedButton() ? m_formatList[m_pFormatButtonGroup->checkedId()].m_comFormat : CMediumFormat();
    165187}
    166188
    167189void UIDiskFormatsGroupBox::setMediumFormat(const CMediumFormat &mediumFormat)
    168190{
    169     int iPosition = m_formats.indexOf(mediumFormat);
     191    int iPosition = -1;
     192    for (int i = 0; i < m_formatList.size(); ++i)
     193    {
     194        if (mediumFormat == m_formatList[i].m_comFormat)
     195            iPosition = i;
     196    }
    170197    if (iPosition >= 0)
    171198    {
     
    180207}
    181208
    182 void UIDiskFormatsGroupBox::prepare(KDeviceType enmDeviceType)
    183 {
    184     QVBoxLayout *pContainerLayout = new QVBoxLayout(this);
    185 
    186     m_pFormatButtonGroup = new QButtonGroup(this);
    187     AssertReturnVoid(m_pFormatButtonGroup);
     209void UIDiskFormatsGroupBox::prepare()
     210{
     211    m_pMainLayout = new QVBoxLayout(this);
     212    populateFormats();
     213    createFormatWidgets();
     214    retranslateUi();
     215}
     216
     217void UIDiskFormatsGroupBox::populateFormats(){
    188218    /* Enumerate medium formats in special order: */
    189219    CSystemProperties properties = uiCommon().virtualBox().GetSystemProperties();
     
    209239    /* Create buttons for VDI, preferred and others: */
    210240    foreach (const QString &strId, vdi.keys())
    211         addFormatButton(pContainerLayout, vdi.value(strId), enmDeviceType, true);
     241        addFormat(vdi.value(strId), true);
    212242    foreach (const QString &strId, preferred.keys())
    213         addFormatButton(pContainerLayout, preferred.value(strId), enmDeviceType, true);
    214 
    215     if (m_fExpertMode || enmDeviceType == KDeviceType_DVD || enmDeviceType == KDeviceType_Floppy)
     243        addFormat(preferred.value(strId), true);
     244
     245    if (m_fExpertMode || m_enmDeviceType == KDeviceType_DVD || m_enmDeviceType == KDeviceType_Floppy)
    216246    {
    217247        foreach (const QString &strId, others.keys())
    218             addFormatButton(pContainerLayout, others.value(strId), enmDeviceType);
    219     }
    220 
    221     /* Select VDI: */
    222     if (!m_pFormatButtonGroup->buttons().isEmpty())
    223     {
    224         m_pFormatButtonGroup->button(0)->click();
    225         m_pFormatButtonGroup->button(0)->setFocus();
    226     }
    227 
    228     connect(m_pFormatButtonGroup, static_cast<void(QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked),
    229             this, &UIDiskFormatsGroupBox::sigMediumFormatChanged);
    230 
    231     retranslateUi();
     248            addFormat(others.value(strId));
     249    }
    232250}
    233251
     
    241259    {
    242260        QAbstractButton *pButton = buttons[i];
    243         UIMediumFormat enmFormat = gpConverter->fromInternalString<UIMediumFormat>(m_formatNames[m_pFormatButtonGroup->id(pButton)]);
     261        UIMediumFormat enmFormat = gpConverter->fromInternalString<UIMediumFormat>(m_formatList[m_pFormatButtonGroup->id(pButton)].m_strName);
    244262        pButton->setText(gpConverter->toString(enmFormat));
    245263    }
    246264}
    247265
    248 void UIDiskFormatsGroupBox::addFormatButton(QVBoxLayout *pFormatLayout, CMediumFormat medFormat,
    249                                             KDeviceType enmDeviceType, bool fPreferred /* = false */)
     266void UIDiskFormatsGroupBox::addFormat(CMediumFormat medFormat, bool fPreferred /* = false */)
    250267{
    251268    /* Check that medium format supports creation: */
     
    264281    QVector<KDeviceType> deviceTypes;
    265282    medFormat.DescribeFileExtensions(fileExtensions, deviceTypes);
    266     if (!deviceTypes.contains(enmDeviceType))
     283    if (!deviceTypes.contains(m_enmDeviceType))
    267284        return;
    268 
    269     /* Create/add corresponding radio-button: */
    270     QRadioButton *pFormatButton = new QRadioButton;
    271     AssertPtrReturnVoid(pFormatButton);
    272     {
     285    m_formatList << Format(medFormat, medFormat.GetName(), defaultExtension(medFormat, m_enmDeviceType), fPreferred);
     286}
     287
     288const QStringList UIDiskFormatsGroupBox::formatExtensions() const
     289{
     290    QStringList extensionList;
     291    foreach (const Format &format, m_formatList)
     292        extensionList << format.m_strExtension;
     293    return extensionList;
     294}
     295
     296void UIDiskFormatsGroupBox::createFormatWidgets()
     297{
     298    AssertReturnVoid(m_pMainLayout);
     299    AssertReturnVoid(!m_formatList.isEmpty());
     300    m_pFormatButtonGroup = new QButtonGroup(this);
     301    AssertReturnVoid(m_pFormatButtonGroup);
     302
     303    for (int i = 0; i < m_formatList.size(); ++i)
     304    {
     305        QRadioButton *pFormatButton = new QRadioButton;
     306        if (!pFormatButton)
     307            continue;
     308
    273309        /* Make the preferred button font bold: */
    274         if (fPreferred && m_fExpertMode)
     310        if (m_formatList[i].m_fPreferred && m_fExpertMode)
    275311        {
    276312            QFont font = pFormatButton->font();
     
    278314            pFormatButton->setFont(font);
    279315        }
    280         pFormatLayout->addWidget(pFormatButton);
    281         m_formats << medFormat;
    282         m_formatNames << medFormat.GetName();
    283         m_pFormatButtonGroup->addButton(pFormatButton, m_formatNames.size() - 1);
    284         m_formatExtensions << defaultExtension(medFormat, enmDeviceType);
    285     }
    286 }
    287 
    288 const QStringList UIDiskFormatsGroupBox::formatExtensions() const
    289 {
    290     return m_formatExtensions;
    291 }
    292 
    293 /* static */
    294 QString UIDiskFormatsGroupBox::defaultExtension(const CMediumFormat &mediumFormatRef, KDeviceType enmDeviceType)
    295 {
    296     if (!mediumFormatRef.isNull())
    297     {
    298         /* Load extension / device list: */
    299         QVector<QString> fileExtensions;
    300         QVector<KDeviceType> deviceTypes;
    301         CMediumFormat mediumFormat(mediumFormatRef);
    302         mediumFormat.DescribeFileExtensions(fileExtensions, deviceTypes);
    303         for (int i = 0; i < fileExtensions.size(); ++i)
    304             if (deviceTypes[i] == enmDeviceType)
    305                 return fileExtensions[i].toLower();
    306     }
    307     AssertMsgFailed(("Extension can't be NULL!\n"));
    308     return QString();
    309 }
    310 
    311 
     316        m_pMainLayout->addWidget(pFormatButton);
     317        m_pFormatButtonGroup->addButton(pFormatButton, i);
     318    }
     319
     320    setMediumFormat(m_formatList[0].m_comFormat);
     321    connect(m_pFormatButtonGroup, static_cast<void(QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked),
     322            this, &UIDiskFormatsGroupBox::sigMediumFormatChanged);
     323}
    312324/*********************************************************************************************************************************
    313325*   UIDiskVariantGroupBox implementation.                                                                                   *
  • trunk/src/VBox/Frontends/VirtualBox/src/wizards/editors/UIWizardDiskEditors.h

    r91223 r91252  
    2525#include <QIcon>
    2626#include <QGroupBox>
     27#include <QVector>
    2728
    2829/* Local includes: */
     
    6465    static QString openFileDialogForDiskFile(const QString &strInitialPath, const CMediumFormat &comMediumFormat,
    6566                                             KDeviceType enmDeviceType, QWidget *pParent);
     67    /** Attempts to find a file extention for the device type @p enmDeviceType within the extensions
     68      * returned by CMediumFormat::DescribeFileExtensions(..). */
     69    static QString defaultExtension(const CMediumFormat &mediumFormatRef, KDeviceType enmDeviceType);
    6670
    6771protected:
     
    8589    const CMediumFormat &VDIMediumFormat() const;
    8690    const QStringList formatExtensions() const;
    87     /** Attempts to find a file extention for the device type @p enmDeviceType within the extensions
    88       * returned by CMediumFormat::DescribeFileExtensions(..). */
    89     static QString defaultExtension(const CMediumFormat &mediumFormatRef, KDeviceType enmDeviceType);
    9091
    9192private:
     93    struct Format
     94    {
     95        CMediumFormat m_comFormat;
     96        QString       m_strName;
     97        QString       m_strExtension;
     98        bool          m_fPreferred;
     99        Format(const CMediumFormat &comFormat, const QString &strName,
     100               const QString &strExtension, bool fPreferred)
     101               : m_comFormat(comFormat)
     102               , m_strName(strName)
     103               , m_strExtension(strExtension)
     104               , m_fPreferred(fPreferred){}
     105    };
    92106
    93     void prepare(KDeviceType enmDeviceType);
    94     void addFormatButton(QVBoxLayout *pFormatLayout, CMediumFormat medFormat, KDeviceType enmDeviceType, bool fPreferred = false);
    95 
     107    void prepare();
     108    void populateFormats();
     109    void addFormat(CMediumFormat medFormat, bool fPreferred = false);
     110    void createFormatWidgets();
    96111    virtual void retranslateUi() /* override final */;
    97112
    98     QList<CMediumFormat>  m_formats;
     113    QVector<Format> m_formatList;
     114
    99115    CMediumFormat m_comVDIMediumFormat;
    100     QStringList           m_formatNames;
    101     QStringList m_formatExtensions;
     116    KDeviceType m_enmDeviceType;
     117
    102118    QButtonGroup *m_pFormatButtonGroup;
     119    QVBoxLayout *m_pMainLayout;
    103120};
    104121
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