elidinglabel.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #pragma once
  36. #include "ads_globals.h"
  37. #include <QLabel>
  38. namespace ADS {
  39. struct ElidingLabelPrivate;
  40. /**
  41. * A QLabel that supports eliding text.
  42. * Because the functions setText() and text() are no virtual functions setting
  43. * and reading the text via a pointer to the base class QLabel does not work
  44. * properly
  45. */
  46. class ADS_EXPORT ElidingLabel : public QLabel
  47. {
  48. Q_OBJECT
  49. private:
  50. ElidingLabelPrivate *d;
  51. friend struct ElidingLabelPrivate;
  52. protected:
  53. virtual void mouseReleaseEvent(QMouseEvent *event) override;
  54. virtual void resizeEvent(QResizeEvent *event) override;
  55. virtual void mouseDoubleClickEvent(QMouseEvent *ev) override;
  56. public:
  57. using Super = QLabel;
  58. ElidingLabel(QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::Widget);
  59. ElidingLabel(const QString &text, QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::Widget);
  60. virtual ~ElidingLabel() override;
  61. /**
  62. * Returns the text elide mode.
  63. * The default mode is ElideNone
  64. */
  65. Qt::TextElideMode elideMode() const;
  66. /**
  67. * Sets the text elide mode
  68. */
  69. void setElideMode(Qt::TextElideMode mode);
  70. /**
  71. * This function indicates whether the text on this label is currently elided
  72. */
  73. bool isElided() const;
  74. public: // reimplements QLabel
  75. virtual QSize minimumSizeHint() const override;
  76. virtual QSize sizeHint() const override;
  77. void setText(const QString &text);
  78. QString text() const;
  79. signals:
  80. /**
  81. * This signal is emitted if the user clicks on the label (i.e. pressed
  82. * down then released while the mouse cursor is inside the label)
  83. */
  84. void clicked();
  85. /**
  86. * This signal is emitted if the user does a double click on the label
  87. */
  88. void doubleClicked();
  89. /**
  90. * This signal is emitted when isElided() state of this label is changed
  91. */
  92. void elidedChanged(bool elided);
  93. }; //class ElidingLabel
  94. } // namespace ADS