designdocumentcontroller.cpp 22.4 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
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
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "designdocumentcontroller.h"
#include "designdocumentcontrollerview.h"
#include "xuifiledialog.h"
#include "componentview.h"
#include "subcomponentmanager.h"
#include "model/viewlogger.h"

#include <allpropertiesbox.h>
#include <itemlibrary.h>
#include <navigatorview.h>
#include <stateseditorwidget.h>
#include <formeditorview.h>
#include <formeditorwidget.h>
43
#include <basetexteditmodifier.h>
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
#include <componenttextmodifier.h>
#include <metainfo.h>
#include <invalidargumentexception.h>
#include <componentaction.h>
#include <qmlobjectnode.h>
#include <rewriterview.h>
#include <nodelistproperty.h>
#include <toolbox.h>

#include <QtCore/QCoreApplication>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QUrl>
#include <QtCore/QProcess>
#include <QtCore/QTemporaryFile>
#include <QtCore/QtDebug>
#include <QtCore/QEvent>

#include <QtGui/QBoxLayout>
#include <QtGui/QComboBox>
#include <QtGui/QErrorMessage>
#include <QtGui/QFileDialog>
#include <QtGui/QLabel>
#include <QtGui/QMdiArea>
#include <QtGui/QMdiSubWindow>
#include <QtGui/QMessageBox>
#include <QtGui/QUndoStack>
#include <QtGui/QPlainTextEdit>

enum {
    debug = false
};

namespace QmlDesigner {

class DesignDocumentControllerPrivate {
public:
    QWeakPointer<FormEditorView> formEditorView;

    QWeakPointer<ItemLibrary> itemLibrary;
    QWeakPointer<NavigatorView> navigator;
    QWeakPointer<AllPropertiesBox> allPropertiesBox;
    QWeakPointer<StatesEditorWidget> statesEditorWidget;
    QWeakPointer<QStackedWidget> stackedWidget;

    QWeakPointer<QmlDesigner::Model> model;
    QWeakPointer<QmlDesigner::Model> subComponentModel;
    QWeakPointer<QmlDesigner::Model> masterModel;
    QWeakPointer<QPlainTextEdit> textEdit;
    QWeakPointer<RewriterView> rewriterView;
95
    QmlDesigner::BaseTextEditModifier *textModifier;
96
97
98
99
100
101
102
103
    QmlDesigner::ComponentTextModifier *componentTextModifier;
    QWeakPointer<SubComponentManager> subComponentManager;
    QWeakPointer<Internal::ViewLogger> viewLogger;

    QString fileName;
    QUrl searchPath;
    bool documentLoaded;
    bool syncBlocked;
104
    QWeakPointer<ComponentAction> componentAction;
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

};

/**
  \class QmlDesigner::DesignDocumentController

  DesignDocumentController acts as a facade to a model representing a qml document,
  and the different views/widgets accessing it.
  */
DesignDocumentController::DesignDocumentController(QObject *parent) :
        QObject(parent),
        m_d(new DesignDocumentControllerPrivate)
{
    m_d->documentLoaded = false;
    m_d->syncBlocked = false;
}

DesignDocumentController::~DesignDocumentController()
123
{
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
    delete m_d->model.data();
    delete m_d->subComponentModel.data();

    delete m_d->rewriterView.data();

    if (m_d->componentTextModifier) //componentTextModifier might not be created
        delete m_d->componentTextModifier;

    delete m_d;
}

Model *DesignDocumentController::model() const
{
    return m_d->model.data();
}

Model *DesignDocumentController::masterModel() const
{
    return m_d->masterModel.data();
}

145
QWidget *DesignDocumentController::centralWidget() const
146
{
147
    return qobject_cast<QWidget*>(parent());
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
}

/*!
  Returns whether the model is automatically updated if the text editor changes.
  */
bool DesignDocumentController::isModelSyncBlocked() const
{
    return m_d->syncBlocked;
}

/*!
  Switches whether the model (and therefore the views) are updated if the text editor
  changes.

  If the synchronization is enabled again, the model is automatically resynchronized
  with the current state of the text editor.
  */
void DesignDocumentController::blockModelSync(bool block)
{
    if (m_d->syncBlocked == block)
        return;

    m_d->syncBlocked = block;

    if (m_d->textModifier) {
        if (m_d->syncBlocked) {
            m_d->textModifier->deactivateChangeSignals();
        } else {
            m_d->textModifier->reactivateChangeSignals();
        }
    }
}

/*!
  Returns any errors that happened when parsing the latest qml file.
  */
QList<RewriterView::Error> DesignDocumentController::qmlErrors() const
{
    return m_d->rewriterView->errors();
}

189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
void DesignDocumentController::setItemLibrary(ItemLibrary* itemLibrary)
{
    m_d->itemLibrary = itemLibrary;
}

void DesignDocumentController::setNavigator(NavigatorView* navigatorView)
{
    m_d->navigator = navigatorView;
}

void DesignDocumentController::setAllPropertiesBox(AllPropertiesBox* allPropertiesBox)
{
    m_d->allPropertiesBox = allPropertiesBox;
}

void DesignDocumentController::setStatesEditorWidget(StatesEditorWidget* statesEditorWidget)
205
{
206
    m_d->statesEditorWidget = statesEditorWidget;
207
208
}

209
void DesignDocumentController::setFormEditorView(FormEditorView *formEditorView)
210
{
211
    m_d->formEditorView = formEditorView;
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
}

QString DesignDocumentController::displayName() const
{
    if (fileName().isEmpty())
        return tr("-New Form-");
    else
        return fileName();
}

QString DesignDocumentController::fileName() const
{
    return m_d->fileName;
}

void DesignDocumentController::setFileName(const QString &fileName)
{
    m_d->fileName = fileName;

    if (QFileInfo(fileName).exists()) {
        m_d->searchPath = QUrl::fromLocalFile(fileName);
    } else {
        m_d->searchPath = QUrl(fileName);
    }

    if (m_d->model)
        m_d->model->setFileUrl(m_d->searchPath);

    if (m_d->itemLibrary)
        m_d->itemLibrary->setResourcePath(QFileInfo(fileName).absolutePath());
    emit displayNameChanged(displayName());
}

QList<RewriterView::Error> DesignDocumentController::loadMaster(QPlainTextEdit *edit)
{
    Q_CHECK_PTR(edit);

    m_d->textEdit = edit;

    connect(edit, SIGNAL(undoAvailable(bool)),
            this, SIGNAL(undoAvailable(bool)));
    connect(edit, SIGNAL(redoAvailable(bool)),
            this, SIGNAL(redoAvailable(bool)));
    connect(edit, SIGNAL(modificationChanged(bool)),
            this, SIGNAL(dirtyStateChanged(bool)));

258
    m_d->textModifier = new BaseTextEditModifier(dynamic_cast<TextEditor::BaseTextEditor*>(m_d->textEdit.data()));
259
260
261
262
263

    m_d->componentTextModifier = 0;

    //m_d->masterModel = Model::create(m_d->textModifier, m_d->searchPath, errors);

264
    m_d->masterModel = Model::create("Qt/Rectangle", 4, 6);
265
266
    m_d->masterModel->setFileUrl(m_d->searchPath);

267
    m_d->subComponentModel = Model::create("Qt/Rectangle", 4, 6);
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
    m_d->subComponentModel->setFileUrl(m_d->searchPath);

    m_d->subComponentManager = new SubComponentManager(m_d->masterModel->metaInfo(), this);
    m_d->subComponentManager->update(m_d->searchPath, m_d->textModifier->text().toUtf8());

    m_d->rewriterView = new RewriterView(RewriterView::Amend, m_d->masterModel.data());
    m_d->rewriterView->setTextModifier( m_d->textModifier);
    connect(m_d->rewriterView.data(), SIGNAL(errorsChanged(const QList<RewriterView::Error> &)),
            this, SIGNAL(qmlErrorsChanged(const QList<RewriterView::Error> &)));

    m_d->masterModel->attachView(m_d->rewriterView.data());
    m_d->model = m_d->masterModel;

#if defined(VIEWLOGGER)
    m_d->viewLogger = new Internal::ViewLogger(m_d->model.data());
    m_d->masterModel->attachView(m_d->viewLogger.data());
#endif

286
    loadCurrentModel();
287

288
    return m_d->rewriterView->errors();
289
290
291
292
293
294
295
296
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
327
328
329
330
331
332
333
334
335
336
}

void DesignDocumentController::changeCurrentModelTo(const ModelNode &componentNode)
{
    Q_ASSERT(m_d->masterModel);
    QWeakPointer<Model> oldModel = m_d->model;
    Q_ASSERT(oldModel.data());

    if (m_d->model == m_d->subComponentModel) {
        //change back to master model
        m_d->model->detachView(m_d->rewriterView.data());
        m_d->rewriterView->setTextModifier(m_d->textModifier);
        m_d->model = m_d->masterModel;
        m_d->model->attachView(m_d->rewriterView.data());
    }
    if (!componentNode.isRootNode()) {
        Q_ASSERT(m_d->model == m_d->masterModel);
        Q_ASSERT(componentNode.isValid());
        //change to subcomponent model
        ModelNode rootModelNode = componentNode.view()->rootModelNode();
        Q_ASSERT(rootModelNode.isValid());
        if (m_d->componentTextModifier)
            delete m_d->componentTextModifier;

        int componentStartOffset = m_d->rewriterView->firstDefinitionInsideOffset(componentNode);
        int componentEndOffset = componentStartOffset + m_d->rewriterView->firstDefinitionInsideLength(componentNode);
        int rootStartOffset = m_d->rewriterView->nodeOffset(rootModelNode);

        m_d->componentTextModifier = new ComponentTextModifier (m_d->textModifier, componentStartOffset, componentEndOffset, rootStartOffset);


        m_d->model->detachView(m_d->rewriterView.data());

        m_d->rewriterView->setTextModifier(m_d->componentTextModifier);

        m_d->subComponentModel->attachView(m_d->rewriterView.data());

        Q_ASSERT(m_d->rewriterView->rootModelNode().isValid());

        m_d->model = m_d->subComponentModel;
    }

    Q_ASSERT(m_d->masterModel);
    Q_ASSERT(m_d->model);

    loadCurrentModel();
}

Kai Koehne's avatar
Kai Koehne committed
337
338
339
340
341
void DesignDocumentController::loadCurrentModel()
{
    Q_ASSERT(m_d->masterModel);
    Q_ASSERT(m_d->model);
    m_d->model->setMasterModel(m_d->masterModel.data());
342
343
344
345

    m_d->model->attachView(m_d->navigator.data());
    m_d->itemLibrary->setMetaInfo(m_d->model->metaInfo());

346
347
348
    if (!m_d->componentAction) {
        m_d->componentAction = new ComponentAction(m_d->formEditorView->widget());
        m_d->componentAction->setModel(m_d->model.data());
349
        connect(m_d->componentAction.data(), SIGNAL(currentComponentChanged(ModelNode)), SLOT(changeCurrentModelTo(ModelNode)));
350
        m_d->formEditorView->widget()->toolBox()->addAction(m_d->componentAction.data());
351
    }
352
353
    // Disable switching between in file components for the time being
    m_d->componentAction->setVisible(false);
354
355

    connect(m_d->itemLibrary.data(), SIGNAL(itemActivated(const QString&)), m_d->formEditorView.data(), SLOT(activateItemCreator(const QString&)));
356
357
358
359
360
361
362
363
364
365

    m_d->model->attachView(m_d->formEditorView.data());


    if (!m_d->textEdit->parent()) // hack to prevent changing owner of external text edit
        m_d->stackedWidget->addWidget(m_d->textEdit.data());

    // Will call setCurrentState (formEditorView etc has to be constructed first)
    m_d->statesEditorWidget->setup(m_d->model.data());

366
367
    m_d->allPropertiesBox->setModel(m_d->model.data());

368
369
    m_d->documentLoaded = true;
    Q_ASSERT(m_d->masterModel);
Kai Koehne's avatar
Kai Koehne committed
370
}
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392

QList<RewriterView::Error> DesignDocumentController::loadMaster(const QByteArray &qml)
{
    QPlainTextEdit *textEdit = new QPlainTextEdit;
    textEdit->setReadOnly(true);
    textEdit->setPlainText(QString(qml));
    return loadMaster(textEdit);
}

void DesignDocumentController::saveAs(QWidget *parent)
{
    QFileInfo oldFileInfo(m_d->fileName);
    XUIFileDialog::runSaveFileDialog(oldFileInfo.path(), parent, this, SLOT(doRealSaveAs(QString)));
}

void DesignDocumentController::doRealSaveAs(const QString &fileName)
{
    if (fileName.isNull())
        return;

    QFileInfo fileInfo(fileName);
    if (fileInfo.exists() && !fileInfo.isWritable()) {
393
        QMessageBox msgBox(centralWidget());
394
395
396
397
398
        msgBox.setIcon(QMessageBox::Warning);
        msgBox.setText(tr("Cannot save to file \"%1\": permission denied.").arg(fileInfo.baseName()));
        msgBox.exec();
        return;
    } else if (!fileInfo.exists() && !fileInfo.dir().exists()) {
399
        QMessageBox msgBox(centralWidget());
400
401
402
403
404
405
406
407
408
        msgBox.setIcon(QMessageBox::Warning);
        msgBox.setText(tr("Parent folder \"%1\" for file \"%2\" does not exist.")
                       .arg(fileInfo.dir().dirName())
                       .arg(fileInfo.baseName()));
        msgBox.exec();
        return;
    }

    setFileName(fileName);
409
    save(centralWidget());
410
411
412
413
414
415
416
417
418
419
420
421
}

bool DesignDocumentController::isDirty() const
{
    if (m_d->textEdit)
        return m_d->textEdit->document()->isModified();
    else
        return false;
}

bool DesignDocumentController::isUndoAvailable() const
{
422
423
424
425

    if (m_d->textEdit)
        return m_d->textEdit->document()->isUndoAvailable();
    return false;
426
427
428
429
}

bool DesignDocumentController::isRedoAvailable() const
{
430
431
432
    if (m_d->textEdit)
        return m_d->textEdit->document()->isRedoAvailable();
    return false;
433
434
435
436
437
438
439
440
441
442
443
444
445
446
}

void DesignDocumentController::close()
{
    m_d->documentLoaded = false;
    emit designDocumentClosed();
}

void DesignDocumentController::deleteSelected()
{
    if (!m_d->model)
        return;

    if (m_d->formEditorView) {
447
        RewriterTransaction transaction(m_d->formEditorView.data());
448
449
450
451
452
453
454
455
456
457
        QList<ModelNode> toDelete = m_d->formEditorView->selectedModelNodes();
        foreach (ModelNode node, toDelete) {
            if (node.isValid() && !node.isRootNode() && QmlObjectNode(node).isValid())
                QmlObjectNode(node).destroy();
        }
    }
}

void DesignDocumentController::copySelected()
{
458
    QScopedPointer<Model> model(Model::create("import Qt 4.7; Qt/Rectangle"));
459
460
    model->setMetaInfo(m_d->model->metaInfo());

461
462
463
464
465
466
467
468
469
470
471
472
473
474
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
    Q_ASSERT(model);

    DesignDocumentControllerView view;

    m_d->model->attachView(&view);

    if (view.selectedModelNodes().isEmpty())
        return;

    QList<ModelNode> selectedNodes(view.selectedModelNodes());

    foreach (const ModelNode &node, selectedNodes) {
        foreach (const ModelNode &node2, selectedNodes) {
            if (node.isAncestorOf(node2))
                selectedNodes.removeAll(node2);
        }
    }

    if (selectedNodes.count() == 1) {
        ModelNode selectedNode(selectedNodes.first());

        if (!selectedNode.isValid())
            return;

        m_d->model->detachView(&view);

        model->attachView(&view);
        view.replaceModel(selectedNode);

        Q_ASSERT(view.rootModelNode().isValid());
        Q_ASSERT(view.rootModelNode().type() != "empty");

        view.toClipboard();
    } else { //multi items selected
        m_d->model->detachView(&view);
        model->attachView(&view);

        foreach (ModelNode node, view.rootModelNode().allDirectSubModelNodes()) {
            node.destroy();
        }
501
        view.changeRootNodeType("Qt/Rectangle", 4, 6);
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
        view.rootModelNode().setId("designer__Selection");

        foreach (const ModelNode &selectedNode, selectedNodes) {
            ModelNode newNode(view.insertModel(selectedNode));
            view.rootModelNode().nodeListProperty("data").reparentHere(newNode);
        }

        view.toClipboard();
    }
}

void DesignDocumentController::cutSelected()
{
    copySelected();
    deleteSelected();
}

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
static void scatterItem(ModelNode pastedNode, const ModelNode targetNode, int offset = -2000)
{

    bool scatter = false;
    foreach (const ModelNode &childNode, targetNode.allDirectSubModelNodes()) {
        if ((childNode.variantProperty("x").value() == pastedNode.variantProperty("x").value()) &&
            (childNode.variantProperty("y").value() == pastedNode.variantProperty("y").value()))
            scatter = true;
    }
    if (!scatter)
        return;

    if (offset == -2000) {
        double x = pastedNode.variantProperty("x").value().toDouble();
        double y = pastedNode.variantProperty("y").value().toDouble();
        double targetWidth = 20;
        double targetHeight = 20;
        x = x + double(qrand()) / RAND_MAX * targetWidth - targetWidth / 2;
        y = y + double(qrand()) / RAND_MAX * targetHeight - targetHeight / 2;
        pastedNode.variantProperty("x") = int(x);
        pastedNode.variantProperty("y") = int(y);
    } else {
        double x = pastedNode.variantProperty("x").value().toDouble();
        double y = pastedNode.variantProperty("y").value().toDouble();
        x = x + offset;
        y = y + offset;
        pastedNode.variantProperty("x") = int(x);
        pastedNode.variantProperty("y") = int(y);
    }
}

550
551
552
void DesignDocumentController::paste()
{
    QScopedPointer<Model> model(Model::create("empty"));
553
554
    model->setMetaInfo(m_d->model->metaInfo());
    model->setFileUrl(m_d->model->fileUrl());
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
    Q_ASSERT(model);

    if (!m_d->model)
        return;

    DesignDocumentControllerView view;
    model->attachView(&view);

    view.fromClipboard();

    ModelNode rootNode(view.rootModelNode());

    if (rootNode.type() == "empty")
        return;

    if (rootNode.id() == "designer__Selection") {
        QList<ModelNode> selectedNodes = rootNode.allDirectSubModelNodes();
        model->detachView(&view);
        m_d->model->attachView(&view);

575
        ModelNode targetNode;
576
577

        if (!view.selectedModelNodes().isEmpty())
578
579
            targetNode = view.selectedModelNodes().first();

580
581
        //In case we copy and paste a selection we paste in the parent item
        if ((view.selectedModelNodes().count() == selectedNodes.count()) && targetNode.isValid() && targetNode.parentProperty().isValid()) {
582
            targetNode = targetNode.parentProperty().parentModelNode();
583
        }
584

585
586
587
        if (!targetNode.isValid())
            targetNode = view.rootModelNode();

588
589
590
591
592
593
        foreach (const ModelNode &node, selectedNodes) {
            foreach (const ModelNode &node2, selectedNodes) {
                if (node.isAncestorOf(node2))
                    selectedNodes.removeAll(node2);
            }
        }
594
595
596

        QList<ModelNode> pastedNodeList;

597
598
        RewriterTransaction transaction(m_d->formEditorView.data());

599
600
        int offset = double(qrand()) / RAND_MAX * 20 - 10;

601
        foreach (const ModelNode &node, selectedNodes) {
602
            QString defaultProperty(targetNode.metaInfo().defaultProperty());
603
            ModelNode pastedNode(view.insertModel(node));
604
            pastedNodeList.append(pastedNode);
605
            scatterItem(pastedNode, targetNode, offset);
606
            targetNode.nodeListProperty(defaultProperty).reparentHere(pastedNode);
607
        }
608
609

        view.setSelectedModelNodes(pastedNodeList);
610
    } else {
611
612
        RewriterTransaction transaction(m_d->formEditorView.data());

613
614
615
        model->detachView(&view);
        m_d->model->attachView(&view);
        ModelNode pastedNode(view.insertModel(rootNode));
616
        ModelNode targetNode;
617
618

        if (!view.selectedModelNodes().isEmpty())
619
620
621
622
623
            targetNode = view.selectedModelNodes().first();

        if (!targetNode.isValid())
            targetNode = view.rootModelNode();

624
625
626
627
628
        if (targetNode.parentProperty().isValid() &&
            (pastedNode.simplifiedTypeName() == targetNode.simplifiedTypeName()) &&
            (pastedNode.variantProperty("width").value() == targetNode.variantProperty("width").value()) &&
            (pastedNode.variantProperty("height").value() == targetNode.variantProperty("height").value()))

629
630
631
            targetNode = targetNode.parentProperty().parentModelNode();

        QString defaultProperty(targetNode.metaInfo().defaultProperty());
632
633

        scatterItem(pastedNode, targetNode);
634
        targetNode.nodeListProperty(defaultProperty).reparentHere(pastedNode);
635

Marco Bubke's avatar
Marco Bubke committed
636
        view.setSelectedModelNodes(QList<ModelNode>() << pastedNode);
637
638
639
    }
}

640
641
642
643
644
645
646
647
648
649
650
651
652
653
void DesignDocumentController::selectAll()
{
    if (!m_d->model)
        return;

    DesignDocumentControllerView view;
    m_d->model->attachView(&view);


    QList<ModelNode> allNodesExceptRootNode(view.allModelNodes());
    allNodesExceptRootNode.removeOne(view.rootModelNode());
    view.setSelectedModelNodes(allNodesExceptRootNode);
}

654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
void DesignDocumentController::showError(const QString &message, QWidget *parent) const
{
    if (!parent)
        parent = m_d->stackedWidget.data();

    QMessageBox msgBox(parent);
    msgBox.setWindowFlags(Qt::Sheet | Qt::MSWindowsFixedSizeDialogHint);
    msgBox.setWindowTitle("Invalid qml");
    msgBox.setText(message);
    msgBox.setIcon(QMessageBox::Warning);
    msgBox.exec();
}

RewriterView *DesignDocumentController::rewriterView() const
{
    return m_d->rewriterView.data();
}

void DesignDocumentController::undo()
{
Thomas Hartmann's avatar
Thomas Hartmann committed
674
675
    if (m_d->rewriterView && !m_d->rewriterView->modificationGroupActive())
        m_d->textEdit->undo();
676
677
678
679
}

void DesignDocumentController::redo()
{
Thomas Hartmann's avatar
Thomas Hartmann committed
680
681
    if (m_d->rewriterView && !m_d->rewriterView->modificationGroupActive())
        m_d->textEdit->redo();
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
}

#ifdef ENABLE_TEXT_VIEW
void DesignDocumentController::showText()
{
    m_d->stackedWidget->setCurrentWidget(m_d->textEdit.data());
}
#endif // ENABLE_TEXT_VIEW

#ifdef ENABLE_TEXT_VIEW
void DesignDocumentController::showForm()
{
    m_d->stackedWidget->setCurrentWidget(m_d->formEditorView->widget());
}
#endif // ENABLE_TEXT_VIEW

698
bool DesignDocumentController::save(QWidget *parent)
699
{
700
701
702
703
704
705
706
707
708
709
710
    //    qDebug() << "Saving document to file \"" << m_d->fileName << "\"...";
    //
    if (m_d->fileName.isEmpty()) {
        saveAs(parent);
        return true;
    }
    QFile file(m_d->fileName);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        showError(tr("Cannot write file: \"%1\".").arg(m_d->fileName), parent);
        return false;
    }
711

712
713
714
715
716
    QString errorMessage;
    bool result = save(&file, &errorMessage);
    if (!result)
        showError(errorMessage, parent);
    return result;
717
718
719
720
721
722
723
724
725
}

bool DesignDocumentController::save(QIODevice *device, QString * /*errorMessage*/)
{
    if (m_d->model) {
        QByteArray data = m_d->textEdit->toPlainText().toLatin1();
        device->write(data);
        m_d->textEdit->setPlainText(data); // clear undo/redo history
    }
726
    return false;
727
728
}

729

730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
QString DesignDocumentController::contextHelpId() const
{
    DesignDocumentControllerView view;
    m_d->model->attachView(&view);

    QList<ModelNode> nodes = view.selectedModelNodes();
    QString helpId;
    if (!nodes.isEmpty()) {
        helpId = nodes.first().type();
        helpId.replace("Qt/", "QML.");
    }

    return helpId;
}

745
} // namespace QmlDesigner