• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • sources
  • kde-4.14
  • kdelibs
  • kdeui
  • widgets
ksqueezedtextlabel.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2000 Ronny Standtke <Ronny.Standtke@gmx.de>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 #include "ksqueezedtextlabel.h"
20 #include <kdebug.h>
21 #include <klocale.h>
22 #include <QContextMenuEvent>
23 #include <kaction.h>
24 #include <QMenu>
25 #include <QClipboard>
26 #include <QApplication>
27 #include <QMimeData>
28 #include <QTextDocument>
29 #include <kglobalsettings.h>
30 
31 class KSqueezedTextLabelPrivate
32 {
33 public:
34 
35  void _k_copyFullText() {
36  QApplication::clipboard()->setText(fullText);
37  }
38 
39  QString fullText;
40  Qt::TextElideMode elideMode;
41 };
42 
43 KSqueezedTextLabel::KSqueezedTextLabel(const QString &text , QWidget *parent)
44  : QLabel (parent),
45  d(new KSqueezedTextLabelPrivate)
46 {
47  setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
48  d->fullText = text;
49  d->elideMode = Qt::ElideMiddle;
50  squeezeTextToLabel();
51 }
52 
53 KSqueezedTextLabel::KSqueezedTextLabel(QWidget *parent)
54  : QLabel (parent),
55  d(new KSqueezedTextLabelPrivate)
56 {
57  setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
58  d->elideMode = Qt::ElideMiddle;
59 }
60 
61 KSqueezedTextLabel::~KSqueezedTextLabel()
62 {
63  delete d;
64 }
65 
66 void KSqueezedTextLabel::resizeEvent(QResizeEvent *)
67 {
68  squeezeTextToLabel();
69 }
70 
71 QSize KSqueezedTextLabel::minimumSizeHint() const
72 {
73  QSize sh = QLabel::minimumSizeHint();
74  sh.setWidth(-1);
75  return sh;
76 }
77 
78 QSize KSqueezedTextLabel::sizeHint() const
79 {
80  int maxWidth = KGlobalSettings::desktopGeometry(this).width() * 3 / 4;
81  QFontMetrics fm(fontMetrics());
82  int textWidth = fm.width(d->fullText);
83  if (textWidth > maxWidth) {
84  textWidth = maxWidth;
85  }
86  return QSize(textWidth, QLabel::sizeHint().height());
87 }
88 
89 void KSqueezedTextLabel::setText(const QString &text)
90 {
91  d->fullText = text;
92  squeezeTextToLabel();
93 }
94 
95 void KSqueezedTextLabel::clear()
96 {
97  d->fullText.clear();
98  QLabel::clear();
99 }
100 
101 void KSqueezedTextLabel::squeezeTextToLabel()
102 {
103  QFontMetrics fm(fontMetrics());
104  int labelWidth = size().width();
105  QStringList squeezedLines;
106  bool squeezed = false;
107  Q_FOREACH(const QString& line, d->fullText.split('\n')) {
108  int lineWidth = fm.width(line);
109  if (lineWidth > labelWidth) {
110  squeezed = true;
111  squeezedLines << fm.elidedText(line, d->elideMode, labelWidth);
112  } else {
113  squeezedLines << line;
114  }
115  }
116 
117  if (squeezed) {
118  QLabel::setText(squeezedLines.join("\n"));
119  setToolTip(d->fullText);
120  } else {
121  QLabel::setText(d->fullText);
122  setToolTip(QString());
123  }
124 }
125 
126 void KSqueezedTextLabel::setAlignment(Qt::Alignment alignment)
127 {
128  // save fullText and restore it
129  QString tmpFull(d->fullText);
130  QLabel::setAlignment(alignment);
131  d->fullText = tmpFull;
132 }
133 
134 Qt::TextElideMode KSqueezedTextLabel::textElideMode() const
135 {
136  return d->elideMode;
137 }
138 
139 void KSqueezedTextLabel::setTextElideMode(Qt::TextElideMode mode)
140 {
141  d->elideMode = mode;
142  squeezeTextToLabel();
143 }
144 
145 QString KSqueezedTextLabel::fullText() const
146 {
147  return d->fullText;
148 }
149 
150 void KSqueezedTextLabel::contextMenuEvent(QContextMenuEvent* ev)
151 {
152  // We want to reimplement "Copy" to include the elided text.
153  // But this means reimplementing the full popup menu, so no more
154  // copy-link-address or copy-selection support anymore, since we
155  // have no access to the QTextDocument.
156  // Maybe we should have a boolean flag in KSqueezedTextLabel itself for
157  // whether to show the "Copy Full Text" custom popup?
158  // For now I chose to show it when the text is squeezed; when it's not, the
159  // standard popup menu can do the job (select all, copy).
160 
161  const bool squeezed = text() != d->fullText;
162  const bool showCustomPopup = squeezed;
163  if (showCustomPopup) {
164  QMenu menu(this);
165 
166  KAction* act = new KAction(i18n("&Copy Full Text"), &menu);
167  connect(act, SIGNAL(triggered()), this, SLOT(_k_copyFullText()));
168  menu.addAction(act);
169 
170  ev->accept();
171  menu.exec(ev->globalPos());
172  } else {
173  QLabel::contextMenuEvent(ev);
174  }
175 }
176 
177 void KSqueezedTextLabel::mouseReleaseEvent(QMouseEvent* ev)
178 {
179 #if QT_VERSION >= 0x040700
180  if (QApplication::clipboard()->supportsSelection() &&
181  textInteractionFlags() != Qt::NoTextInteraction &&
182  ev->button() == Qt::LeftButton &&
183  !d->fullText.isEmpty() &&
184  hasSelectedText()) {
185  // Expand "..." when selecting with the mouse
186  QString txt = selectedText();
187  const QChar ellipsisChar(0x2026); // from qtextengine.cpp
188  const int dotsPos = txt.indexOf(ellipsisChar);
189  if (dotsPos > -1) {
190  // Ex: abcde...yz, selecting de...y (selectionStart=3)
191  // charsBeforeSelection = selectionStart = 2 (ab)
192  // charsAfterSelection = 1 (z)
193  // final selection length= 26 - 2 - 1 = 23
194  const int start = selectionStart();
195  int charsAfterSelection = text().length() - start - selectedText().length();
196  txt = d->fullText;
197  // Strip markup tags
198  if (textFormat() == Qt::RichText
199  || (textFormat() == Qt::AutoText && Qt::mightBeRichText(txt))) {
200  txt.replace(QRegExp("<[^>]*>"), "");
201  // account for stripped characters
202  charsAfterSelection -= d->fullText.length() - txt.length();
203  }
204  txt = txt.mid(selectionStart(), txt.length() - start - charsAfterSelection);
205  }
206  QApplication::clipboard()->setText(txt, QClipboard::Selection);
207  } else
208 #endif
209  {
210  QLabel::mouseReleaseEvent(ev);
211  }
212 }
213 
214 #include "ksqueezedtextlabel.moc"
i18n
QString i18n(const char *text)
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
QResizeEvent
QWidget
KSqueezedTextLabel::mouseReleaseEvent
void mouseReleaseEvent(QMouseEvent *)
Definition: ksqueezedtextlabel.cpp:177
QSize::width
int width() const
KSqueezedTextLabel::contextMenuEvent
void contextMenuEvent(QContextMenuEvent *)
Definition: ksqueezedtextlabel.cpp:150
kdebug.h
KSqueezedTextLabel::setAlignment
virtual void setAlignment(Qt::Alignment)
Overridden for internal reasons; the API remains unaffected.
Definition: ksqueezedtextlabel.cpp:126
kglobalsettings.h
QLabel::selectedText
QString selectedText() const
QChar
KSqueezedTextLabel::setText
void setText(const QString &text)
Sets the text.
Definition: ksqueezedtextlabel.cpp:89
QSizePolicy
QMenu::addAction
void addAction(QAction *action)
KSqueezedTextLabel::minimumSizeHint
virtual QSize minimumSizeHint() const
Definition: ksqueezedtextlabel.cpp:71
QLabel::setAlignment
void setAlignment(QFlags< Qt::AlignmentFlag >)
KGlobalSettings::desktopGeometry
static QRect desktopGeometry(const QPoint &point)
This function returns the desktop geometry for an application that needs to set the geometry of a wid...
Definition: kglobalsettings.cpp:732
QFontMetrics
QMouseEvent
QLabel::clear
void clear()
QStringList::join
QString join(const QString &separator) const
QFrame::lineWidth
int lineWidth() const
Qt::Alignment
typedef Alignment
klocale.h
QContextMenuEvent::globalPos
const QPoint & globalPos() const
KSqueezedTextLabel::squeezeTextToLabel
void squeezeTextToLabel()
does the dirty work
Definition: ksqueezedtextlabel.cpp:101
QLabel::selectionStart
int selectionStart() const
QWidget::size
QSize size() const
QRegExp
QApplication::clipboard
QClipboard * clipboard()
QContextMenuEvent
QMouseEvent::button
Qt::MouseButton button() const
KSqueezedTextLabel::~KSqueezedTextLabel
virtual ~KSqueezedTextLabel()
Definition: ksqueezedtextlabel.cpp:61
QSize::setWidth
void setWidth(int width)
QLabel::hasSelectedText
bool hasSelectedText() const
QFontMetrics::elidedText
QString elidedText(const QString &text, Qt::TextElideMode mode, int width, int flags) const
QLabel::textFormat
Qt::TextFormat textFormat() const
KSqueezedTextLabel::KSqueezedTextLabel
KSqueezedTextLabel(QWidget *parent=0)
Default constructor.
Definition: ksqueezedtextlabel.cpp:53
QLabel::text
QString text() const
QString
QMenu::exec
QAction * exec()
QWidget::setSizePolicy
void setSizePolicy(QSizePolicy)
QStringList
KSqueezedTextLabel::sizeHint
virtual QSize sizeHint() const
Definition: ksqueezedtextlabel.cpp:78
kaction.h
QMenu
QEvent::accept
void accept()
QSize
QFontMetrics::width
int width(const QString &text, int len) const
ksqueezedtextlabel.h
KSqueezedTextLabel::setTextElideMode
void setTextElideMode(Qt::TextElideMode mode)
Sets the text elide mode.
Definition: ksqueezedtextlabel.cpp:139
QLabel::textInteractionFlags
Qt::TextInteractionFlags textInteractionFlags() const
QString::replace
QString & replace(int position, int n, QChar after)
QLabel::contextMenuEvent
virtual void contextMenuEvent(QContextMenuEvent *ev)
KSqueezedTextLabel::resizeEvent
void resizeEvent(QResizeEvent *)
Called when widget is resized.
Definition: ksqueezedtextlabel.cpp:66
QRect::width
int width() const
QString::mid
QString mid(int position, int n) const
QWidget::fontMetrics
QFontMetrics fontMetrics() const
KSqueezedTextLabel::fullText
QString fullText() const
Get the full text set via setText.
Definition: ksqueezedtextlabel.cpp:145
KAction
Class to encapsulate user-driven action or event.
Definition: kaction.h:216
QLabel::mouseReleaseEvent
virtual void mouseReleaseEvent(QMouseEvent *ev)
QLabel::sizeHint
virtual QSize sizeHint() const
QString::length
int length() const
Qt::mightBeRichText
bool mightBeRichText(const QString &text)
QLabel::minimumSizeHint
virtual QSize minimumSizeHint() const
KSqueezedTextLabel::clear
void clear()
Clears the text.
Definition: ksqueezedtextlabel.cpp:95
QClipboard::setText
void setText(const QString &text, Mode mode)
QWidget::setToolTip
void setToolTip(const QString &)
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QLabel
KSqueezedTextLabel::textElideMode
Qt::TextElideMode textElideMode() const
Returns the text elide mode.
QWidget::height
int height() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:24:00 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal