mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
Cleanup QR PR
Fix coding style, #include "" -> #include <> Remove tooltip Move "tox:" addition out of qr-related code Conflicts: src/misc/qrwidget.cpp
This commit is contained in:
parent
01a4cdd07e
commit
b908852e3b
|
@ -68,13 +68,13 @@ sudo pacman -S --needed base-devel qt5 opencv openal libxss qrencode
|
||||||
|
|
||||||
Debian / Ubuntu:
|
Debian / Ubuntu:
|
||||||
```bash
|
```bash
|
||||||
sudo apt-get install build-essential qt5-qmake qt5-default qttools5-dev-tools libqt5opengl5-dev libqt5svg5-dev libopenal-dev libopencv-dev libxss-dev qrencode
|
sudo apt-get install build-essential qt5-qmake qt5-default qttools5-dev-tools libqt5opengl5-dev libqt5svg5-dev libopenal-dev libopencv-dev libxss-dev qrencode libqrencode-dev
|
||||||
```
|
```
|
||||||
|
|
||||||
Fedora:
|
Fedora:
|
||||||
```bash
|
```bash
|
||||||
yum groupinstall "Development Tools"
|
yum groupinstall "Development Tools"
|
||||||
yum install qt-devel qt-doc qt-creator qt5-qtsvg opencv-devel openal-soft-devel libXScrnSaver-devel qrencode
|
yum install qt-devel qt-doc qt-creator qt5-qtsvg opencv-devel openal-soft-devel libXScrnSaver-devel qrencode-devel
|
||||||
```
|
```
|
||||||
|
|
||||||
Slackware:
|
Slackware:
|
||||||
|
|
|
@ -3,7 +3,8 @@
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QBuffer>
|
#include <QBuffer>
|
||||||
#include <QImage>
|
#include <QImage>
|
||||||
#include "qrencode.h"
|
#include <qrencode.h>
|
||||||
|
#include <sys/errno.h>
|
||||||
|
|
||||||
QRWidget::QRWidget(QWidget *parent) : QWidget(parent), data("0")
|
QRWidget::QRWidget(QWidget *parent) : QWidget(parent), data("0")
|
||||||
//Note: The encoding fails with empty string so I just default to something else.
|
//Note: The encoding fails with empty string so I just default to something else.
|
||||||
|
@ -15,9 +16,9 @@ QRWidget::QRWidget(QWidget *parent) : QWidget(parent), data("0")
|
||||||
image = new QImage(size, QImage::Format_RGB32);
|
image = new QImage(size, QImage::Format_RGB32);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QRWidget::setQRData(QString data)
|
void QRWidget::setQRData(const QString& data)
|
||||||
{
|
{
|
||||||
this->data = "tox:" + data;
|
this->data = data;
|
||||||
paintImage();
|
paintImage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,29 +37,17 @@ bool QRWidget::saveImage(QString path)
|
||||||
return image->save(path, 0, 75); //0 - image format same as file extension, 75-quality, png file is ~6.3kb
|
return image->save(path, 0, 75); //0 - image format same as file extension, 75-quality, png file is ~6.3kb
|
||||||
}
|
}
|
||||||
|
|
||||||
QString QRWidget::getImageAsText()
|
|
||||||
{
|
|
||||||
paintImage();
|
|
||||||
QByteArray ba;
|
|
||||||
QBuffer buffer(&ba);
|
|
||||||
buffer.open(QIODevice::WriteOnly);
|
|
||||||
image->save(&buffer, "PNG"); // writes the image in PNG format inside the buffer
|
|
||||||
|
|
||||||
QString iconBase64 = QString::fromLatin1(ba.toBase64().data());
|
|
||||||
QString base64Image = "<img width=\"300\" heigth=\"300\" src=\"data:image/png;base64," + iconBase64 +"\" />";
|
|
||||||
|
|
||||||
return QString(base64Image);
|
|
||||||
}
|
|
||||||
|
|
||||||
//http://stackoverflow.com/questions/21400254/how-to-draw-a-qr-code-with-qt-in-native-c-c
|
//http://stackoverflow.com/questions/21400254/how-to-draw-a-qr-code-with-qt-in-native-c-c
|
||||||
void QRWidget::paintImage()
|
void QRWidget::paintImage()
|
||||||
{
|
{
|
||||||
QPainter painter(image);
|
QPainter painter(image);
|
||||||
//NOTE: I have hardcoded some parameters here that would make more sense as variables.
|
//NOTE: I have hardcoded some parameters here that would make more sense as variables.
|
||||||
// ECLEVEL_M is much faster recognizable by barcodescanner any any other type
|
// ECLEVEL_M is much faster recognizable by barcodescanner any any other type
|
||||||
QRcode *qr = QRcode_encodeString(data.toStdString().c_str(), 1, QR_ECLEVEL_M, QR_MODE_8, 0);
|
// https://fukuchi.org/works/qrencode/manual/qrencode_8h.html#a4cebc3c670efe1b8866b14c42737fc8f
|
||||||
|
// any mode other than QR_MODE_8 or QR_MODE_KANJI results in EINVAL. First 1 is version, second is case sensitivity
|
||||||
|
QRcode* qr = QRcode_encodeString(data.toStdString().c_str(), 1, QR_ECLEVEL_M, QR_MODE_8, 1);
|
||||||
|
|
||||||
if(0 != qr)
|
if (qr != nullptr)
|
||||||
{
|
{
|
||||||
QColor fg("black");
|
QColor fg("black");
|
||||||
QColor bg("white");
|
QColor bg("white");
|
||||||
|
@ -72,14 +61,14 @@ void QRWidget::paintImage()
|
||||||
const double aspect = w / h;
|
const double aspect = w / h;
|
||||||
const double scale = ((aspect > 1.0) ? h : w) / s;
|
const double scale = ((aspect > 1.0) ? h : w) / s;
|
||||||
|
|
||||||
for(int y = 0; y < s; y++)
|
for (int y = 0; y < s; y++)
|
||||||
{
|
{
|
||||||
const int yy = y * s;
|
const int yy = y * s;
|
||||||
for(int x = 0; x < s; x++)
|
for (int x = 0; x < s; x++)
|
||||||
{
|
{
|
||||||
const int xx = yy + x;
|
const int xx = yy + x;
|
||||||
const unsigned char b = qr->data[xx];
|
const unsigned char b = qr->data[xx];
|
||||||
if(b & 0x01)
|
if (b & 0x01)
|
||||||
{
|
{
|
||||||
const double rx1 = x * scale,
|
const double rx1 = x * scale,
|
||||||
ry1 = y * scale;
|
ry1 = y * scale;
|
||||||
|
@ -99,5 +88,5 @@ void QRWidget::paintImage()
|
||||||
qDebug() << "QR FAIL: " << strerror(errno);
|
qDebug() << "QR FAIL: " << strerror(errno);
|
||||||
}
|
}
|
||||||
|
|
||||||
qr = 0;
|
qr = nullptr;
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,15 +11,14 @@ class QRWidget : public QWidget
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit QRWidget(QWidget *parent = 0);
|
explicit QRWidget(QWidget *parent = 0);
|
||||||
void setQRData(QString data);
|
void setQRData(const QString& data);
|
||||||
QString getImageAsText();
|
|
||||||
QImage* getImage();
|
QImage* getImage();
|
||||||
bool saveImage(QString path);
|
bool saveImage(QString path);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QString data;
|
QString data;
|
||||||
void paintImage();
|
void paintImage();
|
||||||
QImage *image;
|
QImage* image;
|
||||||
QSize size;
|
QSize size;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -172,9 +172,8 @@ void ProfileForm::setToxId(const QString& id)
|
||||||
toxId->setCursorPosition(0);
|
toxId->setCursorPosition(0);
|
||||||
|
|
||||||
qr = new QRWidget();
|
qr = new QRWidget();
|
||||||
qr->setQRData(id);
|
qr->setQRData("tox:"+id);
|
||||||
bodyUI->qrCode->setPixmap(QPixmap::fromImage(qr->getImage()->scaledToWidth(150)));
|
bodyUI->qrCode->setPixmap(QPixmap::fromImage(qr->getImage()->scaledToWidth(150)));
|
||||||
bodyUI->qrCode->setToolTip(qr->getImageAsText());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProfileForm::onAvatarClicked()
|
void ProfileForm::onAvatarClicked()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user