VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/medium/UIFDCreationDialog.cpp@ 102493

Last change on this file since 102493 was 100896, checked in by vboxsync, 13 months ago

FE/Qt: bugref:10506: Suitable HiDPI icon sets for all dialogs using QWidget::setWindowIcon.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.0 KB
Line 
1/* $Id: UIFDCreationDialog.cpp 100896 2023-08-17 12:18:19Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIFDCreationDialog class implementation.
4 */
5
6/*
7 * Copyright (C) 2008-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28/* Qt includes */
29#include <QCheckBox>
30#include <QDir>
31#include <QGridLayout>
32#include <QLabel>
33#include <QPushButton>
34
35/* GUI includes */
36#include "QIDialogButtonBox.h"
37#include "UICommon.h"
38#include "UIFDCreationDialog.h"
39#include "UIFilePathSelector.h"
40#include "UIIconPool.h"
41#include "UIMedium.h"
42#include "UIMessageCenter.h"
43#include "UINotificationCenter.h"
44#include "UIModalWindowManager.h"
45
46/* COM includes: */
47#include "CSystemProperties.h"
48#include "CMedium.h"
49#include "CMediumFormat.h"
50
51
52UIFDCreationDialog::UIFDCreationDialog(QWidget *pParent,
53 const QString &strDefaultFolder,
54 const QString &strMachineName /* = QString() */)
55 : QIWithRetranslateUI<QDialog>(pParent)
56 , m_strDefaultFolder(strDefaultFolder)
57 , m_strMachineName(strMachineName)
58 , m_pPathLabel(0)
59 , m_pFilePathSelector(0)
60 , m_pSizeLabel(0)
61 , m_pSizeCombo(0)
62 , m_pFormatCheckBox(0)
63 , m_pButtonBox(0)
64{
65 prepare();
66}
67
68QUuid UIFDCreationDialog::mediumID() const
69{
70 return m_uMediumID;
71}
72
73/* static */
74QUuid UIFDCreationDialog::createFloppyDisk(QWidget *pParent, const QString &strDefaultFolder /* QString() */,
75 const QString &strMachineName /* = QString() */ )
76{
77 QString strStartPath(strDefaultFolder);
78
79 if (strStartPath.isEmpty())
80 strStartPath = uiCommon().defaultFolderPathForType(UIMediumDeviceType_Floppy);
81
82 QWidget *pDialogParent = windowManager().realParentWindow(pParent);
83
84 UIFDCreationDialog *pDialog = new UIFDCreationDialog(pParent, strStartPath, strMachineName);
85 if (!pDialog)
86 return QUuid();
87 windowManager().registerNewParent(pDialog, pDialogParent);
88
89 if (pDialog->exec())
90 {
91 QUuid uMediumID = pDialog->mediumID();
92 delete pDialog;
93 return uMediumID;
94 }
95 delete pDialog;
96 return QUuid();
97}
98
99void UIFDCreationDialog::accept()
100{
101 /* Make Ok button disabled first of all: */
102 m_pButtonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
103
104 /* Acquire medium path & formats: */
105 const QString strMediumLocation = m_pFilePathSelector->path();
106 const QVector<CMediumFormat> mediumFormats = UIMediumDefs::getFormatsForDeviceType(KDeviceType_Floppy);
107 /* Make sure we have both path and formats selected: */
108 if (strMediumLocation.isEmpty() || mediumFormats.isEmpty())
109 return;
110
111 /* Get VBox for further activities: */
112 CVirtualBox comVBox = uiCommon().virtualBox();
113
114 /* Create medium: */
115 CMedium comMedium = comVBox.CreateMedium(mediumFormats[0].GetName(), strMediumLocation,
116 KAccessMode_ReadWrite, KDeviceType_Floppy);
117 if (!comVBox.isOk())
118 {
119 msgCenter().cannotCreateMediumStorage(comVBox, strMediumLocation, this);
120 return;
121 }
122
123 /* Compose medium storage variants: */
124 QVector<KMediumVariant> variants(1, KMediumVariant_Fixed);
125 /* Decide if disk formatting is required: */
126 if (m_pFormatCheckBox && m_pFormatCheckBox->checkState() == Qt::Checked)
127 variants.push_back(KMediumVariant_Formatted);
128
129 /* Create medium storage, asynchronously: */
130 UINotificationProgressMediumCreate *pNotification =
131 new UINotificationProgressMediumCreate(comMedium, m_pSizeCombo->currentData().toLongLong(), variants);
132 connect(pNotification, &UINotificationProgressMediumCreate::sigMediumCreated,
133 &uiCommon(), &UICommon::sltHandleMediumCreated);
134 connect(pNotification, &UINotificationProgressMediumCreate::sigMediumCreated,
135 this, &UIFDCreationDialog::sltHandleMediumCreated);
136 gpNotificationCenter->append(pNotification);
137}
138
139void UIFDCreationDialog::retranslateUi()
140{
141 if (m_strMachineName.isEmpty())
142 setWindowTitle(QString("%1").arg(tr("Floppy Disk Creator")));
143 else
144 setWindowTitle(QString("%1 - %2").arg(m_strMachineName).arg(tr("Floppy Disk Creator")));
145 if (m_pPathLabel)
146 m_pPathLabel->setText(tr("File &Path:"));
147 if (m_pSizeLabel)
148 {
149 m_pSizeLabel->setText(tr("&Size:"));
150 m_pSizeLabel->setToolTip(tr("Sets the size of the floppy disk."));
151 }
152 if (m_pButtonBox)
153 m_pButtonBox->button(QDialogButtonBox::Ok)->setText("C&reate");
154 if (m_pFormatCheckBox)
155 {
156 m_pFormatCheckBox->setText(tr("&Format disk as FAT12"));
157 m_pFormatCheckBox->setToolTip(tr("Formats the floppy disk as FAT12."));
158 }
159 if (m_pSizeCombo)
160 {
161 m_pSizeCombo->setItemText(FDSize_2_88M, tr("2.88M"));
162 m_pSizeCombo->setItemText(FDSize_1_44M, tr("1.44M"));
163 m_pSizeCombo->setItemText(FDSize_1_2M, tr("1.2M"));
164 m_pSizeCombo->setItemText(FDSize_720K, tr("720K"));
165 m_pSizeCombo->setItemText(FDSize_360K, tr("360K"));
166 }
167
168 if (m_pButtonBox && m_pButtonBox->button(QDialogButtonBox::Ok))
169 m_pButtonBox->button(QDialogButtonBox::Ok)->setToolTip(tr("Create the disk and close this dialog."));
170
171 if (m_pButtonBox && m_pButtonBox->button(QDialogButtonBox::Cancel))
172 m_pButtonBox->button(QDialogButtonBox::Cancel)->setToolTip(tr("Cancel"));
173}
174
175void UIFDCreationDialog::sltPathChanged(const QString &strPath)
176{
177 bool fIsFileUnique = checkFilePath(strPath);
178 m_pFilePathSelector->mark(!fIsFileUnique, tr("File already exists"));
179
180 if (m_pButtonBox && m_pButtonBox->button(QDialogButtonBox::Ok))
181 m_pButtonBox->button(QDialogButtonBox::Ok)->setEnabled(fIsFileUnique);
182}
183
184bool UIFDCreationDialog::checkFilePath(const QString &strPath) const
185{
186 return !QFileInfo(strPath).exists();
187}
188
189void UIFDCreationDialog::sltHandleMediumCreated(const CMedium &comMedium)
190{
191 /* Store the ID of the newly created medium: */
192 m_uMediumID = comMedium.GetId();
193
194 /* Close the dialog now: */
195 QDialog::accept();
196}
197
198void UIFDCreationDialog::prepare()
199{
200#ifndef VBOX_WS_MAC
201 /* Assign window icon: */
202 setWindowIcon(UIIconPool::iconSetFull(":/fd_add_32px.png", ":/fd_add_16px.png"));
203#endif
204
205 setWindowModality(Qt::WindowModal);
206 setSizeGripEnabled(false);
207
208 /* Prepare main layout: */
209 QGridLayout *pLayoutMain = new QGridLayout(this);
210 if (pLayoutMain)
211 {
212 /* Prepare path label: */
213 m_pPathLabel = new QLabel(this);
214 if (m_pPathLabel)
215 {
216 m_pPathLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
217 pLayoutMain->addWidget(m_pPathLabel, 0, 0);
218 }
219 /* Prepare file path selector: */
220 m_pFilePathSelector = new UIFilePathSelector(this);
221 if (m_pFilePathSelector)
222 {
223 m_pFilePathSelector->setMode(UIFilePathSelector::Mode_File_Save);
224 const QString strFilePath = getDefaultFilePath();
225 m_pFilePathSelector->setDefaultPath(strFilePath);
226 m_pFilePathSelector->setPath(strFilePath);
227
228 pLayoutMain->addWidget(m_pFilePathSelector, 0, 1, 1, 3);
229 connect(m_pFilePathSelector, &UIFilePathSelector::pathChanged,
230 this, &UIFDCreationDialog::sltPathChanged);
231 if (m_pPathLabel)
232 m_pPathLabel->setBuddy(m_pFilePathSelector);
233 }
234
235 /* Prepare size label: */
236 m_pSizeLabel = new QLabel(this);
237 if (m_pSizeLabel)
238 {
239 m_pSizeLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
240 pLayoutMain->addWidget(m_pSizeLabel, 1, 0);
241 }
242 /* Prepare size combo: */
243 m_pSizeCombo = new QComboBox(this);
244 if (m_pSizeCombo)
245 {
246 m_pSizeCombo->insertItem(FDSize_2_88M, "2.88M", 2949120);
247 m_pSizeCombo->insertItem(FDSize_1_44M, "1.44M", 1474560);
248 m_pSizeCombo->insertItem(FDSize_1_2M, "1.2M", 1228800);
249 m_pSizeCombo->insertItem(FDSize_720K, "720K", 737280);
250 m_pSizeCombo->insertItem(FDSize_360K, "360K", 368640);
251 m_pSizeCombo->setCurrentIndex(FDSize_1_44M);
252
253 pLayoutMain->addWidget(m_pSizeCombo, 1, 1);
254
255 if (m_pSizeLabel)
256 m_pSizeLabel->setBuddy(m_pSizeCombo);
257 }
258
259 /* Prepare format check-box: */
260 m_pFormatCheckBox = new QCheckBox;
261 if (m_pFormatCheckBox)
262 {
263 m_pFormatCheckBox->setCheckState(Qt::Checked);
264 pLayoutMain->addWidget(m_pFormatCheckBox, 2, 1, 1, 2);
265 }
266
267 /* Prepare button-box: */
268 m_pButtonBox = new QIDialogButtonBox(QDialogButtonBox::Help | QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
269 if (m_pButtonBox)
270 {
271 uiCommon().setHelpKeyword(m_pButtonBox->button(QDialogButtonBox::Help), "create-floppy-disk-image");
272 connect(m_pButtonBox, &QDialogButtonBox::accepted, this, &UIFDCreationDialog::accept);
273 connect(m_pButtonBox, &QDialogButtonBox::rejected, this, &UIFDCreationDialog::reject);
274 connect(m_pButtonBox->button(QDialogButtonBox::Help), &QPushButton::pressed,
275 m_pButtonBox, &QIDialogButtonBox::sltHandleHelpRequest);
276 pLayoutMain->addWidget(m_pButtonBox, 3, 0, 1, 3);
277 }
278 }
279
280 /* Apply language settings: */
281 retranslateUi();
282
283#ifdef VBOX_WS_MAC
284 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
285 setFixedSize(minimumSize());
286#endif /* VBOX_WS_MAC */
287
288 /* Adjust dialog size: */
289 adjustSize();
290}
291
292QString UIFDCreationDialog::getDefaultFilePath() const
293{
294 /* Prepare default file-path on the basis of passerd default folder: */
295 QString strDefaultFilePath = m_strDefaultFolder;
296
297 /* Make sure it's not empty if possible: */
298 if (strDefaultFilePath.isEmpty())
299 strDefaultFilePath = uiCommon().virtualBox().GetSystemProperties().GetDefaultMachineFolder();
300 if (strDefaultFilePath.isEmpty())
301 return strDefaultFilePath;
302
303 /* Append file-path with disc name, generate unique file-name if necessary: */
304 QString strDiskname = !m_strMachineName.isEmpty() ? m_strMachineName : "NewFloppyDisk";
305 strDiskname = UICommon::findUniqueFileName(m_strDefaultFolder, strDiskname);
306
307 /* Append file-path with preferred extension finally: */
308 const QString strPreferredExtension = UIMediumDefs::getPreferredExtensionForMedium(KDeviceType_Floppy);
309 strDefaultFilePath = QDir(strDefaultFilePath).absoluteFilePath(strDiskname + "." + strPreferredExtension);
310
311 /* Return default file-path: */
312 return strDefaultFilePath;
313}
Note: See TracBrowser for help on using the repository browser.

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