modelnodeoperations.cpp 18.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "modelnodeoperations.h"
#include "modelnodecontextmenu_helper.h"

#include <cmath>
#include <QApplication>
#include <QMessageBox>
#include <coreplugin/editormanager/editormanager.h>
#include <nodeabstractproperty.h>
#include <nodelistproperty.h>
#include <nodemetainfo.h>
#include <modelnode.h>
#include <qmlitemnode.h>
#include <variantproperty.h>
#include <bindingproperty.h>
#include <nodeproperty.h>
#include <rewritingexception.h>
#include <rewritertransaction.h>
#include <designmodewidget.h>
#include <qmlanchors.h>

namespace QmlDesigner {

const QString auxDataString = QLatin1String("anchors_");

static inline bool isItem(const ModelNode &node)
{
    return node.isValid() && node.metaInfo().isValid() && node.metaInfo().isSubclassOf("QtQuick.Item", -1, -1);
}

static inline QList<QmlItemNode> siblingsForNode(const QmlItemNode &itemNode)
{
    QList<QmlItemNode> siblingList;

    if (itemNode.isValid() && itemNode.modelNode().parentProperty().isValid()) {
        QList<ModelNode> modelNodes = itemNode.modelNode().parentProperty().parentModelNode().allDirectSubModelNodes();
        foreach (const ModelNode &node, modelNodes) {
            QmlItemNode childItemNode = node;
            if (childItemNode.isValid())
                siblingList.append(childItemNode);
        }
    }

    return siblingList;
}

static signed int getMaxZValue(const QList<QmlItemNode> &siblingList)
{
    signed int maximum = INT_MIN;
    foreach (const QmlItemNode &node, siblingList) {
        signed int z  = node.instanceValue("z").toInt();
        if (z > maximum)
            maximum = z;
    }
    return maximum;
}

static signed int getMinZValue(const QList<QmlItemNode> &siblingList)
{
    signed int minimum = INT_MAX;
    foreach (const QmlItemNode &node, siblingList) {
        signed int z  = node.instanceValue("z").toInt();
        if (z < minimum)
            minimum = z;
    }
    return minimum;
}

static inline void getWidthHeight(const ModelNode &node, int &width, int &height)
{
    QmlItemNode itemNode(node);
    if (itemNode.isValid()) {
        width = itemNode.instanceValue("width").toInt();
        height = itemNode.instanceValue("height").toInt();
    }
}

static inline bool modelNodesHaveProperty(const QList<ModelNode> &modelNodeList, const QString &propertyName)
{
    foreach (const ModelNode &modelNode, modelNodeList)
        if (modelNode.hasProperty(propertyName))
            return true;

    return false;
}

Thomas Hartmann's avatar
Thomas Hartmann committed
115 116 117 118

namespace ModelNodeOperations {

void goIntoComponent(const ModelNode &modelNode)
119 120 121 122
{
    ComponentUtils::goIntoComponent(modelNode);
}

Thomas Hartmann's avatar
Thomas Hartmann committed
123
void select(const SelectionContext &selectionState)
124 125 126 127 128
{
    if (selectionState.view())
        selectionState.view()->setSelectedModelNodes(QList<ModelNode>() << selectionState.targetNode());
}

Thomas Hartmann's avatar
Thomas Hartmann committed
129
void deSelect(const SelectionContext &selectionState)
130 131 132 133 134 135 136 137 138 139 140
{
    if (selectionState.view()) {
        QList<ModelNode> selectedNodes = selectionState.view()->selectedModelNodes();
        foreach (const ModelNode &node, selectionState.selectedModelNodes()) {
            if (selectedNodes.contains(node))
                selectedNodes.removeAll(node);
        }
        selectionState.view()->setSelectedModelNodes(selectedNodes);
    }
}

Thomas Hartmann's avatar
Thomas Hartmann committed
141
void cut(const SelectionContext &)
142 143 144 145
{
}


Thomas Hartmann's avatar
Thomas Hartmann committed
146
void copy(const SelectionContext &)
147 148 149
{
}

Thomas Hartmann's avatar
Thomas Hartmann committed
150
void deleteSelection(const SelectionContext &)
151 152 153
{
}

Thomas Hartmann's avatar
Thomas Hartmann committed
154
void toFront(const SelectionContext &selectionState)
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
{
    if (!selectionState.view())
        return;

    try {
        QmlItemNode node = selectionState.selectedModelNodes().first();
        if (node.isValid()) {
            signed int maximumZ = getMaxZValue(siblingsForNode(node));
            maximumZ++;
            node.setVariantProperty("z", maximumZ);
        }
    } catch (RewritingException &e) { //better save then sorry
        QMessageBox::warning(0, "Error", e.description());
    }
}


Thomas Hartmann's avatar
Thomas Hartmann committed
172
void toBack(const SelectionContext &selectionState)
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
{
    if (!selectionState.view())
        return;
    try {
        QmlItemNode node = selectionState.selectedModelNodes().first();
        if (node.isValid()) {
            signed int minimumZ = getMinZValue(siblingsForNode(node));
            minimumZ--;
            node.setVariantProperty("z", minimumZ);
        }

    } catch (RewritingException &e) { //better save then sorry
        QMessageBox::warning(0, "Error", e.description());
    }
}

Thomas Hartmann's avatar
Thomas Hartmann committed
189
void raise(const SelectionContext &selectionState)
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
{
    if (!selectionState.view())
        return;

    try {
        RewriterTransaction transaction(selectionState.view());
        foreach (ModelNode modelNode, selectionState.selectedModelNodes()) {
            QmlItemNode node = modelNode;
            if (node.isValid()) {
                signed int z  = node.instanceValue("z").toInt();
                z++;
                node.setVariantProperty("z", z);
            }
        }
    } catch (RewritingException &e) { //better save then sorry
         QMessageBox::warning(0, "Error", e.description());
    }
}

Thomas Hartmann's avatar
Thomas Hartmann committed
209
void lower(const SelectionContext &selectionState)
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
{

    if (!selectionState.view())
        return;

    try {
        RewriterTransaction transaction(selectionState.view());
        foreach (ModelNode modelNode, selectionState.selectedModelNodes()) {
            QmlItemNode node = modelNode;
            if (node.isValid()) {
                signed int z  = node.instanceValue("z").toInt();
                z--;
                node.setVariantProperty("z", z);
            }
        }
    } catch (RewritingException &e) { //better save then sorry
        QMessageBox::warning(0, "Error", e.description());
    }
}

Thomas Hartmann's avatar
Thomas Hartmann committed
230
void paste(const SelectionContext &)
231 232 233
{
}

Thomas Hartmann's avatar
Thomas Hartmann committed
234
void undo(const SelectionContext &)
235 236 237
{
}

Thomas Hartmann's avatar
Thomas Hartmann committed
238
void redo(const SelectionContext &)
239 240 241
{
}

Thomas Hartmann's avatar
Thomas Hartmann committed
242
void setVisible(const SelectionContext &selectionState)
243 244 245 246 247 248 249 250 251 252 253 254
{
    if (!selectionState.view())
        return;

    try {
        selectionState.selectedModelNodes().first().variantProperty("visible") = selectionState.toggled();
    } catch (RewritingException &e) { //better save then sorry
        QMessageBox::warning(0, "Error", e.description());
    }
}


Thomas Hartmann's avatar
Thomas Hartmann committed
255
void resetSize(const SelectionContext &selectionState)
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
{
    if (!selectionState.view())
        return;

    try {
        RewriterTransaction transaction(selectionState.view());
        foreach (ModelNode node, selectionState.selectedModelNodes()) {
            node.removeProperty("width");
            node.removeProperty("height");
        }
    } catch (RewritingException &e) { //better save then sorry
        QMessageBox::warning(0, "Error", e.description());
    }
}

Thomas Hartmann's avatar
Thomas Hartmann committed
271
void resetPosition(const SelectionContext &selectionState)
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
{
    if (!selectionState.view())
        return;

    try {
        RewriterTransaction transaction(selectionState.view());
        foreach (ModelNode node, selectionState.selectedModelNodes()) {
            node.removeProperty("x");
            node.removeProperty("y");
        }
    } catch (RewritingException &e) { //better save then sorry
        QMessageBox::warning(0, "Error", e.description());
    }
}

Thomas Hartmann's avatar
Thomas Hartmann committed
287
void goIntoComponent(const SelectionContext &selectionState)
288 289 290 291
{
    goIntoComponent(selectionState.currentSingleSelectedNode());
}

Thomas Hartmann's avatar
Thomas Hartmann committed
292
void setId(const SelectionContext &)
293 294 295
{
}

Thomas Hartmann's avatar
Thomas Hartmann committed
296
void resetZ(const SelectionContext &selectionState)
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
{
    if (!selectionState.view())
        return;

    RewriterTransaction transaction(selectionState.view());
    foreach (ModelNode node, selectionState.selectedModelNodes()) {
        node.removeProperty("z");
    }
}

static inline void backupPropertyAndRemove(ModelNode node, const QString &propertyName)
{
    if (node.hasVariantProperty(propertyName)) {
        node.setAuxiliaryData(auxDataString + propertyName, node.variantProperty(propertyName).value());
        node.removeProperty(propertyName);

    }
    if (node.hasBindingProperty(propertyName)) {
        node.setAuxiliaryData(auxDataString + propertyName, QmlItemNode(node).instanceValue(propertyName));
        node.removeProperty(propertyName);
    }
}


static inline void restoreProperty(ModelNode node, const QString &propertyName)
{
    if (node.hasAuxiliaryData(auxDataString + propertyName))
        node.variantProperty(propertyName) = node.auxiliaryData(auxDataString + propertyName);
}

Thomas Hartmann's avatar
Thomas Hartmann committed
327
void anchorsFill(const SelectionContext &selectionState)
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
{
    if (!selectionState.view())
        return;

    RewriterTransaction transaction(selectionState.view());

    ModelNode modelNode = selectionState.currentSingleSelectedNode();

    QmlItemNode node = modelNode;
    if (node.isValid()) {
        node.anchors().fill();
        backupPropertyAndRemove(modelNode, QLatin1String("x"));
        backupPropertyAndRemove(modelNode, QLatin1String("y"));
        backupPropertyAndRemove(modelNode, QLatin1String("width"));
        backupPropertyAndRemove(modelNode, QLatin1String("height"));
    }
}

Thomas Hartmann's avatar
Thomas Hartmann committed
346
void anchorsReset(const SelectionContext &selectionState)
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
{
    if (!selectionState.view())
        return;

    RewriterTransaction transaction(selectionState.view());

    ModelNode modelNode = selectionState.currentSingleSelectedNode();

    QmlItemNode node = modelNode;
    if (node.isValid()) {
        node.anchors().removeAnchors();
        node.anchors().removeMargins();
        restoreProperty(node, "x");
        restoreProperty(node, "y");
        restoreProperty(node, "width");
        restoreProperty(node, "height");
    }
}

static inline void reparentTo(const ModelNode &node, const QmlItemNode &parent)
{

    if (parent.isValid() && node.isValid()) {
        NodeAbstractProperty parentProperty;

372
        if (parent.hasDefaultProperty())
373
            parentProperty = parent.nodeAbstractProperty(parent.defaultProperty());
374
        else
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
            parentProperty = parent.nodeAbstractProperty(QLatin1String("data"));

        parentProperty.reparentHere(node);
    }
}


bool compareByX(const ModelNode &node1, const ModelNode &node2)
{
        QmlItemNode itemNode1 = QmlItemNode(node1);
        QmlItemNode itemNode2 = QmlItemNode(node2);
        if (itemNode1.isValid() && itemNode2.isValid())
            return itemNode1.instancePosition().x() < itemNode2.instancePosition().x();
        return false;
}

bool compareByY(const ModelNode &node1, const ModelNode &node2)
{
        QmlItemNode itemNode1 = QmlItemNode(node1);
        QmlItemNode itemNode2 = QmlItemNode(node2);
        if (itemNode1.isValid() && itemNode2.isValid())
            return itemNode1.instancePosition().y() < itemNode2.instancePosition().y();
        return false;
}

bool compareByGrid(const ModelNode &node1, const ModelNode &node2)
{
        QmlItemNode itemNode1 = QmlItemNode(node1);
        QmlItemNode itemNode2 = QmlItemNode(node2);
        if (itemNode1.isValid() && itemNode2.isValid()) {
            if ((itemNode1.instancePosition().y() + itemNode1.instanceSize().height())  < itemNode2.instancePosition().y())
                return true;
            if ((itemNode2.instancePosition().y() + itemNode2.instanceSize().height())  < itemNode1.instancePosition().y())
                return false; //first sort y (rows)
            return itemNode1.instancePosition().x() < itemNode2.instancePosition().x();
        }
        return false;
}

static inline QPoint getUpperLeftPosition(const QList<ModelNode> &modelNodeList)
{
    QPoint p(INT_MAX, INT_MAX);
    foreach (ModelNode modelNode, modelNodeList) {
        QmlItemNode itemNode = QmlItemNode(modelNode);
        if (itemNode.isValid()) {
            if (itemNode.instancePosition().x() < p.x())
                p.setX(itemNode.instancePosition().x());
            if (itemNode.instancePosition().y() < p.y())
                p.setY(itemNode.instancePosition().y());
        }
    }
    return p;
}

Thomas Hartmann's avatar
Thomas Hartmann committed
429
void layoutRow(const SelectionContext &selectionState)
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
{
    if (!selectionState.view())
        return;

    NodeMetaInfo rowMetaInfo = selectionState.view()->model()->metaInfo(QLatin1String("QtQuick.Row"));

    if (!rowMetaInfo.isValid())
        return;

    QList<ModelNode> modelNodeList = selectionState.selectedModelNodes();

    ModelNode row;
    {
        RewriterTransaction transaction(selectionState.view());

        QmlItemNode parent = QmlItemNode(modelNodeList.first()).instanceParent().toQmlItemNode();
        if (!parent.isValid())
            return;

        qDebug() << parent.modelNode().majorQtQuickVersion();

        row = selectionState.view()->createModelNode(QLatin1String("QtQuick.Row"), rowMetaInfo.majorVersion(), rowMetaInfo.minorVersion());

        reparentTo(row, parent);
    }

    {
        RewriterTransaction transaction(selectionState.view());

        QPoint pos = getUpperLeftPosition(modelNodeList);
        row.variantProperty(QLatin1String("x")) = pos.x();
        row.variantProperty(QLatin1String("y")) = pos.y();

        QList<ModelNode> sortedList = modelNodeList;
        qSort(sortedList.begin(), sortedList.end(), compareByX);

        foreach (ModelNode modelNode, sortedList) {
            reparentTo(modelNode, row);
            modelNode.removeProperty(QLatin1String("x"));
            modelNode.removeProperty(QLatin1String("y"));
        }
    }
}

Thomas Hartmann's avatar
Thomas Hartmann committed
474
void layoutColumn(const SelectionContext &selectionState)
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
{
    if (!selectionState.view())
        return;

    NodeMetaInfo columnMetaInfo = selectionState.view()->model()->metaInfo(QLatin1String("QtQuick.Column"));

    if (!columnMetaInfo.isValid())
        return;

    QList<ModelNode> modelNodeList = selectionState.selectedModelNodes();

    ModelNode column;
    {
        RewriterTransaction transaction(selectionState.view());

        QmlItemNode parent = QmlItemNode(modelNodeList.first()).instanceParent().toQmlItemNode();
        if (!parent.isValid())
            return;

        column = selectionState.view()->createModelNode(QLatin1String("QtQuick.Column"), columnMetaInfo.majorVersion(), columnMetaInfo.minorVersion());

        reparentTo(column, parent);
    }

    {
        RewriterTransaction transaction(selectionState.view());

        QPoint pos = getUpperLeftPosition(modelNodeList);
        column.variantProperty(QLatin1String("x")) = pos.x();
        column.variantProperty(QLatin1String("y")) = pos.y();

        QList<ModelNode> sortedList = modelNodeList;
        qSort(sortedList.begin(), sortedList.end(), compareByY);

        foreach (ModelNode modelNode, sortedList) {
            reparentTo(modelNode, column);
            modelNode.removeProperty(QLatin1String("x"));
            modelNode.removeProperty(QLatin1String("y"));
        }
    }
}

Thomas Hartmann's avatar
Thomas Hartmann committed
517
void layoutGrid(const SelectionContext &selectionState)
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
{
    if (!selectionState.view())
        return;

    NodeMetaInfo gridMetaInfo = selectionState.view()->model()->metaInfo(QLatin1String("QtQuick.Grid"));

    if (!gridMetaInfo.isValid())
        return;

    QList<ModelNode> modelNodeList = selectionState.selectedModelNodes();

    ModelNode grid;
    {
        RewriterTransaction transaction(selectionState.view());

        QmlItemNode parent = QmlItemNode(modelNodeList.first()).instanceParent().toQmlItemNode();
        if (!parent.isValid())
            return;

        grid = selectionState.view()->createModelNode(QLatin1String("QtQuick.Grid"), gridMetaInfo.majorVersion(), gridMetaInfo.minorVersion());
        grid.variantProperty(QLatin1String("columns")) = int(sqrt(double(modelNodeList.count())));

        reparentTo(grid, parent);
    }

    {
        RewriterTransaction transaction(selectionState.view());

        QPoint pos = getUpperLeftPosition(modelNodeList);
        grid.variantProperty(QLatin1String("x")) = pos.x();
        grid.variantProperty(QLatin1String("y")) = pos.y();

        QList<ModelNode> sortedList = modelNodeList;
        qSort(sortedList.begin(), sortedList.end(), compareByGrid);

        foreach (ModelNode modelNode, sortedList) {
            reparentTo(modelNode, grid);
            modelNode.removeProperty(QLatin1String("x"));
            modelNode.removeProperty(QLatin1String("y"));
        }
    }
}

Thomas Hartmann's avatar
Thomas Hartmann committed
561
void layoutFlow(const SelectionContext &selectionState)
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
{
    if (!selectionState.view())
        return;

    NodeMetaInfo flowMetaInfo = selectionState.view()->model()->metaInfo(QLatin1String("QtQuick.Flow"));

    if (!flowMetaInfo.isValid())
        return;

    QList<ModelNode> modelNodeList = selectionState.selectedModelNodes();

    ModelNode flow;
    {
        RewriterTransaction transaction(selectionState.view());

        QmlItemNode parent = QmlItemNode(modelNodeList.first()).instanceParent().toQmlItemNode();
        if (!parent.isValid())
            return;

        flow = selectionState.view()->createModelNode(QLatin1String("QtQuick.Flow"), flowMetaInfo.majorVersion(), flowMetaInfo.minorVersion());

        reparentTo(flow, parent);
    }

    {
        RewriterTransaction transaction(selectionState.view());

        QPoint pos = getUpperLeftPosition(modelNodeList);
        flow.variantProperty(QLatin1String("x")) = pos.x();
        flow.variantProperty(QLatin1String("y")) = pos.y();

        QList<ModelNode> sortedList = modelNodeList;
        qSort(sortedList.begin(), sortedList.end(), compareByGrid);

        foreach (ModelNode modelNode, sortedList) {
            reparentTo(modelNode, flow);
            modelNode.removeProperty(QLatin1String("x"));
            modelNode.removeProperty(QLatin1String("y"));
        }
    }
}

Thomas Hartmann's avatar
Thomas Hartmann committed
604 605
} // namespace Mode

606
} //QmlDesigner