1 | /* $Id: VBoxLicenseViewer.cpp 38311 2011-08-04 13:08:39Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * VBox frontends: Qt4 GUI ("VirtualBox"):
|
---|
5 | * VBoxLicenseViewer class implementation
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #ifdef VBOX_WITH_PRECOMPILED_HEADERS
|
---|
21 | # include "precomp.h"
|
---|
22 | #else /* !VBOX_WITH_PRECOMPILED_HEADERS */
|
---|
23 | #include "VBoxLicenseViewer.h"
|
---|
24 | #include "QIDialogButtonBox.h"
|
---|
25 | #include "VBoxGlobal.h"
|
---|
26 | #include "UIMessageCenter.h"
|
---|
27 |
|
---|
28 | /* Qt includes */
|
---|
29 | #include <QTextBrowser>
|
---|
30 | #include <QPushButton>
|
---|
31 | #include <QVBoxLayout>
|
---|
32 | #include <QScrollBar>
|
---|
33 | #include <QFile>
|
---|
34 | #endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
|
---|
35 |
|
---|
36 | VBoxLicenseViewer::VBoxLicenseViewer(QWidget *pParent /* = 0 */)
|
---|
37 | : QIWithRetranslateUI2<QDialog>(pParent)
|
---|
38 | , mLicenseText (0)
|
---|
39 | , mAgreeButton (0)
|
---|
40 | , mDisagreeButton (0)
|
---|
41 | {
|
---|
42 | #ifndef Q_WS_WIN
|
---|
43 | /* Application icon. On Win32, it's built-in to the executable. */
|
---|
44 | setWindowIcon (QIcon (":/VirtualBox_48px.png"));
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | mLicenseText = new QTextBrowser (this);
|
---|
48 | mAgreeButton = new QPushButton (this);
|
---|
49 | mDisagreeButton = new QPushButton (this);
|
---|
50 | QDialogButtonBox *dbb = new QIDialogButtonBox (this);
|
---|
51 | dbb->addButton (mAgreeButton, QDialogButtonBox::AcceptRole);
|
---|
52 | dbb->addButton (mDisagreeButton, QDialogButtonBox::RejectRole);
|
---|
53 |
|
---|
54 | connect (mLicenseText->verticalScrollBar(), SIGNAL (valueChanged (int)),
|
---|
55 | SLOT (onScrollBarMoving (int)));
|
---|
56 | connect (mAgreeButton, SIGNAL (clicked()), SLOT (accept()));
|
---|
57 | connect (mDisagreeButton, SIGNAL (clicked()), SLOT (reject()));
|
---|
58 |
|
---|
59 | QVBoxLayout *mainLayout = new QVBoxLayout (this);
|
---|
60 | mainLayout->setSpacing (10);
|
---|
61 | VBoxGlobal::setLayoutMargin (mainLayout, 10);
|
---|
62 | mainLayout->addWidget (mLicenseText);
|
---|
63 | mainLayout->addWidget (dbb);
|
---|
64 |
|
---|
65 | mLicenseText->verticalScrollBar()->installEventFilter (this);
|
---|
66 |
|
---|
67 | resize (600, 450);
|
---|
68 |
|
---|
69 | retranslateUi();
|
---|
70 | }
|
---|
71 |
|
---|
72 | int VBoxLicenseViewer::showLicenseFromFile(const QString &strLicenseFileName)
|
---|
73 | {
|
---|
74 | /* Read license file: */
|
---|
75 | QFile file(strLicenseFileName);
|
---|
76 | if (file.open(QIODevice::ReadOnly))
|
---|
77 | {
|
---|
78 | return showLicenseFromString(file.readAll());
|
---|
79 | }
|
---|
80 | else
|
---|
81 | {
|
---|
82 | msgCenter().cannotOpenLicenseFile(this, strLicenseFileName);
|
---|
83 | return QDialog::Rejected;
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | int VBoxLicenseViewer::showLicenseFromString(const QString &strLicenseText)
|
---|
88 | {
|
---|
89 | /* Set license text: */
|
---|
90 | mLicenseText->setText(strLicenseText);
|
---|
91 | return exec();
|
---|
92 | }
|
---|
93 |
|
---|
94 | void VBoxLicenseViewer::retranslateUi()
|
---|
95 | {
|
---|
96 | setWindowTitle (tr ("VirtualBox License"));
|
---|
97 |
|
---|
98 | mAgreeButton->setText (tr ("I &Agree"));
|
---|
99 | mDisagreeButton->setText (tr ("I &Disagree"));
|
---|
100 | }
|
---|
101 |
|
---|
102 | int VBoxLicenseViewer::exec()
|
---|
103 | {
|
---|
104 | return QDialog::exec();
|
---|
105 | }
|
---|
106 |
|
---|
107 | void VBoxLicenseViewer::onScrollBarMoving (int aValue)
|
---|
108 | {
|
---|
109 | if (aValue == mLicenseText->verticalScrollBar()->maximum())
|
---|
110 | unlockButtons();
|
---|
111 | }
|
---|
112 |
|
---|
113 | void VBoxLicenseViewer::unlockButtons()
|
---|
114 | {
|
---|
115 | mAgreeButton->setEnabled (true);
|
---|
116 | mDisagreeButton->setEnabled (true);
|
---|
117 | }
|
---|
118 |
|
---|
119 | void VBoxLicenseViewer::showEvent (QShowEvent *aEvent)
|
---|
120 | {
|
---|
121 | QDialog::showEvent (aEvent);
|
---|
122 | bool isScrollBarHidden = !mLicenseText->verticalScrollBar()->isVisible()
|
---|
123 | && !(windowState() & Qt::WindowMinimized);
|
---|
124 | mAgreeButton->setEnabled (isScrollBarHidden);
|
---|
125 | mDisagreeButton->setEnabled (isScrollBarHidden);
|
---|
126 | }
|
---|
127 |
|
---|
128 | bool VBoxLicenseViewer::eventFilter (QObject *aObject, QEvent *aEvent)
|
---|
129 | {
|
---|
130 | switch (aEvent->type())
|
---|
131 | {
|
---|
132 | case QEvent::Hide:
|
---|
133 | if (aObject == mLicenseText->verticalScrollBar())
|
---|
134 | /* Doesn't work on wm's like ion3 where the window starts
|
---|
135 | * maximized: isActiveWindow() */
|
---|
136 | unlockButtons();
|
---|
137 | default:
|
---|
138 | break;
|
---|
139 | }
|
---|
140 | return QDialog::eventFilter (aObject, aEvent);
|
---|
141 | }
|
---|
142 |
|
---|