aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/edit3d/backgroundcolorselection.cpp
blob: 967fe8b71a01aa51bc666e3b53c9efe6f7a115aa (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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include <QColorDialog>

#include <utils/qtcassert.h>

#include "backgroundcolorselection.h"
#include "edit3dviewconfig.h"

using namespace QmlDesigner;

void BackgroundColorSelection::showBackgroundColorSelectionWidget(QWidget *parent, const QByteArray &key,
                                                                  View3DActionCommand::Type cmdType)
{
    if (m_dialog)
        return;

    m_dialog = BackgroundColorSelection::createColorDialog(parent, key, cmdType);
    QTC_ASSERT(m_dialog, return);

    QObject::connect(m_dialog, &QWidget::destroyed, m_dialog, [&]() {
        m_dialog = nullptr;
    });
}

QColorDialog *BackgroundColorSelection::createColorDialog(QWidget *parent, const QByteArray &key,
                                                          View3DActionCommand::Type cmdType)
{
    auto dialog = new QColorDialog(parent);

    dialog->setModal(true);
    dialog->setAttribute(Qt::WA_DeleteOnClose);

    QList<QColor> oldColorConfig = Edit3DViewConfig::load(key);

    dialog->show();

    QObject::connect(dialog, &QColorDialog::currentColorChanged, dialog, [cmdType](const QColor &color) {
        Edit3DViewConfig::set(cmdType, color);
    });

    QObject::connect(dialog, &QColorDialog::colorSelected, dialog, [key](const QColor &color) {
        Edit3DViewConfig::save(key, color);
    });

    if (Edit3DViewConfig::isValid(oldColorConfig)) {
        QObject::connect(dialog, &QColorDialog::rejected, dialog, [cmdType, oldColorConfig]() {
            Edit3DViewConfig::set(cmdType, oldColorConfig);
        });
    }

    return dialog;
}