PageRenderTime 103ms CodeModel.GetById 40ms app.highlight 20ms RepoModel.GetById 40ms app.codeStats 0ms

/src/libtomahawk/widgets/ElidedLabel.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 248 lines | 179 code | 51 blank | 18 comment | 12 complexity | a9c66392617c6acae06a787bf358cf6f MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4 *
  5 *   Tomahawk is free software: you can redistribute it and/or modify
  6 *   it under the terms of the GNU General Public License as published by
  7 *   the Free Software Foundation, either version 3 of the License, or
  8 *   (at your option) any later version.
  9 *
 10 *   Tomahawk is distributed in the hope that it will be useful,
 11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 13 *   GNU General Public License for more details.
 14 *
 15 *   You should have received a copy of the GNU General Public License
 16 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 17 */
 18
 19#include "ElidedLabel.h"
 20
 21#include <QEvent>
 22#include <QPainter>
 23#include <QFontMetrics>
 24#include <QApplication>
 25#include <QRect>
 26#include <QTextLayout>
 27
 28#include "utils/Logger.h"
 29
 30
 31ElidedLabel::ElidedLabel( QWidget* parent, Qt::WindowFlags flags )
 32    : QFrame( parent, flags )
 33{
 34    init();
 35}
 36
 37
 38ElidedLabel::ElidedLabel( const QString& text, QWidget* parent, Qt::WindowFlags flags )
 39    : QFrame( parent, flags )
 40{
 41    init( text );
 42}
 43
 44
 45ElidedLabel::~ElidedLabel()
 46{
 47}
 48
 49
 50QString
 51ElidedLabel::text() const
 52{
 53    return m_text;
 54}
 55
 56
 57void
 58ElidedLabel::setText( const QString& text )
 59{
 60    if ( m_text != text )
 61    {
 62        m_text = text;
 63        updateLabel();
 64        emit textChanged( text );
 65    }
 66}
 67
 68
 69Qt::Alignment
 70ElidedLabel::alignment() const
 71{
 72    return m_align;
 73}
 74
 75
 76void
 77ElidedLabel::setAlignment( Qt::Alignment alignment )
 78{
 79    if ( m_align != alignment )
 80    {
 81        m_align = alignment;
 82        update(); // no geometry change, repaint is sufficient
 83    }
 84}
 85
 86
 87Qt::TextElideMode
 88ElidedLabel::elideMode() const
 89{
 90    return m_mode;
 91}
 92
 93
 94void
 95ElidedLabel::setElideMode( Qt::TextElideMode mode )
 96{
 97    if ( m_mode != mode )
 98    {
 99        m_mode = mode;
100        updateLabel();
101    }
102}
103
104
105void
106ElidedLabel::setMargin( int margin )
107{
108    m_margin = margin;
109}
110
111
112int
113ElidedLabel::margin() const
114{
115    return m_margin;
116}
117
118
119void
120ElidedLabel::setFont( const QFont& font )
121{
122    QWidget::setFont( font );
123    updateLabel();
124}
125
126
127void
128ElidedLabel::init( const QString& txt )
129{
130    m_text = txt;
131    m_align = Qt::AlignLeft;
132    m_mode = Qt::ElideRight;
133    m_margin = 0;
134    m_multiLine = false;
135
136    setContentsMargins( 0, 0, 0, 0 );
137}
138
139
140void
141ElidedLabel::updateLabel()
142{
143    updateGeometry();
144    update();
145}
146
147
148QSize
149ElidedLabel::sizeHint() const
150{
151    const QFontMetrics& fm = fontMetrics();
152    return QSize( fm.width( m_text ) + m_margin * 2, fm.height() + m_margin * 2 );
153}
154
155
156QSize
157ElidedLabel::minimumSizeHint() const
158{
159    switch ( m_mode )
160    {
161        case Qt::ElideNone:
162            return sizeHint();
163
164        default:
165        {
166            const QFontMetrics& fm = fontMetrics();
167            QSize size( fm.width( "..." ), fm.height() );
168            return size;
169        }
170    }
171}
172
173
174void
175ElidedLabel::paintEvent( QPaintEvent* event )
176{
177    QFrame::paintEvent( event );
178    QPainter p( this );
179    p.setRenderHint( QPainter::TextAntialiasing );
180
181    QRect r = contentsRect();
182    r.adjust( m_margin, m_margin, -m_margin, -m_margin );
183
184    if ( m_multiLine )
185    {
186        QTextLayout textLayout( m_text );
187        textLayout.setFont( p.font() );
188        int widthUsed = 0;
189        int lineCount = 0;
190        int lineLimit = r.height() / fontMetrics().height();
191
192        textLayout.beginLayout();
193        while ( ++lineCount < lineLimit )
194        {
195            QTextLine line = textLayout.createLine();
196            if ( !line.isValid() )
197                break;
198
199            line.setLineWidth( r.width() );
200            widthUsed += line.naturalTextWidth();
201        }
202        textLayout.endLayout();
203        widthUsed += r.width();
204
205        const QString elidedText = fontMetrics().elidedText( m_text, Qt::ElideRight, widthUsed );
206        p.drawText( r, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, elidedText );
207    }
208    else
209    {
210        const QString elidedText = fontMetrics().elidedText( m_text, m_mode, r.width() );
211        p.drawText( r, m_align, elidedText );
212    }
213}
214
215
216void
217ElidedLabel::changeEvent( QEvent* event )
218{
219    QFrame::changeEvent( event );
220    switch ( event->type() )
221    {
222        case QEvent::FontChange:
223        case QEvent::ApplicationFontChange:
224            updateLabel();
225            break;
226
227        default:
228            // nothing to do
229            break;
230    }
231}
232
233
234void
235ElidedLabel::mousePressEvent( QMouseEvent* event )
236{
237    QFrame::mousePressEvent( event );
238    m_time.start();
239}
240
241
242void
243ElidedLabel::mouseReleaseEvent( QMouseEvent* event )
244{
245    QFrame::mouseReleaseEvent( event );
246    if ( m_time.elapsed() < qApp->doubleClickInterval() )
247        emit clicked();
248}