UIWarningPane.cpp revision d34409ad02ea0d28e08a6c4b089a412fdb3b4c9c
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync/* $Id$ */
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync/** @file
d34409ad02ea0d28e08a6c4b089a412fdb3b4c9cvboxsync * VBox Qt GUI - UIWarningPane class implementation.
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync */
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync/*
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync * Copyright (C) 2009-2013 Oracle Corporation
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync *
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync * available from http://www.virtualbox.org. This file is free software;
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync * you can redistribute it and/or modify it under the terms of the GNU
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync * General Public License (GPL) as published by the Free Software
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync */
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync/* Qt includes: */
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync#include <QHBoxLayout>
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync#include <QLabel>
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync#include <QEvent>
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync#include <QTimer>
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync/* GUI includes: */
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync#include "UIWarningPane.h"
d67a5f90076d3e208810415abb792f5c42081c97vboxsync#include "QIWidgetValidator.h"
d67a5f90076d3e208810415abb792f5c42081c97vboxsync
d67a5f90076d3e208810415abb792f5c42081c97vboxsync/* Other VBox includes: */
d67a5f90076d3e208810415abb792f5c42081c97vboxsync#include <VBox/sup.h>
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsyncUIWarningPane::UIWarningPane(QWidget *pParent)
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync : QWidget(pParent)
b885f23e839880e918ddbcd8c135b7023d97ba09vboxsync , m_pIconLayout(0)
b885f23e839880e918ddbcd8c135b7023d97ba09vboxsync , m_pTextLabel(0)
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync , m_pHoverTimer(0)
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync , m_iHoveredIconLabelPosition(-1)
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync{
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync /* Prepare: */
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync prepare();
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync}
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync
d67a5f90076d3e208810415abb792f5c42081c97vboxsyncvoid UIWarningPane::setWarningLabel(const QString &strWarningLabel)
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync{
b885f23e839880e918ddbcd8c135b7023d97ba09vboxsync /* Assign passed text directly to warning-label: */
b885f23e839880e918ddbcd8c135b7023d97ba09vboxsync m_pTextLabel->setText(strWarningLabel);
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync}
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync
d67a5f90076d3e208810415abb792f5c42081c97vboxsyncvoid UIWarningPane::registerValidator(UIPageValidator *pValidator)
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync{
b885f23e839880e918ddbcd8c135b7023d97ba09vboxsync /* Make sure validator exists: */
d67a5f90076d3e208810415abb792f5c42081c97vboxsync AssertPtrReturnVoid(pValidator);
d67a5f90076d3e208810415abb792f5c42081c97vboxsync
b885f23e839880e918ddbcd8c135b7023d97ba09vboxsync /* Make sure validator is not registered yet: */
d67a5f90076d3e208810415abb792f5c42081c97vboxsync if (m_validators.contains(pValidator))
d67a5f90076d3e208810415abb792f5c42081c97vboxsync {
d67a5f90076d3e208810415abb792f5c42081c97vboxsync AssertMsgFailed(("Validator is registered already!\n"));
d67a5f90076d3e208810415abb792f5c42081c97vboxsync return;
d67a5f90076d3e208810415abb792f5c42081c97vboxsync }
d67a5f90076d3e208810415abb792f5c42081c97vboxsync
d67a5f90076d3e208810415abb792f5c42081c97vboxsync /* Register validator: */
d67a5f90076d3e208810415abb792f5c42081c97vboxsync m_validators << pValidator;
d67a5f90076d3e208810415abb792f5c42081c97vboxsync
d67a5f90076d3e208810415abb792f5c42081c97vboxsync /* Create icon-label for newly registered validator: */
d67a5f90076d3e208810415abb792f5c42081c97vboxsync QLabel *pIconLabel = new QLabel;
d67a5f90076d3e208810415abb792f5c42081c97vboxsync {
d67a5f90076d3e208810415abb792f5c42081c97vboxsync /* Add icon-label into list: */
d67a5f90076d3e208810415abb792f5c42081c97vboxsync m_icons << pIconLabel;
d67a5f90076d3e208810415abb792f5c42081c97vboxsync /* Add icon-label into layout: */
b885f23e839880e918ddbcd8c135b7023d97ba09vboxsync m_pIconLayout->addWidget(pIconLabel);
d67a5f90076d3e208810415abb792f5c42081c97vboxsync /* Configure icon-label: */
d67a5f90076d3e208810415abb792f5c42081c97vboxsync pIconLabel->setMouseTracking(true);
d67a5f90076d3e208810415abb792f5c42081c97vboxsync pIconLabel->installEventFilter(this);
d67a5f90076d3e208810415abb792f5c42081c97vboxsync pIconLabel->setPixmap(pValidator->warningPixmap());
d67a5f90076d3e208810415abb792f5c42081c97vboxsync connect(pValidator, SIGNAL(sigShowWarningIcon()), pIconLabel, SLOT(show()));
d67a5f90076d3e208810415abb792f5c42081c97vboxsync connect(pValidator, SIGNAL(sigHideWarningIcon()), pIconLabel, SLOT(hide()));
d67a5f90076d3e208810415abb792f5c42081c97vboxsync }
d67a5f90076d3e208810415abb792f5c42081c97vboxsync
b885f23e839880e918ddbcd8c135b7023d97ba09vboxsync /* Mark icon as 'unhovered': */
d67a5f90076d3e208810415abb792f5c42081c97vboxsync m_hovered << false;
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync}
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync
a1385fbfe3e43d21a14fc643da59512302069d88vboxsyncvoid UIWarningPane::sltHandleHoverTimer()
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync{
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync /* Notify listeners about hovering: */
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync if (m_iHoveredIconLabelPosition >= 0 && m_iHoveredIconLabelPosition < m_validators.size())
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync emit sigHoverEnter(m_validators[m_iHoveredIconLabelPosition]);
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync}
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsyncvoid UIWarningPane::prepare()
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync{
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync /* Prepare content: */
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync prepareContent();
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync}
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsyncvoid UIWarningPane::prepareContent()
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync{
d67a5f90076d3e208810415abb792f5c42081c97vboxsync /* Create main-layout: */
d67a5f90076d3e208810415abb792f5c42081c97vboxsync QHBoxLayout *pMainLayout = new QHBoxLayout(this);
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync {
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync /* Configure layout: */
d67a5f90076d3e208810415abb792f5c42081c97vboxsync pMainLayout->setContentsMargins(0, 0, 0, 0);
b40b3662dc06fdc995315e89ecef4a9d7098fbd8vboxsync pMainLayout->setSpacing(10);
d67a5f90076d3e208810415abb792f5c42081c97vboxsync /* Add left stretch: */
d67a5f90076d3e208810415abb792f5c42081c97vboxsync pMainLayout->addStretch();
b885f23e839880e918ddbcd8c135b7023d97ba09vboxsync /* Create text-label: */
b885f23e839880e918ddbcd8c135b7023d97ba09vboxsync m_pTextLabel = new QLabel;
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync {
d67a5f90076d3e208810415abb792f5c42081c97vboxsync /* Add into main-layout: */
b885f23e839880e918ddbcd8c135b7023d97ba09vboxsync pMainLayout->addWidget(m_pTextLabel);
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync }
d67a5f90076d3e208810415abb792f5c42081c97vboxsync /* Create layout: */
b885f23e839880e918ddbcd8c135b7023d97ba09vboxsync m_pIconLayout = new QHBoxLayout;
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync {
d67a5f90076d3e208810415abb792f5c42081c97vboxsync /* Configure layout: */
b885f23e839880e918ddbcd8c135b7023d97ba09vboxsync m_pIconLayout->setContentsMargins(0, 0, 0, 0);
b40b3662dc06fdc995315e89ecef4a9d7098fbd8vboxsync m_pIconLayout->setSpacing(6);
d67a5f90076d3e208810415abb792f5c42081c97vboxsync /* Add into main-layout: */
b885f23e839880e918ddbcd8c135b7023d97ba09vboxsync pMainLayout->addLayout(m_pIconLayout);
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync }
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync /* Create hover-timer: */
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync m_pHoverTimer = new QTimer(this);
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync {
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync /* Configure timer: */
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync m_pHoverTimer->setInterval(200);
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync m_pHoverTimer->setSingleShot(true);
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync connect(m_pHoverTimer, SIGNAL(timeout()), this, SLOT(sltHandleHoverTimer()));
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync }
d67a5f90076d3e208810415abb792f5c42081c97vboxsync /* Add right stretch: */
d67a5f90076d3e208810415abb792f5c42081c97vboxsync pMainLayout->addStretch();
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync }
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync}
235d3bea31bdca1dd7f555821eacfd0dd36bd6f5vboxsync
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsyncbool UIWarningPane::eventFilter(QObject *pWatched, QEvent *pEvent)
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync{
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync /* Depending on event-type: */
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync switch (pEvent->type())
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync {
d67a5f90076d3e208810415abb792f5c42081c97vboxsync /* One of icons hovered: */
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync case QEvent::MouseMove:
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync {
d67a5f90076d3e208810415abb792f5c42081c97vboxsync /* Cast object to label: */
d67a5f90076d3e208810415abb792f5c42081c97vboxsync if (QLabel *pIconLabel = qobject_cast<QLabel*>(pWatched))
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync {
d67a5f90076d3e208810415abb792f5c42081c97vboxsync /* Search for the corresponding icon: */
d67a5f90076d3e208810415abb792f5c42081c97vboxsync if (m_icons.contains(pIconLabel))
d67a5f90076d3e208810415abb792f5c42081c97vboxsync {
d67a5f90076d3e208810415abb792f5c42081c97vboxsync /* Mark icon-label hovered if not yet: */
d67a5f90076d3e208810415abb792f5c42081c97vboxsync int iIconLabelPosition = m_icons.indexOf(pIconLabel);
d67a5f90076d3e208810415abb792f5c42081c97vboxsync if (!m_hovered[iIconLabelPosition])
d67a5f90076d3e208810415abb792f5c42081c97vboxsync {
d67a5f90076d3e208810415abb792f5c42081c97vboxsync m_hovered[iIconLabelPosition] = true;
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync m_iHoveredIconLabelPosition = iIconLabelPosition;
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync m_pHoverTimer->start();
d67a5f90076d3e208810415abb792f5c42081c97vboxsync }
d67a5f90076d3e208810415abb792f5c42081c97vboxsync }
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync }
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync break;
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync }
d67a5f90076d3e208810415abb792f5c42081c97vboxsync /* One of icons unhovered: */
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync case QEvent::Leave:
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync {
d67a5f90076d3e208810415abb792f5c42081c97vboxsync /* Cast object to label: */
d67a5f90076d3e208810415abb792f5c42081c97vboxsync if (QLabel *pIconLabel = qobject_cast<QLabel*>(pWatched))
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync {
d67a5f90076d3e208810415abb792f5c42081c97vboxsync /* Search for the corresponding icon: */
d67a5f90076d3e208810415abb792f5c42081c97vboxsync if (m_icons.contains(pIconLabel))
d67a5f90076d3e208810415abb792f5c42081c97vboxsync {
d67a5f90076d3e208810415abb792f5c42081c97vboxsync /* Mark icon-label unhovered if not yet: */
d67a5f90076d3e208810415abb792f5c42081c97vboxsync int iIconLabelPosition = m_icons.indexOf(pIconLabel);
d67a5f90076d3e208810415abb792f5c42081c97vboxsync if (m_hovered[iIconLabelPosition])
d67a5f90076d3e208810415abb792f5c42081c97vboxsync {
d67a5f90076d3e208810415abb792f5c42081c97vboxsync m_hovered[iIconLabelPosition] = false;
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync if (m_pHoverTimer->isActive())
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync {
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync m_pHoverTimer->stop();
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync m_iHoveredIconLabelPosition = -1;
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync }
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync else
a1385fbfe3e43d21a14fc643da59512302069d88vboxsync emit sigHoverLeave(m_validators[iIconLabelPosition]);
d67a5f90076d3e208810415abb792f5c42081c97vboxsync }
d67a5f90076d3e208810415abb792f5c42081c97vboxsync }
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync }
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync break;
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync }
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync /* Default case: */
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync default: break;
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync }
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync /* Call to base-class: */
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync return QWidget::eventFilter(pWatched, pEvent);
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync}
1548538a86bdf3de84605db9c0ae03e000a55c6fvboxsync