annotate src/squeezedlabel.cpp @ 571:012ba1b83328

Show cancel button with progress bar only when running an operation that it makes sense to cancel (we don't really want people cancelling e.g. initial folder scan because it would leave things in an inconsistent state)
author Chris Cannam
date Thu, 01 Mar 2012 22:53:54 +0000
parents 533519ebc0cb
children ae67ea0af696
rev   line source
Chris@523 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@523 2
Chris@523 3 /*
Chris@523 4 EasyMercurial
Chris@523 5
Chris@523 6 Based on HgExplorer by Jari Korhonen
Chris@523 7 Copyright (c) 2010 Jari Korhonen
Chris@560 8 Copyright (c) 2012 Chris Cannam
Chris@560 9 Copyright (c) 2012 Queen Mary, University of London
Chris@523 10
Chris@523 11 This program is free software; you can redistribute it and/or
Chris@523 12 modify it under the terms of the GNU General Public License as
Chris@523 13 published by the Free Software Foundation; either version 2 of the
Chris@523 14 License, or (at your option) any later version. See the file
Chris@523 15 COPYING included with this distribution for more information.
Chris@523 16 */
Chris@523 17
Chris@523 18 /*
Chris@523 19 This file adapted from Rosegarden, a sequencer and musical
Chris@560 20 notation editor. Copyright 2000-2012 the Rosegarden development
Chris@523 21 team.
Chris@523 22
Chris@523 23 Adapted from KDE 4.2.0, this code originally Copyright (c) 2000
Chris@523 24 Ronny Standtke.
Chris@523 25 */
Chris@523 26
Chris@523 27 #include "squeezedlabel.h"
Chris@523 28
Chris@523 29 #include <iostream>
Chris@523 30
Chris@523 31 #include <QContextMenuEvent>
Chris@523 32 #include <QAction>
Chris@523 33 #include <QMenu>
Chris@523 34 #include <QClipboard>
Chris@523 35 #include <QApplication>
Chris@523 36 #include <QMimeData>
Chris@523 37 #include <QDesktopWidget>
Chris@523 38
Chris@523 39
Chris@523 40 class SqueezedLabelPrivate
Chris@523 41 {
Chris@523 42 public:
Chris@523 43 };
Chris@523 44
Chris@523 45 void SqueezedLabel::_k_copyFullText()
Chris@523 46 {
Chris@523 47 QMimeData* data = new QMimeData;
Chris@523 48 data->setText(fullText);
Chris@523 49 QApplication::clipboard()->setMimeData(data);
Chris@523 50 }
Chris@523 51
Chris@523 52 SqueezedLabel::SqueezedLabel(const QString &text , QWidget *parent)
Chris@523 53 : QLabel (parent)
Chris@523 54 {
Chris@523 55 setObjectName("SQUEEZED");
Chris@523 56 setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
Chris@523 57 fullText = text;
Chris@523 58 elideMode = Qt::ElideMiddle;
Chris@523 59 squeezeTextToLabel();
Chris@523 60 }
Chris@523 61
Chris@523 62 SqueezedLabel::SqueezedLabel(QWidget *parent)
Chris@523 63 : QLabel (parent)
Chris@523 64 {
Chris@523 65 setObjectName("SQUEEZED");
Chris@523 66 setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
Chris@523 67 elideMode = Qt::ElideMiddle;
Chris@523 68 }
Chris@523 69
Chris@523 70 SqueezedLabel::~SqueezedLabel()
Chris@523 71 {
Chris@523 72 }
Chris@523 73
Chris@523 74 void SqueezedLabel::resizeEvent(QResizeEvent *)
Chris@523 75 {
Chris@523 76 squeezeTextToLabel();
Chris@523 77 }
Chris@523 78
Chris@523 79 QSize SqueezedLabel::minimumSizeHint() const
Chris@523 80 {
Chris@523 81 QSize sh = QLabel::minimumSizeHint();
Chris@523 82 sh.setWidth(-1);
Chris@523 83 return sh;
Chris@523 84 }
Chris@523 85
Chris@523 86 QSize SqueezedLabel::sizeHint() const
Chris@523 87 {
Chris@523 88 int dw = QApplication::desktop()->availableGeometry(QPoint(0, 0)).width();
Chris@523 89 int maxWidth = dw * 3 / 4;
Chris@523 90 QFontMetrics fm(fontMetrics());
Chris@523 91 int textWidth = fm.width(fullText);
Chris@523 92 if (textWidth > maxWidth) {
Chris@523 93 textWidth = maxWidth;
Chris@523 94 }
Chris@523 95 return QSize(textWidth, QLabel::sizeHint().height());
Chris@523 96 }
Chris@523 97
Chris@523 98 void SqueezedLabel::setText(const QString &text)
Chris@523 99 {
Chris@523 100 fullText = text;
Chris@523 101 squeezeTextToLabel();
Chris@523 102 }
Chris@523 103
Chris@523 104 void SqueezedLabel::clear() {
Chris@523 105 fullText.clear();
Chris@523 106 QLabel::clear();
Chris@523 107 }
Chris@523 108
Chris@523 109 void SqueezedLabel::squeezeTextToLabel() {
Chris@523 110 QFontMetrics fm(fontMetrics());
Chris@523 111 int labelWidth = size().width();
Chris@523 112 QStringList squeezedLines;
Chris@523 113 bool squeezed = false;
Chris@523 114 Q_FOREACH(const QString& line, fullText.split('\n')) {
Chris@523 115 int lineWidth = fm.width(line);
Chris@523 116 if (lineWidth > labelWidth) {
Chris@523 117 squeezed = true;
Chris@523 118 squeezedLines << fm.elidedText(line, elideMode, labelWidth);
Chris@523 119 } else {
Chris@523 120 squeezedLines << line;
Chris@523 121 }
Chris@523 122 }
Chris@523 123
Chris@523 124 if (squeezed) {
Chris@523 125 QLabel::setText(squeezedLines.join("\n"));
Chris@523 126 setToolTip(fullText);
Chris@523 127 } else {
Chris@523 128 QLabel::setText(fullText);
Chris@523 129 setToolTip(QString());
Chris@523 130 }
Chris@523 131 }
Chris@523 132
Chris@523 133 void SqueezedLabel::setAlignment(Qt::Alignment alignment)
Chris@523 134 {
Chris@523 135 // save fullText and restore it
Chris@523 136 QString tmpFull(fullText);
Chris@523 137 QLabel::setAlignment(alignment);
Chris@523 138 fullText = tmpFull;
Chris@523 139 }
Chris@523 140
Chris@523 141 Qt::TextElideMode SqueezedLabel::textElideMode() const
Chris@523 142 {
Chris@523 143 return elideMode;
Chris@523 144 }
Chris@523 145
Chris@523 146 void SqueezedLabel::setTextElideMode(Qt::TextElideMode mode)
Chris@523 147 {
Chris@523 148 elideMode = mode;
Chris@523 149 squeezeTextToLabel();
Chris@523 150 }
Chris@523 151
Chris@523 152 void SqueezedLabel::contextMenuEvent(QContextMenuEvent* ev)
Chris@523 153 {
Chris@523 154 // "We" means the KDE team here.
Chris@523 155 //
Chris@523 156 // We want to reimplement "Copy" to include the elided text.
Chris@523 157 // But this means reimplementing the full popup menu, so no more
Chris@523 158 // copy-link-address or copy-selection support anymore, since we
Chris@523 159 // have no access to the QTextDocument.
Chris@523 160 // Maybe we should have a boolean flag in SqueezedLabel itself for
Chris@523 161 // whether to show the "Copy Full Text" custom popup?
Chris@523 162 // For now I chose to show it when the text is squeezed; when it's not, the
Chris@523 163 // standard popup menu can do the job (select all, copy).
Chris@523 164
Chris@523 165 const bool squeezed = text() != fullText;
Chris@523 166 const bool showCustomPopup = squeezed;
Chris@523 167 if (showCustomPopup) {
Chris@523 168 QMenu menu(this);
Chris@523 169
Chris@523 170 QAction* act = new QAction(tr("&Copy Full Text"), this);
Chris@523 171 connect(act, SIGNAL(triggered()), this, SLOT(_k_copyFullText()));
Chris@523 172 menu.addAction(act);
Chris@523 173
Chris@523 174 ev->accept();
Chris@523 175 menu.exec(ev->globalPos());
Chris@523 176 } else {
Chris@523 177 QLabel::contextMenuEvent(ev);
Chris@523 178 }
Chris@523 179 }
Chris@523 180