WScaleLabel.cpp 3.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

#include "../WGuiConsts.h"

#include "WScaleLabel.h"
28
#include "WScaleLabel.moc"
29

30
WScaleLabel::WScaleLabel( size_t length, QWidget* parent ):
31
    QLabel( parent ),
32 33
    m_additionalWidth( 0 ),
    m_minLength( length )
34
{
35
    construct();
36 37
}

38
WScaleLabel::WScaleLabel( const QString &text, size_t length, QWidget* parent ) :
39
    QLabel( text, parent ),
40
    m_orgText( text ),
41 42
    m_additionalWidth( 0 ),
    m_minLength( length )
43
{
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    construct();
}

WScaleLabel::WScaleLabel( QWidget* parent ):
    QLabel( parent ),
    m_additionalWidth( 0 ),
    m_minLength( WPREFERRED_LABEL_LENGTH )
{
    construct();
}

WScaleLabel::WScaleLabel( const QString &text, QWidget* parent ):
    QLabel( text, parent ),
    m_orgText( text ),
    m_additionalWidth( 0 ),
    m_minLength( WPREFERRED_LABEL_LENGTH )
{
    construct();
}

void WScaleLabel::construct()
{
    setMinimumWidth( fontMetrics().width( m_orgText.left( m_minLength ) + tr( ".." ) ) + m_additionalWidth );
67 68 69
    setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Maximum );
    setMargin( WGLOBAL_MARGIN );
    setTextInteractionFlags( Qt::TextSelectableByMouse );
70 71 72 73 74 75 76 77 78
}

void WScaleLabel::resizeEvent( QResizeEvent* /*event*/ )
{
    fitTextToSize();
}

QSize WScaleLabel::sizeHint() const
{
79
    return QSize( calculateSize( m_orgText.length() ), QLabel::sizeHint().height() );
80 81 82 83
}

QSize WScaleLabel::minimumSizeHint() const
{
84 85 86 87 88 89
    return QSize( calculateSize( m_minLength ),QLabel::minimumSizeHint().height() );
}

size_t WScaleLabel::calculateSize( size_t chars ) const
{
    return fontMetrics().width( m_orgText.left( chars ) + tr( ".." ) ) + 2 * margin() + m_additionalWidth;
90
}
91 92 93 94

void WScaleLabel::setText( const QString &text )
{
    m_orgText = text;
95
    setMinimumWidth( fontMetrics().width( m_orgText.left( m_minLength ) + tr( ".." ) ) + 2 * margin() + m_additionalWidth );
96 97 98 99 100
    fitTextToSize();
}

void WScaleLabel::fitTextToSize()
{
101 102
    int newwidth = width();
    QFontMetrics fn = fontMetrics();
103
    if( newwidth < fn.width( m_orgText ) )
104 105
    {
        QString useText = m_orgText.left( m_orgText.length() - 1 );
106
        while( fn.width( useText + tr( ".." ) ) > newwidth || useText.length() == 0 )
107 108 109 110 111 112
        {
            useText = useText.left( useText.length() - 1 );
        }
        QLabel::setText( useText + tr( ".." ) );
    }
    else
113
    {
114
        QLabel::setText( m_orgText );
115
    }
116
}
117 118 119 120 121

void WScaleLabel::addAdditionalWidth( int w )
{
    m_additionalWidth = w;
}
122 123 124 125 126 127 128 129 130 131 132

void WScaleLabel::setMinimalLength( size_t chars )
{
    setText( m_orgText );
    m_minLength = chars;
}

size_t WScaleLabel::getMinimalLength() const
{
    return m_minLength;
}