summaryrefslogtreecommitdiff
path: root/src/plugins/debugger/debuggermainwindow.cpp
blob: 8b7d284de9f04e92241e0cae6e2431cd5923d9a4 (plain)
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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
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
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
423
424
425
426
427
428
429
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
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
545
546
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/****************************************************************************
**
** Copyright (C) 2014 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://www.qt.io/licensing.  For further information
** use the contact form at http://www.qt.io/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 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** 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 "debuggermainwindow.h"
#include "debuggercore.h"
#include "debuggerrunconfigurationaspect.h"

#include <utils/algorithm.h>
#include <utils/appmainwindow.h>
#include <utils/styledbar.h>
#include <utils/qtcassert.h>

#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/imode.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/findplaceholder.h>
#include <coreplugin/icore.h>
#include <coreplugin/navigationwidget.h>
#include <coreplugin/outputpane.h>
#include <coreplugin/rightpane.h>

#include <projectexplorer/project.h>
#include <projectexplorer/runconfiguration.h>
#include <projectexplorer/session.h>
#include <projectexplorer/target.h>

#include <QDebug>

#include <QDockWidget>
#include <QVBoxLayout>
#include <QToolButton>

using namespace Core;
using namespace ProjectExplorer;
using namespace Debugger::Constants;

namespace Debugger {
namespace Internal {

class DockWidgetEventFilter : public QObject
{
public:
    DockWidgetEventFilter(DebuggerMainWindowPrivate *mw) : m_mw(mw) {}
private:
    virtual bool eventFilter(QObject *obj, QEvent *event);
    DebuggerMainWindowPrivate *m_mw;
};

class DebuggerMainWindowPrivate : public QObject
{
    Q_OBJECT

public:
    explicit DebuggerMainWindowPrivate(DebuggerMainWindow *mainWindow);

    void activateQmlCppLayout();
    void activateCppLayout();
    void createViewsMenuItems();
    bool isQmlCppActive() const;
    bool isQmlActive() const;
    void setSimpleDockWidgetArrangement();
    // Debuggable languages are registered with this function.
    void addLanguage(DebuggerLanguage language, const Core::Context &context);

    QDockWidget *dockWidget(const QString &objectName) const
        { return q->findChild<QDockWidget *>(objectName); }

public slots:
    void resetDebuggerLayout();
    void updateUiForProject(ProjectExplorer::Project *project);
    void updateUiForTarget(ProjectExplorer::Target *target);
    void updateUiForRunConfiguration(ProjectExplorer::RunConfiguration *rc);
    void updateUiForCurrentRunConfiguration();
    void updateActiveLanguages();
    void updateDockWidgetSettings();
    void openMemoryEditor() { Internal::openMemoryEditor(); }

public:
    DebuggerMainWindow *q;

    QHash<QString, QVariant> m_dockWidgetActiveStateCpp;
    QHash<QString, QVariant> m_dockWidgetActiveStateQmlCpp;
    DockWidgetEventFilter m_resizeEventFilter;

    QMap<DebuggerLanguage, QWidget *> m_toolBars;

    DebuggerLanguages m_supportedLanguages;

    QWidget *m_debugToolBar;
    QToolButton *m_viewButton;
    QHBoxLayout *m_debugToolBarLayout;

    QHash<DebuggerLanguage, Context> m_contextsForLanguage;

    bool m_inDebugMode;
    bool m_changingUI;

    DebuggerLanguages m_previousDebugLanguages;
    DebuggerLanguages m_activeDebugLanguages;
    DebuggerLanguages m_engineDebugLanguages;

    ActionContainer *m_viewsMenu;

    Project *m_previousProject;
    Target *m_previousTarget;
    RunConfiguration *m_previousRunConfiguration;
};

DebuggerMainWindowPrivate::DebuggerMainWindowPrivate(DebuggerMainWindow *mw)
    : q(mw)
    , m_resizeEventFilter(this)
    , m_supportedLanguages(AnyLanguage)
    , m_debugToolBar(new QWidget)
    , m_debugToolBarLayout(new QHBoxLayout(m_debugToolBar))
    , m_inDebugMode(false)
    , m_changingUI(false)
    , m_previousDebugLanguages(AnyLanguage)
    , m_activeDebugLanguages(AnyLanguage)
    , m_engineDebugLanguages(AnyLanguage)
    , m_viewsMenu(0)
    , m_previousProject(0)
    , m_previousTarget(0)
    , m_previousRunConfiguration(0)
{
    m_debugToolBarLayout->setMargin(0);
    m_debugToolBarLayout->setSpacing(0);
    createViewsMenuItems();
    addLanguage(AnyLanguage, Context());
    addLanguage(CppLanguage, Context(C_CPPDEBUGGER));
    addLanguage(QmlLanguage, Context(C_QMLDEBUGGER));
}

void DebuggerMainWindowPrivate::updateUiForProject(Project *project)
{
    if (m_previousProject) {
        disconnect(m_previousProject,
            SIGNAL(activeTargetChanged(ProjectExplorer::Target*)),
            this, SLOT(updateUiForTarget(ProjectExplorer::Target*)));
    }
    m_previousProject = project;
    if (!project) {
        updateUiForTarget(0);
        return;
    }
    connect(project, SIGNAL(activeTargetChanged(ProjectExplorer::Target*)),
        SLOT(updateUiForTarget(ProjectExplorer::Target*)));
    updateUiForTarget(project->activeTarget());
}

void DebuggerMainWindowPrivate::updateUiForTarget(Target *target)
{
    if (m_previousTarget) {
         disconnect(m_previousTarget,
            SIGNAL(activeRunConfigurationChanged(ProjectExplorer::RunConfiguration*)),
            this, SLOT(updateUiForRunConfiguration(ProjectExplorer::RunConfiguration*)));
    }

    m_previousTarget = target;

    if (!target) {
        updateUiForRunConfiguration(0);
        return;
    }

    connect(target,
        SIGNAL(activeRunConfigurationChanged(ProjectExplorer::RunConfiguration*)),
        SLOT(updateUiForRunConfiguration(ProjectExplorer::RunConfiguration*)));
    updateUiForRunConfiguration(target->activeRunConfiguration());
}

// updates default debug language settings per run config.
void DebuggerMainWindowPrivate::updateUiForRunConfiguration(RunConfiguration *rc)
{
    if (m_previousRunConfiguration)
        disconnect(m_previousRunConfiguration, &RunConfiguration::requestRunActionsUpdate,
                   this, &DebuggerMainWindowPrivate::updateUiForCurrentRunConfiguration);
    m_previousRunConfiguration = rc;
    updateUiForCurrentRunConfiguration();
    if (!rc)
        return;

    connect(m_previousRunConfiguration, &RunConfiguration::requestRunActionsUpdate,
            this, &DebuggerMainWindowPrivate::updateUiForCurrentRunConfiguration);
}

void DebuggerMainWindowPrivate::updateUiForCurrentRunConfiguration()
{
    updateActiveLanguages();
}

void DebuggerMainWindowPrivate::updateActiveLanguages()
{
    DebuggerLanguages newLanguages = AnyLanguage;

    if (m_engineDebugLanguages != AnyLanguage) {
        newLanguages = m_engineDebugLanguages;
    } else if (m_previousRunConfiguration) {
        if (m_previousRunConfiguration->extraAspect<Debugger::DebuggerRunConfigurationAspect>()->useCppDebugger())
            newLanguages |= CppLanguage;
        if (m_previousRunConfiguration->extraAspect<Debugger::DebuggerRunConfigurationAspect>()->useQmlDebugger())
            newLanguages |= QmlLanguage;
    }

    if (newLanguages != m_activeDebugLanguages)
        m_activeDebugLanguages = newLanguages;

    if (m_changingUI || !m_inDebugMode)
        return;

    m_changingUI = true;

    if (isQmlActive())
        activateQmlCppLayout();
    else
        activateCppLayout();

    m_previousDebugLanguages = m_activeDebugLanguages;

    m_changingUI = false;
}

} // namespace Internal

using namespace Internal;

DebuggerMainWindow::DebuggerMainWindow()
{
    d = new DebuggerMainWindowPrivate(this);
}

DebuggerMainWindow::~DebuggerMainWindow()
{
    delete d;
}

DebuggerLanguages DebuggerMainWindow::activeDebugLanguages() const
{
    return d->m_activeDebugLanguages;
}

void DebuggerMainWindow::setEngineDebugLanguages(DebuggerLanguages languages)
{
    if (d->m_engineDebugLanguages == languages)
        return;

    d->m_engineDebugLanguages = languages;
    d->updateActiveLanguages();
}

void DebuggerMainWindow::onModeChanged(IMode *mode)
{
    d->m_inDebugMode = (mode && mode->id() == Constants::MODE_DEBUG);
    setDockActionsVisible(d->m_inDebugMode);

    // Hide all the debugger windows if mode is different.
    if (d->m_inDebugMode) {
        readSettings();
        d->updateActiveLanguages();
    } else {
        // Hide dock widgets manually in case they are floating.
        foreach (QDockWidget *dockWidget, dockWidgets()) {
            if (dockWidget->isFloating())
                dockWidget->hide();
        }
    }
}

void DebuggerMainWindowPrivate::createViewsMenuItems()
{
    Context debugcontext(Constants::C_DEBUGMODE);
    m_viewsMenu = Core::ActionManager::actionContainer(Id(Core::Constants::M_WINDOW_VIEWS));
    QTC_ASSERT(m_viewsMenu, return);

    QAction *openMemoryEditorAction = new QAction(this);
    openMemoryEditorAction->setText(tr("Memory..."));
    connect(openMemoryEditorAction, SIGNAL(triggered()),
       SLOT(openMemoryEditor()));

    // Add menu items
    Command *cmd = 0;
    cmd = Core::ActionManager::registerAction(openMemoryEditorAction,
        "Debugger.Views.OpenMemoryEditor", debugcontext);
    cmd->setAttribute(Command::CA_Hide);
    m_viewsMenu->addAction(cmd, Core::Constants::G_DEFAULT_THREE);

    cmd = Core::ActionManager::registerAction(q->menuSeparator1(),
        "Debugger.Views.Separator1", debugcontext);
    cmd->setAttribute(Command::CA_Hide);
    m_viewsMenu->addAction(cmd, Core::Constants::G_DEFAULT_THREE);
    cmd = Core::ActionManager::registerAction(q->autoHideTitleBarsAction(),
        "Debugger.Views.AutoHideTitleBars", debugcontext);
    cmd->setAttribute(Command::CA_Hide);
    m_viewsMenu->addAction(cmd, Core::Constants::G_DEFAULT_THREE);
    cmd = Core::ActionManager::registerAction(q->menuSeparator2(),
        "Debugger.Views.Separator2", debugcontext);
    cmd->setAttribute(Command::CA_Hide);
    m_viewsMenu->addAction(cmd, Core::Constants::G_DEFAULT_THREE);
}

void DebuggerMainWindowPrivate::addLanguage(DebuggerLanguage languageId,
                                            const Context &context)
{
    m_supportedLanguages = m_supportedLanguages | languageId;
    m_toolBars.insert(languageId, 0);
    m_contextsForLanguage.insert(languageId, context);
}

void DebuggerMainWindowPrivate::activateQmlCppLayout()
{
    Context qmlCppContext = m_contextsForLanguage.value(QmlLanguage);
    qmlCppContext.add(m_contextsForLanguage.value(CppLanguage));
    if (m_toolBars.value(QmlLanguage)) {
        m_debugToolBarLayout->insertWidget(1, m_toolBars.value(QmlLanguage));
        m_toolBars.value(QmlLanguage)->show();
    }

    if (m_previousDebugLanguages & QmlLanguage) {
        m_dockWidgetActiveStateQmlCpp = q->saveSettings();
        ICore::removeAdditionalContext(qmlCppContext);
    } else if (m_previousDebugLanguages & CppLanguage) {
        m_dockWidgetActiveStateCpp = q->saveSettings();
        ICore::removeAdditionalContext(m_contextsForLanguage.value(CppLanguage));
    }

    q->restoreSettings(m_dockWidgetActiveStateQmlCpp);
    ICore::addAdditionalContext(qmlCppContext);
}

void DebuggerMainWindowPrivate::activateCppLayout()
{
    Context qmlCppContext = m_contextsForLanguage.value(QmlLanguage);
    qmlCppContext.add(m_contextsForLanguage.value(CppLanguage));
    if (m_toolBars.value(QmlLanguage)) {
        m_toolBars.value(QmlLanguage)->hide();
        m_debugToolBarLayout->removeWidget(m_toolBars.value(QmlLanguage));
    }

    if (m_previousDebugLanguages & QmlLanguage) {
        m_dockWidgetActiveStateQmlCpp = q->saveSettings();
        ICore::removeAdditionalContext(qmlCppContext);
    } else if (m_previousDebugLanguages & CppLanguage) {
        m_dockWidgetActiveStateCpp = q->saveSettings();
        ICore::removeAdditionalContext(m_contextsForLanguage.value(CppLanguage));
    }

    q->restoreSettings(m_dockWidgetActiveStateCpp);

    const Context &cppContext = m_contextsForLanguage.value(CppLanguage);
    ICore::addAdditionalContext(cppContext);
}

void DebuggerMainWindow::setToolBar(DebuggerLanguage language, QWidget *widget)
{
    Q_ASSERT(d->m_toolBars.contains(language));
    d->m_toolBars[language] = widget;
    if (language == CppLanguage)
        d->m_debugToolBarLayout->addWidget(widget);

    //Add widget at the end
    if (language == AnyLanguage)
        d->m_debugToolBarLayout->insertWidget(-1, widget, 10);
}

/*!
    Keep track of dock widgets so they can be shown/hidden for different languages
*/
QDockWidget *DebuggerMainWindow::createDockWidget(const DebuggerLanguage &language,
    QWidget *widget)
{
    QDockWidget *dockWidget = addDockForWidget(widget);
    dockWidget->setObjectName(widget->objectName());
    addDockWidget(Qt::BottomDockWidgetArea, dockWidget);

    if (!(d->m_activeDebugLanguages & language))
        dockWidget->hide();

    Context globalContext(Core::Constants::C_GLOBAL);

    QAction *toggleViewAction = dockWidget->toggleViewAction();
    Command *cmd = Core::ActionManager::registerAction(toggleViewAction,
             Core::Id("Debugger.").withSuffix(widget->objectName()), globalContext);
    cmd->setAttribute(Command::CA_Hide);

    dockWidget->installEventFilter(&d->m_resizeEventFilter);

    connect(dockWidget->toggleViewAction(), SIGNAL(triggered(bool)),
        d, SLOT(updateDockWidgetSettings()));
    connect(dockWidget, SIGNAL(topLevelChanged(bool)),
        d, SLOT(updateDockWidgetSettings()));
    connect(dockWidget, SIGNAL(dockLocationChanged(Qt::DockWidgetArea)),
        d, SLOT(updateDockWidgetSettings()));

    return dockWidget;
}

void DebuggerMainWindow::addStagedMenuEntries()
{
    addDockActionsToMenu(d->m_viewsMenu->menu());
}

QWidget *DebuggerMainWindow::createContents(IMode *mode)
{
    connect(SessionManager::instance(), SIGNAL(startupProjectChanged(ProjectExplorer::Project*)),
        d, SLOT(updateUiForProject(ProjectExplorer::Project*)));

    d->m_viewsMenu = Core::ActionManager::actionContainer(Core::Id(Core::Constants::M_WINDOW_VIEWS));
    QTC_ASSERT(d->m_viewsMenu, return 0);

    //d->m_mainWindow = new Internal::DebuggerMainWindow(this);
    setDocumentMode(true);
    setDockNestingEnabled(true);
    connect(this, SIGNAL(resetLayout()),
        d, SLOT(resetDebuggerLayout()));
    connect(autoHideTitleBarsAction(), SIGNAL(triggered()),
        d, SLOT(updateDockWidgetSettings()));

    QBoxLayout *editorHolderLayout = new QVBoxLayout;
    editorHolderLayout->setMargin(0);
    editorHolderLayout->setSpacing(0);

    QWidget *editorAndFindWidget = new QWidget;
    editorAndFindWidget->setLayout(editorHolderLayout);
    auto editorManagerPlaceHolder = new EditorManagerPlaceHolder(mode);
    editorHolderLayout->addWidget(editorManagerPlaceHolder);
    editorHolderLayout->addWidget(new FindToolBarPlaceHolder(editorAndFindWidget));

    MiniSplitter *documentAndRightPane = new MiniSplitter;
    documentAndRightPane->addWidget(editorAndFindWidget);
    documentAndRightPane->addWidget(new RightPanePlaceHolder(mode));
    documentAndRightPane->setStretchFactor(0, 1);
    documentAndRightPane->setStretchFactor(1, 0);

    d->m_viewButton = new QToolButton();
    // FIXME: Use real thing after string freeze.
    QString hackyName = QCoreApplication::translate("Core::Internal::MainWindow", "&Views");
    hackyName.replace(QLatin1Char('&'), QString());
    d->m_viewButton->setText(hackyName);

    Utils::StyledBar *debugToolBar = new Utils::StyledBar;
    debugToolBar->setProperty("topBorder", true);
    QHBoxLayout *debugToolBarLayout = new QHBoxLayout(debugToolBar);
    debugToolBarLayout->setMargin(0);
    debugToolBarLayout->setSpacing(0);
    debugToolBarLayout->addWidget(d->m_debugToolBar);
    debugToolBarLayout->addWidget(new Utils::StyledSeparator);
    debugToolBarLayout->addWidget(d->m_viewButton);

    connect(d->m_viewButton, SIGNAL(clicked()), this, SLOT(showViewsMenu()));

    QDockWidget *dock = new QDockWidget(DebuggerMainWindowPrivate::tr("Debugger Toolbar"));
    dock->setObjectName(QLatin1String("Debugger Toolbar"));
    dock->setWidget(debugToolBar);
    dock->setFeatures(QDockWidget::NoDockWidgetFeatures);
    dock->setAllowedAreas(Qt::BottomDockWidgetArea);
    dock->setTitleBarWidget(new QWidget(dock));
    dock->setProperty("managed_dockwidget", QLatin1String("true"));
    addDockWidget(Qt::BottomDockWidgetArea, dock);
    setToolBarDockWidget(dock);

    QWidget *centralWidget = new QWidget;
    setCentralWidget(centralWidget);

    QVBoxLayout *centralLayout = new QVBoxLayout(centralWidget);
    centralWidget->setLayout(centralLayout);
    centralLayout->setMargin(0);
    centralLayout->setSpacing(0);
    centralLayout->addWidget(documentAndRightPane);
    centralLayout->setStretch(0, 1);
    centralLayout->setStretch(1, 0);

    // Right-side window with editor, output etc.
    MiniSplitter *mainWindowSplitter = new MiniSplitter;
    mainWindowSplitter->addWidget(this);
    QWidget *outputPane = new OutputPanePlaceHolder(mode, mainWindowSplitter);
    outputPane->setObjectName(QLatin1String("DebuggerOutputPanePlaceHolder"));
    mainWindowSplitter->addWidget(outputPane);
    mainWindowSplitter->setStretchFactor(0, 10);
    mainWindowSplitter->setStretchFactor(1, 0);
    mainWindowSplitter->setOrientation(Qt::Vertical);

    // Navigation and right-side window.
    MiniSplitter *splitter = new MiniSplitter;
    splitter->setFocusProxy(editorManagerPlaceHolder);
    splitter->addWidget(new NavigationWidgetPlaceHolder(mode));
    splitter->addWidget(mainWindowSplitter);
    splitter->setStretchFactor(0, 0);
    splitter->setStretchFactor(1, 1);
    splitter->setObjectName(QLatin1String("DebugModeWidget"));
    return splitter;
}

void DebuggerMainWindow::writeSettings() const
{
    QSettings *settings = ICore::settings();
    settings->beginGroup(QLatin1String("DebugMode.CppMode"));
    QHashIterator<QString, QVariant> it(d->m_dockWidgetActiveStateCpp);
    while (it.hasNext()) {
        it.next();
        settings->setValue(it.key(), it.value());
    }
    settings->endGroup();

    settings->beginGroup(QLatin1String("DebugMode.CppQmlMode"));
    it = QHashIterator<QString, QVariant>(d->m_dockWidgetActiveStateQmlCpp);
    while (it.hasNext()) {
        it.next();
        settings->setValue(it.key(), it.value());
    }
    settings->endGroup();
}

void DebuggerMainWindow::showViewsMenu()
{
    QMenu menu;
    addDockActionsToMenu(&menu);
    menu.exec(d->m_viewButton->mapToGlobal(QPoint()));
}

void DebuggerMainWindow::readSettings()
{
    QSettings *settings = ICore::settings();
    d->m_dockWidgetActiveStateCpp.clear();
    d->m_dockWidgetActiveStateQmlCpp.clear();

    settings->beginGroup(QLatin1String("DebugMode.CppMode"));
    foreach (const QString &key, settings->childKeys())
        d->m_dockWidgetActiveStateCpp.insert(key, settings->value(key));
    settings->endGroup();

    settings->beginGroup(QLatin1String("DebugMode.CppQmlMode"));
    foreach (const QString &key, settings->childKeys())
        d->m_dockWidgetActiveStateQmlCpp.insert(key, settings->value(key));
    settings->endGroup();

    // Reset initial settings when there are none yet.
    if (d->m_dockWidgetActiveStateQmlCpp.isEmpty()) {
        d->m_activeDebugLanguages = DebuggerLanguage(QmlLanguage|CppLanguage);
        d->setSimpleDockWidgetArrangement();
        d->m_dockWidgetActiveStateCpp = saveSettings();
    }
    if (d->m_dockWidgetActiveStateCpp.isEmpty()) {
        d->m_activeDebugLanguages = CppLanguage;
        d->setSimpleDockWidgetArrangement();
        d->m_dockWidgetActiveStateCpp = saveSettings();
    }
    writeSettings();
}

void DebuggerMainWindowPrivate::resetDebuggerLayout()
{
    m_activeDebugLanguages = DebuggerLanguage(QmlLanguage | CppLanguage);
    setSimpleDockWidgetArrangement();
    m_dockWidgetActiveStateQmlCpp = q->saveSettings();

    m_activeDebugLanguages = CppLanguage;
    m_previousDebugLanguages = CppLanguage;
    setSimpleDockWidgetArrangement();
    // will save state in m_dockWidgetActiveStateCpp
    updateActiveLanguages();
}

void DebuggerMainWindowPrivate::updateDockWidgetSettings()
{
    if (!m_inDebugMode || m_changingUI)
        return;

    if (isQmlActive())
        m_dockWidgetActiveStateQmlCpp = q->saveSettings();
    else
        m_dockWidgetActiveStateCpp = q->saveSettings();
}

bool DebuggerMainWindowPrivate::isQmlCppActive() const
{
    return (m_activeDebugLanguages & CppLanguage)
        && (m_activeDebugLanguages & QmlLanguage);
}

bool DebuggerMainWindowPrivate::isQmlActive() const
{
    return (m_activeDebugLanguages & QmlLanguage);
}

void DebuggerMainWindowPrivate::setSimpleDockWidgetArrangement()
{
    using namespace Constants;
    QTC_ASSERT(q, return);
    q->setTrackingEnabled(false);

    QList<QDockWidget *> dockWidgets = q->dockWidgets();
    foreach (QDockWidget *dockWidget, dockWidgets) {
        dockWidget->setFloating(false);
        q->removeDockWidget(dockWidget);
    }

    foreach (QDockWidget *dockWidget, dockWidgets) {
        int area = Qt::BottomDockWidgetArea;
        QVariant p = dockWidget->property(DOCKWIDGET_DEFAULT_AREA);
        if (p.isValid())
            area = Qt::DockWidgetArea(p.toInt());
        q->addDockWidget(Qt::DockWidgetArea(area), dockWidget);
        dockWidget->hide();
    }

    QDockWidget *toolBarDock = q->toolBarDockWidget();
    QDockWidget *breakDock = dockWidget(QLatin1String(DOCKWIDGET_BREAK));
    QDockWidget *stackDock = dockWidget(QLatin1String(DOCKWIDGET_STACK));
    QDockWidget *watchDock = dockWidget(QLatin1String(DOCKWIDGET_WATCHERS));
    QDockWidget *snapshotsDock = dockWidget(QLatin1String(DOCKWIDGET_SNAPSHOTS));
    QDockWidget *threadsDock = dockWidget(QLatin1String(DOCKWIDGET_THREADS));
    QDockWidget *outputDock = dockWidget(QLatin1String(DOCKWIDGET_OUTPUT));
    QDockWidget *qmlInspectorDock = dockWidget(QLatin1String(DOCKWIDGET_QML_INSPECTOR));
    QDockWidget *modulesDock = dockWidget(QLatin1String(DOCKWIDGET_MODULES));
    QDockWidget *registerDock = dockWidget(QLatin1String(DOCKWIDGET_REGISTER));
    QDockWidget *sourceFilesDock = dockWidget(QLatin1String(DOCKWIDGET_SOURCE_FILES));

    QTC_ASSERT(breakDock, return);
    QTC_ASSERT(stackDock, return);
    QTC_ASSERT(watchDock, return);
    QTC_ASSERT(snapshotsDock, return);
    QTC_ASSERT(threadsDock, return);
    QTC_ASSERT(outputDock, return);
    QTC_ASSERT(modulesDock, return);
    QTC_ASSERT(registerDock, return);
    QTC_ASSERT(sourceFilesDock, return);

    // make sure main docks are visible so that split equally divides the space
    toolBarDock->show();
    stackDock->show();
    breakDock->show();
    watchDock->show();

    // toolBar
    // --------------------------------------------------------------------------------
    // stack,qmlinspector | breakpoints,modules,register,threads,sourceFiles,snapshots
    //
    q->splitDockWidget(toolBarDock, stackDock, Qt::Vertical);
    q->splitDockWidget(stackDock, breakDock, Qt::Horizontal);

    if (qmlInspectorDock)
        q->tabifyDockWidget(stackDock, qmlInspectorDock);

    q->tabifyDockWidget(breakDock, modulesDock);
    q->tabifyDockWidget(breakDock, registerDock);
    q->tabifyDockWidget(breakDock, threadsDock);
    q->tabifyDockWidget(breakDock, sourceFilesDock);
    q->tabifyDockWidget(breakDock, snapshotsDock);

    if (m_activeDebugLanguages.testFlag(Debugger::QmlLanguage)) {
        if (qmlInspectorDock)
            qmlInspectorDock->show();
    } else {
        // CPP only
        threadsDock->show();
        snapshotsDock->show();
    }

    q->setTrackingEnabled(true);
    q->update();
}

bool DockWidgetEventFilter::eventFilter(QObject *obj, QEvent *event)
{
    switch (event->type()) {
    case QEvent::Resize:
    case QEvent::ZOrderChange:
        m_mw->updateDockWidgetSettings();
        break;
    default:
        break;
    }
    return QObject::eventFilter(obj, event);
}

} // namespace Debugger

#include "debuggermainwindow.moc"