1/*
2 This file is part of the KDE project.
3
4 Copyright (c) 2011 Lionel Chauvin <megabigbug@yahoo.fr>
5 Copyright (c) 2011,2012 Cédric Bellegarde <gnumdk@gmail.com>
6
7 Permission is hereby granted, free of charge, to any person obtaining a
8 copy of this software and associated documentation files (the "Software"),
9 to deal in the Software without restriction, including without limitation
10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 and/or sell copies of the Software, and to permit persons to whom the
12 Software is furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 DEALINGS IN THE SOFTWARE.
24*/
25
26#include "topmenubar.h"
27#include "glowbar.h"
28
29//KDE
30#include <Plasma/Svg>
31#include <KWindowSystem>
32
33// Qt
34#include <QMenu>
35#include <QTimer>
36#include <QDebug>
37#include <QApplication>
38#include <QPropertyAnimation>
39#include <QDesktopWidget>
40
41TopMenuBar::TopMenuBar()
42 : MenuBar(),
43 m_prevCursorPos(-1, -1),
44 m_mouseTracker(new QTimer(this)),
45 m_hideGlowTimer(new QTimer(this)),
46 m_glowBar(new GlowBar())
47{
48 connect(this, SIGNAL(aboutToHide()), this, SLOT(slotAboutToHide()));
49 connect(m_mouseTracker, SIGNAL(timeout()), this, SLOT(slotMouseTracker()));
50 connect(m_hideGlowTimer, SIGNAL(timeout()), this, SLOT(slotHideGlowBar()));
51}
52
53TopMenuBar::~TopMenuBar()
54{
55 delete m_mouseTracker;
56 delete m_hideGlowTimer;
57 hideGlowBar();
58 delete m_glowBar;
59}
60
61void TopMenuBar::enableMouseTracking(bool enable)
62{
63 if (enable) {
64 if (!cursorInMenuBar()) {
65 showGlowBar();
66 }
67 m_mouseTracker->start(250);
68 } else {
69 hideGlowBar();
70 m_mouseTracker->stop();
71 }
72}
73
74void TopMenuBar::updateSize()
75{
76 // Enable mouse tracking on resize if needed
77 if (!m_mouseTracker->isActive() && !cursorInMenuBar()) {
78 enableMouseTracking();
79 }
80 resize(sizeHint());
81}
82
83void TopMenuBar::move(QPoint p)
84{
85 MenuBar::move(p);
86 if (m_glowBar) {
87 m_glowBar->move(p);
88 m_glowBar->setPixmap(triggerRect().topLeft(), triggerRect().width());
89 }
90}
91
92bool TopMenuBar::cursorInMenuBar()
93{
94 if (m_mouseTracker->isActive()) {
95 return triggerRect().contains(QCursor::pos());
96 } else {
97 return MenuBar::cursorInMenuBar();
98 }
99}
100
101void TopMenuBar::slotAboutToHide()
102{
103 enableMouseTracking();
104}
105
106void TopMenuBar::slotMouseTracker()
107{
108 QPoint cursorPos = QCursor::pos();
109
110 // reset timer
111 if (cursorPos != m_prevCursorPos && m_hideGlowTimer->isActive()) {
112 m_hideGlowTimer->stop();
113 m_hideGlowTimer->start(10000);
114 }
115
116 if (cursorInMenuBar()) { // show menubar
117 m_mouseTracker->stop();
118 hideGlowBar();
119 show();
120 } else if(cursorPos != m_prevCursorPos) { // change glowbar opacity
121 qreal opacity = glowBarOpacity();
122 QPropertyAnimation *anim = new QPropertyAnimation(m_glowBar, "windowOpacity");
123 anim->setStartValue(m_glowBar->windowOpacity());
124 anim->setEndValue(opacity);
125 anim->setDuration(200);
126 anim->start(QAbstractAnimation::DeleteWhenStopped);
127 // Show menubar if auto hidden
128 if (!m_glowBar->isVisible()) {
129 m_glowBar->show();
130 }
131 }
132 m_prevCursorPos = cursorPos;
133}
134
135void TopMenuBar::slotHideGlowBar()
136{
137 if (m_prevCursorPos == QCursor::pos()) {
138 hideGlowBar();
139 } else {
140 m_hideGlowTimer->start(10000);
141 }
142}
143
144void TopMenuBar::showGlowBar()
145{
146 if (m_glowBar) {
147 m_hideGlowTimer->start(10000);
148 m_glowBar->setWindowOpacity(glowBarOpacity());
149 m_glowBar->show();
150 }
151}
152
153void TopMenuBar::hideGlowBar()
154{
155 if (m_glowBar) {
156 m_glowBar->hide();
157 }
158}
159
160qreal TopMenuBar::glowBarOpacity()
161{
162 QPoint cursorPos = QCursor::pos();
163 QDesktopWidget *desktop = QApplication::desktop();
164 int screen = desktop->screenNumber(cursorPos);
165 QRect desktopRect = desktop->availableGeometry(screen);
166 return 1.0 - ((cursorPos.y() - desktopRect.y())/qreal(desktopRect.height())*2.0);
167}
168
169QRect TopMenuBar::triggerRect()
170{
171 QPoint triggerPoint = QPoint(x(), y());
172 QSize triggerSize = QSize(sizeHint().width(), 5);
173 return QRect(triggerPoint, triggerSize);
174}
175
176#include "topmenubar.moc"