1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-04-14 13:01:53 +02:00

add some eliding so playlists with long name don't increase the window size

minor cleanup in ElidedLabel
This commit is contained in:
Leo Franchi 2011-03-25 15:31:46 -04:00
parent 4f77de3bbc
commit 8f46db649f
6 changed files with 52 additions and 22 deletions

View File

@ -23,6 +23,7 @@
#include "dynamic/GeneratorInterface.h"
#include "dynamic/DynamicControl.h"
#include "utils/tomahawkutils.h"
#include "utils/elidedlabel.h"
#include <QLabel>
#include <QStackedLayout>
@ -85,7 +86,7 @@ CollapsibleControls::init()
m_summaryLayout->setMargin( 0 );
m_summaryWidget->setContentsMargins( 3, 0, 0, 0 );
m_summary = new QLabel( m_summaryWidget );
m_summary = new ElidedLabel( m_summaryWidget );
QFont f = m_summary->font();
f.setPointSize( f.pointSize() + 1 );
f.setBold( true );

View File

@ -27,7 +27,7 @@ class QPaintEvent;
class QHBoxLayout;
class QTimeLine;
class QToolButton;
class QLabel;
class ElidedLabel;
class QStackedLayout;
namespace Tomahawk
{
@ -67,7 +67,7 @@ private:
QWidget* m_summaryWidget;
QHBoxLayout* m_summaryLayout;
QLabel* m_summary;
ElidedLabel* m_summary;
QStackedLayout* m_expandL;
QToolButton* m_summaryExpand;

View File

@ -43,7 +43,7 @@ InfoBar::InfoBar( QWidget* parent )
boldFont.setPixelSize( 12 );
ui->descriptionLabel->setFont( boldFont );
ui->descriptionLabel->setMargin( 2 );
ui->descriptionLabel->setMargin( 10 );
QPalette whitePal = ui->captionLabel->palette();
whitePal.setColor( QPalette::Foreground, Qt::white );
@ -52,6 +52,8 @@ InfoBar::InfoBar( QWidget* parent )
ui->descriptionLabel->setPalette( whitePal );
ui->captionLabel->setText( QString() );
ui->captionLabel->setMargin( 6 );
ui->descriptionLabel->setText( QString() );
ui->imageLabel->setText( QString() );

View File

@ -74,7 +74,7 @@
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="captionLabel">
<widget class="ElidedLabel" name="captionLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
@ -87,7 +87,7 @@
</widget>
</item>
<item>
<widget class="QLabel" name="descriptionLabel">
<widget class="ElidedLabel" name="descriptionLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
@ -119,6 +119,13 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>ElidedLabel</class>
<extends>QLabel</extends>
<header>utils/elidedlabel.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View File

@ -22,6 +22,7 @@
#include <QPainter>
#include <QFontMetrics>
#include <QApplication>
#include <QRect>
ElidedLabel::ElidedLabel( QWidget* parent, Qt::WindowFlags flags )
@ -65,16 +66,16 @@ ElidedLabel::setText( const QString& text )
Qt::Alignment
ElidedLabel::alignment() const
{
return align;
return m_align;
}
void
ElidedLabel::setAlignment( Qt::Alignment alignment )
{
if ( this->align != alignment )
if ( m_align != alignment )
{
this->align = alignment;
m_align = alignment;
update(); // no geometry change, repaint is sufficient
}
}
@ -83,27 +84,40 @@ ElidedLabel::setAlignment( Qt::Alignment alignment )
Qt::TextElideMode
ElidedLabel::elideMode() const
{
return mode;
return m_mode;
}
void
ElidedLabel::setElideMode( Qt::TextElideMode mode )
{
if ( this->mode != mode )
if ( m_mode != mode )
{
this->mode = mode;
m_mode = mode;
updateLabel();
}
}
void
ElidedLabel::setMargin( int margin )
{
m_margin = margin;
}
int
ElidedLabel::margin() const
{
return m_margin;
}
void
ElidedLabel::init( const QString& txt )
{
m_text = txt;
align = Qt::AlignLeft;
mode = Qt::ElideMiddle;
m_align = Qt::AlignLeft;
m_mode = Qt::ElideMiddle;
m_margin = 0;
setContentsMargins( 0, 0, 0, 0 );
}
@ -128,7 +142,7 @@ ElidedLabel::sizeHint() const
QSize
ElidedLabel::minimumSizeHint() const
{
switch ( mode )
switch ( m_mode )
{
case Qt::ElideNone:
return sizeHint();
@ -149,8 +163,10 @@ ElidedLabel::paintEvent( QPaintEvent* event )
QFrame::paintEvent( event );
QPainter p( this );
QRect r = contentsRect();
const QString elidedText = fontMetrics().elidedText( m_text, mode, r.width() );
p.drawText( r, align, elidedText );
r.adjust( m_margin, m_margin, -m_margin, -m_margin );
const QString elidedText = fontMetrics().elidedText( m_text, m_mode, r.width() );
p.drawText( r, m_align, elidedText );
}
@ -176,7 +192,7 @@ void
ElidedLabel::mousePressEvent( QMouseEvent* event )
{
QFrame::mousePressEvent( event );
time.start();
m_time.start();
}
@ -184,6 +200,6 @@ void
ElidedLabel::mouseReleaseEvent( QMouseEvent* event )
{
QFrame::mouseReleaseEvent( event );
if ( time.elapsed() < qApp->doubleClickInterval() )
if ( m_time.elapsed() < qApp->doubleClickInterval() )
emit clicked();
}

View File

@ -44,6 +44,9 @@ public:
Qt::TextElideMode elideMode() const;
void setElideMode( Qt::TextElideMode mode );
void setMargin( int margin );
int margin() const;
virtual QSize sizeHint() const;
virtual QSize minimumSizeHint() const;
@ -64,10 +67,11 @@ protected:
virtual void paintEvent( QPaintEvent* event );
private:
QTime time;
QTime m_time;
QString m_text;
Qt::Alignment align;
Qt::TextElideMode mode;
Qt::Alignment m_align;
Qt::TextElideMode m_mode;
int m_margin;
};
#endif // ELIDEDLABEL_H