2008-04-09 15:32:58 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// oxygenbutton.cpp
|
|
|
|
// -------------------
|
|
|
|
// Ozone window decoration for KDE. Buttons.
|
|
|
|
// -------------------
|
|
|
|
// Copyright (c) 2006, 2007 Riccardo Iaconelli <riccardo@kde.org>
|
|
|
|
// Copyright (c) 2006, 2007 Casper Boemann <cbr@boemann.dk>
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to
|
|
|
|
// deal in the Software without restriction, including without limitation the
|
|
|
|
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
// sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
// IN THE SOFTWARE.
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include <math.h>
|
|
|
|
#include <QPainterPath>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QPen>
|
|
|
|
#include <QBitmap>
|
|
|
|
|
|
|
|
#include <kdecoration.h>
|
|
|
|
#include <kglobal.h>
|
|
|
|
#include <KColorUtils>
|
|
|
|
#include <kdebug.h>
|
|
|
|
#include <KColorScheme>
|
|
|
|
|
|
|
|
#include "oxygenclient.h"
|
|
|
|
#include "oxygenbutton.h"
|
|
|
|
#include "oxygen.h"
|
|
|
|
|
|
|
|
namespace Ozone
|
|
|
|
{
|
|
|
|
namespace Oxygen
|
|
|
|
{
|
|
|
|
// class OxygenClient;
|
|
|
|
/*
|
|
|
|
extern int BUTTONSIZE;
|
|
|
|
extern int DECOSIZE;*/
|
|
|
|
|
|
|
|
// static const int DECOSIZE = 8;
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// OxygenButton Class //
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// OxygenButton()
|
|
|
|
// ---------------
|
|
|
|
// Constructor
|
|
|
|
|
|
|
|
OxygenButton::OxygenButton(OxygenClient &parent,
|
|
|
|
const QString& tip, ButtonType type)
|
|
|
|
: KCommonDecorationButton((::ButtonType)type, &parent)
|
|
|
|
, client_(parent)
|
|
|
|
, helper_(parent.helper_)
|
|
|
|
, type_(type)
|
|
|
|
, lastmouse_(0)
|
|
|
|
, colorCacheInvalid_(true)
|
|
|
|
{
|
|
|
|
setAutoFillBackground(false);
|
|
|
|
setAttribute(Qt::WA_OpaquePaintEvent, false);
|
|
|
|
setFixedSize(OXYGEN_BUTTONSIZE, OXYGEN_BUTTONSIZE);
|
|
|
|
setCursor(Qt::ArrowCursor);
|
|
|
|
setToolTip(tip);
|
|
|
|
}
|
|
|
|
|
|
|
|
OxygenButton::~OxygenButton()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//declare function from oxygenclient.cpp
|
|
|
|
QColor reduceContrast(const QColor &c0, const QColor &c1, double t);
|
|
|
|
|
|
|
|
|
|
|
|
QColor OxygenButton::buttonDetailColor(const QPalette &palette)
|
|
|
|
{
|
|
|
|
if (client_.isActive())
|
|
|
|
return palette.color(QPalette::Active, QPalette::ButtonText);
|
|
|
|
else {
|
|
|
|
if (colorCacheInvalid_) {
|
|
|
|
QColor ab = palette.color(QPalette::Active, QPalette::Button);
|
|
|
|
QColor af = palette.color(QPalette::Active, QPalette::ButtonText);
|
|
|
|
QColor nb = palette.color(QPalette::Inactive, QPalette::Button);
|
|
|
|
QColor nf = palette.color(QPalette::Inactive, QPalette::ButtonText);
|
|
|
|
|
|
|
|
colorCacheInvalid_ = false;
|
|
|
|
cachedButtonDetailColor_ = reduceContrast(nb, nf, qMax(qreal(2.5), KColorUtils::contrastRatio(ab, KColorUtils::mix(ab, af, 0.4))));
|
|
|
|
}
|
|
|
|
return cachedButtonDetailColor_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// sizeHint()
|
|
|
|
// ----------
|
|
|
|
// Return size hint
|
|
|
|
|
|
|
|
QSize OxygenButton::sizeHint() const
|
|
|
|
{
|
|
|
|
return QSize(OXYGEN_BUTTONSIZE, OXYGEN_BUTTONSIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// enterEvent()
|
|
|
|
// ------------
|
|
|
|
// Mouse has entered the button
|
|
|
|
|
|
|
|
void OxygenButton::enterEvent(QEvent *e)
|
|
|
|
{
|
|
|
|
KCommonDecorationButton::enterEvent(e);
|
|
|
|
if (status_ != Oxygen::Pressed) {
|
|
|
|
status_ = Oxygen::Hovered;
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// leaveEvent()
|
|
|
|
// ------------
|
|
|
|
// Mouse has left the button
|
|
|
|
|
|
|
|
void OxygenButton::leaveEvent(QEvent *e)
|
|
|
|
{
|
|
|
|
KCommonDecorationButton::leaveEvent(e);
|
|
|
|
// if we wanted to do mouseovers, we would keep track of it here
|
|
|
|
status_ = Oxygen::Normal;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2008-08-05 12:46:47 +00:00
|
|
|
// mousePressEvent()
|
2008-04-09 15:32:58 +00:00
|
|
|
// ------------
|
|
|
|
// Mouse has pressed the button
|
2008-08-05 12:46:47 +00:00
|
|
|
|
|
|
|
void OxygenButton::mousePressEvent(QMouseEvent *e)
|
2008-04-09 15:32:58 +00:00
|
|
|
{
|
|
|
|
status_ = Oxygen::Pressed;
|
|
|
|
update();
|
2008-08-05 12:46:47 +00:00
|
|
|
|
|
|
|
KCommonDecorationButton::mousePressEvent(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// mouseReleaseEvent()
|
|
|
|
// ------------
|
|
|
|
// Mouse has released the button
|
|
|
|
|
|
|
|
void OxygenButton::mouseReleaseEvent(QMouseEvent *e)
|
|
|
|
{
|
|
|
|
status_ = Oxygen::Normal;
|
|
|
|
update();
|
|
|
|
|
|
|
|
KCommonDecorationButton::mouseReleaseEvent(e);
|
2008-04-09 15:32:58 +00:00
|
|
|
}
|
2008-08-05 12:46:47 +00:00
|
|
|
|
2008-04-09 15:32:58 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
// drawButton()
|
|
|
|
// ------------
|
|
|
|
// Draw the button
|
|
|
|
|
|
|
|
void OxygenButton::paintEvent(QPaintEvent *)
|
|
|
|
{
|
|
|
|
QPainter painter(this);
|
|
|
|
QPalette pal = palette(); // de-const-ify
|
|
|
|
|
2008-05-07 17:08:17 +00:00
|
|
|
|
2008-05-07 17:08:38 +00:00
|
|
|
// Set palette to the right group.
|
|
|
|
// TODO - fix KWin to do this for us :-).
|
|
|
|
if (client_.isActive())
|
|
|
|
pal.setCurrentColorGroup(QPalette::Active);
|
|
|
|
else
|
|
|
|
pal.setCurrentColorGroup(QPalette::Inactive);
|
|
|
|
|
|
|
|
// widget->window()setPalette(pal);
|
|
|
|
|
2008-05-07 17:11:00 +00:00
|
|
|
if( OxygenFactory::blendTitlebarColors())
|
|
|
|
helper_.renderWindowBackground(&painter, this->rect(), this, pal);
|
|
|
|
else {
|
|
|
|
QPalette pal2( pal );
|
|
|
|
pal2.setColor( window()->backgroundRole(), client_.options()->color(
|
|
|
|
KDecorationDefines::ColorTitleBar, client_.isActive()));
|
|
|
|
helper_.renderWindowBackground(&painter, this->rect(), this, pal2);
|
|
|
|
}
|
2008-05-07 17:08:17 +00:00
|
|
|
|
2008-04-09 15:32:58 +00:00
|
|
|
if (type_ == ButtonMenu) {
|
|
|
|
// we paint the mini icon (which is 16 pixels high)
|
|
|
|
int dx = (width() - 16) / 2;
|
|
|
|
int dy = (height() - 16) / 2;
|
|
|
|
painter.drawPixmap(dx, dy, client_.icon().pixmap(16));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(client_.maximizeMode() == OxygenClient::MaximizeRestore)
|
|
|
|
painter.translate(0,-1);
|
|
|
|
|
2008-04-09 15:48:12 +00:00
|
|
|
QColor bg = helper_.backgroundTopColor(OxygenFactory::blendTitlebarColors()?pal.window().color()
|
|
|
|
:client_.options()->color(KDecorationDefines::ColorTitleBar,client_.isActive()));
|
2008-04-09 15:32:58 +00:00
|
|
|
|
2008-05-26 19:22:19 +00:00
|
|
|
QColor color = buttonDetailColor(pal);
|
|
|
|
if(status_ == Oxygen::Hovered || status_ == Oxygen::Pressed) {
|
|
|
|
if(type_ == ButtonClose)
|
|
|
|
color = KColorScheme(pal.currentColorGroup()).foreground(KColorScheme::NegativeText).color();
|
|
|
|
else
|
|
|
|
color = KColorScheme(pal.currentColorGroup()).decoration(KColorScheme::HoverColor).color();
|
2008-04-09 15:32:58 +00:00
|
|
|
|
2008-05-26 19:22:19 +00:00
|
|
|
}
|
|
|
|
QLinearGradient lg = helper_.decoGradient(QRect(4,4,13,13), color);
|
2008-08-05 12:46:47 +00:00
|
|
|
QColor bt = OxygenFactory::blendTitlebarColors()?pal.window().color()
|
2008-04-09 15:48:12 +00:00
|
|
|
:client_.options()->color(KDecorationDefines::ColorButtonBg,client_.isActive());
|
2008-05-26 19:22:19 +00:00
|
|
|
painter.drawPixmap(0, 0, helper_.windecoButton(bt, status_ == Oxygen::Pressed));
|
|
|
|
|
|
|
|
if (client_.isActive()) {
|
|
|
|
painter.setRenderHints(QPainter::Antialiasing);
|
|
|
|
painter.setBrush(Qt::NoBrush);
|
|
|
|
painter.setPen(QPen(lg, 2.2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
|
|
|
|
drawIcon(&painter, pal, type_);
|
2008-04-09 15:32:58 +00:00
|
|
|
}
|
2008-05-26 19:22:19 +00:00
|
|
|
else {
|
|
|
|
// outlined mode
|
|
|
|
QPixmap pixmap(size());
|
|
|
|
pixmap.fill(Qt::transparent);
|
|
|
|
QPainter pp(&pixmap);
|
|
|
|
pp.setRenderHints(QPainter::Antialiasing);
|
|
|
|
pp.setBrush(Qt::NoBrush);
|
|
|
|
pp.setPen(QPen(color, 3.0, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
|
|
|
|
drawIcon(&pp, pal, type_);
|
|
|
|
|
|
|
|
pp.setCompositionMode(QPainter::CompositionMode_DestinationOut);
|
|
|
|
pp.setPen(QPen(color, 1.0, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
|
|
|
|
drawIcon(&pp, pal, type_);
|
|
|
|
|
|
|
|
painter.drawPixmap(QPoint(0,0), pixmap);
|
|
|
|
}
|
|
|
|
}
|
2008-04-09 15:32:58 +00:00
|
|
|
|
2008-05-26 19:22:19 +00:00
|
|
|
void OxygenButton::drawIcon(QPainter *p, QPalette &pal, ButtonType &type)
|
|
|
|
{
|
|
|
|
switch(type)
|
2008-04-09 15:32:58 +00:00
|
|
|
{
|
|
|
|
case ButtonSticky:
|
2008-08-05 12:42:02 +00:00
|
|
|
if(isChecked()) {
|
|
|
|
QPen newPen = p->pen();
|
|
|
|
newPen.setColor(KColorScheme(pal.currentColorGroup()).decoration(KColorScheme::HoverColor).color());
|
|
|
|
p->setPen(newPen);
|
|
|
|
}
|
2008-05-26 19:22:19 +00:00
|
|
|
p->drawPoint(QPointF(10.5,10.5));
|
2008-04-09 15:32:58 +00:00
|
|
|
break;
|
|
|
|
case ButtonHelp:
|
2008-05-26 19:22:19 +00:00
|
|
|
p->translate(1.5, 1.5);
|
|
|
|
p->drawArc(7,5,4,4,135*16, -180*16);
|
|
|
|
p->drawArc(9,8,4,4,135*16,45*16);
|
|
|
|
p->drawPoint(9,12);
|
2008-04-09 15:32:58 +00:00
|
|
|
break;
|
|
|
|
case ButtonMin:
|
2008-05-26 19:22:19 +00:00
|
|
|
p->drawLine(QPointF( 7.5, 9.5), QPointF(10.5,12.5));
|
|
|
|
p->drawLine(QPointF(10.5,12.5), QPointF(13.5, 9.5));
|
2008-04-09 15:32:58 +00:00
|
|
|
break;
|
|
|
|
case ButtonMax:
|
|
|
|
switch(client_.maximizeMode())
|
|
|
|
{
|
|
|
|
case OxygenClient::MaximizeRestore:
|
|
|
|
case OxygenClient::MaximizeVertical:
|
|
|
|
case OxygenClient::MaximizeHorizontal:
|
2008-05-26 19:22:19 +00:00
|
|
|
p->drawLine(QPointF( 7.5,11.5), QPointF(10.5, 8.5));
|
|
|
|
p->drawLine(QPointF(10.5, 8.5), QPointF(13.5,11.5));
|
2008-04-09 15:32:58 +00:00
|
|
|
break;
|
|
|
|
case OxygenClient::MaximizeFull:
|
|
|
|
{
|
2008-05-26 19:22:19 +00:00
|
|
|
p->translate(1.5, 1.5);
|
|
|
|
//p->setBrush(lg);
|
2008-04-09 15:32:58 +00:00
|
|
|
QPoint points[4] = {QPoint(9, 6), QPoint(12, 9), QPoint(9, 12), QPoint(6, 9)};
|
|
|
|
//QPoint points[4] = {QPoint(9, 5), QPoint(13, 9), QPoint(9, 13), QPoint(5, 9)};
|
2008-05-26 19:22:19 +00:00
|
|
|
p->drawPolygon(points, 4);
|
2008-08-05 12:47:45 +00:00
|
|
|
p->translate(-1.5, -1.5);
|
2008-04-09 15:32:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ButtonClose:
|
2008-05-26 19:22:19 +00:00
|
|
|
p->drawLine(QPointF( 7.5,7.5), QPointF(13.5,13.5));
|
|
|
|
p->drawLine(QPointF(13.5,7.5), QPointF( 7.5,13.5));
|
2008-04-09 15:32:58 +00:00
|
|
|
break;
|
|
|
|
case ButtonAbove:
|
|
|
|
if(isChecked()) {
|
2008-05-26 19:22:19 +00:00
|
|
|
QPen newPen = p->pen();
|
2008-04-09 15:32:58 +00:00
|
|
|
newPen.setColor(KColorScheme(pal.currentColorGroup()).decoration(KColorScheme::HoverColor).color());
|
2008-05-26 19:22:19 +00:00
|
|
|
p->setPen(newPen);
|
2008-04-09 15:32:58 +00:00
|
|
|
}
|
|
|
|
|
2008-05-26 19:22:19 +00:00
|
|
|
p->drawLine(QPointF( 7.5,14), QPointF(10.5,11));
|
|
|
|
p->drawLine(QPointF(10.5,11), QPointF(13.5,14));
|
|
|
|
p->drawLine(QPointF( 7.5,10), QPointF(10.5, 7));
|
|
|
|
p->drawLine(QPointF(10.5, 7), QPointF(13.5,10));
|
2008-04-09 15:32:58 +00:00
|
|
|
break;
|
|
|
|
case ButtonBelow:
|
|
|
|
if(isChecked()) {
|
2008-05-26 19:22:19 +00:00
|
|
|
QPen newPen = p->pen();
|
2008-04-09 15:32:58 +00:00
|
|
|
newPen.setColor(KColorScheme(pal.currentColorGroup()).decoration(KColorScheme::HoverColor).color());
|
2008-05-26 19:22:19 +00:00
|
|
|
p->setPen(newPen);
|
2008-04-09 15:32:58 +00:00
|
|
|
}
|
|
|
|
|
2008-05-26 19:22:19 +00:00
|
|
|
p->drawLine(QPointF( 7.5,11), QPointF(10.5,14));
|
|
|
|
p->drawLine(QPointF(10.5,14), QPointF(13.5,11));
|
|
|
|
p->drawLine(QPointF( 7.5, 7), QPointF(10.5,10));
|
|
|
|
p->drawLine(QPointF(10.5,10), QPointF(13.5, 7));
|
2008-04-09 15:32:58 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} //namespace Oxygen
|
|
|
|
} //namespace Ozone
|