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

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 <kglobalsettings.h>
00029 
00030 class KSqueezedTextLabelPrivate
00031 {
00032 public:
00033 
00034     void _k_copyFullText() {
00035         QMimeData* data = new QMimeData;
00036         data->setText(fullText);
00037         QApplication::clipboard()->setMimeData(data);
00038     }
00039 
00040     QString fullText;
00041     Qt::TextElideMode elideMode;
00042 };
00043 
00044 KSqueezedTextLabel::KSqueezedTextLabel( const QString &text , QWidget *parent )
00045  : QLabel ( parent ),
00046   d( new KSqueezedTextLabelPrivate )
00047 {
00048   setSizePolicy(QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ));
00049   d->fullText = text;
00050   d->elideMode = Qt::ElideMiddle;
00051   squeezeTextToLabel();
00052 }
00053 
00054 KSqueezedTextLabel::KSqueezedTextLabel( QWidget *parent )
00055  : QLabel ( parent ),
00056   d( new KSqueezedTextLabelPrivate )
00057 {
00058   setSizePolicy(QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ));
00059   d->elideMode = Qt::ElideMiddle;
00060 }
00061 
00062 KSqueezedTextLabel::~KSqueezedTextLabel()
00063 {
00064   delete d;
00065 }
00066 
00067 void KSqueezedTextLabel::resizeEvent( QResizeEvent * ) {
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   d->fullText = text;
00091   squeezeTextToLabel();
00092 }
00093 
00094 void KSqueezedTextLabel::clear() {
00095   d->fullText.clear();
00096   QLabel::clear();
00097 }
00098 
00099 void KSqueezedTextLabel::squeezeTextToLabel() {
00100   QFontMetrics fm(fontMetrics());
00101   int labelWidth = size().width();
00102   QStringList squeezedLines;
00103   bool squeezed = false;
00104   Q_FOREACH(const QString& line, d->fullText.split('\n')) {
00105     int lineWidth = fm.width(line);
00106     if (lineWidth > labelWidth) {
00107       squeezed = true;
00108       squeezedLines << fm.elidedText(line, d->elideMode, labelWidth);
00109     } else {
00110       squeezedLines << line;
00111     }
00112   }
00113 
00114   if (squeezed) {
00115     QLabel::setText(squeezedLines.join("\n"));
00116     setToolTip(d->fullText);
00117   } else {
00118     QLabel::setText(d->fullText);
00119     setToolTip( QString() );
00120   }
00121 }
00122 
00123 void KSqueezedTextLabel::setAlignment( Qt::Alignment alignment )
00124 {
00125   // save fullText and restore it
00126   QString tmpFull(d->fullText);
00127   QLabel::setAlignment(alignment);
00128   d->fullText = tmpFull;
00129 }
00130 
00131 Qt::TextElideMode KSqueezedTextLabel::textElideMode() const
00132 {
00133   return d->elideMode;
00134 }
00135 
00136 void KSqueezedTextLabel::setTextElideMode(Qt::TextElideMode mode)
00137 {
00138   d->elideMode = mode;
00139   squeezeTextToLabel();
00140 }
00141 
00142 void KSqueezedTextLabel::contextMenuEvent(QContextMenuEvent* ev)
00143 {
00144     // We want to reimplement "Copy" to include the elided text.
00145     // But this means reimplementing the full popup menu, so no more
00146     // copy-link-address or copy-selection support anymore, since we
00147     // have no access to the QTextDocument.
00148     // Maybe we should have a boolean flag in KSqueezedTextLabel itself for
00149     // whether to show the "Copy Full Text" custom popup?
00150     // For now I chose to show it when the text is squeezed; when it's not, the
00151     // standard popup menu can do the job (select all, copy).
00152 
00153     const bool squeezed = text() != d->fullText;
00154     const bool showCustomPopup = squeezed;
00155     if (showCustomPopup) {
00156         QMenu menu(this);
00157 
00158         KAction* act = new KAction(i18n("&Copy Full Text"), this);
00159         connect(act, SIGNAL(triggered()), this, SLOT(_k_copyFullText()));
00160         menu.addAction(act);
00161 
00162         ev->accept();
00163         menu.exec(ev->globalPos());
00164     } else {
00165         QLabel::contextMenuEvent(ev);
00166     }
00167 }
00168 
00169 #include "ksqueezedtextlabel.moc"

KDEUI

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal