Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 andreas 1
/*
21 andreas 2
 * Copyright (C) 2020, 2021 by Andreas Theofilu <andreas@theosys.at>
4 andreas 3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program 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
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#include "tpalette.h"
20
#include "terror.h"
21
#include "tconfig.h"
77 andreas 22
#include "texpat++.h"
4 andreas 23
 
24
using std::string;
25
using std::vector;
26
using std::map;
27
using std::pair;
77 andreas 28
using namespace Expat;
4 andreas 29
 
30
TPalette::TPalette()
31
{
32
    DECL_TRACER("TPalette::TPalette()");
33
}
34
 
35
TPalette::TPalette(const std::string& file)
36
{
37
    DECL_TRACER("TPalette::TPalette(const std::string& file)");
38
    initialize(file);
39
}
40
 
41
TPalette::~TPalette()
42
{
43
    DECL_TRACER("TPalette::~TPalette()");
44
}
45
 
46
void TPalette::initialize(const std::string& file)
47
{
48
    DECL_TRACER("TPalette::initialize(const std::string& file)");
49
 
50
    makeFileName(TConfig::getProjectPath(), file);
51
    string path;
52
 
53
    if (isValidFile())
54
        path = getFileName();
55
 
77 andreas 56
    TExpat xml(path);
57
    xml.setEncoding(ENC_CP1250);
4 andreas 58
 
77 andreas 59
    if (!xml.parse())
4 andreas 60
        return;
61
 
77 andreas 62
    int depth = 0;
63
    size_t index = 0;
4 andreas 64
 
77 andreas 65
    if ((index = xml.getElementIndex("paletteData", &depth)) == TExpat::npos)
4 andreas 66
    {
77 andreas 67
        MSG_ERROR("Element \"paletteData\" was not found!");
4 andreas 68
        TError::setError();
69
        return;
70
    }
71
 
77 andreas 72
    vector<ATTRIBUTE_t> attrs = xml.getAttributes();
73
    string palName = xml.getAttribute("name", attrs);
4 andreas 74
 
75
    if (havePalette(palName))
76
        return;
77
 
77 andreas 78
    string name, content;
79
    index++;
4 andreas 80
 
77 andreas 81
    while ((index = xml.getNextElementFromIndex(index, &name, &content, &attrs)) != TExpat::npos)
4 andreas 82
    {
83
        PDATA_T pal;
84
 
77 andreas 85
        if (name.compare("color") != 0)
4 andreas 86
        {
87
            pal.clear();
88
            continue;
89
        }
90
 
77 andreas 91
        pal.index = xml.getAttributeInt("index", attrs);
92
        pal.name = xml.getAttribute("name", attrs);
93
        string color = content;
4 andreas 94
 
95
        if (color.at(0) == '#')     // Do we have a valid color value?
96
        {
97
            string sCol = "0x" + color.substr(1);
98
            pal.color = strtoul(sCol.c_str(), 0, 16);
99
        }
100
 
101
        if (mColors.find(pal.name) != mColors.end())    // Don't insert color if it's already in list
102
        {
23 andreas 103
            MSG_TRACE("Ignoring color " << pal.name << " because it was read before!");
4 andreas 104
            pal.clear();
105
            continue;
106
        }
107
 
108
        // Insert color into list and get next child if there is one.
109
        mColors.insert(pair<string, PDATA_T>(pal.name, pal));
110
        pal.clear();
111
    }
112
}
113
 
11 andreas 114
void TPalette::reset()
115
{
116
    DECL_TRACER("TPalette::reset()");
117
 
118
    mColors.clear();
119
    mPaletteNames.clear();
120
}
121
 
4 andreas 122
PDATA_T TPalette::findColor(const std::string& name)
123
{
124
    DECL_TRACER("TPalette::findColor(const std::string& name)");
125
 
126
    map<string, PDATA_T>::iterator iter;
127
 
128
    if ((iter = mColors.find(name)) == mColors.end())
129
        return PDATA_T();
130
 
131
    return iter->second;
132
}
133
 
134
PDATA_T TPalette::findColor(int pID)
135
{
136
    DECL_TRACER("TPalette::findColor(int pID)");
137
 
138
    map<string, PDATA_T>::iterator iter;
139
 
140
    for (iter = mColors.begin(); iter != mColors.end(); iter++)
141
    {
142
        if (iter->second.index == pID)
143
            return iter->second;
144
    }
145
 
146
    return PDATA_T();
147
}
148
 
149
bool TPalette::havePalette(const std::string& name)
150
{
151
    DECL_TRACER("TPalette::havePalette(const std::string& name)");
152
 
153
    vector<string>::iterator iter;
154
 
155
    for (iter = mPaletteNames.begin(); iter != mPaletteNames.end(); iter++)
156
    {
157
        if (iter->compare(name) == 0)
158
            return true;
159
    }
160
 
161
    return false;
162
}