Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 andreas 1
/*
101 andreas 2
 * Copyright (C) 2020 to 2022 by Andreas Theofilu <andreas@theosys.at>
5 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 <QTextObject>
20
#include <QLabel>
21
#include <QImage>
22
#include <QWidget>
41 andreas 23
#include <QPropertyAnimation>
21 andreas 24
#include <QtMultimedia/QMediaPlayer>
25
#include <QtMultimediaWidgets/QVideoWidget>
5 andreas 26
#include "tobject.h"
27
#include "terror.h"
28
 
107 andreas 29
using std::string;
30
 
31
std::mutex mutex_obj;
32
 
5 andreas 33
TObject::TObject()
34
{
35
    DECL_TRACER("TObject::TObject()");
36
}
37
 
38
TObject::~TObject()
39
{
40
    DECL_TRACER("TObject::~TObject()");
41
 
89 andreas 42
    clear();
43
}
44
 
45
void TObject::clear(bool force)
46
{
47
    DECL_TRACER("TObject::clear()");
48
 
5 andreas 49
    OBJECT_t *obj = mObject;
50
 
51
    while (obj)
52
    {
53
        OBJECT_t *next = obj->next;
89 andreas 54
 
55
        if (!force)
56
            dropContent(obj);
57
 
5 andreas 58
        delete obj;
59
        obj = next;
60
    }
89 andreas 61
 
62
    mObject = nullptr;
5 andreas 63
}
64
 
14 andreas 65
void TObject::dropContent(OBJECT_t* obj)
66
{
67
    DECL_TRACER("TObject::dropContent(OBJECT_t* obj)");
68
 
107 andreas 69
    mutex_obj.lock();
70
 
89 andreas 71
    try
14 andreas 72
    {
89 andreas 73
        switch (obj->type)
74
        {
75
            case OBJ_TEXT:
76
            case OBJ_INPUT:
77
                if (obj->object.multitext)
78
                {
79
                    obj->object.multitext = nullptr;
80
                }
14 andreas 81
 
89 andreas 82
                if (obj->object.linetext)
83
                    obj->object.linetext = nullptr;
51 andreas 84
 
89 andreas 85
                obj->wid = 0;
86
            break;
51 andreas 87
 
89 andreas 88
            case OBJ_BUTTON:
89
                if (obj->object.label)
90
                {
91
                    obj->object.label = nullptr;
92
                }
93
            break;
94
 
95
            case OBJ_PAGE:
96
            case OBJ_SUBPAGE:
97
                if (obj->object.widget)
98
                {
99
                    delete obj->object.widget;
100
                    obj->object.widget = nullptr;
96 andreas 101
                }
89 andreas 102
                break;
14 andreas 103
 
89 andreas 104
            case OBJ_VIDEO:
105
                if (obj->object.vwidget)
106
                {
107
                    delete obj->object.vwidget;
14 andreas 108
 
89 andreas 109
                    if (obj->player)
110
                        delete obj->player;
21 andreas 111
 
89 andreas 112
                    obj->object.vwidget = nullptr;
113
                    obj->player = nullptr;
114
                }
21 andreas 115
 
89 andreas 116
            default:
117
                break;
118
        }
14 andreas 119
    }
89 andreas 120
    catch (std::exception& e)
121
    {
122
        MSG_ERROR("Error freeing an object: " << e.what());
123
    }
107 andreas 124
 
125
    mutex_obj.unlock();
14 andreas 126
}
127
 
5 andreas 128
TObject::OBJECT_t *TObject::addObject()
129
{
130
    DECL_TRACER("TObject::addObject()");
131
 
107 andreas 132
    mutex_obj.lock();
5 andreas 133
    OBJECT_t *obj = new OBJECT_t;
134
    obj->next = nullptr;
21 andreas 135
    obj->object.vwidget = nullptr;
136
    obj->player = nullptr;
6 andreas 137
    obj->object.label = nullptr;
51 andreas 138
    obj->object.multitext = nullptr;
139
    obj->object.linetext = nullptr;
6 andreas 140
    obj->object.widget = nullptr;
5 andreas 141
 
142
    if (!mObject)
143
        mObject = obj;
144
    else
145
    {
146
        OBJECT_t *p = mObject;
147
 
148
        while (p->next)
149
            p = p->next;
150
 
151
        p->next = obj;
152
    }
153
 
107 andreas 154
    mutex_obj.unlock();
5 andreas 155
    return obj;
156
}
157
 
158
TObject::OBJECT_t *TObject::findObject(ulong handle)
159
{
160
    DECL_TRACER("TObject::findObject(ulong handle)");
161
 
162
    OBJECT_t *obj = mObject;
163
 
164
    while (obj)
165
    {
166
        if (obj->handle == handle)
167
            return obj;
168
 
169
        obj = obj->next;
170
    }
171
 
172
    return nullptr;
173
}
174
 
51 andreas 175
TObject::OBJECT_t * TObject::findObject(WId id)
176
{
177
    DECL_TRACER("TObject::findObject(WId id)");
178
 
179
    OBJECT_t *obj = mObject;
180
 
181
    while (obj)
182
    {
183
        if (obj->wid == id)
184
            return obj;
185
 
186
        obj = obj->next;
187
    }
188
 
189
    return nullptr;
190
}
191
 
11 andreas 192
TObject::OBJECT_t *TObject::findFirstChild(ulong handle)
193
{
194
    DECL_TRACER("TObject::findFirstChild(ulong handle)");
195
 
196
    OBJECT_t *obj = mObject;
197
 
198
    while (obj)
199
    {
13 andreas 200
        if (obj->handle != (handle & 0xffff0000) && (obj->handle & 0xffff0000) == (handle & 0xffff0000))
11 andreas 201
            return obj;
202
 
203
        obj = obj->next;
204
    }
205
 
206
    return nullptr;
207
}
208
 
209
TObject::OBJECT_t *TObject::findNextChild(ulong handle)
210
{
211
    DECL_TRACER("TObject::findNextChild(ulong handle)");
212
 
213
    OBJECT_t *obj = mObject;
214
    bool next = false;
215
 
216
    while (obj)
217
    {
218
        if (next && (obj->handle & 0xffff0000) == (handle & 0xffff0000))
219
            return obj;
220
 
221
        if (obj->handle == handle)
222
            next = true;
223
 
224
        obj = obj->next;
225
    }
226
 
227
    return nullptr;
228
}
229
 
42 andreas 230
TObject::OBJECT_t * TObject::getMarkedRemove()
231
{
232
    DECL_TRACER("TObject::getMarkedRemove()");
51 andreas 233
 
42 andreas 234
    OBJECT_t *obj = mObject;
51 andreas 235
 
42 andreas 236
    while (obj)
237
    {
238
        if (obj->remove)
239
            return obj;
51 andreas 240
 
42 andreas 241
        obj = obj->next;
242
    }
51 andreas 243
 
42 andreas 244
    return nullptr;
245
}
246
 
247
TObject::OBJECT_t * TObject::getNextMarkedRemove(TObject::OBJECT_t* object)
248
{
249
    DECL_TRACER("TObject::getNextMarkedRemove(TObject::OBJECT_t* obj)");
51 andreas 250
 
42 andreas 251
    OBJECT_t *obj = nullptr;
51 andreas 252
 
42 andreas 253
    if (!object || !object->next)
254
        return nullptr;
51 andreas 255
 
42 andreas 256
    obj = object->next;
51 andreas 257
 
42 andreas 258
    while (obj)
259
    {
260
        if (obj->remove)
261
            return obj;
51 andreas 262
 
42 andreas 263
        obj = obj->next;
264
    }
51 andreas 265
 
42 andreas 266
    return nullptr;
267
}
268
 
142 andreas 269
TObject::OBJECT_t *TObject::findFirstWindow()
270
{
271
    DECL_TRACER("TObject::getFirstWindow()");
272
 
273
    OBJECT_t *obj = mObject;
274
 
275
    while (obj)
276
    {
277
        if (obj->type == OBJ_SUBPAGE)
278
            return obj;
279
 
280
        obj = obj->next;
281
    }
282
 
283
    return nullptr;
284
}
285
 
286
TObject::OBJECT_t *TObject::findNextWindow(TObject::OBJECT_t *obj)
287
{
288
    DECL_TRACER("TObject::findNextWindow()");
289
 
290
    if (!obj || !obj->next)
291
        return nullptr;
292
 
293
    OBJECT_t *o = obj->next;
294
 
295
    while (o)
296
    {
297
        if (o->type == OBJ_SUBPAGE)
298
            return o;
299
 
300
        o = o->next;
301
    }
302
 
303
    return nullptr;
304
}
305
 
5 andreas 306
void TObject::removeObject(ulong handle)
307
{
308
    DECL_TRACER("TObject::removeObject(ulong handle)");
309
 
107 andreas 310
    mutex_obj.lock();
5 andreas 311
    OBJECT_t *obj = mObject;
312
    OBJECT_t *prev = nullptr;
313
 
314
    while (obj)
315
    {
316
        if (obj->handle == handle)
317
        {
318
            if (!prev)
319
            {
320
                mObject = obj->next;
321
                delete obj;
322
            }
323
            else
324
            {
325
                prev->next = obj->next;
326
                delete obj;
327
            }
328
 
107 andreas 329
            mutex_obj.unlock();
5 andreas 330
            return;
331
        }
332
 
333
        prev = obj;
334
        obj = obj->next;
335
    }
107 andreas 336
 
337
    mutex_obj.unlock();
5 andreas 338
}
339
 
11 andreas 340
void TObject::removeAllChilds(ulong handle)
341
{
342
    DECL_TRACER("TObject::removeAllChilds(ulong handle)");
343
 
107 andreas 344
    mutex_obj.lock();
14 andreas 345
    OBJECT_t *obj = mObject;
346
    OBJECT_t *prev = nullptr;
11 andreas 347
 
348
    while (obj)
349
    {
14 andreas 350
        if ((obj->handle & 0xffff0000) == (handle & 0xffff0000) && obj->handle != (handle & 0xffff0000))
351
        {
352
            if (!prev)
353
            {
354
                mObject = obj->next;
355
                delete obj;
356
                obj = mObject;
357
                continue;
358
            }
359
            else
360
            {
361
                prev->next = obj->next;
362
                delete obj;
363
                obj = prev;
364
            }
365
        }
366
 
367
        prev = obj;
368
        obj = obj->next;
11 andreas 369
    }
107 andreas 370
 
371
    mutex_obj.unlock();
11 andreas 372
}
14 andreas 373
 
107 andreas 374
void TObject::cleanMarked()
14 andreas 375
{
107 andreas 376
    DECL_TRACER("TObject::cleanMarked()");
377
 
378
    mutex_obj.lock();
379
    OBJECT_t *obj = mObject;
380
    OBJECT_t *prev = nullptr;
381
 
382
    while (obj)
383
    {
384
        if (obj->remove && (!obj->animation || obj->animation->state() != QAbstractAnimation::Running))
385
        {
386
            if (obj->type == OBJ_SUBPAGE && obj->object.widget)
387
                delete obj->object.widget;
388
 
389
            if (!prev)
390
            {
391
                mObject = obj->next;
392
                delete obj;
393
                obj = mObject;
394
                continue;
395
            }
396
            else
397
            {
398
                prev->next = obj->next;
399
                delete obj;
400
                obj = prev;
401
            }
402
        }
403
 
404
        prev = obj;
405
        obj = obj->next;
406
    }
407
 
408
    mutex_obj.unlock();
409
}
410
 
411
string TObject::objectToString(TObject::OBJECT_TYPE o)
412
{
14 andreas 413
    switch(o)
414
    {
415
        case OBJ_BUTTON:  return "BUTTON"; break;
416
        case OBJ_INPUT:   return "INPUT"; break;
417
        case OBJ_NONE:    return "undefined"; break;
418
        case OBJ_PAGE:    return "PAGE"; break;
419
        case OBJ_SUBPAGE: return "SUBPAGE"; break;
420
        case OBJ_TEXT:    return "TEXT"; break;
21 andreas 421
        case OBJ_VIDEO:   return "VIDEO"; break;
14 andreas 422
    }
78 andreas 423
 
107 andreas 424
    return string();   // Should not happen but is needed to satisfy the compiler.
14 andreas 425
}