OpenWalnut  1.5.0dev
WScaleLabel.cpp
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 #include <cmath>
26 
27 #include "../WGuiConsts.h"
28 
29 #include "WScaleLabel.h"
30 
31 WScaleLabel::WScaleLabel( size_t length, QWidget* parent ):
32  QLabel( parent ),
33  m_additionalWidth( 0 ),
34  m_minLength( length )
35 {
36  construct();
37 }
38 
39 WScaleLabel::WScaleLabel( const QString &text, size_t length, QWidget* parent ) :
40  QLabel( text, parent ),
41  m_orgText( text ),
42  m_additionalWidth( 0 ),
43  m_minLength( length )
44 {
45  construct();
46 }
47 
48 WScaleLabel::WScaleLabel( QWidget* parent ):
49  QLabel( parent ),
50  m_additionalWidth( 0 ),
51  m_minLength( WPREFERRED_LABEL_LENGTH )
52 {
53  construct();
54 }
55 
56 WScaleLabel::WScaleLabel( const QString &text, QWidget* parent ):
57  QLabel( text, parent ),
58  m_orgText( text ),
59  m_additionalWidth( 0 ),
60  m_minLength( WPREFERRED_LABEL_LENGTH )
61 {
62  construct();
63 }
64 
66 {
67  setMinimumWidth( fontMetrics().horizontalAdvance( m_orgText.left( m_minLength ) + tr( "..." ) ) + m_additionalWidth );
68  setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Maximum );
69  setMargin( WGLOBAL_MARGIN );
70  setTextInteractionFlags( Qt::TextSelectableByMouse );
71 }
72 
73 void WScaleLabel::resizeEvent( QResizeEvent* /*event*/ )
74 {
75  fitTextToSize();
76 }
77 
78 QSize WScaleLabel::sizeHint() const
79 {
80  return QSize( calculateSize( m_orgText.length() ), QLabel::sizeHint().height() );
81 }
82 
84 {
85  return QSize( calculateSize( m_minLength ), QLabel::minimumSizeHint().height() );
86 }
87 
88 size_t WScaleLabel::calculateSize( size_t chars ) const
89 {
90  return fontMetrics().horizontalAdvance( m_orgText.left( chars ) + tr( "..." ) ) + 2 * margin() + m_additionalWidth;
91 }
92 
93 void WScaleLabel::setText( const QString &text )
94 {
95  m_orgText = text;
96  setMinimumWidth( fontMetrics().horizontalAdvance( m_orgText.left( m_minLength ) + tr( "..." ) ) + 2 * margin() + m_additionalWidth );
97  fitTextToSize();
98 }
99 
101 {
102  QString useText = fontMetrics().elidedText( m_orgText, Qt::ElideRight, width() );
103  QLabel::setText( useText );
104 }
105 
107 {
108  m_additionalWidth = w;
109 }
110 
111 void WScaleLabel::setMinimalLength( size_t chars )
112 {
113  setText( m_orgText );
114  m_minLength = chars;
115 }
116 
118 {
119  return m_minLength;
120 }
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