1 | /* $Id: VBoxLicenseViewer.cpp 71574 2018-03-29 15:10:10Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - VBoxLicenseViewer class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2018 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 <QFile>
|
---|
24 | # include <QPushButton>
|
---|
25 | # include <QScrollBar>
|
---|
26 | # include <QTextBrowser>
|
---|
27 | # include <QVBoxLayout>
|
---|
28 |
|
---|
29 | /* GUI includes: */
|
---|
30 | # include "QIDialogButtonBox.h"
|
---|
31 | # include "VBoxLicenseViewer.h"
|
---|
32 | # include "UIMessageCenter.h"
|
---|
33 |
|
---|
34 | #endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
|
---|
35 |
|
---|
36 |
|
---|
37 | VBoxLicenseViewer::VBoxLicenseViewer(QWidget *pParent /* = 0 */)
|
---|
38 | : QIWithRetranslateUI2<QDialog>(pParent)
|
---|
39 | , m_pLicenseBrowser(0)
|
---|
40 | , m_pButtonAgree(0)
|
---|
41 | , m_pButtonDisagree(0)
|
---|
42 | {
|
---|
43 | #if !(defined(VBOX_WS_WIN) || defined(VBOX_WS_MAC))
|
---|
44 | /* Assign application icon: */
|
---|
45 | setWindowIcon(QIcon(":/VirtualBox_48px.png"));
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | /* Create main layout: */
|
---|
49 | QVBoxLayout *pMainLayout = new QVBoxLayout(this);
|
---|
50 | if (pMainLayout)
|
---|
51 | {
|
---|
52 | /* Create license browser: */
|
---|
53 | m_pLicenseBrowser = new QTextBrowser(this);
|
---|
54 | if (m_pLicenseBrowser)
|
---|
55 | {
|
---|
56 | /* Configure license browser: */
|
---|
57 | m_pLicenseBrowser->verticalScrollBar()->installEventFilter(this);
|
---|
58 | connect(m_pLicenseBrowser->verticalScrollBar(), &QScrollBar::valueChanged,
|
---|
59 | this, &VBoxLicenseViewer::sltHandleScrollBarMoved);
|
---|
60 |
|
---|
61 | /* Add into layout: */
|
---|
62 | pMainLayout->addWidget(m_pLicenseBrowser);
|
---|
63 | }
|
---|
64 |
|
---|
65 | /* Create agree button: */
|
---|
66 | /** @todo rework buttons to be a part of button-box itself */
|
---|
67 | QDialogButtonBox *pDialogButtonBox = new QIDialogButtonBox;
|
---|
68 | if (pDialogButtonBox)
|
---|
69 | {
|
---|
70 | /* Create agree button: */
|
---|
71 | m_pButtonAgree = new QPushButton;
|
---|
72 | if (m_pButtonAgree)
|
---|
73 | {
|
---|
74 | /* Configure button: */
|
---|
75 | connect(m_pButtonAgree, &QPushButton::clicked, this, &QDialog::accept);
|
---|
76 |
|
---|
77 | /* Add into button-box: */
|
---|
78 | pDialogButtonBox->addButton(m_pButtonAgree, QDialogButtonBox::AcceptRole);
|
---|
79 | }
|
---|
80 |
|
---|
81 | /* Create agree button: */
|
---|
82 | m_pButtonDisagree = new QPushButton;
|
---|
83 | if (m_pButtonDisagree)
|
---|
84 | {
|
---|
85 | /* Configure button: */
|
---|
86 | connect(m_pButtonDisagree, &QPushButton::clicked, this, &QDialog::reject);
|
---|
87 |
|
---|
88 | /* Add into button-box: */
|
---|
89 | pDialogButtonBox->addButton(m_pButtonDisagree, QDialogButtonBox::RejectRole);
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | /* Add into layout: */
|
---|
94 | pMainLayout->addWidget(pDialogButtonBox);
|
---|
95 | }
|
---|
96 |
|
---|
97 | /* Configure self: */
|
---|
98 | resize(600, 450);
|
---|
99 |
|
---|
100 | /* Apply language settings: */
|
---|
101 | retranslateUi();
|
---|
102 | }
|
---|
103 |
|
---|
104 | int VBoxLicenseViewer::showLicenseFromString(const QString &strLicenseText)
|
---|
105 | {
|
---|
106 | /* Set license text: */
|
---|
107 | m_pLicenseBrowser->setText(strLicenseText);
|
---|
108 | return exec();
|
---|
109 | }
|
---|
110 |
|
---|
111 | int VBoxLicenseViewer::showLicenseFromFile(const QString &strLicenseFileName)
|
---|
112 | {
|
---|
113 | /* Read license file: */
|
---|
114 | QFile file(strLicenseFileName);
|
---|
115 | if (file.open(QIODevice::ReadOnly))
|
---|
116 | return showLicenseFromString(file.readAll());
|
---|
117 | else
|
---|
118 | {
|
---|
119 | msgCenter().cannotOpenLicenseFile(strLicenseFileName, this);
|
---|
120 | return QDialog::Rejected;
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | bool VBoxLicenseViewer::eventFilter(QObject *pObject, QEvent *pEvent)
|
---|
125 | {
|
---|
126 | /* Handle known event types: */
|
---|
127 | switch (pEvent->type())
|
---|
128 | {
|
---|
129 | case QEvent::Hide:
|
---|
130 | if (pObject == m_pLicenseBrowser->verticalScrollBar())
|
---|
131 | /* Doesn't work on wm's like ion3 where the window starts maximized: isActiveWindow() */
|
---|
132 | sltUnlockButtons();
|
---|
133 | default:
|
---|
134 | break;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /* Call to base-class: */
|
---|
138 | return QDialog::eventFilter(pObject, pEvent);
|
---|
139 | }
|
---|
140 |
|
---|
141 | void VBoxLicenseViewer::showEvent(QShowEvent *pEvent)
|
---|
142 | {
|
---|
143 | /* Call to base-class: */
|
---|
144 | QDialog::showEvent(pEvent);
|
---|
145 |
|
---|
146 | /* Enable/disable buttons accordingly: */
|
---|
147 | bool fScrollBarHidden = !m_pLicenseBrowser->verticalScrollBar()->isVisible()
|
---|
148 | && !(windowState() & Qt::WindowMinimized);
|
---|
149 | m_pButtonAgree->setEnabled(fScrollBarHidden);
|
---|
150 | m_pButtonDisagree->setEnabled(fScrollBarHidden);
|
---|
151 | }
|
---|
152 |
|
---|
153 | void VBoxLicenseViewer::retranslateUi()
|
---|
154 | {
|
---|
155 | /* Translate dialog title: */
|
---|
156 | setWindowTitle(tr("VirtualBox License"));
|
---|
157 |
|
---|
158 | /* Translate buttons: */
|
---|
159 | m_pButtonAgree->setText(tr("I &Agree"));
|
---|
160 | m_pButtonDisagree->setText(tr("I &Disagree"));
|
---|
161 | }
|
---|
162 |
|
---|
163 | int VBoxLicenseViewer::exec()
|
---|
164 | {
|
---|
165 | /* Nothing wrong with that, just hiding slot: */
|
---|
166 | return QDialog::exec();
|
---|
167 | }
|
---|
168 |
|
---|
169 | void VBoxLicenseViewer::sltHandleScrollBarMoved(int iValue)
|
---|
170 | {
|
---|
171 | if (iValue == m_pLicenseBrowser->verticalScrollBar()->maximum())
|
---|
172 | sltUnlockButtons();
|
---|
173 | }
|
---|
174 |
|
---|
175 | void VBoxLicenseViewer::sltUnlockButtons()
|
---|
176 | {
|
---|
177 | m_pButtonAgree->setEnabled(true);
|
---|
178 | m_pButtonDisagree->setEnabled(true);
|
---|
179 | }
|
---|
180 |
|
---|