• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.8.5 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 <kglobalsettings.h>
29 
30 class KSqueezedTextLabelPrivate
31 {
32 public:
33 
34  void _k_copyFullText() {
35  QApplication::clipboard()->setText(fullText);
36  }
37 
38  QString fullText;
39  Qt::TextElideMode elideMode;
40 };
41 
42 KSqueezedTextLabel::KSqueezedTextLabel(const QString &text , QWidget *parent)
43  : QLabel (parent),
44  d(new KSqueezedTextLabelPrivate)
45 {
46  setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
47  d->fullText = text;
48  d->elideMode = Qt::ElideMiddle;
49  squeezeTextToLabel();
50 }
51 
52 KSqueezedTextLabel::KSqueezedTextLabel(QWidget *parent)
53  : QLabel (parent),
54  d(new KSqueezedTextLabelPrivate)
55 {
56  setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
57  d->elideMode = Qt::ElideMiddle;
58 }
59 
60 KSqueezedTextLabel::~KSqueezedTextLabel()
61 {
62  delete d;
63 }
64 
65 void KSqueezedTextLabel::resizeEvent(QResizeEvent *)
66 {
67  squeezeTextToLabel();
68 }
69 
70 QSize KSqueezedTextLabel::minimumSizeHint() const
71 {
72  QSize sh = QLabel::minimumSizeHint();
73  sh.setWidth(-1);
74  return sh;
75 }
76 
77 QSize KSqueezedTextLabel::sizeHint() const
78 {
79  int maxWidth = KGlobalSettings::desktopGeometry(this).width() * 3 / 4;
80  QFontMetrics fm(fontMetrics());
81  int textWidth = fm.width(d->fullText);
82  if (textWidth > maxWidth) {
83  textWidth = maxWidth;
84  }
85  return QSize(textWidth, QLabel::sizeHint().height());
86 }
87 
88 void KSqueezedTextLabel::setText(const QString &text)
89 {
90  d->fullText = text;
91  squeezeTextToLabel();
92 }
93 
94 void KSqueezedTextLabel::clear()
95 {
96  d->fullText.clear();
97  QLabel::clear();
98 }
99 
100 void KSqueezedTextLabel::squeezeTextToLabel()
101 {
102  QFontMetrics fm(fontMetrics());
103  int labelWidth = size().width();
104  QStringList squeezedLines;
105  bool squeezed = false;
106  Q_FOREACH(const QString& line, d->fullText.split('\n')) {
107  int lineWidth = fm.width(line);
108  if (lineWidth > labelWidth) {
109  squeezed = true;
110  squeezedLines << fm.elidedText(line, d->elideMode, labelWidth);
111  } else {
112  squeezedLines << line;
113  }
114  }
115 
116  if (squeezed) {
117  QLabel::setText(squeezedLines.join("\n"));
118  setToolTip(d->fullText);
119  } else {
120  QLabel::setText(d->fullText);
121  setToolTip(QString());
122  }
123 }
124 
125 void KSqueezedTextLabel::setAlignment(Qt::Alignment alignment)
126 {
127  // save fullText and restore it
128  QString tmpFull(d->fullText);
129  QLabel::setAlignment(alignment);
130  d->fullText = tmpFull;
131 }
132 
133 Qt::TextElideMode KSqueezedTextLabel::textElideMode() const
134 {
135  return d->elideMode;
136 }
137 
138 void KSqueezedTextLabel::setTextElideMode(Qt::TextElideMode mode)
139 {
140  d->elideMode = mode;
141  squeezeTextToLabel();
142 }
143 
144 QString KSqueezedTextLabel::fullText() const
145 {
146  return d->fullText;
147 }
148 
149 void KSqueezedTextLabel::contextMenuEvent(QContextMenuEvent* ev)
150 {
151  // We want to reimplement "Copy" to include the elided text.
152  // But this means reimplementing the full popup menu, so no more
153  // copy-link-address or copy-selection support anymore, since we
154  // have no access to the QTextDocument.
155  // Maybe we should have a boolean flag in KSqueezedTextLabel itself for
156  // whether to show the "Copy Full Text" custom popup?
157  // For now I chose to show it when the text is squeezed; when it's not, the
158  // standard popup menu can do the job (select all, copy).
159 
160  const bool squeezed = text() != d->fullText;
161  const bool showCustomPopup = squeezed;
162  if (showCustomPopup) {
163  QMenu menu(this);
164 
165  KAction* act = new KAction(i18n("&Copy Full Text"), &menu);
166  connect(act, SIGNAL(triggered()), this, SLOT(_k_copyFullText()));
167  menu.addAction(act);
168 
169  ev->accept();
170  menu.exec(ev->globalPos());
171  } else {
172  QLabel::contextMenuEvent(ev);
173  }
174 }
175 
176 void KSqueezedTextLabel::mouseReleaseEvent(QMouseEvent* ev)
177 {
178 #if QT_VERSION >= 0x040700
179  if (QApplication::clipboard()->supportsSelection() &&
180  textInteractionFlags() != Qt::NoTextInteraction &&
181  ev->button() == Qt::LeftButton &&
182  !d->fullText.isEmpty() &&
183  hasSelectedText()) {
184  // Expand "..." when selecting with the mouse
185  QString txt = selectedText();
186  const QChar ellipsisChar(0x2026); // from qtextengine.cpp
187  const int dotsPos = txt.indexOf(ellipsisChar);
188  if (dotsPos > -1) {
189  // Ex: abcde...yz, selecting de...y (selectionStart=3)
190  // charsBeforeSelection = selectionStart = 2 (ab)
191  // charsAfterSelection = 1 (z)
192  // final selection length= 26 - 2 - 1 = 23
193  const int start = selectionStart();
194  const int charsAfterSelection = text().length() - start - selectedText().length();
195  txt = d->fullText.mid(selectionStart(), d->fullText.length() - start - charsAfterSelection);
196  }
197  QApplication::clipboard()->setText(txt, QClipboard::Selection);
198  } else
199 #endif
200  {
201  QLabel::mouseReleaseEvent(ev);
202  }
203 }
204 
205 #include "ksqueezedtextlabel.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Fri Nov 16 2012 15:02:10 by doxygen 1.8.1 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.8.5 API Reference

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