1 //////////////////////////////////////////////////////////////////////////////
4 // Oxygen window decoration for KDE. Buttons.
6 // Copyright (c) 2006, 2007 Riccardo Iaconelli <riccardo@kde.org>
7 // Copyright (c) 2006, 2007 Casper Boemann <cbr@boemann.dk>
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to
11 // deal in the Software without restriction, including without limitation the
12 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
13 // sell copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 //////////////////////////////////////////////////////////////////////////////
28 #include <QPainterPath>
33 #include <kdecoration.h>
35 #include <KColorUtils>
37 #include <KColorScheme>
39 #include "oxygenclient.h"
40 #include "oxygenbutton.h"
45 // class OxygenClient;
47 extern int BUTTONSIZE;
48 extern int DECOSIZE;*/
50 // static const int DECOSIZE = 8;
51 //////////////////////////////////////////////////////////////////////////////
52 // OxygenButton Class //
53 //////////////////////////////////////////////////////////////////////////////
55 //////////////////////////////////////////////////////////////////////////////
60 OxygenButton::OxygenButton(OxygenClient
&parent
,
61 const QString
& tip
, ButtonType type
)
62 : KCommonDecorationButton((::ButtonType
)type
, &parent
)
64 , helper_(parent
.helper_
)
67 , colorCacheInvalid_(true)
69 setAutoFillBackground(false);
70 setAttribute(Qt::WA_OpaquePaintEvent
, false);
71 setFixedSize(OXYGEN_BUTTONSIZE
, OXYGEN_BUTTONSIZE
);
72 setCursor(Qt::ArrowCursor
);
76 OxygenButton::~OxygenButton()
80 //declare function from oxygenclient.cpp
81 QColor
reduceContrast(const QColor
&c0
, const QColor
&c1
, double t
);
84 QColor
OxygenButton::buttonDetailColor(const QPalette
&palette
)
86 if (client_
.isActive())
87 return palette
.color(QPalette::Active
, QPalette::ButtonText
);
89 if (colorCacheInvalid_
) {
90 QColor ab
= palette
.color(QPalette::Active
, QPalette::Button
);
91 QColor af
= palette
.color(QPalette::Active
, QPalette::ButtonText
);
92 QColor nb
= palette
.color(QPalette::Inactive
, QPalette::Button
);
93 QColor nf
= palette
.color(QPalette::Inactive
, QPalette::ButtonText
);
95 colorCacheInvalid_
= false;
96 cachedButtonDetailColor_
= reduceContrast(nb
, nf
, qMax(qreal(2.5), KColorUtils::contrastRatio(ab
, KColorUtils::mix(ab
, af
, 0.4))));
98 return cachedButtonDetailColor_
;
102 //////////////////////////////////////////////////////////////////////////////
107 QSize
OxygenButton::sizeHint() const
109 return QSize(OXYGEN_BUTTONSIZE
, OXYGEN_BUTTONSIZE
);
112 //////////////////////////////////////////////////////////////////////////////
115 // Mouse has entered the button
117 void OxygenButton::enterEvent(QEvent
*e
)
119 KCommonDecorationButton::enterEvent(e
);
120 if (status_
!= Oxygen::Pressed
) {
121 status_
= Oxygen::Hovered
;
126 //////////////////////////////////////////////////////////////////////////////
129 // Mouse has left the button
131 void OxygenButton::leaveEvent(QEvent
*e
)
133 KCommonDecorationButton::leaveEvent(e
);
134 // if we wanted to do mouseovers, we would keep track of it here
135 status_
= Oxygen::Normal
;
139 //////////////////////////////////////////////////////////////////////////////
142 // Mouse has pressed the button
144 void OxygenButton::pressSlot()
146 status_
= Oxygen::Pressed
;
149 //////////////////////////////////////////////////////////////////////////////
154 void OxygenButton::paintEvent(QPaintEvent
*)
156 QPainter
painter(this);
157 QPalette pal
= palette(); // de-const-ify
159 if (type_
== ButtonMenu
) {
160 // we paint the mini icon (which is 16 pixels high)
161 int dx
= (width() - 16) / 2;
162 int dy
= (height() - 16) / 2;
163 painter
.drawPixmap(dx
, dy
, client_
.icon().pixmap(16));
167 // Set palette to the right group.
168 // TODO - fix KWin to do this for us :-).
169 if (client_
.isActive())
170 pal
.setCurrentColorGroup(QPalette::Active
);
172 pal
.setCurrentColorGroup(QPalette::Inactive
);
174 if(client_
.maximizeMode() == OxygenClient::MaximizeRestore
)
175 painter
.translate(0,-1);
177 QColor bg
= helper_
.backgroundTopColor(pal
.window());
179 QLinearGradient lg
= helper_
.decoGradient(QRect(4,4,13,13), buttonDetailColor(pal
));
181 if(status_
== Oxygen::Hovered
) {
182 if(type_
== ButtonClose
) {
183 QColor color
= KColorScheme(pal
.currentColorGroup()).foreground(KColorScheme::NegativeText
).color();
184 lg
= helper_
.decoGradient(QRect(4,4,13,13), color
);
185 painter
.drawPixmap(0, 0, helper_
.windecoButtonFocused(pal
.button(), color
,7));
188 QColor color
= KColorScheme(pal
.currentColorGroup()).decoration(KColorScheme::HoverColor
).color();
189 painter
.drawPixmap(0, 0, helper_
.windecoButtonFocused(pal
.button(), color
, 7));
193 painter
.drawPixmap(0, 0, helper_
.windecoButton(pal
.button()));
195 painter
.setRenderHints(QPainter::Antialiasing
);
196 painter
.setBrush(Qt::NoBrush
);
197 painter
.setPen(QPen(lg
, 2.2, Qt::SolidLine
, Qt::RoundCap
, Qt::RoundJoin
));
201 painter
.drawPoint(QPointF(10.5,10.5));
204 painter
.translate(1.5, 1.5);
205 painter
.drawArc(7,5,4,4,135*16, -180*16);
206 painter
.drawArc(9,8,4,4,135*16,45*16);
207 painter
.drawPoint(9,12);
210 painter
.drawLine(QPointF( 7.5, 9.5), QPointF(10.5,12.5));
211 painter
.drawLine(QPointF(10.5,12.5), QPointF(13.5, 9.5));
214 switch(client_
.maximizeMode())
216 case OxygenClient::MaximizeRestore
:
217 case OxygenClient::MaximizeVertical
:
218 case OxygenClient::MaximizeHorizontal
:
219 painter
.drawLine(QPointF( 7.5,11.5), QPointF(10.5, 8.5));
220 painter
.drawLine(QPointF(10.5, 8.5), QPointF(13.5,11.5));
222 case OxygenClient::MaximizeFull
:
224 painter
.translate(1.5, 1.5);
225 //painter.setBrush(lg);
226 QPoint points
[4] = {QPoint(9, 6), QPoint(12, 9), QPoint(9, 12), QPoint(6, 9)};
227 //QPoint points[4] = {QPoint(9, 5), QPoint(13, 9), QPoint(9, 13), QPoint(5, 9)};
228 painter
.drawPolygon(points
, 4);
234 painter
.drawLine(QPointF( 7.5,7.5), QPointF(13.5,13.5));
235 painter
.drawLine(QPointF(13.5,7.5), QPointF( 7.5,13.5));
239 QPen newPen
= painter
.pen();
240 newPen
.setColor(KColorScheme(pal
.currentColorGroup()).decoration(KColorScheme::HoverColor
).color());
241 painter
.setPen(newPen
);
244 painter
.drawLine(QPointF( 7.5,14), QPointF(10.5,11));
245 painter
.drawLine(QPointF(10.5,11), QPointF(13.5,14));
246 painter
.drawLine(QPointF( 7.5,10), QPointF(10.5, 7));
247 painter
.drawLine(QPointF(10.5, 7), QPointF(13.5,10));
251 QPen newPen
= painter
.pen();
252 newPen
.setColor(KColorScheme(pal
.currentColorGroup()).decoration(KColorScheme::HoverColor
).color());
253 painter
.setPen(newPen
);
256 painter
.drawLine(QPointF( 7.5,11), QPointF(10.5,14));
257 painter
.drawLine(QPointF(10.5,14), QPointF(13.5,11));
258 painter
.drawLine(QPointF( 7.5, 7), QPointF(10.5,10));
259 painter
.drawLine(QPointF(10.5,10), QPointF(13.5, 7));