KWidgetsAddons

kcapacitybar.h
1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2008 Rafael Fernández López <ereslibre@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#ifndef KCAPACITYBAR_H
9#define KCAPACITYBAR_H
10
11#include <QStyle>
12#include <QWidget>
13#include <memory>
14
15#include <kwidgetsaddons_export.h>
16
17class QPaintEvent;
18
19/**
20 * @class KCapacityBar kcapacitybar.h KCapacityBar
21 *
22 * @brief This widget shows a bar which is filled to show the level of usage of
23 * a certain device.
24 *
25 * This widget represents a bar which goal is to show the level of usage of a
26 * device. Its look is similar to a progress bar, but different, because this
27 * widget does not want to give a notion of progress.
28 *
29 * @since 4.2
30 *
31 * \image html kcapacitybar.png "KCapacityBar Widget"
32 *
33 * @author Rafael Fernández López <ereslibre@kde.org>
34 */
35class KWIDGETSADDONS_EXPORT KCapacityBar : public QWidget
36{
38
39 Q_PROPERTY(int value READ value WRITE setValue)
40 Q_PROPERTY(QString text READ text WRITE setText)
41 Q_PROPERTY(DrawTextMode drawTextMode READ drawTextMode WRITE setDrawTextMode)
42 Q_PROPERTY(bool fillFullBlocks READ fillFullBlocks WRITE setFillFullBlocks)
43 Q_PROPERTY(bool continuous READ continuous WRITE setContinuous)
44 Q_PROPERTY(int barHeight READ barHeight WRITE setBarHeight)
45 Q_PROPERTY(Qt::Alignment horizontalTextAlignment READ horizontalTextAlignment WRITE setHorizontalTextAlignment)
46
47public:
49 DrawTextInline = 0, ///< If any text set, draw it into the capacity bar
50 DrawTextOutline, ///< If any text set, draw it out of the capacity bar
51 };
52 Q_ENUM(DrawTextMode)
53
54 /**
55 * Constructs a capacity bar with DrawTextOutline as draw text mode.
56 * @param parent The parent of the widget.
57 * @since 5.24
58 */
59 explicit KCapacityBar(QWidget *parent = nullptr);
60
61 /**
62 * Capacity bar constructor.
63 *
64 * @param drawTextMode If any text set, whether to draw it into the capacity bar
65 * or not.
66 * @param parent The parent of the widget.
67 */
68 explicit KCapacityBar(DrawTextMode drawTextMode, QWidget *parent = nullptr);
69 ~KCapacityBar() override;
70
71 /**
72 * Capacity bar fill value.
73 *
74 * @param value This parameter can take values from 0 to 100.
75 *
76 * @note Its value is 0 by default.
77 */
78 void setValue(int value);
79
80 /**
81 * @return The fill value of the capacity bar.
82 */
83 int value() const;
84
85 /**
86 * Sets the text for the capacity bar.
87 *
88 * @param text The text that the capacity bar will show.
89 *
90 * @note This is an empty string by default.
91 */
92 void setText(const QString &text);
93
94 /**
95 * @return The text that the capacity bar will show.
96 */
97 QString text() const;
98
99 /**
100 * When the capacity bar is non-continuous, sets whether the last block
101 * shown should be drawn full or can be cut off (depending on the capacity
102 * bar width, and the value set on it).
103 *
104 * @param fillFullBlocks If true, the last block drawn will be fully filled,
105 * on other case, the last block drawn could be cut off.
106 *
107 * @note This method is only relevant if the capacity bar is in
108 * non-continuous mode.
109 *
110 * @note Its value is true by default.
111 *
112 * @see setContinuous, continuous
113 */
114 void setFillFullBlocks(bool fillFullBlocks);
115
116 /**
117 * @return Whether the last block shown can be cut off when necessary.
118 */
119 bool fillFullBlocks() const;
120
121 /**
122 * Sets whether the fill of the capacity bar should be continuous or in
123 * block mode.
124 *
125 * @param continuous If true, the fill of the capacity bar is done in a
126 * continuous way. In other case, the fill is done with
127 * separated blocks.
128 *
129 * @note Its value is true by default.
130 */
131 void setContinuous(bool continuous);
132
133 /**
134 * @return Whether the fill of the capacity bar should be continuous or
135 * block-based.
136 */
137 bool continuous() const;
138
139 /**
140 * Sets the height (in pixels) of the bar.
141 *
142 * @param barHeight The preferred height (in pixels) of the capacity bar.
143 *
144 * @note If you set a certain text and the capacity bar is in inline mode,
145 * the height of the bar will be the maximum of the font height and
146 * this value.
147 *
148 * @note If you set a certain text and the capacity bar is in outline mode,
149 * the height of the whole capacity bar will be bigger than this
150 * value. Take in count the height of this widget is got from adding
151 * the bar height, the font metrics height and a small separator
152 * between the bar and the outline text.
153 *
154 * @note Its value is 12 pixels by default.
155 */
156 void setBarHeight(int barHeight);
157
158 /**
159 * @return The preferred height of the capacity bar.
160 */
161 int barHeight() const;
162
163 /**
164 * If the capacity bar is in outline text mode, draw the text with
165 * @p textAlignment alignment.
166 *
167 * @param textAlignment Sets the horizontal alignment for the text if
168 * the capacity bar is in outline text mode.
169 *
170 * @note If @p textAlignemt contains vertical alignment flags, they will be
171 * ignored.
172 *
173 * @note If the capacity bar is in inline text mode, the text is always
174 * centered, and both vertical and horizontal flags set through this
175 * method are ignored.
176 *
177 * @note Its value is centered by default.
178 */
179 void setHorizontalTextAlignment(Qt::Alignment textAlignment);
180
181 /**
182 * @return The horizontal alignment for the text that will be drawn.
183 */
184 Qt::Alignment horizontalTextAlignment() const;
185
186 /**
187 * Set the way text is drawn if any is set
188 *
189 * @param mode If any text set, whether to draw it into the capacity bar
190 * or not.
191 */
192 void setDrawTextMode(DrawTextMode mode);
193
194 /**
195 * The way text is drawn, inside the capacity bar or outside of it
196 */
197 DrawTextMode drawTextMode() const;
198
199 /**
200 * This method allows you to draw the widget, directly, for example on
201 * item delegates. You only need the painter object and the rect where
202 * this widget should be drawn.
203 */
204 void drawCapacityBar(QPainter *p, const QRect &rect) const;
205
206 /**
207 * This method allows you to draw the widget, directly, for example on
208 * item delegates. You need the painter object and the rect where
209 * this widget should be drawn, and the state that it should be painted in.
210 *
211 * For example, when in a selected delegate, setting the correct state
212 * ensures that the bar keeps sufficient contrast to the selection color.
213 *
214 * @since 6.12
215 */
216 void drawCapacityBar(QPainter *p, const QRect &rect, QStyle::State state) const;
217
218 // Reimplemented from QWidget
219 QSize minimumSizeHint() const override;
220
221protected:
222 // Reimplemented from QWidget
223 void paintEvent(QPaintEvent *event) override;
224
225 // Reimplemented from QWidget
226 void changeEvent(QEvent *event) override;
227
228private:
229 /**
230 * @internal
231 */
232 std::unique_ptr<class KCapacityBarPrivate> const d;
233};
234
235#endif
This widget shows a bar which is filled to show the level of usage of a certain device.
void setFillFullBlocks(bool fillFullBlocks)
When the capacity bar is non-continuous, sets whether the last block shown should be drawn full or ca...
void setValue(int value)
Capacity bar fill value.
void setContinuous(bool continuous)
Sets whether the fill of the capacity bar should be continuous or in block mode.
void setHorizontalTextAlignment(Qt::Alignment textAlignment)
If the capacity bar is in outline text mode, draw the text with textAlignment alignment.
void setBarHeight(int barHeight)
Sets the height (in pixels) of the bar.
KCapacityBar(QWidget *parent=nullptr)
Constructs a capacity bar with DrawTextOutline as draw text mode.
void setDrawTextMode(DrawTextMode mode)
Set the way text is drawn if any is set.
void setText(const QString &text)
Sets the text for the capacity bar.
@ DrawTextOutline
If any text set, draw it out of the capacity bar.
@ DrawTextInline
If any text set, draw it into the capacity bar.
Q_ENUM(...)
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
typedef State
typedef Alignment
QWidget(QWidget *parent, Qt::WindowFlags f)
virtual void changeEvent(QEvent *event)
virtual void paintEvent(QPaintEvent *event)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 28 2025 12:02:04 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.