summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Common/Code/Serialize/ProjectSettingsSerializer.h
blob: acfa4712a2bf29a316ed73468922d2f669b422d6 (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
/****************************************************************************
**
** Copyright (C) 2002 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

//==============================================================================
//	Prefix
//==============================================================================
#ifndef INCLUDED_PROJECT_SETTINGS_SERIALIZER_H
#define INCLUDED_PROJECT_SETTINGS_SERIALIZER_H 1

#pragma once

//==============================================================================
//	Includes
//==============================================================================
#include "StudioProjectSettings.h"
#include "Qt3DSDMXML.h"
#include "Qt3DSDMWStrOpsImpl.h"

#include <QtWidgets/qcolordialog.h>
#include <QtCore/qsize.h>

namespace qt3dsdm {
}

class CProjectSettingsSerializer
{
    class CustomColorSerializer
    {
    public:
        void Serialize(qt3dsdm::IDOMWriter &ar)
        {
            using namespace qt3dsdm;
            ar.Att(L"count", static_cast<qt3ds::QT3DSI32>(QColorDialog::customCount()));
            QString strColor;
            for (int i = 0; i < QColorDialog::customCount(); ++i) {
                if (i > 0)
                    strColor += QLatin1Char(' ');
                QColor color = QColorDialog::customColor(i);
                strColor += QVariant(color).toString();
            }
            QByteArray data = strColor.toLatin1();
            ar.Value(data.data());
        }
        void Serialize(qt3dsdm::IDOMReader &ar)
        {
            using namespace qt3dsdm;
            TCharStr countStr;
            ar.Att(L"count", countStr);
            int count = QString::fromWCharArray(countStr.wide_str()).toInt();
            const wchar_t *value;
            if (ar.Value(value)) {
                QString strColor = QString::fromWCharArray(value);
                QStringList strColors = strColor.split(" ");
                if (count != strColors.size())
                    qWarning() << "Color count is invalid.";
                for (int i = 0; i < strColors.size(); ++i) {
                    QColor color = QColor(strColors.at(i));
                    QColorDialog::setCustomColor(i, color);
                }
            }
        }
    };

public:
    CProjectSettingsSerializer(CStudioProjectSettings *inSettings)
        : m_ProjectSettings(inSettings)
    {
    }
    virtual ~CProjectSettingsSerializer() {}

    void Serialize(qt3dsdm::IDOMWriter &ar)
    {
        using namespace qt3dsdm;
        using namespace std;
        CStudioProjectSettings *theProjectSettings = GetProjectSettings();
        Q3DStudio::CString author =
                Q3DStudio::CString::fromQString(theProjectSettings->getAuthor());
        TCharPtr theAuthor = author;
        ar.Att(L"author", theAuthor);
        Q3DStudio::CString company =
                Q3DStudio::CString::fromQString(theProjectSettings->getCompany());
        TCharPtr theCompany = company;
        ar.Att(L"company", theCompany);

        QSize theSize = theProjectSettings->getPresentationSize();
        ar.Att(L"presentationWidth", static_cast<qt3ds::QT3DSI32>(theSize.width()));
        ar.Att(L"presentationHeight", static_cast<qt3ds::QT3DSI32>(theSize.height()));

        if (theProjectSettings->getRotatePresentation())
            ar.Att("presentationRotation", "90");

        bool theMaintainAspect = theProjectSettings->getMaintainAspect();
        ar.Att("maintainAspect", theMaintainAspect);

        bool thePreferKtx = theProjectSettings->getPreferCompressedTextures();
        ar.Att("preferKtx", thePreferKtx);

        if (QColorDialog::customCount() > 0) {
            CustomColorSerializer ccs;
            ar.Serialize(L"CustomColors", ccs);
        }
    }
    void Serialize(qt3dsdm::IDOMReader &ar)
    {
        using namespace qt3dsdm;
        using namespace std;
        CStudioProjectSettings *theProjectSettings = GetProjectSettings();
        theProjectSettings->reset();

        TCharStr theAuthor;
        ar.Att(L"author", theAuthor);
        Q3DStudio::CString theAuthorStr(theAuthor.wide_str());
        theProjectSettings->setAuthor(theAuthorStr.toQString());

        TCharStr theCompany;
        ar.Att(L"company", theCompany);
        Q3DStudio::CString theCompanyStr(theCompany.wide_str());
        theProjectSettings->setCompany(theCompanyStr.toQString());

        qt3ds::QT3DSI32 thePresentationWidth;
        qt3ds::QT3DSI32 thePresentationHeight;
        ar.Att("presentationWidth", thePresentationWidth);
        ar.Att("presentationHeight", thePresentationHeight);
        theProjectSettings->setPresentationSize(QSize(thePresentationWidth, thePresentationHeight));

        qt3ds::QT3DSI32 thePresentationRotate;
        if (ar.Att("presentationRotation", thePresentationRotate) && thePresentationRotate == 90)
            theProjectSettings->setRotatePresentation(true);

        bool theMaintainAspect;
        ar.Att("maintainAspect", theMaintainAspect);
        theProjectSettings->setMaintainAspect(theMaintainAspect);

        bool thePreferKtx = false;
        if (ar.Att("preferKtx", thePreferKtx))
            theProjectSettings->setPreferCompressedTextures(thePreferKtx);

        {
            CustomColorSerializer ccs;
            ar.Serialize(L"CustomColors", ccs);
        }
    }

    CStudioProjectSettings *GetProjectSettings() { return m_ProjectSettings; }

private:
    CStudioProjectSettings *m_ProjectSettings;
};

template <typename Archive>
void save(Archive &ar, const CProjectSettingsSerializer &inData, unsigned int)
{
    CStudioProjectSettings *theProjectSettings =
        const_cast<CProjectSettingsSerializer &>(inData).GetProjectSettings();
}

template <typename Archive>
void load(Archive &ar, CProjectSettingsSerializer &inData, unsigned int)
{
}

#endif // INCLUDED_PROJECT_SETTINGS_SERIALIZER_H