elidinglabel.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2020 Uwe Kindler
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of Qt Creator.
  7. **
  8. ** Commercial License Usage
  9. ** Licensees holding valid commercial Qt licenses may use this file in
  10. ** accordance with the commercial license agreement provided with the
  11. ** Software or, alternatively, in accordance with the terms contained in
  12. ** a written agreement between you and The Qt Company. For licensing terms
  13. ** and conditions see https://www.qt.io/terms-conditions. For further
  14. ** information use the contact form at https://www.qt.io/contact-us.
  15. **
  16. ** GNU Lesser General Public License Usage
  17. ** Alternatively, this file may be used under the terms of the GNU Lesser
  18. ** General Public License version 2.1 or (at your option) any later version.
  19. ** The licenses are as published by the Free Software Foundation
  20. ** and appearing in the file LICENSE.LGPLv21 included in the packaging
  21. ** of this file. Please review the following information to ensure
  22. ** the GNU Lesser General Public License version 2.1 requirements
  23. ** will be met: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 3 or (at your option) any later version
  28. ** approved by the KDE Free Qt Foundation. The licenses are as published by
  29. ** the Free Software Foundation and appearing in the file LICENSE.GPL3
  30. ** included in the packaging of this file. Please review the following
  31. ** information to ensure the GNU General Public License requirements will
  32. ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
  33. **
  34. ****************************************************************************/
  35. #include "elidinglabel.h"
  36. #include <QMouseEvent>
  37. namespace ADS {
  38. /**
  39. * Private data of public ClickableLabel
  40. */
  41. struct ElidingLabelPrivate
  42. {
  43. ElidingLabel *q;
  44. Qt::TextElideMode m_elideMode = Qt::ElideNone;
  45. QString m_text;
  46. bool m_isElided = false;
  47. ElidingLabelPrivate(ElidingLabel *parent)
  48. : q(parent)
  49. {}
  50. void elideText(int width);
  51. /**
  52. * Convenience function to check if the
  53. */
  54. bool isModeElideNone() const { return Qt::ElideNone == m_elideMode; }
  55. };
  56. void ElidingLabelPrivate::elideText(int width)
  57. {
  58. if (isModeElideNone())
  59. return;
  60. QFontMetrics fm = q->fontMetrics();
  61. QString str = fm.elidedText(m_text, m_elideMode, width - q->margin() * 2 - q->indent());
  62. if (str == QChar(0x2026))
  63. str = m_text.at(0);
  64. bool wasElided = m_isElided;
  65. m_isElided = str != m_text;
  66. if (m_isElided != wasElided)
  67. emit q->elidedChanged(m_isElided);
  68. q->QLabel::setText(str);
  69. }
  70. ElidingLabel::ElidingLabel(QWidget *parent, Qt::WindowFlags flags)
  71. : QLabel(parent, flags)
  72. , d(new ElidingLabelPrivate(this))
  73. {}
  74. ElidingLabel::ElidingLabel(const QString &text, QWidget *parent, Qt::WindowFlags flags)
  75. : QLabel(text, parent, flags)
  76. , d(new ElidingLabelPrivate(this))
  77. {
  78. d->m_text = text;
  79. internal::setToolTip(this, text);
  80. }
  81. ElidingLabel::~ElidingLabel()
  82. {
  83. delete d;
  84. }
  85. Qt::TextElideMode ElidingLabel::elideMode() const
  86. {
  87. return d->m_elideMode;
  88. }
  89. void ElidingLabel::setElideMode(Qt::TextElideMode mode)
  90. {
  91. d->m_elideMode = mode;
  92. d->elideText(size().width());
  93. }
  94. bool ElidingLabel::isElided() const
  95. {
  96. return d->m_isElided;
  97. }
  98. void ElidingLabel::mouseReleaseEvent(QMouseEvent *event)
  99. {
  100. Super::mouseReleaseEvent(event);
  101. if (event->button() != Qt::LeftButton) {
  102. return;
  103. }
  104. emit clicked();
  105. }
  106. void ElidingLabel::mouseDoubleClickEvent(QMouseEvent *event)
  107. {
  108. Q_UNUSED(event)
  109. emit doubleClicked();
  110. Super::mouseDoubleClickEvent(event);
  111. }
  112. void ElidingLabel::resizeEvent(QResizeEvent *event)
  113. {
  114. if (!d->isModeElideNone()) {
  115. d->elideText(event->size().width());
  116. }
  117. Super::resizeEvent(event);
  118. }
  119. QSize ElidingLabel::minimumSizeHint() const
  120. {
  121. if (pixmap() != nullptr || d->isModeElideNone()) {
  122. return QLabel::minimumSizeHint();
  123. }
  124. const QFontMetrics &fm = fontMetrics();
  125. #if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
  126. QSize size(fm.horizontalAdvance(d->m_text.left(2) + "…"), fm.height());
  127. #else
  128. QSize size(fm.width(d->m_text.left(2) + "…"), fm.height());
  129. #endif
  130. return size;
  131. }
  132. QSize ElidingLabel::sizeHint() const
  133. {
  134. if (pixmap() != nullptr || d->isModeElideNone()) {
  135. return QLabel::sizeHint();
  136. }
  137. const QFontMetrics &fm = fontMetrics();
  138. #if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
  139. QSize size(fm.horizontalAdvance(d->m_text), QLabel::sizeHint().height());
  140. #else
  141. QSize size(fm.width(d->m_text), QLabel::sizeHint().height());
  142. #endif
  143. return size;
  144. }
  145. void ElidingLabel::setText(const QString &text)
  146. {
  147. if (d->isModeElideNone()) {
  148. Super::setText(text);
  149. } else {
  150. d->m_text = text;
  151. internal::setToolTip(this, text);
  152. d->elideText(this->size().width());
  153. }
  154. }
  155. QString ElidingLabel::text() const
  156. {
  157. return d->m_text;
  158. }
  159. } // namespace ADS