Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include "dropdownview.h"
24 :
25 : #include "log.h"
26 :
27 : using namespace muse::uicomponents;
28 :
29 0 : DropdownView::DropdownView(QQuickItem* parent)
30 0 : : PopupView(parent)
31 : {
32 0 : }
33 :
34 0 : void DropdownView::updateGeometry()
35 : {
36 0 : const QQuickItem* parent = parentItem();
37 0 : IF_ASSERT_FAILED(parent) {
38 0 : return;
39 : }
40 :
41 0 : QPointF parentTopLeft = parent->mapToGlobal(QPoint(0, 0));
42 :
43 0 : if (m_globalPos.isNull()) {
44 0 : m_globalPos = parentTopLeft;
45 : }
46 :
47 0 : QRectF anchorRect = anchorGeometry();
48 0 : QRectF popupRect(m_globalPos, contentItem()->size());
49 :
50 : //! NOTE: should be after resolving anchor geometry
51 : //! because we can move out of the screen
52 0 : m_globalPos += m_localPos;
53 :
54 0 : auto movePos = [this, &popupRect](qreal x, qreal y) {
55 0 : m_globalPos.setX(x);
56 0 : m_globalPos.setY(y);
57 :
58 0 : popupRect.moveTopLeft(m_globalPos);
59 0 : };
60 :
61 : // move to focus item
62 0 : movePos(m_globalPos.x(), m_globalPos.y() - focusItemY());
63 :
64 0 : if (popupRect.bottom() > anchorRect.bottom()) {
65 : // move to the right of the parent and move to top to an area that doesn't fit
66 0 : movePos(m_globalPos.x(), m_globalPos.y() - (popupRect.bottom() - anchorRect.bottom()) + padding());
67 : }
68 :
69 0 : if (popupRect.left() < anchorRect.left()) {
70 : // move to the right to an area that doesn't fit
71 0 : movePos(m_globalPos.x() + anchorRect.left() - popupRect.left(), m_globalPos.y());
72 : }
73 :
74 0 : if (popupRect.right() > anchorRect.right()) {
75 : // move to the left to an area that doesn't fit
76 0 : movePos(m_globalPos.x() - (popupRect.right() - anchorRect.right()), m_globalPos.y());
77 : }
78 :
79 0 : if (popupRect.top() < anchorRect.top()) {
80 : // move to the bottom to an area that doesn't fit
81 0 : movePos(m_globalPos.x(), anchorRect.top() + padding());
82 : }
83 :
84 : // remove padding for arrow
85 0 : movePos(m_globalPos.x() - padding(), m_globalPos.y());
86 :
87 0 : updateContentPosition();
88 : }
89 :
90 0 : int DropdownView::focusItemY() const
91 : {
92 0 : return m_focusItemY;
93 : }
94 :
95 0 : void DropdownView::setFocusItemY(int newFocusItemY)
96 : {
97 0 : if (m_focusItemY == newFocusItemY) {
98 0 : return;
99 : }
100 :
101 0 : m_focusItemY = newFocusItemY;
102 0 : emit focusItemYChanged();
103 : }
|