File: multipagewidget.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 (322 lines) | stat: -rw-r--r-- 9,591 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
 * 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 "multipagewidget.h"
#include "gui/application.h"
#include "support/configuration.h"
#include "support/icon.h"
#include "support/proxystyle.h"
#include "support/squeezedtextlabel.h"
#include "support/utils.h"
#ifdef Q_OS_MAC
#include "support/osxstyle.h"
#endif
#include "listview.h"
#include "singlepagewidget.h"
#include "toolbutton.h"
#include <QGridLayout>
#include <QLabel>
#include <QScrollArea>
#include <QStylePainter>
#include <QVBoxLayout>
#include <algorithm>

class SelectorButton : public ToolButton {
public:
	SelectorButton(const QString& t, const QString& s, const QIcon& icnIn, QWidget* p)
		: ToolButton(p)
	{
		icn = icnIn;
		QGridLayout* layout = new QGridLayout(this);
		icon = new QLabel(this);
		mainText = new SqueezedTextLabel(this);
		subText = new SqueezedTextLabel(this);
		subText->setFont(Utils::smallFont(mainText->font()));
		layout->setSpacing(2);
		int textSize = mainText->sizeHint().height() + subText->sizeHint().height() + layout->spacing();
		textSize += 6;
		int size = textSize;
		if (size < 72) {
			size = Utils::scaleForDpi(32);
		}
		QColor col(mainText->palette().windowText().color());
		subText->setStyleSheet(QString("QLabel{color:rgba(%1, %2, %3, 0.5)}").arg(col.red()).arg(col.green()).arg(col.blue()));

		icon->setFixedSize(size, size);
		layout->addWidget(icon, 0, 0, 2, 1);
		layout->addItem(new QSpacerItem(Utils::layoutSpacing(this), 2, QSizePolicy::Fixed, QSizePolicy::Fixed), 0, 1);
		layout->addWidget(mainText, 0, 2, 1, 1);
		layout->addWidget(subText, 1, 2, 1, 1);
		mainText->setAlignment(Qt::AlignBottom);
		subText->setAlignment(Qt::AlignTop);
		icon->setAlignment(Qt::AlignCenter);
		double dpr = DEVICE_PIXEL_RATIO();
		QPixmap pix = Icon::getScaledPixmap(icn, icon->width() * dpr, icon->height() * dpr, 96 * dpr);
		pix.setDevicePixelRatio(dpr);
		icon->setPixmap(pix);
		setAutoRaise(true);
		setLayout(layout);
		setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
		mainText->setText(t);
		subText->setText(s);
		int left;
		int top;
		int right;
		int bottom;
		layout->getContentsMargins(&left, &top, &right, &bottom);
		layout->setContentsMargins(qMin(8, left), qMin(8, top), qMin(8, right), qMin(8, bottom));

		setMinimumHeight(qMax(textSize, size) + (layout->contentsMargins().top() * 2));
		updateToolTip();
		setFocusPolicy(Qt::TabFocus);

		connect(Icon::fa(), &fa::QtAwesome::defaultOptionsReset, this, &SelectorButton::iconsUpdated);
	}

	void changeEvent(QEvent* e) override
	{
		if (e->type() == QEvent::PaletteChange) {
			QColor col(qApp->palette().windowText().color());
			subText->setStyleSheet(QString("QLabel{color:rgba(%1, %2, %3, 0.5)}").arg(col.red()).arg(col.green()).arg(col.blue()));
		}
		ToolButton::changeEvent(e);
	}

	void setSubText(const QString& str)
	{
		subText->setText(str);
		updateToolTip();
	}

	void updateToolTip()
	{
		setToolTip("<b>" + mainText->fullText() + "</b><br/>" + subText->fullText());
	}

	void paintEvent(QPaintEvent* /*e*/) override
	{
		QStylePainter p(this);
		QStyleOptionToolButton opt;
		initStyleOption(&opt);

		if (opt.state & QStyle::State_Sunken || opt.state & QStyle::State_MouseOver) {
#ifdef Q_OS_MAC
			QColor col = OSXStyle::self()->viewPalette().highlight().color();
#else
			QColor col = qApp->palette().highlight().color();
#endif
			col.setAlphaF(opt.state & QStyle::State_Sunken ? 0.5 : 0.2);
			p.fillRect(opt.rect, col);
		}
		opt.state &= ~(QStyle::State_Sunken | QStyle::State_MouseOver | QStyle::State_Raised | QStyle::State_On);
		opt.state |= QStyle::State_AutoRaise;
		p.drawComplexControl(QStyle::CC_ToolButton, opt);
	}
private Q_SLOTS:
	void iconsUpdated()
	{
		double dpr = DEVICE_PIXEL_RATIO();
		QPixmap pix = Icon::getScaledPixmap(icn, icon->width() * dpr, icon->height() * dpr, 96 * dpr);
		pix.setDevicePixelRatio(dpr);
		icon->setPixmap(pix);
	}

private:
	SqueezedTextLabel* mainText;
	SqueezedTextLabel* subText;
	QLabel* icon;
	QIcon icn;
};

MultiPageWidget::MultiPageWidget(QWidget* p)
	: StackedPageWidget(p)
{
	mainPage = new QWidget(this);
	QVBoxLayout* mainLayout = new QVBoxLayout(mainPage);
	infoLabel = new QLabel(mainPage);
	QWidget* sizer = new QWidget(mainPage);
	Application::fixSize(sizer);
	QScrollArea* scroll = new QScrollArea(this);
	view = new QWidget(scroll);
	QVBoxLayout* layout = new QVBoxLayout(view);
	scroll->setWidget(view);
	scroll->setWidgetResizable(true);
	view->setBackgroundRole(QPalette::Base);

	layout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Fixed, QSizePolicy::MinimumExpanding));
	view->setLayout(layout);
	scroll->setProperty(ProxyStyle::constModifyFrameProp, ProxyStyle::VF_Side | ProxyStyle::VF_Top);
	mainPage->setLayout(mainLayout);
	mainLayout->addWidget(scroll);
	mainLayout->setContentsMargins(0, 0, 0, 0);
	mainLayout->setSpacing(0);
	mainLayout->addWidget(infoLabel);
	infoLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
	mainLayout->addWidget(sizer);
	layout->setSpacing(0);
	layout->setSizeConstraint(QLayout::SetMinimumSize);
	int left;
	int top;
	int right;
	int bottom;
	layout->getContentsMargins(&left, &top, &right, &bottom);
	layout->setContentsMargins(qMin(4, left), qMin(4, top), qMin(4, right), qMin(4, bottom));
#ifdef Q_OS_MAC
	// TODO: This feels a bt of a hack...
	mainPage->setContentsMargins(-3, 0, -3, 0);
#endif
	addWidget(mainPage);
}

MultiPageWidget::~MultiPageWidget()
{
}

static const char* constCurrentPageKey = "currentPage";

void MultiPageWidget::load(Configuration& config)
{
	QString p = config.get(constCurrentPageKey, QString());

	if (!p.isEmpty()) {
		QMap<QString, Entry>::ConstIterator it = entries.find(p);
		if (it != entries.constEnd()) {
			setCurrentWidget(it.value().page);
		}
	}
}

void MultiPageWidget::save(Configuration& config) const
{
	QString p;
	QWidget* cw = currentWidget();

	QMap<QString, Entry>::ConstIterator it = entries.constBegin();
	QMap<QString, Entry>::ConstIterator end = entries.constEnd();

	for (; it != end; ++it) {
		if (it.value().page == cw) {
			p = it.key();
			break;
		}
	}
	config.set(constCurrentPageKey, p);
}
void MultiPageWidget::setInfoText(const QString& text)
{
	infoLabel->setText(text);
}

void MultiPageWidget::addPage(const QString& name, const QString& icon, const QString& text, const QString& subText, QWidget* widget)
{
	QIcon i;
	i.addFile(":" + icon);
	addPage(name, i, text, subText, widget);
}

void MultiPageWidget::addPage(const QString& name, const QIcon& icon, const QString& text, const QString& subText, QWidget* widget)
{
	if (entries.contains(name)) {
		return;
	}
	Entry e(new SelectorButton(text, subText, icon, view), widget);
	static_cast<QVBoxLayout*>(view->layout())->insertWidget(view->layout()->count() - 1, e.btn);
	addWidget(widget);
	entries.insert(name, e);
	connect(e.btn, SIGNAL(clicked()), SLOT(setPage()));
	infoLabel->setVisible(false);
	if (qobject_cast<SinglePageWidget*>(widget)) {
		connect(static_cast<SinglePageWidget*>(widget), SIGNAL(close()), this, SLOT(showMainView()));
	}
	else if (qobject_cast<StackedPageWidget*>(widget)) {
		connect(static_cast<StackedPageWidget*>(widget), SIGNAL(close()), this, SLOT(showMainView()));
	}
}

void MultiPageWidget::removePage(const QString& name)
{
	QMap<QString, Entry>::Iterator it = entries.find(name);
	if (it == entries.end()) {
		return;
	}
	if (it.value().page == currentWidget()) {
		setCurrentWidget(mainPage);
	}
	static_cast<QVBoxLayout*>(view->layout())->removeWidget(it.value().btn);
	it.value().btn->deleteLater();
	entries.erase(it);
}

void MultiPageWidget::updatePageSubText(const QString& name, const QString& text)
{
	QMap<QString, Entry>::Iterator it = entries.find(name);
	if (it == entries.end()) {
		return;
	}
	it.value().btn->setSubText(text);
}

void MultiPageWidget::showMainView()
{
	setCurrentWidget(mainPage);
}

void MultiPageWidget::setPage()
{
	QToolButton* btn = qobject_cast<QToolButton*>(sender());

	if (!btn) {
		return;
	}

	QMap<QString, Entry>::ConstIterator it = entries.constBegin();
	QMap<QString, Entry>::ConstIterator end = entries.constEnd();

	for (; it != end; ++it) {
		if (it.value().btn == btn) {
			setCurrentWidget(it.value().page);
			return;
		}
	}
}

void MultiPageWidget::sortItems()
{
	QList<QString> keys = entries.keys();
	std::sort(keys.begin(), keys.end());
	infoLabel->setVisible(0 == entries.count());
	QVBoxLayout* layout = static_cast<QVBoxLayout*>(view->layout());

	QMap<QString, Entry>::ConstIterator it = entries.constBegin();
	QMap<QString, Entry>::ConstIterator end = entries.constEnd();

	for (; it != end; ++it) {
		layout->removeWidget(it.value().btn);
	}

	for (const QString& key : keys) {
		layout->insertWidget(view->layout()->count() - 1, entries[key].btn);
	}
}

#include "moc_multipagewidget.cpp"