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