modelnodeoperations.cpp 18.1 KB
Newer Older
1 2
/****************************************************************************
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
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
** 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 <QMessageBox>
#include <nodeabstractproperty.h>
#include <nodemetainfo.h>
#include <modelnode.h>
#include <qmlitemnode.h>
#include <variantproperty.h>
#include <rewritingexception.h>
#include <rewritertransaction.h>
#include <qmlanchors.h>

namespace QmlDesigner {

46
const PropertyName auxDataString("anchors_");
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

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();
    }
}

100
static inline bool modelNodesHaveProperty(const QList<ModelNode> &modelNodeList, const PropertyName &propertyName)
101 102 103 104 105 106 107 108
{
    foreach (const ModelNode &modelNode, modelNodeList)
        if (modelNode.hasProperty(propertyName))
            return true;

    return false;
}

Thomas Hartmann's avatar
Thomas Hartmann committed
109 110 111 112

namespace ModelNodeOperations {

void goIntoComponent(const ModelNode &modelNode)
113 114 115 116
{
    ComponentUtils::goIntoComponent(modelNode);
}

Thomas Hartmann's avatar
Thomas Hartmann committed
117
void select(const SelectionContext &selectionState)
118
{
119 120
    if (selectionState.qmlModelView())
        selectionState.qmlModelView()->setSelectedModelNodes(QList<ModelNode>() << selectionState.targetNode());
121 122
}

Thomas Hartmann's avatar
Thomas Hartmann committed
123
void deSelect(const SelectionContext &selectionState)
124
{
125 126
    if (selectionState.qmlModelView()) {
        QList<ModelNode> selectedNodes = selectionState.qmlModelView()->selectedModelNodes();
127 128 129 130
        foreach (const ModelNode &node, selectionState.selectedModelNodes()) {
            if (selectedNodes.contains(node))
                selectedNodes.removeAll(node);
        }
131
        selectionState.qmlModelView()->setSelectedModelNodes(selectedNodes);
132 133 134
    }
}

Thomas Hartmann's avatar
Thomas Hartmann committed
135
void cut(const SelectionContext &)
136 137 138 139
{
}


Thomas Hartmann's avatar
Thomas Hartmann committed
140
void copy(const SelectionContext &)
141 142 143
{
}

Thomas Hartmann's avatar
Thomas Hartmann committed
144
void deleteSelection(const SelectionContext &)
145 146 147
{
}

Thomas Hartmann's avatar
Thomas Hartmann committed
148
void toFront(const SelectionContext &selectionState)
149
{
150
    if (!selectionState.qmlModelView())
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
        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
166
void toBack(const SelectionContext &selectionState)
167
{
168
    if (!selectionState.qmlModelView())
169 170 171 172 173 174 175 176 177 178 179 180 181 182
        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
183
void raise(const SelectionContext &selectionState)
184
{
185
    if (!selectionState.qmlModelView())
186 187 188
        return;

    try {
189
        RewriterTransaction transaction(selectionState.qmlModelView());
190 191 192 193 194 195 196 197 198 199 200 201 202
        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
203
void lower(const SelectionContext &selectionState)
204 205
{

206
    if (!selectionState.qmlModelView())
207 208 209
        return;

    try {
210
        RewriterTransaction transaction(selectionState.qmlModelView());
211 212 213 214 215 216 217 218 219 220 221 222 223
        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
224
void paste(const SelectionContext &)
225 226 227
{
}

Thomas Hartmann's avatar
Thomas Hartmann committed
228
void undo(const SelectionContext &)
229 230 231
{
}

Thomas Hartmann's avatar
Thomas Hartmann committed
232
void redo(const SelectionContext &)
233 234 235
{
}

Thomas Hartmann's avatar
Thomas Hartmann committed
236
void setVisible(const SelectionContext &selectionState)
237
{
238
    if (!selectionState.qmlModelView())
239 240 241 242 243 244 245 246 247 248
        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
249
void resetSize(const SelectionContext &selectionState)
250
{
251
    if (!selectionState.qmlModelView())
252 253 254
        return;

    try {
255
        RewriterTransaction transaction(selectionState.qmlModelView());
256 257 258 259 260 261 262 263 264
        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
265
void resetPosition(const SelectionContext &selectionState)
266
{
267
    if (!selectionState.qmlModelView())
268 269 270
        return;

    try {
271
        RewriterTransaction transaction(selectionState.qmlModelView());
272 273 274 275 276 277 278 279 280
        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
281
void goIntoComponent(const SelectionContext &selectionState)
282 283 284 285
{
    goIntoComponent(selectionState.currentSingleSelectedNode());
}

Thomas Hartmann's avatar
Thomas Hartmann committed
286
void setId(const SelectionContext &)
287 288 289
{
}

Thomas Hartmann's avatar
Thomas Hartmann committed
290
void resetZ(const SelectionContext &selectionState)
291
{
292
    if (!selectionState.qmlModelView())
293 294
        return;

295
    RewriterTransaction transaction(selectionState.qmlModelView());
296 297 298 299 300
    foreach (ModelNode node, selectionState.selectedModelNodes()) {
        node.removeProperty("z");
    }
}

301
static inline void backupPropertyAndRemove(ModelNode node, const PropertyName &propertyName)
302 303 304 305 306 307 308 309 310 311 312 313 314
{
    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);
    }
}


315
static inline void restoreProperty(ModelNode node, const PropertyName &propertyName)
316 317 318 319 320
{
    if (node.hasAuxiliaryData(auxDataString + propertyName))
        node.variantProperty(propertyName) = node.auxiliaryData(auxDataString + propertyName);
}

Thomas Hartmann's avatar
Thomas Hartmann committed
321
void anchorsFill(const SelectionContext &selectionState)
322
{
323
    if (!selectionState.qmlModelView())
324 325
        return;

326
    RewriterTransaction transaction(selectionState.qmlModelView());
327 328 329 330 331 332

    ModelNode modelNode = selectionState.currentSingleSelectedNode();

    QmlItemNode node = modelNode;
    if (node.isValid()) {
        node.anchors().fill();
333 334 335 336
        backupPropertyAndRemove(modelNode, "x");
        backupPropertyAndRemove(modelNode, "y");
        backupPropertyAndRemove(modelNode, "width");
        backupPropertyAndRemove(modelNode, "height");
337 338 339
    }
}

Thomas Hartmann's avatar
Thomas Hartmann committed
340
void anchorsReset(const SelectionContext &selectionState)
341
{
342
    if (!selectionState.qmlModelView())
343 344
        return;

345
    RewriterTransaction transaction(selectionState.qmlModelView());
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365

    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;

366
        if (parent.hasDefaultProperty())
367
            parentProperty = parent.nodeAbstractProperty(parent.defaultProperty());
368
        else
369
            parentProperty = parent.nodeAbstractProperty("data");
370 371 372 373 374 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

        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
423
void layoutRow(const SelectionContext &selectionState)
424
{
425
    if (!selectionState.qmlModelView())
426 427
        return;

428
    NodeMetaInfo rowMetaInfo = selectionState.qmlModelView()->model()->metaInfo("QtQuick.Row");
429 430 431 432 433 434 435 436

    if (!rowMetaInfo.isValid())
        return;

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

    ModelNode row;
    {
437
        RewriterTransaction transaction(selectionState.qmlModelView());
438 439 440 441 442 443 444

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

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

445
        row = selectionState.qmlModelView()->createModelNode("QtQuick.Row", rowMetaInfo.majorVersion(), rowMetaInfo.minorVersion());
446 447 448 449 450

        reparentTo(row, parent);
    }

    {
451
        RewriterTransaction transaction(selectionState.qmlModelView());
452 453

        QPoint pos = getUpperLeftPosition(modelNodeList);
454 455
        row.variantProperty("x") = pos.x();
        row.variantProperty("y") = pos.y();
456 457 458 459 460 461

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

        foreach (ModelNode modelNode, sortedList) {
            reparentTo(modelNode, row);
462 463
            modelNode.removeProperty("x");
            modelNode.removeProperty("y");
464 465 466 467
        }
    }
}

Thomas Hartmann's avatar
Thomas Hartmann committed
468
void layoutColumn(const SelectionContext &selectionState)
469
{
470
    if (!selectionState.qmlModelView())
471 472
        return;

473
    NodeMetaInfo columnMetaInfo = selectionState.qmlModelView()->model()->metaInfo("QtQuick.Column");
474 475 476 477 478 479 480 481

    if (!columnMetaInfo.isValid())
        return;

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

    ModelNode column;
    {
482
        RewriterTransaction transaction(selectionState.qmlModelView());
483 484 485 486 487

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

488
        column = selectionState.qmlModelView()->createModelNode("QtQuick.Column", columnMetaInfo.majorVersion(), columnMetaInfo.minorVersion());
489 490 491 492 493

        reparentTo(column, parent);
    }

    {
494
        RewriterTransaction transaction(selectionState.qmlModelView());
495 496

        QPoint pos = getUpperLeftPosition(modelNodeList);
497 498
        column.variantProperty("x") = pos.x();
        column.variantProperty("y") = pos.y();
499 500 501 502 503 504

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

        foreach (ModelNode modelNode, sortedList) {
            reparentTo(modelNode, column);
505 506
            modelNode.removeProperty("x");
            modelNode.removeProperty("y");
507 508 509 510
        }
    }
}

Thomas Hartmann's avatar
Thomas Hartmann committed
511
void layoutGrid(const SelectionContext &selectionState)
512
{
513
    if (!selectionState.qmlModelView())
514 515
        return;

516
    NodeMetaInfo gridMetaInfo = selectionState.qmlModelView()->model()->metaInfo("QtQuick.Grid");
517 518 519 520 521 522 523 524

    if (!gridMetaInfo.isValid())
        return;

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

    ModelNode grid;
    {
525
        RewriterTransaction transaction(selectionState.qmlModelView());
526 527 528 529 530

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

531
        grid = selectionState.qmlModelView()->createModelNode("QtQuick.Grid", gridMetaInfo.majorVersion(), gridMetaInfo.minorVersion());
532
        grid.variantProperty("columns") = int(sqrt(double(modelNodeList.count())));
533 534 535 536 537

        reparentTo(grid, parent);
    }

    {
538
        RewriterTransaction transaction(selectionState.qmlModelView());
539 540

        QPoint pos = getUpperLeftPosition(modelNodeList);
541 542
        grid.variantProperty("x") = pos.x();
        grid.variantProperty("y") = pos.y();
543 544 545 546 547 548

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

        foreach (ModelNode modelNode, sortedList) {
            reparentTo(modelNode, grid);
549 550
            modelNode.removeProperty("x");
            modelNode.removeProperty("y");
551 552 553 554
        }
    }
}

Thomas Hartmann's avatar
Thomas Hartmann committed
555
void layoutFlow(const SelectionContext &selectionState)
556
{
557
    if (!selectionState.qmlModelView())
558 559
        return;

560
    NodeMetaInfo flowMetaInfo = selectionState.qmlModelView()->model()->metaInfo("QtQuick.Flow");
561 562 563 564 565 566 567 568

    if (!flowMetaInfo.isValid())
        return;

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

    ModelNode flow;
    {
569
        RewriterTransaction transaction(selectionState.qmlModelView());
570 571 572 573 574

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

575
        flow = selectionState.qmlModelView()->createModelNode("QtQuick.Flow", flowMetaInfo.majorVersion(), flowMetaInfo.minorVersion());
576 577 578 579 580

        reparentTo(flow, parent);
    }

    {
581
        RewriterTransaction transaction(selectionState.qmlModelView());
582 583

        QPoint pos = getUpperLeftPosition(modelNodeList);
584 585
        flow.variantProperty("x") = pos.x();
        flow.variantProperty("y") = pos.y();
586 587 588 589 590 591

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

        foreach (ModelNode modelNode, sortedList) {
            reparentTo(modelNode, flow);
592 593
            modelNode.removeProperty("x");
            modelNode.removeProperty("y");
594 595 596 597
        }
    }
}

Thomas Hartmann's avatar
Thomas Hartmann committed
598 599
} // namespace Mode

600
} //QmlDesigner