designdocumentcontroller.cpp 25.1 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
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
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
    QmlDesigner::ComponentTextModifier *componentTextModifier;
    QWeakPointer<SubComponentManager> subComponentManager;
    QWeakPointer<Internal::ViewLogger> viewLogger;

    QWeakPointer<QProcess> previewProcess;
    QWeakPointer<QProcess> previewWithDebugProcess;

    QString fileName;
    QUrl searchPath;
    bool documentLoaded;
    bool syncBlocked;

};

/**
  \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->itemLibrary = new ItemLibrary;
    m_d->navigator = new NavigatorView(this);
    m_d->allPropertiesBox = new AllPropertiesBox;
    m_d->statesEditorWidget = new StatesEditorWidget;
    m_d->stackedWidget = new QStackedWidget;

    m_d->documentLoaded = false;
    m_d->syncBlocked = false;
}

DesignDocumentController::~DesignDocumentController()
{
    delete m_d->formEditorView.data();

    // deleting the widgets should be safe because these are QWeakPointer's
    delete m_d->itemLibrary.data();
    delete m_d->allPropertiesBox.data();
    delete m_d->statesEditorWidget.data();
    delete m_d->stackedWidget.data();

    delete m_d->model.data();
    delete m_d->subComponentModel.data();

    delete m_d->rewriterView.data();
    // m_d->textEdit is child of stackedWidget or owned by external widget
    delete m_d->textModifier;

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

    delete m_d->previewProcess.data();
    delete m_d->previewWithDebugProcess.data();

    delete m_d;
}

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

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

QWidget *DesignDocumentController::documentWidget() const
{
    return m_d->stackedWidget.data();
}

void DesignDocumentController::togglePreview(bool visible)
{
    if (!m_d->documentLoaded)
        return;

    if (visible) {
        Q_ASSERT(!m_d->previewProcess);

        QString directory;
        if (!m_d->fileName.isEmpty()) {
            directory = QFileInfo(m_d->fileName).absoluteDir().path();
        } else {
            directory = QDir::tempPath();
        }
        m_d->previewProcess = createPreviewProcess(directory);
        connect(m_d->previewProcess.data(), SIGNAL(finished(int, QProcess::ExitStatus)),
                this, SLOT(emitPreviewVisibilityChanged()));
    } else {
        Q_ASSERT(m_d->previewProcess);

        delete m_d->previewProcess.data();
    }
}

void DesignDocumentController::toggleWithDebugPreview(bool visible)
{
    if (!m_d->documentLoaded)
        return;

    if (visible) {
        Q_ASSERT(!m_d->previewWithDebugProcess);

        QString directory;
        if (!m_d->fileName.isEmpty()) {
            directory = QFileInfo(m_d->fileName).absoluteDir().path();
        } else {
            directory = QDir::tempPath();
        }
        m_d->previewWithDebugProcess = createPreviewWithDebugProcess(directory);
        connect(m_d->previewWithDebugProcess.data(), SIGNAL(finished(int, QProcess::ExitStatus)),
                this, SLOT(emitPreviewWithDebugVisibilityChanged()));
    } else {
        Q_ASSERT(m_d->previewWithDebugProcess);

        delete m_d->previewWithDebugProcess.data();
    }
}

bool DesignDocumentController::previewVisible() const
{
    if (!m_d->documentLoaded)
        return false;

    return m_d->previewProcess;
}

bool DesignDocumentController::previewWithDebugVisible() const
{
    if (!m_d->documentLoaded)
        return false;

    return m_d->previewWithDebugProcess;
}


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

void DesignDocumentController::emitPreviewVisibilityChanged()
{
    emit previewVisibilityChanged(false);
}

void DesignDocumentController::emitPreviewWithDebugVisibilityChanged()
{
    emit previewWithDebugVisibilityChanged(false);
}

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

329
    m_d->textModifier = new BaseTextEditModifier(m_d->textEdit.data());
330
331
332
333
334

    m_d->componentTextModifier = 0;

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

335
    m_d->masterModel = Model::create("Qt/Rectangle", 4, 6);
336
337
    m_d->masterModel->setFileUrl(m_d->searchPath);

338
    m_d->subComponentModel = Model::create("Qt/Rectangle", 4, 6);
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
    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

357
    loadCurrentModel();
358

359
    return m_d->rewriterView->errors();
360
361
362
363
364
365
366
367
368
369
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
}

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
408
409
410
411
412
void DesignDocumentController::loadCurrentModel()
{
    Q_ASSERT(m_d->masterModel);
    Q_ASSERT(m_d->model);
    m_d->model->setMasterModel(m_d->masterModel.data());
413
414
415
416
417
418
419
420
421
422
423

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

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

    if (m_d->formEditorView .isNull()) {
        m_d->formEditorView = new FormEditorView(this);
        m_d->stackedWidget->addWidget(m_d->formEditorView->widget());
        ComponentAction *componentAction = new ComponentAction(m_d->formEditorView->widget());
        componentAction->setModel(m_d->model.data());
424
        m_d->formEditorView->widget()->lowerToolBox()->addAction(componentAction);
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
        connect(componentAction, SIGNAL(currentComponentChanged(ModelNode)), SLOT(changeCurrentModelTo(ModelNode))); //TODO component action
        connect(m_d->itemLibrary.data(), SIGNAL(itemActivated(const QString&)), m_d->formEditorView.data(), SLOT(activateItemCreator(const QString&)));
    }

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

    m_d->documentLoaded = true;
    Q_ASSERT(m_d->masterModel);
Kai Koehne's avatar
Kai Koehne committed
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
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
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

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()) {
        QMessageBox msgBox(documentWidget());
        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()) {
        QMessageBox msgBox(documentWidget());
        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);
    save(documentWidget());
}

bool DesignDocumentController::save(QWidget *parent)
{
//    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;
    }

    QString errorMessage;
    bool result = save(&file, &errorMessage);
    if (!result)
        showError(errorMessage, parent);
    return result;
}

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

bool DesignDocumentController::isUndoAvailable() const
{
    return m_d->textEdit->document()->isUndoAvailable();
}

bool DesignDocumentController::isRedoAvailable() const
{
    return m_d->textEdit->document()->isRedoAvailable();
}

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

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

    if (m_d->formEditorView) {
        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()
{
    QScopedPointer<Model> model(Model::create("Qt/Rectangle"));
545
546
    model->setMetaInfo(m_d->model->metaInfo());

547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
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
604
605
606
607
    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();
        }
        view.rootModelNode().changeType("Qt/Rectangle", 4, 6);
        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();
}

void DesignDocumentController::paste()
{
    QScopedPointer<Model> model(Model::create("empty"));
608
609
    model->setMetaInfo(m_d->model->metaInfo());
    model->setFileUrl(m_d->model->fileUrl());
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
    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);

630
        ModelNode targetNode;
631
632

        if (!view.selectedModelNodes().isEmpty())
633
634
635
636
637
638
639
640
            targetNode = view.selectedModelNodes().first();

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


        if (targetNode.parentProperty().isValid())
            targetNode = targetNode.parentProperty().parentModelNode();
641
642
643
644
645
646
647
648

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

        }
649
650
651

        QList<ModelNode> pastedNodeList;

652
        foreach (const ModelNode &node, selectedNodes) {
653
            QString defaultProperty(targetNode.metaInfo().defaultProperty());
654
            ModelNode pastedNode(view.insertModel(node));
655
656
            pastedNodeList.append(pastedNode);
            targetNode.nodeListProperty(defaultProperty).reparentHere(pastedNode);
657
        }
658
659

        view.setSelectedModelNodes(pastedNodeList);
660
661
662
663
    } else {
        model->detachView(&view);
        m_d->model->attachView(&view);
        ModelNode pastedNode(view.insertModel(rootNode));
664
        ModelNode targetNode;
665
666

        if (!view.selectedModelNodes().isEmpty())
667
668
669
670
671
672
673
674
675
676
            targetNode = view.selectedModelNodes().first();

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

        if (targetNode.parentProperty().isValid())
            targetNode = targetNode.parentProperty().parentModelNode();

        QString defaultProperty(targetNode.metaInfo().defaultProperty());
        targetNode.nodeListProperty(defaultProperty).reparentHere(pastedNode);
677
678
679
680
681


    }
}

682
683
684
685
686
687
688
689
690
691
692
693
694
695
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);
}

696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
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();
}

QWidget *DesignDocumentController::itemLibrary() const
{
    return m_d->itemLibrary.data();
}

QWidget *DesignDocumentController::navigator() const
{
    return m_d->navigator->widget();
}

QWidget *DesignDocumentController::allPropertiesBox() const
{
    return m_d->allPropertiesBox.data();
}

QWidget *DesignDocumentController::statesEditorWidget() const
{
    return m_d->statesEditorWidget.data();
}

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

void DesignDocumentController::undo()
{
    m_d->textEdit->undo();
}

void DesignDocumentController::redo()
{
    m_d->textEdit->redo();
}

#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

QProcess *DesignDocumentController::createPreviewProcess(const QString &dirPath)
{
    Q_ASSERT(QDir(dirPath).exists());

    QProcess *previewProcess = new QProcess(this);
    previewProcess->setWorkingDirectory(dirPath);

    QTemporaryFile *temporaryFile = new QTemporaryFile(QDir(dirPath).absoluteFilePath("bauhaus_tmp"));
    temporaryFile->setParent(previewProcess);

    temporaryFile->open();

    m_d->textModifier->save(temporaryFile);

    QStringList qmlViewerArgumentList;
//        qmlViewerArgumentList.append("-L");
//        qmlViewerArgumentList.append(dirPath);
    previewProcess->setWorkingDirectory(dirPath);
    qmlViewerArgumentList.append(temporaryFile->fileName());

    temporaryFile->close();

    previewProcess->start(QString("%1/qmlviewer").arg(QCoreApplication::applicationDirPath()), qmlViewerArgumentList);
    if (!previewProcess->waitForStarted())
        previewProcess->start("qmlviewer", qmlViewerArgumentList);
    if (!previewProcess->waitForStarted())
        QMessageBox::warning(documentWidget(), "qmlviewer not found", "qmlviewer should be in the PATH or in the same directory like the bauhaus binary.");

    return previewProcess;
}

QProcess *DesignDocumentController::createPreviewWithDebugProcess(const QString &dirPath)
{
    Q_ASSERT(QDir(dirPath).exists());

    QProcess *previewProcess = new QProcess(this);
    previewProcess->setWorkingDirectory(dirPath);

    QProcess *debugger = new QProcess(previewProcess);

    QTemporaryFile *temporaryFile = new QTemporaryFile(QDir(dirPath).absoluteFilePath("bauhaus_tmp"));
    temporaryFile->setParent(previewProcess);

    temporaryFile->open();

    m_d->textModifier->save(temporaryFile);

    QStringList environmentList = QProcess::systemEnvironment();
    environmentList.append("QML_DEBUG_SERVER_PORT=3768");
    previewProcess->setEnvironment(environmentList);

    QStringList qmlViewerArgumentList;
//        qmlViewerArgumentList.append("-L");
//        qmlViewerArgumentList.append(libraryPath);
    qmlViewerArgumentList.append(temporaryFile->fileName());

    temporaryFile->close();

    debugger->setWorkingDirectory(dirPath);
    debugger->start(QString("%1/qmldebugger").arg(QCoreApplication::applicationDirPath()));
    if (!debugger->waitForStarted())
        debugger->start("qmldebugger", qmlViewerArgumentList);
    if (!debugger->waitForStarted())
        QMessageBox::warning(documentWidget(), "qmldebugger not found", "qmldebugger should in the PATH or in the same directory like the bauhaus binary.");

    previewProcess->setWorkingDirectory(dirPath);
    previewProcess->start(QString("%1/qmlviewer").arg(QCoreApplication::applicationDirPath()), qmlViewerArgumentList);
    if (!previewProcess->waitForStarted())
        previewProcess->start("qmlviewer", qmlViewerArgumentList);
    if (!previewProcess->waitForStarted())
        QMessageBox::warning(documentWidget(), "qmlviewer not found", "qmlviewer should in the PATH or in the same directory like the bauhaus binary.");

    return previewProcess;
}

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
        return true;
    } else {
        return false;
    }
}

} // namespace QmlDesigner