OpenWalnut  1.5.0dev
WScaleToolButton.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 "WScaleToolButton.h"
26 
27 WScaleToolButton::WScaleToolButton( size_t length, QWidget *parent /*= NULL */ ):
28  QToolButton( parent ),
29  m_additionalWidth( 0 ),
30  m_minLength( length )
31 {
32  setMinimumWidth( fontMetrics().horizontalAdvance( m_orgText.left( m_minLength ) + tr( ".." ) ) + m_additionalWidth );
33  setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Maximum );
34 }
35 
36 WScaleToolButton::WScaleToolButton( const QString &text, size_t length, QWidget *parent /*= NULL */ ) :
37  QToolButton( parent ),
38  m_orgText( text ),
39  m_additionalWidth( 0 ),
40  m_minLength( length )
41 {
42  setText( text );
43  setMinimumWidth( fontMetrics().horizontalAdvance( m_orgText.left( m_minLength ) + tr( ".." ) ) + m_additionalWidth );
44  setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Maximum );
45 }
46 
47 void WScaleToolButton::resizeEvent( QResizeEvent* /*event*/ )
48 {
49  fitTextToSize();
50 }
51 
53 {
54  return QSize( fontMetrics().horizontalAdvance( m_orgText ) + m_additionalWidth,
55  QToolButton::sizeHint().height() );
56 }
57 
59 {
60  return QSize( fontMetrics().horizontalAdvance( m_orgText.left( m_minLength ) + tr( ".." ) ) + m_additionalWidth,
61  QToolButton::minimumSizeHint().height() );
62 }
63 
64 void WScaleToolButton::setText( const QString &text )
65 {
66  m_orgText = text;
67  setMinimumWidth( fontMetrics().horizontalAdvance( m_orgText.left( m_minLength ) + tr( ".." ) ) + m_additionalWidth );
68  fitTextToSize();
69 }
70 
72 {
73  int newwidth = width();
74  QFontMetrics fn = fontMetrics();
75  if( newwidth < fn.horizontalAdvance( m_orgText ) )
76  {
77  QString useText = m_orgText.left( m_orgText.length() - 1 );
78  while( fn.horizontalAdvance( useText + tr( ".." ) ) > newwidth || useText.length() == 0 )
79  {
80  useText = useText.left( useText.length() - 1 );
81  }
82  QToolButton::setText( useText + tr( ".." ) );
83  }
84  else
85  {
86  QToolButton::setText( m_orgText );
87  }
88 }
89 
91 {
93 }
94 
96 {
97  setText( m_orgText );
98  m_minLength = chars;
99 }
100 
102 {
103  return m_minLength;
104 }
virtual void resizeEvent(QResizeEvent *event)
custom implementation of the resize event to fit the QString into the butons current size
int m_additionalWidth
The additional width we need to reserver (like for margins).
WScaleToolButton(size_t length=WPREFERRED_LABEL_LENGTH, QWidget *parent=NULL)
Constructor.
virtual QSize sizeHint() const
overwritten from QToolButton, returning the widgets prefered size
virtual void setMinimalLength(size_t chars)
How many characters should be visible all the time?
virtual void addAdditionalWidth(int margin)
Set this to reserve extra space for a margin.
void fitTextToSize()
set the actual text which is shown on the QToolButton
QString m_orgText
QString to remember the original unshortend text of the widget.
virtual QSize minimumSizeHint() const
overwritten from QToolButton, returning the widgets prefered size
size_t m_minLength
Minimal character number.
virtual void setText(const QString &text)
reimplemented function to setText
virtual size_t getMinimalLength() const
Get the current minimal number of characters.