~ubuntu-branches/ubuntu/wily/libkscreen/wily-proposed

« back to all changes in this revision

Viewing changes to backends/fake/parser.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2014-10-01 15:47:21 UTC
  • Revision ID: package-import@ubuntu.com-20141001154721-96ng88bgtxpj8sjv
Tags: upstream-5.1.0.1
ImportĀ upstreamĀ versionĀ 5.1.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*************************************************************************************
 
2
 *  Copyright (C) 2012 by Alejandro Fiestas Olivares <afiestas@kde.org>              *
 
3
 *                                                                                   *
 
4
 *  This library is free software; you can redistribute it and/or                    *
 
5
 *  modify it under the terms of the GNU Lesser General Public                       *
 
6
 *  License as published by the Free Software Foundation; either                     *
 
7
 *  version 2.1 of the License, or (at your option) any later version.               *
 
8
 *                                                                                   *
 
9
 *  This library is distributed in the hope that it will be useful,                  *
 
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
 
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                *
 
12
 *  Lesser General Public License for more details.                                  *
 
13
 *                                                                                   *
 
14
 *  You should have received a copy of the GNU Lesser General Public                 *
 
15
 *  License along with this library; if not, write to the Free Software              *
 
16
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA       *
 
17
 *************************************************************************************/
 
18
 
 
19
#include "parser.h"
 
20
#include "fake.h"
 
21
 
 
22
#include <config.h>
 
23
 
 
24
#include <QtCore/QFile>
 
25
#include <QLoggingCategory>
 
26
#include <QJsonDocument>
 
27
#include <QJsonArray>
 
28
#include <QJsonObject>
 
29
 
 
30
using namespace KScreen;
 
31
 
 
32
Config* Parser::fromJson(const QByteArray& data)
 
33
{
 
34
    Config *config =  new Config();
 
35
 
 
36
    QJsonObject json = QJsonDocument::fromJson(data).object();
 
37
 
 
38
    Screen* screen = Parser::screenFromJson(json["screen"].toObject().toVariantMap());
 
39
 
 
40
    QVariantList outputs = json["outputs"].toArray().toVariantList();
 
41
    if (outputs.isEmpty()) {
 
42
        return config;
 
43
    }
 
44
 
 
45
    Output *output;
 
46
    OutputList outputList;
 
47
    Q_FOREACH(const QVariant &value, outputs) {
 
48
        output = Parser::outputFromJson(value.toMap());
 
49
        outputList.insert(output->id(), output);
 
50
    }
 
51
 
 
52
    config->setScreen(screen);
 
53
    config->setOutputs(outputList);
 
54
    return config;
 
55
}
 
56
 
 
57
Config* Parser::fromJson(const QString& path)
 
58
{
 
59
    QFile file(path);
 
60
    file.open(QIODevice::ReadOnly);
 
61
 
 
62
    return Parser::fromJson(file.readAll());
 
63
}
 
64
 
 
65
Screen* Parser::screenFromJson(const QVariantMap &data)
 
66
{
 
67
    Screen* screen = new Screen;
 
68
    screen->setId(data["id"].toInt());
 
69
    screen->setMinSize(Parser::sizeFromJson(data["minSize"].toMap()));
 
70
    screen->setMaxSize(Parser::sizeFromJson(data["maxSize"].toMap()));
 
71
    screen->setCurrentSize(Parser::sizeFromJson(data["currentSize"].toMap()));
 
72
    screen->setMaxActiveOutputsCount(data["maxActiveOutputsCount"].toInt());
 
73
 
 
74
    return screen;
 
75
}
 
76
 
 
77
void Parser::qvariant2qobject(const QVariantMap& variant, QObject* object)
 
78
{
 
79
    for ( QVariantMap::const_iterator iter = variant.begin(); iter != variant.end(); ++iter )
 
80
    {
 
81
        QVariant property = object->property( iter.key().toLatin1() );
 
82
        Q_ASSERT( property.isValid() );
 
83
        if ( property.isValid() )
 
84
        {
 
85
            QVariant value = iter.value();
 
86
            if ( value.canConvert( property.type() ) )
 
87
            {
 
88
                value.convert( property.type() );
 
89
                object->setProperty( iter.key().toLatin1(), value );
 
90
            } else if ( QString( QLatin1String("QVariant") ).compare( QLatin1String( property.typeName() ) ) == 0) {
 
91
                object->setProperty( iter.key().toLatin1(), value );
 
92
            }
 
93
        }
 
94
    }
 
95
}
 
96
 
 
97
Output* Parser::outputFromJson(QMap< QString, QVariant > map)
 
98
{
 
99
    Output *output = new Output;
 
100
    output->setId(map["id"].toInt());
 
101
 
 
102
    QStringList preferredModes;
 
103
    QVariantList modes = map["preferredModes"].toList();
 
104
    Q_FOREACH(const QVariant &mode, modes) {
 
105
        preferredModes.append(mode.toString());
 
106
    }
 
107
    output->setPreferredModes(preferredModes);
 
108
    map.remove(QLatin1Literal("preferredModes"));
 
109
 
 
110
    Mode *mode;
 
111
    ModeList modelist;
 
112
    modes = map["modes"].toList();
 
113
    Q_FOREACH(const QVariant &modeValue, modes) {
 
114
        mode = Parser::modeFromJson(modeValue);
 
115
        modelist.insert(mode->id(), mode);
 
116
    }
 
117
    output->setModes(modelist);
 
118
    map.remove(QLatin1Literal("modes"));
 
119
 
 
120
    if(map.contains("clones")) {
 
121
        QList<int> clones;
 
122
        Q_FOREACH(const QVariant &id, map["clones"].toList()) {
 
123
            clones.append(id.toInt());
 
124
        }
 
125
 
 
126
        output->setClones(clones);
 
127
        map.remove(QLatin1Literal("clones"));
 
128
    }
 
129
 
 
130
    QString type = map["type"].toByteArray().toUpper();
 
131
    if (type.contains("LVDS") || type.contains("EDP") || type.contains("IDP")) {
 
132
        output->setType(Output::Panel);
 
133
    } else if (type.contains("VGA")) {
 
134
        output->setType(Output::VGA);
 
135
    } else if (type.contains("DVI")) {
 
136
        output->setType(Output::DVI);
 
137
    } else if (type.contains("DVI-I")) {
 
138
        output->setType(Output::DVII);
 
139
    } else if (type.contains("DVI-A")) {
 
140
        output->setType(Output::DVIA);
 
141
    } else if (type.contains("DVI-D")) {
 
142
        output->setType(Output::DVID);
 
143
    } else if (type.contains("HDMI")) {
 
144
        output->setType(Output::HDMI);
 
145
    } else if (type.contains("Panel")) {
 
146
        output->setType(Output::Panel);
 
147
    } else if (type.contains("TV")) {
 
148
        output->setType(Output::TV);
 
149
    } else if (type.contains("TV-Composite")) {
 
150
        output->setType(Output::TVComposite);
 
151
    } else if (type.contains("TV-SVideo")) {
 
152
        output->setType(Output::TVSVideo);
 
153
    } else if (type.contains("TV-Component")) {
 
154
        output->setType(Output::TVComponent);
 
155
    } else if (type.contains("TV-SCART")) {
 
156
        output->setType(Output::TVSCART);
 
157
    } else if (type.contains("TV-C4")) {
 
158
        output->setType(Output::TVC4);
 
159
    } else if (type.contains("DisplayPort")) {
 
160
        output->setType(Output::DisplayPort);
 
161
    } else if (type.contains("Unknown")) {
 
162
        output->setType(Output::Unknown);
 
163
    } else {
 
164
        qCWarning(KSCREEN_FAKE) << "Output Type not translated:" << type;
 
165
    }
 
166
    map.remove(QLatin1Literal("type"));
 
167
 
 
168
    if (map.contains("pos")) {
 
169
        output->setPos(Parser::pointFromJson(map["pos"].toMap()));
 
170
        map.remove(QLatin1Literal("pos"));
 
171
    }
 
172
 
 
173
    //Remove some extra properties that we do not want or need special treatment
 
174
    map.remove(QLatin1Literal("edid"));
 
175
 
 
176
    Parser::qvariant2qobject(map, output);
 
177
    return output;
 
178
}
 
179
 
 
180
Mode* Parser::modeFromJson(const QVariant& data)
 
181
{
 
182
    QVariantMap map = data.toMap();
 
183
    Mode *mode = new Mode;
 
184
    Parser::qvariant2qobject(map, mode);
 
185
 
 
186
    mode->setSize(Parser::sizeFromJson(map["size"].toMap()));
 
187
 
 
188
    return mode;
 
189
}
 
190
 
 
191
QSize Parser::sizeFromJson(const QVariant& data)
 
192
{
 
193
    QVariantMap map = data.toMap();
 
194
 
 
195
    QSize size;
 
196
    size.setWidth(map["width"].toInt());
 
197
    size.setHeight(map["height"].toInt());
 
198
 
 
199
    return size;
 
200
}
 
201
 
 
202
QPoint Parser::pointFromJson(const QVariant& data)
 
203
{
 
204
    QVariantMap map = data.toMap();
 
205
 
 
206
    QPoint point;
 
207
    point.setX(map["x"].toInt());
 
208
    point.setY(map["y"].toInt());
 
209
 
 
210
    return point;
 
211
}
 
212
 
 
213
QRect Parser::rectFromJson(const QVariant& data)
 
214
{
 
215
    QRect rect;
 
216
    rect.setSize(Parser::sizeFromJson(data));
 
217
    rect.setBottomLeft(Parser::pointFromJson(data));
 
218
 
 
219
    return rect;
 
220
}
 
221
 
 
222
bool Parser::validate(const QByteArray& data)
 
223
{
 
224
    Q_UNUSED(data);
 
225
    return true;
 
226
}
 
227
 
 
228
bool Parser::validate(const QString& data)
 
229
{
 
230
    Q_UNUSED(data);
 
231
    return true;
 
232
}