File: apikeyssettings.cpp

package info (click to toggle)
cantata 3.4.0.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,428 kB
  • sloc: cpp: 109,584; perl: 1,366; xml: 722; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (125 lines) | stat: -rw-r--r-- 4,997 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
/*
 * Cantata
 *
 * Copyright (c) 2018-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 "apikeyssettings.h"
#include "apikeys.h"
#include "support/utils.h"
#include "widgets/basicitemdelegate.h"
#include <QDesktopServices>
#include <QItemDelegate>
#include <QLabel>
#include <QPainter>
#include <QTreeWidget>
#include <QUrl>
#include <QVBoxLayout>

class ApiKeyDelegate : public BasicItemDelegate {
public:
	ApiKeyDelegate(QObject* parent)
		: BasicItemDelegate(parent) {}

	QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override
	{
		if (2 == index.column()) {
			return BasicItemDelegate::createEditor(parent, option, index);
		}
		return nullptr;
	}

	void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override
	{
		if (index.isValid() && 1 == index.column()) {
			QStyleOptionViewItem opt = option;
			opt.palette.setColor(QPalette::Text, opt.palette.color(QPalette::Link));
			BasicItemDelegate::paint(painter, opt, index);
			painter->setPen(opt.palette.color(opt.state & QStyle::State_Selected ? QPalette::HighlightedText : QPalette::Link));
			QRect r = opt.rect;
			int pad = 3;
			int w = qMin(r.width() - (2 * pad), opt.fontMetrics.horizontalAdvance(index.data().toString()));
			painter->drawLine(r.x() + pad, r.y() + r.height() - 2, r.x() + w - 1, r.y() + r.height() - 2);
		}
		else {
			BasicItemDelegate::paint(painter, option, index);
		}
	}
};

ApiKeysSettings::ApiKeysSettings(QWidget* p)
	: QWidget(p)
{
	int spacing = Utils::layoutSpacing(this);
	QVBoxLayout* layout = new QVBoxLayout(this);
	layout->setContentsMargins(0, 0, 0, 0);
	QLabel* label = new QLabel(tr("Cantata uses various internet services to provide covers, radio stream listings, etc. "
	                              "Most of these require an application's developer to register with the service, and obtain an API Key. "
	                              "Cantata contains the keys required to access these services. Unfortunately, some of these services have "
	                              "a usage limit per API key. Therefore, some of Cantata's features may not function if the key (which is shared "
	                              "with all Cantata users) has reached its monthly usage limit. To work-around this, you can register with a "
	                              "service yourself, and configure Cantata to use your personal API key. This key may be entered below. For any "
	                              "blank values below, Cantata will use its default key."),
	                           this);
	QFont f(Utils::smallFont(label->font()));
	f.setItalic(true);
	label->setFont(f);
	label->setWordWrap(true);
	layout->addWidget(label);
	layout->addItem(new QSpacerItem(spacing, spacing, QSizePolicy::Fixed, QSizePolicy::Fixed));

	tree = new QTreeWidget(this);
	tree->setHeaderLabels(QStringList() << tr("Service") << tr("Service URL") << tr("API Key"));
	tree->setRootIsDecorated(false);
	tree->setUniformRowHeights(true);
	tree->setItemDelegate(new ApiKeyDelegate(this));
	tree->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding));
	layout->addWidget(tree);

	int padding = Utils::scaleForDpi(22);
	int nameWidth = padding;
	int urlWidth = padding;
	QFontMetrics fm(tree->font());
	const auto keys = ApiKeys::self()->getDetails();
	for (const auto& k : keys) {
		QTreeWidgetItem* item = new QTreeWidgetItem(tree, QStringList() << k.name << k.url << k.key);
		item->setFlags(item->flags() | Qt::ItemIsEditable);
		nameWidth = qMax(nameWidth, fm.horizontalAdvance(k.name) + padding);
		urlWidth = qMax(urlWidth, fm.horizontalAdvance(k.url) + padding);
	}
	tree->setColumnWidth(0, nameWidth);
	tree->setColumnWidth(1, urlWidth);
	connect(tree, SIGNAL(itemClicked(QTreeWidgetItem*, int)), SLOT(itemClicked(QTreeWidgetItem*, int)));
}

void ApiKeysSettings::save()
{
	for (int i = 0; i < tree->topLevelItemCount(); ++i) {
		ApiKeys::self()->set((ApiKeys::Service)i, tree->topLevelItem(i)->text(2));
	}
	ApiKeys::self()->save();
}

void ApiKeysSettings::itemClicked(QTreeWidgetItem* item, int column)
{
	if (1 == column) {
		QDesktopServices::openUrl(QUrl(item->text(column)));
	}
}