1 | /* $Id: UIPopupStackViewport.cpp 71521 2018-03-26 17:09:08Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIPopupStackViewport class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-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 | /* GUI includes: */
|
---|
23 | # include "UIPopupPane.h"
|
---|
24 | # include "UIPopupStackViewport.h"
|
---|
25 |
|
---|
26 | /* Other VBox includes: */
|
---|
27 | # include <VBox/sup.h>
|
---|
28 |
|
---|
29 | #endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
|
---|
30 |
|
---|
31 |
|
---|
32 | UIPopupStackViewport::UIPopupStackViewport()
|
---|
33 | : m_iLayoutMargin(1)
|
---|
34 | , m_iLayoutSpacing(1)
|
---|
35 | {
|
---|
36 | }
|
---|
37 |
|
---|
38 | bool UIPopupStackViewport::exists(const QString &strID) const
|
---|
39 | {
|
---|
40 | /* Is there already popup-pane with the same ID? */
|
---|
41 | return m_panes.contains(strID);
|
---|
42 | }
|
---|
43 |
|
---|
44 | void UIPopupStackViewport::createPopupPane(const QString &strID,
|
---|
45 | const QString &strMessage, const QString &strDetails,
|
---|
46 | const QMap<int, QString> &buttonDescriptions)
|
---|
47 | {
|
---|
48 | /* Make sure there is no such popup-pane already: */
|
---|
49 | if (m_panes.contains(strID))
|
---|
50 | {
|
---|
51 | AssertMsgFailed(("Popup-pane already exists!"));
|
---|
52 | return;
|
---|
53 | }
|
---|
54 |
|
---|
55 | /* Create new popup-pane: */
|
---|
56 | UIPopupPane *pPopupPane = m_panes[strID] = new UIPopupPane(this,
|
---|
57 | strMessage, strDetails,
|
---|
58 | buttonDescriptions);
|
---|
59 |
|
---|
60 | /* Attach popup-pane connection: */
|
---|
61 | connect(this, &UIPopupStackViewport::sigProposePopupPaneSize, pPopupPane, &UIPopupPane::sltHandleProposalForSize);
|
---|
62 | connect(pPopupPane, SIGNAL(sigSizeHintChanged()), this, SLOT(sltAdjustGeometry()));
|
---|
63 | connect(pPopupPane, SIGNAL(sigDone(int)), this, SLOT(sltPopupPaneDone(int)));
|
---|
64 |
|
---|
65 | /* Show popup-pane: */
|
---|
66 | pPopupPane->show();
|
---|
67 | }
|
---|
68 |
|
---|
69 | void UIPopupStackViewport::updatePopupPane(const QString &strID,
|
---|
70 | const QString &strMessage, const QString &strDetails)
|
---|
71 | {
|
---|
72 | /* Make sure there is such popup-pane already: */
|
---|
73 | if (!m_panes.contains(strID))
|
---|
74 | {
|
---|
75 | AssertMsgFailed(("Popup-pane doesn't exists!"));
|
---|
76 | return;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /* Get existing popup-pane: */
|
---|
80 | UIPopupPane *pPopupPane = m_panes[strID];
|
---|
81 |
|
---|
82 | /* Update message and details: */
|
---|
83 | pPopupPane->setMessage(strMessage);
|
---|
84 | pPopupPane->setDetails(strDetails);
|
---|
85 | }
|
---|
86 |
|
---|
87 | void UIPopupStackViewport::recallPopupPane(const QString &strID)
|
---|
88 | {
|
---|
89 | /* Make sure there is such popup-pane already: */
|
---|
90 | if (!m_panes.contains(strID))
|
---|
91 | {
|
---|
92 | AssertMsgFailed(("Popup-pane doesn't exists!"));
|
---|
93 | return;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /* Get existing popup-pane: */
|
---|
97 | UIPopupPane *pPopupPane = m_panes[strID];
|
---|
98 |
|
---|
99 | /* Recall popup-pane: */
|
---|
100 | pPopupPane->recall();
|
---|
101 | }
|
---|
102 |
|
---|
103 | void UIPopupStackViewport::sltHandleProposalForSize(QSize newSize)
|
---|
104 | {
|
---|
105 | /* Subtract layout margins: */
|
---|
106 | newSize.setWidth(newSize.width() - 2 * m_iLayoutMargin);
|
---|
107 | newSize.setHeight(newSize.height() - 2 * m_iLayoutMargin);
|
---|
108 |
|
---|
109 | /* Propagate resulting size to popups: */
|
---|
110 | emit sigProposePopupPaneSize(newSize);
|
---|
111 | }
|
---|
112 |
|
---|
113 | void UIPopupStackViewport::sltAdjustGeometry()
|
---|
114 | {
|
---|
115 | /* Update size-hint: */
|
---|
116 | updateSizeHint();
|
---|
117 |
|
---|
118 | /* Layout content: */
|
---|
119 | layoutContent();
|
---|
120 |
|
---|
121 | /* Notify parent popup-stack: */
|
---|
122 | emit sigSizeHintChanged();
|
---|
123 | }
|
---|
124 |
|
---|
125 | void UIPopupStackViewport::sltPopupPaneDone(int iResultCode)
|
---|
126 | {
|
---|
127 | /* Make sure the sender is the popup-pane: */
|
---|
128 | UIPopupPane *pPopupPane = qobject_cast<UIPopupPane*>(sender());
|
---|
129 | if (!pPopupPane)
|
---|
130 | {
|
---|
131 | AssertMsgFailed(("Should be called by popup-pane only!"));
|
---|
132 | return;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /* Make sure the popup-pane still exists: */
|
---|
136 | const QString strID(m_panes.key(pPopupPane, QString()));
|
---|
137 | if (strID.isNull())
|
---|
138 | {
|
---|
139 | AssertMsgFailed(("Popup-pane already destroyed!"));
|
---|
140 | return;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /* Notify listeners about popup-pane removal: */
|
---|
144 | emit sigPopupPaneDone(strID, iResultCode);
|
---|
145 |
|
---|
146 | /* Delete popup-pane asyncronously.
|
---|
147 | * To avoid issues with events which already posted: */
|
---|
148 | m_panes.remove(strID);
|
---|
149 | pPopupPane->deleteLater();
|
---|
150 |
|
---|
151 | /* Notify listeners about popup-pane removed: */
|
---|
152 | emit sigPopupPaneRemoved(strID);
|
---|
153 |
|
---|
154 | /* Adjust geometry: */
|
---|
155 | sltAdjustGeometry();
|
---|
156 |
|
---|
157 | /* Make sure this stack still contains popup-panes: */
|
---|
158 | if (!m_panes.isEmpty())
|
---|
159 | return;
|
---|
160 |
|
---|
161 | /* Notify listeners about popup-stack: */
|
---|
162 | emit sigPopupPanesRemoved();
|
---|
163 | }
|
---|
164 |
|
---|
165 | void UIPopupStackViewport::updateSizeHint()
|
---|
166 | {
|
---|
167 | /* Calculate minimum width-hint: */
|
---|
168 | int iMinimumWidthHint = 0;
|
---|
169 | {
|
---|
170 | /* Take into account all the panes: */
|
---|
171 | foreach (UIPopupPane *pPane, m_panes)
|
---|
172 | iMinimumWidthHint = qMax(iMinimumWidthHint, pPane->minimumSizeHint().width());
|
---|
173 |
|
---|
174 | /* And two margins finally: */
|
---|
175 | iMinimumWidthHint += 2 * m_iLayoutMargin;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /* Calculate minimum height-hint: */
|
---|
179 | int iMinimumHeightHint = 0;
|
---|
180 | {
|
---|
181 | /* Take into account all the panes: */
|
---|
182 | foreach (UIPopupPane *pPane, m_panes)
|
---|
183 | iMinimumHeightHint += pPane->minimumSizeHint().height();
|
---|
184 |
|
---|
185 | /* Take into account all the spacings, if any: */
|
---|
186 | if (!m_panes.isEmpty())
|
---|
187 | iMinimumHeightHint += (m_panes.size() - 1) * m_iLayoutSpacing;
|
---|
188 |
|
---|
189 | /* And two margins finally: */
|
---|
190 | iMinimumHeightHint += 2 * m_iLayoutMargin;
|
---|
191 | }
|
---|
192 |
|
---|
193 | /* Compose minimum size-hint: */
|
---|
194 | m_minimumSizeHint = QSize(iMinimumWidthHint, iMinimumHeightHint);
|
---|
195 | }
|
---|
196 |
|
---|
197 | void UIPopupStackViewport::layoutContent()
|
---|
198 | {
|
---|
199 | /* Get attributes: */
|
---|
200 | int iX = m_iLayoutMargin;
|
---|
201 | int iY = m_iLayoutMargin;
|
---|
202 |
|
---|
203 | /* Layout every pane we have: */
|
---|
204 | foreach (UIPopupPane *pPane, m_panes)
|
---|
205 | {
|
---|
206 | /* Get pane attributes: */
|
---|
207 | QSize paneSize = pPane->minimumSizeHint();
|
---|
208 | const int iPaneWidth = paneSize.width();
|
---|
209 | const int iPaneHeight = paneSize.height();
|
---|
210 | /* Adjust geometry for the pane: */
|
---|
211 | pPane->setGeometry(iX, iY, iPaneWidth, iPaneHeight);
|
---|
212 | pPane->layoutContent();
|
---|
213 | /* Increment placeholder: */
|
---|
214 | iY += (iPaneHeight + m_iLayoutSpacing);
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|