-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtoolmenu.cpp
196 lines (171 loc) · 6.36 KB
/
toolmenu.cpp
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "toolmenu.h"
#include "ui_toolmenu.h"
#include <QDebug>
#include <QMouseEvent>
#include <QTime>
#include <QTimer>
#include <windows.h>
#include <QScreen>
#include <Utils/keystate.h>
ToolMenu::ToolMenu(QWidget *parent) :
QDialog(parent),
ui(new Ui::ToolMenu)
{
ui->setupUi(this);
setWindowFlag(Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground); //启用半透明背景//也可以实现背景穿透!!!
resize(qApp->primaryScreen()->availableSize());
timer = new QTimer(this);
timer->callOnTimeout([=](){
if(qApp->focusWidget() == nullptr) //焦点离开本程序后关闭
this->close();
KeyState::clearLock();
if(KeyState::isRelease(VK_MBUTTON)){ //有时qApp检测不到中键松开(比如划过btn开启Menu,再移开关闭Menu后,将鼠标移出窗体)
QMouseEvent event(QEvent::MouseButtonRelease, QPointF(), Qt::MidButton, Qt::MidButton, Qt::NoModifier);
qApp->sendEvent(this, &event); //同步,执行任意一个event后,close,不会重复处理event
return;
}
QWidget* wgt = qApp->widgetAt(QCursor::pos());
if(!wgt || wgt->inherits("ToolMenu")){ //鼠标离开时关闭Menu ToolMenu为托底透明层 nullptr为窗体外
for(auto btn: qAsConst(btnMenuList))
btn->menu()->close();
}
if(wgt && wgt->inherits("QToolButton")){ //wgt->metaObject()->className() == QStringLiteral("QToolButton")
QToolButton* btn = static_cast<QToolButton*>(wgt);
btn->setStyleSheet("background-color:rgb(29,84,92);");
if(lastHoverBtn && lastHoverBtn != btn)
lastHoverBtn->setStyleSheet("");
lastHoverBtn = btn;
}else if(lastHoverBtn)
lastHoverBtn->setStyleSheet("");
});
timer->setInterval(50);
}
ToolMenu::~ToolMenu()
{
delete ui;
}
QToolButton* ToolMenu::addMenu(QMenu *menu, const QPoint &offset)
{
Q_ASSERT(menu);
QToolButton* btn = new QToolButton(this);
//if(!menu->actions().isEmpty()){ // empty也得接受 考虑AudioMenu一开始就是NULL aboutToShow才开始加载
btnMenuList << btn;
btn->setMenu(menu);
//}
btn->setText(menu->title());
btn->adjustSize(); //很重要 否则size不正确
setCenter(btn, this->rect().center() + offset);
return btn;
}
void ToolMenu::setBtnText(QToolButton *btn, const QString &text)
{
Q_ASSERT(btn);
QPoint offset = btn->geometry().center(); //可以直接获取 无需传入参数 省 都可以省
btn->setText(text);
btn->adjustSize(); //很重要 否则size不正确
setCenter(btn, this->rect().center() + offset);
}
QToolButton* ToolMenu::addAction(const QString &text, const QPoint &offset, std::function<void ()> todo)
{
QToolButton* btn = new QToolButton(this);
connect(btn, &QToolButton::clicked, todo);
btn->setText(text);
btn->adjustSize(); //很重要 否则size不正确
setCenter(btn, rect().center() + offset);
return btn;
}
QLabel *ToolMenu::addLabelTip(QToolButton *target, const QString &text, TipAlign align, int margin)
{
QLabel* label = new QLabel("", this);
label->setAlignment(Qt::AlignCenter);
setLabelTip(target, label, text, align, margin);
return label;
}
void ToolMenu::setLabelTip(QToolButton *target, QLabel *label, const QString &text, TipAlign align, int margin)
{
label->setText(text);
label->adjustSize();
QPoint offset;
switch (align) {
case UP:
offset = -QPoint(0, target->height()/2 + label->height()/2 + margin);
break;
case DOWN:
offset = QPoint(0, target->height()/2 + label->height()/2 + margin);
break;
case LEFT:
offset = -QPoint(target->width()/2 + label->width()/2 + margin, 0);
break;
case RIGHT:
offset = QPoint(target->width()/2 + label->width()/2 + margin, 0);
break;
}
setCenter(label, target->geometry().center() + offset);
}
void ToolMenu::setCenter(QWidget *wgt, const QPoint ¢er)
{
auto rect = wgt->geometry();
rect.moveCenter(center);
wgt->move(rect.topLeft());
}
bool ToolMenu::eventFilter(QObject *watched, QEvent *event)
{
// qDebug() << watched << event;
if((event->type() == QEvent::MouseButtonRelease) ||
(event->type() == QEvent::KeyRelease && static_cast<QKeyEvent*>(event)->key() == Qt::Key_Shift)){
qDebug() << "#about to close tMenu:" << watched << event;
QWidget* wgt = qApp->widgetAt(QCursor::pos());
if(wgt && wgt->inherits("QMenu")){
QMenu* menu = static_cast<QMenu*>(wgt);
QAction* act = menu->actionAt(menu->mapFromGlobal(QCursor::pos()));
if(act){
// qDebug() << "Act" << act->text();
act->trigger();
}
}else if(wgt && wgt->inherits("QToolButton")){
QToolButton *btn = static_cast<QToolButton*>(wgt);
if(btn->menu() == nullptr)
btn->click();
}
this->close();
return true;
}
if(event->type() == QEvent::MouseMove){
QWidget* wgt = qApp->widgetAt(QCursor::pos());
if(wgt && wgt->inherits("QToolButton")){
QToolButton* btn = static_cast<QToolButton*>(wgt);
for(auto _btn: qAsConst(btnMenuList))
if(_btn != btn)
_btn->menu()->close();
if(btn->isVisible()){
//btn->setFocus();
// QTime t = QTime::currentTime();
btn->showMenu();
//阻塞(类似exec 子事件循环) 所以会导致QTimer停止 不能放在Timer里 在menu close之后时间才会开始流动
//而在eventFilter中实际上是一个func被阻塞 qApp开启另一个func
// qDebug() << "showMenu" << t;
}
}
}
return QDialog::eventFilter(watched, event); //调用父类eventFilter 否则可能出乎意料(崩溃)
}
void ToolMenu::showEvent(QShowEvent *event)
{
Q_UNUSED(event)
qApp->installEventFilter(this);
timer->start();
setCenter(this, QCursor::pos());
emit showed();
}
void ToolMenu::closeEvent(QCloseEvent *event)
{
Q_UNUSED(event)
qApp->removeEventFilter(this);
timer->stop();
for(auto btn: qAsConst(btnMenuList))
btn->menu()->close();
if(lastHoverBtn)
lastHoverBtn->setStyleSheet("");
emit closed();
}