1
0
Fork 0

Fix canvas sizing + scaling

This commit is contained in:
James Turner 2017-11-03 15:31:57 +00:00
parent 4bae38f994
commit 623305965b
7 changed files with 43 additions and 43 deletions

View file

@ -81,8 +81,7 @@ bool CanvasConnection::restoreState(QJsonObject state)
m_destRect = QRectF(rect[0].toDouble(), rect[1].toDouble(),
rect[2].toDouble(), rect[3].toDouble());
emit sizeChanged(m_destRect.size());
emit centerChanged(m_destRect.center());
emit geometryChanged();
emit rootPathChanged();
emit webSocketUrlChanged();
@ -100,13 +99,13 @@ void CanvasConnection::showDebugTree()
qWarning() << Q_FUNC_INFO << "implement me!";
}
void CanvasConnection::setCenter(QPointF c)
void CanvasConnection::setOrigin(QPointF c)
{
if (center() == c)
if (m_destRect.topLeft() == c)
return;
m_destRect.moveCenter(c);
emit centerChanged(c);
m_destRect.moveTopLeft(c);
emit geometryChanged();
}
void CanvasConnection::setSize(QSizeF sz)
@ -115,7 +114,7 @@ void CanvasConnection::setSize(QSizeF sz)
return;
m_destRect.setSize(sz);
emit sizeChanged(sz);
emit geometryChanged();
}
void CanvasConnection::connectWebSocket(QByteArray hostName, int port)
@ -133,9 +132,9 @@ void CanvasConnection::connectWebSocket(QByteArray hostName, int port)
setStatus(Connecting);
}
QPointF CanvasConnection::center() const
QPointF CanvasConnection::origin() const
{
return m_destRect.center();
return m_destRect.topLeft();
}
QSizeF CanvasConnection::size() const

View file

@ -40,8 +40,8 @@ class CanvasConnection : public QObject
Q_PROPERTY(Status status READ status NOTIFY statusChanged)
// QML exposed versions of the destination rect
Q_PROPERTY(QPointF center READ center WRITE setCenter NOTIFY centerChanged)
Q_PROPERTY(QSizeF size READ size WRITE setSize NOTIFY sizeChanged)
Q_PROPERTY(QPointF origin READ origin WRITE setOrigin NOTIFY geometryChanged)
Q_PROPERTY(QSizeF size READ size WRITE setSize NOTIFY geometryChanged)
Q_PROPERTY(QUrl webSocketUrl READ webSocketUrl NOTIFY webSocketUrlChanged)
Q_PROPERTY(QString rootPath READ rootPath NOTIFY rootPathChanged)
@ -71,7 +71,7 @@ public:
bool restoreState(QJsonObject state);
void connectWebSocket(QByteArray hostName, int port);
QPointF center() const;
QPointF origin() const;
QSizeF size() const;
@ -97,16 +97,14 @@ public Q_SLOTS:
// not on iOS / Android - requires widgets
void showDebugTree();
void setCenter(QPointF center);
void setOrigin(QPointF center);
void setSize(QSizeF size);
signals:
void statusChanged(Status status);
void centerChanged(QPointF center);
void sizeChanged(QSizeF size);
void geometryChanged();
void rootPathChanged();

View file

@ -123,16 +123,12 @@ void CanvasDisplay::onCanvasSizeChanged()
{
m_sourceSize = QSizeF(m_connection->propertyRoot()->value("size", 400).toDouble(),
m_connection->propertyRoot()->value("size[1]", 400).toDouble());
recomputeScaling();
}
void CanvasDisplay::recomputeScaling()
{
double xScaleFactor = width() / m_sourceSize.width();
double yScaleFactor = height() / m_sourceSize.height();
double finalScaleFactor = std::min(xScaleFactor, yScaleFactor);
setScale(finalScaleFactor);
const double xScaleFactor = width() / m_sourceSize.width();
const double yScaleFactor = height() / m_sourceSize.height();
setScale(std::min(xScaleFactor, yScaleFactor));
}

View file

@ -93,8 +93,7 @@ void FGCanvasGroup::doPolish()
if (_zIndicesDirty) {
std::sort(_children.begin(), _children.end(), ChildOrderingFunction());
_zIndicesDirty = false;
qWarning() << Q_FUNC_INFO << "adjust z order of quick items";
resetChildQuickItemZValues();
}
for (FGCanvasElement* element : _children) {
@ -102,9 +101,20 @@ void FGCanvasGroup::doPolish()
}
}
void FGCanvasGroup::resetChildQuickItemZValues()
{
int counter = 0;
for (auto e : _children) {
auto qq = e->quickItem();
if (qq) {
qq->setZ(counter++);
}
}
}
bool FGCanvasGroup::onChildAdded(LocalProp *prop)
{
const bool isRootGroup = (parent() == nullptr);
const bool isRootGroup = (_parent == nullptr);
int newChildCount = 0;
if (FGCanvasElement::onChildAdded(prop)) {

View file

@ -42,6 +42,7 @@ protected:
private:
void markCachedSymbolDirty();
int indexOfChildWithProp(LocalProp *prop) const;
void resetChildQuickItemZValues();
private:
mutable FGCanvasElementVec _children;

View file

@ -37,7 +37,7 @@ FGQCanvasImage::FGQCanvasImage(FGCanvasGroup* pr, LocalProp* prop) :
void FGQCanvasImage::setEngine(QQmlEngine *engine)
{
static_imageComponent = new QQmlComponent(engine, QUrl("image.qml"));
static_imageComponent = new QQmlComponent(engine, QUrl("qrc:///qml/image.qml"));
if (!static_imageComponent || !static_imageComponent->errors().empty()) {
qWarning() << static_imageComponent->errorString();
}

View file

@ -6,23 +6,21 @@ Item {
property bool showDecorations: true
property alias canvas: canvasDisplay.canvas
readonly property var centerPoint: Qt.point(width / 2, height / 2)
clip: true;
//clip: true;
Component.onCompleted: {
if (canvas) {
width = canvas.size.width
height = canvas.size.height
x = canvas.center.x - (width / 2)
y = canvas.center.y - (height / 2)
x = canvas.origin.x
y = canvas.origin.y
}
}
function saveGeometry()
{
canvas.center = Qt.point(x + (width / 2), y + (height / 2))
canvas.size = Qt.size(root.width / 2, root.height / 2);
canvas.origin = Qt.point(x, y )
canvas.size = Qt.size(root.width, root.height);
}
FG.CanvasDisplay {
@ -33,9 +31,8 @@ Item {
if (canvas) {
root.width = canvas.size.width
root.height = canvas.size.height
root.x = canvas.center.x - (root.width / 2)
root.y = canvas.center.y - (root.height / 2)
root.x = canvas.origin.x
root.y = canvas.origin.y
}
}
}
@ -73,13 +70,11 @@ Item {
onPositionChanged: {
var rootPos = mapToItem(root, mouse.x, mouse.y);
var rootDiff = Qt.point(rootPos.x - root.centerPoint.x,
rootPos.y - root.centerPoint.y);
// var rootDiff = Qt.point(rootPos.x - root.x,
// rootPos.y - root.y);
root.width = rootDiff.x * 2;
root.height = rootDiff.y * 2;
root.x = canvas.center.x - (root.width / 2)
root.y = canvas.center.y - (root.height / 2)
root.width = rootPos.x;
root.height = rootPos.y;
}
onReleased: {
@ -104,6 +99,7 @@ Item {
case FG.CanvasConnection.NotConnected: return "Not connected";
case FG.CanvasConnection.Connecting: return "Connecting";
case FG.CanvasConnection.Connected: return "Connected";
case FG.CanvasConnection.Closed: return "Closed";
case FG.CanvasConnection.Reconnecting: return "Re-connecting";
case FG.CanvasConnection.Error: return "Error";
}