Chris@523: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@523: Chris@523: /* Chris@523: EasyMercurial Chris@523: Chris@523: Based on HgExplorer by Jari Korhonen Chris@523: Copyright (c) 2010 Jari Korhonen Chris@644: Copyright (c) 2013 Chris Cannam Chris@644: Copyright (c) 2013 Queen Mary, University of London Chris@523: Chris@523: This program is free software; you can redistribute it and/or Chris@523: modify it under the terms of the GNU General Public License as Chris@523: published by the Free Software Foundation; either version 2 of the Chris@523: License, or (at your option) any later version. See the file Chris@523: COPYING included with this distribution for more information. Chris@523: */ Chris@523: Chris@523: /* Chris@523: This file adapted from Rosegarden, a sequencer and musical Chris@560: notation editor. Copyright 2000-2012 the Rosegarden development Chris@523: team. Chris@523: Chris@523: Adapted from KDE 4.2.0, this code originally Copyright (c) 2000 Chris@523: Ronny Standtke. Chris@523: */ Chris@523: Chris@523: #include "squeezedlabel.h" Chris@523: Chris@523: #include Chris@523: Chris@523: #include Chris@523: #include Chris@523: #include Chris@523: #include Chris@523: #include Chris@523: #include Chris@523: #include Chris@523: Chris@523: Chris@523: class SqueezedLabelPrivate Chris@523: { Chris@523: public: Chris@523: }; Chris@523: Chris@523: void SqueezedLabel::_k_copyFullText() Chris@523: { Chris@523: QMimeData* data = new QMimeData; Chris@523: data->setText(fullText); Chris@523: QApplication::clipboard()->setMimeData(data); Chris@523: } Chris@523: Chris@523: SqueezedLabel::SqueezedLabel(const QString &text , QWidget *parent) Chris@523: : QLabel (parent) Chris@523: { Chris@523: setObjectName("SQUEEZED"); Chris@523: setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed)); Chris@523: fullText = text; Chris@523: elideMode = Qt::ElideMiddle; Chris@523: squeezeTextToLabel(); Chris@523: } Chris@523: Chris@523: SqueezedLabel::SqueezedLabel(QWidget *parent) Chris@523: : QLabel (parent) Chris@523: { Chris@523: setObjectName("SQUEEZED"); Chris@523: setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed)); Chris@523: elideMode = Qt::ElideMiddle; Chris@523: } Chris@523: Chris@523: SqueezedLabel::~SqueezedLabel() Chris@523: { Chris@523: } Chris@523: Chris@523: void SqueezedLabel::resizeEvent(QResizeEvent *) Chris@523: { Chris@523: squeezeTextToLabel(); Chris@523: } Chris@523: Chris@523: QSize SqueezedLabel::minimumSizeHint() const Chris@523: { Chris@523: QSize sh = QLabel::minimumSizeHint(); Chris@523: sh.setWidth(-1); Chris@523: return sh; Chris@523: } Chris@523: Chris@523: QSize SqueezedLabel::sizeHint() const Chris@523: { Chris@523: int dw = QApplication::desktop()->availableGeometry(QPoint(0, 0)).width(); Chris@523: int maxWidth = dw * 3 / 4; Chris@523: QFontMetrics fm(fontMetrics()); Chris@523: int textWidth = fm.width(fullText); Chris@523: if (textWidth > maxWidth) { Chris@523: textWidth = maxWidth; Chris@523: } Chris@523: return QSize(textWidth, QLabel::sizeHint().height()); Chris@523: } Chris@523: Chris@523: void SqueezedLabel::setText(const QString &text) Chris@523: { Chris@523: fullText = text; Chris@523: squeezeTextToLabel(); Chris@523: } Chris@523: Chris@523: void SqueezedLabel::clear() { Chris@523: fullText.clear(); Chris@523: QLabel::clear(); Chris@523: } Chris@523: Chris@523: void SqueezedLabel::squeezeTextToLabel() { Chris@523: QFontMetrics fm(fontMetrics()); Chris@523: int labelWidth = size().width(); Chris@523: QStringList squeezedLines; Chris@523: bool squeezed = false; Chris@523: Q_FOREACH(const QString& line, fullText.split('\n')) { Chris@523: int lineWidth = fm.width(line); Chris@523: if (lineWidth > labelWidth) { Chris@523: squeezed = true; Chris@523: squeezedLines << fm.elidedText(line, elideMode, labelWidth); Chris@523: } else { Chris@523: squeezedLines << line; Chris@523: } Chris@523: } Chris@523: Chris@523: if (squeezed) { Chris@523: QLabel::setText(squeezedLines.join("\n")); Chris@523: setToolTip(fullText); Chris@523: } else { Chris@523: QLabel::setText(fullText); Chris@523: setToolTip(QString()); Chris@523: } Chris@523: } Chris@523: Chris@523: void SqueezedLabel::setAlignment(Qt::Alignment alignment) Chris@523: { Chris@523: // save fullText and restore it Chris@523: QString tmpFull(fullText); Chris@523: QLabel::setAlignment(alignment); Chris@523: fullText = tmpFull; Chris@523: } Chris@523: Chris@523: Qt::TextElideMode SqueezedLabel::textElideMode() const Chris@523: { Chris@523: return elideMode; Chris@523: } Chris@523: Chris@523: void SqueezedLabel::setTextElideMode(Qt::TextElideMode mode) Chris@523: { Chris@523: elideMode = mode; Chris@523: squeezeTextToLabel(); Chris@523: } Chris@523: Chris@523: void SqueezedLabel::contextMenuEvent(QContextMenuEvent* ev) Chris@523: { Chris@523: // "We" means the KDE team here. Chris@523: // Chris@523: // We want to reimplement "Copy" to include the elided text. Chris@523: // But this means reimplementing the full popup menu, so no more Chris@523: // copy-link-address or copy-selection support anymore, since we Chris@523: // have no access to the QTextDocument. Chris@523: // Maybe we should have a boolean flag in SqueezedLabel itself for Chris@523: // whether to show the "Copy Full Text" custom popup? Chris@523: // For now I chose to show it when the text is squeezed; when it's not, the Chris@523: // standard popup menu can do the job (select all, copy). Chris@523: Chris@523: const bool squeezed = text() != fullText; Chris@523: const bool showCustomPopup = squeezed; Chris@523: if (showCustomPopup) { Chris@523: QMenu menu(this); Chris@523: Chris@523: QAction* act = new QAction(tr("&Copy Full Text"), this); Chris@523: connect(act, SIGNAL(triggered()), this, SLOT(_k_copyFullText())); Chris@523: menu.addAction(act); Chris@523: Chris@523: ev->accept(); Chris@523: menu.exec(ev->globalPos()); Chris@523: } else { Chris@523: QLabel::contextMenuEvent(ev); Chris@523: } Chris@523: } Chris@523: