aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/Ui/qsui/dockwidgetlist.cpp
blob: 66f72df618f1829c5d54f47c0a1681696b93ad7f (plain) (blame)
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
#include <QDockWidget>
#include <QMenu>
#include <QAction>
#include <QDebug>
#include <qmmpui/general.h>
#include <qmmpui/uihelper.h>
#include "dockwidgetlist.h"

DockWidgetList::DockWidgetList(QMainWindow *parent) : QObject(parent), m_mw(parent)
{
    connect(UiHelper::instance(), SIGNAL(widgetAdded(QString)), SLOT(onWidgetAdded(QString)));
    connect(UiHelper::instance(), SIGNAL(widgetRemoved(QString)), SLOT(onWidgetRemoved(QString)));
    connect(UiHelper::instance(), SIGNAL(widgetUpdated(QString)), SLOT(onWidgetUpdated(QString)));

    for(const QString &id : General::enabledWidgets())
    {
        WidgetDescription desc = General::widgetDescription(id);
        QDockWidget *dockWidget = new QDockWidget(desc.name, m_mw);
        dockWidget->setObjectName(id);
        dockWidget->setAllowedAreas(desc.allowedAreas);
        m_mw->addDockWidget(Qt::LeftDockWidgetArea, dockWidget, Qt::Vertical);
        connect(dockWidget->toggleViewAction(), SIGNAL(toggled(bool)), SLOT(onViewActionToggled(bool)));
        m_dockWidgetList << dockWidget;
    }
}

void DockWidgetList::registerMenu(QMenu *menu, QAction *before)
{
    m_menu = menu;
    m_beforeAction = before;

    for(QDockWidget *dock : qAsConst(m_dockWidgetList))
        menu->insertAction(m_beforeAction, dock->toggleViewAction());
}

void DockWidgetList::onViewActionToggled(bool checked)
{
    if(!sender() || !sender()->parent())
        return;

    QString id = sender()->parent()->objectName();
    QDockWidget *dockWidget = qobject_cast<QDockWidget *>(sender()->parent());
    if(!dockWidget)
        return;

    if(checked)
    {
        QWidget *w = General::createWidget(id, m_mw);
        if(w)
        {
            dockWidget->setWidget(w);
            w->show();
        }
    }
    else if(dockWidget->widget())
    {
        dockWidget->widget()->deleteLater();
    }
}

void DockWidgetList::onWidgetAdded(const QString &id)
{
    for(QDockWidget *dockWidget : m_dockWidgetList)
    {
        if(dockWidget->objectName() == id)
            return;
    }

    WidgetDescription desc = General::widgetDescription(id);
    QDockWidget *dockWidget = new QDockWidget(desc.name, m_mw);
    dockWidget->setObjectName(id);
    dockWidget->setAllowedAreas(desc.allowedAreas);
    if(m_menu && m_beforeAction)
        m_menu->insertAction(m_beforeAction, dockWidget->toggleViewAction());
    m_mw->addDockWidget(Qt::LeftDockWidgetArea, dockWidget, Qt::Vertical);
    connect(dockWidget->toggleViewAction(), SIGNAL(toggled(bool)), SLOT(onViewActionToggled(bool)));
    m_dockWidgetList << dockWidget;

    QWidget *w = General::createWidget(id, m_mw);
    dockWidget->setWidget(w);
    w->show();
}

void DockWidgetList::onWidgetRemoved(const QString &id)
{
    for(QDockWidget *dockWidget : m_dockWidgetList)
    {
        if(dockWidget->objectName() == id)
        {
            m_dockWidgetList.removeAll(dockWidget);
            if(dockWidget->widget())
                dockWidget->widget()->deleteLater();
            dockWidget->deleteLater();
        }
    }
}

void DockWidgetList::onWidgetUpdated(const QString &id)
{
    for(QDockWidget *dockWidget : m_dockWidgetList)
    {
        if(dockWidget->objectName() == id && dockWidget->widget())
        {
            dockWidget->widget()->deleteLater();
            QWidget *w = General::createWidget(id, m_mw);
            if(w)
            {
                dockWidget->setWidget(w);
                w->show();
            }
            break;
        }
    }
}