File: menubutton.cpp

package info (click to toggle)
cantata 3.3.1.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,300 kB
  • sloc: cpp: 105,773; perl: 1,366; xml: 723; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (130 lines) | stat: -rw-r--r-- 3,470 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * Cantata
 *
 * Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
 *
 * ----
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "menubutton.h"
#include "icons.h"
#include "support/icon.h"
#include <QAction>
#include <QApplication>
#include <QEvent>
#include <QMenu>
#include <QProxyStyle>
#include <QScreen>

#ifdef Q_OS_WIN
#include <windows.h>
#include <VersionHelpers.h>
#ifndef _WIN32_WINNT_WIN10
#define _WIN32_WINNT_WIN10 0x0A00
#endif
#endif

MenuButton::MenuButton(QWidget* parent)
	: ToolButton(parent)
{
	setPopupMode(QToolButton::InstantPopup);
	setIcon(Icons::self()->menuIcon);
	setToolTip(tr("Menu"));
	installEventFilter(this);
}

void MenuButton::controlState()
{
	if (!menu()) {
		return;
	}
	for (QAction* a : menu()->actions()) {
		if (a->isEnabled() && a->isVisible() && !a->isSeparator()) {
			setEnabled(true);
			return;
		}
	}
	setEnabled(false);
}

void MenuButton::setAlignedMenu(QMenu* m)
{
	QToolButton::setMenu(m);
#ifdef Q_OS_WIN
	if (!IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WIN10), LOBYTE(_WIN32_WINNT_WIN10), 0)) {
		return;
	}
#endif
	m->installEventFilter(this);
}

void MenuButton::addSeparator()
{
	QAction* sep = new QAction(this);
	sep->setSeparator(true);
	addAction(sep);
}

bool MenuButton::eventFilter(QObject* o, QEvent* e)
{
	if (QEvent::Show == e->type()) {
		if (qobject_cast<QMenu*>(o)) {
			static int shadowAdjust = -1;
			if (-1 == shadowAdjust) {
				// Kvantum style adds its own shadows, which makes the menu appear in the wrong place.
				// However, Kvatum sill set a property on the style object to indicate the size of
				// the shadows. Use this to adjust positioning.
				QProxyStyle* proxy = qobject_cast<QProxyStyle*>(style());
				QStyle* check = proxy && proxy->baseStyle() ? proxy->baseStyle() : style();
				QList<int> shadows = check ? check->property("menu_shadow").value<QList<int>>() : QList<int>();
				shadowAdjust = 4 == shadows.length() ? shadows.at(2) : 0;
				if (shadowAdjust < 0 || shadowAdjust > 18) {
					shadowAdjust = 0;
				}
			}
			QMenu* mnu = static_cast<QMenu*>(o);
			QPoint p = parentWidget()->mapToGlobal(pos());
			int newPos = isRightToLeft()
					? p.x()
					: ((p.x() + width() + shadowAdjust) - mnu->width());

			if (newPos < 0) {
				newPos = 0;
			}
			else {
				QScreen* sc = QGuiApplication::primaryScreen();
				if (sc) {
					QRect geo = sc->availableGeometry();
					int maxWidth = geo.x() + geo.width();
					if (maxWidth > 0 && (newPos + mnu->width()) > maxWidth) {
						newPos = maxWidth - mnu->width();
					}
				}
			}
			mnu->move(newPos, mnu->y());
		}
		else if (o == this) {
			setMinimumWidth(height());
			removeEventFilter(this);
		}
	}

	return ToolButton::eventFilter(o, e);
}

#include "moc_menubutton.cpp"