summaryrefslogtreecommitdiffstats
path: root/pica-client/dialogs/showpicaiddialog.cpp
blob: e47e78a026f9ab0910c19e6b04e9d820574856e0 (plain) (blame)
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
/*
	(c) Copyright  2012 - 2018 Anton Sviridenko
	https://picapica.im

	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, version 3.

	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.  If not, see <https://www.gnu.org/licenses/>.
*/
#include "showpicaiddialog.h"
#include <QVBoxLayout>
#include <QFontMetrics>
#include <QStyle>
#include <QPixmap>

#ifdef HAVE_QRCODE
#include "../qrcode.h"

// The QR code's side, in device independent pixels: large enough for a camera
// across a desk to resolve its modules, small enough for the dialog to stay a
// dialog. Rounded to a whole number of physical pixels per module, so that no
// module comes out a pixel narrower than its neighbour.
static const int kQrCodeSide = 300;
#endif

ShowPicaIdDialog::ShowPicaIdDialog(QString name, QByteArray id, QString caption, QWidget *parent) :
	QDialog(parent)
{
	setWindowTitle(caption);
	QVBoxLayout *layout = new QVBoxLayout(this);

	lbname_ = new QLabel(tr("Account: <b>%1</b>").arg(name.toHtmlEscaped()));
	lbname_ ->setTextFormat(Qt::RichText);

	picaid_ = new QLineEdit();

	picaid_ ->setText(id.toBase64().constData());
	picaid_->setReadOnly(true);
	picaid_->setSelection(0, picaid_->text().size());
	picaid_->setMinimumWidth(picaid_->fontMetrics().horizontalAdvance(picaid_->text()) + 16 + 2 * picaid_->style()->pixelMetric(QStyle::PM_DefaultFrameWidth));

	btok_ = new QPushButton(tr("OK"));
	btok_->setDefault(true);
	btok_->setMaximumWidth(btok_->fontMetrics().horizontalAdvance(btok_->text()) * 3 / 2 + 2 * btok_->style()->pixelMetric(QStyle::PM_ButtonMargin));

	layout->addWidget(lbname_);
	layout->addWidget(picaid_);

#ifdef HAVE_QRCODE
	// The same text the line above shows, for another Pica Pica to read with
	// "Contacts -> Scan the QR Code" - or any phone's scanner app.
	const QImage code = picaQrCodeImage(picaid_->text());

	if (!code.isNull())
	{
		const qreal dpr = devicePixelRatioF();
		const int modulePixels = qMax(1, qRound(kQrCodeSide * dpr / code.width()));
		QPixmap pixmap = QPixmap::fromImage(code.scaled(code.size() * modulePixels,
		                                                Qt::IgnoreAspectRatio,
		                                                Qt::FastTransformation));
		pixmap.setDevicePixelRatio(dpr);

		qrcode_ = new QLabel();
		qrcode_->setPixmap(pixmap);
		qrcode_->setAlignment(Qt::AlignCenter);
		layout->addWidget(qrcode_, 0, Qt::AlignHCenter);
	}
#endif
	layout->addWidget(btok_, 0, Qt::AlignHCenter);

	connect(btok_, SIGNAL(clicked()), this, SLOT(Ok()));
}

void ShowPicaIdDialog::Ok()
{
	done(0);
}