KDEUI
ksqueezedtextlabel.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2000 Ronny Standtke <Ronny.Standtke@gmx.de> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 00017 */ 00018 00019 #include "ksqueezedtextlabel.h" 00020 #include <kdebug.h> 00021 #include <klocale.h> 00022 #include <QContextMenuEvent> 00023 #include <kaction.h> 00024 #include <QMenu> 00025 #include <QClipboard> 00026 #include <QApplication> 00027 #include <QMimeData> 00028 #include <QTextDocument> 00029 #include <kglobalsettings.h> 00030 00031 class KSqueezedTextLabelPrivate 00032 { 00033 public: 00034 00035 void _k_copyFullText() { 00036 QApplication::clipboard()->setText(fullText); 00037 } 00038 00039 QString fullText; 00040 Qt::TextElideMode elideMode; 00041 }; 00042 00043 KSqueezedTextLabel::KSqueezedTextLabel(const QString &text , QWidget *parent) 00044 : QLabel (parent), 00045 d(new KSqueezedTextLabelPrivate) 00046 { 00047 setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed)); 00048 d->fullText = text; 00049 d->elideMode = Qt::ElideMiddle; 00050 squeezeTextToLabel(); 00051 } 00052 00053 KSqueezedTextLabel::KSqueezedTextLabel(QWidget *parent) 00054 : QLabel (parent), 00055 d(new KSqueezedTextLabelPrivate) 00056 { 00057 setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed)); 00058 d->elideMode = Qt::ElideMiddle; 00059 } 00060 00061 KSqueezedTextLabel::~KSqueezedTextLabel() 00062 { 00063 delete d; 00064 } 00065 00066 void KSqueezedTextLabel::resizeEvent(QResizeEvent *) 00067 { 00068 squeezeTextToLabel(); 00069 } 00070 00071 QSize KSqueezedTextLabel::minimumSizeHint() const 00072 { 00073 QSize sh = QLabel::minimumSizeHint(); 00074 sh.setWidth(-1); 00075 return sh; 00076 } 00077 00078 QSize KSqueezedTextLabel::sizeHint() const 00079 { 00080 int maxWidth = KGlobalSettings::desktopGeometry(this).width() * 3 / 4; 00081 QFontMetrics fm(fontMetrics()); 00082 int textWidth = fm.width(d->fullText); 00083 if (textWidth > maxWidth) { 00084 textWidth = maxWidth; 00085 } 00086 return QSize(textWidth, QLabel::sizeHint().height()); 00087 } 00088 00089 void KSqueezedTextLabel::setText(const QString &text) 00090 { 00091 d->fullText = text; 00092 squeezeTextToLabel(); 00093 } 00094 00095 void KSqueezedTextLabel::clear() 00096 { 00097 d->fullText.clear(); 00098 QLabel::clear(); 00099 } 00100 00101 void KSqueezedTextLabel::squeezeTextToLabel() 00102 { 00103 QFontMetrics fm(fontMetrics()); 00104 int labelWidth = size().width(); 00105 QStringList squeezedLines; 00106 bool squeezed = false; 00107 Q_FOREACH(const QString& line, d->fullText.split('\n')) { 00108 int lineWidth = fm.width(line); 00109 if (lineWidth > labelWidth) { 00110 squeezed = true; 00111 squeezedLines << fm.elidedText(line, d->elideMode, labelWidth); 00112 } else { 00113 squeezedLines << line; 00114 } 00115 } 00116 00117 if (squeezed) { 00118 QLabel::setText(squeezedLines.join("\n")); 00119 setToolTip(d->fullText); 00120 } else { 00121 QLabel::setText(d->fullText); 00122 setToolTip(QString()); 00123 } 00124 } 00125 00126 void KSqueezedTextLabel::setAlignment(Qt::Alignment alignment) 00127 { 00128 // save fullText and restore it 00129 QString tmpFull(d->fullText); 00130 QLabel::setAlignment(alignment); 00131 d->fullText = tmpFull; 00132 } 00133 00134 Qt::TextElideMode KSqueezedTextLabel::textElideMode() const 00135 { 00136 return d->elideMode; 00137 } 00138 00139 void KSqueezedTextLabel::setTextElideMode(Qt::TextElideMode mode) 00140 { 00141 d->elideMode = mode; 00142 squeezeTextToLabel(); 00143 } 00144 00145 QString KSqueezedTextLabel::fullText() const 00146 { 00147 return d->fullText; 00148 } 00149 00150 void KSqueezedTextLabel::contextMenuEvent(QContextMenuEvent* ev) 00151 { 00152 // We want to reimplement "Copy" to include the elided text. 00153 // But this means reimplementing the full popup menu, so no more 00154 // copy-link-address or copy-selection support anymore, since we 00155 // have no access to the QTextDocument. 00156 // Maybe we should have a boolean flag in KSqueezedTextLabel itself for 00157 // whether to show the "Copy Full Text" custom popup? 00158 // For now I chose to show it when the text is squeezed; when it's not, the 00159 // standard popup menu can do the job (select all, copy). 00160 00161 const bool squeezed = text() != d->fullText; 00162 const bool showCustomPopup = squeezed; 00163 if (showCustomPopup) { 00164 QMenu menu(this); 00165 00166 KAction* act = new KAction(i18n("&Copy Full Text"), &menu); 00167 connect(act, SIGNAL(triggered()), this, SLOT(_k_copyFullText())); 00168 menu.addAction(act); 00169 00170 ev->accept(); 00171 menu.exec(ev->globalPos()); 00172 } else { 00173 QLabel::contextMenuEvent(ev); 00174 } 00175 } 00176 00177 void KSqueezedTextLabel::mouseReleaseEvent(QMouseEvent* ev) 00178 { 00179 #if QT_VERSION >= 0x040700 00180 if (QApplication::clipboard()->supportsSelection() && 00181 textInteractionFlags() != Qt::NoTextInteraction && 00182 ev->button() == Qt::LeftButton && 00183 !d->fullText.isEmpty() && 00184 hasSelectedText()) { 00185 // Expand "..." when selecting with the mouse 00186 QString txt = selectedText(); 00187 const QChar ellipsisChar(0x2026); // from qtextengine.cpp 00188 const int dotsPos = txt.indexOf(ellipsisChar); 00189 if (dotsPos > -1) { 00190 // Ex: abcde...yz, selecting de...y (selectionStart=3) 00191 // charsBeforeSelection = selectionStart = 2 (ab) 00192 // charsAfterSelection = 1 (z) 00193 // final selection length= 26 - 2 - 1 = 23 00194 const int start = selectionStart(); 00195 int charsAfterSelection = text().length() - start - selectedText().length(); 00196 txt = d->fullText; 00197 // Strip markup tags 00198 if (textFormat() == Qt::RichText 00199 || (textFormat() == Qt::AutoText && Qt::mightBeRichText(txt))) { 00200 txt.replace(QRegExp("<[^>]*>"), ""); 00201 // account for stripped characters 00202 charsAfterSelection -= d->fullText.length() - txt.length(); 00203 } 00204 txt = txt.mid(selectionStart(), txt.length() - start - charsAfterSelection); 00205 } 00206 QApplication::clipboard()->setText(txt, QClipboard::Selection); 00207 } else 00208 #endif 00209 { 00210 QLabel::mouseReleaseEvent(ev); 00211 } 00212 } 00213 00214 #include "ksqueezedtextlabel.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2019 The KDE developers.
Generated on Mon Jan 21 2019 12:32:43 by doxygen 1.7.5.1 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2019 The KDE developers.
Generated on Mon Jan 21 2019 12:32:43 by doxygen 1.7.5.1 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.