1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-07-31 03:10:12 +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 c90a875de7
6 changed files with 52 additions and 22 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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