mirror of
https://github.com/YACReader/yacreader
synced 2025-05-28 03:10:27 -04:00
Replace qrencode with libqrencode (and do some code cleanup)
This commit is contained in:
parent
ce50b285d4
commit
89ed9508ec
@ -9,6 +9,7 @@
|
|||||||
#include <QIntValidator>
|
#include <QIntValidator>
|
||||||
#include <QFormLayout>
|
#include <QFormLayout>
|
||||||
#include <QBitmap>
|
#include <QBitmap>
|
||||||
|
#include <QPainter>
|
||||||
|
|
||||||
#include "startup.h"
|
#include "startup.h"
|
||||||
#include "yacreader_global_gui.h"
|
#include "yacreader_global_gui.h"
|
||||||
@ -82,7 +83,6 @@ ServerConfigDialog::ServerConfigDialog(QWidget * parent)
|
|||||||
:QDialog(parent)
|
:QDialog(parent)
|
||||||
{
|
{
|
||||||
accept = new QPushButton(tr("set port"),this);
|
accept = new QPushButton(tr("set port"),this);
|
||||||
qrCodeImage = new QPixmap();
|
|
||||||
qrCode = new QLabel(this);
|
qrCode = new QLabel(this);
|
||||||
qrCode->move(64, 112);
|
qrCode->move(64, 112);
|
||||||
qrCode->setFixedSize(200,200);
|
qrCode->setFixedSize(200,200);
|
||||||
@ -132,7 +132,7 @@ ServerConfigDialog::ServerConfigDialog(QWidget * parent)
|
|||||||
port->setValidator(validator);
|
port->setValidator(validator);
|
||||||
|
|
||||||
QWidget * portWidget = new QWidget(this);
|
QWidget * portWidget = new QWidget(this);
|
||||||
QHBoxLayout * portWidgetLayout = new QHBoxLayout;
|
QHBoxLayout * portWidgetLayout = new QHBoxLayout(this);
|
||||||
portWidgetLayout->addWidget(port);
|
portWidgetLayout->addWidget(port);
|
||||||
portWidgetLayout->addWidget(accept);
|
portWidgetLayout->addWidget(accept);
|
||||||
portWidgetLayout->setMargin(0);
|
portWidgetLayout->setMargin(0);
|
||||||
@ -152,21 +152,6 @@ ServerConfigDialog::ServerConfigDialog(QWidget * parent)
|
|||||||
performanceWorkaroundCheck->setText(tr("display less information about folders in the browser\nto improve the performance"));
|
performanceWorkaroundCheck->setText(tr("display less information about folders in the browser\nto improve the performance"));
|
||||||
performanceWorkaroundCheck->setStyleSheet("QCheckBox {color:#262626; font-size:13px; font-family: Arial;}");
|
performanceWorkaroundCheck->setStyleSheet("QCheckBox {color:#262626; font-size:13px; font-family: Arial;}");
|
||||||
|
|
||||||
//accept->move(444, 242);
|
|
||||||
//check->setLayoutDirection(Qt::RightToLeft);
|
|
||||||
|
|
||||||
//elementsLayout->setSpacing(40);
|
|
||||||
//elementsLayout->addWidget(iphone);
|
|
||||||
//elementsLayout->addStretch();
|
|
||||||
//elementsLayout->addLayout(configLayout);
|
|
||||||
|
|
||||||
//QVBoxLayout * mainLayout = new QVBoxLayout;
|
|
||||||
//mainLayout->addLayout(elementsLayout);
|
|
||||||
//mainLayout->addLayout(buttons);
|
|
||||||
//mainLayout->addWidget(qrCode,0,1);
|
|
||||||
|
|
||||||
//this->setLayout(mainLayout);
|
|
||||||
|
|
||||||
QPalette Pal(palette());
|
QPalette Pal(palette());
|
||||||
// set black background
|
// set black background
|
||||||
QPalette palette;
|
QPalette palette;
|
||||||
@ -236,16 +221,6 @@ void ServerConfigDialog::enableperformanceWorkaround(int status)
|
|||||||
|
|
||||||
void ServerConfigDialog::generateQR()
|
void ServerConfigDialog::generateQR()
|
||||||
{
|
{
|
||||||
//QString items;
|
|
||||||
//foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces())
|
|
||||||
//{
|
|
||||||
// if (~interface.flags() & QNetworkInterface::IsLoopBack)//interface.flags().testFlag(QNetworkInterface::IsRunning))
|
|
||||||
// foreach (QNetworkAddressEntry entry, interface.addressEntries())
|
|
||||||
// {
|
|
||||||
// if ( interface.hardwareAddress() != "00:00:00:00:00:00" && entry.ip().toString().contains("."))
|
|
||||||
// items.append(interface.name() + entry.ip().toString());
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
ip->clear();
|
ip->clear();
|
||||||
QString dir;
|
QString dir;
|
||||||
|
|
||||||
@ -299,58 +274,29 @@ void ServerConfigDialog::generateQR()
|
|||||||
ip->addItems(otherAddresses);
|
ip->addItems(otherAddresses);
|
||||||
port->setText(s->getPort());
|
port->setText(s->getPort());
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
//qrCode->setText(dir+":8080");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerConfigDialog::generateQR(const QString & serverAddress)
|
void ServerConfigDialog::generateQR(const QString & serverAddress)
|
||||||
{
|
{
|
||||||
qrCode->clear();
|
qrCode->clear();
|
||||||
qrGenerator = new QProcess();
|
|
||||||
QStringList attributes;
|
|
||||||
int pixels = devicePixelRatio() * 8;
|
|
||||||
attributes << "-o" << "-" /*QCoreApplication::applicationDirPath()+"/utils/tmp.png"*/ << "-s" << QString::number(pixels) << "-l" << "H" << "-m" << "0" << serverAddress;
|
|
||||||
connect(qrGenerator,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(updateImage(void)));
|
|
||||||
connect(qrGenerator,SIGNAL(error(QProcess::ProcessError)),this,SLOT(openingError(QProcess::ProcessError))); //TODO: implement openingError
|
|
||||||
#if defined Q_OS_UNIX && !defined Q_OS_MAC
|
|
||||||
qrGenerator->start(QString("qrencode"),attributes);
|
|
||||||
#else
|
|
||||||
qrGenerator->start(QCoreApplication::applicationDirPath()+"/utils/qrencode",attributes);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void ServerConfigDialog::updateImage()
|
QrEncoder encoder;
|
||||||
|
QBitmap image = encoder.encode(serverAddress);
|
||||||
|
if (image.isNull())
|
||||||
{
|
{
|
||||||
QByteArray imgBinary = qrGenerator->readAllStandardOutput();
|
qrCode->setText(tr("Could not load libqrencode."));
|
||||||
//imgBinary = imgBinary.replace(0x0D0A,0x0A);
|
}
|
||||||
|
|
||||||
if(!qrCodeImage->loadFromData(imgBinary))
|
|
||||||
qrCode->setText(tr("QR generator error!"));
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
QPixmap p = *qrCodeImage;
|
image = image.scaled(qrCode->size()*devicePixelRatio());
|
||||||
QPixmap pMask( p.size() );
|
|
||||||
pMask.fill( QColor(66, 66, 66) );
|
|
||||||
pMask.setMask( p.createMaskFromColor( Qt::white ) );
|
|
||||||
|
|
||||||
|
QPixmap pMask(image.size());
|
||||||
|
pMask.fill( QColor(66, 66, 66) );
|
||||||
|
pMask.setMask(image.createMaskFromColor(Qt::white));
|
||||||
pMask.setDevicePixelRatio(devicePixelRatio());
|
pMask.setDevicePixelRatio(devicePixelRatio());
|
||||||
|
|
||||||
*qrCodeImage = pMask;
|
qrCode->setPixmap(pMask);
|
||||||
|
|
||||||
qrCode->setPixmap(*qrCodeImage);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
delete qrGenerator;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* qrCodeImage->load(QCoreApplication::applicationDirPath()+"/utils/tmp.png");
|
|
||||||
qrCode->setPixmap(*qrCodeImage);
|
|
||||||
|
|
||||||
delete qrGenerator;*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerConfigDialog::regenerateQR(const QString & ip)
|
void ServerConfigDialog::regenerateQR(const QString & ip)
|
||||||
@ -372,3 +318,37 @@ void ServerConfigDialog::updatePort()
|
|||||||
generateQR(ip->currentText()+":"+port->text());
|
generateQR(ip->currentText()+":"+port->text());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QrEncoder::QrEncoder()
|
||||||
|
{
|
||||||
|
QLibrary encoder("qrencode");
|
||||||
|
QRcode_encodeString8bit = (_QRcode_encodeString8bit) encoder.resolve("QRcode_encodeString8bit");
|
||||||
|
QRcode_free = (_QRcode_free) encoder.resolve("QRcode_free");
|
||||||
|
}
|
||||||
|
|
||||||
|
QBitmap QrEncoder::encode(const QString & string)
|
||||||
|
{
|
||||||
|
if (!QRcode_encodeString8bit)
|
||||||
|
{
|
||||||
|
return QBitmap();
|
||||||
|
}
|
||||||
|
QRcode *code;
|
||||||
|
code = QRcode_encodeString8bit(string.toUtf8().data(), 0, 0);
|
||||||
|
QBitmap result(code->width, code->width);
|
||||||
|
result.fill();
|
||||||
|
/* convert to QBitmap */
|
||||||
|
QPainter painter;
|
||||||
|
painter.begin(&result);
|
||||||
|
unsigned char * pointer = code->data;
|
||||||
|
for (int x=0; x < code->width; x++) {
|
||||||
|
for (int y=0; y < code->width; y++) {
|
||||||
|
if ((*pointer++ & 0x1)==1)
|
||||||
|
{
|
||||||
|
painter.drawPoint(x, y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
painter.end();
|
||||||
|
QRcode_free(code);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
@ -6,10 +6,9 @@
|
|||||||
#include <QLineEdit>
|
#include <QLineEdit>
|
||||||
#include <QPushButton>
|
#include <QPushButton>
|
||||||
#include <QPixmap>
|
#include <QPixmap>
|
||||||
#include <QProcess>
|
|
||||||
#include <QPixmap>
|
|
||||||
#include <QComboBox>
|
#include <QComboBox>
|
||||||
#include <QCheckBox>
|
#include <QCheckBox>
|
||||||
|
#include <QLibrary>
|
||||||
|
|
||||||
class ServerConfigDialog : public QDialog
|
class ServerConfigDialog : public QDialog
|
||||||
{
|
{
|
||||||
@ -26,15 +25,11 @@ Q_OBJECT
|
|||||||
QPushButton * close;
|
QPushButton * close;
|
||||||
QPushButton * accept;
|
QPushButton * accept;
|
||||||
QLabel * qrCode;
|
QLabel * qrCode;
|
||||||
QPixmap * qrCodeImage;
|
|
||||||
|
|
||||||
QProcess * qrGenerator;
|
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void generateQR();
|
void generateQR();
|
||||||
void generateQR(const QString & serverAddress);
|
void generateQR(const QString & serverAddress);
|
||||||
void regenerateQR(const QString & ip);
|
void regenerateQR(const QString & ip);
|
||||||
void updateImage();
|
|
||||||
void enableServer(int status);
|
void enableServer(int status);
|
||||||
void enableperformanceWorkaround(int status);
|
void enableperformanceWorkaround(int status);
|
||||||
void updatePort();
|
void updatePort();
|
||||||
@ -43,5 +38,22 @@ signals:
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class QrEncoder
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
QrEncoder();
|
||||||
|
QBitmap encode(const QString & string);
|
||||||
|
private:
|
||||||
|
/*libqrencode data structures*/
|
||||||
|
typedef struct {
|
||||||
|
int version; ///< version of the symbol
|
||||||
|
int width; ///< width of the symbol
|
||||||
|
unsigned char *data; ///< symbol data
|
||||||
|
} QRcode;
|
||||||
|
typedef QRcode* (*_QRcode_encodeString8bit)(char [], int, int);
|
||||||
|
typedef void (*_QRcode_free)(QRcode*);
|
||||||
|
_QRcode_free QRcode_free;
|
||||||
|
_QRcode_encodeString8bit QRcode_encodeString8bit;
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user