summaryrefslogtreecommitdiff
path: root/libtemplate/templateloopfunction.c
blob: b0030d39c12d351a15d9f5b0ee9d569d53bebc4b (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "templateloopfunction.h"
#include "../libcore/helpers.h"

using namespace std;

// --- cTemplateFunction -------------------------------------------------------------

cTemplateLoopFunction::cTemplateLoopFunction(void) : cTemplateFunction(ftLoop) { 
}

cTemplateLoopFunction::~cTemplateLoopFunction(void) {
}

void cTemplateLoopFunction::InitIterator(void) {
    funcIt = functions.begin();
}

void cTemplateLoopFunction::AddFunction(string name, vector<pair<string, string> > &params) {
    eFuncType type = ftNone;

    if (!name.compare("drawtext")) {
        type = ftDrawText;
    } else if (!name.compare("drawtextbox")) {
                type = ftDrawTextBox;
        } else if (!name.compare("drawimage")) {
        type = ftDrawImage;
    } else if (!name.compare("drawrectangle")) {
        type = ftDrawRectangle;
    } else if (!name.compare("drawellipse")) {
        type = ftDrawEllipse;
    }

    if (type == ftNone) {
        return;
    }

    cTemplateFunction *f = new cTemplateFunction(type);
    f->SetParameters(params);
    functions.push_back(f);
}

void cTemplateLoopFunction::CalculateLoopFuncParameters(void) {
    int columnWidth = GetNumericParameter(ptColumnWidth);
    int rowHeight = GetNumericParameter(ptRowHeight);
    for (vector<cTemplateFunction*>::iterator func = functions.begin(); func != functions.end(); func++) {
        (*func)->SetGlobals(globals);
        (*func)->SetContainer(0, 0, containerWidth, containerHeight);
        (*func)->SetLoopContainer(columnWidth, rowHeight);
        (*func)->CalculateParameters();
        (*func)->CompleteParameters();
    }
}

cTemplateFunction *cTemplateLoopFunction::GetNextFunction(void) {
    if (funcIt == functions.end())
        return NULL;
    cTemplateFunction *func = *funcIt;
    funcIt++;
    return func;
}

void cTemplateLoopFunction::ClearDynamicParameters(void) {
    InitIterator();
    cTemplateFunction *func = NULL;
    while(func = GetNextFunction()) {
        func->ClearDynamicParameters();
    }
}

void cTemplateLoopFunction::ParseDynamicParameters(map <string,string> *tokens) {
    if (!tokens)
        return;
    InitIterator();
    cTemplateFunction *func = NULL;
    
    map <string,int> intTokens;
    for (map <string,string>::iterator it = tokens->begin(); it != tokens->end(); it++) {
        if (isNumber(it->second))
            intTokens.insert(pair<string, int>(it->first, atoi((it->second).c_str())));
    } 

    bool completelyParsed = true;
    while(func = GetNextFunction()) {
        func->SetStringTokens(tokens);
        func->SetIntTokens(&intTokens);
        bool funcCompletelyParsed = func->ParseParameters();
        if (!funcCompletelyParsed)
            completelyParsed = false;
        if (func->Updated())
            func->CompleteParameters();
        func->UnsetStringTokens();
        func->UnsetIntTokens();
    }
    if (completelyParsed) {
        return;
    }

    ReplaceWidthFunctions();
    ReplaceHeightFunctions();
}

int cTemplateLoopFunction::GetLoopElementsWidth(void) {
    int cW = GetNumericParameter(ptColumnWidth);
    if (cW > 0) {
        return cW;
    }
    InitIterator();
    cTemplateFunction *func = NULL;
    int maxWidth = 1;
    while(func = GetNextFunction()) {
        int funcWidth = func->GetWidth(true);
        if (funcWidth > maxWidth)
            maxWidth = funcWidth;
    }
    return maxWidth;
}

int cTemplateLoopFunction::GetLoopElementsHeight(void) {
    int rH = GetNumericParameter(ptRowHeight);
    if (rH > 0)
        return rH;
    InitIterator();
    cTemplateFunction *func = NULL;
    int maxHeight = 1;
    while(func = GetNextFunction()) {
        int funcY = func->GetNumericParameter(ptY);
        int funcHeight = func->GetHeight();
        int totalHeight = funcY + funcHeight;
        if (totalHeight > maxHeight)
            maxHeight = totalHeight;
    }
    return maxHeight;
}

void cTemplateLoopFunction::ReplaceWidthFunctions(void) {
    InitIterator();
    cTemplateFunction *func = NULL;
    while(func = GetNextFunction()) {
        if (func->ParsedCompletely()) {
            continue;
        }
        multimap<eParamType,string> widths;
        func->GetNeededWidths(&widths);
        for (map<eParamType, string>::iterator names = widths.begin(); names !=widths.end(); names++) {
            eParamType type = names->first;
            string label = names->second;
            int funcWidth = 0;
            for (vector<cTemplateFunction*>::iterator it = functions.begin(); it != functions.end(); it++) {
                cTemplateFunction *myFunc = *it;
                string myFuncName = myFunc->GetParameter(ptName); 
                if (!myFuncName.compare(label)) {
                    funcWidth = myFunc->GetWidth();
                    func->SetWidth(type, label, funcWidth);
                    if (func->Updated()) {
                        func->CompleteParameters();
                    }
                }
            }
        }
    }
}

void cTemplateLoopFunction::ReplaceHeightFunctions(void) {
    InitIterator();
    cTemplateFunction *func = NULL;
    while(func = GetNextFunction()) {
        if (func->ParsedCompletely()) {
            continue;
        }
        multimap<eParamType,string> heights;
        func->GetNeededHeights(&heights);
        for (map<eParamType, string>::iterator names = heights.begin(); names !=heights.end(); names++) {
            eParamType type = names->first;
            string label = names->second;
            int funcHeight = 0;
            for (vector<cTemplateFunction*>::iterator it = functions.begin(); it != functions.end(); it++) {
                cTemplateFunction *myFunc = *it;
                string myFuncName = myFunc->GetParameter(ptName); 
                if (!myFuncName.compare(label)) {
                    funcHeight = myFunc->GetHeight();
                    func->SetHeight(type, label, funcHeight);
                    if (func->Updated()) {
                        func->CompleteParameters();
                    }
                }
            }
        }
    }
}

bool cTemplateLoopFunction::Ready(void) {
    bool isReady = true;
    map< eParamType, string >::iterator hit = numericDynamicParameters.find(ptColumnWidth);
    if (hit != numericDynamicParameters.end())
        isReady = false;
    hit = numericDynamicParameters.find(ptRowHeight);
    if (hit != numericDynamicParameters.end())
        isReady = false;
    return isReady;
}

void cTemplateLoopFunction::Debug(void) {
    cTemplateFunction::Debug();
    esyslog("skindesigner: functions to be looped:");
    for (vector<cTemplateFunction*>::iterator func = functions.begin(); func != functions.end(); func++) {
        (*func)->Debug();
    }
}