diff options
Diffstat (limited to 'pica-client/dialogs')
| -rw-r--r-- | pica-client/dialogs/qrscandialog.cpp | 310 | ||||
| -rw-r--r-- | pica-client/dialogs/qrscandialog.h | 109 | ||||
| -rw-r--r-- | pica-client/dialogs/showpicaiddialog.cpp | 32 | ||||
| -rw-r--r-- | pica-client/dialogs/showpicaiddialog.h | 3 |
4 files changed, 454 insertions, 0 deletions
diff --git a/pica-client/dialogs/qrscandialog.cpp b/pica-client/dialogs/qrscandialog.cpp new file mode 100644 index 0000000..db6dac4 --- /dev/null +++ b/pica-client/dialogs/qrscandialog.cpp @@ -0,0 +1,310 @@ +/* + (c) Copyright 2012 - 2026 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 "qrscandialog.h" +#include "../qrcode.h" +#include "../videodevice.h" +#include "../settings.h" +#include "../globals.h" + +#include <QComboBox> +#include <QLabel> +#include <QPushButton> +#include <QVBoxLayout> +#include <QHBoxLayout> +#include <QPainter> +#include <QPainterPath> +#include <QMetaObject> + +// What the camera is asked for. 720p resolves the modules of a code shown on a +// phone held at a comfortable distance, where 480p often does not, and is +// still cheap to search; a camera without it is opened at the nearest size +// below. The rate only has to be enough for the picture to follow the hand. +static const int kScanWidth = 1280; +static const int kScanHeight = 720; +static const int kScanFrameRate = 30; + +// The guide square's side, as a share of the shorter side of the picture. +static const double kGuideSize = 0.6; + +QrViewfinder::QrViewfinder(QWidget *parent) : + QWidget(parent) +{ + setMinimumSize(480, 360); + setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); +} + +void QrViewfinder::setFrame(const QImage &frame) +{ + m_frame = frame; + update(); +} + +void QrViewfinder::setMessage(const QString &message) +{ + m_message = message; + update(); +} + +void QrViewfinder::paintEvent(QPaintEvent *) +{ + QPainter p(this); + + p.fillRect(rect(), Qt::black); + + if (m_frame.isNull()) + { + p.setPen(Qt::white); + p.drawText(rect().adjusted(16, 16, -16, -16), Qt::AlignCenter | Qt::TextWordWrap, m_message); + return; + } + + // The picture fitted into the widget, keeping its aspect ratio. + QSize fitted = m_frame.size().scaled(size(), Qt::KeepAspectRatio); + QRect target(QPoint((width() - fitted.width()) / 2, (height() - fitted.height()) / 2), fitted); + + p.setRenderHint(QPainter::SmoothPixmapTransform); + p.save(); + // Mirrored about the picture's vertical centre line. + p.translate(target.left() * 2 + target.width(), 0); + p.scale(-1, 1); + p.drawImage(target, m_frame); + p.restore(); + + // The guide: everything but the square dimmed... + const int side = (int)(qMin(target.width(), target.height()) * kGuideSize); + const QRect guide(target.center().x() - side / 2, target.center().y() - side / 2, side, side); + + QPainterPath outside; + outside.addRect(target); + QPainterPath inside; + inside.addRect(guide); + p.fillPath(outside.subtracted(inside), QColor(0, 0, 0, 128)); + + // ...and its corners marked. + const int arm = side / 6; + QPen pen(Qt::white); + pen.setWidth(qMax(3, side / 60)); + pen.setCapStyle(Qt::SquareCap); + p.setPen(pen); + p.setRenderHint(QPainter::Antialiasing); + + const QPoint corners[4] = { guide.topLeft(), guide.topRight(), guide.bottomRight(), guide.bottomLeft() }; + const int dx[4] = { 1, -1, -1, 1 }; + const int dy[4] = { 1, 1, -1, -1 }; + + for (int i = 0; i < 4; i++) + { + p.drawLine(corners[i], corners[i] + QPoint(dx[i] * arm, 0)); + p.drawLine(corners[i], corners[i] + QPoint(0, dy[i] * arm)); + } +} + +QrScanDialog::QrScanDialog(QWidget *parent) : + QDialog(parent), + m_sessionNumber(0), + m_cameraRunning(false), + m_readerBusy(false) +{ + setWindowTitle(tr("Scan the QR Code")); + + QVBoxLayout *layout = new QVBoxLayout(this); + + QLabel *hint = new QLabel(tr("Hold the QR code shown by the other person's Pica Pica " + "(<i>Account → Show My Pica Pica ID</i>) inside the square.")); + hint->setTextFormat(Qt::RichText); + hint->setWordWrap(true); + + m_cameras = new QComboBox(); + m_viewfinder = new QrViewfinder(); + m_status = new QLabel(); + m_status->setWordWrap(true); + + m_cancel = new QPushButton(tr("Cancel")); + connect(m_cancel, SIGNAL(clicked()), this, SLOT(reject())); + + QHBoxLayout *cameraRow = new QHBoxLayout(); + cameraRow->addWidget(new QLabel(tr("Camera:"))); + cameraRow->addWidget(m_cameras, 1); + + layout->addWidget(hint); + layout->addLayout(cameraRow); + layout->addWidget(m_viewfinder, 1); + layout->addWidget(m_status); + layout->addWidget(m_cancel, 0, Qt::AlignHCenter); + + m_camera = new VideoDevice(); + m_camera->moveToThread(&m_cameraThread); + connect(m_camera, SIGNAL(captureStarted(QString,int,int,double)), this, SLOT(cameraStarted(QString,int,int,double))); + connect(m_camera, SIGNAL(frameReady(QImage)), this, SLOT(cameraFrame(QImage))); + connect(m_camera, SIGNAL(errorOccurred(QString)), this, SLOT(cameraError(QString))); + connect(m_camera, SIGNAL(cameraBusy()), this, SLOT(cameraBusy())); + m_cameraThread.start(); + + m_reader = new QrCodeReader(); + m_reader->moveToThread(&m_readerThread); + connect(m_reader, SIGNAL(frameRead(QString)), this, SLOT(frameRead(QString))); + m_readerThread.start(); + + // The cameras, with the one calls use selected - with nothing configured + // yet, the first one, as a call would. + // Enumerated through an instance of its own: m_camera belongs to its thread. + VideoDevice enumerator; + QList<MediaDeviceInfo> cameras = enumerator.Enumerate(CAPTURE); + const QString configured = Settings(config_dbname).loadValue("video.capture_device", QString()).toString(); + int selected = 0; + + for (int i = 0; i < cameras.size(); i++) + { + m_cameras->addItem(cameras.at(i).humanReadable.isEmpty() + ? cameras.at(i).device + : QString("%1 (%2)").arg(cameras.at(i).humanReadable, cameras.at(i).device), + cameras.at(i).device); + + if (cameras.at(i).device == configured) + selected = i; + } + + if (cameras.isEmpty()) + { + m_cameras->setEnabled(false); + m_viewfinder->setMessage(tr("No camera found")); + return; + } + + m_cameras->setCurrentIndex(selected); + connect(m_cameras, SIGNAL(currentIndexChanged(int)), this, SLOT(cameraChanged(int))); + + startCamera(); +} + +QrScanDialog::~QrScanDialog() +{ + // Direct call: the camera's thread is inside Preview(), see stopCamera(). + m_camera->Close(); + + m_cameraThread.quit(); + m_cameraThread.wait(); + delete m_camera; + + m_readerThread.quit(); + m_readerThread.wait(); + delete m_reader; +} + +void QrScanDialog::startCamera() +{ + const QString device = m_cameras->itemData(m_cameras->currentIndex()).toString(); + + if (device.isEmpty()) + return; + + m_sessionId = QString("QR code scan %1").arg(++m_sessionNumber).toUtf8(); + m_cameraRunning = true; + m_viewfinder->setFrame(QImage()); + m_viewfinder->setMessage(tr("Starting the camera...")); + m_status->clear(); + + m_camera->openSession(m_sessionId, m_sessionId); + QMetaObject::invokeMethod(m_camera, "configureCapture", Qt::QueuedConnection, + Q_ARG(QString, device), + Q_ARG(int, kScanWidth), Q_ARG(int, kScanHeight), + Q_ARG(int, kScanFrameRate), + Q_ARG(bool, false), Q_ARG(QString, QString()), + Q_ARG(int, 0), Q_ARG(int, 0), + Q_ARG(QByteArray, m_sessionId), Q_ARG(QByteArray, m_sessionId)); + QMetaObject::invokeMethod(m_camera, "Preview", Qt::QueuedConnection); +} + +void QrScanDialog::stopCamera() +{ + // A direct call, since the camera's thread is busy inside Preview() and a + // queued one would wait behind it for good - see VideoDevice. + m_camera->Close(); + m_cameraRunning = false; +} + +void QrScanDialog::cameraChanged(int) +{ + stopCamera(); + startCamera(); +} + +void QrScanDialog::cameraStarted(QString codec, int width, int height, double frameRate) +{ + if (!m_cameraRunning) + return; + + m_status->setText(tr("Camera: %1x%2 @%3 %4") + .arg(width).arg(height) + .arg(frameRate, 0, 'f', frameRate < 10.0 ? 1 : 0) + .arg(codec.isEmpty() ? tr("uncompressed") : codec)); +} + +void QrScanDialog::cameraFrame(QImage frame) +{ + // Frames of a camera already stopped, still in the event queue. + if (!m_cameraRunning) + return; + + m_viewfinder->setFrame(frame); + + if (m_readerBusy) + return; + + m_readerBusy = true; + QMetaObject::invokeMethod(m_reader, "read", Qt::QueuedConnection, Q_ARG(QImage, frame)); +} + +void QrScanDialog::cameraError(QString message) +{ + if (!m_cameraRunning) + return; + + m_cameraRunning = false; + m_viewfinder->setFrame(QImage()); + m_viewfinder->setMessage(message); +} + +void QrScanDialog::cameraBusy() +{ + // Not something to retry from here: the camera is only freed by whoever + // holds it, and the dialog cannot tell when that happens. + cameraError(tr("The camera is in use by a call or another application.\n\n" + "Choose another camera above, or close this window and try again " + "once the camera is free.")); +} + +void QrScanDialog::frameRead(QString text) +{ + m_readerBusy = false; + + if (text.isEmpty() || !m_cameraRunning) + return; + + const QByteArray id = picaIdFromQrText(text); + + if (id.isEmpty()) + { + // Some other QR code - a web address, say. Scanning goes on. + m_status->setText(tr("This QR code does not contain a Pica Pica ID")); + return; + } + + m_scannedId = id; + stopCamera(); + accept(); +} diff --git a/pica-client/dialogs/qrscandialog.h b/pica-client/dialogs/qrscandialog.h new file mode 100644 index 0000000..4ee89e8 --- /dev/null +++ b/pica-client/dialogs/qrscandialog.h @@ -0,0 +1,109 @@ +/* + (c) Copyright 2012 - 2026 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/>. +*/ +#ifndef QRSCANDIALOG_H +#define QRSCANDIALOG_H + +// "Scan the QR Code": adds a contact by pointing the camera at the QR code +// another Pica Pica client shows in its "Show My Pica Pica ID" dialog - on a +// monitor, or on a phone's screen. Only built with HAVE_QRCODE. + +#include <QDialog> +#include <QWidget> +#include <QImage> +#include <QThread> +#include <QByteArray> +#include <QString> + +class QComboBox; +class QLabel; +class QPushButton; +class VideoDevice; +class QrCodeReader; + +// The camera picture with the guide drawn over it: the area outside a centred +// square dimmed and the square's corners marked, for the user to bring the code +// into. Only a guide - the whole picture is searched for a code, wherever in it +// the code may be. +// +// The picture is shown mirrored, the way a camera facing the user is expected to +// look: moving the code to the left moves it to the left on the screen too. +class QrViewfinder : public QWidget +{ + Q_OBJECT +public: + explicit QrViewfinder(QWidget *parent = nullptr); + + // The newest camera frame; a null image goes back to showing message(). + void setFrame(const QImage &frame); + + // Shown while there is no picture: the camera starting, or why it did not. + void setMessage(const QString &message); + +protected: + void paintEvent(QPaintEvent *event) override; + +private: + QImage m_frame; + QString m_message; +}; + +class QrScanDialog : public QDialog +{ + Q_OBJECT +public: + explicit QrScanDialog(QWidget *parent = nullptr); + ~QrScanDialog(); + + // The Pica Pica ID that was read, once the dialog has been accepted. + QByteArray scannedId() const { return m_scannedId; } + +private slots: + void cameraChanged(int index); + void cameraStarted(QString codec, int width, int height, double frameRate); + void cameraFrame(QImage frame); + void cameraError(QString message); + void cameraBusy(); + void frameRead(QString text); + +private: + void startCamera(); + void stopCamera(); + + QComboBox *m_cameras; + QrViewfinder *m_viewfinder; + QLabel *m_status; + QPushButton *m_cancel; + + VideoDevice *m_camera; + QThread m_cameraThread; + QrCodeReader *m_reader; + QThread m_readerThread; + + // Each start of the camera is a session of its own - see MediaCallSession. + // Reusing one would let a preview that is still on its way out after + // Close() carry on as soon as the next start reopened the same session. + int m_sessionNumber; + QByteArray m_sessionId; + bool m_cameraRunning; + + // A frame is with m_reader; the ones arriving meanwhile are only shown. + bool m_readerBusy; + + QByteArray m_scannedId; +}; + +#endif // QRSCANDIALOG_H diff --git a/pica-client/dialogs/showpicaiddialog.cpp b/pica-client/dialogs/showpicaiddialog.cpp index cff91c0..e47e78a 100644 --- a/pica-client/dialogs/showpicaiddialog.cpp +++ b/pica-client/dialogs/showpicaiddialog.cpp @@ -18,6 +18,17 @@ #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) @@ -41,6 +52,27 @@ ShowPicaIdDialog::ShowPicaIdDialog(QString name, QByteArray id, QString caption, 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())); diff --git a/pica-client/dialogs/showpicaiddialog.h b/pica-client/dialogs/showpicaiddialog.h index c8ffd78..82539df 100644 --- a/pica-client/dialogs/showpicaiddialog.h +++ b/pica-client/dialogs/showpicaiddialog.h @@ -36,6 +36,9 @@ private slots: private: QLabel *lbname_; QLineEdit *picaid_; +#ifdef HAVE_QRCODE + QLabel *qrcode_ = nullptr; +#endif QPushButton *btok_; }; |
