VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/wizards/UINativeWizard.cpp

Last change on this file was 104358, checked in by vboxsync, 4 weeks ago

FE/Qt. bugref:10622. More refactoring around the retranslation functionality.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 30.1 KB
Line 
1/* $Id: UINativeWizard.cpp 104358 2024-04-18 05:33:40Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UINativeWizard class implementation.
4 */
5
6/*
7 * Copyright (C) 2009-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 <QApplication>
30#include <QHBoxLayout>
31#include <QLabel>
32#include <QPainter>
33#include <QPushButton>
34#include <QStackedWidget>
35#include <QStyle>
36#include <QVBoxLayout>
37#include <QWindow>
38
39/* GUI includes: */
40#include "QIRichTextLabel.h"
41#include "UICommon.h"
42#include "UIDesktopWidgetWatchdog.h"
43#include "UIExtraDataManager.h"
44#include "UIHelpBrowserDialog.h"
45#include "UIIconPool.h"
46#include "UINativeWizard.h"
47#include "UINativeWizardPage.h"
48#include "UINotificationCenter.h"
49#include "UIShortcutPool.h"
50#include "UITranslationEventListener.h"
51
52#ifdef VBOX_WS_MAC
53UIFrame::UIFrame(QWidget *pParent)
54 : QWidget(pParent)
55{
56}
57
58void UIFrame::paintEvent(QPaintEvent *pEvent)
59{
60 /* Sanity check: */
61 AssertPtrReturnVoid(pEvent);
62
63 /* Prepare painter: */
64 QPainter painter(this);
65
66 /* Limit painting with incoming rectangle: */
67 painter.setClipRect(pEvent->rect());
68
69 /* Check whether we should use Active or Inactive palette: */
70 const bool fActive = parentWidget() && parentWidget()->isActiveWindow();
71
72 /* Paint background: */
73 QColor backgroundColor = QGuiApplication::palette().color(fActive ? QPalette::Active : QPalette::Inactive, QPalette::Window);
74 backgroundColor.setAlpha(100);
75 painter.setPen(backgroundColor);
76 painter.setBrush(backgroundColor);
77 painter.drawRect(rect());
78
79 /* Paint borders: */
80 painter.setPen(QGuiApplication::palette().color(fActive ? QPalette::Active : QPalette::Inactive, QPalette::Window).darker(130));
81 QLine line1(0, 0, rect().width() - 1, 0);
82 QLine line2(rect().width() - 1, 0, rect().width() - 1, rect().height() - 1);
83 QLine line3(rect().width() - 1, rect().height() - 1, 0, rect().height() - 1);
84 QLine line4(0, rect().height() - 1, 0, 0);
85 painter.drawLine(line1);
86 painter.drawLine(line2);
87 painter.drawLine(line3);
88 painter.drawLine(line4);
89}
90#endif /* VBOX_WS_MAC */
91
92
93UINativeWizard::UINativeWizard(QWidget *pParent,
94 WizardType enmType,
95 WizardMode enmMode /* = WizardMode_Auto */,
96 const QString &strHelpKeyword /* = QString() */)
97 : QDialog(pParent, Qt::Window)
98 , m_enmType(enmType)
99 , m_enmMode(enmMode == WizardMode_Auto ? gEDataManager->modeForWizardType(m_enmType) : enmMode)
100 , m_strHelpKeyword(strHelpKeyword)
101 , m_iLastIndex(-1)
102 , m_fClosed(false)
103 , m_pLabelPixmap(0)
104 , m_pLayoutRight(0)
105 , m_pLabelPageTitle(0)
106 , m_pWidgetStack(0)
107 , m_pNotificationCenter(0)
108{
109 prepare();
110}
111
112UINativeWizard::~UINativeWizard()
113{
114 cleanup();
115}
116
117UINotificationCenter *UINativeWizard::notificationCenter() const
118{
119 return m_pNotificationCenter;
120}
121
122bool UINativeWizard::handleNotificationProgressNow(UINotificationProgress *pProgress)
123{
124 wizardButton(WizardButtonType_Expert)->setEnabled(false);
125 const bool fResult = m_pNotificationCenter->handleNow(pProgress);
126 wizardButton(WizardButtonType_Expert)->setEnabled(true);
127 return fResult;
128}
129
130QPushButton *UINativeWizard::wizardButton(const WizardButtonType &enmType) const
131{
132 return m_buttons.value(enmType);
133}
134
135int UINativeWizard::exec()
136{
137 /* Init wizard: */
138 init();
139
140 /* Call to base-class: */
141 return QDialog::exec();
142}
143
144void UINativeWizard::show()
145{
146 /* Init wizard: */
147 init();
148
149 /* Call to base-class: */
150 return QDialog::show();
151}
152
153void UINativeWizard::setPixmapName(const QString &strName)
154{
155 m_strPixmapName = strName;
156}
157
158bool UINativeWizard::isPageVisible(int iIndex) const
159{
160 return !m_invisiblePages.contains(iIndex);
161}
162
163void UINativeWizard::setPageVisible(int iIndex, bool fVisible)
164{
165 AssertMsgReturnVoid(iIndex || fVisible, ("Can't hide 1st wizard page!\n"));
166 if (fVisible)
167 m_invisiblePages.remove(iIndex);
168 else
169 m_invisiblePages.insert(iIndex);
170 /* Update the button labels since the last visible page might have changed. Thus 'Next' <-> 'Finish' might be needed: */
171 sltRetranslateUI();
172}
173
174int UINativeWizard::addPage(UINativeWizardPage *pPage)
175{
176 /* Sanity check: */
177 AssertPtrReturn(pPage, -1);
178 AssertPtrReturn(pPage->layout(), -1);
179
180 /* Adjust page layout: */
181 const int iL = 0;
182 const int iT = 0;
183 const int iR = qApp->style()->pixelMetric(QStyle::PM_LayoutRightMargin);
184 const int iB = qApp->style()->pixelMetric(QStyle::PM_LayoutBottomMargin);
185 pPage->layout()->setContentsMargins(iL, iT, iR, iB);
186
187 /* Add page to wizard's stack: */
188 m_pWidgetStack->blockSignals(true);
189 const int iIndex = m_pWidgetStack->addWidget(pPage);
190 m_pWidgetStack->blockSignals(false);
191
192 /* Make sure wizard is aware of page validity changes: */
193 connect(pPage, &UINativeWizardPage::completeChanged,
194 this, &UINativeWizard::sltCompleteChanged);
195
196 /* Returns added page index: */
197 return iIndex;
198}
199
200void UINativeWizard::sltRetranslateUI()
201{
202 /* Translate Help button: */
203 QPushButton *pButtonHelp = wizardButton(WizardButtonType_Help);
204 if (pButtonHelp)
205 {
206 pButtonHelp->setText(tr("&Help"));
207 pButtonHelp->setToolTip(tr("Open corresponding Help topic."));
208 }
209
210 /* Translate basic/expert button: */
211 QPushButton *pButtonExpert = wizardButton(WizardButtonType_Expert);
212 AssertMsgReturnVoid(pButtonExpert, ("No Expert wizard button found!\n"));
213 switch (m_enmMode)
214 {
215 case WizardMode_Basic:
216 pButtonExpert->setText(tr("&Expert Mode"));
217 pButtonExpert->setToolTip(tr("Switch to the Expert Mode, "
218 "a one-page dialog for experienced users."));
219 break;
220 case WizardMode_Expert:
221 pButtonExpert->setText(tr("&Guided Mode"));
222 pButtonExpert->setToolTip(tr("Switch to the Guided Mode, "
223 "a step-by-step dialog with detailed explanations."));
224 break;
225 default:
226 AssertMsgFailed(("Invalid wizard mode: %d", m_enmMode));
227 break;
228 }
229
230 /* Translate Back button: */
231 QPushButton *pButtonBack = wizardButton(WizardButtonType_Back);
232 AssertMsgReturnVoid(pButtonBack, ("No Back wizard button found!\n"));
233 pButtonBack->setText(tr("&Back"));
234 pButtonBack->setToolTip(tr("Go to previous wizard page."));
235
236 /* Translate Next button: */
237 QPushButton *pButtonNext = wizardButton(WizardButtonType_Next);
238 AssertMsgReturnVoid(pButtonNext, ("No Next wizard button found!\n"));
239 if (!isLastVisiblePage(m_pWidgetStack->currentIndex()))
240 {
241 pButtonNext->setText(tr("&Next"));
242 pButtonNext->setToolTip(tr("Go to next wizard page."));
243 }
244 else
245 {
246 pButtonNext->setText(tr("&Finish"));
247 pButtonNext->setToolTip(tr("Commit all wizard data."));
248 }
249
250 /* Translate Cancel button: */
251 QPushButton *pButtonCancel = wizardButton(WizardButtonType_Cancel);
252 AssertMsgReturnVoid(pButtonCancel, ("No Cancel wizard button found!\n"));
253 pButtonCancel->setText(tr("&Cancel"));
254 pButtonCancel->setToolTip(tr("Cancel wizard execution."));
255}
256
257void UINativeWizard::keyPressEvent(QKeyEvent *pEvent)
258{
259 /* Different handling depending on current modality: */
260 const Qt::WindowModality enmModality = windowHandle()->modality();
261
262 /* For non-modal case: */
263 if (enmModality == Qt::NonModal)
264 {
265 /* Special pre-processing for some keys: */
266 switch (pEvent->key())
267 {
268 case Qt::Key_Escape:
269 {
270 close();
271 return;
272 }
273 default:
274 break;
275 }
276 }
277
278 /* Call to base-class: */
279 return QDialog::keyPressEvent(pEvent);
280}
281
282void UINativeWizard::closeEvent(QCloseEvent *pEvent)
283{
284 /* Different handling depending on current modality: */
285 const Qt::WindowModality enmModality = windowHandle()->modality();
286
287 /* For non-modal case: */
288 if (enmModality == Qt::NonModal)
289 {
290 /* Ignore event initially: */
291 pEvent->ignore();
292
293 /* Let the notification-center abort blocking operations: */
294 if (m_pNotificationCenter->hasOperationsPending())
295 m_pNotificationCenter->abortOperations();
296 else
297 /* Tell the listener to close us (once): */
298 if (!m_fClosed)
299 {
300 m_fClosed = true;
301 emit sigClose(m_enmType);
302 }
303
304 return;
305 }
306
307 /* Call to base-class: */
308 QDialog::closeEvent(pEvent);
309}
310
311void UINativeWizard::sltCurrentIndexChanged(int iIndex /* = -1 */)
312{
313 /* Update translation: */
314 sltRetranslateUI();
315
316 /* Sanity check: */
317 AssertPtrReturnVoid(m_pWidgetStack);
318
319 /* -1 means current one page: */
320 if (iIndex == -1)
321 iIndex = m_pWidgetStack->currentIndex();
322
323 /* Hide/show Expert button (hidden by default): */
324 bool fIsExpertButtonAvailable = false;
325 /* Show Expert button for 1st page: */
326 if (iIndex == 0)
327 fIsExpertButtonAvailable = true;
328 /* Hide/show Expert button finally: */
329 QPushButton *pButtonExpert = wizardButton(WizardButtonType_Expert);
330 AssertMsgReturnVoid(pButtonExpert, ("No Expert wizard button found!\n"));
331 pButtonExpert->setVisible(fIsExpertButtonAvailable);
332
333 /* Disable/enable Back button: */
334 QPushButton *pButtonBack = wizardButton(WizardButtonType_Back);
335 AssertMsgReturnVoid(pButtonBack, ("No Back wizard button found!\n"));
336 pButtonBack->setEnabled(iIndex > 0);
337
338 /* Initialize corresponding page: */
339 UINativeWizardPage *pPage = qobject_cast<UINativeWizardPage*>(m_pWidgetStack->widget(iIndex));
340 AssertPtrReturnVoid(pPage);
341 m_pLabelPageTitle->setText(pPage->title());
342 if (iIndex > m_iLastIndex)
343 pPage->initializePage();
344
345 /* Disable/enable Next button: */
346 QPushButton *pButtonNext = wizardButton(WizardButtonType_Next);
347 AssertMsgReturnVoid(pButtonNext, ("No Next wizard button found!\n"));
348 pButtonNext->setEnabled(pPage->isComplete());
349
350 /* Update last index: */
351 m_iLastIndex = iIndex;
352}
353
354void UINativeWizard::sltCompleteChanged()
355{
356 /* Make sure sender is current widget: */
357 QWidget *pSender = qobject_cast<QWidget*>(sender());
358 if (pSender != m_pWidgetStack->currentWidget())
359 return;
360
361 /* Allow Next button only if current page is complete: */
362 UINativeWizardPage *pPage = qobject_cast<UINativeWizardPage*>(pSender);
363 QPushButton *pButtonNext = wizardButton(WizardButtonType_Next);
364 AssertMsgReturnVoid(pButtonNext, ("No Next wizard button found!\n"));
365 pButtonNext->setEnabled(pPage->isComplete());
366}
367
368void UINativeWizard::sltExpert()
369{
370 /* Toggle mode: */
371 switch (m_enmMode)
372 {
373 case WizardMode_Basic: m_enmMode = WizardMode_Expert; break;
374 case WizardMode_Expert: m_enmMode = WizardMode_Basic; break;
375 default: AssertMsgFailed(("Invalid mode: %d", m_enmMode)); break;
376 }
377 gEDataManager->setModeForWizardType(m_enmType, m_enmMode);
378
379 /* Reinit everything: */
380 deinit();
381 init();
382}
383
384void UINativeWizard::sltPrevious()
385{
386 /* For all allowed pages besides the 1st one we going backward: */
387 bool fPreviousFound = false;
388 int iIteratedIndex = m_pWidgetStack->currentIndex();
389 while (!fPreviousFound && iIteratedIndex > 0)
390 if (isPageVisible(--iIteratedIndex))
391 fPreviousFound = true;
392 if (fPreviousFound)
393 m_pWidgetStack->setCurrentIndex(iIteratedIndex);
394}
395
396void UINativeWizard::sltNext()
397{
398 /* Look for Next button: */
399 QPushButton *pButtonNext = wizardButton(WizardButtonType_Next);
400 AssertMsgReturnVoid(pButtonNext, ("No Next wizard button found!\n"));
401
402 /* Validate page before going forward: */
403 AssertReturnVoid(m_pWidgetStack->currentIndex() < m_pWidgetStack->count());
404 UINativeWizardPage *pPage = qobject_cast<UINativeWizardPage*>(m_pWidgetStack->currentWidget());
405 AssertPtrReturnVoid(pPage);
406 pButtonNext->setEnabled(false);
407 const bool fIsPageValid = pPage->validatePage();
408 pButtonNext->setEnabled(true);
409 if (!fIsPageValid)
410 return;
411
412 /* For all allowed pages besides the last one we going forward: */
413 bool fNextFound = false;
414 int iIteratedIndex = m_pWidgetStack->currentIndex();
415 while (!fNextFound && iIteratedIndex < m_pWidgetStack->count() - 1)
416 if (isPageVisible(++iIteratedIndex))
417 fNextFound = true;
418 if (fNextFound)
419 m_pWidgetStack->setCurrentIndex(iIteratedIndex);
420 /* For last one we just accept the wizard: */
421 else
422 {
423 /* Different handling depending on current modality: */
424 if (windowHandle()->modality() == Qt::NonModal)
425 close();
426 else
427 accept();
428 }
429}
430
431void UINativeWizard::sltHandleHelpRequest()
432{
433 UIHelpBrowserDialog::findManualFileAndShow(uiCommon().helpKeyword(this));
434}
435
436void UINativeWizard::prepare()
437{
438 /* Prepare main layout: */
439 QVBoxLayout *pLayoutMain = new QVBoxLayout(this);
440 if (pLayoutMain)
441 {
442 /* No need for margins and spacings between sub-layouts: */
443 pLayoutMain->setContentsMargins(0, 0, 0, 0);
444 pLayoutMain->setSpacing(0);
445
446 /* Prepare upper layout: */
447 QHBoxLayout *pLayoutUpper = new QHBoxLayout;
448 if (pLayoutUpper)
449 {
450#ifdef VBOX_WS_MAC
451 /* No need for bottom margin on macOS, reseting others to default: */
452 const int iL = qApp->style()->pixelMetric(QStyle::PM_LayoutLeftMargin);
453 const int iT = qApp->style()->pixelMetric(QStyle::PM_LayoutTopMargin);
454 const int iR = qApp->style()->pixelMetric(QStyle::PM_LayoutRightMargin);
455 pLayoutUpper->setContentsMargins(iL, iT, iR, 0);
456#endif /* VBOX_WS_MAC */
457 /* Reset spacing to default, it was flawed by parent inheritance: */
458 const int iSpacing = qApp->style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing);
459 pLayoutUpper->setSpacing(iSpacing);
460
461 /* Prepare pixmap label: */
462 m_pLabelPixmap = new QLabel(this);
463 if (m_pLabelPixmap)
464 {
465 m_pLabelPixmap->setAlignment(Qt::AlignTop);
466#ifdef VBOX_WS_MAC
467 /* On macOS this label contains background, which isn't a part of layout, moving manually: */
468 m_pLabelPixmap->move(0, 0);
469 /* Spacer to make look&feel native on macOS: */
470 QSpacerItem *pSpacer = new QSpacerItem(200, 0, QSizePolicy::Fixed, QSizePolicy::Minimum);
471 pLayoutUpper->addItem(pSpacer);
472#else /* !VBOX_WS_MAC */
473 /* Just add label into layout on other platforms: */
474 pLayoutUpper->addWidget(m_pLabelPixmap);
475#endif /* !VBOX_WS_MAC */
476 }
477
478 /* Prepare right layout: */
479 m_pLayoutRight = new QVBoxLayout;
480 if (m_pLayoutRight)
481 {
482 /* Prepare page title label: */
483 m_pLabelPageTitle = new QLabel(this);
484 if (m_pLabelPageTitle)
485 {
486 /* Title should have big/fat font: */
487 QFont labelFont = m_pLabelPageTitle->font();
488 labelFont.setBold(true);
489 labelFont.setPointSize(labelFont.pointSize() + 4);
490 m_pLabelPageTitle->setFont(labelFont);
491
492 m_pLayoutRight->addWidget(m_pLabelPageTitle);
493 }
494
495#ifdef VBOX_WS_MAC
496 /* Prepare frame around widget-stack on macOS for nativity purposes: */
497 UIFrame *pFrame = new UIFrame(this);
498 if (pFrame)
499 {
500 /* Prepare frame layout: */
501 QVBoxLayout *pLayoutFrame = new QVBoxLayout(pFrame);
502 if (pLayoutFrame)
503 {
504 /* Prepare widget-stack: */
505 m_pWidgetStack = new QStackedWidget(pFrame);
506 if (m_pWidgetStack)
507 {
508 connect(m_pWidgetStack, &QStackedWidget::currentChanged, this, &UINativeWizard::sltCurrentIndexChanged);
509 pLayoutFrame->addWidget(m_pWidgetStack);
510 }
511 }
512
513 /* Add to layout: */
514 m_pLayoutRight->addWidget(pFrame);
515 }
516#else /* !VBOX_WS_MAC */
517 /* Prepare widget-stack directly on other platforms: */
518 m_pWidgetStack = new QStackedWidget(this);
519 if (m_pWidgetStack)
520 {
521 connect(m_pWidgetStack, &QStackedWidget::currentChanged, this, &UINativeWizard::sltCurrentIndexChanged);
522 m_pLayoutRight->addWidget(m_pWidgetStack);
523 }
524#endif /* !VBOX_WS_MAC */
525
526 /* Add to layout: */
527 pLayoutUpper->addLayout(m_pLayoutRight);
528 }
529
530 /* Add to layout: */
531 pLayoutMain->addLayout(pLayoutUpper, 1);
532 }
533
534 /* Prepare bottom widget: */
535 QWidget *pWidgetBottom = new QWidget(this);
536 if (pWidgetBottom)
537 {
538#ifndef VBOX_WS_MAC
539 /* Adjust palette a bit on Windows/X11 for native purposes: */
540 pWidgetBottom->setAutoFillBackground(true);
541 QPalette pal = QGuiApplication::palette();
542 pal.setColor(QPalette::Active, QPalette::Window, pal.color(QPalette::Active, QPalette::Window).darker(110));
543 pal.setColor(QPalette::Inactive, QPalette::Window, pal.color(QPalette::Inactive, QPalette::Window).darker(110));
544 pWidgetBottom->setPalette(pal);
545#endif /* !VBOX_WS_MAC */
546
547 /* Prepare bottom layout: */
548 QHBoxLayout *pLayoutBottom = new QHBoxLayout(pWidgetBottom);
549 if (pLayoutBottom)
550 {
551 /* Reset margins to default, they were flawed by parent inheritance: */
552 const int iL = qApp->style()->pixelMetric(QStyle::PM_LayoutLeftMargin);
553 const int iT = qApp->style()->pixelMetric(QStyle::PM_LayoutTopMargin);
554 const int iR = qApp->style()->pixelMetric(QStyle::PM_LayoutRightMargin);
555 const int iB = qApp->style()->pixelMetric(QStyle::PM_LayoutBottomMargin);
556 pLayoutBottom->setContentsMargins(iL, iT, iR, iB);
557
558 // WORKAROUND:
559 // Prepare dialog button-box? Huh, no .. QWizard has different opinion.
560 // So we are hardcoding order, same on all platforms, which is the case.
561 for (int i = WizardButtonType_Invalid + 1; i < WizardButtonType_Max; ++i)
562 {
563 const WizardButtonType enmType = (WizardButtonType)i;
564 /* Create Help button only if help keyword is set.
565 * Create other buttons in any case: */
566 if (enmType != WizardButtonType_Help || !m_strHelpKeyword.isEmpty())
567 m_buttons[enmType] = new QPushButton(pWidgetBottom);
568 QPushButton *pButton = wizardButton(enmType);
569 if (pButton)
570 pLayoutBottom->addWidget(pButton);
571 if (enmType == WizardButtonType_Help)
572 pLayoutBottom->addStretch(1);
573 if ( pButton
574 && enmType == WizardButtonType_Next)
575 pButton->setDefault(true);
576 }
577 /* Connect buttons: */
578 if (wizardButton(WizardButtonType_Help))
579 {
580 connect(wizardButton(WizardButtonType_Help), &QPushButton::clicked,
581 this, &UINativeWizard::sltHandleHelpRequest);
582 wizardButton(WizardButtonType_Help)->setShortcut(UIShortcutPool::standardSequence(QKeySequence::HelpContents));
583 uiCommon().setHelpKeyword(this, m_strHelpKeyword);
584 }
585 connect(wizardButton(WizardButtonType_Expert), &QPushButton::clicked,
586 this, &UINativeWizard::sltExpert);
587 connect(wizardButton(WizardButtonType_Back), &QPushButton::clicked,
588 this, &UINativeWizard::sltPrevious);
589 connect(wizardButton(WizardButtonType_Next), &QPushButton::clicked,
590 this, &UINativeWizard::sltNext);
591 connect(wizardButton(WizardButtonType_Cancel), &QPushButton::clicked,
592 this, &UINativeWizard::close);
593 }
594
595 /* Add to layout: */
596 pLayoutMain->addWidget(pWidgetBottom);
597 }
598 }
599
600 /* Prepare local notification-center: */
601 m_pNotificationCenter = new UINotificationCenter(this);
602 if (m_pNotificationCenter)
603 connect(m_pNotificationCenter, &UINotificationCenter::sigOperationsAborted,
604 this, &UINativeWizard::close, Qt::QueuedConnection);
605
606 connect(&translationEventListener(), &UITranslationEventListener::sigRetranslateUI,
607 this, &UINativeWizard::sltRetranslateUI);
608}
609
610void UINativeWizard::cleanup()
611{
612 /* Cleanup local notification-center: */
613 delete m_pNotificationCenter;
614 m_pNotificationCenter = 0;
615}
616
617void UINativeWizard::init()
618{
619 /* Populate pages: */
620 populatePages();
621
622 /* Translate wizard: */
623 sltRetranslateUI();
624 /* Translate wizard pages: */
625 retranslatePages();
626
627 /* Resize wizard to 'golden ratio': */
628 resizeToGoldenRatio();
629
630 /* Make sure current page initialized: */
631 sltCurrentIndexChanged();
632}
633
634void UINativeWizard::deinit()
635{
636 /* Remove all the pages: */
637 m_pWidgetStack->blockSignals(true);
638 while (m_pWidgetStack->count() > 0)
639 {
640 QWidget *pLastWidget = m_pWidgetStack->widget(m_pWidgetStack->count() - 1);
641 m_pWidgetStack->removeWidget(pLastWidget);
642 delete pLastWidget;
643 }
644 m_pWidgetStack->blockSignals(false);
645
646 /* Update last index: */
647 m_iLastIndex = -1;
648 /* Update invisible pages: */
649 m_invisiblePages.clear();
650
651 /* Clean wizard finally: */
652 cleanWizard();
653}
654
655void UINativeWizard::retranslatePages()
656{
657 /* Translate all the pages: */
658 for (int i = 0; i < m_pWidgetStack->count(); ++i)
659 qobject_cast<UINativeWizardPage*>(m_pWidgetStack->widget(i))->retranslate();
660}
661
662void UINativeWizard::resizeToGoldenRatio()
663{
664 /* Standard top margin: */
665 const int iT = qApp->style()->pixelMetric(QStyle::PM_LayoutTopMargin);
666 m_pLayoutRight->setContentsMargins(0, iT, 0, 0);
667 /* Show title label for Basic mode case: */
668 m_pLabelPageTitle->setVisible(m_enmMode == WizardMode_Basic);
669#ifndef VBOX_WS_MAC
670 /* Hide/show pixmap label on Windows/X11 only, on macOS it's in the background: */
671 m_pLabelPixmap->setVisible(!m_strPixmapName.isEmpty());
672#endif /* !VBOX_WS_MAC */
673
674 /* For wizard in Basic mode: */
675 if (m_enmMode == WizardMode_Basic)
676 {
677 /* Temporary hide all the QIRichTextLabel(s) to exclude
678 * influence onto m_pWidgetStack minimum size-hint below: */
679 foreach (QIRichTextLabel *pLabel, findChildren<QIRichTextLabel*>())
680 pLabel->hide();
681 /* Gather suitable dimensions: */
682 const int iStepWidth = 100;
683 const int iMinWidth = qMax(100, m_pWidgetStack->minimumSizeHint().width());
684 const int iMaxWidth = qMax(iMinWidth, gpDesktop->availableGeometry(this).width() * 3 / 4);
685 /* Show all the QIRichTextLabel(s) again, they were hidden above: */
686 foreach (QIRichTextLabel *pLabel, findChildren<QIRichTextLabel*>())
687 pLabel->show();
688 /* Now look for a golden ratio: */
689 int iCurrentWidth = iMinWidth;
690 do
691 {
692 /* Assign current QIRichTextLabel(s) width: */
693 foreach (QIRichTextLabel *pLabel, findChildren<QIRichTextLabel*>())
694 pLabel->setMinimumTextWidth(iCurrentWidth);
695
696 /* Calculate current ratio: */
697 const QSize msh = m_pWidgetStack->minimumSizeHint();
698 int iWidth = msh.width();
699 int iHeight = msh.height();
700#ifndef VBOX_WS_MAC
701 /* Advance width for standard watermark width: */
702 if (!m_strPixmapName.isEmpty())
703 iWidth += 145;
704 /* Advance height for spacing & title height: */
705 if (m_pLayoutRight)
706 {
707 int iL, iT, iR, iB;
708 m_pLayoutRight->getContentsMargins(&iL, &iT, &iR, &iB);
709 iHeight += iT + m_pLayoutRight->spacing() + iB;
710 }
711 if (m_pLabelPageTitle)
712 iHeight += m_pLabelPageTitle->minimumSizeHint().height();
713#endif /* !VBOX_WS_MAC */
714 const double dRatio = (double)iWidth / iHeight;
715 if (dRatio > 1.6)
716 break;
717
718 /* Advance current width: */
719 iCurrentWidth += iStepWidth;
720 }
721 while (iCurrentWidth < iMaxWidth);
722 }
723
724#ifdef VBOX_WS_MAC
725 /* Assign background finally: */
726 if (!m_strPixmapName.isEmpty())
727 assignBackground();
728#else
729 /* Assign watermark finally: */
730 if (!m_strPixmapName.isEmpty())
731 assignWatermark();
732#endif /* !VBOX_WS_MAC */
733
734 /* Make sure layouts are freshly updated & activated: */
735 foreach (QLayout *pLayout, findChildren<QLayout*>())
736 {
737 pLayout->update();
738 pLayout->activate();
739 }
740 QCoreApplication::sendPostedEvents(0, QEvent::LayoutRequest);
741
742 /* Resize to minimum size-hint: */
743 resize(minimumSizeHint());
744}
745
746bool UINativeWizard::isLastVisiblePage(int iPageIndex) const
747{
748 if (!m_pWidgetStack)
749 return false;
750 if (iPageIndex == -1)
751 return false;
752 /* The page itself is not visible: */
753 if (m_invisiblePages.contains(iPageIndex))
754 return false;
755 bool fLastVisible = true;
756 /* Look at the page coming after the page with @p iPageIndex and check if they are visible: */
757 for (int i = iPageIndex + 1; i < m_pWidgetStack->count(); ++i)
758 {
759 if (!m_invisiblePages.contains(i))
760 {
761 fLastVisible = false;
762 break;
763 }
764 }
765 return fLastVisible;
766}
767
768#ifdef VBOX_WS_MAC
769void UINativeWizard::assignBackground()
770{
771 /* Load pixmap to icon first, this will gather HiDPI pixmaps as well: */
772 const QIcon icon = UIIconPool::iconSet(m_strPixmapName);
773
774 /* Acquire pixmap of required size and scale (on basis of parent-widget's device pixel ratio): */
775 const QSize standardSize(620, 440);
776 const qreal fDevicePixelRatio = parentWidget() && parentWidget()->windowHandle() ? parentWidget()->windowHandle()->devicePixelRatio() : 1;
777 const QPixmap pixmapOld = icon.pixmap(standardSize, fDevicePixelRatio);
778
779 /* Assign background finally: */
780 m_pLabelPixmap->setPixmap(pixmapOld);
781 m_pLabelPixmap->resize(m_pLabelPixmap->minimumSizeHint());
782}
783
784#else
785
786void UINativeWizard::assignWatermark()
787{
788 /* Load pixmap to icon first, this will gather HiDPI pixmaps as well: */
789 const QIcon icon = UIIconPool::iconSet(m_strPixmapName);
790
791 /* Acquire pixmap of required size and scale (on basis of parent-widget's device pixel ratio): */
792 const QSize standardSize(145, 290);
793 const qreal fDevicePixelRatio = parentWidget() && parentWidget()->windowHandle() ? parentWidget()->windowHandle()->devicePixelRatio() : 1;
794 const QPixmap pixmapOld = icon.pixmap(standardSize, fDevicePixelRatio);
795
796 /* Convert watermark to image which allows to manage pixel data directly: */
797 const QImage imageOld = pixmapOld.toImage();
798 /* Use the right-top watermark pixel as frame color: */
799 const QRgb rgbFrame = imageOld.pixel(imageOld.width() - 1, 0);
800
801 /* Compose desired height up to pixmap device pixel ratio: */
802 int iL, iT, iR, iB;
803 m_pLayoutRight->getContentsMargins(&iL, &iT, &iR, &iB);
804 const int iSpacing = iT + m_pLayoutRight->spacing() + iB;
805 const int iTitleHeight = m_pLabelPageTitle->minimumSizeHint().height();
806 const int iStackHeight = m_pWidgetStack->minimumSizeHint().height();
807 const int iDesiredHeight = (iTitleHeight + iSpacing + iStackHeight) * pixmapOld.devicePixelRatio();
808 /* Create final image on the basis of incoming, applying the rules: */
809 QImage imageNew(imageOld.width(), qMax(imageOld.height(), iDesiredHeight), imageOld.format());
810 for (int y = 0; y < imageNew.height(); ++y)
811 {
812 for (int x = 0; x < imageNew.width(); ++x)
813 {
814 /* Border rule: */
815 if (x == imageNew.width() - 1)
816 imageNew.setPixel(x, y, rgbFrame);
817 /* Horizontal extension rule - use last used color: */
818 else if (x >= imageOld.width() && y < imageOld.height())
819 imageNew.setPixel(x, y, imageOld.pixel(imageOld.width() - 1, y));
820 /* Vertical extension rule - use last used color: */
821 else if (y >= imageOld.height() && x < imageOld.width())
822 imageNew.setPixel(x, y, imageOld.pixel(x, imageOld.height() - 1));
823 /* Common extension rule - use last used color: */
824 else if (x >= imageOld.width() && y >= imageOld.height())
825 imageNew.setPixel(x, y, imageOld.pixel(imageOld.width() - 1, imageOld.height() - 1));
826 /* Else just copy color: */
827 else
828 imageNew.setPixel(x, y, imageOld.pixel(x, y));
829 }
830 }
831
832 /* Convert processed image to pixmap: */
833 QPixmap pixmapNew = QPixmap::fromImage(imageNew);
834 /* For HiDPI support parent-widget's device pixel ratio is to be taken into account: */
835 double dRatio = 1.0;
836 if ( parentWidget()
837 && parentWidget()->window()
838 && parentWidget()->window()->windowHandle())
839 dRatio = parentWidget()->window()->windowHandle()->devicePixelRatio();
840 pixmapNew.setDevicePixelRatio(dRatio);
841 /* Assign watermark finally: */
842 m_pLabelPixmap->setPixmap(pixmapNew);
843}
844
845#endif /* !VBOX_WS_MAC */
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use