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;
98  internal::setToolTip(this, 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
void setToolTip(QObjectPtr obj, const QString &tip)
Definition: ads_globals.h:252
Qt::TextElideMode elideMode() const
void setText(const QString &text)
Declaration of CElidingLabel.
MQTTClient d
Definition: test10.c:1656
virtual ~CElidingLabel()
Qt::TextElideMode ElideMode
void elideText(int Width)
ElidingLabelPrivate(CElidingLabel *_public)
bool isElided() const
void setElideMode(Qt::TextElideMode mode)
virtual void mouseDoubleClickEvent(QMouseEvent *ev) override
virtual QSize minimumSizeHint() const override
QString text() const
bool isModeElideNone() const
void elidedChanged(bool elided)
CElidingLabel(QWidget *parent=0, Qt::WindowFlags f=0)
virtual QSize sizeHint() const override
ElidingLabelPrivate * d
Definition: ElidingLabel.h:50
virtual void mouseReleaseEvent(QMouseEvent *event) override
virtual void resizeEvent(QResizeEvent *event) override


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:47:34