Warning, file /multimedia/amarok/src/widgets/ElidingButton.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /**************************************************************************************** 0002 * Copyright (c) 2009 Nikolaj Hald Nielsen <nhn@kde.org> * 0003 * Copyright (c) 2009 Seb Ruiz <ruiz@kde.org> * 0004 * * 0005 * This program is free software; you can redistribute it and/or modify it under * 0006 * the terms of the GNU General Public License as published by the Free Software * 0007 * Foundation; either version 2 of the License, or (at your option) any later * 0008 * version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, but WITHOUT ANY * 0011 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * 0012 * PARTICULAR PURPOSE. See the GNU General Public License for more details. * 0013 * * 0014 * You should have received a copy of the GNU General Public License along with * 0015 * this program. If not, see <http://www.gnu.org/licenses/>. * 0016 ****************************************************************************************/ 0017 0018 #include "ElidingButton.h" 0019 0020 #include "core/support/Debug.h" 0021 0022 #include <QFontMetrics> 0023 #include <QMargins> 0024 0025 namespace Amarok { 0026 0027 ElidingButton::ElidingButton( QWidget *parent ) 0028 : QPushButton( parent ) 0029 { 0030 init(); 0031 } 0032 0033 ElidingButton::ElidingButton( const QString & text, QWidget * parent ) 0034 : QPushButton( text, parent ) 0035 , m_fullText( text ) 0036 { 0037 init(); 0038 } 0039 0040 ElidingButton::ElidingButton( const QIcon & icon, const QString & text, QWidget * parent ) 0041 : QPushButton( icon, text, parent ) 0042 , m_fullText( text ) 0043 { 0044 init(); 0045 } 0046 0047 ElidingButton::~ElidingButton() 0048 { 0049 } 0050 0051 void ElidingButton::init() 0052 { 0053 m_isElided = false; 0054 int width = iconSize().width() + 4; 0055 if( !text().isEmpty() ) 0056 { 0057 QFontMetrics fm( font() ); 0058 width += fm.horizontalAdvance( QLatin1String( "XX" ) ) / 2; 0059 } 0060 setMinimumWidth( width ); 0061 } 0062 0063 QSizePolicy ElidingButton::sizePolicy() const 0064 { 0065 //This has got to be the mother of all hacks... 0066 //If the text is currently elided, the button should try to get more space. If not elided 0067 //then the button has all the space it needs and should really not try to expand beyond this. 0068 //Since the size hint depends on the actual text shown (which is very short when elided) we 0069 //cannot depend on this for making the button grow again... 0070 if( !m_isElided ) 0071 return QSizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed ); 0072 0073 return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ); 0074 } 0075 0076 bool ElidingButton::isElided() const 0077 { 0078 return m_isElided; 0079 } 0080 0081 void ElidingButton::resizeEvent( QResizeEvent *event ) 0082 { 0083 elideText( event->size() ); 0084 QPushButton::resizeEvent( event ); 0085 } 0086 0087 void ElidingButton::setText( const QString &text ) 0088 { 0089 m_fullText = text; 0090 elideText( size() ); 0091 // elideText will call QPushButton::setText() 0092 } 0093 0094 void ElidingButton::elideText( const QSize &widgetSize ) 0095 { 0096 const int width = widgetSize.width(); 0097 const int iconWidth = icon().isNull() ? 0 : iconSize().width(); 0098 0099 QMargins margins = contentsMargins(); 0100 int padding = margins.left() + margins.right() + 4; 0101 int textWidth = width - ( iconWidth + padding ); 0102 0103 QFontMetrics fm( font() ); 0104 QString elidedText = fm.elidedText( m_fullText, Qt::ElideRight, textWidth ); 0105 QPushButton::setText( elidedText ); 0106 0107 bool elided = ( elidedText != m_fullText ); 0108 0109 // If there is no tooltip set, then we set it to be the full text when elided, 0110 // and clear it if the button is no longer elided. 0111 const QString tip = toolTip(); 0112 if( elided && tip.isEmpty() ) 0113 setToolTip( m_fullText ); 0114 else if( !elided && tip == m_fullText ) 0115 setToolTip( QString() ); 0116 0117 if( elided ) 0118 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ); 0119 else 0120 setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Fixed ); 0121 0122 if( m_isElided != elided ) 0123 { 0124 m_isElided = elided; 0125 Q_EMIT( sizePolicyChanged() ); 0126 } 0127 } 0128 0129 } 0130