ElidingLabel.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 ** Qt Advanced Docking System
3 ** Copyright (C) 2017 Uwe Kindler
4 **
5 ** This library is free software; you can redistribute it and/or
6 ** modify it under the terms of the GNU Lesser General Public
7 ** License as published by the Free Software Foundation; either
8 ** version 2.1 of the License, or (at your option) any later version.
9 **
10 ** This library 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 GNU
13 ** Lesser General Public License for more details.
14 **
15 ** You should have received a copy of the GNU Lesser General Public
16 ** License along with this library; If not, see <http://www.gnu.org/licenses/>.
17 ******************************************************************************/
18 
19 
20 //============================================================================
25 //============================================================================
26 
27 //============================================================================
28 // INCLUDES
29 //============================================================================
30 #include "ElidingLabel.h"
31 #include <QMouseEvent>
32 
33 
34 namespace ads
35 {
40 {
42  Qt::TextElideMode ElideMode = Qt::ElideNone;
43  QString Text;
44  bool IsElided = false;
45 
46  ElidingLabelPrivate(CElidingLabel* _public) : _this(_public) {}
47 
48  void elideText(int Width);
49 
53  bool isModeElideNone() const
54  {
55  return Qt::ElideNone == ElideMode;
56  }
57 };
58 
59 
60 //============================================================================
62 {
63  if (isModeElideNone())
64  {
65  return;
66  }
67  QFontMetrics fm = _this->fontMetrics();
68  QString str = fm.elidedText(Text, ElideMode, Width - _this->margin() * 2 - _this->indent());
69  if (str == "…")
70  {
71  str = Text.at(0);
72  }
73  bool WasElided = IsElided;
74  IsElided = str != Text;
75  if(IsElided != WasElided)
76  {
78  }
79  _this->QLabel::setText(str);
80 }
81 
82 
83 //============================================================================
84 CElidingLabel::CElidingLabel(QWidget* parent, Qt::WindowFlags f)
85  : QLabel(parent, f),
86  d(new ElidingLabelPrivate(this))
87 {
88 
89 }
90 
91 
92 //============================================================================
93 CElidingLabel::CElidingLabel(const QString& text, QWidget* parent, Qt::WindowFlags f)
94  : QLabel(text, parent,f),
95  d(new ElidingLabelPrivate(this))
96 {
97  d->Text = text;
99 }
100 
101 
102 //============================================================================
104 {
105  delete d;
106 }
107 
108 
109 //============================================================================
110 Qt::TextElideMode CElidingLabel::elideMode() const
111 {
112  return d->ElideMode;
113 }
114 
115 
116 //============================================================================
117 void CElidingLabel::setElideMode(Qt::TextElideMode mode)
118 {
119  d->ElideMode = mode;
120  d->elideText(size().width());
121 }
122 
123 //============================================================================
125 {
126  return d->IsElided;
127 }
128 
129 
130 //============================================================================
131 void CElidingLabel::mouseReleaseEvent(QMouseEvent* event)
132 {
133  Super::mouseReleaseEvent(event);
134  if (event->button() != Qt::LeftButton)
135  {
136  return;
137  }
138 
139  emit clicked();
140 }
141 
142 
143 //============================================================================
145 {
146  Q_UNUSED(ev)
147  emit doubleClicked();
148  Super::mouseDoubleClickEvent(ev);
149 }
150 
151 
152 //============================================================================
153 void CElidingLabel::resizeEvent(QResizeEvent *event)
154 {
155  if (!d->isModeElideNone())
156  {
157  d->elideText(event->size().width());
158  }
159  Super::resizeEvent(event);
160 }
161 
162 
163 //============================================================================
165 {
166  if (pixmap() != nullptr || d->isModeElideNone())
167  {
168  return QLabel::minimumSizeHint();
169  }
170  const QFontMetrics &fm = fontMetrics();
171  #if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
172  QSize size(fm.horizontalAdvance(d->Text.left(2) + "…"), fm.height());
173  #else
174  QSize size(fm.width(d->Text.left(2) + "…"), fm.height());
175  #endif
176  return size;
177 }
178 
179 
180 //============================================================================
182 {
183  if (pixmap() != nullptr || d->isModeElideNone())
184  {
185  return QLabel::sizeHint();
186  }
187  const QFontMetrics& fm = fontMetrics();
188  #if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
189  QSize size(fm.horizontalAdvance(d->Text), QLabel::sizeHint().height());
190  #else
191  QSize size(fm.width(d->Text), QLabel::sizeHint().height());
192  #endif
193  return size;
194 }
195 
196 
197 //============================================================================
198 void CElidingLabel::setText(const QString &text)
199 {
200  d->Text = text;
201  if (d->isModeElideNone())
202  {
203  Super::setText(text);
204  }
205  else
206  {
207  internal::setToolTip(this, text);
208  d->elideText(this->size().width());
209  }
210 }
211 
212 
213 //============================================================================
214 QString CElidingLabel::text() const
215 {
216  return d->Text;
217 }
218 } // namespace QtLabb
219 
220 //---------------------------------------------------------------------------
221 // EOF ClickableLabel.cpp
ads::CElidingLabel::mouseDoubleClickEvent
virtual void mouseDoubleClickEvent(QMouseEvent *ev) override
Definition: ElidingLabel.cpp:144
ads::CElidingLabel::CElidingLabel
CElidingLabel(QWidget *parent=0, Qt::WindowFlags f=0)
Definition: ElidingLabel.cpp:84
ads::CElidingLabel::elidedChanged
void elidedChanged(bool elided)
ads::CElidingLabel::setElideMode
void setElideMode(Qt::TextElideMode mode)
Definition: ElidingLabel.cpp:117
ads::CElidingLabel::text
QString text() const
Definition: ElidingLabel.cpp:214
ads::CElidingLabel::d
ElidingLabelPrivate * d
Definition: ElidingLabel.h:50
ads::ElidingLabelPrivate::IsElided
bool IsElided
Definition: ElidingLabel.cpp:44
ads::CElidingLabel::mouseReleaseEvent
virtual void mouseReleaseEvent(QMouseEvent *event) override
Definition: ElidingLabel.cpp:131
ads::CElidingLabel::clicked
void clicked()
ads::CElidingLabel::elideMode
Qt::TextElideMode elideMode() const
Definition: ElidingLabel.cpp:110
ads::ElidingLabelPrivate::isModeElideNone
bool isModeElideNone() const
Definition: ElidingLabel.cpp:53
ads::ElidingLabelPrivate
Definition: ElidingLabel.cpp:39
f
f
nonstd::span_lite::size
span_constexpr std::size_t size(span< T, Extent > const &spn)
Definition: span.hpp:1554
ads::ElidingLabelPrivate::_this
CElidingLabel * _this
Definition: ElidingLabel.cpp:41
ads
Definition: ads_globals.h:69
ads::CElidingLabel::isElided
bool isElided() const
Definition: ElidingLabel.cpp:124
ads::ElidingLabelPrivate::elideText
void elideText(int Width)
Definition: ElidingLabel.cpp:61
ads::CElidingLabel::sizeHint
virtual QSize sizeHint() const override
Definition: ElidingLabel.cpp:181
ads::CElidingLabel::setText
void setText(const QString &text)
Definition: ElidingLabel.cpp:198
d
d
ads::CElidingLabel::resizeEvent
virtual void resizeEvent(QResizeEvent *event) override
Definition: ElidingLabel.cpp:153
ads::internal::setToolTip
void setToolTip(QObjectPtr obj, const QString &tip)
Definition: ads_globals.h:252
ads::ElidingLabelPrivate::ElideMode
Qt::TextElideMode ElideMode
Definition: ElidingLabel.cpp:42
ads::ElidingLabelPrivate::ElidingLabelPrivate
ElidingLabelPrivate(CElidingLabel *_public)
Definition: ElidingLabel.cpp:46
ads::CElidingLabel::doubleClicked
void doubleClicked()
ads::CElidingLabel::minimumSizeHint
virtual QSize minimumSizeHint() const override
Definition: ElidingLabel.cpp:164
ads::CElidingLabel::~CElidingLabel
virtual ~CElidingLabel()
Definition: ElidingLabel.cpp:103
ads::CElidingLabel
Definition: ElidingLabel.h:46
ElidingLabel.h
Declaration of CElidingLabel.
ads::ElidingLabelPrivate::Text
QString Text
Definition: ElidingLabel.cpp:43


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:44