VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/ui/VBoxNewHDWzd.ui.h@ 132

Last change on this file since 132 was 132, checked in by vboxsync, 18 years ago

Main:

  • Prototyped IConsoleCallback::onRuntimeError();
  • All size parameters in IHardDisk are now ULONG64.

Frontends:

  • Updated according to the above.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.3 KB
Line 
1/**
2 *
3 * VBox frontends: Qt GUI ("VirtualBox"):
4 * "New hard disk" wizard UI include (Qt Designer)
5 */
6
7/*
8 * Copyright (C) 2006 InnoTek Systemberatung GmbH
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License as published by the Free Software Foundation,
14 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
15 * distribution. VirtualBox OSE is distributed in the hope that it will
16 * be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * If you received this file as part of a commercial VirtualBox
19 * distribution, then only the terms of your commercial VirtualBox
20 * license agreement apply instead of the previous paragraph.
21 */
22
23/****************************************************************************
24** ui.h extension file, included from the uic-generated form implementation.
25**
26** If you want to add, delete, or rename functions or slots, use
27** Qt Designer to update this file, preserving your code.
28**
29** You should not define a constructor or destructor in this file.
30** Instead, write your code in functions called init() and destroy().
31** These will automatically be called by the form's constructor and
32** destructor.
33*****************************************************************************/
34
35static const int MinVDISize = 4;
36
37/**
38 * Composes a file name from the given relative or full file name
39 * based on the home directory and the default VDI directory.
40 */
41static QString composeFullFileName (const QString file)
42{
43 CVirtualBox vbox = vboxGlobal().virtualBox();
44 QString homeFolder = vbox.GetHomeFolder();
45 QString defaultFolder = vbox.GetSystemProperties().GetDefaultVDIFolder();
46
47 QFileInfo fi = QFileInfo (file);
48 if (fi.fileName() == file)
49 {
50 // no path info at all, use defaultFolder
51 fi = QFileInfo (defaultFolder, file);
52 }
53 else if (fi.isRelative())
54 {
55 // resolve relatively to homeFolder
56 fi = QFileInfo (homeFolder, file);
57 }
58
59 return QDir::convertSeparators (fi.absFilePath());
60}
61
62
63class QISizeValidator : public QValidator
64{
65public:
66 QISizeValidator (QObject * aParent, ULONG64 aMinSize, ULONG64 aMaxSize,
67 const char * aName = 0) :
68 QValidator (aParent, aName), mMinSize(aMinSize), mMaxSize(aMaxSize) {}
69
70 ~QISizeValidator() {}
71
72 State validate (QString &input, int &/*pos*/) const
73 {
74 QRegExp regexp ("^([^M^G^T^P^\\d\\s]*)(\\d+(([^\\s^\\d^M^G^T^P]+)(\\d*))?)?(\\s*)([MGTP]B?)?$");
75 int position = regexp.search (input);
76 if (position == -1)
77 return Invalid;
78 else
79 {
80 if (!regexp.cap (1).isEmpty() ||
81 regexp.cap (4).length() > 1 ||
82 regexp.cap (5).length() > 2 ||
83 regexp.cap (6).length() > 1)
84 return Invalid;
85
86 if (regexp.cap (7).length() == 1)
87 return Intermediate;
88
89 QString size = regexp.cap (2);
90 if (size.isEmpty())
91 return Intermediate;
92
93 bool result = false;
94 bool warning = false;
95 if (!regexp.cap (4).isEmpty() && regexp.cap (5).isEmpty())
96 {
97 size += "0";
98 warning = true;
99 }
100 QLocale::system().toDouble (size, &result);
101
102 ULONG64 sizeMB = vboxGlobal().parseSize (input) / _1M;
103 if (sizeMB > mMaxSize || sizeMB < mMinSize)
104 warning = true;
105
106 if (result)
107 return warning ? Intermediate : Acceptable;
108 else
109 return Invalid;
110 }
111 }
112
113protected:
114 ULONG64 mMinSize;
115 ULONG64 mMaxSize;
116};
117
118
119void VBoxNewHDWzd::init()
120{
121 // disable help buttons
122 helpButton()->setShown( false );
123
124 // fix tab order to get the proper direction
125 // (originally the focus goes Next/Finish -> Back -> Cancel -> page)
126 QWidget::setTabOrder( backButton(), nextButton() );
127 QWidget::setTabOrder( nextButton(), finishButton() );
128 QWidget::setTabOrder( finishButton(), cancelButton() );
129
130 // setup connections and set validation for pages
131 // ----------------------------------------------------------------------
132
133 // Image type page
134
135 // Name and Size page
136
137 CSystemProperties sysProps = vboxGlobal().virtualBox().GetSystemProperties();
138
139 MaxVDISize = sysProps.GetMaxVDISize();
140
141 leName->setValidator (new QRegExpValidator (QRegExp( ".+" ), this));
142
143 leSize->setValidator (new QISizeValidator (this, MinVDISize, MaxVDISize));
144 leSize->setAlignment (Qt::AlignRight);
145
146 wvalNameAndSize = new QIWidgetValidator (pageNameAndSize, this );
147 connect(
148 wvalNameAndSize, SIGNAL( validityChanged (const QIWidgetValidator *) ),
149 this, SLOT( enableNext (const QIWidgetValidator *) )
150 );
151
152 // Summary page
153
154 // filter out Enter keys in order to direct them to the default dlg button
155 QIKeyFilter *ef = new QIKeyFilter (this, Key_Enter);
156 ef->watchOn (teSummary) ;
157
158 // set initial values
159 // ----------------------------------------------------------------------
160
161 // Image type page
162
163 // Name and Size page
164
165 static ulong HDNumber = 0;
166 leName->setText (QString ("NewHardDisk%1.vdi").arg (++ HDNumber));
167
168 sliderStep = 50;
169 slSize->setFocusPolicy (QWidget::StrongFocus);
170 slSize->setPageStep (sliderStep);
171 slSize->setLineStep (sliderStep);
172 slSize->setTickInterval (0);
173 slSize->setMinValue ((int)(log10 ((double)MinVDISize) / log10 (2.0) * sliderStep));
174 slSize->setMaxValue ((int)(log10 ((double)(MaxVDISize + 1)) / log10 (2.0) * sliderStep));
175 txSizeMin->setText (vboxGlobal().formatSize (MinVDISize * _1M));
176 txSizeMax->setText (vboxGlobal().formatSize (MaxVDISize * _1M));
177 // initial HD size value = pow(2.0, 11) = 2048 MB
178 currentSize = 2 * _1K;
179 leSize->setText (vboxGlobal().formatSize (currentSize * _1M));
180 slSize->setValue ((int)(log10 ((double)currentSize) / log10 (2.0) * sliderStep));
181 // limit the max. size of QLineEdit (STUPID Qt has NO correct means for that)
182 leSize->setMaximumSize(
183 leSize->fontMetrics().width ("88888.88 MB") + leSize->frameWidth() * 2,
184 leSize->height()
185 );
186
187 // Summary page
188
189 teSummary->setPaper (pageSummary->backgroundBrush());
190
191 // update the next button state for pages with validation
192 // (validityChanged() connected to enableNext() will do the job)
193 wvalNameAndSize->revalidate();
194
195 // the finish button on the Summary page is always enabled
196 setFinishEnabled (pageSummary, true);
197}
198
199
200void VBoxNewHDWzd::setRecommendedFileName (const QString &aName)
201{
202 leName->setText (aName);
203}
204
205
206void VBoxNewHDWzd::setRecommendedSize (ulong aSize)
207{
208 AssertReturn (aSize >= (ulong) MinVDISize &&
209 aSize <= (ulong) MaxVDISize, (void) 0);
210 currentSize = aSize;
211 slSize->setValue ((int)(log10 ((double)currentSize) / log10 (2.0) * sliderStep));
212 leSize->setText (vboxGlobal().formatSize(aSize * _1M));
213}
214
215
216QString VBoxNewHDWzd::imageFileName()
217{
218 QString name = QDir::convertSeparators (leName->text());
219 if (QFileInfo (name).extension().isEmpty())
220 name += ".vdi";
221 return name;
222}
223
224
225Q_UINT64 VBoxNewHDWzd::imageSize()
226{
227 return currentSize;
228}
229
230
231bool VBoxNewHDWzd::isDynamicImage()
232{
233 return rbDynamicType->isOn();
234}
235
236
237void VBoxNewHDWzd::enableNext( const QIWidgetValidator *wval )
238{
239 setNextEnabled (wval->widget(), wval->isValid());
240}
241
242
243void VBoxNewHDWzd::revalidate( QIWidgetValidator * /*wval*/ )
244{
245 // do individual validations for pages
246
247// template:
248// QWidget *pg = wval->widget();
249// bool valid = wval->isOtherValid();
250//
251// if ( pg == pageXXX ) {
252// valid = XXX;
253// }
254//
255// wval->setOtherValid( valid );
256}
257
258
259void VBoxNewHDWzd::showPage( QWidget *page )
260{
261 if (currentPage() == pageNameAndSize)
262 {
263 if (QFileInfo (imageFileName()).exists())
264 {
265 vboxProblem().sayCannotOverwriteHardDiskImage (this, imageFileName());
266 return;
267 }
268 }
269
270 if (page == pageSummary)
271 {
272 QString type = rbDynamicType->isOn() ? rbDynamicType->text()
273 : rbFixedType->text();
274 type.remove ('&');
275
276 // compose summary
277 QString summary = QString (tr(
278 "<table>"
279 "<tr><td>Type:</td><td>%1</td></tr>"
280 "<tr><td>Location:</td><td>%2</td></tr>"
281 "<tr><td>Size:</td><td>%3&nbsp;MB</td></tr>"
282 "</table>"
283 ))
284 .arg (type)
285 .arg (composeFullFileName (imageFileName()))
286 .arg (imageSize());
287 teSummary->setText (summary);
288 // set Finish to default
289 finishButton()->setDefault (true);
290 }
291 else
292 {
293 // always set Next to default
294 nextButton()->setDefault (true);
295 }
296
297 QWizard::showPage (page);
298
299 // fix focus on the last page. when we go to the last page
300 // having the Next in focus the focus goes to the Cancel
301 // button because when the Next hides Finish is not yet shown.
302 if (page == pageSummary && focusWidget() == cancelButton())
303 finishButton()->setFocus();
304
305 // setup focus for individual pages
306 if (page == pageType) {
307 bgType->setFocus();
308 } else if (page == pageNameAndSize) {
309 leName->setFocus();
310 } else if (page == pageSummary) {
311 teSummary->setFocus();
312 }
313}
314
315void VBoxNewHDWzd::accept()
316{
317 /*
318 * Try to create the hard disk when the Finish button is pressed.
319 * On failure, the wisard will remain open to give it another try.
320 */
321 if (createHardDisk())
322 QWizard::accept();
323}
324
325/**
326 * Performs steps necessary to create a hard disk. This method handles all
327 * errors and shows the corresponding messages when appropriate.
328 *
329 * @return wheter the creation was successful or not
330 */
331bool VBoxNewHDWzd::createHardDisk()
332{
333 QString src = imageFileName();
334 Q_UINT64 size = imageSize();
335
336 AssertReturn (!src.isEmpty(), false);
337 AssertReturn (size > 0, false);
338
339 CVirtualBox vbox = vboxGlobal().virtualBox();
340
341 CProgress progress;
342 CHardDisk hd = vbox.CreateHardDisk (CEnums::VirtualDiskImage);
343
344 /// @todo (dmik) later, change wrappers so that converting
345 // to CUnknown is not necessary for cross-assignments
346 CVirtualDiskImage vdi = CUnknown (hd);
347
348 if (!vbox.isOk())
349 {
350 vboxProblem().cannotCreateHardDiskImage (this,
351 vbox, src, vdi, progress);
352 return false;
353 }
354
355 vdi.SetFilePath (src);
356
357 if (isDynamicImage())
358 progress = vdi.CreateDynamicImage (size);
359 else
360 progress = vdi.CreateFixedImage (size);
361
362 if (!vdi.isOk())
363 {
364 vboxProblem().cannotCreateHardDiskImage (this,
365 vbox, src, vdi, progress);
366 return false;
367 }
368
369 vboxProblem().showModalProgressDialog (progress, caption(), parentWidget());
370
371 if (progress.GetResultCode() != 0)
372 {
373 vboxProblem().cannotCreateHardDiskImage (this,
374 vbox, src, vdi, progress);
375 return false;
376 }
377
378 vbox.RegisterHardDisk (hd);
379 if (!vbox.isOk())
380 {
381 vboxProblem().cannotRegisterMedia (this, vbox, VBoxDefs::HD,
382 vdi.GetFilePath());
383 /* delete the image file on failure */
384 vdi.DeleteImage();
385 return false;
386 }
387
388 chd = hd;
389 return true;
390}
391
392
393void VBoxNewHDWzd::slSize_valueChanged( int val )
394{
395 if (focusWidget() == slSize)
396 {
397 currentSize = (ULONG64)(pow (2.0, ((double)val / sliderStep)));
398 leSize->setText (vboxGlobal().formatSize (currentSize * _1M));
399 }
400}
401
402
403void VBoxNewHDWzd::leSize_textChanged( const QString &text )
404{
405 if (focusWidget() == leSize)
406 {
407 currentSize = vboxGlobal().parseSize (text) / _1M;
408 slSize->setValue ((int)(log10 ((double)currentSize) / log10 (2.0) * sliderStep));
409 }
410}
411
412
413void VBoxNewHDWzd::tbNameSelect_clicked()
414{
415 // set the first parent directory that exists as the current
416 QFileInfo fld (composeFullFileName (leName->text()));
417 do
418 {
419 QString dp = fld.dirPath (false);
420 fld = QFileInfo (dp);
421 }
422 while (!fld.exists() && !QDir (fld.absFilePath()).isRoot());
423
424 if (!fld.exists())
425 {
426 CVirtualBox vbox = vboxGlobal().virtualBox();
427 fld = QFileInfo (vbox.GetSystemProperties().GetDefaultVDIFolder());
428 if (!fld.exists())
429 fld = vbox.GetHomeFolder();
430 }
431
432// QFileDialog fd (this, "NewDiskImageDialog", TRUE);
433// fd.setMode (QFileDialog::AnyFile);
434// fd.setViewMode (QFileDialog::List);
435// fd.addFilter (tr( "Hard disk images (*.vdi)" ));
436// fd.setCaption (tr( "Select a file for the new hard disk image file" ));
437// fd.setDir (d);
438
439 QString selected = QFileDialog::getSaveFileName (
440 fld.absFilePath(),
441 tr ("Hard disk images (*.vdi)"),
442 this,
443 "NewDiskImageDialog",
444 tr ("Select a file for the new hard disk image file"));
445
446// if ( fd.exec() == QDialog::Accepted ) {
447// leName->setText (QDir::convertSeparators (fd.selectedFile()));
448 if (selected)
449 {
450 if (QFileInfo (selected).extension().isEmpty())
451 selected += ".vdi";
452 leName->setText (QDir::convertSeparators (selected));
453 leName->selectAll();
454 leName->setFocus();
455 }
456}
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