Commit 0a1acb51 authored by Alberto Mardegan's avatar Alberto Mardegan
Browse files

VncOutput: store transformation as a QTransform

Only compute it when the size of the item or of the VNC content changes.
parent c4179eb7
......@@ -24,6 +24,8 @@
#include <QDebug>
#include <QImage>
#include <QPainter>
#include <QPointF>
#include <QTransform>
using namespace LomiriVNC;
......@@ -36,8 +38,14 @@ public:
VncOutputPrivate(VncOutput *q);
~VncOutputPrivate();
void updateMapping();
private:
VncClient *m_client;
QSize m_vncSize;
QRect m_paintedRect;
QTransform m_itemToVnc;
QTransform m_vncToItem;
VncOutput *q_ptr;
};
......@@ -53,6 +61,33 @@ VncOutputPrivate::~VncOutputPrivate()
{
}
void VncOutputPrivate::updateMapping()
{
Q_Q(VncOutput);
QTransform itemToVnc;
QRect paintedRect;
int w = q->width();
int h = q->height();
const QSize vncSize = m_client ? m_client->image().size() : QSize();
if (vncSize.isValid() && w > 0 && h > 0) {
QSize scaledSize = vncSize.scaled(w, h, Qt::KeepAspectRatio);
int x = (w - scaledSize.width()) / 2;
int y = (h - scaledSize.height()) / 2;
qreal scale = qreal(vncSize.width()) / qreal(scaledSize.width());
paintedRect = QRect(x, y, scaledSize.width(), scaledSize.height());
itemToVnc.scale(scale, scale);
itemToVnc.translate(-x, -y);
}
m_vncSize = vncSize;
m_paintedRect = paintedRect;
m_itemToVnc = itemToVnc;
m_vncToItem = itemToVnc.inverted();
}
VncOutput::VncOutput(QQuickItem *parent):
QQuickPaintedItem(parent),
d_ptr(new VncOutputPrivate(this))
......@@ -78,6 +113,7 @@ void VncOutput::setClient(VncClient *client)
d->m_client->removeViewer(this);
}
d->m_client = client;
d->updateMapping();
Q_EMIT clientChanged();
}
......@@ -92,10 +128,16 @@ void VncOutput::paint(QPainter *painter)
Q_D(VncOutput);
const QImage &image = d->m_client->image();
int w = width();
int h = height();
QSize scaledSize = image.size().scaled(w, h, Qt::KeepAspectRatio);
int x = (w - scaledSize.width()) / 2;
int y = (h - scaledSize.height()) / 2;
painter->drawImage(QRectF(QPointF(x, y), scaledSize), image);
if (image.size() != d->m_vncSize) {
d->updateMapping();
}
painter->drawImage(d->m_paintedRect, image);
}
void VncOutput::geometryChanged(const QRectF &newGeometry,
const QRectF &oldGeometry)
{
Q_D(VncOutput);
QQuickPaintedItem::geometryChanged(newGeometry, oldGeometry);
d->updateMapping();
}
......@@ -45,6 +45,10 @@ public:
Q_SIGNALS:
void clientChanged();
protected:
void geometryChanged(const QRectF &newGeometry,
const QRectF &oldGeometry) override;
private:
Q_DECLARE_PRIVATE(VncOutput)
QScopedPointer<VncOutputPrivate> d_ptr;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment