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