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"
22
#include "treadxml.h"
23
 
24
using std::string;
25
using std::vector;
26
using std::map;
27
using std::pair;
28
 
29
TPalette::TPalette()
30
{
31
    DECL_TRACER("TPalette::TPalette()");
32
}
33
 
34
TPalette::TPalette(const std::string& file)
35
{
36
    DECL_TRACER("TPalette::TPalette(const std::string& file)");
37
    initialize(file);
38
}
39
 
40
TPalette::~TPalette()
41
{
42
    DECL_TRACER("TPalette::~TPalette()");
43
}
44
 
45
void TPalette::initialize(const std::string& file)
46
{
47
    DECL_TRACER("TPalette::initialize(const std::string& file)");
48
 
49
    makeFileName(TConfig::getProjectPath(), file);
50
    string path;
51
 
52
    if (isValidFile())
53
        path = getFileName();
54
 
55
    TReadXML reader(path);
56
 
57
    if (TError::isError())
58
        return;
59
 
60
    reader.findElement("paletteData", "name");
61
 
62
    if (!reader.success())
63
    {
64
        MSG_ERROR("Error reading palatte file " << file << "!");
65
        TError::setError();
66
        return;
67
    }
68
 
69
    string palName = reader.getAttribute("name");
70
 
71
    if (havePalette(palName))
72
        return;
73
 
74
    mxml_node_t *node = reader.getFirstChild();
75
 
76
    while (node)
77
    {
78
        PDATA_T pal;
79
        string e = reader.getElementName(node);
80
 
81
        if (e.compare("color") != 0)
82
        {
83
            node = reader.getNextChild();
84
            pal.clear();
85
            continue;
86
        }
87
 
88
        pal.index = atoi(reader.getAttributeFromNode(node, "index").c_str());
89
        pal.name = reader.getAttributeFromNode(node, "name");
90
        string color = reader.getTextFromNode(node);
91
 
92
        if (color.at(0) == '#')     // Do we have a valid color value?
93
        {
94
            string sCol = "0x" + color.substr(1);
95
            pal.color = strtoul(sCol.c_str(), 0, 16);
96
        }
97
 
98
        if (mColors.find(pal.name) != mColors.end())    // Don't insert color if it's already in list
99
        {
23 andreas 100
            MSG_TRACE("Ignoring color " << pal.name << " because it was read before!");
4 andreas 101
            node = reader.getNextChild();
102
            pal.clear();
103
            continue;
104
        }
105
 
106
        // Insert color into list and get next child if there is one.
107
        mColors.insert(pair<string, PDATA_T>(pal.name, pal));
108
        pal.clear();
109
        node = reader.getNextChild();
110
    }
111
}
112
 
11 andreas 113
void TPalette::reset()
114
{
115
    DECL_TRACER("TPalette::reset()");
116
 
117
    mColors.clear();
118
    mPaletteNames.clear();
119
}
120
 
4 andreas 121
PDATA_T TPalette::findColor(const std::string& name)
122
{
123
    DECL_TRACER("TPalette::findColor(const std::string& name)");
124
 
125
    map<string, PDATA_T>::iterator iter;
126
 
127
    if ((iter = mColors.find(name)) == mColors.end())
128
        return PDATA_T();
129
 
130
    return iter->second;
131
}
132
 
133
PDATA_T TPalette::findColor(int pID)
134
{
135
    DECL_TRACER("TPalette::findColor(int pID)");
136
 
137
    map<string, PDATA_T>::iterator iter;
138
 
139
    for (iter = mColors.begin(); iter != mColors.end(); iter++)
140
    {
141
        if (iter->second.index == pID)
142
            return iter->second;
143
    }
144
 
145
    return PDATA_T();
146
}
147
 
148
bool TPalette::havePalette(const std::string& name)
149
{
150
    DECL_TRACER("TPalette::havePalette(const std::string& name)");
151
 
152
    vector<string>::iterator iter;
153
 
154
    for (iter = mPaletteNames.begin(); iter != mPaletteNames.end(); iter++)
155
    {
156
        if (iter->compare(name) == 0)
157
            return true;
158
    }
159
 
160
    return false;
161
}