KWidgetsAddons

kruler.h
1/* -*- c++ -*- */
2/*
3 This file is part of the KDE libraries
4 SPDX-FileCopyrightText: 1998 Jörg Habenicht <j.habenicht@europemail.com>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#ifndef KRULER_H
10#define KRULER_H
11
12#include <kwidgetsaddons_export.h>
13
14#include <QAbstractSlider>
15#include <memory>
16
17/**
18 * @class KRuler kruler.h KRuler
19 *
20 * A ruler widget.
21 *
22 * The vertical ruler looks similar to this:
23 *
24 *\code
25 * meters inches
26 *
27 * ------ <--- end mark ---> ------
28 * -- -
29 * -- <---little mark---> --
30 * -- -
31 * -- ---
32 * --- <---medium mark -
33 * -- --
34 * -- tiny mark----> -
35 * -- ----
36 * -- -
37 * ---- <-----big mark --
38 * -- -
39 * |>-- <--ruler pointer--> |>--
40 *
41 * \endcode
42 *
43 * There are tiny marks, little marks, medium marks, and big marks along the
44 * ruler.
45 *
46 * To receive mouse clicks or mouse moves, the class has to be overloaded.
47 *
48 * \image html kruler.png "KRuler Widget"
49 *
50 * @short A ruler widget.
51 * @author Jörg Habenicht
52 */
53class KWIDGETSADDONS_EXPORT KRuler : public QAbstractSlider
54{
56 Q_PROPERTY(bool showTinyMarks READ showTinyMarks WRITE setShowTinyMarks)
57 Q_PROPERTY(bool showLittleMarks READ showLittleMarks WRITE setShowLittleMarks)
58 Q_PROPERTY(bool showMediumMarks READ showMediumMarks WRITE setShowMediumMarks)
59 Q_PROPERTY(bool showBigMarks READ showBigMarks WRITE setShowBigMarks)
60 Q_PROPERTY(bool showPointer READ showPointer WRITE setShowPointer)
61 Q_PROPERTY(bool showEndLabel READ showEndLabel WRITE setShowEndLabel)
62 Q_PROPERTY(int tinyMarkDistance READ tinyMarkDistance WRITE setTinyMarkDistance)
63 Q_PROPERTY(int littleMarkDistance READ littleMarkDistance WRITE setLittleMarkDistance)
64 Q_PROPERTY(int mediumMarkDistance READ mediumMarkDistance WRITE setBigMarkDistance)
65 Q_PROPERTY(int bigMarkDistance READ bigMarkDistance WRITE setBigMarkDistance)
66 Q_PROPERTY(double pixelPerMark READ pixelPerMark WRITE setPixelPerMark)
67 Q_PROPERTY(bool lengthFixed READ lengthFixed WRITE setLengthFixed)
68 Q_PROPERTY(QString endLabel READ endLabel WRITE setEndLabel)
69 Q_PROPERTY(int length READ length WRITE setLength)
70 Q_PROPERTY(int offset READ offset)
71 Q_PROPERTY(int endOffset READ endOffset)
72
73public:
74 /**
75 * The types of units used.
76 */
78 Custom = 0,
79 Pixel,
80 Inch,
81 Millimetres,
82 Centimetres,
83 Metres
84 };
85 Q_ENUM(MetricStyle)
86
87 /**
88 * Constructs a horizontal ruler.
89 */
90 explicit KRuler(QWidget *parent = nullptr);
91 /**
92 * Constructs a ruler with orientation @p orient.
93 *
94 * @p parent and @p f are passed to QFrame.
95 * The default look is a raised widget
96 * but may be changed with the inherited QFrame methods.
97 *
98 * @param orient Orientation of the ruler.
99 * @param parent Will be handed over to QFrame.
100 * @param f Will be handed over to QFrame.
101 *
102 */
103 explicit KRuler(Qt::Orientation orient, QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
104
105 /**
106 * Constructs a ruler with orientation @p orient and initial width @p widgetWidth.
107 *
108 * The width sets the fixed width of the widget. This is useful if you
109 * want to draw the ruler bigger or smaller than the default size.
110 * @note The size of the marks doesn't change.
111 * @p parent and @p f are passed to QFrame.
112 *
113 * @param orient Orientation of the ruler.
114 * @param widgetWidth Fixed width of the widget.
115 * @param parent Will be handed over to QFrame.
116 * @param f Will be handed over to QFrame.
117 *
118 */
119 KRuler(Qt::Orientation orient, int widgetWidth, QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
120
121 /**
122 * Destructor.
123 */
124 ~KRuler() override;
125
126 /**
127 * Sets the distance between tiny marks.
128 *
129 * This is mostly used in the English system (inches) with distance of 1.
130 */
131 void setTinyMarkDistance(int);
132 /**
133 * Returns the distance between tiny marks.
134 */
135 int tinyMarkDistance() const;
136
137 /**
138 * Sets the distance between little marks.
139 *
140 * The default value is 1 in the metric system and 2 in the English (inches) system.
141 */
142 void setLittleMarkDistance(int);
143
144 /**
145 * Returns the distance between little marks.
146 */
147 int littleMarkDistance() const;
148
149 /**
150 * Sets the distance between medium marks.
151 *
152 * For English (inches) styles it defaults to twice the little mark distance.
153 * For metric styles it defaults to five times the little mark distance.
154 */
155 void setMediumMarkDistance(int);
156 int mediumMarkDistance() const;
157
158 /**
159 * Sets distance between big marks.
160 *
161 * For English (inches) or metric styles it is twice the medium mark distance.
162 */
163 void setBigMarkDistance(int);
164 /**
165 * Returns the distance between big marks.
166 */
167 int bigMarkDistance() const;
168
169 /**
170 * Shows/hides tiny marks.
171 */
172 void setShowTinyMarks(bool);
173 bool showTinyMarks() const;
174 /**
175 * Shows/hides little marks.
176 */
177 void setShowLittleMarks(bool);
178 bool showLittleMarks() const;
179 /**
180 * Shows/hides medium marks.
181 */
182 void setShowMediumMarks(bool);
183 bool showMediumMarks() const;
184 /**
185 * Shows/hides big marks.
186 */
187 void setShowBigMarks(bool);
188 bool showBigMarks() const;
189 /**
190 * Shows/hides end marks.
191 */
192 void setShowEndMarks(bool);
193 bool showEndMarks() const;
194 /**
195 * Shows/hides the pointer.
196 */
197 void setShowPointer(bool);
198 bool showPointer() const;
199
200 /**
201 * Show/hide number values of the end marks.
202 *
203 * Default is @p false.
204 */
205 void setShowEndLabel(bool);
206 bool showEndLabel() const;
207
208 /**
209 * Sets the label this is drawn at the beginning of the visible part
210 * of the ruler to @p label
211 */
212 void setEndLabel(const QString &);
213 QString endLabel() const;
214
215 /**
216 * Sets up the necessary tasks for the provided styles.
217 *
218 * A convenience method.
219 */
221
222 /**
223 * Sets the number of pixels between two base marks.
224 *
225 * Calling this method stretches or shrinks your ruler.
226 *
227 * For pixel display ( MetricStyle) the value is 10.0 marks
228 * per pixel ;-)
229 * For English (inches) it is 9.0, and for centimetres ~2.835 -> 3.0 .
230 * If you want to magnify your part of display, you have to
231 * adjust the mark distance @p here.
232 * Notice: The double type is only supported to give the possibility
233 * of having some double values.
234 * It should be used with care. Using values below 10.0
235 * shows visible jumps of markpositions (e.g. 2.345).
236 * Using whole numbers is highly recommended.
237 * To use @p int values use setPixelPerMark((int)your_int_value);
238 * default: 1 mark per 10 pixels
239 */
240 void setPixelPerMark(double rate);
241
242 /**
243 * Returns the number of pixels between two base marks.
244 */
245 double pixelPerMark() const;
246
247 /**
248 * Sets the length of the ruler, i.e. the difference between
249 * the begin mark and the end mark of the ruler.
250 *
251 * Same as (width() - offset())
252 *
253 * when the length is not locked, it gets adjusted with the
254 * length of the widget.
255 */
256 void setLength(int);
257 int length() const;
258
259 /**
260 * Locks the length of the ruler, i.e. the difference between
261 * the two end marks doesn't change when the widget is resized.
262 *
263 * @param fix fixes the length, if true
264 */
265 void setLengthFixed(bool fix);
266 bool lengthFixed() const;
267
268 /**
269 * Sets the number of pixels by which the ruler may slide up or left.
270 * The number of pixels moved is realive to the previous position.
271 * The Method makes sense for updating a ruler, which is working with
272 * a scrollbar.
273 *
274 * This doesn't affect the position of the ruler pointer.
275 * Only the visible part of the ruler is moved.
276 *
277 * @param count Number of pixel moving up or left relative to the previous position
278 */
279 void slideUp(int count = 1);
280
281 /**
282 * Sets the number of pixels by which the ruler may slide down or right.
283 * The number of pixels moved is realive to the previous position.
284 * The Method makes sense for updating a ruler, which is working with
285 * a scrollbar.
286 *
287 * This doesn't affect the position of the ruler pointer.
288 * Only the visible part of the ruler is moved.
289 *
290 * @param count Number of pixel moving up or left relative to the previous position
291 */
292 void slideDown(int count = 1);
293
294 /**
295 * Sets the ruler slide offset.
296 *
297 * This is like slideup() or slidedown() with an absolute offset
298 * from the start of the ruler.
299 *
300 * @param offset Number of pixel to move the ruler up or left from the beginning
301 */
302 void setOffset(int offset);
303
304 /**
305 * Returns the current ruler offset.
306 */
307 int offset() const;
308
309 int endOffset() const;
310
311public Q_SLOTS:
312
313 /**
314 * Sets the pointer to a new position.
315 *
316 * The offset is NOT updated.
317 * QWidget::repaint() is called afterwards.
318 */
319 void slotNewValue(int);
320
321 /**
322 * Sets the ruler marks to a new position.
323 *
324 * The pointer is NOT updated.
325 * QWidget::repaint() is called afterwards.
326 */
327 void slotNewOffset(int);
328
329 void slotEndOffset(int);
330
331protected:
332 void paintEvent(QPaintEvent *) override;
333
334private:
335 KWIDGETSADDONS_NO_EXPORT void initWidget(Qt::Orientation orientation);
336
337private:
338 std::unique_ptr<class KRulerPrivate> const d;
339};
340
341#endif
A ruler widget.
Definition kruler.h:54
void setMediumMarkDistance(int)
Sets the distance between medium marks.
Definition kruler.cpp:190
void slotNewOffset(int)
Sets the ruler marks to a new position.
Definition kruler.cpp:508
void setPixelPerMark(double rate)
Sets the number of pixels between two base marks.
Definition kruler.cpp:402
void setShowEndLabel(bool)
Show/hide number values of the end marks.
Definition kruler.cpp:294
KRuler(QWidget *parent=nullptr)
Constructs a horizontal ruler.
Definition kruler.cpp:98
void setLittleMarkDistance(int)
Sets the distance between little marks.
Definition kruler.cpp:177
void setShowBigMarks(bool)
Shows/hides big marks.
Definition kruler.cpp:255
void setShowTinyMarks(bool)
Shows/hides tiny marks.
Definition kruler.cpp:216
void slideUp(int count=1)
Sets the number of pixels by which the ruler may slide up or left.
Definition kruler.cpp:468
void setRulerMetricStyle(KRuler::MetricStyle)
Sets up the necessary tasks for the provided styles.
Definition kruler.cpp:326
void setLengthFixed(bool fix)
Locks the length of the ruler, i.e.
Definition kruler.cpp:436
void setEndLabel(const QString &)
Sets the label this is drawn at the beginning of the visible part of the ruler to label.
Definition kruler.cpp:307
void setOffset(int offset)
Sets the ruler slide offset.
Definition kruler.cpp:446
~KRuler() override
Destructor.
void slideDown(int count=1)
Sets the number of pixels by which the ruler may slide down or right.
Definition kruler.cpp:476
void setShowEndMarks(bool)
Shows/hides end marks.
Definition kruler.cpp:268
void setBigMarkDistance(int)
Sets distance between big marks.
Definition kruler.cpp:203
void setTinyMarkDistance(int)
Sets the distance between tiny marks.
Definition kruler.cpp:164
void slotNewValue(int)
Sets the pointer to a new position.
Definition kruler.cpp:484
void setShowPointer(bool)
Shows/hides the pointer.
Definition kruler.cpp:281
void setShowLittleMarks(bool)
Shows/hides little marks.
Definition kruler.cpp:229
void setLength(int)
Sets the length of the ruler, i.e.
Definition kruler.cpp:414
MetricStyle
The types of units used.
Definition kruler.h:77
void setShowMediumMarks(bool)
Shows/hides medium marks.
Definition kruler.cpp:242
QAbstractSlider(QWidget *parent)
Q_ENUM(...)
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
Q_SLOTSQ_SLOTS
Orientation
typedef WindowFlags
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.