move oxygen code that will be shared by the style into its own .cpp/.h

svn path=/trunk/KDE/kdebase/workspace/; revision=689747
This commit is contained in:
Matthew Woehlke 2007-07-19 01:32:16 +00:00
parent b7ae1af212
commit 1cfffc602e
4 changed files with 205 additions and 121 deletions

View file

@ -1,8 +1,9 @@
########### next target ###############
set(kwin_oxy_SRCS
oxygenclient.cpp
oxygenbutton.cpp
set(kwin_oxy_SRCS
lib/helper.cpp
oxygenclient.cpp
oxygenbutton.cpp
oxygen.cpp
definitions.cpp
)

View file

@ -0,0 +1,134 @@
/*
* Copyright 2007 Matthew Woehlke <mw_triad@users.sourceforge.net>
* Copyright 2007 Casper Boemann <cbr@boemann.dk>
* Copyright 2007 Fredrik Höglund <fredrik@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License version 2 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "helper.h"
#include <KGlobalSettings>
#include <KColorScheme>
#include <QtGui/QPainter>
// alphaBlendColors Copyright 2003 Sandro Giessl <ceebx@users.sourceforge.net>
// DEPRECATED (use KColorUtils::mix to the extent we still need such a critter)
QColor alphaBlendColors(const QColor &bgColor, const QColor &fgColor, const int a)
{
// normal button...
QRgb rgb = bgColor.rgb();
QRgb rgb_b = fgColor.rgb();
int alpha = a;
if(alpha>255) alpha = 255;
if(alpha<0) alpha = 0;
int inv_alpha = 255 - alpha;
QColor result = QColor( qRgb(qRed(rgb_b)*inv_alpha/255 + qRed(rgb)*alpha/255,
qGreen(rgb_b)*inv_alpha/255 + qGreen(rgb)*alpha/255,
qBlue(rgb_b)*inv_alpha/255 + qBlue(rgb)*alpha/255) );
return result;
}
// NOTE: OxygenStyleHelper needs to use a KConfig from its own KComponentData
// Since the ctor order causes a SEGV if we try to pass in a KConfig here from
// a KComponentData constructed in the OxygenStyleHelper ctor, we'll just keep
// one here, even though the window decoration doesn't really need it.
OxygenHelper::OxygenHelper(const QByteArray &componentName)
: _componentData(componentName, 0, KComponentData::SkipMainComponentRegistration)
{
_config = _componentData.config();
_contrast = KGlobalSettings::contrastF(_config);
m_cache.setMaxCost(64);
}
KSharedConfigPtr OxygenHelper::config() const
{
return _config;
}
QColor OxygenHelper::backgroundRadialColor(const QColor &color) const
{
return KColorScheme::shade(color, KColorScheme::LightShade, _contrast);
}
QColor OxygenHelper::backgroundTopColor(const QColor &color) const
{
return KColorScheme::shade(color, KColorScheme::MidlightShade, _contrast);
}
QColor OxygenHelper::backgroundBottomColor(const QColor &color) const
{
return KColorScheme::shade(color, KColorScheme::MidShade, _contrast - 1.1);
}
QPixmap OxygenHelper::verticalGradient(const QColor &color, int height)
{
quint64 key = (quint64(color.rgba()) << 32) | height | 0x8000;
QPixmap *pixmap = m_cache.object(key);
if (!pixmap)
{
pixmap = new QPixmap(32, height);
QLinearGradient gradient(0, 0, 0, height);
gradient.setColorAt(0.0, backgroundTopColor(color));
gradient.setColorAt(0.5, color);
gradient.setColorAt(1.0, backgroundBottomColor(color));
QPainter p(pixmap);
p.setCompositionMode(QPainter::CompositionMode_Source);
p.fillRect(pixmap->rect(), gradient);
m_cache.insert(key, pixmap);
}
return *pixmap;
}
QPixmap OxygenHelper::radialGradient(const QColor &color, int width)
{
quint64 key = (quint64(color.rgba()) << 32) | width | 0xb000;
QPixmap *pixmap = m_cache.object(key);
if (!pixmap)
{
width /= 2;
pixmap = new QPixmap(width, 64);
pixmap->fill(QColor(0,0,0,0));
QColor radialColor = backgroundRadialColor(color);
radialColor.setAlpha(255);
QRadialGradient gradient(64, 0, 64);
gradient.setColorAt(0, radialColor);
radialColor.setAlpha(101);
gradient.setColorAt(0.5, radialColor);
radialColor.setAlpha(37);
gradient.setColorAt(0.75, radialColor);
radialColor.setAlpha(0);
gradient.setColorAt(1, radialColor);
QPainter p(pixmap);
p.scale(width/128.0,1);
p.fillRect(pixmap->rect(), gradient);
m_cache.insert(key, pixmap);
}
return *pixmap;
}

View file

@ -0,0 +1,59 @@
/*
* Copyright 2007 Matthew Woehlke <mw_triad@users.sourceforge.net>
* Copyright 2007 Casper Boemann <cbr@boemann.dk>
* Copyright 2007 Fredrik Höglund <fredrik@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License version 2 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __OXYGEN_HELPER_H
#define __OXYGEN_HELPER_H
#include <KSharedConfig>
#include <KComponentData>
#include <QtGui/QColor>
#include <QtGui/QPixmap>
#include <QtCore/QCache>
// alphaBlendColors Copyright 2003 Sandro Giessl <ceebx@users.sourceforge.net>
// DEPRECATED
QColor alphaBlendColors(const QColor &backgroundColor, const QColor &foregroundColor, const int alpha);
// WARNING - OxygenHelper must be a K_GLOBAL_STATIC!
class OxygenHelper
{
public:
explicit OxygenHelper(const QByteArray &componentName);
virtual ~OxygenHelper() {}
KSharedConfigPtr config() const;
QColor backgroundRadialColor(const QColor &color) const;
QColor backgroundTopColor(const QColor &color) const;
QColor backgroundBottomColor(const QColor &color) const;
QPixmap verticalGradient(const QColor &color, int height);
QPixmap radialGradient(const QColor &color, int width);
protected:
KComponentData _componentData;
KSharedConfigPtr _config;
qreal _contrast;
QCache<quint64, QPixmap> m_cache;
};
#endif // __OXYGEN_HELPER_H

View file

@ -36,6 +36,8 @@
#include <QTimer>
#include <QCache>
#include "lib/helper.h"
#include "oxygenclient.h"
#include "oxygenclient.moc"
#include "oxygenbutton.h"
@ -57,119 +59,7 @@ namespace Oxygen
// static const int RFRAMESIZE = 7;BUTTONSIZE
// static const int FRAMEBUTTONSPACE = 67;
//BEGIN SHARED CODE
// TODO this should all be moved to its own file and shared with the style
class OxygenHelper
{
public:
static QColor backgroundRadialColor(const QColor &color);
static QColor backgroundTopColor(const QColor &color);
static QColor backgroundBottomColor(const QColor &color);
};
QColor OxygenHelper::backgroundRadialColor(const QColor &color)
{
return KColorScheme::shade(color, KColorScheme::LightShade, 0.7/*FIXME*/);
}
QColor OxygenHelper::backgroundTopColor(const QColor &color)
{
return KColorScheme::shade(color, KColorScheme::MidlightShade, 0.7/*FIXME*/);
}
QColor OxygenHelper::backgroundBottomColor(const QColor &color)
{
return KColorScheme::shade(color, KColorScheme::MidShade, 0.7/*FIXME*/ * -0.6);
}
class TileCache
{
private:
TileCache();
public:
~TileCache() {}
static TileCache *instance();
QPixmap verticalGradient(const QColor &color, int height);
QPixmap radialGradient(const QColor &color, int width);
private:
static TileCache *s_instance;
QCache<quint64, QPixmap> m_cache;
};
TileCache *TileCache::s_instance = 0;
TileCache::TileCache()
{
m_cache.setMaxCost(64);
}
TileCache *TileCache::instance()
{
if (!s_instance)
s_instance = new TileCache;
return s_instance;
}
QPixmap TileCache::verticalGradient(const QColor &color, int height)
{
quint64 key = (quint64(color.rgba()) << 32) | height | 0x8000;
QPixmap *pixmap = m_cache.object(key);
if (!pixmap)
{
pixmap = new QPixmap(32, height);
QLinearGradient gradient(0, 0, 0, height);
gradient.setColorAt(0.0, OxygenHelper::backgroundTopColor(color));
gradient.setColorAt(0.5, color);
gradient.setColorAt(1.0, OxygenHelper::backgroundBottomColor(color));
QPainter p(pixmap);
p.setCompositionMode(QPainter::CompositionMode_Source);
p.fillRect(pixmap->rect(), gradient);
m_cache.insert(key, pixmap);
}
return *pixmap;
}
QPixmap TileCache::radialGradient(const QColor &color, int width)
{
quint64 key = (quint64(color.rgba()) << 32) | width | 0xb000;
QPixmap *pixmap = m_cache.object(key);
if (!pixmap)
{
width /= 2;
pixmap = new QPixmap(width, 64);
pixmap->fill(QColor(0,0,0,0));
QColor radialColor = OxygenHelper::backgroundRadialColor(color);
radialColor.setAlpha(255);
QRadialGradient gradient(64, 0, 64);
gradient.setColorAt(0, radialColor);
radialColor.setAlpha(101);
gradient.setColorAt(0.5, radialColor);
radialColor.setAlpha(37);
gradient.setColorAt(0.75, radialColor);
radialColor.setAlpha(0);
gradient.setColorAt(1, radialColor);
QPainter p(pixmap);
p.scale(width/128.0,1);
p.fillRect(pixmap->rect(), gradient);
m_cache.insert(key, pixmap);
}
return *pixmap;
}
//END SHARED CODE
K_GLOBAL_STATIC_WITH_ARGS(OxygenHelper, globalHelper, ("OxygenDeco"))
void renderDot(QPainter *p, const QPointF &point, qreal diameter)
{
@ -587,21 +477,21 @@ void OxygenClient::paintEvent(QPaintEvent *e)
int splitY = qMin(300, 3*frame.height()/4);
QPixmap tile = TileCache::instance()->verticalGradient(color, splitY);
QPixmap tile = globalHelper->verticalGradient(color, splitY);
painter.drawTiledPixmap(QRect(0, 0, frame.width(), TITLESIZE + TFRAMESIZE), tile);
painter.drawTiledPixmap(QRect(0, 0, LFRAMESIZE, splitY), tile);
painter.fillRect(0, splitY, LFRAMESIZE, frame.height() - splitY, OxygenHelper::backgroundBottomColor(color));
painter.fillRect(0, splitY, LFRAMESIZE, frame.height() - splitY, globalHelper->backgroundBottomColor(color));
painter.drawTiledPixmap(QRect(frame.width()-RFRAMESIZE, 0,
RFRAMESIZE, splitY), tile,
QPoint(frame.width()-RFRAMESIZE, 0));
painter.fillRect(frame.width()-RFRAMESIZE, splitY, RFRAMESIZE, frame.height() - splitY, OxygenHelper::backgroundBottomColor(color));
painter.fillRect(frame.width()-RFRAMESIZE, splitY, RFRAMESIZE, frame.height() - splitY, globalHelper->backgroundBottomColor(color));
painter.fillRect(0, frame.height() - BFRAMESIZE, frame.width(), BFRAMESIZE, OxygenHelper::backgroundBottomColor(color));
painter.fillRect(0, frame.height() - BFRAMESIZE, frame.width(), BFRAMESIZE, globalHelper->backgroundBottomColor(color));
int radialW = qMin(600, frame.width());
tile = TileCache::instance()->radialGradient(color, radialW);
tile = globalHelper->radialGradient(color, radialW);
QRect radialRect = QRect((frame.width() - radialW) / 2, 0, radialW, 64);
painter.drawPixmap(radialRect, tile);