You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
346 lines
7.6 KiB
346 lines
7.6 KiB
15 years ago
|
/* This file is part of the KDE libraries
|
||
|
Copyright (C) 1996 Martynas Kunigelis
|
||
|
|
||
|
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.
|
||
|
*/
|
||
|
/**
|
||
|
* KGameProgress -- a progress indicator widget for KDE.
|
||
|
*/
|
||
|
|
||
15 years ago
|
#include <tqpainter.h>
|
||
|
#include <tqpixmap.h>
|
||
|
#include <tqstring.h>
|
||
|
#include <tqregexp.h>
|
||
|
#include <tqstyle.h>
|
||
15 years ago
|
|
||
|
#include "kgameprogress.h"
|
||
|
|
||
|
#include <kapplication.h>
|
||
|
|
||
14 years ago
|
KGameProgress::KGameProgress(TQWidget *parent, const char *name)
|
||
|
: TQFrame(parent, name),
|
||
15 years ago
|
TQRangeControl(0, 100, 1, 10, 0),
|
||
14 years ago
|
orient(Qt::Horizontal)
|
||
15 years ago
|
{
|
||
|
initialize();
|
||
|
}
|
||
|
|
||
14 years ago
|
KGameProgress::KGameProgress(Qt::Orientation orientation, TQWidget *parent, const char *name)
|
||
|
: TQFrame(parent, name),
|
||
15 years ago
|
TQRangeControl(0, 100, 1, 10, 0),
|
||
15 years ago
|
orient(orientation)
|
||
|
{
|
||
|
initialize();
|
||
|
}
|
||
|
|
||
|
KGameProgress::KGameProgress(int minValue, int maxValue, int value,
|
||
14 years ago
|
Qt::Orientation orientation, TQWidget *parent, const char *name)
|
||
|
: TQFrame(parent, name),
|
||
15 years ago
|
TQRangeControl(minValue, maxValue, 1, 10, value),
|
||
15 years ago
|
orient(orientation)
|
||
|
{
|
||
|
initialize();
|
||
|
}
|
||
|
|
||
|
KGameProgress::~KGameProgress()
|
||
|
{
|
||
|
delete bar_pixmap;
|
||
|
}
|
||
|
|
||
|
void KGameProgress::advance(int offset)
|
||
|
{
|
||
|
setValue(value() + offset);
|
||
|
}
|
||
|
|
||
|
void KGameProgress::initialize()
|
||
|
{
|
||
|
format_ = "%p%";
|
||
|
use_supplied_bar_color = false;
|
||
|
bar_pixmap = 0;
|
||
|
bar_style = Solid;
|
||
|
text_enabled = TRUE;
|
||
|
setBackgroundMode( PaletteBackground );
|
||
15 years ago
|
connect(kapp, TQT_SIGNAL(appearanceChanged()), this, TQT_SLOT(paletteChange()));
|
||
15 years ago
|
paletteChange();
|
||
|
}
|
||
|
|
||
|
void KGameProgress::paletteChange()
|
||
|
{
|
||
15 years ago
|
TQPalette p = kapp->palette();
|
||
14 years ago
|
const TQColorGroup &tqcolorGroup = p.active();
|
||
15 years ago
|
if (!use_supplied_bar_color)
|
||
14 years ago
|
bar_color = tqcolorGroup.highlight();
|
||
|
bar_text_color = tqcolorGroup.highlightedText();
|
||
|
text_color = tqcolorGroup.text();
|
||
15 years ago
|
setPalette(p);
|
||
|
|
||
|
adjustStyle();
|
||
|
}
|
||
|
|
||
|
|
||
15 years ago
|
void KGameProgress::setBarPixmap(const TQPixmap &pixmap)
|
||
15 years ago
|
{
|
||
|
if (pixmap.isNull())
|
||
|
return;
|
||
|
if (bar_pixmap)
|
||
|
delete bar_pixmap;
|
||
|
|
||
15 years ago
|
bar_pixmap = new TQPixmap(pixmap);
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
void KGameProgress::setBarColor(const TQColor &color)
|
||
15 years ago
|
{
|
||
|
bar_color = color;
|
||
|
use_supplied_bar_color = true;
|
||
|
if (bar_pixmap) {
|
||
|
delete bar_pixmap;
|
||
|
bar_pixmap = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void KGameProgress::setBarStyle(BarStyle style)
|
||
|
{
|
||
|
if (bar_style != style) {
|
||
|
bar_style = style;
|
||
|
update();
|
||
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
void KGameProgress::setOrientation(Qt::Orientation orientation)
|
||
15 years ago
|
{
|
||
|
if (orient != orientation) {
|
||
|
orient = orientation;
|
||
|
update();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void KGameProgress::setValue(int value)
|
||
|
{
|
||
15 years ago
|
TQRangeControl::setValue(value);
|
||
15 years ago
|
}
|
||
|
|
||
|
void KGameProgress::setTextEnabled(bool enable)
|
||
|
{
|
||
|
text_enabled = enable;
|
||
|
}
|
||
|
|
||
15 years ago
|
const TQColor & KGameProgress::barColor() const
|
||
15 years ago
|
{
|
||
|
return bar_color;
|
||
|
}
|
||
|
|
||
15 years ago
|
const TQPixmap * KGameProgress::barPixmap() const
|
||
15 years ago
|
{
|
||
|
return bar_pixmap;
|
||
|
}
|
||
|
|
||
|
bool KGameProgress::textEnabled() const
|
||
|
{
|
||
|
return text_enabled;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQSize KGameProgress::tqsizeHint() const
|
||
15 years ago
|
{
|
||
15 years ago
|
TQSize s( size() );
|
||
15 years ago
|
|
||
14 years ago
|
if(orientation() == Qt::Vertical) {
|
||
15 years ago
|
s.setWidth(24);
|
||
|
} else {
|
||
|
s.setHeight(24);
|
||
|
}
|
||
|
|
||
|
return s;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQSize KGameProgress::tqminimumSizeHint() const
|
||
15 years ago
|
{
|
||
14 years ago
|
return tqsizeHint();
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
TQSizePolicy KGameProgress::sizePolicy() const
|
||
15 years ago
|
{
|
||
14 years ago
|
if ( orientation()==Qt::Vertical )
|
||
15 years ago
|
return TQSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Expanding );
|
||
15 years ago
|
else
|
||
15 years ago
|
return TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Fixed );
|
||
15 years ago
|
}
|
||
|
|
||
|
KGameProgress::Orientation KGameProgress::orientation() const
|
||
|
{
|
||
|
return orient;
|
||
|
}
|
||
|
|
||
|
KGameProgress::BarStyle KGameProgress::barStyle() const
|
||
|
{
|
||
|
return bar_style;
|
||
|
}
|
||
|
|
||
|
int KGameProgress::recalcValue(int range)
|
||
|
{
|
||
|
int abs_value = value() - minValue();
|
||
|
int abs_range = maxValue() - minValue();
|
||
|
return abs_range ? range * abs_value / abs_range : 0;
|
||
|
}
|
||
|
|
||
|
void KGameProgress::valueChange()
|
||
|
{
|
||
14 years ago
|
tqrepaint(contentsRect(), FALSE);
|
||
15 years ago
|
emit percentageChanged(recalcValue(100));
|
||
|
}
|
||
|
|
||
|
void KGameProgress::rangeChange()
|
||
|
{
|
||
14 years ago
|
tqrepaint(contentsRect(), FALSE);
|
||
15 years ago
|
emit percentageChanged(recalcValue(100));
|
||
|
}
|
||
|
|
||
15 years ago
|
void KGameProgress::styleChange(TQStyle&)
|
||
15 years ago
|
{
|
||
|
adjustStyle();
|
||
|
}
|
||
|
|
||
|
void KGameProgress::adjustStyle()
|
||
|
{
|
||
14 years ago
|
switch (tqstyle().tqstyleHint(TQStyle::SH_GUIStyle)) {
|
||
15 years ago
|
case WindowsStyle:
|
||
15 years ago
|
setFrameStyle(TQFrame::WinPanel | TQFrame::Sunken);
|
||
15 years ago
|
break;
|
||
|
case MotifStyle:
|
||
|
default:
|
||
15 years ago
|
setFrameStyle(TQFrame::Panel | TQFrame::Sunken);
|
||
15 years ago
|
setLineWidth( 2 );
|
||
|
break;
|
||
|
}
|
||
|
update();
|
||
|
}
|
||
|
|
||
15 years ago
|
void KGameProgress::paletteChange( const TQPalette &p )
|
||
15 years ago
|
{
|
||
|
// This never gets called for global color changes
|
||
|
// because we call setPalette() ourselves.
|
||
15 years ago
|
TQFrame::paletteChange(p);
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
void KGameProgress::drawText(TQPainter *p)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQRect r(contentsRect());
|
||
|
//TQColor c(bar_color.rgb() ^ backgroundColor().rgb());
|
||
15 years ago
|
|
||
|
// Rik: Replace the tags '%p', '%v' and '%m' with the current percentage,
|
||
|
// the current value and the maximum value respectively.
|
||
15 years ago
|
TQString s(format_);
|
||
15 years ago
|
|
||
14 years ago
|
s.replace(TQRegExp(TQString::tqfromLatin1("%p")), TQString::number(recalcValue(100)));
|
||
|
s.replace(TQRegExp(TQString::tqfromLatin1("%v")), TQString::number(value()));
|
||
|
s.replace(TQRegExp(TQString::tqfromLatin1("%m")), TQString::number(maxValue()));
|
||
15 years ago
|
|
||
|
p->setPen(text_color);
|
||
15 years ago
|
TQFont font = p->font();
|
||
15 years ago
|
font.setBold(true);
|
||
|
p->setFont(font);
|
||
|
//p->setRasterOp(XorROP);
|
||
|
p->drawText(r, AlignCenter, s);
|
||
|
p->setClipRegion( fr );
|
||
|
p->setPen(bar_text_color);
|
||
|
p->drawText(r, AlignCenter, s);
|
||
|
}
|
||
|
|
||
15 years ago
|
void KGameProgress::drawContents(TQPainter *p)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQRect cr = contentsRect(), er = cr;
|
||
15 years ago
|
fr = cr;
|
||
15 years ago
|
TQBrush fb(bar_color), eb(backgroundColor());
|
||
15 years ago
|
|
||
|
if (bar_pixmap)
|
||
|
fb.setPixmap(*bar_pixmap);
|
||
|
|
||
|
if (backgroundPixmap())
|
||
|
eb.setPixmap(*backgroundPixmap());
|
||
|
|
||
|
switch (bar_style) {
|
||
|
case Solid:
|
||
14 years ago
|
if (orient ==Qt::Horizontal) {
|
||
15 years ago
|
fr.setWidth(recalcValue(cr.width()));
|
||
|
er.setLeft(fr.right() + 1);
|
||
|
} else {
|
||
|
fr.setTop(cr.bottom() - recalcValue(cr.height()));
|
||
|
er.setBottom(fr.top() - 1);
|
||
|
}
|
||
|
|
||
|
p->setBrushOrigin(cr.topLeft());
|
||
|
p->fillRect(fr, fb);
|
||
|
|
||
|
p->fillRect(er, eb);
|
||
|
|
||
|
break;
|
||
|
|
||
|
case Blocked:
|
||
|
const int margin = 2;
|
||
|
int max, num, dx, dy;
|
||
14 years ago
|
if (orient ==Qt::Horizontal) {
|
||
15 years ago
|
fr.setHeight(cr.height() - 2 * margin);
|
||
|
fr.setWidth((int)(0.67 * fr.height()));
|
||
15 years ago
|
fr.moveTopLeft(TQPoint(cr.left() + margin, cr.top() + margin));
|
||
15 years ago
|
dx = fr.width() + margin;
|
||
|
dy = 0;
|
||
|
max = (cr.width() - margin) / (fr.width() + margin) + 1;
|
||
|
num = recalcValue(max);
|
||
|
} else {
|
||
|
fr.setWidth(cr.width() - 2 * margin);
|
||
|
fr.setHeight((int)(0.67 * fr.width()));
|
||
15 years ago
|
fr.moveBottomLeft(TQPoint(cr.left() + margin, cr.bottom() - margin));
|
||
15 years ago
|
dx = 0;
|
||
|
dy = - (fr.height() + margin);
|
||
|
max = (cr.height() - margin) / (fr.height() + margin) + 1;
|
||
|
num = recalcValue(max);
|
||
|
}
|
||
|
p->setClipRect(cr.x() + margin, cr.y() + margin,
|
||
|
cr.width() - margin, cr.height() - margin);
|
||
|
for (int i = 0; i < num; i++) {
|
||
|
p->setBrushOrigin(fr.topLeft());
|
||
|
p->fillRect(fr, fb);
|
||
|
fr.moveBy(dx, dy);
|
||
|
}
|
||
|
|
||
|
if (num != max) {
|
||
14 years ago
|
if (orient ==Qt::Horizontal)
|
||
15 years ago
|
er.setLeft(fr.right() + 1);
|
||
|
else
|
||
|
er.setBottom(fr.bottom() + 1);
|
||
|
if (!er.isNull()) {
|
||
|
p->setBrushOrigin(cr.topLeft());
|
||
|
p->fillRect(er, eb);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
if (text_enabled && bar_style != Blocked)
|
||
|
drawText(p);
|
||
|
}
|
||
|
|
||
15 years ago
|
void KGameProgress::setFormat(const TQString & format)
|
||
15 years ago
|
{
|
||
|
format_ = format;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString KGameProgress::format() const
|
||
15 years ago
|
{
|
||
|
return format_;
|
||
|
}
|
||
|
|
||
|
#include "kgameprogress.moc"
|