File: view.cpp

package info (click to toggle)
cantata 3.3.1.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,300 kB
  • sloc: cpp: 105,773; perl: 1,366; xml: 723; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (275 lines) | stat: -rw-r--r-- 7,300 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * Cantata
 *
 * Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
 *
 * ----
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "view.h"
#include "gui/settings.h"
#include "network/networkaccessmanager.h"
#include "support/action.h"
#include "support/actioncollection.h"
#include "support/gtkstyle.h"
#include "support/spinner.h"
#include "widgets/icons.h"
#include "widgets/textbrowser.h"
#include <QBuffer>
#include <QComboBox>
#include <QFile>
#include <QImage>
#include <QLabel>
#include <QLocale>
#include <QMenu>
#include <QMouseEvent>
#include <QNetworkReply>
#include <QScrollBar>
#include <QStackedWidget>
#include <QTimer>
#include <QVBoxLayout>
#include <QWheelEvent>

#define CONTEXT_CENTERED

static QString headerTag;
QString View::subTag;

QString View::encode(const QImage& img)
{
	QByteArray bytes;
	QBuffer buffer(&bytes);
	buffer.open(QIODevice::WriteOnly);
	img.save(&buffer, "PNG");
#ifdef CONTEXT_CENTERED
	return QString("<div style=\"text-align:center;\"><img src=\"data:image/png;base64,%1\"/></div>").arg(QString(buffer.data().toBase64()));
#else
	return QString("<img src=\"data:image/png;base64,%1\"/>").arg(QString(buffer.data().toBase64()));
#endif
}

void View::initHeaderTags()
{
	bool small = Settings::self()->wikipediaIntroOnly();
	headerTag = small ? "h2" : "h1";
	subTag = small ? "h3" : "h2";
}

static TextBrowser* createView(QWidget* parent)
{
	TextBrowser* text = new TextBrowser(parent);
	if (GtkStyle::isActive()) {
		text->verticalScrollBar()->setAttribute(Qt::WA_OpaquePaintEvent, false);
	}
	text->setOpenLinks(false);
	text->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	text->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
	text->setFrameShape(QFrame::NoFrame);
	text->setReadOnly(true);
	text->viewport()->setAutoFillBackground(false);
	return text;
}

View::View(QWidget* parent, const QStringList& views)
	: QWidget(parent), needToUpdate(false), spinner(nullptr), selector(nullptr), stack(nullptr)
{
	QVBoxLayout* layout = new QVBoxLayout(this);
	header = new QLabel(this);

	if (views.isEmpty()) {
		TextBrowser* t = createView(this);
		texts.append(t);
	}
	else {
		stack = new QStackedWidget(this);
		selector = new SelectorLabel(this);
		selector->setUseArrow(true);
		for (const QString& v : views) {
			TextBrowser* t = createView(stack);
			selector->addItem(v, v);
			stack->addWidget(t);
			texts.append(t);
		}
		connect(selector, SIGNAL(activated(int)), stack, SLOT(setCurrentIndex(int)));
		connect(selector, SIGNAL(activated(int)), this, SIGNAL(viewChanged()));
	}

	header->setWordWrap(true);
	header->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
	header->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);

	layout->setContentsMargins(2, 2, 2, 2);
	layout->addWidget(header);
	if (views.isEmpty()) {
		layout->addWidget(texts.at(0));
	}
	else {
		layout->addWidget(selector);
		layout->addItem(new QSpacerItem(1, 1, QSizePolicy::MinimumExpanding, QSizePolicy::Fixed));
		layout->addWidget(stack);
	}
	layout->addItem(new QSpacerItem(1, fontMetrics().height() / 4, QSizePolicy::Fixed, QSizePolicy::Fixed));
	if (headerTag.isEmpty()) {
		initHeaderTags();
	}

	cancelJobAction = new Action(Icons::self()->cancelIcon, tr("Cancel"), this);
	cancelJobAction->setEnabled(false);
	connect(cancelJobAction, SIGNAL(triggered()), SLOT(abort()));
	text = texts.at(0);
	setMinimumWidth(Utils::scaleForDpi(300));
}

View::~View()
{
	abort();
}

void View::clear()
{
	setHeader(stdHeader);
	for (TextBrowser* t : texts) {
		t->clear();
	}
}

void View::setHeader(const QString& str)
{
#ifdef CONTEXT_CENTERED
	header->setText("<" + headerTag + " width=\"100%\" align=\"center\">" + str.toHtmlEscaped() + "</" + headerTag + ">");
#else
	header->setText("<" + headerTag + ">" + str.toHtmlEscaped() + "</" + headerTag + ">");
#endif
}

void View::setPicSize(const QSize& sz)
{
	for (TextBrowser* t : texts) {
		t->setPicSize(sz);
	}
}

QSize View::picSize() const
{
	return text->picSize();
}

QString View::createPicTag(const QImage& img, const QString& file)
{
	if (!file.isEmpty() && QFile::exists(file)) {
#ifdef CONTEXT_CENTERED
		return QString("<div style=\"text-align:center;\"><img src=\"%1\"/></div>").arg(file);
#else
		return QString("<img src=\"%1\"/>").arg(file);
#endif
	}
	if (img.isNull()) {
		return QString();
	}
	// No filename given, or file does not exist - therefore encode & scale image.
	return encode(img);
}

void View::showEvent(QShowEvent* e)
{
	if (needToUpdate) {
		update(currentSong, true);
	}
	needToUpdate = false;
	QWidget::showEvent(e);
}

void View::showSpinner(bool enableCancel)
{
	if (!spinner) {
		spinner = new Spinner(this);
		spinner->setWidget(this);
	}
	if (!spinner->isActive()) {
		spinner->start();
		if (enableCancel) {
			cancelJobAction->setEnabled(true);
		}
	}
}

void View::hideSpinner(bool disableCancel)
{
	if (spinner) {
		spinner->stop();
		if (disableCancel) {
			cancelJobAction->setEnabled(false);
		}
	}
}

void View::setEditable(bool e, int index)
{
	TextBrowser* t = texts.at(index);
	t->setReadOnly(!e);
	t->viewport()->setAutoFillBackground(e);
}

void View::setPal(const QPalette& pal, const QColor& linkColor, const QColor& prevLinkColor)
{
	for (TextBrowser* t : texts) {
		// QTextBrowser seems to save link colour within the HTML, so we need to manually
		// update this when the palette changes!
		QString old = t->toHtml();
		t->setPal(pal);
		old = old.replace("color:" + prevLinkColor.name() + ";", "color:" + linkColor.name() + ";");
		t->setHtml(old);
	}
	// header uses window/button text - so need to set these now...
	QPalette hdrPal = pal;
	hdrPal.setColor(QPalette::WindowText, pal.color(QPalette::Text));
	hdrPal.setColor(QPalette::ButtonText, pal.color(QPalette::Text));
	header->setPalette(hdrPal);
	if (selector) {
		selector->setPalette(hdrPal);
		selector->setColor(pal.text().color());
	}
}

void View::addEventFilter(QObject* obj)
{
	installEventFilter(obj);
	for (TextBrowser* t : texts) {
		t->installEventFilter(obj);
		t->viewport()->installEventFilter(obj);
	}
	header->installEventFilter(obj);
}

void View::setHtml(const QString& h, int index)
{
	texts.at(index)->setText(QLatin1String("<html><head><style type=text/css>a:link {color:") + text->palette().link().color().name() + QLatin1String("; text-decoration:underline;}</style></head><body>") + h + QLatin1String("</body></html>"));
}

void View::abort()
{
}

void View::setScaleImage(bool s)
{
	for (TextBrowser* t : texts) {
		t->setFullWidthImage(s);
	}
}

#include "moc_view.cpp"