• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDEUI

kcapacitybar.cpp

Go to the documentation of this file.
00001 /*
00002  * This file is part of the KDE project
00003  * Copyright (C) 2008 Rafael Fernández López <ereslibre@kde.org>
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Library General Public License
00016  * along with this library; see the file COPYING.LIB.  If not, write to
00017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include "kcapacitybar.h"
00022 #include <kstyle.h>
00023 
00024 #include <math.h>
00025 
00026 #include <QtGui/QApplication>
00027 #include <QtGui/QLabel>
00028 #include <QtGui/QStyle>
00029 #include <QtGui/QPainter>
00030 #include <QtGui/QBoxLayout>
00031 #include <QtGui/QPaintEvent>
00032 #include <QtGui/QPainterPath>
00033 #include <QtGui/QLinearGradient>
00034 #include <QtGui/QStyleOptionFrame>
00035 
00036 #include <kcolorscheme.h>
00037 
00038 #define ROUND_MARGIN     6
00039 #define VERTICAL_SPACING 1
00040 
00041 class KCapacityBar::Private
00042 {
00043 public:
00044     Private(KCapacityBar::DrawTextMode drawTextMode)
00045         : value(0)
00046         , fillFullBlocks(true)
00047         , continuous(true)
00048         , barHeight(12)
00049         , horizontalTextAlignment(Qt::AlignCenter)
00050         , drawTextMode(drawTextMode) {}
00051 
00052     ~Private() {}
00053 
00054     int value;
00055     QString text;
00056     bool fillFullBlocks;
00057     bool continuous;
00058     int barHeight;
00059     Qt::Alignment horizontalTextAlignment;
00060     QStyle::ControlElement ce_capacityBar;
00061 
00062     const KCapacityBar::DrawTextMode drawTextMode;
00063 };
00064 
00065 KCapacityBar::KCapacityBar(KCapacityBar::DrawTextMode drawTextMode, QWidget *parent)
00066     : QWidget(parent)
00067     , d(new Private(drawTextMode))
00068 {
00069     d->ce_capacityBar = KStyle::customControlElement("CE_CapacityBar", this);
00070 }
00071 
00072 KCapacityBar::~KCapacityBar()
00073 {
00074     delete d;
00075 }
00076 
00077 void KCapacityBar::setValue(int value)
00078 {
00079     d->value = value;
00080 }
00081 
00082 int KCapacityBar::value() const
00083 {
00084     return d->value;
00085 }
00086 
00087 void KCapacityBar::setText(const QString &text)
00088 {
00089     d->text = text;
00090 }
00091 
00092 QString KCapacityBar::text() const
00093 {
00094     return d->text;
00095 }
00096 
00097 void KCapacityBar::setFillFullBlocks(bool fillFullBlocks)
00098 {
00099     d->fillFullBlocks = fillFullBlocks;
00100 }
00101 
00102 bool KCapacityBar::fillFullBlocks() const
00103 {
00104     return d->fillFullBlocks;
00105 }
00106 
00107 void KCapacityBar::setContinuous(bool continuous)
00108 {
00109     d->continuous = continuous;
00110 }
00111 
00112 bool KCapacityBar::continuous() const
00113 {
00114     return d->continuous;
00115 }
00116 
00117 void KCapacityBar::setBarHeight(int barHeight)
00118 {
00119     // automatically convert odd values to even. This will make the bar look
00120     // better.
00121     d->barHeight = (barHeight % 2) ? barHeight + 1 : barHeight;
00122 }
00123 
00124 int KCapacityBar::barHeight() const
00125 {
00126     return d->barHeight;
00127 }
00128 
00129 void KCapacityBar::setHorizontalTextAlignment(Qt::Alignment horizontalTextAlignment)
00130 {
00131     Qt::Alignment alignment = horizontalTextAlignment;
00132 
00133     // if the value came with any vertical alignment flag, remove it.
00134     alignment &= ~Qt::AlignTop;
00135     alignment &= ~Qt::AlignBottom;
00136     alignment &= ~Qt::AlignVCenter;
00137 
00138     d->horizontalTextAlignment = alignment;
00139 }
00140 
00141 Qt::Alignment KCapacityBar::horizontalTextAlignment() const
00142 {
00143     return d->horizontalTextAlignment;
00144 }
00145 
00146 void KCapacityBar::drawCapacityBar(QPainter *p, const QRect &rect) const
00147 {
00148     if (d->ce_capacityBar)
00149     {
00150         QStyleOptionProgressBar opt;
00151         opt.initFrom(this);
00152         opt.rect = rect;
00153         opt.minimum = 0;
00154         opt.maximum = 100;
00155         opt.progress = d->value;
00156         opt.text = d->text;
00157         opt.textAlignment = Qt::AlignCenter;
00158         opt.textVisible = true;
00159         style()->drawControl(d->ce_capacityBar, &opt, p, this);
00160 
00161         return;
00162     }
00163 
00164     p->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
00165 
00166     p->save();
00167 
00168     QRect drawRect(rect);
00169 
00170     if (d->drawTextMode == DrawTextOutline) {
00171         drawRect.setHeight(d->barHeight);
00172     }
00173 
00174     QPainterPath outline;
00175     outline.moveTo(rect.left() + ROUND_MARGIN / 4 + 1, rect.top());
00176     outline.lineTo(rect.left() + drawRect.width() - ROUND_MARGIN / 4 - 1, rect.top());
00177     outline.quadTo(rect.left() + drawRect.width() + ROUND_MARGIN / 2, drawRect.height() / 2 + rect.top(), rect.left() + drawRect.width() - ROUND_MARGIN / 4 - 1, drawRect.height() + rect.top());
00178     outline.lineTo(rect.left() + ROUND_MARGIN / 4 + 1, drawRect.height() + rect.top());
00179     outline.quadTo(-ROUND_MARGIN / 2 + rect.left(), drawRect.height() / 2 + rect.top(), rect.left() + ROUND_MARGIN / 4 + 1, rect.top());
00180     const QColor fillColor = KColorScheme::shade(palette().window().color(), KColorScheme::DarkShade);
00181     p->fillPath(outline, QColor(fillColor.red(), fillColor.green(), fillColor.blue(), 50));
00182 
00183     QRadialGradient bottomGradient(QPointF(rect.width() / 2, drawRect.bottom() + 1), rect.width() / 2);
00184     bottomGradient.setColorAt(0, KColorScheme::shade(palette().window().color(), KColorScheme::LightShade));
00185     bottomGradient.setColorAt(1, Qt::transparent);
00186     p->fillRect(QRect(rect.left(), drawRect.bottom() + rect.top(), rect.width(), 1), bottomGradient);
00187 
00188     p->translate(rect.left() + 2, rect.top() + 1);
00189 
00190     drawRect.setWidth(drawRect.width() - 4);
00191     drawRect.setHeight(drawRect.height() - 2);
00192 
00193     QPainterPath path;
00194     path.moveTo(ROUND_MARGIN / 4, 0);
00195     path.lineTo(drawRect.width() - ROUND_MARGIN / 4, 0);
00196     path.quadTo(drawRect.width() + ROUND_MARGIN / 2, drawRect.height() / 2, drawRect.width() - ROUND_MARGIN / 4, drawRect.height());
00197     path.lineTo(ROUND_MARGIN / 4, drawRect.height());
00198     path.quadTo(-ROUND_MARGIN / 2, drawRect.height() / 2, ROUND_MARGIN / 4, 0);
00199 
00200     QLinearGradient linearGradient(0, 0, 0, drawRect.height());
00201     linearGradient.setColorAt(0.5, KColorScheme::shade(palette().window().color(), KColorScheme::MidShade));
00202     linearGradient.setColorAt(1, KColorScheme::shade(palette().window().color(), KColorScheme::LightShade));
00203     p->fillPath(path, linearGradient);
00204 
00205     p->setBrush(Qt::NoBrush);
00206     p->setPen(Qt::NoPen);
00207 
00208     if (d->continuous || !d->fillFullBlocks) {
00209         int start = (layoutDirection() == Qt::LeftToRight) ? -1
00210                                                            : (drawRect.width() + 2) - (drawRect.width() + 2) * (d->value / 100.0);
00211 
00212         p->setClipRect(QRect(start, 0, (drawRect.width() + 2) * (d->value / 100.0), drawRect.height()), Qt::IntersectClip);
00213     }
00214 
00215     int left = (layoutDirection() == Qt::LeftToRight) ? 0
00216                                                       : drawRect.width();
00217 
00218     int right = (layoutDirection() == Qt::LeftToRight) ? drawRect.width()
00219                                                        : 0;
00220 
00221     int roundMargin = (layoutDirection() == Qt::LeftToRight) ? ROUND_MARGIN
00222                                                              : -ROUND_MARGIN;
00223 
00224     int spacing = 2;
00225     int verticalSpacing = VERTICAL_SPACING;
00226     int slotWidth = 6;
00227     int start = roundMargin / 4;
00228 
00229     QPainterPath internalBar;
00230     internalBar.moveTo(left + roundMargin / 4, 0);
00231     internalBar.lineTo(right - roundMargin / 4, 0);
00232     internalBar.quadTo(right + roundMargin / 2, drawRect.height() / 2, right - roundMargin / 4, drawRect.height());
00233     internalBar.lineTo(left + roundMargin / 4, drawRect.height());
00234     internalBar.quadTo(left - roundMargin / 2, drawRect.height() / 2, left + roundMargin / 4, 0);
00235 
00236     QLinearGradient fillInternalBar(left, 0, right, 0);
00237     fillInternalBar.setColorAt(0, KColorScheme::shade(palette().highlight().color(), KColorScheme::MidShade));
00238     fillInternalBar.setColorAt(0.5, KColorScheme::shade(palette().highlight().color(), KColorScheme::LightShade));
00239     fillInternalBar.setColorAt(1, KColorScheme::shade(palette().highlight().color(), KColorScheme::MidShade));
00240 
00241     if (d->drawTextMode == KCapacityBar::DrawTextInline) {
00242         p->save();
00243         p->setOpacity(p->opacity() * 0.7);
00244     }
00245 
00246     if (!d->continuous) {
00247         int numSlots = (drawRect.width() - ROUND_MARGIN - ((slotWidth + spacing) * 2)) / (slotWidth + spacing);
00248         int stopSlot = floor((numSlots + 2) * (d->value / 100.0));
00249 
00250         int plusOffset = d->fillFullBlocks ? ((drawRect.width() - ROUND_MARGIN - ((slotWidth + spacing) * 2)) - (numSlots * (slotWidth + spacing))) / 2.0
00251                                            : 0;
00252 
00253         if (!d->fillFullBlocks || stopSlot) {
00254             QPainterPath firstSlot;
00255             firstSlot.moveTo(left + roundMargin / 4, verticalSpacing);
00256             firstSlot.lineTo(left + slotWidth + roundMargin / 4 + plusOffset, verticalSpacing);
00257             firstSlot.lineTo(left + slotWidth + roundMargin / 4 + plusOffset, drawRect.height() - verticalSpacing);
00258             firstSlot.lineTo(left + roundMargin / 4, drawRect.height() - verticalSpacing);
00259             firstSlot.quadTo(left, drawRect.height() / 2, left + roundMargin / 4, verticalSpacing);
00260             p->fillPath(firstSlot, fillInternalBar);
00261             start += slotWidth + spacing + plusOffset;
00262 
00263             bool stopped = false;
00264             for (int i = 0; i < numSlots + 1; i++) {
00265                 if (d->fillFullBlocks && (i == (stopSlot + 1))) {
00266                     stopped = true;
00267                     break;
00268                 }
00269                 p->fillRect(QRect(rect.left() + start, rect.top() + verticalSpacing, slotWidth, drawRect.height() - verticalSpacing * 2), fillInternalBar);
00270                 start += slotWidth + spacing;
00271             }
00272 
00273             if (!d->fillFullBlocks || (!stopped && (stopSlot != (numSlots + 1)) && (stopSlot != numSlots))) {
00274                 QPainterPath lastSlot;
00275                 lastSlot.moveTo(start, verticalSpacing);
00276                 lastSlot.lineTo(start, drawRect.height() - verticalSpacing);
00277                 lastSlot.lineTo(start + slotWidth + plusOffset, drawRect.height() - verticalSpacing);
00278                 lastSlot.quadTo(start + roundMargin, drawRect.height() / 2, start + slotWidth + plusOffset, verticalSpacing);
00279                 lastSlot.lineTo(start, verticalSpacing);
00280                 p->fillPath(lastSlot, fillInternalBar);
00281             }
00282         }
00283     } else {
00284         p->fillPath(internalBar, fillInternalBar);
00285     }
00286 
00287     if (d->drawTextMode == KCapacityBar::DrawTextInline) {
00288         p->restore();
00289     }
00290 
00291     p->save();
00292     p->setClipping(false);
00293     QRadialGradient topGradient(QPointF(rect.width() / 2, drawRect.top()), rect.width() / 2);
00294     const QColor fillTopColor = KColorScheme::shade(palette().window().color(), KColorScheme::LightShade);
00295     topGradient.setColorAt(0, QColor(fillTopColor.red(), fillTopColor.green(), fillTopColor.blue(), 127));
00296     topGradient.setColorAt(1, Qt::transparent);
00297     p->fillRect(QRect(rect.left(), rect.top() + drawRect.top(), rect.width(), 2), topGradient);
00298     p->restore();
00299 
00300     p->save();
00301     p->setClipRect(QRect(-1, 0, rect.width(), drawRect.height() / 2), Qt::ReplaceClip);
00302     QLinearGradient glassGradient(0, -5, 0, drawRect.height());
00303     const QColor fillGlassColor = palette().base().color();
00304     glassGradient.setColorAt(0, QColor(fillGlassColor.red(), fillGlassColor.green(), fillGlassColor.blue(), 255));
00305     glassGradient.setColorAt(1, Qt::transparent);
00306     p->fillPath(internalBar, glassGradient);
00307     p->restore();
00308 
00309     p->restore();
00310 
00311     if (d->drawTextMode == KCapacityBar::DrawTextInline) {
00312         QRect rect(drawRect);
00313         rect.setHeight(rect.height() + 4);
00314         p->drawText(rect, Qt::AlignCenter, fontMetrics().elidedText(d->text, Qt::ElideRight, drawRect.width() - 2 * ROUND_MARGIN));
00315     } else {
00316         p->drawText(rect, Qt::AlignBottom | d->horizontalTextAlignment, fontMetrics().elidedText(d->text, Qt::ElideRight, drawRect.width()));
00317     }
00318 }
00319 
00320 void KCapacityBar::changeEvent(QEvent *event)
00321 {
00322     if (event->type() == QEvent::StyleChange) {
00323         d->ce_capacityBar = KStyle::customControlElement("CE_CapacityBar", this);
00324     }
00325 
00326     QWidget::changeEvent(event);
00327 }
00328 
00329 QSize KCapacityBar::minimumSizeHint() const
00330 {
00331     int width = (d->drawTextMode == KCapacityBar::DrawTextInline) ?
00332                 fontMetrics().width(d->text) + ROUND_MARGIN * 2 :
00333                 fontMetrics().width(d->text);
00334 
00335     int height = (d->drawTextMode == KCapacityBar::DrawTextInline) ?
00336                  qMax(fontMetrics().height(), d->barHeight) :
00337                  (d->text.isEmpty() ? 0 : fontMetrics().height() + VERTICAL_SPACING * 2) + d->barHeight;
00338 
00339     if (height % 2) {
00340         height++;
00341     }
00342 
00343     return QSize(width, height);
00344 }
00345 
00346 void KCapacityBar::paintEvent(QPaintEvent *event)
00347 {
00348     QPainter p(this);
00349     drawCapacityBar(&p, event->rect());
00350     p.end();
00351 }

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal