Replace qrencode with libqrencode (and do some code cleanup)

This commit is contained in:
Felix Kauselmann 2018-03-10 11:22:03 +01:00
parent ce50b285d4
commit 89ed9508ec
2 changed files with 86 additions and 94 deletions

View File

@ -9,6 +9,7 @@
#include <QIntValidator>
#include <QFormLayout>
#include <QBitmap>
#include <QPainter>
#include "startup.h"
#include "yacreader_global_gui.h"
@ -82,7 +83,6 @@ ServerConfigDialog::ServerConfigDialog(QWidget * parent)
:QDialog(parent)
{
accept = new QPushButton(tr("set port"),this);
qrCodeImage = new QPixmap();
qrCode = new QLabel(this);
qrCode->move(64, 112);
qrCode->setFixedSize(200,200);
@ -132,7 +132,7 @@ ServerConfigDialog::ServerConfigDialog(QWidget * parent)
port->setValidator(validator);
QWidget * portWidget = new QWidget(this);
QHBoxLayout * portWidgetLayout = new QHBoxLayout;
QHBoxLayout * portWidgetLayout = new QHBoxLayout(this);
portWidgetLayout->addWidget(port);
portWidgetLayout->addWidget(accept);
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->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());
// set black background
QPalette palette;
@ -190,7 +175,7 @@ ServerConfigDialog::ServerConfigDialog(QWidget * parent)
performanceWorkaroundCheck->setChecked(settings->value(REMOTE_BROWSE_PERFORMANCE_WORKAROUND,false).toBool());
settings->endGroup();
settings->endGroup();
connect(check,SIGNAL(stateChanged(int)),this,SLOT(enableServer(int)));
connect(performanceWorkaroundCheck,SIGNAL(stateChanged(int)),this,SLOT(enableperformanceWorkaround(int)));
@ -236,16 +221,6 @@ void ServerConfigDialog::enableperformanceWorkaround(int status)
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();
QString dir;
@ -258,7 +233,7 @@ void ServerConfigDialog::generateQR()
QString tmp = add.toString();
if(tmp.contains(".") && !tmp.startsWith("127"))
{
otherAddresses.push_back(tmp);
otherAddresses.push_back(tmp);
}
}
@ -299,58 +274,29 @@ void ServerConfigDialog::generateQR()
ip->addItems(otherAddresses);
port->setText(s->getPort());
}
else
{
}
//qrCode->setText(dir+":8080");
}
void ServerConfigDialog::generateQR(const QString & serverAddress)
{
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
}
qrCode->clear();
void ServerConfigDialog::updateImage()
{
QByteArray imgBinary = qrGenerator->readAllStandardOutput();
//imgBinary = imgBinary.replace(0x0D0A,0x0A);
if(!qrCodeImage->loadFromData(imgBinary))
qrCode->setText(tr("QR generator error!"));
else
QrEncoder encoder;
QBitmap image = encoder.encode(serverAddress);
if (image.isNull())
{
QPixmap p = *qrCodeImage;
QPixmap pMask( p.size() );
pMask.fill( QColor(66, 66, 66) );
pMask.setMask( p.createMaskFromColor( Qt::white ) );
qrCode->setText(tr("Could not load libqrencode."));
}
else
{
image = image.scaled(qrCode->size()*devicePixelRatio());
QPixmap pMask(image.size());
pMask.fill( QColor(66, 66, 66) );
pMask.setMask(image.createMaskFromColor(Qt::white));
pMask.setDevicePixelRatio(devicePixelRatio());
*qrCodeImage = pMask;
qrCode->setPixmap(*qrCodeImage);
qrCode->setPixmap(pMask);
}
delete qrGenerator;
/* qrCodeImage->load(QCoreApplication::applicationDirPath()+"/utils/tmp.png");
qrCode->setPixmap(*qrCodeImage);
delete qrGenerator;*/
}
void ServerConfigDialog::regenerateQR(const QString & ip)
@ -372,3 +318,37 @@ void ServerConfigDialog::updatePort()
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;
}

View File

@ -6,10 +6,9 @@
#include <QLineEdit>
#include <QPushButton>
#include <QPixmap>
#include <QProcess>
#include <QPixmap>
#include <QComboBox>
#include <QCheckBox>
#include <QLibrary>
class ServerConfigDialog : public QDialog
{
@ -21,27 +20,40 @@ Q_OBJECT
QLineEdit * port;
QCheckBox * check;
QCheckBox * performanceWorkaroundCheck;
QCheckBox * performanceWorkaroundCheck;
QPushButton * close;
QPushButton * accept;
QLabel * qrCode;
QPixmap * qrCodeImage;
QProcess * qrGenerator;
public slots:
void generateQR();
void generateQR(const QString & serverAddress);
void regenerateQR(const QString & ip);
void updateImage();
void enableServer(int status);
void enableperformanceWorkaround(int status);
void enableperformanceWorkaround(int status);
void updatePort();
signals:
void portChanged(QString port);
};
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