VirtualBox

Changeset 90453 in vbox for trunk/src


Ignore:
Timestamp:
Aug 1, 2021 9:04:40 AM (4 years ago)
Author:
vboxsync
Message:

FE/Qt: bugref:10067: UIFDCreationDialog: Full cleanup / refactoring; Make sure it creates floppy async way.

Location:
trunk/src/VBox/Frontends/VirtualBox/src/medium
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/medium/UIFDCreationDialog.cpp

    r82968 r90453  
    55
    66/*
    7  * Copyright (C) 2008-2020 Oracle Corporation
     7 * Copyright (C) 2008-2021 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    1717
    1818/* Qt includes */
    19 #include<QCheckBox>
    20 #include<QDialogButtonBox>
    21 #include<QDir>
    22 #include<QGridLayout>
    23 #include<QLabel>
    24 #include<QPushButton>
     19#include <QCheckBox>
     20#include <QDialogButtonBox>
     21#include <QDir>
     22#include <QGridLayout>
     23#include <QLabel>
     24#include <QPushButton>
    2525
    2626/* GUI includes */
     27#include "UICommon.h"
    2728#include "UIFDCreationDialog.h"
    2829#include "UIFilePathSelector.h"
    2930#include "UIMedium.h"
    3031#include "UIMessageCenter.h"
    31 #include "UICommon.h"
     32#include "UINotificationCenter.h"
    3233
    3334/* COM includes: */
     
    3839
    3940UIFDCreationDialog::UIFDCreationDialog(QWidget *pParent,
    40                                            const QString &strDefaultFolder,
    41                                            const QString &strMachineName /* = QString() */)
    42    : QIWithRetranslateUI<QDialog>(pParent)
    43     , m_pFilePathselector(0)
    44     , m_pPathLabel(0)
    45     , m_pSizeLabel(0)
    46     , m_pSizeCombo(0)
    47     , m_pButtonBox(0)
    48     , m_pFormatCheckBox(0)
     41                                       const QString &strDefaultFolder,
     42                                       const QString &strMachineName /* = QString() */)
     43    : QIWithRetranslateUI<QDialog>(pParent)
    4944    , m_strDefaultFolder(strDefaultFolder)
    5045    , m_strMachineName(strMachineName)
    51 {
    52 
     46    , m_pLabelPath(0)
     47    , m_pFilePathSelector(0)
     48    , m_pSizeLabel(0)
     49    , m_pComboSize(0)
     50    , m_pCheckBoxFormat(0)
     51    , m_pButtonBox(0)
     52{
    5353    prepare();
    54     /* Adjust dialog size: */
    55     adjustSize();
     54}
     55
     56QUuid UIFDCreationDialog::mediumID() const
     57{
     58    return m_uMediumID;
     59}
     60
     61void UIFDCreationDialog::accept()
     62{
     63    /* Make Ok button disabled first of all: */
     64    m_pButtonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
     65
     66    /* Acquire medium path & formats: */
     67    const QString strMediumLocation = m_pFilePathSelector->path();
     68    const QVector<CMediumFormat> mediumFormats = UIMediumDefs::getFormatsForDeviceType(KDeviceType_Floppy);
     69    /* Make sure we have both path and formats selected: */
     70    if (strMediumLocation.isEmpty() || mediumFormats.isEmpty())
     71        return;
     72
     73    /* Get VBox for further activities: */
     74    CVirtualBox comVBox = uiCommon().virtualBox();
     75
     76    /* Create medium: */
     77    CMedium comMedium = comVBox.CreateMedium(mediumFormats[0].GetName(), strMediumLocation,
     78                                             KAccessMode_ReadWrite, KDeviceType_Floppy);
     79    if (!comVBox.isOk())
     80    {
     81        msgCenter().cannotCreateMediumStorage(comVBox, strMediumLocation, this);
     82        return;
     83    }
     84
     85    /* Compose medium storage variants: */
     86    QVector<KMediumVariant> variants(1, KMediumVariant_Fixed);
     87    /* Decide if disk formatting is required: */
     88    if (m_pCheckBoxFormat && m_pCheckBoxFormat->checkState() == Qt::Checked)
     89        variants.push_back(KMediumVariant_Formatted);
     90
     91    /* Create medium storage, asynchronously: */
     92    UINotificationProgressMediumCreate *pNotification =
     93        new UINotificationProgressMediumCreate(comMedium, m_pComboSize->currentData().toLongLong(), variants);
     94    connect(pNotification, &UINotificationProgressMediumCreate::sigMediumCreated,
     95            &uiCommon(), &UICommon::sltHandleMediumCreated);
     96    connect(pNotification, &UINotificationProgressMediumCreate::sigMediumCreated,
     97            this, &UIFDCreationDialog::sltHandleMediumCreated);
     98    notificationCenter().append(pNotification);
     99}
     100
     101void UIFDCreationDialog::retranslateUi()
     102{
     103    if (m_strMachineName.isEmpty())
     104        setWindowTitle(QString("%1").arg(tr("Floppy Disk Creator")));
     105    else
     106        setWindowTitle(QString("%1 - %2").arg(m_strMachineName).arg(tr("Floppy Disk Creator")));
     107    if (m_pLabelPath)
     108        m_pLabelPath->setText(tr("File Path:"));
     109    if (m_pSizeLabel)
     110        m_pSizeLabel->setText(tr("Size:"));
     111    if (m_pButtonBox)
     112        m_pButtonBox->button(QDialogButtonBox::Ok)->setText("Create");
     113    if (m_pCheckBoxFormat)
     114        m_pCheckBoxFormat->setText(tr("Format disk as FAT12"));
     115    if (m_pComboSize)
     116    {
     117        //m_pComboSize->setItemText(FDSize_2_88M, tr("2.88M"));
     118        m_pComboSize->setItemText(FDSize_1_44M, tr("1.44M"));
     119        m_pComboSize->setItemText(FDSize_1_2M, tr("1.2M"));
     120        m_pComboSize->setItemText(FDSize_720K, tr("720K"));
     121        m_pComboSize->setItemText(FDSize_360K, tr("360K"));
     122    }
     123}
     124
     125void UIFDCreationDialog::sltHandleMediumCreated(const CMedium &comMedium)
     126{
     127    /* Store the ID of the newly created medium: */
     128    m_uMediumID = comMedium.GetId();
     129
     130    /* Close the dialog now: */
     131    QDialog::accept();
     132}
     133
     134void UIFDCreationDialog::prepare()
     135{
     136#ifndef VBOX_WS_MAC
     137    setWindowIcon(QIcon(":/fd_add_32px.png"));
     138#endif
     139
     140    setWindowModality(Qt::WindowModal);
     141    setSizeGripEnabled(false);
     142
     143    /* Prepare main layout: */
     144    QGridLayout *pLayoutMain = new QGridLayout(this);
     145    if (pLayoutMain)
     146    {
     147        /* Prepare path label: */
     148        m_pLabelPath = new QLabel(this);
     149        if (m_pLabelPath)
     150        {
     151            m_pLabelPath->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
     152            pLayoutMain->addWidget(m_pLabelPath, 0, 0);
     153        }
     154        /* Prepare file path selector: */
     155        m_pFilePathSelector = new UIFilePathSelector(this);
     156        if (m_pFilePathSelector)
     157        {
     158            m_pFilePathSelector->setMode(UIFilePathSelector::Mode_File_Save);
     159            const QString strFilePath = getDefaultFilePath();
     160            m_pFilePathSelector->setDefaultPath(strFilePath);
     161            m_pFilePathSelector->setPath(strFilePath);
     162
     163            pLayoutMain->addWidget(m_pFilePathSelector, 0, 1, 1, 3);
     164        }
     165
     166        /* Prepare size label: */
     167        m_pSizeLabel = new QLabel(this);
     168        if (m_pSizeLabel)
     169        {
     170            m_pSizeLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
     171            pLayoutMain->addWidget(m_pSizeLabel, 1, 0);
     172        }
     173        /* Prepare size combo: */
     174        m_pComboSize = new QComboBox(this);
     175        if (m_pComboSize)
     176        {
     177            //m_pComboSize->insertItem(FDSize_2_88M, "2.88M", 2949120);
     178            m_pComboSize->insertItem(FDSize_1_44M, "1.44M", 1474560);
     179            m_pComboSize->insertItem(FDSize_1_2M, "1.2M", 1228800);
     180            m_pComboSize->insertItem(FDSize_720K, "720K", 737280);
     181            m_pComboSize->insertItem(FDSize_360K, "360K", 368640);
     182            m_pComboSize->setCurrentIndex(FDSize_1_44M);
     183
     184            pLayoutMain->addWidget(m_pComboSize, 1, 1);
     185        }
     186
     187        /* Prepare format check-box: */
     188        m_pCheckBoxFormat = new QCheckBox;
     189        if (m_pCheckBoxFormat)
     190        {
     191            m_pCheckBoxFormat->setCheckState(Qt::Checked);
     192            pLayoutMain->addWidget(m_pCheckBoxFormat, 2, 1, 1, 2);
     193        }
     194
     195        /* Prepare button-box: */
     196        m_pButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
     197        if (m_pButtonBox)
     198        {
     199            connect(m_pButtonBox, &QDialogButtonBox::accepted, this, &UIFDCreationDialog::accept);
     200            connect(m_pButtonBox, &QDialogButtonBox::rejected, this, &UIFDCreationDialog::reject);
     201
     202            pLayoutMain->addWidget(m_pButtonBox, 3, 0, 1, 3);
     203        }
     204    }
     205
     206    /* Apply language settings: */
     207    retranslateUi();
    56208
    57209#ifdef VBOX_WS_MAC
     
    59211    setFixedSize(minimumSize());
    60212#endif /* VBOX_WS_MAC */
    61 }
    62 
    63 void UIFDCreationDialog::retranslateUi()
    64 {
    65     if (m_strMachineName.isEmpty())
    66         setWindowTitle(QString("%1").arg(tr("Floppy Disk Creator")));
    67     else
    68         setWindowTitle(QString("%1 - %2").arg(m_strMachineName).arg(tr("Floppy Disk Creator")));
    69     if (m_pPathLabel)
    70         m_pPathLabel->setText(tr("File Path:"));
    71     if (m_pSizeLabel)
    72         m_pSizeLabel->setText(tr("Size:"));
    73     if (m_pButtonBox)
    74         m_pButtonBox->button(QDialogButtonBox::Ok)->setText("Create");
    75     if (m_pFormatCheckBox)
    76         m_pFormatCheckBox->setText(tr("Format disk as FAT12"));
    77     if (m_pSizeCombo)
    78     {
    79         //m_pSizeCombo->setItemText(FDSize_2_88M, tr("2.88M"));
    80         m_pSizeCombo->setItemText(FDSize_1_44M, tr("1.44M"));
    81         m_pSizeCombo->setItemText(FDSize_1_2M, tr("1.2M"));
    82         m_pSizeCombo->setItemText(FDSize_720K, tr("720K"));
    83         m_pSizeCombo->setItemText(FDSize_360K, tr("360K"));
    84     }
    85 }
    86 
    87 void UIFDCreationDialog::prepare()
    88 {
    89 
    90 #ifndef VBOX_WS_MAC
    91     setWindowIcon(QIcon(":/fd_add_32px.png"));
    92 #endif
    93 
    94     setWindowModality(Qt::WindowModal);
    95     setSizeGripEnabled(false);
    96 
    97     QGridLayout *pMainLayout = new QGridLayout;
    98     if (!pMainLayout)
    99         return;
    100     setLayout(pMainLayout);
    101 
    102     m_pPathLabel = new QLabel;
    103     if (m_pPathLabel)
    104     {
    105         pMainLayout->addWidget(m_pPathLabel, 0, 0, 1, 1);
    106         m_pPathLabel->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
    107     }
    108 
    109     m_pFilePathselector = new UIFilePathSelector;
    110     if (m_pFilePathselector)
    111     {
    112         pMainLayout->addWidget(m_pFilePathselector, 0, 1, 1, 2);
    113         m_pFilePathselector->setMode(UIFilePathSelector::Mode_File_Save);
    114         QString strFolder = getDefaultFolder();
    115         m_pFilePathselector->setDefaultPath(strFolder);
    116         m_pFilePathselector->setPath(strFolder);
    117     }
    118 
    119     m_pSizeLabel = new QLabel;
    120     if (m_pSizeLabel)
    121     {
    122         pMainLayout->addWidget(m_pSizeLabel, 1, 0, 1, 1);
    123         m_pSizeLabel->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
    124     }
    125 
    126     m_pSizeCombo = new QComboBox;
    127     if (m_pSizeCombo)
    128     {
    129         pMainLayout->addWidget(m_pSizeCombo, 1, 1, 1, 1);
    130         //m_pSizeCombo->insertItem(FDSize_2_88M, "2.88M", 2949120);
    131         m_pSizeCombo->insertItem(FDSize_1_44M, "1.44M", 1474560);
    132         m_pSizeCombo->insertItem(FDSize_1_2M, "1.2M", 1228800);
    133         m_pSizeCombo->insertItem(FDSize_720K, "720K", 737280);
    134         m_pSizeCombo->insertItem(FDSize_360K, "360K", 368640);
    135         m_pSizeCombo->setCurrentIndex(FDSize_1_44M);
    136 
    137     }
    138 
    139     m_pFormatCheckBox = new QCheckBox;
    140     if (m_pFormatCheckBox)
    141     {
    142         pMainLayout->addWidget(m_pFormatCheckBox, 2, 1, 1, 1);
    143         m_pFormatCheckBox->setCheckState(Qt::Checked);
    144     }
    145 
    146     m_pButtonBox =
    147         new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
    148     if (m_pButtonBox)
    149     {
    150         pMainLayout->addWidget(m_pButtonBox, 3, 0, 1, 3);
    151         connect(m_pButtonBox, &QDialogButtonBox::accepted, this, &UIFDCreationDialog::accept);
    152         connect(m_pButtonBox, &QDialogButtonBox::rejected, this, &UIFDCreationDialog::reject);
    153     }
    154     retranslateUi();
    155 }
    156 
    157 QString UIFDCreationDialog::getDefaultFolder() const
    158 {
    159     QString strPreferredExtension = UIMediumDefs::getPreferredExtensionForMedium(KDeviceType_Floppy);
    160 
    161     QString strInitialPath = m_strDefaultFolder;
    162     if (strInitialPath.isEmpty())
    163         strInitialPath = uiCommon().virtualBox().GetSystemProperties().GetDefaultMachineFolder();
    164 
    165     if (strInitialPath.isEmpty())
    166         return strInitialPath;
    167 
    168     QString strDiskname = !(m_strMachineName.isEmpty()) ? m_strMachineName : "NewFloppyDisk";
     213
     214    /* Adjust dialog size: */
     215    adjustSize();
     216}
     217
     218QString UIFDCreationDialog::getDefaultFilePath() const
     219{
     220    /* Prepare default file-path on the basis of passerd default folder: */
     221    QString strDefaultFilePath = m_strDefaultFolder;
     222
     223    /* Make sure it's not empty if possible: */
     224    if (strDefaultFilePath.isEmpty())
     225        strDefaultFilePath = uiCommon().virtualBox().GetSystemProperties().GetDefaultMachineFolder();
     226    if (strDefaultFilePath.isEmpty())
     227        return strDefaultFilePath;
     228
     229    /* Append file-path with disc name, generate unique file-name if necessary: */
     230    QString strDiskname = !m_strMachineName.isEmpty() ? m_strMachineName : "NewFloppyDisk";
    169231    strDiskname = UICommon::findUniqueFileName(m_strDefaultFolder, strDiskname);
    170232
    171     strInitialPath = QDir(strInitialPath).absoluteFilePath(strDiskname + "." + strPreferredExtension);
    172     return strInitialPath;
    173 }
    174 
    175 void UIFDCreationDialog::accept()
    176 {
    177     QVector<CMediumFormat>  mediumFormats = UIMediumDefs::getFormatsForDeviceType(KDeviceType_Floppy);
    178 
    179     if (m_pFilePathselector->path().isEmpty() || mediumFormats.isEmpty())
    180         return;
    181 
    182     CVirtualBox vbox = uiCommon().virtualBox();
    183     QString strMediumLocation = m_pFilePathselector->path();
    184 
    185     CMedium newMedium = vbox.CreateMedium(mediumFormats[0].GetName(), strMediumLocation,
    186                                           KAccessMode_ReadWrite, KDeviceType_Floppy);
    187     if (!vbox.isOk())
    188     {
    189         msgCenter().cannotCreateMediumStorage(vbox, strMediumLocation, this);
    190         return;
    191     }
    192 
    193     QVector<KMediumVariant> variants(1, KMediumVariant_Fixed);
    194     /* Decide if formatting the disk is required: */
    195     if (m_pFormatCheckBox && m_pFormatCheckBox->checkState() == Qt::Checked)
    196         variants.push_back(KMediumVariant_Formatted);
    197     CProgress progress = newMedium.CreateBaseStorage(m_pSizeCombo->currentData().toLongLong(), variants);
    198 
    199     if (!newMedium.isOk())
    200     {
    201         msgCenter().cannotCreateMediumStorage(newMedium, strMediumLocation, this);
    202         return;
    203     }
    204     /* Show creation progress: */
    205     msgCenter().showModalProgressDialog(progress, windowTitle(), ":/progress_media_create_90px.png", this);
    206     if (progress.GetCanceled())
    207         return;
    208 
    209     if (!progress.isOk() || progress.GetResultCode() != 0)
    210     {
    211         msgCenter().cannotCreateHardDiskStorage(progress, strMediumLocation, this);
    212         return;
    213     }
    214     /* Store the id of the newly create medium: */
    215     m_uMediumID = newMedium.GetId();
    216 
    217     /* Notify UICommon about the new medium: */
    218     uiCommon().createMedium(UIMedium(newMedium, UIMediumDeviceType_Floppy, KMediumState_Created));
    219 
    220     /* After a successful creation and initilization of the floppy disk we call base class accept
    221        effectively closing this dialog: */
    222     QDialog::accept();
    223 }
    224 
    225 QUuid UIFDCreationDialog::mediumID() const
    226 {
    227     return m_uMediumID;
    228 }
     233    /* Append file-path with preferred extension finally: */
     234    const QString strPreferredExtension = UIMediumDefs::getPreferredExtensionForMedium(KDeviceType_Floppy);
     235    strDefaultFilePath = QDir(strDefaultFilePath).absoluteFilePath(strDiskname + "." + strPreferredExtension);
     236
     237    /* Return default file-path: */
     238    return strDefaultFilePath;
     239}
  • trunk/src/VBox/Frontends/VirtualBox/src/medium/UIFDCreationDialog.h

    r82968 r90453  
    55
    66/*
    7  * Copyright (C) 2008-2020 Oracle Corporation
     7 * Copyright (C) 2008-2021 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    3535class QLabel;
    3636class UIFilePathSelector;
     37class CMedium;
    3738
    38 /* A QDialog extension to get necessary setting from the user for floppy disk creation */
     39/* A QDialog extension to get necessary setting from the user for floppy disk creation. */
    3940class SHARED_LIBRARY_STUFF UIFDCreationDialog : public QIWithRetranslateUI<QDialog>
    40 
    4141{
    4242    Q_OBJECT;
    4343
    44 
    4544public:
    4645
     46    /** Constructs the floppy disc creation dialog passing @a pParent to the base-class.
     47      * @param  strDefaultFolder  Brings the default folder.
     48      * @param  strMachineName    Brings the machine name. */
    4749    UIFDCreationDialog(QWidget *pParent,
    4850                       const QString &strDefaultFolder,
    4951                       const QString &strMachineName = QString());
    5052
     53    /** Return the medium ID. */
     54    QUuid mediumID() const;
    5155
    52     virtual void accept() /* override */;
    53     /* Return the mediumID */
    54     QUuid mediumID() const;
     56public slots:
     57
     58    /** Creates the floppy disc image, asynchronously. */
     59    virtual void accept() /* override final */;
    5560
    5661protected:
    5762
    58     void retranslateUi();
     63    /** Handles translation event. */
     64    virtual void retranslateUi() /* override final */;
    5965
    6066private slots:
    6167
     68    /** Handles signal about @a comMedium was created. */
     69    void sltHandleMediumCreated(const CMedium &comMedium);
    6270
    6371private:
     72
     73    /** Floppy disc sizes. */
    6474    enum FDSize
    6575    {
     
    7080        FDSize_360K
    7181    };
    72     void                prepare();
    73     QString             getDefaultFolder() const;
    7482
    75     UIFilePathSelector *m_pFilePathselector;
    76     QLabel             *m_pPathLabel;
     83    /** Prepares all. */
     84    void prepare();
     85
     86    /** Returns default file-path. */
     87    QString getDefaultFilePath() const;
     88
     89    /** Holds the default folder. */
     90    QString  m_strDefaultFolder;
     91    /** Holds the machine name. */
     92    QString  m_strMachineName;
     93
     94    /** Holds the path label instance. */
     95    QLabel             *m_pLabelPath;
     96    /** Holds the file path selector instance. */
     97    UIFilePathSelector *m_pFilePathSelector;
     98    /** Holds the size label instance. */
    7799    QLabel             *m_pSizeLabel;
    78     QComboBox          *m_pSizeCombo;
     100    /** Holds the size combo instance. */
     101    QComboBox          *m_pComboSize;
     102    /** Holds the format check-box instance. */
     103    QCheckBox          *m_pCheckBoxFormat;
     104    /** holds the button-box instance. */
    79105    QDialogButtonBox   *m_pButtonBox;
    80     QCheckBox          *m_pFormatCheckBox;
    81     QString             m_strDefaultFolder;
    82     QString             m_strMachineName;
    83     QUuid               m_uMediumID;
     106
     107    /** Holds the created medium ID. */
     108    QUuid  m_uMediumID;
    84109};
    85110
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