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 "menubar.h" |
27 | #include "shadows.h" |
28 | |
29 | #include <QGraphicsLinearLayout> |
30 | #include <QPainter> |
31 | #include <QMenu> |
32 | #include <QDesktopWidget> |
33 | #include <QGraphicsDropShadowEffect> |
34 | |
35 | #include <KWindowSystem> |
36 | #include <Plasma/FrameSvg> |
37 | #include <Plasma/Theme> |
38 | #include <Plasma/WindowEffects> |
39 | #include <KApplication> |
40 | |
41 | MenuBar::() |
42 | : QGraphicsView(), |
43 | m_hideTimer(new QTimer(this)), |
44 | m_background(new Plasma::FrameSvg(this)), |
45 | m_shadows(new Shadows(this)), |
46 | m_scene(new QGraphicsScene(this)), |
47 | m_container(new MenuWidget(this)) |
48 | { |
49 | qreal left, top, right, bottom; |
50 | |
51 | //Setup the window properties |
52 | setWindowFlags(Qt::Tool|Qt::X11BypassWindowManagerHint|Qt::WindowStaysOnTopHint); |
53 | setAttribute(Qt::WA_TranslucentBackground); |
54 | KWindowSystem::setType(winId(), NET::Dock); |
55 | setFrameStyle(QFrame::NoFrame); |
56 | viewport()->setAutoFillBackground(false); |
57 | setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
58 | setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
59 | |
60 | //Setup the widgets |
61 | m_background->setImagePath("widgets/tooltip" ); |
62 | m_background->setEnabledBorders(Plasma::FrameSvg::BottomBorder|Plasma::FrameSvg::LeftBorder|Plasma::FrameSvg::RightBorder); |
63 | |
64 | m_container->initLayout(); |
65 | |
66 | m_scene->addItem(m_container); |
67 | |
68 | setScene(m_scene); |
69 | |
70 | m_background->getMargins(left, top, right, bottom); |
71 | m_container->layout()->setContentsMargins(left, top, right, bottom); |
72 | |
73 | resize(sizeHint()); |
74 | |
75 | connect(m_container, SIGNAL(aboutToHide()), this, SLOT(slotAboutToHide())); |
76 | connect(m_container, SIGNAL(needResize()), this, SIGNAL(needResize())); |
77 | connect(m_hideTimer, SIGNAL(timeout()), this, SLOT(slotAboutToHide())); |
78 | |
79 | connect(KWindowSystem::self(), SIGNAL(compositingChanged(bool)), this, SLOT(slotCompositingChanged(bool))); |
80 | } |
81 | |
82 | MenuBar::() |
83 | { |
84 | } |
85 | |
86 | QSize MenuBar::() const |
87 | { |
88 | QSizeF size = m_container->minimumSize(); |
89 | return QSize(size.width(), size.height() - m_container->contentBottomMargin()); |
90 | } |
91 | |
92 | void MenuBar::() |
93 | { |
94 | // Add shadow for better readability |
95 | if (! Plasma::WindowEffects::isEffectAvailable(Plasma::WindowEffects::BlurBehind)) { |
96 | QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(); |
97 | shadow->setBlurRadius(5); |
98 | shadow->setOffset(QPointF(1, 1)); |
99 | shadow->setColor(Plasma::Theme::defaultTheme()->color(Plasma::Theme::BackgroundColor)); |
100 | setGraphicsEffect(shadow); |
101 | } else { |
102 | setGraphicsEffect(0); |
103 | } |
104 | m_hideTimer->start(1000); |
105 | QGraphicsView::show(); |
106 | |
107 | } |
108 | |
109 | void MenuBar::() |
110 | { |
111 | emit aboutToHide(); |
112 | m_hideTimer->stop(); |
113 | QGraphicsView::hide(); |
114 | } |
115 | |
116 | void MenuBar::() |
117 | { |
118 | if (m_container->aMenuIsVisible()) { // MenuBar::m_hideTimer |
119 | m_hideTimer->stop(); // menu is visible, menubar will be hidden by another aboutToHide() signal |
120 | } |
121 | else if (!cursorInMenuBar()) { //MenuWidget::AboutToHide signal |
122 | hide(); |
123 | } else if (!m_hideTimer->isActive()){ //use click on menubar button while a popup was shown |
124 | m_hideTimer->start(1000); |
125 | } |
126 | } |
127 | |
128 | void MenuBar::(bool) |
129 | { |
130 | updateMask(); |
131 | } |
132 | |
133 | bool MenuBar::() |
134 | { |
135 | return QRect(pos(), size()).contains(QCursor::pos()); |
136 | } |
137 | |
138 | void MenuBar::(QPainter *painter, const QRectF &/*rectF*/) |
139 | { |
140 | painter->save(); |
141 | painter->setCompositionMode(QPainter::CompositionMode_Source); |
142 | m_background->paintFrame(painter); |
143 | painter->restore(); |
144 | } |
145 | |
146 | void MenuBar::(QResizeEvent*) |
147 | { |
148 | m_background->resizeFrame(size()); |
149 | m_scene->setSceneRect(0, 0, width(), height()); |
150 | updateMask(); |
151 | } |
152 | |
153 | void MenuBar::() |
154 | { |
155 | // Enable the mask only when compositing is disabled; |
156 | // As this operation is quite slow, it would be nice to find some |
157 | // way to workaround it for no-compositing users. |
158 | if (KWindowSystem::compositingActive()) { |
159 | clearMask(); |
160 | Plasma::WindowEffects::overrideShadow(winId(), true); |
161 | Plasma::WindowEffects::enableBlurBehind(winId(), true, m_background->mask()); |
162 | m_shadows->addWindow(this, Plasma::FrameSvg::BottomBorder|Plasma::FrameSvg::LeftBorder|Plasma::FrameSvg::RightBorder); |
163 | } else { |
164 | setMask(m_background->mask()); |
165 | } |
166 | } |