• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • widgets
kcapacitybar.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the KDE project
3 * Copyright (C) 2008 Rafael Fernández López <ereslibre@kde.org>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include "kcapacitybar.h"
22#include <kstyle.h>
23
24#include <math.h>
25
26#include <QtGui/QApplication>
27#include <QtGui/QLabel>
28#include <QtGui/QStyle>
29#include <QtGui/QPainter>
30#include <QtGui/QBoxLayout>
31#include <QtGui/QPaintEvent>
32#include <QtGui/QPainterPath>
33#include <QtGui/QLinearGradient>
34#include <QtGui/QStyleOptionFrame>
35
36#include <kcolorscheme.h>
37
38#define ROUND_MARGIN 6
39#define VERTICAL_SPACING 1
40
41class KCapacityBar::Private
42{
43public:
44 Private(KCapacityBar::DrawTextMode drawTextMode)
45 : value(0)
46 , fillFullBlocks(true)
47 , continuous(true)
48 , barHeight(12)
49 , horizontalTextAlignment(Qt::AlignCenter)
50 , drawTextMode(drawTextMode) {}
51
52 ~Private() {}
53
54 int value;
55 QString text;
56 bool fillFullBlocks;
57 bool continuous;
58 int barHeight;
59 Qt::Alignment horizontalTextAlignment;
60 QStyle::ControlElement ce_capacityBar;
61
62 KCapacityBar::DrawTextMode drawTextMode;
63};
64
65KCapacityBar::KCapacityBar(KCapacityBar::DrawTextMode drawTextMode, QWidget *parent)
66 : QWidget(parent)
67 , d(new Private(drawTextMode))
68{
69 d->ce_capacityBar = KStyle::customControlElement("CE_CapacityBar", this);
70}
71
72KCapacityBar::~KCapacityBar()
73{
74 delete d;
75}
76
77void KCapacityBar::setValue(int value)
78{
79 d->value = value;
80 update();
81}
82
83int KCapacityBar::value() const
84{
85 return d->value;
86}
87
88void KCapacityBar::setText(const QString &text)
89{
90 bool updateGeom = d->text.isEmpty() || text.isEmpty();
91 d->text = text;
92 if (updateGeom) {
93 updateGeometry();
94 }
95
96 setAccessibleName(text);
97
98 update();
99}
100
101QString KCapacityBar::text() const
102{
103 return d->text;
104}
105
106void KCapacityBar::setFillFullBlocks(bool fillFullBlocks)
107{
108 d->fillFullBlocks = fillFullBlocks;
109 update();
110}
111
112bool KCapacityBar::fillFullBlocks() const
113{
114 return d->fillFullBlocks;
115}
116
117void KCapacityBar::setContinuous(bool continuous)
118{
119 d->continuous = continuous;
120 update();
121}
122
123bool KCapacityBar::continuous() const
124{
125 return d->continuous;
126}
127
128void KCapacityBar::setBarHeight(int barHeight)
129{
130 // automatically convert odd values to even. This will make the bar look
131 // better.
132 d->barHeight = (barHeight % 2) ? barHeight + 1 : barHeight;
133 updateGeometry();
134}
135
136int KCapacityBar::barHeight() const
137{
138 return d->barHeight;
139}
140
141void KCapacityBar::setHorizontalTextAlignment(Qt::Alignment horizontalTextAlignment)
142{
143 Qt::Alignment alignment = horizontalTextAlignment;
144
145 // if the value came with any vertical alignment flag, remove it.
146 alignment &= ~Qt::AlignTop;
147 alignment &= ~Qt::AlignBottom;
148 alignment &= ~Qt::AlignVCenter;
149
150 d->horizontalTextAlignment = alignment;
151 update();
152}
153
154Qt::Alignment KCapacityBar::horizontalTextAlignment() const
155{
156 return d->horizontalTextAlignment;
157}
158
159void KCapacityBar::setDrawTextMode(DrawTextMode mode)
160{
161 d->drawTextMode = mode;
162 update();
163}
164
165KCapacityBar::DrawTextMode KCapacityBar::drawTextMode() const
166{
167 return d->drawTextMode;
168}
169
170void KCapacityBar::drawCapacityBar(QPainter *p, const QRect &rect) const
171{
172 if (d->ce_capacityBar)
173 {
174 QStyleOptionProgressBar opt;
175 opt.initFrom(this);
176 opt.rect = rect;
177 opt.minimum = 0;
178 opt.maximum = 100;
179 opt.progress = d->value;
180 opt.text = d->text;
181 opt.textAlignment = Qt::AlignCenter;
182 opt.textVisible = true;
183 style()->drawControl(d->ce_capacityBar, &opt, p, this);
184
185 return;
186 }
187
188 p->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
189
190 p->save();
191
192 QRect drawRect(rect);
193
194 if (d->drawTextMode == DrawTextOutline) {
195 drawRect.setHeight(d->barHeight);
196 }
197
198 QPainterPath outline;
199 outline.moveTo(rect.left() + ROUND_MARGIN / 4 + 1, rect.top());
200 outline.lineTo(rect.left() + drawRect.width() - ROUND_MARGIN / 4 - 1, rect.top());
201 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());
202 outline.lineTo(rect.left() + ROUND_MARGIN / 4 + 1, drawRect.height() + rect.top());
203 outline.quadTo(-ROUND_MARGIN / 2 + rect.left(), drawRect.height() / 2 + rect.top(), rect.left() + ROUND_MARGIN / 4 + 1, rect.top());
204 const QColor fillColor = KColorScheme::shade(palette().window().color(), KColorScheme::DarkShade);
205 p->fillPath(outline, QColor(fillColor.red(), fillColor.green(), fillColor.blue(), 50));
206
207 QRadialGradient bottomGradient(QPointF(rect.width() / 2, drawRect.bottom() + 1), rect.width() / 2);
208 bottomGradient.setColorAt(0, KColorScheme::shade(palette().window().color(), KColorScheme::LightShade));
209 bottomGradient.setColorAt(1, Qt::transparent);
210 p->fillRect(QRect(rect.left(), drawRect.bottom() + rect.top(), rect.width(), 1), bottomGradient);
211
212 p->translate(rect.left() + 2, rect.top() + 1);
213
214 drawRect.setWidth(drawRect.width() - 4);
215 drawRect.setHeight(drawRect.height() - 2);
216
217 QPainterPath path;
218 path.moveTo(ROUND_MARGIN / 4, 0);
219 path.lineTo(drawRect.width() - ROUND_MARGIN / 4, 0);
220 path.quadTo(drawRect.width() + ROUND_MARGIN / 2, drawRect.height() / 2, drawRect.width() - ROUND_MARGIN / 4, drawRect.height());
221 path.lineTo(ROUND_MARGIN / 4, drawRect.height());
222 path.quadTo(-ROUND_MARGIN / 2, drawRect.height() / 2, ROUND_MARGIN / 4, 0);
223
224 QLinearGradient linearGradient(0, 0, 0, drawRect.height());
225 linearGradient.setColorAt(0.5, KColorScheme::shade(palette().window().color(), KColorScheme::MidShade));
226 linearGradient.setColorAt(1, KColorScheme::shade(palette().window().color(), KColorScheme::LightShade));
227 p->fillPath(path, linearGradient);
228
229 p->setBrush(Qt::NoBrush);
230 p->setPen(Qt::NoPen);
231
232 if (d->continuous || !d->fillFullBlocks) {
233 int start = (layoutDirection() == Qt::LeftToRight) ? -1
234 : (drawRect.width() + 2) - (drawRect.width() + 2) * (d->value / 100.0);
235
236 p->setClipRect(QRect(start, 0, (drawRect.width() + 2) * (d->value / 100.0), drawRect.height()), Qt::IntersectClip);
237 }
238
239 int left = (layoutDirection() == Qt::LeftToRight) ? 0
240 : drawRect.width();
241
242 int right = (layoutDirection() == Qt::LeftToRight) ? drawRect.width()
243 : 0;
244
245 int roundMargin = (layoutDirection() == Qt::LeftToRight) ? ROUND_MARGIN
246 : -ROUND_MARGIN;
247
248 int spacing = 2;
249 int verticalSpacing = VERTICAL_SPACING;
250 int slotWidth = 6;
251 int start = roundMargin / 4;
252
253 QPainterPath internalBar;
254 internalBar.moveTo(left + roundMargin / 4, 0);
255 internalBar.lineTo(right - roundMargin / 4, 0);
256 internalBar.quadTo(right + roundMargin / 2, drawRect.height() / 2, right - roundMargin / 4, drawRect.height());
257 internalBar.lineTo(left + roundMargin / 4, drawRect.height());
258 internalBar.quadTo(left - roundMargin / 2, drawRect.height() / 2, left + roundMargin / 4, 0);
259
260 QLinearGradient fillInternalBar(left, 0, right, 0);
261 fillInternalBar.setColorAt(0, KColorScheme::shade(palette().highlight().color(), KColorScheme::MidShade));
262 fillInternalBar.setColorAt(0.5, KColorScheme::shade(palette().highlight().color(), KColorScheme::LightShade));
263 fillInternalBar.setColorAt(1, KColorScheme::shade(palette().highlight().color(), KColorScheme::MidShade));
264
265 if (d->drawTextMode == KCapacityBar::DrawTextInline) {
266 p->save();
267 p->setOpacity(p->opacity() * 0.7);
268 }
269
270 if (!d->continuous) {
271 int numSlots = (drawRect.width() - ROUND_MARGIN - ((slotWidth + spacing) * 2)) / (slotWidth + spacing);
272 int stopSlot = floor((numSlots + 2) * (d->value / 100.0));
273
274 int plusOffset = d->fillFullBlocks ? ((drawRect.width() - ROUND_MARGIN - ((slotWidth + spacing) * 2)) - (numSlots * (slotWidth + spacing))) / 2.0
275 : 0;
276
277 if (!d->fillFullBlocks || stopSlot) {
278 QPainterPath firstSlot;
279 firstSlot.moveTo(left + roundMargin / 4, verticalSpacing);
280 firstSlot.lineTo(left + slotWidth + roundMargin / 4 + plusOffset, verticalSpacing);
281 firstSlot.lineTo(left + slotWidth + roundMargin / 4 + plusOffset, drawRect.height() - verticalSpacing);
282 firstSlot.lineTo(left + roundMargin / 4, drawRect.height() - verticalSpacing);
283 firstSlot.quadTo(left, drawRect.height() / 2, left + roundMargin / 4, verticalSpacing);
284 p->fillPath(firstSlot, fillInternalBar);
285 start += slotWidth + spacing + plusOffset;
286
287 bool stopped = false;
288 for (int i = 0; i < numSlots + 1; i++) {
289 if (d->fillFullBlocks && (i == (stopSlot + 1))) {
290 stopped = true;
291 break;
292 }
293 p->fillRect(QRect(rect.left() + start, rect.top() + verticalSpacing, slotWidth, drawRect.height() - verticalSpacing * 2), fillInternalBar);
294 start += slotWidth + spacing;
295 }
296
297 if (!d->fillFullBlocks || (!stopped && (stopSlot != (numSlots + 1)) && (stopSlot != numSlots))) {
298 QPainterPath lastSlot;
299 lastSlot.moveTo(start, verticalSpacing);
300 lastSlot.lineTo(start, drawRect.height() - verticalSpacing);
301 lastSlot.lineTo(start + slotWidth + plusOffset, drawRect.height() - verticalSpacing);
302 lastSlot.quadTo(start + roundMargin, drawRect.height() / 2, start + slotWidth + plusOffset, verticalSpacing);
303 lastSlot.lineTo(start, verticalSpacing);
304 p->fillPath(lastSlot, fillInternalBar);
305 }
306 }
307 } else {
308 p->fillPath(internalBar, fillInternalBar);
309 }
310
311 if (d->drawTextMode == KCapacityBar::DrawTextInline) {
312 p->restore();
313 }
314
315 p->save();
316 p->setClipping(false);
317 QRadialGradient topGradient(QPointF(rect.width() / 2, drawRect.top()), rect.width() / 2);
318 const QColor fillTopColor = KColorScheme::shade(palette().window().color(), KColorScheme::LightShade);
319 topGradient.setColorAt(0, QColor(fillTopColor.red(), fillTopColor.green(), fillTopColor.blue(), 127));
320 topGradient.setColorAt(1, Qt::transparent);
321 p->fillRect(QRect(rect.left(), rect.top() + drawRect.top(), rect.width(), 2), topGradient);
322 p->restore();
323
324 p->save();
325 p->setClipRect(QRect(-1, 0, rect.width(), drawRect.height() / 2), Qt::ReplaceClip);
326 QLinearGradient glassGradient(0, -5, 0, drawRect.height());
327 const QColor fillGlassColor = palette().base().color();
328 glassGradient.setColorAt(0, QColor(fillGlassColor.red(), fillGlassColor.green(), fillGlassColor.blue(), 255));
329 glassGradient.setColorAt(1, Qt::transparent);
330 p->fillPath(internalBar, glassGradient);
331 p->restore();
332
333 p->restore();
334
335 if (d->drawTextMode == KCapacityBar::DrawTextInline) {
336 QRect rect(drawRect);
337 rect.setHeight(rect.height() + 4);
338 p->drawText(rect, Qt::AlignCenter, fontMetrics().elidedText(d->text, Qt::ElideRight, drawRect.width() - 2 * ROUND_MARGIN));
339 } else {
340 p->drawText(rect, Qt::AlignBottom | d->horizontalTextAlignment, fontMetrics().elidedText(d->text, Qt::ElideRight, drawRect.width()));
341 }
342}
343
344void KCapacityBar::changeEvent(QEvent *event)
345{
346 if (event->type() == QEvent::StyleChange) {
347 d->ce_capacityBar = KStyle::customControlElement("CE_CapacityBar", this);
348 }
349
350 QWidget::changeEvent(event);
351}
352
353QSize KCapacityBar::minimumSizeHint() const
354{
355 int width = (d->drawTextMode == KCapacityBar::DrawTextInline) ?
356 fontMetrics().width(d->text) + ROUND_MARGIN * 2 :
357 fontMetrics().width(d->text);
358
359 int height = (d->drawTextMode == KCapacityBar::DrawTextInline) ?
360 qMax(fontMetrics().height(), d->barHeight) :
361 (d->text.isEmpty() ? 0 : fontMetrics().height() + VERTICAL_SPACING * 2) + d->barHeight;
362
363 if (height % 2) {
364 height++;
365 }
366
367 return QSize(width, height);
368}
369
370void KCapacityBar::paintEvent(QPaintEvent *event)
371{
372 QPainter p(this);
373 p.setClipRect(event->rect());
374 drawCapacityBar(&p, contentsRect());
375 p.end();
376}
377
378#include "kcapacitybar.moc"
KCapacityBar::value
int value
Definition: kcapacitybar.h:49
KCapacityBar::~KCapacityBar
~KCapacityBar()
Definition: kcapacitybar.cpp:72
KCapacityBar::setFillFullBlocks
void setFillFullBlocks(bool fillFullBlocks)
When the capacity bar is non-continuous, sets whether the last block shown should be drawn full or ca...
Definition: kcapacitybar.cpp:106
KCapacityBar::minimumSizeHint
virtual QSize minimumSizeHint() const
Definition: kcapacitybar.cpp:353
KCapacityBar::drawCapacityBar
void drawCapacityBar(QPainter *p, const QRect &rect) const
This method allows you to draw the widget, directly, for example on item delegates.
Definition: kcapacitybar.cpp:170
KCapacityBar::horizontalTextAlignment
Qt::Alignment horizontalTextAlignment
Definition: kcapacitybar.h:56
KCapacityBar::KCapacityBar
KCapacityBar(DrawTextMode drawTextMode=DrawTextOutline, QWidget *parent=0)
Capacity bar constructor.
Definition: kcapacitybar.cpp:65
KCapacityBar::paintEvent
virtual void paintEvent(QPaintEvent *event)
Definition: kcapacitybar.cpp:370
KCapacityBar::setValue
void setValue(int value)
Capacity bar fill value.
Definition: kcapacitybar.cpp:77
KCapacityBar::setContinuous
void setContinuous(bool continuous)
Sets whether the fill of the capacity bar should be continuous or in block mode.
Definition: kcapacitybar.cpp:117
KCapacityBar::setHorizontalTextAlignment
void setHorizontalTextAlignment(Qt::Alignment textAlignment)
If the capacity bar is in outline text mode, draw the text with textAlignment alignment.
Definition: kcapacitybar.cpp:141
KCapacityBar::drawTextMode
DrawTextMode drawTextMode
Definition: kcapacitybar.h:51
KCapacityBar::setBarHeight
void setBarHeight(int barHeight)
Sets the height (in pixels) of the bar.
Definition: kcapacitybar.cpp:128
KCapacityBar::barHeight
int barHeight
Definition: kcapacitybar.h:54
KCapacityBar::fillFullBlocks
bool fillFullBlocks
Definition: kcapacitybar.h:52
KCapacityBar::text
QString text
Definition: kcapacitybar.h:50
KCapacityBar::continuous
bool continuous
Definition: kcapacitybar.h:53
KCapacityBar::setDrawTextMode
void setDrawTextMode(DrawTextMode mode)
Set the way text is drawn if any is set.
Definition: kcapacitybar.cpp:159
KCapacityBar::changeEvent
virtual void changeEvent(QEvent *event)
Definition: kcapacitybar.cpp:344
KCapacityBar::setText
void setText(const QString &text)
Sets the text for the capacity bar.
Definition: kcapacitybar.cpp:88
KCapacityBar::DrawTextMode
DrawTextMode
Definition: kcapacitybar.h:60
KCapacityBar::DrawTextOutline
@ DrawTextOutline
If any text set, draw it out of the capacity bar.
Definition: kcapacitybar.h:62
KCapacityBar::DrawTextInline
@ DrawTextInline
If any text set, draw it into the capacity bar.
Definition: kcapacitybar.h:61
KColorScheme::DarkShade
@ DarkShade
The dark color is in between mid() and shadow().
Definition: kcolorscheme.h:292
KColorScheme::LightShade
@ LightShade
The light color is lighter than dark() or shadow() and contrasts with the base color.
Definition: kcolorscheme.h:280
KColorScheme::MidShade
@ MidShade
The mid color is in between base() and dark().
Definition: kcolorscheme.h:288
KColorScheme::shade
QColor shade(ShadeRole) const
Retrieve the requested shade color, using KColorScheme::background(KColorScheme::NormalBackground) as...
Definition: kcolorscheme.cpp:469
KStyle::customControlElement
static ControlElement customControlElement(const QString &element, const QWidget *widget)
Definition: kstyle.cpp:391
QWidget
VERTICAL_SPACING
#define VERTICAL_SPACING
Definition: kcapacitybar.cpp:39
ROUND_MARGIN
#define ROUND_MARGIN
Definition: kcapacitybar.cpp:38
kcapacitybar.h
kcolorscheme.h
kstyle.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

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

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal