VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/wizards/newvd/UIWizardNewVDPageBasic3.cpp@ 71027

Last change on this file since 71027 was 71027, checked in by vboxsync, 7 years ago

FE/Qt: big svn props cleanup

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.0 KB
Line 
1/* $Id: UIWizardNewVDPageBasic3.cpp 71027 2018-02-15 14:33:48Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIWizardNewVDPageBasic3 class implementation.
4 */
5
6/*
7 * Copyright (C) 2006-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#ifdef VBOX_WITH_PRECOMPILED_HEADERS
19# include <precomp.h>
20#else /* !VBOX_WITH_PRECOMPILED_HEADERS */
21
22/* Qt includes: */
23# include <QDir>
24# include <QRegExpValidator>
25# include <QVBoxLayout>
26# include <QHBoxLayout>
27# include <QLineEdit>
28# include <QSlider>
29# include <QLabel>
30# include <QSpacerItem>
31
32/* GUI includes: */
33# include "UIWizardNewVDPageBasic3.h"
34# include "UIWizardNewVD.h"
35# include "VBoxGlobal.h"
36# include "UIMessageCenter.h"
37# include "UIIconPool.h"
38# include "QIFileDialog.h"
39# include "QIRichTextLabel.h"
40# include "QIToolButton.h"
41# include "QILineEdit.h"
42# include "UIMediumSizeEditor.h"
43
44/* COM includes: */
45# include "CSystemProperties.h"
46# include "CMediumFormat.h"
47
48#endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
49
50
51UIWizardNewVDPage3::UIWizardNewVDPage3(const QString &strDefaultName, const QString &strDefaultPath)
52 : m_strDefaultName(strDefaultName.isEmpty() ? QString("NewVirtualDisk1") : strDefaultName)
53 , m_strDefaultPath(strDefaultPath)
54 , m_uMediumSizeMin(_4M)
55 , m_uMediumSizeMax(vboxGlobal().virtualBox().GetSystemProperties().GetInfoVDSize())
56{
57}
58
59void UIWizardNewVDPage3::onSelectLocationButtonClicked()
60{
61 /* Get current folder and filename: */
62 QFileInfo fullFilePath(mediumPath());
63 QDir folder = fullFilePath.path();
64 QString strFileName = fullFilePath.fileName();
65
66 /* Set the first parent folder that exists as the current: */
67 while (!folder.exists() && !folder.isRoot())
68 {
69 QFileInfo folderInfo(folder.absolutePath());
70 if (folder == QDir(folderInfo.absolutePath()))
71 break;
72 folder = folderInfo.absolutePath();
73 }
74
75 /* But if it doesn't exists at all: */
76 if (!folder.exists() || folder.isRoot())
77 {
78 /* Use recommended one folder: */
79 QFileInfo defaultFilePath(absoluteFilePath(strFileName, m_strDefaultPath));
80 folder = defaultFilePath.path();
81 }
82
83 /* Prepare backends list: */
84 QVector<QString> fileExtensions;
85 QVector<KDeviceType> deviceTypes;
86 CMediumFormat mediumFormat = fieldImp("mediumFormat").value<CMediumFormat>();
87 mediumFormat.DescribeFileExtensions(fileExtensions, deviceTypes);
88 QStringList validExtensionList;
89 for (int i = 0; i < fileExtensions.size(); ++i)
90 if (deviceTypes[i] == KDeviceType_HardDisk)
91 validExtensionList << QString("*.%1").arg(fileExtensions[i]);
92 /* Compose full filter list: */
93 QString strBackendsList = QString("%1 (%2)").arg(mediumFormat.GetName()).arg(validExtensionList.join(" "));
94
95 /* Open corresponding file-dialog: */
96 QString strChosenFilePath = QIFileDialog::getSaveFileName(folder.absoluteFilePath(strFileName),
97 strBackendsList, thisImp(),
98 VBoxGlobal::tr("Please choose a location for new virtual hard disk file"));
99
100 /* If there was something really chosen: */
101 if (!strChosenFilePath.isEmpty())
102 {
103 /* If valid file extension is missed, append it: */
104 if (QFileInfo(strChosenFilePath).suffix().isEmpty())
105 strChosenFilePath += QString(".%1").arg(m_strDefaultExtension);
106 m_pLocationEditor->setText(QDir::toNativeSeparators(strChosenFilePath));
107 m_pLocationEditor->selectAll();
108 m_pLocationEditor->setFocus();
109 }
110}
111
112/* static */
113QString UIWizardNewVDPage3::toFileName(const QString &strName, const QString &strExtension)
114{
115 /* Convert passed name to native separators (it can be full, actually): */
116 QString strFileName = QDir::toNativeSeparators(strName);
117
118 /* Remove all trailing dots to avoid multiple dots before extension: */
119 int iLen;
120 while (iLen = strFileName.length(), iLen > 0 && strFileName[iLen - 1] == '.')
121 strFileName.truncate(iLen - 1);
122
123 /* Add passed extension if its not done yet: */
124 if (QFileInfo(strFileName).suffix().toLower() != strExtension)
125 strFileName += QString(".%1").arg(strExtension);
126
127 /* Return result: */
128 return strFileName;
129}
130
131/* static */
132QString UIWizardNewVDPage3::absoluteFilePath(const QString &strFileName, const QString &strDefaultPath)
133{
134 /* Wrap file-info around received file name: */
135 QFileInfo fileInfo(strFileName);
136 /* If path-info is relative or there is no path-info at all: */
137 if (fileInfo.fileName() == strFileName || fileInfo.isRelative())
138 {
139 /* Resolve path on the basis of default path we have: */
140 fileInfo = QFileInfo(strDefaultPath, strFileName);
141 }
142 /* Return full absolute hard disk file path: */
143 return QDir::toNativeSeparators(fileInfo.absoluteFilePath());
144}
145
146/* static */
147QString UIWizardNewVDPage3::defaultExtension(const CMediumFormat &mediumFormatRef)
148{
149 /* Load extension / device list: */
150 QVector<QString> fileExtensions;
151 QVector<KDeviceType> deviceTypes;
152 CMediumFormat mediumFormat(mediumFormatRef);
153 mediumFormat.DescribeFileExtensions(fileExtensions, deviceTypes);
154 for (int i = 0; i < fileExtensions.size(); ++i)
155 if (deviceTypes[i] == KDeviceType_HardDisk)
156 return fileExtensions[i].toLower();
157 AssertMsgFailed(("Extension can't be NULL!\n"));
158 return QString();
159}
160
161QString UIWizardNewVDPage3::mediumPath() const
162{
163 return absoluteFilePath(toFileName(m_pLocationEditor->text(), m_strDefaultExtension), m_strDefaultPath);
164}
165
166qulonglong UIWizardNewVDPage3::mediumSize() const
167{
168 return m_pEditorSize->mediumSize();
169}
170
171void UIWizardNewVDPage3::setMediumSize(qulonglong uMediumSize)
172{
173 m_pEditorSize->setMediumSize(uMediumSize);
174}
175
176UIWizardNewVDPageBasic3::UIWizardNewVDPageBasic3(const QString &strDefaultName, const QString &strDefaultPath, qulonglong uDefaultSize)
177 : UIWizardNewVDPage3(strDefaultName, strDefaultPath)
178{
179 /* Create widgets: */
180 QVBoxLayout *pMainLayout = new QVBoxLayout(this);
181 {
182 m_pLocationLabel = new QIRichTextLabel(this);
183 QHBoxLayout *pLocationLayout = new QHBoxLayout;
184 {
185 m_pLocationEditor = new QLineEdit(this);
186 m_pLocationOpenButton = new QIToolButton(this);
187 {
188 m_pLocationOpenButton->setAutoRaise(true);
189 m_pLocationOpenButton->setIcon(UIIconPool::iconSet(":/select_file_16px.png", "select_file_disabled_16px.png"));
190 }
191 pLocationLayout->addWidget(m_pLocationEditor);
192 pLocationLayout->addWidget(m_pLocationOpenButton);
193 }
194 m_pSizeLabel = new QIRichTextLabel(this);
195 m_pEditorSize = new UIMediumSizeEditor;
196 setMediumSize(uDefaultSize);
197 pMainLayout->addWidget(m_pLocationLabel);
198 pMainLayout->addLayout(pLocationLayout);
199 pMainLayout->addWidget(m_pSizeLabel);
200 pMainLayout->addWidget(m_pEditorSize);
201 pMainLayout->addStretch();
202 }
203
204 /* Setup connections: */
205 connect(m_pLocationEditor, &QLineEdit::textChanged, this, &UIWizardNewVDPageBasic3::completeChanged);
206 connect(m_pLocationOpenButton, &QIToolButton::clicked, this, &UIWizardNewVDPageBasic3::sltSelectLocationButtonClicked);
207 connect(m_pEditorSize, &UIMediumSizeEditor::sigSizeChanged, this, &UIWizardNewVDPageBasic3::completeChanged);
208
209 /* Register fields: */
210 registerField("mediumPath", this, "mediumPath");
211 registerField("mediumSize", this, "mediumSize");
212}
213
214void UIWizardNewVDPageBasic3::sltSelectLocationButtonClicked()
215{
216 /* Call to base-class: */
217 onSelectLocationButtonClicked();
218}
219
220void UIWizardNewVDPageBasic3::retranslateUi()
221{
222 /* Translate page: */
223 setTitle(UIWizardNewVD::tr("File location and size"));
224
225 /* Translate widgets: */
226 m_pLocationLabel->setText(UIWizardNewVD::tr("Please type the name of the new virtual hard disk file into the box below or "
227 "click on the folder icon to select a different folder to create the file in."));
228 m_pLocationOpenButton->setToolTip(UIWizardNewVD::tr("Choose a location for new virtual hard disk file..."));
229 m_pSizeLabel->setText(UIWizardNewVD::tr("Select the size of the virtual hard disk in megabytes. "
230 "This size is the limit on the amount of file data "
231 "that a virtual machine will be able to store on the hard disk."));
232}
233
234void UIWizardNewVDPageBasic3::initializePage()
235{
236 /* Translate page: */
237 retranslateUi();
238
239 /* Get default extension for new virtual-disk: */
240 m_strDefaultExtension = defaultExtension(field("mediumFormat").value<CMediumFormat>());
241 /* Set default name as text for location editor: */
242 m_pLocationEditor->setText(m_strDefaultName);
243}
244
245bool UIWizardNewVDPageBasic3::isComplete() const
246{
247 /* Make sure current name is not empty and current size feats the bounds: */
248 return !m_pLocationEditor->text().trimmed().isEmpty() &&
249 mediumSize() >= m_uMediumSizeMin && mediumSize() <= m_uMediumSizeMax;
250}
251
252bool UIWizardNewVDPageBasic3::validatePage()
253{
254 /* Initial result: */
255 bool fResult = true;
256
257 /* Make sure such file doesn't exists already: */
258 QString strMediumPath(mediumPath());
259 fResult = !QFileInfo(strMediumPath).exists();
260 if (!fResult)
261 msgCenter().cannotOverwriteHardDiskStorage(strMediumPath, this);
262
263 if (fResult)
264 {
265 /* Lock finish button: */
266 startProcessing();
267
268 /* Try to create virtual hard drive file: */
269 fResult = qobject_cast<UIWizardNewVD*>(wizard())->createVirtualDisk();
270
271 /* Unlock finish button: */
272 endProcessing();
273 }
274
275 /* Return result: */
276 return fResult;
277}
278
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