OpenWalnut  1.5.0dev
WScaleLabel.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WSCALELABEL_H
26 #define WSCALELABEL_H
27 
28 #include <QLabel>
29 #include <QtCore/QString>
30 
31 #include "../WGuiConsts.h"
32 
33 /**
34  * Special Label that can shrink and expand in a layout
35  */
36 class WScaleLabel: public QLabel
37 {
38  Q_OBJECT
39 public:
40  /**
41  * Constructor
42  *
43  * \param parent the widgets parent
44  * \param length the minimal number of characters visible.
45  */
46  WScaleLabel( size_t length = WPREFERRED_LABEL_LENGTH, QWidget* parent = NULL );
47 
48  /**
49  * Constructor. Creates the label with its original text
50  *
51  * \param text text of the label
52  * \param parent the widgets parent
53  * \param length the minimal number of characters visible.
54  */
55  WScaleLabel( const QString &text, size_t length = WPREFERRED_LABEL_LENGTH, QWidget* parent = NULL );
56 
57  /**
58  * Constructor
59  *
60  * \param parent the widgets parent
61  */
62  explicit WScaleLabel( QWidget* parent );
63 
64  /**
65  * Constructor. Creates the label with its original text
66  *
67  * \param text text of the label
68  * \param parent the widgets parent
69  */
70  WScaleLabel( const QString &text, QWidget* parent );
71 
72  /**
73  * overwritten from QLabel, returning the widgets prefered size
74  *
75  * \return prefered size of the label
76  */
77  virtual QSize sizeHint() const;
78 
79  /**
80  * overwritten from QLabel, returning the widgets prefered size
81  *
82  * \return minimum size of the label
83  */
84  virtual QSize minimumSizeHint() const;
85 
86  /**
87  * reimplemented function to setText
88  *
89  * \param text text of the label
90  */
91  virtual void setText( const QString &text );
92 
93  /**
94  * Set this to reserve extra space for a margin. This function does not set the margin. This is still your task, using stylesheets. You will
95  * not need this when using QLabel::setMargin().
96  *
97  * \param margin the margin to keep in mind for size calculations
98  */
99  virtual void addAdditionalWidth( int margin );
100 
101  /**
102  * How many characters should be visible all the time?
103  *
104  * \param chars the number of chars
105  */
106  virtual void setMinimalLength( size_t chars );
107 
108  /**
109  * Get the current minimal number of characters
110  *
111  * \return the number of chars visible all the time
112  */
113  virtual size_t getMinimalLength() const;
114 
115  /**
116  * Calculate the size that is needed for the given number of chars.
117  *
118  * \param chars number of chars
119  *
120  * \return the size in pixels
121  */
122  virtual size_t calculateSize( size_t chars ) const;
123 protected:
124  /**
125  * custom implementation of the resize event
126  * to fit the QString into the labels current size
127  *
128  * \param event resize event passed from the parent widgets event handling
129  */
130  virtual void resizeEvent( QResizeEvent * event );
131 private:
132  /**
133  * Deferred construction.
134  */
135  void construct();
136 
137  /**
138  * set the actual text which is shown on the QLabel
139  */
140  void fitTextToSize();
141 
142  /**
143  * QString to remember the original unshortend text of the widget
144  */
145  QString m_orgText;
146 
147  /**
148  * The additional width we need to reserver (like for margins).
149  */
151 
152  /**
153  * Minimal character number
154  */
155  size_t m_minLength;
156 };
157 
158 #endif // WSCALELABEL_H
Special Label that can shrink and expand in a layout.
Definition: WScaleLabel.h:37
virtual size_t calculateSize(size_t chars) const
Calculate the size that is needed for the given number of chars.
Definition: WScaleLabel.cpp:88
void fitTextToSize()
set the actual text which is shown on the QLabel
virtual void setMinimalLength(size_t chars)
How many characters should be visible all the time?
void construct()
Deferred construction.
Definition: WScaleLabel.cpp:65
virtual QSize sizeHint() const
overwritten from QLabel, returning the widgets prefered size
Definition: WScaleLabel.cpp:78
size_t m_minLength
Minimal character number.
Definition: WScaleLabel.h:155
int m_additionalWidth
The additional width we need to reserver (like for margins).
Definition: WScaleLabel.h:150
virtual QSize minimumSizeHint() const
overwritten from QLabel, returning the widgets prefered size
Definition: WScaleLabel.cpp:83
virtual size_t getMinimalLength() const
Get the current minimal number of characters.
virtual void resizeEvent(QResizeEvent *event)
custom implementation of the resize event to fit the QString into the labels current size
Definition: WScaleLabel.cpp:73
WScaleLabel(size_t length=WPREFERRED_LABEL_LENGTH, QWidget *parent=NULL)
Constructor.
Definition: WScaleLabel.cpp:31
virtual void addAdditionalWidth(int margin)
Set this to reserve extra space for a margin.
virtual void setText(const QString &text)
reimplemented function to setText
Definition: WScaleLabel.cpp:93
QString m_orgText
QString to remember the original unshortend text of the widget.
Definition: WScaleLabel.h:145