/
autohidewidget.cpp
189 lines (159 loc) · 5.52 KB
/
autohidewidget.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
#include "autohidewidget.h"
#include <QTimer>
#include <QEvent>
#include <QVBoxLayout>
#include <QMouseEvent>
#include <QAction>
#include <QDebug>
#include <QMenu>
AutoHideWidget::AutoHideWidget(QWidget *parent)
: QWidget(parent)
,turned_on(false)
,auto_hide(false)
,use_animation(false)
,spacing(0)
,perc_width(100)
,activation_area(Bottom)
,internal_widget(0)
,timer(0)
{
setBackgroundRole(QPalette::Window);
setAutoFillBackground(true);
setLayoutDirection(Qt::LeftToRight);
QWidget *widget_to_watch = parent;
widget_to_watch->installEventFilter(this);
installFiliter(widget_to_watch);
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(checkUnderMouse()));
timer->setInterval(3000);
QVBoxLayout *layout = new QVBoxLayout;
layout->setSpacing(0);
layout->setMargin(0);
setLayout(layout);
}
AutoHideWidget::~AutoHideWidget(){}
void AutoHideWidget::setInternalWidget(QWidget *w){
layout()->addWidget(w);
internal_widget = w;
}
void AutoHideWidget::setHideDelay(int ms){
timer->setInterval(ms);
}
int AutoHideWidget::hideDelay(){
return timer->interval();
}
void AutoHideWidget::installFiliter(QObject *o){
QObjectList children = o->children();
for(int n=0;n < children.count();n++){
if(children[n]->isWidgetType()){
qDebug()<<"AutoHideWidget::installFilter:child name:"<<children[n]->objectName();
QWidget *w = static_cast<QWidget*>(children[n]);
w->setMouseTracking(true);
w->installEventFilter(this);
installFiliter(children[n]);
}
}
}
bool AutoHideWidget::visiblePopups() {
//qDebug() << "AutohideWidget::visiblePopups: internal_widget:" << internal_widget;
if (!internal_widget) return false;
// Check if any of the menus in the internal widget is visible
QObjectList children = internal_widget->children();
foreach(QObject * child, children) {
if (child->isWidgetType()) {
//qDebug() << "AutohideWidget::visiblePopups:" << child << "child name:" << child->objectName();
QWidget *w = static_cast<QWidget *>(child);
QList<QAction *> actions = w->actions();
foreach(QAction * action, actions) {
//qDebug() << "AutohideWidget::visiblePopups: action:" << action;
QList<QWidget *> aw = action->associatedWidgets();
//qDebug() << "AutohideWidget::visiblePopups: aw:" << aw;
QMenu * menu = 0;
foreach(QWidget * widget, aw) {
//qDebug() << "AutohideWidget::visiblePopups: widget:" << widget;
if ((menu = qobject_cast<QMenu *>(widget))) {
//qDebug() << "AutohideWidget::visiblePopups: menu:" << menu << "visible:" << menu->isVisible();
if (menu->isVisible()) return true;
}
}
menu = action->menu();
if (menu) {
//qDebug() << "AutohideWidget::visiblePopups: menu:" << menu << "visible:" << menu->isVisible();
if (menu->isVisible()) return true;
}
}
}
}
return false;
}
void AutoHideWidget::activate() {
turned_on = true;
timer->start();
}
void AutoHideWidget::deactivate() {
turned_on = false;
timer->stop();
hide();
}
void AutoHideWidget::show() {
qDebug() << "AutohideWidget::show";
resizeAndMove();
if (use_animation) {
showAnimated();
} else {
QWidget::show();
}
// Restart timer
if (timer->isActive()) timer->start();
}
void AutoHideWidget::setAutoHide(bool b) {
auto_hide = b;
}
void AutoHideWidget::checkUnderMouse() {
if (auto_hide) {
if (isVisible() && !underMouse() && !visiblePopups()) {
hide();
}
}
}
void AutoHideWidget::resizeAndMove() {
QWidget * widget = parentWidget();
int w = widget->width() * perc_width / 100;
int h = height();
resize(w, h);
int x = (widget->width() - width() ) / 2;
int y = widget->height() - height() - spacing;
move(x, y);
}
bool AutoHideWidget::eventFilter(QObject * obj, QEvent * event) {
if (turned_on) {
if (event->type() == QEvent::MouseMove) {
//qDebug() << "AutohideWidget::eventFilter: mouse move" << obj;
if (!isVisible()) {
if (activation_area == Anywhere) {
show();
} else {
QMouseEvent * mouse_event = dynamic_cast<QMouseEvent*>(event);
QWidget * parent = parentWidget();
QPoint p = parent->mapFromGlobal(mouse_event->globalPos());
//qDebug() << "AutohideWidget::eventFilter: y:" << p.y();
if (p.y() > (parent->height() - height() - spacing)) {
show();
}
}
}
}
else
if (event->type() == QEvent::MouseButtonRelease && obj == this) {
event->setAccepted(true);
return true;
}
}
return QWidget::eventFilter(obj, event);
}
void AutoHideWidget::showAnimated() {
QPoint initial_position = QPoint(pos().x(), parentWidget()->size().height());
QPoint final_position = pos();
move(initial_position);
QWidget::show();
}