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

KDEUI

  • 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
31class KSqueezedTextLabelPrivate
32{
33public:
34
35 void _k_copyFullText() {
36 QApplication::clipboard()->setText(fullText);
37 }
38
39 QString fullText;
40 Qt::TextElideMode elideMode;
41};
42
43KSqueezedTextLabel::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
53KSqueezedTextLabel::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
61KSqueezedTextLabel::~KSqueezedTextLabel()
62{
63 delete d;
64}
65
66void KSqueezedTextLabel::resizeEvent(QResizeEvent *)
67{
68 squeezeTextToLabel();
69}
70
71QSize KSqueezedTextLabel::minimumSizeHint() const
72{
73 QSize sh = QLabel::minimumSizeHint();
74 sh.setWidth(-1);
75 return sh;
76}
77
78QSize 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
89void KSqueezedTextLabel::setText(const QString &text)
90{
91 d->fullText = text;
92 squeezeTextToLabel();
93}
94
95void KSqueezedTextLabel::clear()
96{
97 d->fullText.clear();
98 QLabel::clear();
99}
100
101void 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
126void 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
134Qt::TextElideMode KSqueezedTextLabel::textElideMode() const
135{
136 return d->elideMode;
137}
138
139void KSqueezedTextLabel::setTextElideMode(Qt::TextElideMode mode)
140{
141 d->elideMode = mode;
142 squeezeTextToLabel();
143}
144
145QString KSqueezedTextLabel::fullText() const
146{
147 return d->fullText;
148}
149
150void 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
177void 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"
KAction
Class to encapsulate user-driven action or event.
Definition: kaction.h:217
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
KSqueezedTextLabel::setAlignment
virtual void setAlignment(Qt::Alignment)
Overridden for internal reasons; the API remains unaffected.
Definition: ksqueezedtextlabel.cpp:126
KSqueezedTextLabel::contextMenuEvent
void contextMenuEvent(QContextMenuEvent *)
Definition: ksqueezedtextlabel.cpp:150
KSqueezedTextLabel::minimumSizeHint
virtual QSize minimumSizeHint() const
Definition: ksqueezedtextlabel.cpp:71
KSqueezedTextLabel::setTextElideMode
void setTextElideMode(Qt::TextElideMode mode)
Sets the text elide mode.
Definition: ksqueezedtextlabel.cpp:139
KSqueezedTextLabel::clear
void clear()
Clears the text.
Definition: ksqueezedtextlabel.cpp:95
KSqueezedTextLabel::KSqueezedTextLabel
KSqueezedTextLabel(QWidget *parent=0)
Default constructor.
Definition: ksqueezedtextlabel.cpp:53
KSqueezedTextLabel::fullText
QString fullText() const
Get the full text set via setText.
Definition: ksqueezedtextlabel.cpp:145
KSqueezedTextLabel::resizeEvent
void resizeEvent(QResizeEvent *)
Called when widget is resized.
Definition: ksqueezedtextlabel.cpp:66
KSqueezedTextLabel::~KSqueezedTextLabel
virtual ~KSqueezedTextLabel()
Definition: ksqueezedtextlabel.cpp:61
KSqueezedTextLabel::setText
void setText(const QString &text)
Sets the text.
Definition: ksqueezedtextlabel.cpp:89
KSqueezedTextLabel::squeezeTextToLabel
void squeezeTextToLabel()
does the dirty work
Definition: ksqueezedtextlabel.cpp:101
KSqueezedTextLabel::textElideMode
Qt::TextElideMode textElideMode
Definition: ksqueezedtextlabel.h:49
KSqueezedTextLabel::mouseReleaseEvent
void mouseReleaseEvent(QMouseEvent *)
Definition: ksqueezedtextlabel.cpp:177
KSqueezedTextLabel::sizeHint
virtual QSize sizeHint() const
Definition: ksqueezedtextlabel.cpp:78
QLabel
QMenu
QWidget
kaction.h
kdebug.h
kglobalsettings.h
klocale.h
i18n
QString i18n(const char *text)
ksqueezedtextlabel.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 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-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 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
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