1 | /* $Id: UIVMActivityToolWidget.cpp 104445 2024-04-26 14:09:35Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIVMActivityToolWidget class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-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 <QApplication>
|
---|
30 | #include <QVBoxLayout>
|
---|
31 | #include <QStyle>
|
---|
32 | #include <QTabWidget>
|
---|
33 |
|
---|
34 | /* GUI includes: */
|
---|
35 | #include "QIToolBar.h"
|
---|
36 | #include "UIActionPoolManager.h"
|
---|
37 | #include "UIGlobalSession.h"
|
---|
38 | #include "UIVMActivityMonitor.h"
|
---|
39 | #include "UIVMActivityToolWidget.h"
|
---|
40 | #include "UIMessageCenter.h"
|
---|
41 | #include "UIVirtualMachineItem.h"
|
---|
42 | #include "UIVirtualMachineItemCloud.h"
|
---|
43 | #include "UIVirtualMachineItemLocal.h"
|
---|
44 | #include "UIVMActivityMonitorPaneContainer.h"
|
---|
45 | #ifdef VBOX_WS_MAC
|
---|
46 | # include "UIWindowMenuManager.h"
|
---|
47 | #endif /* VBOX_WS_MAC */
|
---|
48 |
|
---|
49 | /* COM includes: */
|
---|
50 | #include "assert.h"
|
---|
51 | #include "CMachine.h"
|
---|
52 |
|
---|
53 |
|
---|
54 | UIVMActivityToolWidget::UIVMActivityToolWidget(EmbedTo enmEmbedding, UIActionPool *pActionPool,
|
---|
55 | bool fShowToolbar /* = true */, QWidget *pParent /* = 0 */)
|
---|
56 | : QWidget(pParent)
|
---|
57 | , m_enmEmbedding(enmEmbedding)
|
---|
58 | , m_pActionPool(pActionPool)
|
---|
59 | , m_fShowToolbar(fShowToolbar)
|
---|
60 | , m_pToolBar(0)
|
---|
61 | , m_pExportToFileAction(0)
|
---|
62 | , m_pPaneContainer(0)
|
---|
63 | , m_pTabWidget(0)
|
---|
64 | {
|
---|
65 | prepare();
|
---|
66 | prepareActions();
|
---|
67 | prepareToolBar();
|
---|
68 | sltCurrentTabChanged(0);
|
---|
69 | }
|
---|
70 |
|
---|
71 | QMenu *UIVMActivityToolWidget::menu() const
|
---|
72 | {
|
---|
73 | return NULL;
|
---|
74 | }
|
---|
75 |
|
---|
76 | bool UIVMActivityToolWidget::isCurrentTool() const
|
---|
77 | {
|
---|
78 | return m_fIsCurrentTool;
|
---|
79 | }
|
---|
80 |
|
---|
81 | void UIVMActivityToolWidget::setIsCurrentTool(bool fIsCurrentTool)
|
---|
82 | {
|
---|
83 | m_fIsCurrentTool = fIsCurrentTool;
|
---|
84 | }
|
---|
85 |
|
---|
86 | void UIVMActivityToolWidget::prepare()
|
---|
87 | {
|
---|
88 | QVBoxLayout *pMainLayout = new QVBoxLayout(this);
|
---|
89 |
|
---|
90 | m_pTabWidget = new QTabWidget(this);
|
---|
91 | m_pTabWidget->setTabPosition(QTabWidget::East);
|
---|
92 | m_pTabWidget->setTabBarAutoHide(true);
|
---|
93 |
|
---|
94 | pMainLayout->addWidget(m_pTabWidget);
|
---|
95 |
|
---|
96 |
|
---|
97 | m_pPaneContainer = new UIVMActivityMonitorPaneContainer(this, m_enmEmbedding);
|
---|
98 | m_pPaneContainer->hide();
|
---|
99 | pMainLayout->addWidget(m_pPaneContainer);
|
---|
100 | connect(m_pTabWidget, &QTabWidget::currentChanged,
|
---|
101 | this, &UIVMActivityToolWidget::sltCurrentTabChanged);
|
---|
102 | }
|
---|
103 |
|
---|
104 | void UIVMActivityToolWidget::setSelectedVMListItems(const QList<UIVirtualMachineItem*> &items)
|
---|
105 | {
|
---|
106 | setMachines(items);
|
---|
107 | }
|
---|
108 |
|
---|
109 | void UIVMActivityToolWidget::setMachines(const QList<UIVirtualMachineItem*> &machines)
|
---|
110 | {
|
---|
111 | QVector<QUuid> machineIds;
|
---|
112 | foreach (const UIVirtualMachineItem* pMachine, machines)
|
---|
113 | {
|
---|
114 | if (!pMachine)
|
---|
115 | continue;
|
---|
116 | machineIds << pMachine->id();
|
---|
117 | }
|
---|
118 | /* List of machines that are newly added to selected machine list: */
|
---|
119 | QList<UIVirtualMachineItem*> newSelections;
|
---|
120 | QVector<QUuid> unselectedMachines(m_machineIds);
|
---|
121 |
|
---|
122 | foreach (UIVirtualMachineItem* pMachine, machines)
|
---|
123 | {
|
---|
124 | if (!pMachine)
|
---|
125 | continue;
|
---|
126 | QUuid id = pMachine->id();
|
---|
127 | unselectedMachines.removeAll(id);
|
---|
128 | if (!m_machineIds.contains(id))
|
---|
129 | newSelections << pMachine;
|
---|
130 | }
|
---|
131 | m_machineIds = machineIds;
|
---|
132 |
|
---|
133 | removeTabs(unselectedMachines);
|
---|
134 | addTabs(newSelections);
|
---|
135 | }
|
---|
136 |
|
---|
137 | void UIVMActivityToolWidget::prepareActions()
|
---|
138 | {
|
---|
139 | QAction *pToResourcesAction =
|
---|
140 | m_pActionPool->action(UIActionIndex_M_Activity_S_ToVMActivityOverview);
|
---|
141 | if (pToResourcesAction)
|
---|
142 | connect(pToResourcesAction, &QAction::triggered, this, &UIVMActivityToolWidget::sigSwitchToActivityOverviewPane);
|
---|
143 |
|
---|
144 | m_pExportToFileAction =
|
---|
145 | m_pActionPool->action(UIActionIndex_M_Activity_S_Export);
|
---|
146 | if (m_pExportToFileAction)
|
---|
147 | connect(m_pExportToFileAction, &QAction::triggered, this, &UIVMActivityToolWidget::sltExportToFile);
|
---|
148 | if (m_pActionPool->action(UIActionIndex_M_Activity_T_Preferences))
|
---|
149 | connect(m_pActionPool->action(UIActionIndex_M_Activity_T_Preferences), &QAction::toggled,
|
---|
150 | this, &UIVMActivityToolWidget::sltTogglePreferencesPane);
|
---|
151 | }
|
---|
152 |
|
---|
153 | void UIVMActivityToolWidget::prepareToolBar()
|
---|
154 | {
|
---|
155 | /* Create toolbar: */
|
---|
156 | m_pToolBar = new QIToolBar(parentWidget());
|
---|
157 | AssertPtrReturnVoid(m_pToolBar);
|
---|
158 | {
|
---|
159 | /* Configure toolbar: */
|
---|
160 | const int iIconMetric = (int)(QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize));
|
---|
161 | m_pToolBar->setIconSize(QSize(iIconMetric, iIconMetric));
|
---|
162 | m_pToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
|
---|
163 |
|
---|
164 | #ifdef VBOX_WS_MAC
|
---|
165 | /* Check whether we are embedded into a stack: */
|
---|
166 | if (m_enmEmbedding == EmbedTo_Stack)
|
---|
167 | {
|
---|
168 | /* Add into layout: */
|
---|
169 | layout()->addWidget(m_pToolBar);
|
---|
170 | }
|
---|
171 | #else
|
---|
172 | /* Add into layout: */
|
---|
173 | layout()->addWidget(m_pToolBar);
|
---|
174 | #endif
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | void UIVMActivityToolWidget::loadSettings()
|
---|
179 | {
|
---|
180 | }
|
---|
181 |
|
---|
182 | void UIVMActivityToolWidget::removeTabs(const QVector<QUuid> &machineIdsToRemove)
|
---|
183 | {
|
---|
184 | AssertReturnVoid(m_pTabWidget);
|
---|
185 | QVector<UIVMActivityMonitor*> removeList;
|
---|
186 |
|
---|
187 | for (int i = m_pTabWidget->count() - 1; i >= 0; --i)
|
---|
188 | {
|
---|
189 | UIVMActivityMonitor *pMonitor = qobject_cast<UIVMActivityMonitor*>(m_pTabWidget->widget(i));
|
---|
190 | if (!pMonitor)
|
---|
191 | continue;
|
---|
192 | if (machineIdsToRemove.contains(pMonitor->machineId()))
|
---|
193 | {
|
---|
194 | removeList << pMonitor;
|
---|
195 | m_pTabWidget->removeTab(i);
|
---|
196 | }
|
---|
197 | }
|
---|
198 | qDeleteAll(removeList.begin(), removeList.end());
|
---|
199 | }
|
---|
200 |
|
---|
201 | void UIVMActivityToolWidget::addTabs(const QList<UIVirtualMachineItem*> &machines)
|
---|
202 | {
|
---|
203 | AssertReturnVoid(m_pTabWidget);
|
---|
204 | foreach (UIVirtualMachineItem* pMachine, machines)
|
---|
205 | {
|
---|
206 | if (!pMachine)
|
---|
207 | continue;
|
---|
208 | if (pMachine->toLocal())
|
---|
209 | {
|
---|
210 | CMachine comMachine = pMachine->toLocal()->machine();
|
---|
211 | m_pTabWidget->addTab(new UIVMActivityMonitorLocal(m_enmEmbedding, this, comMachine), comMachine.GetName());
|
---|
212 | continue;
|
---|
213 | }
|
---|
214 | if (pMachine->toCloud())
|
---|
215 | {
|
---|
216 | CCloudMachine comMachine = pMachine->toCloud()->machine();
|
---|
217 | if (!comMachine.isOk())
|
---|
218 | continue;
|
---|
219 | m_pTabWidget->addTab(new UIVMActivityMonitorCloud(m_enmEmbedding, this, comMachine), comMachine.GetName());
|
---|
220 | continue;
|
---|
221 | }
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | void UIVMActivityToolWidget::sltExportToFile()
|
---|
226 | {
|
---|
227 | AssertReturnVoid(m_pTabWidget);
|
---|
228 | UIVMActivityMonitor *pActivityMonitor = qobject_cast<UIVMActivityMonitor*>(m_pTabWidget->currentWidget());
|
---|
229 | if (pActivityMonitor)
|
---|
230 | pActivityMonitor->sltExportMetricsToFile();
|
---|
231 | }
|
---|
232 |
|
---|
233 | void UIVMActivityToolWidget::sltCurrentTabChanged(int iIndex)
|
---|
234 | {
|
---|
235 | AssertReturnVoid(m_pTabWidget);
|
---|
236 | Q_UNUSED(iIndex);
|
---|
237 | UIVMActivityMonitor *pActivityMonitor = qobject_cast<UIVMActivityMonitor*>(m_pTabWidget->currentWidget());
|
---|
238 | if (pActivityMonitor)
|
---|
239 | {
|
---|
240 | CMachine comMachine = gpGlobalSession->virtualBox().FindMachine(pActivityMonitor->machineId().toString());
|
---|
241 | if (!comMachine.isNull())
|
---|
242 | {
|
---|
243 | setExportActionEnabled(comMachine.GetState() == KMachineState_Running);
|
---|
244 | }
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | void UIVMActivityToolWidget::sltTogglePreferencesPane(bool fChecked)
|
---|
249 | {
|
---|
250 | AssertReturnVoid(m_pPaneContainer);
|
---|
251 | m_pPaneContainer->setVisible(fChecked);
|
---|
252 | }
|
---|
253 |
|
---|
254 | void UIVMActivityToolWidget::setExportActionEnabled(bool fEnabled)
|
---|
255 | {
|
---|
256 | if (m_pExportToFileAction)
|
---|
257 | m_pExportToFileAction->setEnabled(fEnabled);
|
---|
258 | }
|
---|