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