/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. #include "ElidedLabel.h"
  19. #include <QEvent>
  20. #include <QPainter>
  21. #include <QFontMetrics>
  22. #include <QApplication>
  23. #include <QRect>
  24. #include <QTextLayout>
  25. #include "utils/Logger.h"
  26. ElidedLabel::ElidedLabel( QWidget* parent, Qt::WindowFlags flags )
  27. : QFrame( parent, flags )
  28. {
  29. init();
  30. }
  31. ElidedLabel::ElidedLabel( const QString& text, QWidget* parent, Qt::WindowFlags flags )
  32. : QFrame( parent, flags )
  33. {
  34. init( text );
  35. }
  36. ElidedLabel::~ElidedLabel()
  37. {
  38. }
  39. QString
  40. ElidedLabel::text() const
  41. {
  42. return m_text;
  43. }
  44. void
  45. ElidedLabel::setText( const QString& text )
  46. {
  47. if ( m_text != text )
  48. {
  49. m_text = text;
  50. updateLabel();
  51. emit textChanged( text );
  52. }
  53. }
  54. Qt::Alignment
  55. ElidedLabel::alignment() const
  56. {
  57. return m_align;
  58. }
  59. void
  60. ElidedLabel::setAlignment( Qt::Alignment alignment )
  61. {
  62. if ( m_align != alignment )
  63. {
  64. m_align = alignment;
  65. update(); // no geometry change, repaint is sufficient
  66. }
  67. }
  68. Qt::TextElideMode
  69. ElidedLabel::elideMode() const
  70. {
  71. return m_mode;
  72. }
  73. void
  74. ElidedLabel::setElideMode( Qt::TextElideMode mode )
  75. {
  76. if ( m_mode != mode )
  77. {
  78. m_mode = mode;
  79. updateLabel();
  80. }
  81. }
  82. void
  83. ElidedLabel::setMargin( int margin )
  84. {
  85. m_margin = margin;
  86. }
  87. int
  88. ElidedLabel::margin() const
  89. {
  90. return m_margin;
  91. }
  92. void
  93. ElidedLabel::setFont( const QFont& font )
  94. {
  95. QWidget::setFont( font );
  96. updateLabel();
  97. }
  98. void
  99. ElidedLabel::init( const QString& txt )
  100. {
  101. m_text = txt;
  102. m_align = Qt::AlignLeft;
  103. m_mode = Qt::ElideRight;
  104. m_margin = 0;
  105. m_multiLine = false;
  106. setContentsMargins( 0, 0, 0, 0 );
  107. }
  108. void
  109. ElidedLabel::updateLabel()
  110. {
  111. updateGeometry();
  112. update();
  113. }
  114. QSize
  115. ElidedLabel::sizeHint() const
  116. {
  117. const QFontMetrics& fm = fontMetrics();
  118. return QSize( fm.width( m_text ) + m_margin * 2, fm.height() + m_margin * 2 );
  119. }
  120. QSize
  121. ElidedLabel::minimumSizeHint() const
  122. {
  123. switch ( m_mode )
  124. {
  125. case Qt::ElideNone:
  126. return sizeHint();
  127. default:
  128. {
  129. const QFontMetrics& fm = fontMetrics();
  130. QSize size( fm.width( "..." ), fm.height() );
  131. return size;
  132. }
  133. }
  134. }
  135. void
  136. ElidedLabel::paintEvent( QPaintEvent* event )
  137. {
  138. QFrame::paintEvent( event );
  139. QPainter p( this );
  140. p.setRenderHint( QPainter::TextAntialiasing );
  141. QRect r = contentsRect();
  142. r.adjust( m_margin, m_margin, -m_margin, -m_margin );
  143. if ( m_multiLine )
  144. {
  145. QTextLayout textLayout( m_text );
  146. textLayout.setFont( p.font() );
  147. int widthUsed = 0;
  148. int lineCount = 0;
  149. int lineLimit = r.height() / fontMetrics().height();
  150. textLayout.beginLayout();
  151. while ( ++lineCount < lineLimit )
  152. {
  153. QTextLine line = textLayout.createLine();
  154. if ( !line.isValid() )
  155. break;
  156. line.setLineWidth( r.width() );
  157. widthUsed += line.naturalTextWidth();
  158. }
  159. textLayout.endLayout();
  160. widthUsed += r.width();
  161. const QString elidedText = fontMetrics().elidedText( m_text, Qt::ElideRight, widthUsed );
  162. p.drawText( r, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, elidedText );
  163. }
  164. else
  165. {
  166. const QString elidedText = fontMetrics().elidedText( m_text, m_mode, r.width() );
  167. p.drawText( r, m_align, elidedText );
  168. }
  169. }
  170. void
  171. ElidedLabel::changeEvent( QEvent* event )
  172. {
  173. QFrame::changeEvent( event );
  174. switch ( event->type() )
  175. {
  176. case QEvent::FontChange:
  177. case QEvent::ApplicationFontChange:
  178. updateLabel();
  179. break;
  180. default:
  181. // nothing to do
  182. break;
  183. }
  184. }
  185. void
  186. ElidedLabel::mousePressEvent( QMouseEvent* event )
  187. {
  188. QFrame::mousePressEvent( event );
  189. m_time.start();
  190. }
  191. void
  192. ElidedLabel::mouseReleaseEvent( QMouseEvent* event )
  193. {
  194. QFrame::mouseReleaseEvent( event );
  195. if ( m_time.elapsed() < qApp->doubleClickInterval() )
  196. emit clicked();
  197. }