1 | /* $Id: UIGraphicsToolBar.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIGraphicsToolBar class definition.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-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 | /* GUI includes: */
|
---|
29 | #include "UIGraphicsToolBar.h"
|
---|
30 | #include "UIGraphicsButton.h"
|
---|
31 |
|
---|
32 |
|
---|
33 | UIGraphicsToolBar::UIGraphicsToolBar(QIGraphicsWidget *pParent, int iRows, int iColumns)
|
---|
34 | : QIGraphicsWidget(pParent)
|
---|
35 | , m_iMargin(3)
|
---|
36 | , m_iRows(iRows)
|
---|
37 | , m_iColumns(iColumns)
|
---|
38 | {
|
---|
39 | }
|
---|
40 |
|
---|
41 | int UIGraphicsToolBar::toolBarMargin() const
|
---|
42 | {
|
---|
43 | return m_iMargin;
|
---|
44 | }
|
---|
45 |
|
---|
46 | void UIGraphicsToolBar::setToolBarMargin(int iMargin)
|
---|
47 | {
|
---|
48 | m_iMargin = iMargin;
|
---|
49 | }
|
---|
50 |
|
---|
51 | void UIGraphicsToolBar::insertItem(UIGraphicsButton *pButton, int iRow, int iColumn)
|
---|
52 | {
|
---|
53 | UIGraphicsToolBarIndex key = qMakePair(iRow, iColumn);
|
---|
54 | m_buttons.insert(key, pButton);
|
---|
55 | }
|
---|
56 |
|
---|
57 | void UIGraphicsToolBar::updateLayout()
|
---|
58 | {
|
---|
59 | /* For all the rows: */
|
---|
60 | for (int iRow = 0; iRow < m_iRows; ++iRow)
|
---|
61 | {
|
---|
62 | /* For all the columns: */
|
---|
63 | for (int iColumn = 0; iColumn < m_iColumns; ++iColumn)
|
---|
64 | {
|
---|
65 | /* Generate key: */
|
---|
66 | UIGraphicsToolBarIndex key = qMakePair(iRow, iColumn);
|
---|
67 | /* Check if key present: */
|
---|
68 | if (m_buttons.contains(key))
|
---|
69 | {
|
---|
70 | /* Get corresponding button: */
|
---|
71 | UIGraphicsButton *pButton = m_buttons.value(key);
|
---|
72 | QSize minimumSize = pButton->minimumSizeHint().toSize();
|
---|
73 | pButton->setPos(toolBarMargin() + iColumn * minimumSize.width(),
|
---|
74 | toolBarMargin() + iRow * minimumSize.height());
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | QSizeF UIGraphicsToolBar::sizeHint(Qt::SizeHint which, const QSizeF &constraint /* = QSizeF() */) const
|
---|
81 | {
|
---|
82 | /* If Qt::MinimumSize hint requested: */
|
---|
83 | if (which == Qt::MinimumSize)
|
---|
84 | {
|
---|
85 | /* Prepare variables: */
|
---|
86 | int iProposedWidth = 2 * toolBarMargin();
|
---|
87 | int iProposedHeight = 2 * toolBarMargin();
|
---|
88 | /* Search for any button: */
|
---|
89 | UIGraphicsButton *pButton = 0;
|
---|
90 | for (int iRow = 0; !pButton && iRow < m_iRows; ++iRow)
|
---|
91 | {
|
---|
92 | /* For all the columns: */
|
---|
93 | for (int iColumn = 0; !pButton && iColumn < m_iColumns; ++iColumn)
|
---|
94 | {
|
---|
95 | /* Generate key: */
|
---|
96 | UIGraphicsToolBarIndex key = qMakePair(iRow, iColumn);
|
---|
97 | /* Check if key present: */
|
---|
98 | if (m_buttons.contains(key))
|
---|
99 | {
|
---|
100 | /* Get corresponding button: */
|
---|
101 | pButton = m_buttons.value(key);
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 | /* If any button found: */
|
---|
106 | if (pButton)
|
---|
107 | {
|
---|
108 | /* Get button minimum-size: */
|
---|
109 | QSize minimumSize = pButton->minimumSizeHint().toSize();
|
---|
110 | iProposedWidth += m_iColumns * minimumSize.width();
|
---|
111 | iProposedHeight += m_iRows * minimumSize.height();
|
---|
112 | }
|
---|
113 | /* Return result: */
|
---|
114 | return QSizeF(iProposedWidth, iProposedHeight);
|
---|
115 | }
|
---|
116 | /* Else call to base-class: */
|
---|
117 | return QIGraphicsWidget::sizeHint(which, constraint);
|
---|
118 | }
|
---|
119 |
|
---|