Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 andreas 1
/*
258 andreas 2
 * Copyright (C) 2020 to 2023 by Andreas Theofilu <andreas@theosys.at>
2 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
 
21 andreas 19
/** @file tqtmain.cpp
20
 * @brief Implements the surface of the application.
21
 *
22
 * This file implements the callback functions of the suface. While the most
23
 * classes are drawing the elements, the methods here take the ready elements
24
 * and display them. This file makes the surface completely independent of
25
 * the rest of the application which makes it easy to change the surface by
26
 * any other technology.
27
 */
2 andreas 28
#include <QApplication>
23 andreas 29
#include <QGuiApplication>
14 andreas 30
#include <QByteArray>
2 andreas 31
#include <QCommandLineParser>
32
#include <QCommandLineOption>
9 andreas 33
#include <QLabel>
2 andreas 34
#include <QtWidgets>
264 andreas 35
#include <QStackedWidget>
10 andreas 36
#include <QMouseEvent>
15 andreas 37
#include <QMoveEvent>
59 andreas 38
#include <QTouchEvent>
5 andreas 39
#include <QPalette>
9 andreas 40
#include <QPixmap>
7 andreas 41
#include <QFont>
42
#include <QFontDatabase>
21 andreas 43
#include <QtMultimediaWidgets/QVideoWidget>
44
#include <QtMultimedia/QMediaPlayer>
264 andreas 45
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
181 andreas 46
#   include <QtMultimedia/QMediaPlaylist>
47
#else
48
#   include <QAudioOutput>
346 andreas 49
#   include <QMediaMetaData>
181 andreas 50
#endif
205 andreas 51
#include <QListWidget>
24 andreas 52
#include <QLayout>
40 andreas 53
#include <QSizePolicy>
21 andreas 54
#include <QUrl>
13 andreas 55
#include <QThread>
258 andreas 56
#ifdef Q_OS_IOS
252 andreas 57
#include <QOrientationSensor>
253 andreas 58
#endif
264 andreas 59
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
181 andreas 60
#   include <QSound>
267 andreas 61
#if defined(Q_OS_IOS) || defined(Q_OS_ANDROID)
181 andreas 62
#   include <QtSensors/QOrientationSensor>
217 andreas 63
#   include <qpa/qplatformscreen.h>
267 andreas 64
#endif
187 andreas 65
#else
267 andreas 66
#   include <QPlainTextEdit>
67
#if defined(Q_OS_IOS) || defined(Q_OS_ANDROID)
254 andreas 68
#   include <QGeoPositionInfoSource>
217 andreas 69
#   include <QtSensors/QOrientationReading>
181 andreas 70
#endif
267 andreas 71
#endif
264 andreas 72
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) && defined(Q_OS_ANDROID)
183 andreas 73
#include <QtAndroidExtras/QAndroidJniObject>
211 andreas 74
#include <QtAndroidExtras/QtAndroid>
88 andreas 75
#endif
5 andreas 76
#include <functional>
14 andreas 77
#include <mutex>
255 andreas 78
#ifdef Q_OS_ANDROID
23 andreas 79
#include <android/log.h>
80
#endif
4 andreas 81
#include "tpagemanager.h"
2 andreas 82
#include "tqtmain.h"
83
#include "tconfig.h"
251 andreas 84
#if !defined(Q_OS_IOS) && !defined(Q_OS_ANDROID)
2 andreas 85
#include "tqtsettings.h"
213 andreas 86
#endif
62 andreas 87
#include "tqkeyboard.h"
88
#include "tqkeypad.h"
5 andreas 89
#include "tcolor.h"
92 andreas 90
#include "texcept.h"
118 andreas 91
#include "ttpinit.h"
179 andreas 92
#include "tqdownload.h"
140 andreas 93
#include "tqtphone.h"
190 andreas 94
#include "tqeditline.h"
260 andreas 95
#include "tqtwait.h"
211 andreas 96
#include "terror.h"
270 andreas 97
#include "tresources.h"
285 andreas 98
#include "tqscrollarea.h"
292 andreas 99
#include "tlock.h"
243 andreas 100
#ifdef Q_OS_IOS
250 andreas 101
#include "ios/QASettings.h"
102
#include "ios/tiosrotate.h"
103
#include "ios/tiosbattery.h"
243 andreas 104
#endif
326 andreas 105
#if TESTMODE == 1
106
#include "testmode.h"
107
#endif
2 andreas 108
 
209 andreas 109
#if __cplusplus < 201402L
110
#   error "This module requires at least C++14 standard!"
111
#else
112
#   if __cplusplus < 201703L
113
#       include <experimental/filesystem>
114
namespace fs = std::experimental::filesystem;
115
#       warning "Support for C++14 and experimental filesystem will be removed in a future version!"
116
#   else
117
#       include <filesystem>
118
#       ifdef __ANDROID__
119
namespace fs = std::__fs::filesystem;
120
#       else
121
namespace fs = std::filesystem;
122
#       endif
123
#   endif
124
#endif
125
 
21 andreas 126
/**
44 andreas 127
 * @def THREAD_WAIT
128
 * This defines a time in milliseconds. It is used in the callbacks for the
129
 * functions to draw the elements. Qt need a little time to do it's work.
130
 * Therefore we're waiting a little bit. Otherwise it may result in missing
131
 * elements or black areas on the screen.
21 andreas 132
 */
100 andreas 133
#define THREAD_WAIT     1
44 andreas 134
 
135
extern amx::TAmxNet *gAmxNet;                   //!< Pointer to the class running the thread which handles the communication with the controller.
90 andreas 136
extern bool _restart_;                          //!< If this is set to true then the whole program will start over.
92 andreas 137
extern TPageManager *gPageManager;              //!< The pointer to the global defined main class.
38 andreas 138
static bool isRunning = false;                  //!< TRUE = the pageManager was started.
100 andreas 139
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
44 andreas 140
static double gScale = 1.0;                     //!< Global variable holding the scale factor.
141
static int gFullWidth = 0;                      //!< Global variable holding the width of the AMX screen. This is used to calculate the scale factor for the settings dialog.
155 andreas 142
std::atomic<double> mScaleFactorW{1.0};
143
std::atomic<double> mScaleFactorH{1.0};
144
int gScreenWidth{0};
145
int gScreenHeight{0};
264 andreas 146
bool isPortrait{false};
100 andreas 147
#endif
21 andreas 148
 
5 andreas 149
using std::bind;
296 andreas 150
using std::map;
61 andreas 151
using std::pair;
21 andreas 152
using std::string;
51 andreas 153
using std::vector;
5 andreas 154
 
107 andreas 155
string _NO_OBJECT = "The global class TObject is not available!";
156
 
21 andreas 157
/**
158
 * @brief qtmain is used here as the entry point for the surface.
159
 *
160
 * The main entry function parses the command line parameters, if there were
161
 * any and sets the basic attributes. It creates the main window and starts the
162
 * application.
163
 *
164
 * @param argc      The number of command line arguments
165
 * @param argv      A pointer to a 2 dimensional array containing the command
166
 *                  line parameters.
167
 * @param pmanager  A pointer to the page manager class which is the main class
168
 *                  managing everything.
169
 *
170
 * @return If no errors occured it returns 0.
171
 */
3 andreas 172
int qtmain(int argc, char **argv, TPageManager *pmanager)
2 andreas 173
{
31 andreas 174
    DECL_TRACER("qtmain(int argc, char **argv, TPageManager *pmanager)");
2 andreas 175
 
58 andreas 176
    if (!pmanager)
177
    {
178
        MSG_ERROR("Fatal: No pointer to the page manager received!");
179
        return 1;
180
    }
181
 
299 andreas 182
    gPageManager = pmanager;
88 andreas 183
#ifdef __ANDROID__
184
    MSG_INFO("Android API version: " << __ANDROID_API__);
3 andreas 185
 
256 andreas 186
#if __ANDROID_API__ < 30
258 andreas 187
#warning "The Android API version is less than 30! Some functions may not work!"
256 andreas 188
#endif  // __ANDROID_API__
264 andreas 189
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
256 andreas 190
    QAndroidJniObject activity = QtAndroid::androidActivity();
191
    QAndroidJniObject::callStaticMethod<void>("org/qtproject/theosys/HideToolbar", "hide", "(Landroid/app/Activity;Z)V", activity.object(), true);
192
#else
193
    QJniObject activity = QNativeInterface::QAndroidApplication::context();
194
    QJniObject::callStaticMethod<void>("org/qtproject/theosys/HideToolbar", "hide", "(Landroid/app/Activity;Z)V", activity.object(), true);
195
#endif  // QT5_LINUX
196
#endif  // __ANDROID__
197
 
264 andreas 198
#if defined(Q_OS_ANDROID)
38 andreas 199
    QApplication::setAttribute(Qt::AA_ForceRasterWidgets);
264 andreas 200
//    QApplication::setAttribute(Qt::AA_Use96Dpi);
178 andreas 201
//    QApplication::setAttribute(Qt::AA_DontUseNativeDialogs);
2 andreas 202
#endif
203
 
5 andreas 204
    QApplication app(argc, argv);
58 andreas 205
    // Set the orientation
206
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
207
    QScreen *screen = QGuiApplication::primaryScreen();
208
 
209
    if (!screen)
210
    {
211
        MSG_ERROR("Couldn't determine the primary screen!")
212
        return 1;
213
    }
264 andreas 214
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
151 andreas 215
    if (pmanager->getSettings()->isPortrait())  // portrait?
88 andreas 216
    {
217
        MSG_INFO("Orientation set to portrait mode.");
264 andreas 218
        screen->setOrientationUpdateMask(Qt::PortraitOrientation);
88 andreas 219
    }
220
    else
221
    {
222
        MSG_INFO("Orientation set to landscape mode.");
264 andreas 223
        screen->setOrientationUpdateMask(Qt::LandscapeOrientation);
88 andreas 224
    }
264 andreas 225
#endif  // QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
38 andreas 226
    double scale = 1.0;
212 andreas 227
    double setupScaleFactor = 1.0;
24 andreas 228
    // Calculate the scale factor
229
    if (TConfig::getScale())
230
    {
43 andreas 231
        // Because we've no window here we can not know on which screen, if
232
        // there are more than one, the application will start. Because on a
233
        // mobile mostly no external screen is connected, we take always the
234
        // resolution of the first (built in) screen.
235
        // TODO: Find a way to get the screen the application will start and
236
        // take this screen to calculate the scale factor.
264 andreas 237
        QRect screenGeometry = screen->availableGeometry();
88 andreas 238
        double width = 0.0;
239
        double height = 0.0;
212 andreas 240
        double sysWidth = 0.0;
241
        double sysHeight = 0.0;
264 andreas 242
        gScreenWidth = std::max(screenGeometry.width(), screenGeometry.height());
243
        gScreenHeight = std::min(screenGeometry.height(), screenGeometry.width());
217 andreas 244
 
264 andreas 245
        if (screenGeometry.width() > screenGeometry.height())
246
            isPortrait = false;
247
        else
248
            isPortrait = true;
217 andreas 249
 
250
        int minWidth = pmanager->getSettings()->getWidth();
24 andreas 251
        int minHeight = pmanager->getSettings()->getHeight();
217 andreas 252
        int minSysWidth = pmanager->getSystemSettings()->getWidth();
212 andreas 253
        int minSysHeight = pmanager->getSystemSettings()->getHeight();
88 andreas 254
 
151 andreas 255
        if (pmanager->getSettings()->isPortrait())  // portrait?
88 andreas 256
        {
217 andreas 257
            width = std::min(gScreenWidth, gScreenHeight);
258
            height = std::max(gScreenHeight, gScreenWidth);
88 andreas 259
        }
260
        else
261
        {
217 andreas 262
            width = std::max(gScreenWidth, gScreenHeight);
263
            height = std::min(gScreenHeight, gScreenWidth);
88 andreas 264
        }
212 andreas 265
        // The setup pages are always landscape
217 andreas 266
        sysWidth = std::max(gScreenWidth, gScreenHeight);
267
        sysHeight = std::min(gScreenHeight, gScreenWidth);
88 andreas 268
 
156 andreas 269
        if (!TConfig::getToolbarSuppress() && TConfig::getToolbarForce())
120 andreas 270
            minWidth += 48;
271
 
31 andreas 272
        MSG_INFO("Dimension of AMX screen:" << minWidth << " x " << minHeight);
88 andreas 273
        MSG_INFO("Screen size: " << width << " x " << height);
43 andreas 274
        // The scale factor is always calculated in difference to the prefered
275
        // size of the original AMX panel.
212 andreas 276
        mScaleFactorW = width / (double)minWidth;
277
        mScaleFactorH = height / (double)minHeight;
217 andreas 278
        double scaleFactorW = sysWidth / (double)minSysWidth;
279
        double scaleFactorH = sysHeight / (double)minSysHeight;
155 andreas 280
        scale = std::min(mScaleFactorW, mScaleFactorH);
212 andreas 281
        setupScaleFactor = std::min(scaleFactorW, scaleFactorH);
235 andreas 282
#ifdef __ANDROID__
212 andreas 283
        __android_log_print(ANDROID_LOG_DEBUG, "tpanel", "scale: %f (Screen: %1.0fx%1.0f, Page: %dx%d)", scale, width, height, minWidth, minHeight);
284
        __android_log_print(ANDROID_LOG_DEBUG, "tpanel", "setupScaleFactor: %f (Screen: %1.0fx%1.0f, Page: %dx%d)", setupScaleFactor, sysWidth, sysHeight, minSysWidth, minSysHeight);
235 andreas 285
#endif
43 andreas 286
        gScale = scale;     // The calculated scale factor
88 andreas 287
        gFullWidth = width;
155 andreas 288
        MSG_INFO("Calculated scale factor: " << scale);
43 andreas 289
        // This preprocessor variable allows the scaling to be done by the Skia
290
        // library, which is used to draw everything. In comparison to Qt this
291
        // library is a bit slower and sometimes does not honor the aspect ratio
44 andreas 292
        // correct. But in case there is another framework than Qt in use, this
43 andreas 293
        // could be necessary.
294
#ifdef _SCALE_SKIA_
26 andreas 295
        if (scale != 0.0)
296
        {
297
            pmanager->setScaleFactor(scale);
31 andreas 298
            MSG_INFO("Scale factor: " << scale);
26 andreas 299
        }
31 andreas 300
 
301
        if (scaleW != 0.0)
302
            pmanager->setScaleFactorWidth(scaleW);
303
 
304
        if (scaleH != 0.0)
305
            pmanager->setScaleFactorHeight(scaleH);
58 andreas 306
#endif  // _SCALE_SKIA_
24 andreas 307
    }
58 andreas 308
#endif  // defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
235 andreas 309
#if not defined(Q_OS_ANDROID) && not defined(Q_OS_IOS)
213 andreas 310
    double setupScaleFactor = 1.0;
222 andreas 311
 
312
    if (pmanager->getSettings() != pmanager->getSystemSettings())
211 andreas 313
    {
212 andreas 314
        double width = 0.0;
315
        double height = 0.0;
217 andreas 316
        int minWidth = pmanager->getSystemSettings()->getWidth();
212 andreas 317
        int minHeight = pmanager->getSystemSettings()->getHeight();
318
 
319
        if (!TConfig::getToolbarSuppress() && TConfig::getToolbarForce())
320
            minWidth += 48;
321
 
219 andreas 322
        width = std::max(pmanager->getSettings()->getWidth(), pmanager->getSettings()->getHeight());
323
        height = std::min(pmanager->getSettings()->getHeight(), pmanager->getSettings()->getWidth());
222 andreas 324
 
212 andreas 325
        MSG_INFO("Dimension of AMX screen:" << minWidth << " x " << minHeight);
326
        MSG_INFO("Screen size: " << width << " x " << height);
327
        // The scale factor is always calculated in difference to the prefered
328
        // size of the original AMX panel.
329
        double scaleFactorW = width / (double)minWidth;
330
        double scaleFactorH = height / (double)minHeight;
331
        setupScaleFactor = std::min(scaleFactorW, scaleFactorH);
265 andreas 332
        MSG_DEBUG("Scale factor for setup screen: " << setupScaleFactor);
198 andreas 333
    }
222 andreas 334
#endif
24 andreas 335
    // Initialize the application
164 andreas 336
    pmanager->setDPI(QGuiApplication::primaryScreen()->logicalDotsPerInch());
5 andreas 337
    QCoreApplication::setOrganizationName(TConfig::getProgName().c_str());
164 andreas 338
    QCoreApplication::setApplicationName("TPanel");
24 andreas 339
    QCoreApplication::setApplicationVersion(VERSION_STRING());
5 andreas 340
    QCommandLineParser parser;
341
    parser.setApplicationDescription(QCoreApplication::applicationName());
342
    parser.addHelpOption();
343
    parser.addVersionOption();
344
    parser.process(app);
2 andreas 345
 
5 andreas 346
    MainWindow mainWin;
58 andreas 347
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
348
#   ifndef _SCALE_SKIA_
43 andreas 349
    if (TConfig::getScale() && scale != 1.0)
38 andreas 350
        mainWin.setScaleFactor(scale);
58 andreas 351
#   endif   // _SCALE_SKIA_
352
#endif  // defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
22 andreas 353
    mainWin.setConfigFile(TConfig::getConfigPath() + "/" + TConfig::getConfigFileName());
299 andreas 354
    QPalette palette(mainWin.palette());
355
    palette.setColor(QPalette::Window, Qt::black);  // Keep the background black. Helps to save battery on OLED displays.
356
    mainWin.setPalette(palette);
58 andreas 357
    mainWin.grabGesture(Qt::PinchGesture);
153 andreas 358
    mainWin.grabGesture(Qt::SwipeGesture);
264 andreas 359
    mainWin.setOrientation(Qt::PrimaryOrientation);
360
//#ifdef Q_OS_IOS
361
 //   mainWin.setWindowFlag(Qt::MaximizeUsingFullscreenGeometryHint, true);
362
//#endif
198 andreas 363
    if (setupScaleFactor != 1.0 && setupScaleFactor > 0.0)
364
        mainWin.setSetupScaleFactor(setupScaleFactor);
365
 
5 andreas 366
    mainWin.show();
367
    return app.exec();
2 andreas 368
}
369
 
21 andreas 370
/**
371
 * @brief MainWindow::MainWindow constructor
372
 *
373
 * This method is the constructor for this class. It registers the callback
374
 * functions to the class TPageManager and starts the main run loop.
43 andreas 375
 *
376
 * Qt is used only to manage widgets to handle pages and subpages. A page as
377
 * well as a subpage may contain a background graphic and some elements. The
378
 * elements could be buttons, bargraphs and other objects. The underlying layer
379
 * draw every element as a ready graphic image and call a callback function to
380
 * let Qt display the graphic.
381
 * If there are some animations on a subpage defined, this is also handled by
382
 * Qt. Especialy sliding and fading.
21 andreas 383
 */
2 andreas 384
MainWindow::MainWindow()
385
{
5 andreas 386
    DECL_TRACER("MainWindow::MainWindow()");
59 andreas 387
 
296 andreas 388
    TObject::setParent(this);
389
 
264 andreas 390
    if (!gPageManager)
391
    {
392
        EXCEPTFATALMSG("The class TPageManager was not initialized!");
393
    }
394
 
323 andreas 395
    mGestureFilter = new TQGestureFilter(this);
396
    connect(mGestureFilter, &TQGestureFilter::gestureEvent, this, &MainWindow::onGestureEvent);
59 andreas 397
    setAttribute(Qt::WA_AcceptTouchEvents, true);   // We accept touch events
398
    grabGesture(Qt::PinchGesture);                  // We use a pinch gesture to open the settings dialog
153 andreas 399
    grabGesture(Qt::SwipeGesture);                  // We support swiping also
88 andreas 400
 
252 andreas 401
#ifdef Q_OS_IOS                                     // Block autorotate on IOS
402
    initGeoLocation();
263 andreas 403
    mIosRotate = new TIOSRotate;
404
    mIosRotate->automaticRotation(false);           // FIXME: This doesn't work!
252 andreas 405
 
406
    if (!mSensor)
407
    {
408
        mSensor = new QOrientationSensor(this);
409
 
410
        if (mSensor)
411
        {
412
            connect(mSensor, &QSensor::currentOrientationChanged, this, &MainWindow::onCurrentOrientationChanged);
263 andreas 413
            mSensor->setAxesOrientationMode(QSensor::UserOrientation);
414
            mSensor->setUserOrientation(180);
415
 
416
            if (gPageManager && gPageManager->getSettings()->isPortrait())
417
                mSensor->setCurrentOrientation(Qt::PortraitOrientation);
418
            else
419
                mSensor->setCurrentOrientation(Qt::LandscapeOrientation);
252 andreas 420
        }
421
    }
263 andreas 422
#endif  // Q_OS_IOS
264 andreas 423
 
424
    // We create the central widget here to make sure the application
425
    // initializes correct. On mobiles the whole screen is used while on
426
    // desktops a window with the necessary size is created.
266 andreas 427
    QWidget *central = new QWidget;
264 andreas 428
    central->setObjectName("centralWidget");
429
    central->setBackgroundRole(QPalette::Window);
271 andreas 430
#if defined(Q_OS_IOS) || defined(Q_OS_ANDROID)
277 andreas 431
//    central->setAutoFillBackground(false);
264 andreas 432
    central->setFixedSize(gScreenWidth, gScreenHeight);
433
#endif  // defined(Q_OS_IOS) || defined(Q_OSANDROID)
275 andreas 434
    setCentralWidget(central);      // Here we set the central widget
435
    central->show();
264 andreas 436
    // This is a stacked widget used to hold all pages. With it we can also
437
    // simply manage the objects bound to a page.
438
    mCentralWidget = new QStackedWidget(central);
266 andreas 439
    mCentralWidget->setObjectName("stackedPageWidgets");
265 andreas 440
#if defined(Q_OS_IOS) || defined(Q_OS_ANDROID)
264 andreas 441
    MSG_DEBUG("Size will be set for " << (isPortrait ? "PORTRAIT" : "LANDSCAPE"));
442
 
443
    if (gPageManager && gPageManager->getSettings()->isLandscape())
88 andreas 444
    {
264 andreas 445
        if (!isPortrait)
446
            mCentralWidget->setFixedSize((mToolbar ? gScreenWidth - mToolbar->width() : gScreenWidth), gScreenHeight);
447
        else
448
            mCentralWidget->setFixedSize(gScreenWidth, gScreenHeight);
449
    }
450
    else
451
    {
452
        if (isPortrait)
453
            mCentralWidget->setFixedSize((mToolbar ? gScreenHeight - mToolbar->width() : gScreenHeight), gScreenWidth);
454
        else
455
            mCentralWidget->setFixedSize(gScreenHeight, gScreenWidth);
456
    }
266 andreas 457
#else
270 andreas 458
    QSize qs = menuBar()->sizeHint();
266 andreas 459
    QSize icSize =  iconSize();
460
    int lwidth = gPageManager->getSettings()->getWidth() + icSize.width() + 16;
270 andreas 461
    int lheight = gPageManager->getSettings()->getHeight() + qs.height();
462
    mCentralWidget->setFixedSize(gPageManager->getSettings()->getWidth(), gPageManager->getSettings()->getHeight());
265 andreas 463
#endif  // defined(Q_OS_IOS) || defined(Q_OS_ANDROID)
275 andreas 464
 
266 andreas 465
#if defined(Q_OS_IOS) || defined(Q_OS_ANDROID)
264 andreas 466
    if (gPageManager->getSettings()->isPortrait())  // portrait?
467
    {
88 andreas 468
        MSG_INFO("Orientation set to portrait mode.");
469
        _setOrientation(O_PORTRAIT);
264 andreas 470
        mOrientation = Qt::PortraitOrientation;
88 andreas 471
    }
472
    else
473
    {
474
        MSG_INFO("Orientation set to landscape mode.");
475
        _setOrientation(O_LANDSCAPE);
264 andreas 476
        mOrientation = Qt::LandscapeOrientation;
88 andreas 477
    }
266 andreas 478
#else
479
    QRect rectMain = geometry();
480
    rectMain.setWidth(lwidth);
481
    rectMain.setHeight(lheight);
270 andreas 482
    setGeometry(rectMain);
266 andreas 483
    MSG_DEBUG("Height of main window:  " << rectMain.height());
484
    MSG_DEBUG("Height of panel screen: " << lheight);
485
    // If our first top pixel is not 0, maybe because of a menu, window
486
    // decorations or a toolbar, we must add this extra height to the
487
    // positions of widgets and mouse presses.
488
    int avHeight = rectMain.height() - gPageManager->getSettings()->getHeight();
489
    MSG_DEBUG("Difference in height:   " << avHeight);
490
    gPageManager->setFirstTopPixel(avHeight);
264 andreas 491
#endif
113 andreas 492
    setWindowIcon(QIcon(":images/icon.png"));
92 andreas 493
 
43 andreas 494
    // First we register all our surface callbacks to the underlying work
495
    // layer. All the graphics are drawn by the Skia library. The layer below
496
    // call the following functions to let Qt display the graphics on the
497
    // screen let it manage the widgets containing the graphics.
92 andreas 498
    gPageManager->registerCallbackDB(bind(&MainWindow::_displayButton, this,
5 andreas 499
                                       std::placeholders::_1,
500
                                       std::placeholders::_2,
501
                                       std::placeholders::_3,
502
                                       std::placeholders::_4,
503
                                       std::placeholders::_5,
504
                                       std::placeholders::_6,
298 andreas 505
                                       std::placeholders::_7,
506
                                       std::placeholders::_8));
5 andreas 507
 
280 andreas 508
    gPageManager->regDisplayViewButton(bind(&MainWindow::_displayViewButton, this,
509
                                          std::placeholders::_1,
510
                                          std::placeholders::_2,
511
                                          std::placeholders::_3,
512
                                          std::placeholders::_4,
513
                                          std::placeholders::_5,
514
                                          std::placeholders::_6,
515
                                          std::placeholders::_7,
516
                                          std::placeholders::_8,
285 andreas 517
                                          std::placeholders::_9,
518
                                          std::placeholders::_10));
280 andreas 519
 
281 andreas 520
    gPageManager->regAddViewButtonItems(bind(&MainWindow::_addViewButtonItems, this,
285 andreas 521
                                             std::placeholders::_1,
522
                                             std::placeholders::_2));
281 andreas 523
 
300 andreas 524
    gPageManager->regUpdateViewButton(bind(&MainWindow::_updateViewButton, this,
525
                                           std::placeholders::_1,
526
                                           std::placeholders::_2,
527
                                           std::placeholders::_3,
528
                                           std::placeholders::_4));
529
 
530
    gPageManager->regUpdateViewButtonItem(bind(&MainWindow::_updateViewButtonItem, this,
531
                                               std::placeholders::_1,
532
                                               std::placeholders::_2));
533
 
534
    gPageManager->regShowSubViewItem(bind(&MainWindow::_showViewButtonItem, this,
535
                                          std::placeholders::_1,
536
                                          std::placeholders::_2,
537
                                          std::placeholders::_3,
538
                                          std::placeholders::_4));
539
 
318 andreas 540
    gPageManager->regHideAllSubViewItems(bind(&MainWindow::_hideAllViewItems, this, std::placeholders::_1));
541
    gPageManager->regHideSubViewItem(bind(&MainWindow::_hideViewItem, this, std::placeholders::_1, std::placeholders::_2));
542
    gPageManager->regSetSubViewPadding(bind(&MainWindow::_setSubViewPadding, this, std::placeholders::_1, std::placeholders::_2));
543
 
92 andreas 544
    gPageManager->registerCallbackSP(bind(&MainWindow::_setPage, this,
5 andreas 545
                                         std::placeholders::_1,
546
                                         std::placeholders::_2,
547
                                         std::placeholders::_3));
548
 
92 andreas 549
    gPageManager->registerCallbackSSP(bind(&MainWindow::_setSubPage, this,
5 andreas 550
                                          std::placeholders::_1,
551
                                          std::placeholders::_2,
552
                                          std::placeholders::_3,
553
                                          std::placeholders::_4,
41 andreas 554
                                          std::placeholders::_5,
217 andreas 555
                                          std::placeholders::_6,
556
                                          std::placeholders::_7));
5 andreas 557
 
92 andreas 558
    gPageManager->registerCallbackSB(bind(&MainWindow::_setBackground, this,
5 andreas 559
                                         std::placeholders::_1,
560
                                         std::placeholders::_2,
561
                                         std::placeholders::_3,
562
                                         std::placeholders::_4,
262 andreas 563
#ifdef _OPAQUE_SKIA_
289 andreas 564
                                         std::placeholders::_5));
262 andreas 565
#else
289 andreas 566
                                         std::placeholders::_5,
567
                                         std::placeholders::_6));
262 andreas 568
#endif
92 andreas 569
    gPageManager->regCallDropPage(bind(&MainWindow::_dropPage, this, std::placeholders::_1));
350 andreas 570
    gPageManager->regCallDropSubPage(bind(&MainWindow::_dropSubPage, this, std::placeholders::_1, std::placeholders::_2));
92 andreas 571
    gPageManager->regCallPlayVideo(bind(&MainWindow::_playVideo, this,
21 andreas 572
                                       std::placeholders::_1,
573
                                       std::placeholders::_2,
574
                                       std::placeholders::_3,
575
                                       std::placeholders::_4,
576
                                       std::placeholders::_5,
577
                                       std::placeholders::_6,
578
                                       std::placeholders::_7,
579
                                       std::placeholders::_8,
580
                                       std::placeholders::_9));
192 andreas 581
    gPageManager->regCallInputText(bind(&MainWindow::_inputText, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
200 andreas 582
    gPageManager->regCallListBox(bind(&MainWindow::_listBox, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
190 andreas 583
    gPageManager->registerDropButton(bind(&MainWindow::_dropButton, this, std::placeholders::_1));
92 andreas 584
    gPageManager->regCallbackKeyboard(bind(&MainWindow::_showKeyboard, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
585
    gPageManager->regCallbackKeypad(bind(&MainWindow::_showKeypad, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
586
    gPageManager->regCallResetKeyboard(bind(&MainWindow::_resetKeyboard, this));
587
    gPageManager->regCallShowSetup(bind(&MainWindow::_showSetup, this));
588
    gPageManager->regCallbackResetSurface(bind(&MainWindow::_resetSurface, this));
589
    gPageManager->regCallbackShutdown(bind(&MainWindow::_shutdown, this));
590
    gPageManager->regCallbackPlaySound(bind(&MainWindow::_playSound, this, std::placeholders::_1));
141 andreas 591
    gPageManager->regCallbackStopSound(bind(&MainWindow::_stopSound, this));
592
    gPageManager->regCallbackMuteSound(bind(&MainWindow::_muteSound, this, std::placeholders::_1));
335 andreas 593
    gPageManager->regCallbackSetVolume(bind(&MainWindow::_setVolume, this, std::placeholders::_1));
98 andreas 594
    gPageManager->registerCBsetVisible(bind(&MainWindow::_setVisible, this, std::placeholders::_1, std::placeholders::_2));
111 andreas 595
    gPageManager->regSendVirtualKeys(bind(&MainWindow::_sendVirtualKeys, this, std::placeholders::_1));
140 andreas 596
    gPageManager->regShowPhoneDialog(bind(&MainWindow::_showPhoneDialog, this, std::placeholders::_1));
597
    gPageManager->regSetPhoneNumber(bind(&MainWindow::_setPhoneNumber, this, std::placeholders::_1));
598
    gPageManager->regSetPhoneStatus(bind(&MainWindow::_setPhoneStatus, this, std::placeholders::_1));
141 andreas 599
    gPageManager->regSetPhoneState(bind(&MainWindow::_setPhoneState, this, std::placeholders::_1, std::placeholders::_2));
130 andreas 600
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
601
    gPageManager->regOnOrientationChange(bind(&MainWindow::_orientationChanged, this, std::placeholders::_1));
259 andreas 602
    gPageManager->regOnSettingsChanged(bind(&MainWindow::_activateSettings, this, std::placeholders::_1,
603
                                            std::placeholders::_2,
604
                                            std::placeholders::_3,
605
                                            std::placeholders::_4,
606
                                            std::placeholders::_5,
607
                                            std::placeholders::_6));
130 andreas 608
#endif
142 andreas 609
    gPageManager->regRepaintWindows(bind(&MainWindow::_repaintWindows, this));
151 andreas 610
    gPageManager->regToFront(bind(&MainWindow::_toFront, this, std::placeholders::_1));
252 andreas 611
#if !defined (Q_OS_ANDROID) && !defined(Q_OS_IOS)
197 andreas 612
    gPageManager->regSetMainWindowSize(bind(&MainWindow::_setSizeMainWindow, this, std::placeholders::_1, std::placeholders::_2));
211 andreas 613
#endif
206 andreas 614
    gPageManager->regDownloadSurface(bind(&MainWindow::_downloadSurface, this, std::placeholders::_1, std::placeholders::_2));
615
    gPageManager->regDisplayMessage(bind(&MainWindow::_displayMessage, this, std::placeholders::_1, std::placeholders::_2));
209 andreas 616
    gPageManager->regFileDialogFunction(bind(&MainWindow::_fileDialog, this,
617
                                             std::placeholders::_1,
618
                                             std::placeholders::_2,
619
                                             std::placeholders::_3,
620
                                             std::placeholders::_4));
260 andreas 621
    gPageManager->regStartWait(bind(&MainWindow::_startWait, this, std::placeholders::_1));
622
    gPageManager->regStopWait(bind(&MainWindow::_stopWait, this));
271 andreas 623
    gPageManager->regPageFinished(bind(&MainWindow::_pageFinished, this, std::placeholders::_1));
92 andreas 624
    gPageManager->deployCallbacks();
5 andreas 625
 
264 andreas 626
    createActions();        // Create the toolbar, if enabled by settings.
627
 
2 andreas 628
#ifndef QT_NO_SESSIONMANAGER
264 andreas 629
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
5 andreas 630
    QGuiApplication::setFallbackSessionManagementEnabled(false);
631
    connect(qApp, &QGuiApplication::commitDataRequest,
632
            this, &MainWindow::writeSettings);
264 andreas 633
#endif  // QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
634
#endif  // QT_NO_SESSIONMANAGER
635
 
43 andreas 636
    // Some types used to transport data from the layer below.
14 andreas 637
    qRegisterMetaType<size_t>("size_t");
638
    qRegisterMetaType<QByteArray>("QByteArray");
41 andreas 639
    qRegisterMetaType<ANIMATION_t>("ANIMATION_t");
291 andreas 640
    qRegisterMetaType<Button::TButton*>("Button::TButton*");
62 andreas 641
    qRegisterMetaType<std::string>("std::string");
279 andreas 642
    qRegisterMetaType<SUBVIEWLIST_T>("SUBVIEWLIST_T");
283 andreas 643
    qRegisterMetaType<PGSUBVIEWITEM_T>("PGSUBVIEWITEM_T");
303 andreas 644
    qRegisterMetaType<PGSUBVIEWITEM_T>("PGSUBVIEWITEM_T&");
283 andreas 645
    qRegisterMetaType<PGSUBVIEWATOM_T>("PGSUBVIEWATOM_T");
646
    qRegisterMetaType<TBitmap>("TBitmap");
14 andreas 647
 
43 andreas 648
    // All the callback functions doesn't act directly. Instead they emit an
649
    // event. Then Qt decides whether the real function is started directly and
650
    // immediately or if the call is queued and called later in a thread. To
651
    // handle this we're "connecting" the real functions to some signals.
15 andreas 652
    try
653
    {
654
        connect(this, &MainWindow::sigDisplayButton, this, &MainWindow::displayButton);
280 andreas 655
        connect(this, &MainWindow::sigDisplayViewButton, this, &MainWindow::displayViewButton);
656
        connect(this, &MainWindow::sigAddViewButtonItems, this, &MainWindow::addViewButtonItems);
303 andreas 657
        connect(this, &MainWindow::sigShowViewButtonItem, this, &MainWindow::showViewButtonItem);
658
        connect(this, &MainWindow::sigUpdateViewButton, this, &MainWindow::updateViewButton);
659
        connect(this, &MainWindow::sigUpdateViewButtonItem, this, &MainWindow::updateViewButtonItem);
15 andreas 660
        connect(this, &MainWindow::sigSetPage, this, &MainWindow::setPage);
661
        connect(this, &MainWindow::sigSetSubPage, this, &MainWindow::setSubPage);
662
        connect(this, &MainWindow::sigSetBackground, this, &MainWindow::setBackground);
663
        connect(this, &MainWindow::sigDropPage, this, &MainWindow::dropPage);
664
        connect(this, &MainWindow::sigDropSubPage, this, &MainWindow::dropSubPage);
21 andreas 665
        connect(this, &MainWindow::sigPlayVideo, this, &MainWindow::playVideo);
50 andreas 666
        connect(this, &MainWindow::sigInputText, this, &MainWindow::inputText);
200 andreas 667
        connect(this, &MainWindow::sigListBox, this, &MainWindow::listBox);
62 andreas 668
        connect(this, &MainWindow::sigKeyboard, this, &MainWindow::showKeyboard);
669
        connect(this, &MainWindow::sigKeypad, this, &MainWindow::showKeypad);
64 andreas 670
        connect(this, &MainWindow::sigShowSetup, this, &MainWindow::showSetup);
71 andreas 671
        connect(this, &MainWindow::sigPlaySound, this, &MainWindow::playSound);
335 andreas 672
        connect(this, &MainWindow::sigSetVolume, this, &MainWindow::setVolume);
98 andreas 673
        connect(this, &MainWindow::sigDropButton, this, &MainWindow::dropButton);
674
        connect(this, &MainWindow::sigSetVisible, this, &MainWindow::SetVisible);
111 andreas 675
        connect(this, &MainWindow::sigSendVirtualKeys, this, &MainWindow::sendVirtualKeys);
140 andreas 676
        connect(this, &MainWindow::sigShowPhoneDialog, this, &MainWindow::showPhoneDialog);
677
        connect(this, &MainWindow::sigSetPhoneNumber, this, &MainWindow::setPhoneNumber);
678
        connect(this, &MainWindow::sigSetPhoneStatus, this, &MainWindow::setPhoneStatus);
141 andreas 679
        connect(this, &MainWindow::sigSetPhoneState, this, &MainWindow::setPhoneState);
142 andreas 680
        connect(this, &MainWindow::sigRepaintWindows, this, &MainWindow::repaintWindows);
151 andreas 681
        connect(this, &MainWindow::sigToFront, this, &MainWindow::toFront);
179 andreas 682
        connect(this, &MainWindow::sigOnProgressChanged, this, &MainWindow::onProgressChanged);
252 andreas 683
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
197 andreas 684
        connect(this, &MainWindow::sigSetSizeMainWindow, this, &MainWindow::setSizeMainWindow);
211 andreas 685
#endif
206 andreas 686
        connect(this, &MainWindow::sigDownloadSurface, this, &MainWindow::downloadSurface);
687
        connect(this, &MainWindow::sigDisplayMessage, this, &MainWindow::displayMessage);
209 andreas 688
        connect(this, &MainWindow::sigFileDialog, this, &MainWindow::fileDialog);
260 andreas 689
        connect(this, &MainWindow::sigStartWait, this, &MainWindow::startWait);
690
        connect(this, &MainWindow::sigStopWait, this, &MainWindow::stopWait);
271 andreas 691
        connect(this, &MainWindow::sigPageFinished, this, &MainWindow::pageFinished);
213 andreas 692
        connect(qApp, &QGuiApplication::applicationStateChanged, this, &MainWindow::onAppStateChanged);
130 andreas 693
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
694
        QScreen *screen = QGuiApplication::primaryScreen();
695
        connect(screen, &QScreen::orientationChanged, this, &MainWindow::onScreenOrientationChanged);
259 andreas 696
        connect(this, &MainWindow::sigActivateSettings, this, &MainWindow::activateSettings);
130 andreas 697
#endif
15 andreas 698
    }
699
    catch (std::exception& e)
700
    {
701
        MSG_ERROR("Connection error: " << e.what());
702
    }
93 andreas 703
    catch(...)
704
    {
705
        MSG_ERROR("Unexpected exception occured [MainWindow::MainWindow()]");
706
    }
15 andreas 707
 
5 andreas 708
    setUnifiedTitleAndToolBarOnMac(true);
182 andreas 709
#ifdef Q_OS_ANDROID
61 andreas 710
    // At least initialize the phone call listener
711
    if (gPageManager)
712
        gPageManager->initPhoneState();
92 andreas 713
 
93 andreas 714
    /*
715
     * In case this class was created not the first time, we initiate a small
716
     * thread to send the signal ApplicationActive to initiate the communication
717
     * with the controller again. This also starts the page manager thread
718
     * (TPageManager), which handles all elements of the surface.
719
     */
92 andreas 720
    if (_restart_ && gPageManager)
721
    {
93 andreas 722
        try
723
        {
724
            std::thread mThread = std::thread([=] { this->_signalState(Qt::ApplicationActive); });
725
            mThread.detach();   // We detach immediately to leave this method.
726
        }
727
        catch (std::exception& e)
728
        {
729
            MSG_ERROR("Error starting the thread to reinvoke communication!");
730
        }
731
        catch(...)
732
        {
733
            MSG_ERROR("Unexpected exception occured [MainWindow::MainWindow()]");
734
        }
92 andreas 735
    }
93 andreas 736
#endif
247 andreas 737
#ifdef Q_OS_IOS
738
    // To get the battery level periodicaly we setup a timer.
739
    if (!mIosBattery)
740
        mIosBattery = new TIOSBattery;
741
 
742
    mIosBattery->update();
743
 
264 andreas 744
    int left = mIosBattery->getBatteryLeft();
745
    int state = mIosBattery->getBatteryState();
746
    // At this point no buttons are registered and therefore the battery
747
    // state will not be visible. To have the state at the moment a button
748
    // is registered, we tell the page manager to store the values.
749
    gPageManager->setBattery(left, state);
750
    MSG_DEBUG("Battery state was set to " << left << "% and state " << state);
751
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
752
    if (gPageManager->getSettings()->isPortrait())
753
        QGuiApplication::primaryScreen()->setOrientationUpdateMask(Qt::PortraitOrientation | Qt::InvertedPortraitOrientation);
754
    else
755
        QGuiApplication::primaryScreen()->setOrientationUpdateMask(Qt::LandscapeOrientation | Qt::InvertedLandscapeOrientation);
756
#endif  // QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
247 andreas 757
#endif  // Q_OS_IOS
758
 
92 andreas 759
    _restart_ = false;
2 andreas 760
}
761
 
34 andreas 762
MainWindow::~MainWindow()
763
{
764
    DECL_TRACER("MainWindow::~MainWindow()");
40 andreas 765
 
34 andreas 766
    killed = true;
767
    prg_stopped = true;
296 andreas 768
 
769
    disconnect(this, &MainWindow::sigDisplayButton, this, &MainWindow::displayButton);
770
    disconnect(this, &MainWindow::sigDisplayViewButton, this, &MainWindow::displayViewButton);
771
    disconnect(this, &MainWindow::sigAddViewButtonItems, this, &MainWindow::addViewButtonItems);
772
    disconnect(this, &MainWindow::sigSetPage, this, &MainWindow::setPage);
773
    disconnect(this, &MainWindow::sigSetSubPage, this, &MainWindow::setSubPage);
774
    disconnect(this, &MainWindow::sigSetBackground, this, &MainWindow::setBackground);
775
    disconnect(this, &MainWindow::sigDropPage, this, &MainWindow::dropPage);
776
    disconnect(this, &MainWindow::sigDropSubPage, this, &MainWindow::dropSubPage);
777
    disconnect(this, &MainWindow::sigPlayVideo, this, &MainWindow::playVideo);
778
    disconnect(this, &MainWindow::sigInputText, this, &MainWindow::inputText);
779
    disconnect(this, &MainWindow::sigListBox, this, &MainWindow::listBox);
780
    disconnect(this, &MainWindow::sigKeyboard, this, &MainWindow::showKeyboard);
781
    disconnect(this, &MainWindow::sigKeypad, this, &MainWindow::showKeypad);
782
    disconnect(this, &MainWindow::sigShowSetup, this, &MainWindow::showSetup);
783
    disconnect(this, &MainWindow::sigPlaySound, this, &MainWindow::playSound);
784
    disconnect(this, &MainWindow::sigDropButton, this, &MainWindow::dropButton);
785
    disconnect(this, &MainWindow::sigSetVisible, this, &MainWindow::SetVisible);
786
    disconnect(this, &MainWindow::sigSendVirtualKeys, this, &MainWindow::sendVirtualKeys);
787
    disconnect(this, &MainWindow::sigShowPhoneDialog, this, &MainWindow::showPhoneDialog);
788
    disconnect(this, &MainWindow::sigSetPhoneNumber, this, &MainWindow::setPhoneNumber);
789
    disconnect(this, &MainWindow::sigSetPhoneStatus, this, &MainWindow::setPhoneStatus);
790
    disconnect(this, &MainWindow::sigSetPhoneState, this, &MainWindow::setPhoneState);
791
    disconnect(this, &MainWindow::sigRepaintWindows, this, &MainWindow::repaintWindows);
792
    disconnect(this, &MainWindow::sigToFront, this, &MainWindow::toFront);
793
    disconnect(this, &MainWindow::sigOnProgressChanged, this, &MainWindow::onProgressChanged);
794
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
795
    disconnect(this, &MainWindow::sigSetSizeMainWindow, this, &MainWindow::setSizeMainWindow);
796
#endif
797
    disconnect(this, &MainWindow::sigDownloadSurface, this, &MainWindow::downloadSurface);
798
    disconnect(this, &MainWindow::sigDisplayMessage, this, &MainWindow::displayMessage);
799
    disconnect(this, &MainWindow::sigFileDialog, this, &MainWindow::fileDialog);
800
    disconnect(this, &MainWindow::sigStartWait, this, &MainWindow::startWait);
801
    disconnect(this, &MainWindow::sigStopWait, this, &MainWindow::stopWait);
802
    disconnect(this, &MainWindow::sigPageFinished, this, &MainWindow::pageFinished);
803
    disconnect(qApp, &QGuiApplication::applicationStateChanged, this, &MainWindow::onAppStateChanged);
804
 
254 andreas 805
#ifdef Q_OS_IOS
252 andreas 806
    if (mSource)
807
    {
808
        delete mSource;
809
        mSource = nullptr;
810
    }
254 andreas 811
#endif
141 andreas 812
    if (mMediaPlayer)
181 andreas 813
    {
264 andreas 814
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
181 andreas 815
        delete mAudioOutput;
816
#endif
141 andreas 817
        delete mMediaPlayer;
181 andreas 818
    }
141 andreas 819
 
34 andreas 820
    if (gAmxNet && !gAmxNet->isStopped())
821
        gAmxNet->stop();
90 andreas 822
 
823
    if (mToolbar)
824
    {
825
        removeToolBar(mToolbar);
826
        mToolbar = nullptr;
827
    }
828
 
829
    isRunning = false;
247 andreas 830
#ifdef Q_OS_IOS
252 andreas 831
    if (mIosRotate)
832
        mIosRotate->automaticRotation(true);
247 andreas 833
 
834
    if (mIosBattery)
835
    {
836
        delete mIosBattery;
837
        mIosBattery = nullptr;
838
    }
252 andreas 839
 
840
    if (mIosRotate)
841
        delete mIosRotate;
247 andreas 842
#endif
323 andreas 843
 
844
    if (mGestureFilter)
845
    {
846
        disconnect(mGestureFilter, &TQGestureFilter::gestureEvent, this, &MainWindow::onGestureEvent);
847
        delete mGestureFilter;
848
    }
34 andreas 849
}
850
 
235 andreas 851
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
21 andreas 852
/**
93 andreas 853
 * @brief Small thread to invoke the initialization on an Android device.
854
 *
855
 * On Android devices the signal ApplicationActive is not send if the class
856
 * \p MainWindow is destroyed and recreated. Therefore we need this little
857
 * helper to send the signal when the class is initialized.
858
 *
859
 * @param state     This defines the signal to send.
860
 */
861
void MainWindow::_signalState(Qt::ApplicationState state)
862
{
863
    DECL_TRACER("MainWindow::_signalState(Qt::ApplicationState state)");
864
 
865
    std::this_thread::sleep_for(std::chrono::seconds(1));   // Wait a second
213 andreas 866
    onAppStateChanged(state);
93 andreas 867
}
130 andreas 868
 
869
void MainWindow::_orientationChanged(int orientation)
870
{
871
    DECL_TRACER("MainWindow::_orientationChanged(int orientation)");
872
 
264 andreas 873
    if (gPageManager && gPageManager->getSettings()->isPortrait())  // portrait?
131 andreas 874
    {
875
        if (orientation == O_REVERSE_PORTRAIT && mOrientation != Qt::InvertedPortraitOrientation)
245 andreas 876
        {
131 andreas 877
            _setOrientation((J_ORIENTATION)orientation);
245 andreas 878
            mOrientation = Qt::InvertedPortraitOrientation;
879
        }
131 andreas 880
        else if (orientation == O_PORTRAIT && mOrientation != Qt::PortraitOrientation)
245 andreas 881
        {
131 andreas 882
            _setOrientation((J_ORIENTATION)orientation);
245 andreas 883
            mOrientation = Qt::PortraitOrientation;
884
        }
131 andreas 885
    }
886
    else
887
    {
888
        if (orientation == O_REVERSE_LANDSCAPE && mOrientation != Qt::InvertedLandscapeOrientation)
245 andreas 889
        {
131 andreas 890
            _setOrientation((J_ORIENTATION)orientation);
245 andreas 891
            mOrientation = Qt::InvertedLandscapeOrientation;
892
        }
131 andreas 893
        else if (orientation == O_LANDSCAPE && mOrientation != Qt::LandscapeOrientation)
245 andreas 894
        {
131 andreas 895
            _setOrientation((J_ORIENTATION)orientation);
245 andreas 896
            mOrientation = Qt::LandscapeOrientation;
897
        }
131 andreas 898
    }
130 andreas 899
}
93 andreas 900
 
259 andreas 901
void MainWindow::_activateSettings(const std::string& oldNetlinx, int oldPort, int oldChannelID, const std::string& oldSurface, bool oldToolbarSuppress, bool oldToolbarForce)
902
{
903
    DECL_TRACER("MainWindow::_activateSettings(const std::string& oldNetlinx, int oldPort, int oldChannelID, const std::string& oldSurface, bool oldToolbarSuppress, bool oldToolbarForce)");
904
 
905
    emit sigActivateSettings(oldNetlinx, oldPort, oldChannelID, oldSurface, oldToolbarSuppress, oldToolbarForce);
906
}
907
 
217 andreas 908
/**
259 andreas 909
 * @brief MainWindow::activateSettings
910
 * This method activates some urgent settings. It is called on Android and IOS
911
 * after the setup dialog was closed. The method expects some values taken
912
 * immediately before the setup dialog was started. If takes some actions like
913
 * downloading a surface when the setting for it changed or removes the
914
 * toolbar on the right if the user reuqsted it.
915
 *
916
 * @param oldNetlinx        The IP or name of the Netlinx
917
 * @param oldPort           The network port number used to connect to Netlinx
918
 * @param oldChannelID      The channel ID TPanel uses to identify against the
919
 *                          Netlinx.
920
 * @param oldSurface        The name of theprevious TP4 file.
921
 * @param oldToolbarSuppress    State of toolbar suppress switch
922
 * @param oldToolbarForce   The state of toolbar force switch
923
 */
924
void MainWindow::activateSettings(const std::string& oldNetlinx, int oldPort, int oldChannelID, const std::string& oldSurface, bool oldToolbarSuppress, bool oldToolbarForce)
925
{
926
    DECL_TRACER("MainWindow::activateSettings(const std::string& oldNetlinx, int oldPort, int oldChannelID, const std::string& oldSurface, bool oldToolbarSuppress, bool oldToolbarForce)");
927
 
928
#ifdef Q_OS_IOS
929
    TConfig cf(TConfig::getConfigPath() + "/" + TConfig::getConfigFileName());
930
#endif
931
    bool rebootAnyway = false;
260 andreas 932
    bool doDownload = false;
259 andreas 933
    string newSurface = TConfig::getFtpSurface();
934
 
935
    if (!TConfig::getToolbarSuppress() && oldToolbarForce != TConfig::getToolbarForce())
936
    {
937
        QMessageBox msgBox(this);
938
        msgBox.setText("The change for the visibility of the toolbar will be active on the next start of TPanel!");
939
        msgBox.exec();
940
    }
941
    else if (oldToolbarSuppress != TConfig::getToolbarSuppress() && TConfig::getToolbarSuppress())
942
    {
943
        if (mToolbar)
944
        {
945
            mToolbar->close();
946
            delete mToolbar;
947
            mToolbar = nullptr;
948
        }
949
    }
260 andreas 950
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
259 andreas 951
    if (newSurface != oldSurface || TTPInit::haveSystemMarker())
952
#else
262 andreas 953
    time_t dlTime = TConfig::getFtpDownloadTime();
954
    time_t actTime = time(NULL);
955
 
259 andreas 956
    if (newSurface != oldSurface || dlTime == 0 || dlTime < (actTime - 60))
957
#endif
958
    {
959
        MSG_DEBUG("Surface should be downloaded (Old: " << oldSurface << ", New: " << newSurface << ")");
960
 
961
        QMessageBox msgBox(this);
962
        msgBox.setText(QString("Should the surface <b>") + newSurface.c_str() + "</b> be installed?");
963
        msgBox.addButton(QMessageBox::Yes);
964
        msgBox.addButton(QMessageBox::No);
965
        int ret = msgBox.exec();
966
 
967
        if (ret == QMessageBox::Yes)
968
        {
260 andreas 969
            doDownload = true;
259 andreas 970
            TTPInit tpinit;
971
            std::vector<TTPInit::FILELIST_t> mFileList;
972
            // Get the list of TP4 files from NetLinx, if there are any.
260 andreas 973
            TQtWait waitBox(this, string("Please wait while I'm looking at the disc of Netlinx (") + TConfig::getController().c_str() + ") for TP4 files ...");
974
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
975
            waitBox.setScaleFactor(mScaleFactor);
976
            waitBox.doResize();
977
            waitBox.start();
978
#endif
259 andreas 979
            tpinit.setPath(TConfig::getProjectPath());
980
            tpinit.regCallbackProcessEvents(bind(&MainWindow::runEvents, this));
981
            tpinit.regCallbackProgressBar(bind(&MainWindow::_onProgressChanged, this, std::placeholders::_1));
982
            mFileList = tpinit.getFileList(".tp4");
983
            bool found = false;
984
 
985
            if (mFileList.size() > 0)
986
            {
987
                vector<TTPInit::FILELIST_t>::iterator iter;
988
 
989
                for (iter = mFileList.begin(); iter != mFileList.end(); ++iter)
990
                {
991
                    if (iter->fname == newSurface)
992
                    {
993
                        tpinit.setFileSize(iter->size);
994
                        found = true;
995
                        break;
996
                    }
997
                }
998
            }
999
 
260 andreas 1000
            waitBox.end();
259 andreas 1001
 
1002
            if (found)
1003
            {
1004
                string msg = "Loading file <b>" + newSurface + "</b>.";
1005
                MSG_DEBUG("Download of surface " << newSurface << " was forced!");
1006
 
1007
                downloadBar(msg, this);
1008
 
1009
                if (tpinit.loadSurfaceFromController(true))
1010
                    rebootAnyway = true;
1011
 
1012
                mDownloadBar->close();
1013
                mBusy = false;
1014
            }
1015
            else
1016
            {
1017
                MSG_PROTOCOL("The surface " << newSurface << " does not exist on NetLinx or the NetLinx " << TConfig::getController() << " was not found!");
1018
                displayMessage("The surface " + newSurface + " does not exist on NetLinx or the NetLinx " + TConfig::getController() + " was not found!", "Information");
1019
            }
1020
        }
1021
    }
1022
 
260 andreas 1023
    if (doDownload &&
1024
        (TConfig::getController() != oldNetlinx ||
259 andreas 1025
        TConfig::getChannel() != oldChannelID ||
260 andreas 1026
        TConfig::getPort() != oldPort || rebootAnyway))
259 andreas 1027
    {
1028
        // Start over by exiting this class
1029
        MSG_INFO("Program will start over!");
1030
        _restart_ = true;
1031
        prg_stopped = true;
1032
        killed = true;
1033
 
1034
        if (gAmxNet)
1035
            gAmxNet->stop();
1036
 
1037
        close();
1038
    }
1039
#ifdef Q_OS_ANDROID
1040
    else
1041
    {
1042
        TConfig cf(TConfig::getConfigPath() + "/" + TConfig::getConfigFileName());
1043
    }
1044
#endif
1045
}
1046
 
1047
/**
217 andreas 1048
 * @brief MainWindow::_freezeWorkaround: A workaround for the screen freeze.
271 andreas 1049
 * On Mobiles the screen sometimes stay frozen after the application state
1050
 * changes to ACTIVE or some yet unidentified things happened.
217 andreas 1051
 * The workaround produces a faked geometry change which makes the Qt framework
271 andreas 1052
 * to reattach to the screen.
1053
 * There may be situations where this workaround could trigger a repaint of all
1054
 * objects on the screen but then the surface is still frozen. At the moment of
1055
 * writing this comment I have no workaround or even an explanation for this.
217 andreas 1056
 */
264 andreas 1057
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
271 andreas 1058
/**
1059
 * @brief MainWindow::_freezeWorkaround
1060
 * This hack was made from Thomas Andersen. You'll find it at:
1061
 * https://bugreports.qt.io/browse/QTBUG-76142
1062
 */
217 andreas 1063
void MainWindow::_freezeWorkaround()
1064
{
1065
    DECL_TRACER("MainWindow::_freezeWorkaround()");
1066
 
1067
    QScreen* scr = QGuiApplication::screens().first();
1068
    QPlatformScreen* l_pScr = scr->handle(); /*QAndroidPlatformScreen*/
1069
    QRect l_geomHackAdjustedRect = l_pScr->availableGeometry();
1070
    QRect l_geomHackRect = l_geomHackAdjustedRect;
271 andreas 1071
    l_geomHackAdjustedRect.adjust(0, 0, 0, 5);
217 andreas 1072
    QMetaObject::invokeMethod(dynamic_cast<QObject*>(l_pScr), "setAvailableGeometry", Qt::DirectConnection, Q_ARG( const QRect &, l_geomHackAdjustedRect ));
1073
    QMetaObject::invokeMethod(dynamic_cast<QObject*>(l_pScr), "setAvailableGeometry", Qt::QueuedConnection, Q_ARG( const QRect &, l_geomHackRect ));
1074
}
271 andreas 1075
#endif  // QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
235 andreas 1076
#endif  // Q_OS_ANDROID || Q_OS_IOS
217 andreas 1077
 
142 andreas 1078
void MainWindow::_repaintWindows()
1079
{
1080
    DECL_TRACER("MainWindow::_repaintWindows()");
1081
 
156 andreas 1082
    emit sigRepaintWindows();
142 andreas 1083
}
151 andreas 1084
 
1085
void MainWindow::_toFront(ulong handle)
1086
{
1087
    DECL_TRACER("MainWindow::_toFront(ulong handle)");
1088
 
156 andreas 1089
    emit sigToFront(handle);
151 andreas 1090
}
1091
 
206 andreas 1092
void MainWindow::_downloadSurface(const string &file, size_t size)
1093
{
1094
    DECL_TRACER("MainWindow::_downloadSurface(const string &file, size_t size)");
1095
 
1096
    emit sigDownloadSurface(file, size);
1097
}
1098
 
260 andreas 1099
void MainWindow::_startWait(const string& text)
1100
{
1101
    DECL_TRACER("MainWindow::_startWait(const string& text)");
1102
 
1103
    emit sigStartWait(text);
1104
}
1105
 
1106
void MainWindow::_stopWait()
1107
{
1108
    DECL_TRACER("MainWindow::_stopWait()");
1109
 
1110
    emit sigStopWait();
1111
}
1112
 
271 andreas 1113
void MainWindow::_pageFinished(uint handle)
1114
{
1115
    DECL_TRACER("MainWindow::_pageFinished(uint handle)");
1116
 
1117
    emit sigPageFinished(handle);
1118
}
1119
 
93 andreas 1120
/**
21 andreas 1121
 * @brief MainWindow::closeEvent called when the application receives an exit event.
1122
 *
1123
 * If the user clicks on the exit icon or on the menu point _Exit_ this method
1124
 * is called. It makes sure everything is written to the configuration file
1125
 * and accepts the evernt.
1126
 *
1127
 * @param event The exit event
1128
 */
2 andreas 1129
void MainWindow::closeEvent(QCloseEvent *event)
1130
{
5 andreas 1131
    DECL_TRACER("MainWindow::closeEvent(QCloseEvent *event)");
24 andreas 1132
#ifdef __ANDROID__
23 andreas 1133
    __android_log_print(ANDROID_LOG_DEBUG,"tpanel","Close event; settingsChanged=%s", (settingsChanged ? "true":"false"));
24 andreas 1134
#endif
5 andreas 1135
    if (settingsChanged)
1136
    {
1137
        writeSettings();
1138
        event->accept();
1139
    }
1140
    else
1141
    {
21 andreas 1142
        prg_stopped = true;
34 andreas 1143
        killed = true;
36 andreas 1144
        MSG_INFO("Program will stop!");
37 andreas 1145
#ifdef __ANDROID__
92 andreas 1146
        if (gPageManager)
1147
            gPageManager->stopNetworkState();
37 andreas 1148
#endif
5 andreas 1149
        event->accept();
1150
    }
2 andreas 1151
}
1152
 
21 andreas 1153
/**
57 andreas 1154
 * @brief MainWindow::event Looks for a gesture
1155
 * @param event The event occured
1156
 * @return TRUE if event was accepted
1157
 */
1158
bool MainWindow::event(QEvent* event)
1159
{
1160
    if (event->type() == QEvent::Gesture)
1161
        return gestureEvent(static_cast<QGestureEvent*>(event));
1162
 
323 andreas 1163
    return QMainWindow::event(event);
57 andreas 1164
}
1165
 
1166
/**
1167
 * @brief MainWindow::gestureEvent handles a pinch event
1168
 * If a pinch event occured where the scale factor increased, the settings
1169
 * dialog is called. This exists for devices, where the left toolbox is not
1170
 * visible.
1171
 * @param event The guesture occured
130 andreas 1172
 * @return FALSE
57 andreas 1173
 */
1174
bool MainWindow::gestureEvent(QGestureEvent* event)
1175
{
1176
    DECL_TRACER("MainWindow::gestureEvent(QGestureEvent* event)");
59 andreas 1177
 
57 andreas 1178
    if (QGesture *pinch = event->gesture(Qt::PinchGesture))
1179
    {
323 andreas 1180
        QPinchGesture *pg = static_cast<QPinchGesture *>(pinch);
298 andreas 1181
#ifdef QT_DEBUG
57 andreas 1182
        string gs;
323 andreas 1183
 
57 andreas 1184
        switch(pg->state())
1185
        {
1186
            case Qt::NoGesture:         gs.assign("no gesture"); break;
1187
            case Qt::GestureStarted:    gs.assign("gesture started"); break;
1188
            case Qt::GestureUpdated:    gs.assign("gesture updated"); break;
1189
            case Qt::GestureFinished:   gs.assign("gesture finished"); break;
1190
            case Qt::GestureCanceled:   gs.assign("gesture canceled"); break;
1191
        }
1192
 
59 andreas 1193
        MSG_DEBUG("PinchGesture state " << gs << " detected");
298 andreas 1194
#endif
57 andreas 1195
        if (pg->state() == Qt::GestureFinished)
1196
        {
59 andreas 1197
            MSG_DEBUG("total scale: " << pg->totalScaleFactor() << ", scale: " << pg->scaleFactor() << ", last scale: " << pg->lastScaleFactor());
1198
 
57 andreas 1199
            if (pg->totalScaleFactor() > pg->scaleFactor())
1200
                settings();
323 andreas 1201
 
1202
            return true;
57 andreas 1203
        }
1204
    }
153 andreas 1205
    else if (QGesture *swipe = event->gesture(Qt::SwipeGesture))
1206
    {
1207
        if (!gPageManager)
1208
            return false;
1209
 
1210
        QSwipeGesture *sw = static_cast<QSwipeGesture *>(swipe);
1211
        MSG_DEBUG("Swipe gesture detected.");
1212
 
1213
        if (sw->state() == Qt::GestureFinished)
1214
        {
1215
            if (sw->horizontalDirection() == QSwipeGesture::Left)
1216
                gPageManager->onSwipeEvent(TPageManager::SW_LEFT);
1217
            else if (sw->horizontalDirection() == QSwipeGesture::Right)
1218
                gPageManager->onSwipeEvent(TPageManager::SW_RIGHT);
1219
            else if (sw->verticalDirection() == QSwipeGesture::Up)
1220
                gPageManager->onSwipeEvent(TPageManager::SW_UP);
1221
            else if (sw->verticalDirection() == QSwipeGesture::Down)
1222
                gPageManager->onSwipeEvent(TPageManager::SW_DOWN);
323 andreas 1223
 
1224
            return true;
153 andreas 1225
        }
1226
    }
1227
 
57 andreas 1228
    return false;
1229
}
1230
 
1231
/**
319 andreas 1232
 * @brief MainWindow::mousePressEvent catches the event Qt::LeftButton.
1233
 *
1234
 * If the user presses the left mouse button somewhere in the main window, this
1235
 * method is triggered. It retrieves the position of the mouse pointer and
1236
 * sends it to the page manager TPageManager.
1237
 *
1238
 * @param event The event
1239
 */
1240
void MainWindow::mousePressEvent(QMouseEvent* event)
1241
{
1242
    DECL_TRACER("MainWindow::mousePressEvent(QMouseEvent* event)");
1243
 
1244
    if (!gPageManager)
1245
        return;
1246
 
1247
    if(event->button() == Qt::LeftButton)
1248
    {
1249
        int nx = 0, ny = 0;
320 andreas 1250
#ifdef Q_OS_IOS
319 andreas 1251
        if (mHaveNotchPortrait && gPageManager->getSettings()->isPortrait())
1252
        {
1253
            nx = mNotchPortrait.left();
1254
            ny = mNotchPortrait.top();
1255
        }
1256
        else if (mHaveNotchLandscape && gPageManager->getSettings()->isLandscape())
1257
        {
1258
            nx = mNotchLandscape.left();
1259
            ny = mNotchLandscape.top();
1260
        }
1261
        else
1262
        {
1263
            MSG_WARNING("Have no notch distances!");
1264
        }
320 andreas 1265
#endif
361 andreas 1266
        int x = event->position().x() - nx;
1267
        int y = event->position().y() - ny;
1268
        MSG_DEBUG("Mouse press coordinates: x: " << event->position().x() << ", y: " << event->position().y() << " [new x: " << x << ", y: " << y << " -- \"notch\" nx: " << nx << ", ny: " << ny << "]");
1269
 
319 andreas 1270
        mLastPressX = x;
1271
        mLastPressY = y;
320 andreas 1272
/*
319 andreas 1273
        QWidget *w = childAt(event->x(), event->y());
1274
 
1275
        if (w)
1276
        {
1277
            MSG_DEBUG("Object " << w->objectName().toStdString() << " is under mouse cursor.");
1278
            QObject *par = w->parent();
1279
 
1280
            if (par)
1281
            {
1282
                MSG_DEBUG("The parent is " << par->objectName().toStdString());
1283
 
1284
                if (par->objectName().startsWith("Item_"))
1285
                {
1286
                    QObject *ppar = par->parent();
1287
 
1288
                    if (ppar)
1289
                    {
1290
                        MSG_DEBUG("The pparent is " << ppar->objectName().toStdString());
1291
 
1292
                        if (ppar->objectName().startsWith("View_"))
1293
                        {
1294
                            QMouseEvent *mev = new QMouseEvent(event->type(), event->localPos(), event->globalPos(), event->button(), event->buttons(), event->modifiers());
1295
                            QApplication::postEvent(ppar, mev);
1296
                            return;
1297
                        }
1298
                    }
1299
                }
1300
            }
1301
        }
320 andreas 1302
*/
319 andreas 1303
        if (gPageManager->isSetupActive() && isSetupScaled())
1304
        {
1305
            x = (int)((double)x / mSetupScaleFactor);
1306
            y = (int)((double)y / mSetupScaleFactor);
1307
        }
1308
        else if (isScaled())
1309
        {
1310
            x = (int)((double)x / mScaleFactor);
1311
            y = (int)((double)y / mScaleFactor);
1312
        }
1313
 
1314
        gPageManager->mouseEvent(x, y, true);
1315
        mTouchStart = std::chrono::steady_clock::now();
1316
        mTouchX = mLastPressX;
1317
        mTouchY = mLastPressY;
1318
    }
1319
    else if (event->button() == Qt::MiddleButton)
1320
        settings();
1321
}
1322
 
1323
/**
1324
 * @brief MainWindow::mouseReleaseEvent catches the event Qt::LeftButton.
1325
 *
1326
 * If the user releases the left mouse button somewhere in the main window, this
1327
 * method is triggered. It retrieves the position of the mouse pointer and
1328
 * sends it to the page manager TPageManager.
1329
 *
1330
 * @param event The event
1331
 */
1332
void MainWindow::mouseReleaseEvent(QMouseEvent* event)
1333
{
1334
    DECL_TRACER("MainWindow::mouseReleaseEvent(QMouseEvent* event)");
1335
 
1336
    if (!gPageManager)
1337
        return;
1338
 
1339
    if(event->button() == Qt::LeftButton)
1340
    {
1341
        int nx = 0, ny = 0;
320 andreas 1342
#ifdef Q_OS_IOS
319 andreas 1343
        if (mHaveNotchPortrait && gPageManager->getSettings()->isPortrait())
1344
        {
1345
            nx = mNotchPortrait.left();
1346
            ny = mNotchPortrait.top();
1347
        }
1348
        else if (mHaveNotchLandscape && gPageManager->getSettings()->isLandscape())
1349
        {
1350
            nx = mNotchLandscape.left();
1351
            ny = mNotchLandscape.top();
1352
        }
320 andreas 1353
#endif
361 andreas 1354
        int x = ((mLastPressX >= 0) ? mLastPressX : (event->position().x() - nx));
1355
        int y = ((mLastPressY >= 0) ? mLastPressY : (event->position().y() - ny));
1356
        MSG_DEBUG("Mouse press coordinates: x: " << event->position().x() << ", y: " << event->position().y());
319 andreas 1357
        mLastPressX = mLastPressY = -1;
320 andreas 1358
/*
319 andreas 1359
        QWidget *w = childAt(event->x(), event->y());
1360
 
1361
        if (w)
1362
        {
1363
            MSG_DEBUG("Object " << w->objectName().toStdString() << " is under mouse cursor.");
1364
            QObject *par = w->parent();
1365
 
1366
            if (par)
1367
            {
1368
                MSG_DEBUG("The parent is " << par->objectName().toStdString());
1369
 
1370
                if (par->objectName().startsWith("Item_"))
1371
                {
1372
                    QObject *ppar = par->parent();
1373
 
1374
                    if (ppar)
1375
                    {
1376
                        MSG_DEBUG("The pparent is " << ppar->objectName().toStdString());
1377
 
1378
                        if (ppar->objectName().startsWith("View_"))
1379
                        {
1380
                            QMouseEvent *mev = new QMouseEvent(event->type(), event->localPos(), event->globalPos(), event->button(), event->buttons(), event->modifiers());
1381
                            QApplication::postEvent(ppar, mev);
1382
                            return;
1383
                        }
1384
                    }
1385
                }
1386
            }
1387
        }
320 andreas 1388
*/
319 andreas 1389
        if (gPageManager->isSetupActive() && isSetupScaled())
1390
        {
1391
            x = (int)((double)x / mSetupScaleFactor);
1392
            y = (int)((double)y / mSetupScaleFactor);
1393
        }
1394
        else if (isScaled())
1395
        {
1396
            x = (int)((double)x / mScaleFactor);
1397
            y = (int)((double)y / mScaleFactor);
1398
        }
1399
 
1400
        gPageManager->mouseEvent(x, y, false);
1401
        std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
1402
        std::chrono::nanoseconds difftime = end - mTouchStart;
1403
        std::chrono::milliseconds msecs = std::chrono::duration_cast<std::chrono::milliseconds>(difftime);
1404
 
1405
        if (msecs.count() < 100)    // 1/10 of a second
1406
        {
1407
            MSG_DEBUG("Time was too short: " << msecs.count());
1408
            return;
1409
        }
1410
 
361 andreas 1411
        x = event->position().x();
1412
        y = event->position().y();
319 andreas 1413
        bool setupActive = gPageManager->isSetupActive();
1414
        int width = (setupActive ? scaleSetup(gPageManager->getSystemSettings()->getWidth()) : scale(gPageManager->getSettings()->getWidth()));
1415
        int height = (setupActive ? scaleSetup(gPageManager->getSystemSettings()->getHeight()) : scale(gPageManager->getSettings()->getHeight()));
1416
        MSG_DEBUG("Coordinates: x1=" << mTouchX << ", y1=" << mTouchY << ", x2=" << x << ", y2=" << y << ", width=" << width << ", height=" << height);
1417
 
1418
        if (mTouchX < x && (x - mTouchX) > (width / 3))
1419
            gPageManager->onSwipeEvent(TPageManager::SW_RIGHT);
1420
        else if (x < mTouchX && (mTouchX - x) > (width / 3))
1421
            gPageManager->onSwipeEvent(TPageManager::SW_LEFT);
1422
        else if (mTouchY < y && (y - mTouchY) > (height / 3))
1423
            gPageManager->onSwipeEvent(TPageManager::SW_DOWN);
1424
        else if (y < mTouchY && (mTouchY - y) > (height / 3))
1425
            gPageManager->onSwipeEvent(TPageManager::SW_UP);
1426
    }
1427
}
1428
 
1429
void MainWindow::mouseMoveEvent(QMouseEvent* event)
1430
{
1431
    DECL_TRACER("MainWindow::mouseMoveEvent(QMouseEvent* event)");
323 andreas 1432
 
1433
    Q_UNUSED(event);
320 andreas 1434
/*
319 andreas 1435
    QWidget *w = childAt(event->x(), event->y());
1436
 
1437
    if (w)
1438
    {
1439
        MSG_DEBUG("Object " << w->objectName().toStdString() << " is under mouse cursor.");
1440
        QObject *par = w->parent();
1441
 
1442
        if (par)
1443
        {
1444
            MSG_DEBUG("The parent is " << par->objectName().toStdString());
1445
 
1446
            if (par->objectName().startsWith("Item_"))
1447
            {
1448
                QObject *ppar = par->parent();
1449
 
1450
                if (ppar)
1451
                {
1452
                    MSG_DEBUG("The pparent is " << ppar->objectName().toStdString());
1453
 
1454
                    if (ppar->objectName().startsWith("View_"))
1455
                    {
1456
                        QMouseEvent *mev = new QMouseEvent(event->type(), event->localPos(), event->globalPos(), event->button(), event->buttons(), event->modifiers());
1457
                        QApplication::postEvent(ppar, mev);
1458
                        return;
1459
                    }
1460
                }
1461
            }
1462
        }
1463
    }
320 andreas 1464
*/
319 andreas 1465
}
1466
 
1467
void MainWindow::keyPressEvent(QKeyEvent *event)
1468
{
1469
    DECL_TRACER("MainWindow::keyPressEvent(QKeyEvent *event)");
1470
 
1471
    if (event && event->key() == Qt::Key_Back && !mToolbar)
1472
    {
1473
        QMessageBox msgBox(this);
1474
        msgBox.setText("Select what to do next:");
1475
        msgBox.addButton("Quit", QMessageBox::AcceptRole);
1476
        msgBox.addButton("Setup", QMessageBox::RejectRole);
1477
        msgBox.addButton("Cancel", QMessageBox::ResetRole);
1478
        int ret = msgBox.exec();
1479
 
1480
        if (ret == QMessageBox::Accepted)   // This is correct! QT seems to change here the buttons.
1481
        {
1482
            showSetup();
1483
            return;
1484
        }
1485
        else if (ret == QMessageBox::Rejected)  // This is correct! QT seems to change here the buttons.
1486
            close();
1487
    }
1488
}
1489
 
1490
void MainWindow::keyReleaseEvent(QKeyEvent *event)
1491
{
1492
    DECL_TRACER("MainWindow::keyReleaseEvent(QKeyEvent *event)");
1493
 
323 andreas 1494
    Q_UNUSED(event);
319 andreas 1495
}
1496
 
1497
/**
130 andreas 1498
 * @brief MainWindow::onScreenOrientationChanged sets invers or normal orientation.
1499
 * This method sets according to the actual set orientation the invers
1500
 * orientations or switches back to normal orientation.
1501
 * For example: When the orientation is set to portrait and the device is turned
1502
 * to top down, then the orientation is set to invers portrait.
1503
 * @param ori   The detected orientation
1504
 */
1505
void MainWindow::onScreenOrientationChanged(Qt::ScreenOrientation ori)
1506
{
1507
    DECL_TRACER("MainWindow::onScreenOrientationChanged(int ori)");
265 andreas 1508
#if defined(QT_DEBUG) && (defined(Q_OS_IOS) || defined(Q_OS_ANDROID))
298 andreas 1509
    MSG_DEBUG("Orientation changed to " << orientationToString(ori) << " (mOrientation: " << orientationToString(mOrientation) << ")");
263 andreas 1510
#endif
249 andreas 1511
    if (!gPageManager)
130 andreas 1512
        return;
1513
 
249 andreas 1514
    if (gPageManager->getSettings()->isPortrait())
1515
    {
263 andreas 1516
#ifdef Q_OS_IOS
1517
        if (!mHaveNotchPortrait)
1518
            setNotch();
1519
#endif
249 andreas 1520
        if (ori == Qt::PortraitOrientation || ori == Qt::InvertedPortraitOrientation)
1521
        {
264 andreas 1522
#ifdef Q_OS_IOS
1523
            if (mSensor)
1524
                mSensor->setCurrentOrientation(ori);
1525
#endif
249 andreas 1526
            if (mOrientation == ori)
1527
                return;
130 andreas 1528
 
249 andreas 1529
            mOrientation = ori;
1530
        }
1531
        else if (mOrientation != Qt::PortraitOrientation && mOrientation != Qt::InvertedPortraitOrientation)
1532
            mOrientation = Qt::PortraitOrientation;
1533
    }
1534
    else
1535
    {
263 andreas 1536
#ifdef Q_OS_IOS
1537
        if (!mHaveNotchLandscape)
1538
            setNotch();
1539
#endif
249 andreas 1540
        if (ori == Qt::LandscapeOrientation || ori == Qt::InvertedLandscapeOrientation)
1541
        {
264 andreas 1542
#ifdef Q_OS_IOS
1543
            if (mSensor)
1544
                mSensor->setCurrentOrientation(ori);
1545
#endif
249 andreas 1546
            if (mOrientation == ori)
1547
                return;
130 andreas 1548
 
249 andreas 1549
            mOrientation = ori;
1550
        }
1551
        else if (mOrientation != Qt::LandscapeOrientation && mOrientation != Qt::InvertedLandscapeOrientation)
1552
            mOrientation = Qt::LandscapeOrientation;
1553
    }
1554
 
130 andreas 1555
    J_ORIENTATION jori = O_UNDEFINED;
1556
 
249 andreas 1557
    switch(mOrientation)
130 andreas 1558
    {
1559
        case Qt::LandscapeOrientation:          jori = O_LANDSCAPE; break;
1560
        case Qt::InvertedLandscapeOrientation:  jori = O_REVERSE_LANDSCAPE; break;
1561
        case Qt::PortraitOrientation:           jori = O_PORTRAIT; break;
1562
        case Qt::InvertedPortraitOrientation:   jori = O_REVERSE_PORTRAIT; break;
1563
        default:
1564
            return;
1565
    }
1566
 
1567
    _setOrientation(jori);
249 andreas 1568
#ifdef Q_OS_IOS
252 andreas 1569
    setNotch();
1570
#endif
1571
}
249 andreas 1572
 
252 andreas 1573
void MainWindow::onCurrentOrientationChanged(int currentOrientation)
1574
{
1575
    DECL_TRACER("MainWindow::onCurrentOrientationChanged(int currentOrientation)");
265 andreas 1576
#if defined(QT_DEBUG) && (defined(Q_OS_IOS) || defined(Q_OS_ANDROID))
264 andreas 1577
    MSG_DEBUG("User orientation: " << orientationToString((Qt::ScreenOrientation)currentOrientation));
1578
#else
1579
    Q_UNUSED(currentOrientation);
1580
#endif
252 andreas 1581
}
1582
/**
1583
 * @brief MainWindow::onPositionUpdated
1584
 * This method is a callback function for the Qt framework. It is called
1585
 * whenever the geo position changes. The position information is never really
1586
 * used and is implemented only to keep the application on IOS running in the
1587
 * background.
1588
 *
1589
 * @param update    A structure containing the geo position information.
1590
 */
254 andreas 1591
#ifdef Q_OS_IOS
252 andreas 1592
void MainWindow::onPositionUpdated(const QGeoPositionInfo &update)
1593
{
1594
    DECL_TRACER("MainWindow::onPositionUpdated(const QGeoPositionInfo &update)");
249 andreas 1595
 
252 andreas 1596
    QGeoCoordinate coord = update.coordinate();
1597
    MSG_DEBUG("Geo location: " << coord.toString().toStdString());
130 andreas 1598
}
1599
 
252 andreas 1600
void MainWindow::onErrorOccurred(QGeoPositionInfoSource::Error positioningError)
1601
{
1602
    DECL_TRACER("MainWindow::onErrorOccurred(QGeoPositionInfoSource::Error positioningError)");
1603
 
1604
    switch(positioningError)
1605
    {
1606
        case QGeoPositionInfoSource::AccessError:   MSG_ERROR("The connection setup to the remote positioning backend failed because the application lacked the required privileges."); break;
1607
        case QGeoPositionInfoSource::ClosedError:   MSG_ERROR("The remote positioning backend closed the connection, which happens for example in case the user is switching location services to off. As soon as the location service is re-enabled regular updates will resume."); break;
1608
        case QGeoPositionInfoSource::UnknownSourceError: MSG_ERROR("An unidentified error occurred."); break;
264 andreas 1609
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
252 andreas 1610
        case QGeoPositionInfoSource::UpdateTimeoutError: MSG_ERROR("Current position could not be retrieved within the specified timeout."); break;
1611
#endif
1612
        default:
1613
            return;
1614
    }
1615
}
254 andreas 1616
#endif
130 andreas 1617
/**
140 andreas 1618
 * @brief Displays or hides a phone dialog window.
1619
 * This method creates and displays a phone dialog window containing everything
1620
 * a simple phone needs. Depending on the parameter \p state the dialog is
1621
 * created or an exeisting dialog is closed.
1622
 * @param state     If TRUE the dialog is created or if it is not visible
1623
 * brought to front and is made visible.
1624
 * If this is FALSE an existing dialog window is destroid and disappears. If
1625
 * the window didn't exist nothing happens.
1626
 */
1627
void MainWindow::showPhoneDialog(bool state)
1628
{
1629
    DECL_TRACER("MainWindow::showPhoneDialog(bool state)");
1630
 
1631
    if (mPhoneDialog)
1632
    {
1633
        if (!state)
1634
        {
1635
            mPhoneDialog->close();
1636
            delete mPhoneDialog;
1637
            mPhoneDialog = nullptr;
1638
            return;
1639
        }
1640
 
1641
        if (!mPhoneDialog->isVisible())
1642
            mPhoneDialog->setVisible(true);
1643
 
1644
        return;
1645
    }
1646
 
1647
    if (!state)
1648
        return;
1649
 
1650
    mPhoneDialog = new TQtPhone(this);
243 andreas 1651
#if defined(Q_OS_ANDROID)
140 andreas 1652
    // On mobile devices we set the scale factor always because otherwise the
1653
    // dialog will be unusable.
1654
    mPhoneDialog->setScaleFactor(gScale);
1655
    mPhoneDialog->doResize();
1656
#endif
1657
    mPhoneDialog->open();
1658
}
1659
 
1660
/**
1661
 * Displays a phone number (can also be an URL) on a label in the phone dialog
1662
 * window.
1663
 * @param number    The string contains the phone number to display.
1664
 */
1665
void MainWindow::setPhoneNumber(const std::string& number)
1666
{
1667
    DECL_TRACER("MainWindow::setPhoneNumber(const std::string& number)");
1668
 
1669
    if (!mPhoneDialog)
1670
        return;
1671
 
1672
    mPhoneDialog->setPhoneNumber(number);
1673
}
1674
 
1675
/**
1676
 * Displays a message in the status line on the bottom of the phone dialog
1677
 * window.
1678
 * @param msg   The string conaining a message.
1679
 */
1680
void MainWindow::setPhoneStatus(const std::string& msg)
1681
{
1682
    DECL_TRACER("MainWindow::setPhoneStatus(const std::string& msg)");
1683
 
1684
    if (!mPhoneDialog)
1685
        return;
1686
 
1687
    mPhoneDialog->setPhoneStatus(msg);
1688
}
1689
 
1690
void MainWindow::setPhoneState(int state, int id)
1691
{
1692
    DECL_TRACER("MainWindow::setPhoneState(int state)");
1693
 
1694
    if (mPhoneDialog)
1695
        mPhoneDialog->setPhoneState(state, id);
1696
}
1697
 
1698
/**
39 andreas 1699
 * @brief MainWindow::createActions creates the toolbar on the right side.
21 andreas 1700
 */
2 andreas 1701
void MainWindow::createActions()
1702
{
5 andreas 1703
    DECL_TRACER("MainWindow::createActions()");
2 andreas 1704
 
151 andreas 1705
    // If the toolbar should not be visible at all we return here immediately.
1706
    if (TConfig::getToolbarSuppress())
1707
        return;
1708
 
88 andreas 1709
    // Add a mToolbar (on the right side)
1710
    mToolbar = new QToolBar(this);
300 andreas 1711
    QPalette palette(mToolbar->palette());
1712
    palette.setColor(QPalette::Window, QColorConstants::Svg::honeydew);
1713
    mToolbar->setPalette(palette);
58 andreas 1714
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
88 andreas 1715
    mToolbar->setAllowedAreas(Qt::RightToolBarArea);
1716
    mToolbar->setFloatable(false);
1717
    mToolbar->setMovable(false);
243 andreas 1718
#ifdef Q_OS_ANDROID
259 andreas 1719
    if (isScaled())
38 andreas 1720
    {
217 andreas 1721
        int width = (int)((double)gPageManager->getSettings()->getWidth() * gScale);
38 andreas 1722
        int tbWidth = (int)(48.0 * gScale);
1723
        int icWidth = (int)(40.0 * gScale);
1724
 
120 andreas 1725
        if ((gFullWidth - width) < tbWidth && !TConfig::getToolbarForce())
38 andreas 1726
        {
88 andreas 1727
            delete mToolbar;
1728
            mToolbar = nullptr;
38 andreas 1729
            return;
1730
        }
1731
 
262 andreas 1732
        MSG_DEBUG("Icon size: " << icWidth << "x" << icWidth << ", Toolbar width: " << tbWidth);
38 andreas 1733
        QSize iSize(icWidth, icWidth);
88 andreas 1734
        mToolbar->setIconSize(iSize);
38 andreas 1735
    }
243 andreas 1736
#endif  // Q_OS_ANDROID
39 andreas 1737
#else
88 andreas 1738
    mToolbar->setFloatable(true);
1739
    mToolbar->setMovable(true);
1740
    mToolbar->setAllowedAreas(Qt::RightToolBarArea | Qt::BottomToolBarArea);
243 andreas 1741
#endif  // defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
32 andreas 1742
    QAction *arrowUpAct = new QAction(QIcon(":/images/arrow_up.png"), tr("Up"), this);
1743
    connect(arrowUpAct, &QAction::triggered, this, &MainWindow::arrowUp);
88 andreas 1744
    mToolbar->addAction(arrowUpAct);
31 andreas 1745
 
32 andreas 1746
    QAction *arrowLeftAct = new QAction(QIcon(":/images/arrow_left.png"), tr("Left"), this);
1747
    connect(arrowLeftAct, &QAction::triggered, this, &MainWindow::arrowLeft);
88 andreas 1748
    mToolbar->addAction(arrowLeftAct);
31 andreas 1749
 
32 andreas 1750
    QAction *arrowRightAct = new QAction(QIcon(":/images/arrow_right.png"), tr("Right"), this);
1751
    connect(arrowRightAct, &QAction::triggered, this, &MainWindow::arrowRight);
88 andreas 1752
    mToolbar->addAction(arrowRightAct);
31 andreas 1753
 
32 andreas 1754
    QAction *arrowDownAct = new QAction(QIcon(":/images/arrow_down.png"), tr("Down"), this);
1755
    connect(arrowDownAct, &QAction::triggered, this, &MainWindow::arrowDown);
88 andreas 1756
    mToolbar->addAction(arrowDownAct);
31 andreas 1757
 
32 andreas 1758
    QAction *selectOkAct = new QAction(QIcon(":/images/ok.png"), tr("Ok"), this);
1759
    connect(selectOkAct, &QAction::triggered, this, &MainWindow::selectOk);
88 andreas 1760
    mToolbar->addAction(selectOkAct);
31 andreas 1761
 
88 andreas 1762
    mToolbar->addSeparator();
31 andreas 1763
 
35 andreas 1764
    QToolButton *btVolUp = new QToolButton(this);
1765
    btVolUp->setIcon(QIcon(":/images/vol_up.png"));
1766
    connect(btVolUp, &QToolButton::pressed, this, &MainWindow::volumeUpPressed);
1767
    connect(btVolUp, &QToolButton::released, this, &MainWindow::volumeUpReleased);
88 andreas 1768
    mToolbar->addWidget(btVolUp);
40 andreas 1769
 
35 andreas 1770
    QToolButton *btVolDown = new QToolButton(this);
1771
    btVolDown->setIcon(QIcon(":/images/vol_down.png"));
1772
    connect(btVolDown, &QToolButton::pressed, this, &MainWindow::volumeDownPressed);
1773
    connect(btVolDown, &QToolButton::released, this, &MainWindow::volumeDownReleased);
88 andreas 1774
    mToolbar->addWidget(btVolDown);
38 andreas 1775
/*
33 andreas 1776
    QAction *volMute = new QAction(QIcon(":/images/vol_mute.png"), tr("X"), this);
1777
    connect(volMute, &QAction::triggered, this, &MainWindow::volumeMute);
88 andreas 1778
    mToolbar->addAction(volMute);
38 andreas 1779
*/
88 andreas 1780
    mToolbar->addSeparator();
36 andreas 1781
    const QIcon settingsIcon = QIcon::fromTheme("settings-configure", QIcon(":/images/settings.png"));
5 andreas 1782
    QAction *settingsAct = new QAction(settingsIcon, tr("&Settings..."), this);
1783
    settingsAct->setStatusTip(tr("Change the settings"));
1784
    connect(settingsAct, &QAction::triggered, this, &MainWindow::settings);
88 andreas 1785
    mToolbar->addAction(settingsAct);
250 andreas 1786
 
38 andreas 1787
    const QIcon aboutIcon = QIcon::fromTheme("help-about", QIcon(":/images/info.png"));
5 andreas 1788
    QAction *aboutAct = new QAction(aboutIcon, tr("&About..."), this);
1789
    aboutAct->setShortcuts(QKeySequence::Open);
1790
    aboutAct->setStatusTip(tr("About this program"));
1791
    connect(aboutAct, &QAction::triggered, this, &MainWindow::about);
88 andreas 1792
    mToolbar->addAction(aboutAct);
2 andreas 1793
 
33 andreas 1794
    const QIcon exitIcon = QIcon::fromTheme("application-exit", QIcon(":/images/off.png"));
88 andreas 1795
    QAction *exitAct = mToolbar->addAction(exitIcon, tr("E&xit"), this, &QWidget::close);
5 andreas 1796
    exitAct->setShortcuts(QKeySequence::Quit);
1797
    exitAct->setStatusTip(tr("Exit the application"));
31 andreas 1798
 
88 andreas 1799
    addToolBar(Qt::RightToolBarArea, mToolbar);
2 andreas 1800
}
1801
 
21 andreas 1802
/**
1803
 * @brief MainWindow::settings initiates the configuration dialog.
1804
 */
2 andreas 1805
void MainWindow::settings()
1806
{
5 andreas 1807
    DECL_TRACER("MainWindow::settings()");
251 andreas 1808
#if defined(Q_OS_IOS) || defined(Q_OS_ANDROID)
259 andreas 1809
#ifdef Q_OS_ANDROID
197 andreas 1810
    if (gPageManager)
1811
    {
1812
        gPageManager->showSetup();
1813
        return;
1814
    }
211 andreas 1815
    else    // This "else" should never be executed!
1816
        displayMessage("<b>Fatal error</b>: An internal mandatory class was not initialized!<br>Unable to show setup dialog!", "Fatal error");
1817
#else
259 andreas 1818
    mIOSSettingsActive = true;
250 andreas 1819
    QASettings::openSettings();
238 andreas 1820
#endif
1821
#else
90 andreas 1822
    // Save some old values to decide whether to start over or not.
1823
    string oldHost = TConfig::getController();
1824
    int oldPort = TConfig::getPort();
1825
    int oldChannelID = TConfig::getChannel();
118 andreas 1826
    string oldSurface = TConfig::getFtpSurface();
120 andreas 1827
    bool oldToolbar = TConfig::getToolbarForce();
151 andreas 1828
    bool oldToolbarSuppress = TConfig::getToolbarSuppress();
90 andreas 1829
    // Initialize and open the settings dialog.
5 andreas 1830
    TQtSettings *dlg_settings = new TQtSettings(this);
23 andreas 1831
    int ret = dlg_settings->exec();
118 andreas 1832
    bool rebootAnyway = false;
23 andreas 1833
 
179 andreas 1834
    if ((ret && dlg_settings->hasChanged()) || (ret && dlg_settings->downloadForce()))
89 andreas 1835
    {
23 andreas 1836
        writeSettings();
89 andreas 1837
 
151 andreas 1838
        if (!TConfig::getToolbarSuppress() && oldToolbar != TConfig::getToolbarForce())
120 andreas 1839
        {
122 andreas 1840
            QMessageBox msgBox(this);
120 andreas 1841
            msgBox.setText("The change for the visibility of the toolbar will be active on the next start of TPanel!");
1842
            msgBox.exec();
1843
        }
151 andreas 1844
        else if (oldToolbarSuppress != TConfig::getToolbarSuppress() && TConfig::getToolbarSuppress())
1845
        {
1846
            if (mToolbar)
1847
            {
1848
                mToolbar->close();
1849
                delete mToolbar;
1850
                mToolbar = nullptr;
1851
            }
1852
        }
120 andreas 1853
 
122 andreas 1854
        if (TConfig::getFtpSurface() != oldSurface || dlg_settings->downloadForce())
118 andreas 1855
        {
122 andreas 1856
            bool dlYes = true;
118 andreas 1857
 
179 andreas 1858
            MSG_DEBUG("Surface should be downloaded (Old: " << oldSurface << ", New: " << TConfig::getFtpSurface() << ")");
1859
 
122 andreas 1860
            if (!dlg_settings->downloadForce())
1861
            {
1862
                QMessageBox msgBox(this);
1863
                msgBox.setText(QString("Should the surface <b>") + TConfig::getFtpSurface().c_str() + "</b> be installed?");
1864
                msgBox.addButton(QMessageBox::Yes);
1865
                msgBox.addButton(QMessageBox::No);
1866
                int ret = msgBox.exec();
120 andreas 1867
 
122 andreas 1868
                if (ret == QMessageBox::No)
1869
                    dlYes = false;
1870
            }
119 andreas 1871
 
122 andreas 1872
            if (dlYes)
1873
            {
1874
                TTPInit tpinit;
1875
                tpinit.regCallbackProcessEvents(bind(&MainWindow::runEvents, this));
179 andreas 1876
                tpinit.regCallbackProgressBar(bind(&MainWindow::_onProgressChanged, this, std::placeholders::_1));
122 andreas 1877
                tpinit.setPath(TConfig::getProjectPath());
179 andreas 1878
                tpinit.setFileSize(dlg_settings->getSelectedFtpFileSize());
1879
                string msg = "Loading file <b>" + TConfig::getFtpSurface() + "</b>.";
1880
                MSG_DEBUG("Download of surface " << TConfig::getFtpSurface() << " was forced!");
122 andreas 1881
 
179 andreas 1882
                downloadBar(msg, this);
122 andreas 1883
 
1884
                if (tpinit.loadSurfaceFromController(true))
1885
                    rebootAnyway = true;
1886
 
179 andreas 1887
                mDownloadBar->close();
122 andreas 1888
                mBusy = false;
1889
            }
1890
            else
1891
            {
179 andreas 1892
                MSG_DEBUG("No change of surface. Old surface " << oldSurface << " was saved again.");
122 andreas 1893
                TConfig::saveFtpSurface(oldSurface);
1894
                writeSettings();
1895
            }
118 andreas 1896
        }
1897
 
90 andreas 1898
        if (TConfig::getController() != oldHost ||
1899
            TConfig::getChannel() != oldChannelID ||
118 andreas 1900
            TConfig::getPort() != oldPort || rebootAnyway)
89 andreas 1901
        {
90 andreas 1902
            // Start over by exiting this class
1903
            MSG_INFO("Program will start over!");
1904
            _restart_ = true;
1905
            prg_stopped = true;
1906
            killed = true;
1907
 
1908
            if (gAmxNet)
1909
                gAmxNet->stop();
1910
 
1911
            close();
89 andreas 1912
        }
1913
    }
23 andreas 1914
    else if (!ret && dlg_settings->hasChanged())
1915
    {
1916
        TConfig cf(TConfig::getConfigPath() + "/" + TConfig::getConfigFileName());
1917
    }
44 andreas 1918
 
1919
    delete dlg_settings;
251 andreas 1920
#endif  // defined(Q_OS_IOS) || defined(Q_OS_ANDROID)
2 andreas 1921
}
1922
 
21 andreas 1923
/**
1924
 * @brief MainWindow::writeSettings Writes the settings into the configuration file.
1925
 */
2 andreas 1926
void MainWindow::writeSettings()
1927
{
5 andreas 1928
    DECL_TRACER("MainWindow::writeSettings()");
23 andreas 1929
 
1930
    TConfig::saveSettings();
43 andreas 1931
    MSG_INFO("Wrote settings.");
2 andreas 1932
}
1933
 
21 andreas 1934
/**
1935
 * @brief MainWindow::about displays the _about_ dialog.
1936
 */
2 andreas 1937
void MainWindow::about()
1938
{
5 andreas 1939
    DECL_TRACER("MainWindow::about()");
2 andreas 1940
 
259 andreas 1941
#ifdef Q_OS_IOS
1942
    // On IOS the explicit about dialog is shown over the whole screen with
1943
    // the text in a small stripe on the left. This looks ugly and therefor
1944
    // we construct our own about dialog.
1945
    std::string msg = "About TPanel\n\n";
1946
    msg.append("Simulation of an AMX G4 panel\n");
1947
    msg.append("Version v").append(VERSION_STRING()).append("\n");
260 andreas 1948
    msg.append("(C) Copyright 2020 to 2023 by Andreas Theofilu (andreas@theosys.at)\n");
259 andreas 1949
 
1950
    QMessageBox about(this);
1951
    about.addButton(QMessageBox::Ok);
1952
    about.setWindowTitle(tr("About TPanel"));
1953
    about.setIconPixmap(QPixmap(":images/icon.png"));
1954
    about.setTextFormat(Qt::PlainText);
1955
    about.setText(tr(msg.c_str()));
1956
    about.setInformativeText(tr("This program is under the terms of GPL version 3!"));
1957
    about.exec();
1958
#else
82 andreas 1959
    std::string msg = "Simulation of an AMX G4 panel\n";
1960
    msg.append("Version v").append(VERSION_STRING()).append("\n");
259 andreas 1961
    msg.append("(C) Copyright 2020 to 2023 by Andreas Theofilu <andreas@theosys.at>\n");
1962
    msg.append("This program is under the terms of GPL version 3!");
1963
    QMessageBox::about(this, tr("About TPanel"), tr(msg.c_str()));
1964
#endif
2 andreas 1965
}
1966
 
32 andreas 1967
void MainWindow::arrowUp()
1968
{
1969
    DECL_TRACER("MainWindow::arrowUp()");
35 andreas 1970
 
1971
    extButtons_t btType = EXT_CURSOR_UP;
1972
 
1973
    if (TConfig::getPanelType().find("Android") != string::npos)
1974
        btType = EXT_GESTURE_UP;
1975
 
92 andreas 1976
    gPageManager->externalButton(btType, true);
1977
    gPageManager->externalButton(btType, false);
32 andreas 1978
}
14 andreas 1979
 
32 andreas 1980
void MainWindow::arrowLeft()
1981
{
1982
    DECL_TRACER("MainWindow::arrowLeft()");
35 andreas 1983
    extButtons_t btType = EXT_CURSOR_LEFT;
1984
 
1985
    if (TConfig::getPanelType().find("Android") != string::npos)
1986
        btType = EXT_GESTURE_LEFT;
1987
 
92 andreas 1988
    gPageManager->externalButton(btType, true);
1989
    gPageManager->externalButton(btType, false);
32 andreas 1990
}
1991
 
1992
void MainWindow::arrowRight()
1993
{
1994
    DECL_TRACER("MainWindow::arrowRight()");
35 andreas 1995
    extButtons_t btType = EXT_CURSOR_RIGHT;
1996
 
1997
    if (TConfig::getPanelType().find("Android") != string::npos)
1998
        btType = EXT_GESTURE_RIGHT;
1999
 
92 andreas 2000
    gPageManager->externalButton(btType, true);
2001
    gPageManager->externalButton(btType, false);
32 andreas 2002
}
2003
 
2004
void MainWindow::arrowDown()
2005
{
2006
    DECL_TRACER("MainWindow::arrowDown()");
35 andreas 2007
    extButtons_t btType = EXT_CURSOR_DOWN;
2008
 
2009
    if (TConfig::getPanelType().find("Android") != string::npos)
2010
        btType = EXT_GESTURE_DOWN;
2011
 
92 andreas 2012
    gPageManager->externalButton(btType, true);
2013
    gPageManager->externalButton(btType, false);
32 andreas 2014
}
2015
 
2016
void MainWindow::selectOk()
2017
{
2018
    DECL_TRACER("MainWindow::selectOk()");
35 andreas 2019
    extButtons_t btType = EXT_CURSOR_SELECT;
2020
 
2021
    if (TConfig::getPanelType().find("Android") != string::npos)
2022
        btType = EXT_GESTURE_DOUBLE_PRESS;
2023
 
92 andreas 2024
    gPageManager->externalButton(btType, true);
2025
    gPageManager->externalButton(btType, false);
32 andreas 2026
}
2027
 
33 andreas 2028
void MainWindow::volumeUpPressed()
2029
{
2030
    DECL_TRACER("MainWindow::volumeUpPressed()");
35 andreas 2031
    extButtons_t btType = EXT_CURSOR_ROTATE_RIGHT;
2032
 
2033
    if (TConfig::getPanelType().find("Android") != string::npos)
2034
        btType = EXT_GESTURE_ROTATE_RIGHT;
2035
 
92 andreas 2036
    gPageManager->externalButton(btType, true);
33 andreas 2037
}
2038
 
2039
void MainWindow::volumeUpReleased()
2040
{
2041
    DECL_TRACER("MainWindow::volumeUpReleased()");
35 andreas 2042
    extButtons_t btType = EXT_CURSOR_ROTATE_RIGHT;
2043
 
2044
    if (TConfig::getPanelType().find("Android") != string::npos)
2045
        btType = EXT_GESTURE_ROTATE_RIGHT;
2046
 
92 andreas 2047
    gPageManager->externalButton(btType, false);
33 andreas 2048
}
2049
 
2050
void MainWindow::volumeDownPressed()
2051
{
2052
    DECL_TRACER("MainWindow::volumeDownPressed()");
35 andreas 2053
    extButtons_t btType = EXT_CURSOR_ROTATE_LEFT;
2054
 
2055
    if (TConfig::getPanelType().find("Android") != string::npos)
2056
        btType = EXT_GESTURE_ROTATE_LEFT;
2057
 
92 andreas 2058
    gPageManager->externalButton(btType, true);
33 andreas 2059
}
2060
 
2061
void MainWindow::volumeDownReleased()
2062
{
2063
    DECL_TRACER("MainWindow::volumeDownReleased()");
35 andreas 2064
    extButtons_t btType = EXT_CURSOR_ROTATE_LEFT;
2065
 
2066
    if (TConfig::getPanelType().find("Android") != string::npos)
2067
        btType = EXT_GESTURE_ROTATE_LEFT;
2068
 
92 andreas 2069
    gPageManager->externalButton(btType, false);
33 andreas 2070
}
38 andreas 2071
/*
33 andreas 2072
void MainWindow::volumeMute()
2073
{
2074
    DECL_TRACER("MainWindow::volumeMute()");
92 andreas 2075
    gPageManager->externalButton(EXT_GENERAL, true);
2076
    gPageManager->externalButton(EXT_GENERAL, false);
33 andreas 2077
}
38 andreas 2078
*/
295 andreas 2079
 
2080
void MainWindow::animationInFinished()
2081
{
2082
    DECL_TRACER("MainWindow::animationInFinished()");
2083
 
350 andreas 2084
    if (mAnimObjects.empty())
2085
    {
2086
#if TESTMODE == 1
2087
        setScreenDone();
2088
#endif
2089
        return;
2090
    }
2091
 
296 andreas 2092
    TLOCKER(anim_mutex);
2093
    map<ulong, OBJECT_t *>::iterator iter;
2094
 
2095
    for (iter = mAnimObjects.begin(); iter != mAnimObjects.end(); ++iter)
295 andreas 2096
    {
296 andreas 2097
        if (!iter->second->animation)
2098
            continue;
295 andreas 2099
 
297 andreas 2100
        if (!iter->second->invalid && iter->second->type == OBJ_SUBPAGE && iter->second->animation->state() == QAbstractAnimation::Stopped)
295 andreas 2101
        {
296 andreas 2102
            if (iter->second->object.widget)
2103
            {
2104
                iter->second->object.widget->lower();
2105
                iter->second->object.widget->show();
2106
                iter->second->object.widget->raise();
2107
            }
2108
 
2109
            disconnect(iter->second->animation, &QPropertyAnimation::finished, this, &MainWindow::animationInFinished);
2110
            delete iter->second->animation;
2111
            iter->second->animation = nullptr;
295 andreas 2112
        }
296 andreas 2113
    }
295 andreas 2114
 
296 andreas 2115
    // Delete all empty/finished animations
2116
    bool repeat = false;
2117
 
2118
    do
2119
    {
2120
        repeat = false;
2121
 
2122
        for (iter = mAnimObjects.begin(); iter != mAnimObjects.end(); ++iter)
2123
        {
2124
            if (!iter->second->remove && !iter->second->animation)
2125
            {
2126
                mAnimObjects.erase(iter);
2127
                repeat = true;
2128
                break;
2129
            }
2130
        }
295 andreas 2131
    }
296 andreas 2132
    while (repeat);
332 andreas 2133
#if TESTMODE == 1
2134
    __success = true;
334 andreas 2135
    setScreenDone();
332 andreas 2136
#endif
295 andreas 2137
}
2138
 
42 andreas 2139
void MainWindow::animationFinished()
2140
{
2141
    DECL_TRACER("MainWindow::animationFinished()");
43 andreas 2142
 
350 andreas 2143
    if (mAnimObjects.empty())
2144
    {
2145
#if TESTMODE == 1
2146
        setScreenDone();
2147
#endif
2148
        return;
2149
    }
2150
 
296 andreas 2151
    TLOCKER(anim_mutex);
2152
    map<ulong, OBJECT_t *>::iterator iter;
43 andreas 2153
 
296 andreas 2154
    for (iter = mAnimObjects.begin(); iter != mAnimObjects.end(); ++iter)
42 andreas 2155
    {
297 andreas 2156
        OBJECT_t *obj = findObject(iter->first);
2157
 
2158
        if (obj && obj->remove && obj->animation && obj->animation->state() == QAbstractAnimation::Stopped)
296 andreas 2159
        {
2160
            MSG_DEBUG("Invalidating object " << handleToString(iter->first));
297 andreas 2161
            disconnect(obj->animation, &QPropertyAnimation::finished, this, &MainWindow::animationFinished);
2162
            delete obj->animation;
2163
            obj->animation = nullptr;
296 andreas 2164
            invalidateAllSubObjects(iter->first);
2165
            invalidateObject(iter->first);
43 andreas 2166
 
297 andreas 2167
            if (obj->type == OBJ_SUBPAGE && obj->object.widget)
2168
                obj->object.widget->hide();
296 andreas 2169
        }
42 andreas 2170
    }
296 andreas 2171
    // Delete all empty/finished animations
2172
    bool repeat = false;
43 andreas 2173
 
296 andreas 2174
    do
42 andreas 2175
    {
296 andreas 2176
        repeat = false;
101 andreas 2177
 
296 andreas 2178
        for (iter = mAnimObjects.begin(); iter != mAnimObjects.end(); ++iter)
2179
        {
2180
            if (iter->second->remove && !iter->second->animation)
2181
            {
2182
                mAnimObjects.erase(iter);
2183
                repeat = true;
2184
                break;
2185
            }
2186
        }
42 andreas 2187
    }
296 andreas 2188
    while (repeat);
332 andreas 2189
#if TESTMODE == 1
2190
    __success = true;
334 andreas 2191
    setScreenDone();
332 andreas 2192
#endif
42 andreas 2193
}
2194
 
142 andreas 2195
void MainWindow::repaintWindows()
2196
{
2197
    DECL_TRACER("MainWindow::repaintWindows()");
2198
 
2199
    if (mWasInactive)
148 andreas 2200
    {
2201
        MSG_INFO("Refreshing of visible popups will be requested.");
142 andreas 2202
        mDoRepaint = true;
148 andreas 2203
    }
142 andreas 2204
}
2205
 
151 andreas 2206
void MainWindow::toFront(ulong handle)
2207
{
2208
    DECL_TRACER("MainWindow::toFront(ulong handle)");
2209
 
277 andreas 2210
    TObject::OBJECT_t *obj = findObject(handle);
151 andreas 2211
 
2212
    if (!obj)
2213
    {
271 andreas 2214
        MSG_WARNING("Object with " << handleToString(handle) << " not found!");
332 andreas 2215
#if TESTMODE == 1
334 andreas 2216
        setScreenDone();
332 andreas 2217
#endif
151 andreas 2218
        return;
2219
    }
2220
 
2221
    if (obj->type == TObject::OBJ_SUBPAGE && obj->object.widget)
2222
        obj->object.widget->raise();
332 andreas 2223
#if TESTMODE == 1
2224
    __success = true;
334 andreas 2225
    setScreenDone();
332 andreas 2226
#endif
151 andreas 2227
}
2228
 
206 andreas 2229
void MainWindow::downloadSurface(const string &file, size_t size)
205 andreas 2230
{
206 andreas 2231
    DECL_TRACER("MainWindow::downloadSurface(const string &file, size_t size)");
205 andreas 2232
 
206 andreas 2233
    if (mBusy)
205 andreas 2234
        return;
2235
 
206 andreas 2236
    QMessageBox msgBox(this);
208 andreas 2237
    msgBox.setText(QString("Should the surface <b>") + file.c_str() + "</b> be installed?<br><i><u>Hint</u>: This will also save all current made settings.</i>");
206 andreas 2238
    msgBox.addButton(QMessageBox::Yes);
2239
    msgBox.addButton(QMessageBox::No);
2240
    int ret = msgBox.exec();
2241
 
2242
    if (ret == QMessageBox::Yes)
2243
    {
2244
        TTPInit tpinit;
2245
        tpinit.regCallbackProcessEvents(bind(&MainWindow::runEvents, this));
2246
        tpinit.regCallbackProgressBar(bind(&MainWindow::_onProgressChanged, this, std::placeholders::_1));
2247
        tpinit.setPath(TConfig::getProjectPath());
208 andreas 2248
 
2249
        if (size)
2250
            tpinit.setFileSize(size);
2251
        else
2252
        {
2253
            size = tpinit.getFileSize(file);
2254
 
2255
            if (!size)
2256
            {
2257
                displayMessage("File <b>" + file + "</b> either doesn't exist on " + TConfig::getController() + " or the NetLinx is not reachable!", "Error");
2258
                return;
2259
            }
2260
 
2261
            tpinit.setFileSize(size);
2262
        }
2263
 
206 andreas 2264
        string msg = "Loading file <b>" + file + "</b>.";
2265
 
2266
        downloadBar(msg, this);
2267
        bool reboot = false;
2268
 
2269
        if (tpinit.loadSurfaceFromController(true))
2270
            reboot = true;
2271
        else
2272
            displayMessage("Error downloading file <b>" + file + "</b>!", "Error");
2273
 
2274
        mDownloadBar->close();
208 andreas 2275
        TConfig::setTemporary(true);
2276
        TConfig::saveSettings();
206 andreas 2277
 
2278
        if (reboot)
2279
        {
2280
            // Start over by exiting this class
2281
            MSG_INFO("Program will start over!");
2282
            _restart_ = true;
2283
            prg_stopped = true;
2284
            killed = true;
2285
 
2286
            if (gAmxNet)
2287
                gAmxNet->stop();
2288
 
2289
            close();
2290
        }
2291
    }
2292
 
2293
    mBusy = false;
2294
}
2295
 
2296
void MainWindow::displayMessage(const string &msg, const string &title)
2297
{
2298
    DECL_TRACER("MainWindow::displayMessage(const string &msg, const string &title)");
2299
 
2300
    QMessageBox msgBox(this);
259 andreas 2301
    msgBox.setText(msg.c_str());
206 andreas 2302
 
2303
    if (!title.empty())
2304
        msgBox.setWindowTitle(title.c_str());
2305
 
259 andreas 2306
    msgBox.setWindowModality(Qt::WindowModality::ApplicationModal);
206 andreas 2307
    msgBox.addButton(QMessageBox::Ok);
2308
    msgBox.exec();
2309
}
2310
 
209 andreas 2311
void MainWindow::fileDialog(ulong handle, const string &path, const std::string& extension, const std::string& suffix)
2312
{
2313
    DECL_TRACER("MainWindow::fileDialog(ulong handle, const string &path, const std::string& extension, const std::string& suffix)");
2314
 
2315
    std::string pt = path;
2316
 
2317
    if (fs::exists(path) && fs::is_regular_file(path))
2318
    {
2319
        size_t pos = pt.find_last_of("/");
2320
 
2321
        if (pos != std::string::npos)
2322
            pt = pt.substr(0, pos);
2323
        else
2324
        {
2325
            char hv0[4096];
2326
            getcwd(hv0, sizeof(hv0));
2327
            pt = hv0;
2328
        }
2329
    }
2330
 
2331
    QString actPath(pt.c_str());
2332
    QFileDialog fdialog(this, tr("File"), actPath, tr(extension.c_str()));
2333
    fdialog.setAcceptMode(QFileDialog::AcceptSave);
2334
 
2335
    if (!suffix.empty())
2336
        fdialog.setDefaultSuffix(suffix.c_str());
2337
 
2338
    fdialog.setOption(QFileDialog::DontConfirmOverwrite);
2339
    QString fname;
2340
 
2341
    if (fdialog.exec())
2342
    {
2343
        QDir dir = fdialog.directory();
2344
        QStringList list = fdialog.selectedFiles();
2345
 
2346
        if (list.size() > 0)
2347
            fname = dir.absoluteFilePath(list.at(0));
2348
        else
2349
            return;
2350
    }
2351
    else
2352
        return;
2353
 
2354
#ifdef Q_OS_ANDROID
247 andreas 2355
    // In case of Android we get some kind of URL instead of a clear
209 andreas 2356
    // path. Because of this we must call some Java API functions to find the
2357
    // real path.
2358
    QString fileName = fname;
2359
 
2360
    if (fileName.contains("content://"))
2361
    {
264 andreas 2362
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
209 andreas 2363
        QAndroidJniObject uri = QAndroidJniObject::callStaticObjectMethod(
2364
            "android/net/Uri", "parse", "(Ljava/lang/String;)Landroid/net/Uri;",
2365
            QAndroidJniObject::fromString(fileName).object<jstring>());
2366
 
2367
        fileName = QAndroidJniObject::callStaticObjectMethod(
2368
            "org/qtproject/theosys/UriToPath", "getFileName",
2369
            "(Landroid/net/Uri;Landroid/content/Context;)Ljava/lang/String;",
2370
            uri.object(), QtAndroid::androidContext().object()).toString();
2371
#else   // QT5_LINUX
2372
        // For QT6 the API has slightly changed. Especialy the class name to
2373
        // call Java objects.
2374
        QJniObject uri = QJniObject::callStaticObjectMethod(
2375
            "android/net/Uri", "parse", "(Ljava/lang/String;)Landroid/net/Uri;",
2376
            QJniObject::fromString(fileName).object<jstring>());
2377
 
2378
        fileName = QJniObject::callStaticObjectMethod(
2379
            "org/qtproject/theosys/UriToPath", "getFileName",
2380
            "(Landroid/net/Uri;Landroid/content/Context;)Ljava/lang/String;",
2381
            uri.object(), QNativeInterface::QAndroidApplication::context()).toString();
2382
#endif  // QT5_LINUX
2383
        if (fileName.length() > 0)
2384
            fname = fileName;
2385
    }
2386
    else
2387
    {
2388
        MSG_WARNING("Not an Uri? (" << fname.toStdString() << ")");
2389
    }
2390
#endif  // Q_OS_ANDROID
2391
 
2392
    if (gPageManager)
2393
        gPageManager->setTextToButton(handle, fname.toStdString(), true);
2394
}
2395
 
213 andreas 2396
void MainWindow::onTListCallbackCurrentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
206 andreas 2397
{
332 andreas 2398
    DECL_TRACER("MainWindow::onTListCallbackCurrentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)");
206 andreas 2399
 
2400
    if (!current || current == previous)
2401
        return;
2402
 
205 andreas 2403
    QListWidget *w = current->listWidget();
277 andreas 2404
    TObject::OBJECT_t *objWindow = findFirstWindow();
205 andreas 2405
 
2406
    while(objWindow)
2407
    {
277 andreas 2408
        TObject::OBJECT_t *objItem = findFirstChild(objWindow->handle);
205 andreas 2409
 
206 andreas 2410
        while (objItem)
205 andreas 2411
        {
206 andreas 2412
            if (objItem->type == TObject::OBJ_LIST && objItem->object.list == w)
2413
            {
2414
                int row = objItem->object.list->currentRow();
2415
                gPageManager->setSelectedRow(objItem->handle, row + 1, current->text().toStdString());
2416
                return;
2417
            }
2418
 
277 andreas 2419
            objItem = findNextChild(objItem->handle);
205 andreas 2420
        }
206 andreas 2421
 
277 andreas 2422
        objWindow = findNextWindow(objWindow);
205 andreas 2423
    }
2424
}
2425
 
179 andreas 2426
void MainWindow::onProgressChanged(int percent)
2427
{
2428
    DECL_TRACER("MainWindow::onProgressChanged(int percent)");
2429
 
2430
    if (!mDownloadBar || !mBusy)
2431
        return;
2432
 
2433
    mDownloadBar->setProgress(percent);
2434
}
2435
 
260 andreas 2436
void MainWindow::startWait(const string& text)
2437
{
2438
    DECL_TRACER("MainWindow::startWait(const string& text)");
2439
 
2440
    if (mWaitBox)
2441
    {
2442
        mWaitBox->setText(text);
2443
        return;
2444
    }
2445
 
2446
    mWaitBox = new TQtWait(this, text);
2447
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
2448
    mWaitBox->setScaleFactor(mScaleFactor);
2449
    mWaitBox->doResize();
2450
    mWaitBox->start();
2451
#endif
2452
}
2453
 
2454
void MainWindow::stopWait()
2455
{
2456
    DECL_TRACER("MainWindow::stopWait()");
2457
 
2458
    if (!mWaitBox)
2459
        return;
2460
 
2461
    mWaitBox->end();
2462
    delete mWaitBox;
2463
    mWaitBox = nullptr;
2464
}
2465
 
273 andreas 2466
void MainWindow::pageFinished(ulong handle)
271 andreas 2467
{
2468
    DECL_TRACER("MainWindow::pageFinished(uint handle)");
2469
 
277 andreas 2470
    TObject::OBJECT_t *obj = findObject(handle);
273 andreas 2471
 
279 andreas 2472
    if (obj)
2473
    {
298 andreas 2474
        if (obj->type == TObject::OBJ_SUBPAGE && (obj->animate.showEffect == SE_NONE || obj->animate.showTime <= 0) && obj->object.widget)
295 andreas 2475
        {
297 andreas 2476
            if (!obj->object.widget->isEnabled())
2477
                obj->object.widget->setEnabled(true);
2478
 
295 andreas 2479
            obj->object.widget->lower();
279 andreas 2480
            obj->object.widget->show();
295 andreas 2481
            obj->object.widget->raise();
2482
        }
297 andreas 2483
 
295 andreas 2484
        if (obj->type == TObject::OBJ_SUBPAGE && obj->animate.showEffect != SE_NONE && obj->object.widget)
2485
        {
298 andreas 2486
            if (startAnimation(obj, obj->animate))
2487
                return;
295 andreas 2488
        }
279 andreas 2489
 
2490
        if ((obj->type == TObject::OBJ_PAGE || obj->type == TObject::OBJ_SUBPAGE) && obj->object.widget)
2491
        {
2492
            QObjectList list = obj->object.widget->children();
2493
            QObjectList::Iterator iter;
2494
 
2495
            for (iter = list.begin(); iter != list.end(); ++iter)
2496
            {
2497
                QObject *o = *iter;
297 andreas 2498
                ulong child = extractHandle(o->objectName().toStdString());
2499
                OBJECT_t *obj = nullptr;
279 andreas 2500
 
297 andreas 2501
                if (child && (obj = findObject(child)) != nullptr)
279 andreas 2502
                {
297 andreas 2503
                    if (obj->invalid && obj->type != OBJ_SUBPAGE)
2504
                        obj->invalid = false;
279 andreas 2505
 
297 andreas 2506
                    if (obj->remove)
2507
                        obj->remove = false;
2508
 
2509
                    switch (obj->type)
279 andreas 2510
                    {
297 andreas 2511
                        case OBJ_PAGE:
2512
                        case OBJ_SUBPAGE:
2513
                            if (obj->object.widget && !obj->invalid && obj->object.widget->isHidden())
2514
                            {
2515
                                if (!obj->object.widget->isEnabled())
2516
                                    obj->object.widget->setEnabled(true);
2517
 
298 andreas 2518
                                obj->object.widget->lower();
297 andreas 2519
                                obj->object.widget->show();
298 andreas 2520
                                obj->object.widget->raise();
297 andreas 2521
                            }
2522
                        break;
2523
 
2524
                        case OBJ_BUTTON:
2525
                            if (obj->object.label && obj->object.label->isHidden())
2526
                                obj->object.label->show();
2527
                        break;
2528
 
2529
                        case OBJ_INPUT:
2530
                        case OBJ_TEXT:
2531
                            if (obj->object.plaintext && obj->object.plaintext->isHidden())
2532
                                obj->object.plaintext->show();
2533
                        break;
2534
 
2535
                        case OBJ_LIST:
2536
                            if (obj->object.list && obj->object.list->isHidden())
2537
                                obj->object.list->show();
2538
                        break;
2539
 
2540
                        case OBJ_SUBVIEW:
2541
                            if (obj->object.area && obj->object.area->isHidden())
298 andreas 2542
                            {
2543
                                obj->object.area->lower();
297 andreas 2544
                                obj->object.area->show();
298 andreas 2545
                                obj->object.area->raise();
2546
                            }
297 andreas 2547
                        break;
2548
 
2549
                        case OBJ_VIDEO:
2550
                            if (obj->object.vwidget && obj->object.vwidget->isHidden())
2551
                                obj->object.vwidget->show();
2552
                        break;
2553
 
2554
                        default:
2555
                            MSG_WARNING("Object " << handleToString(child) << " is an invalid type!");
279 andreas 2556
                    }
2557
                }
297 andreas 2558
                else
2559
                {
2560
                    QString obName = o->objectName();
2561
 
2562
                    if (obName.startsWith("Label_"))
2563
                    {
2564
                        QLabel *l = dynamic_cast<QLabel *>(o);
2565
 
2566
                        if (l->isHidden())
2567
                            l->show();
2568
                    }
2569
                }
279 andreas 2570
            }
2571
        }
284 andreas 2572
 
296 andreas 2573
        if (obj->type == OBJ_SUBVIEW && obj->object.area)
285 andreas 2574
            obj->object.area->show();
332 andreas 2575
#if TESTMODE == 1
2576
        __success = true;
2577
#endif
279 andreas 2578
    }
297 andreas 2579
#ifdef QT_DEBUG
2580
    else
2581
    {
2582
        MSG_WARNING("Object " << handleToString(handle) << " not found!");
2583
    }
2584
#endif
332 andreas 2585
#if TESTMODE == 1
334 andreas 2586
    setScreenDone();
332 andreas 2587
#endif
271 andreas 2588
}
2589
 
61 andreas 2590
/**
2591
 * @brief MainWindow::appStateChanged - Is called whenever the state of the app changes.
2592
 * This callback method is called whenever the state of the application
2593
 * changes. This is mostly usefull on mobile devices. Whenever the main window
2594
 * looses the focus (screen closed, application is put into background, ...)
2595
 * this method is called and updates a flag. If the application is not able
2596
 * to draw to the screen (suspended) all events are cached. At the moment the
2597
 * application becomes active, all queued messages are applied.
2598
 * @param state     The new state of the application.
2599
 */
213 andreas 2600
void MainWindow::onAppStateChanged(Qt::ApplicationState state)
31 andreas 2601
{
265 andreas 2602
    DECL_TRACER("MainWindow::onAppStateChanged(Qt::ApplicationState state)");
31 andreas 2603
 
2604
    switch (state)
2605
    {
61 andreas 2606
        case Qt::ApplicationSuspended:              // Should not occure on a normal desktop
36 andreas 2607
            MSG_INFO("Switched to mode SUSPEND");
31 andreas 2608
            mHasFocus = false;
131 andreas 2609
#ifdef Q_OS_ANDROID
264 andreas 2610
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
131 andreas 2611
            QAndroidJniObject::callStaticMethod<void>("org/qtproject/theosys/Orientation", "pauseOrientationListener", "()V");
182 andreas 2612
#else
2613
            QJniObject::callStaticMethod<void>("org/qtproject/theosys/Orientation", "pauseOrientationListener", "()V");
2614
#endif
252 andreas 2615
//#elif defined(Q_OS_IOS)
2616
//            if (mIosRotate)
2617
//                mIosRotate->automaticRotation(false);
183 andreas 2618
#endif
31 andreas 2619
        break;
61 andreas 2620
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)      // On a normal desktop we can ignore this signals
31 andreas 2621
        case Qt::ApplicationInactive:
36 andreas 2622
            MSG_INFO("Switched to mode INACTIVE");
31 andreas 2623
            mHasFocus = false;
142 andreas 2624
            mWasInactive = true;
235 andreas 2625
#ifdef Q_OS_ANDROID
264 andreas 2626
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
131 andreas 2627
            QAndroidJniObject::callStaticMethod<void>("org/qtproject/theosys/Orientation", "pauseOrientationListener", "()V");
182 andreas 2628
#else
2629
            QJniObject::callStaticMethod<void>("org/qtproject/theosys/Orientation", "pauseOrientationListener", "()V");
2630
#endif
235 andreas 2631
#endif
31 andreas 2632
        break;
2633
 
2634
        case Qt::ApplicationHidden:
36 andreas 2635
            MSG_INFO("Switched to mode HIDDEN");
31 andreas 2636
            mHasFocus = false;
235 andreas 2637
#ifdef Q_OS_ANDROID
264 andreas 2638
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
131 andreas 2639
            QAndroidJniObject::callStaticMethod<void>("org/qtproject/theosys/Orientation", "pauseOrientationListener", "()V");
182 andreas 2640
#else
2641
            QJniObject::callStaticMethod<void>("org/qtproject/theosys/Orientation", "pauseOrientationListener", "()V");
2642
#endif
235 andreas 2643
#endif
31 andreas 2644
        break;
61 andreas 2645
#endif
31 andreas 2646
        case Qt::ApplicationActive:
36 andreas 2647
            MSG_INFO("Switched to mode ACTIVE");
31 andreas 2648
            mHasFocus = true;
38 andreas 2649
 
92 andreas 2650
            if (!isRunning && gPageManager)
38 andreas 2651
            {
2652
                // Start the core application
92 andreas 2653
                gPageManager->startUp();
2654
                gPageManager->run();
38 andreas 2655
                isRunning = true;
142 andreas 2656
                mWasInactive = false;
252 andreas 2657
#ifdef Q_OS_IOS
2658
                if (!mSource)
2659
                    initGeoLocation();
2660
 
2661
                if (mSource)
2662
                {
2663
                    mSource->startUpdates();
2664
                    MSG_DEBUG("Geo location updates started.");
2665
                }
2666
                else
2667
                {
2668
                    MSG_WARNING("Geo location is not available!");
2669
                }
2670
 
2671
                if (mSensor)
2672
                {
263 andreas 2673
                    if (mIosRotate && mOrientation == Qt::PrimaryOrientation) // Unknown?
2674
                    {
2675
                        switch(mIosRotate->getCurrentOrientation())
2676
                        {
2677
                            case O_PORTRAIT:            mOrientation = Qt::PortraitOrientation; break;
2678
                            case O_REVERSE_PORTRAIT:    mOrientation = Qt::InvertedPortraitOrientation; break;
2679
                            case O_REVERSE_LANDSCAPE:   mOrientation = Qt::InvertedLandscapeOrientation; break;
2680
                            case O_LANDSCAPE:           mOrientation = Qt::LandscapeOrientation; break;
2681
                        }
2682
                    }
2683
#ifdef QT_DEBUG
2684
                    MSG_DEBUG("Orientation after activate: " << orientationToString(mOrientation));
2685
#endif
2686
                    if (gPageManager && mIosRotate && gPageManager->getSettings()->isPortrait() &&
2687
                        mOrientation != Qt::PortraitOrientation)
264 andreas 2688
                    {
263 andreas 2689
                        mIosRotate->rotate(O_PORTRAIT);
264 andreas 2690
                        mOrientation = Qt::PortraitOrientation;
2691
                    }
263 andreas 2692
                    else if (gPageManager && mIosRotate && mOrientation != Qt::LandscapeOrientation)
264 andreas 2693
                    {
263 andreas 2694
                        mIosRotate->rotate(O_LANDSCAPE);
264 andreas 2695
                        mOrientation = Qt::LandscapeOrientation;
2696
                    }
263 andreas 2697
 
2698
                    setNotch();
252 andreas 2699
                }
2700
#endif
38 andreas 2701
            }
2702
            else
142 andreas 2703
            {
264 andreas 2704
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) && defined(Q_OS_ANDROID)
217 andreas 2705
                _freezeWorkaround();
2706
#endif
38 andreas 2707
                playShowList();
142 andreas 2708
 
2709
                if (mDoRepaint && mWasInactive)
2710
                    repaintObjects();
2711
 
2712
                mDoRepaint = false;
2713
                mWasInactive = false;
2714
            }
131 andreas 2715
#ifdef Q_OS_ANDROID
264 andreas 2716
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
271 andreas 2717
            QAndroidJniObject activity = QtAndroid::androidActivity();
2718
            QAndroidJniObject::callStaticMethod<void>("org/qtproject/theosys/HideToolbar", "hide", "(Landroid/app/Activity;Z)V", activity.object(), true);
131 andreas 2719
            QAndroidJniObject::callStaticMethod<void>("org/qtproject/theosys/Orientation", "resumeOrientationListener", "()V");
182 andreas 2720
#else
271 andreas 2721
            QJniObject activity = QNativeInterface::QAndroidApplication::context();
2722
            QJniObject::callStaticMethod<void>("org/qtproject/theosys/HideToolbar", "hide", "(Landroid/app/Activity;Z)V", activity.object(), true);
182 andreas 2723
            QJniObject::callStaticMethod<void>("org/qtproject/theosys/Orientation", "resumeOrientationListener", "()V");
131 andreas 2724
#endif
182 andreas 2725
#endif
249 andreas 2726
#ifdef Q_OS_IOS
259 andreas 2727
            if (mIOSSettingsActive)
2728
            {
2729
                mIOSSettingsActive = false;
2730
                MSG_DEBUG("Activating settings");
2731
                activateSettings(QASettings::getOldNetlinx(),
2732
                                  QASettings::getOldPort(),
2733
                                  QASettings::getOoldChannelID(),
2734
                                  QASettings::getOldSurface(),
2735
                                  QASettings::getOldToolbarSuppress(),
2736
                                  QASettings::getOldToolbarForce());
2737
            }
249 andreas 2738
#endif
326 andreas 2739
#if TESTMODE == 1
330 andreas 2740
            if (_gTestMode)
2741
                _gTestMode->run();
2742
 
2743
            _run_test_ready = true;
326 andreas 2744
#endif
31 andreas 2745
        break;
264 andreas 2746
#if not defined(Q_OS_IOS) && not defined(Q_OS_ANDROID)
61 andreas 2747
        default:
2748
            mHasFocus = true;
2749
#endif
31 andreas 2750
    }
235 andreas 2751
#if defined(Q_OS_ANDROID)
92 andreas 2752
    if (mHasFocus && gPageManager)
38 andreas 2753
    {
92 andreas 2754
        gPageManager->initNetworkState();
2755
        gPageManager->initBatteryState();
38 andreas 2756
    }
92 andreas 2757
    else if (gPageManager)
38 andreas 2758
    {
92 andreas 2759
        gPageManager->stopNetworkState();
2760
        gPageManager->stopBatteryState();
38 andreas 2761
    }
37 andreas 2762
#endif
31 andreas 2763
}
2764
 
64 andreas 2765
void MainWindow::_shutdown()
2766
{
2767
    DECL_TRACER("MainWindow::_shutdown()")
2768
 
2769
    close();
2770
}
2771
 
32 andreas 2772
/******************* Signal handling *************************/
2773
 
44 andreas 2774
void MainWindow::_resetSurface()
2775
{
2776
    DECL_TRACER("MainWindow::_resetSurface()");
2777
 
90 andreas 2778
    // Start over by exiting this class
2779
    MSG_INFO("Program will start over!");
2780
    _restart_ = true;
2781
    prg_stopped = true;
2782
    killed = true;
88 andreas 2783
 
90 andreas 2784
    if (gAmxNet)
2785
        gAmxNet->stop();
88 andreas 2786
 
90 andreas 2787
    close();
44 andreas 2788
}
2789
 
298 andreas 2790
void MainWindow::_displayButton(ulong handle, ulong parent, TBitmap buffer, int width, int height, int left, int top, bool passthrough)
4 andreas 2791
{
298 andreas 2792
    DECL_TRACER("MainWindow::_displayButton(ulong handle, ulong parent, TBitmap buffer, int width, int height, int left, int top, bool passthrough)");
14 andreas 2793
 
21 andreas 2794
    if (prg_stopped)
2795
        return;
2796
 
61 andreas 2797
    if (!mHasFocus)     // Suspended?
2798
    {
298 andreas 2799
        addButton(handle, parent, buffer, left, top, width, height, passthrough);
61 andreas 2800
        return;
2801
    }
2802
 
298 andreas 2803
    emit sigDisplayButton(handle, parent, buffer, width, height, left, top, passthrough);
14 andreas 2804
}
2805
 
289 andreas 2806
void MainWindow::_displayViewButton(ulong handle, ulong parent, bool vertical, TBitmap buffer, int width, int height, int left, int top, int space, TColor::COLOR_T fillColor)
279 andreas 2807
{
289 andreas 2808
    DECL_TRACER("MainWindow::_displayViewButton(ulong handle, ulong parent, TBitmap buffer, int width, int height, int left, int top)");
279 andreas 2809
 
2810
    if (prg_stopped)
2811
        return;
2812
 
2813
    if (!mHasFocus)     // Suspended?
2814
    {
289 andreas 2815
        addViewButton(handle, parent, vertical, buffer, left, top, width, height, space, fillColor);
279 andreas 2816
        return;
2817
    }
2818
 
289 andreas 2819
    emit sigDisplayViewButton(handle, parent, vertical, buffer, width, height, left, top, space, fillColor);
279 andreas 2820
}
2821
 
285 andreas 2822
void MainWindow::_addViewButtonItems(ulong parent, vector<PGSUBVIEWITEM_T> items)
279 andreas 2823
{
285 andreas 2824
    DECL_TRACER("MainWindow::_addViewButtonItems(ulong parent, vector<PGSUBVIEWITEM_T> items)");
279 andreas 2825
 
2826
    if (prg_stopped)
2827
        return;
2828
 
2829
    try
2830
    {
285 andreas 2831
        emit sigAddViewButtonItems(parent, items);
279 andreas 2832
    }
280 andreas 2833
    catch(std::exception& e)
2834
    {
2835
        MSG_ERROR("Error triggering function \"addViewButtonItems()\": " << e.what());
2836
    }
2837
}
2838
 
300 andreas 2839
void MainWindow::_updateViewButton(ulong handle, ulong parent, TBitmap buffer, TColor::COLOR_T fillColor)
2840
{
2841
    DECL_TRACER("MainWindow::_updateViewButton(ulong handle, ulong parent, TBitmap buffer, TColor::COLOR_T fillColor)");
2842
 
2843
    if (prg_stopped)
2844
        return;
2845
 
2846
    try
2847
    {
2848
        emit sigUpdateViewButton(handle, parent, buffer, fillColor);
2849
    }
2850
    catch (std::exception& e)
2851
    {
2852
        MSG_ERROR("Error triggering function \"updateViewButton()\": " << e.what());
2853
    }
2854
}
2855
 
2856
void MainWindow::_updateViewButtonItem(PGSUBVIEWITEM_T& item, ulong parent)
2857
{
2858
    DECL_TRACER("MainWindow::_updateViewButtonItem(PGSUBVIEWITEM_T& item, ulong parent)");
2859
 
2860
    if (prg_stopped)
2861
        return;
2862
 
2863
    try
2864
    {
2865
        emit sigUpdateViewButtonItem(item, parent);
2866
    }
2867
    catch (std::exception& e)
2868
    {
2869
        MSG_ERROR("Error triggering function \"updateViewButtonItem()\": " << e.what());
2870
    }
2871
}
2872
 
2873
void MainWindow::_showViewButtonItem(ulong handle, ulong parent, int position, int timer)
2874
{
2875
    DECL_TRACER("MainWindow::_showViewButtonItem(ulong handle, ulong parent, int position, int timer)");
2876
 
2877
    if (prg_stopped)
2878
        return;
2879
 
2880
    try
2881
    {
303 andreas 2882
        emit sigShowViewButtonItem(handle, parent, position, timer);
300 andreas 2883
    }
2884
    catch (std::exception& e)
2885
    {
2886
        MSG_ERROR("Error triggering function \"showViewButtonItem()\": " << e.what());
2887
    }
2888
}
2889
 
318 andreas 2890
void MainWindow::_hideAllViewItems(ulong handle)
2891
{
2892
    DECL_TRACER("MainWindow::_hideAllViewItems(ulong handle)");
2893
 
2894
    if (prg_stopped)
2895
        return;
2896
 
2897
    emit sigHideAllViewItems(handle);
2898
}
2899
 
2900
void MainWindow::_toggleViewButtonItem(ulong handle, ulong parent, int position, int timer)
2901
{
2902
    DECL_TRACER("MainWindow::_toggleViewButtonItem(ulong handle, ulong parent, int position, int timer)");
2903
 
2904
    if (prg_stopped)
2905
        return;
2906
 
2907
    emit sigToggleViewButtonItem(handle, parent, position, timer);
2908
}
2909
 
2910
void MainWindow::_hideViewItem(ulong handle, ulong parent)
2911
{
2912
    DECL_TRACER("MainWindow::_hideViewItem(ulong handle, ulong parent)");
2913
 
2914
    if (prg_stopped)
2915
        return;
2916
 
2917
    emit sigHideViewItem(handle, parent);
2918
}
2919
 
98 andreas 2920
void MainWindow::_setVisible(ulong handle, bool state)
2921
{
2922
    DECL_TRACER("MainWindow::_setVisible(ulong handle, bool state)");
2923
 
2924
    if (prg_stopped)
2925
        return;
2926
 
2927
    emit sigSetVisible(handle, state);
2928
}
2929
 
318 andreas 2930
void MainWindow::_setSubViewPadding(ulong handle, int padding)
2931
{
2932
    DECL_TRACER("MainWindow::_setSubViewPadding(ulong handle, int padding)");
2933
 
2934
    if (prg_stopped)
2935
        return;
2936
 
2937
    emit sigSetSubViewPadding(handle, padding);
2938
}
2939
 
14 andreas 2940
void MainWindow::_setPage(ulong handle, int width, int height)
2941
{
2942
    DECL_TRACER("MainWindow::_setPage(ulong handle, int width, int height)");
2943
 
21 andreas 2944
    if (prg_stopped)
2945
        return;
2946
 
61 andreas 2947
    if (!mHasFocus)
2948
    {
2949
        addPage(handle, width, height);
2950
        return;
2951
    }
2952
 
14 andreas 2953
    emit sigSetPage(handle, width, height);
2954
}
2955
 
217 andreas 2956
void MainWindow::_setSubPage(ulong handle, ulong parent, int left, int top, int width, int height, ANIMATION_t animate)
14 andreas 2957
{
217 andreas 2958
    DECL_TRACER("MainWindow::_setSubPage(ulong handle, ulong parent, int left, int top, int width, int height)");
14 andreas 2959
 
21 andreas 2960
    if (prg_stopped)
2961
        return;
2962
 
61 andreas 2963
    if (!mHasFocus)
2964
    {
217 andreas 2965
        addSubPage(handle, parent, left, top, width, height, animate);
61 andreas 2966
        return;
2967
    }
2968
 
217 andreas 2969
    emit sigSetSubPage(handle, parent, left, top, width, height, animate);
298 andreas 2970
//#ifndef Q_OS_ANDROID
2971
//    std::this_thread::sleep_for(std::chrono::milliseconds(THREAD_WAIT));
2972
//#endif
14 andreas 2973
}
2974
 
262 andreas 2975
#ifdef _OPAQUE_SKIA_
289 andreas 2976
void MainWindow::_setBackground(ulong handle, TBitmap image, int width, int height, ulong color)
262 andreas 2977
#else
289 andreas 2978
void MainWindow::_setBackground(ulong handle, TBitmap image, int width, int height, ulong color, int opacity)
262 andreas 2979
#endif
14 andreas 2980
{
289 andreas 2981
    DECL_TRACER("MainWindow::_setBackground(ulong handle, TBitmap image, int width, int height, ulong color [, int opacity])");
14 andreas 2982
 
21 andreas 2983
    if (prg_stopped)
2984
        return;
2985
 
61 andreas 2986
    if (!mHasFocus)
2987
    {
262 andreas 2988
#ifdef _OPAQUE_SKIA_
289 andreas 2989
        addBackground(handle, image, width, height, color);
262 andreas 2990
#else
289 andreas 2991
        addBackground(handle, image, width, height, color, opacity);
262 andreas 2992
#endif
61 andreas 2993
        return;
2994
    }
2995
 
262 andreas 2996
#ifdef _OPAQUE_SKIA_
289 andreas 2997
    emit sigSetBackground(handle, image, width, height, color);
262 andreas 2998
#else
289 andreas 2999
    emit sigSetBackground(handle, image, width, height, color, opacity);
57 andreas 3000
#endif
14 andreas 3001
}
3002
 
3003
void MainWindow::_dropPage(ulong handle)
3004
{
3005
    DECL_TRACER("MainWindow::_dropPage(ulong handle)");
3006
 
70 andreas 3007
    doReleaseButton();
3008
 
61 andreas 3009
    if (!mHasFocus)
3010
    {
3011
        markDrop(handle);
3012
        return;
3013
    }
3014
 
14 andreas 3015
    emit sigDropPage(handle);
3016
}
3017
 
350 andreas 3018
void MainWindow::_dropSubPage(ulong handle, ulong parent)
14 andreas 3019
{
350 andreas 3020
    DECL_TRACER("MainWindow::_dropSubPage(ulong handle, ulong parent)");
14 andreas 3021
 
70 andreas 3022
    doReleaseButton();
3023
 
61 andreas 3024
    if (!mHasFocus)
3025
    {
3026
        markDrop(handle);
3027
        return;
3028
    }
3029
 
350 andreas 3030
    emit sigDropSubPage(handle, parent);
14 andreas 3031
}
3032
 
98 andreas 3033
void MainWindow::_dropButton(ulong handle)
3034
{
3035
    DECL_TRACER("MainWindow::_dropButton(ulong handle)");
3036
 
3037
    if (!mHasFocus)
3038
    {
3039
        markDrop(handle);
3040
        return;
3041
    }
3042
 
3043
    emit sigDropButton(handle);
3044
}
3045
 
21 andreas 3046
void MainWindow::_playVideo(ulong handle, ulong parent, int left, int top, int width, int height, const string& url, const string& user, const string& pw)
3047
{
3048
    DECL_TRACER("MainWindow::_playVideo(ulong handle, const string& url)");
3049
 
3050
    if (prg_stopped)
3051
        return;
3052
 
61 andreas 3053
    if (!mHasFocus)
3054
    {
3055
        addVideo(handle, parent, left, top, width, height, url, user, pw);
3056
        return;
3057
    }
3058
 
21 andreas 3059
    emit sigPlayVideo(handle, parent, left, top, width, height, url, user, pw);
298 andreas 3060
//#ifndef Q_OS_ANDROID
3061
//    std::this_thread::sleep_for(std::chrono::milliseconds(THREAD_WAIT));
3062
//#endif
21 andreas 3063
}
3064
 
291 andreas 3065
void MainWindow::_inputText(Button::TButton *button, Button::BITMAP_t& bm, int frame)
50 andreas 3066
{
291 andreas 3067
    DECL_TRACER("MainWindow::_inputText(Button::TButton *button, Button::BITMAP_t& bm, int frame)");
50 andreas 3068
 
291 andreas 3069
    if (prg_stopped || !button)
50 andreas 3070
        return;
3071
 
61 andreas 3072
    if (!mHasFocus)
3073
    {
291 andreas 3074
        addInText(button->getHandle(), button, bm, frame);
61 andreas 3075
        return;
3076
    }
3077
 
52 andreas 3078
    QByteArray buf;
3079
 
3080
    if (bm.buffer && bm.rowBytes > 0)
3081
    {
363 andreas 3082
        size_t size = (size_t)bm.width * (size_t)bm.height * (size_t)(bm.rowBytes / bm.width);
52 andreas 3083
        buf.insert(0, (const char *)bm.buffer, size);
3084
    }
57 andreas 3085
 
192 andreas 3086
    emit sigInputText(button, buf, bm.width, bm.height, frame, bm.rowBytes);
298 andreas 3087
//#ifndef Q_OS_ANDROID
3088
//    std::this_thread::sleep_for(std::chrono::milliseconds(THREAD_WAIT));
3089
//#endif
50 andreas 3090
}
3091
 
291 andreas 3092
void MainWindow::_listBox(Button::TButton *button, Button::BITMAP_t& bm, int frame)
200 andreas 3093
{
279 andreas 3094
    DECL_TRACER("MainWindow::_listBox(Button::TButton& button, Button::BITMAP_t& bm, int frame)");
200 andreas 3095
 
279 andreas 3096
    if (prg_stopped)
200 andreas 3097
        return;
3098
 
3099
    if (!mHasFocus)
3100
    {
291 andreas 3101
        addListBox(button, bm, frame);
200 andreas 3102
        return;
3103
    }
3104
 
3105
    QByteArray buf;
3106
 
3107
    if (bm.buffer && bm.rowBytes > 0)
3108
    {
363 andreas 3109
        size_t size = (size_t)bm.width * (size_t)bm.height * (size_t)(bm.rowBytes / bm.width);
200 andreas 3110
        buf.insert(0, (const char *)bm.buffer, size);
3111
    }
3112
 
3113
    emit sigListBox(button, buf, bm.width, bm.height, frame, bm.rowBytes);
3114
}
3115
 
63 andreas 3116
void MainWindow::_showKeyboard(const std::string& init, const std::string& prompt, bool priv)
62 andreas 3117
{
63 andreas 3118
    DECL_TRACER("MainWindow::_showKeyboard(std::string &init, std::string &prompt, bool priv)");
62 andreas 3119
 
3120
    if (prg_stopped)
3121
        return;
3122
 
70 andreas 3123
    doReleaseButton();
63 andreas 3124
    emit sigKeyboard(init, prompt, priv);
62 andreas 3125
}
3126
 
63 andreas 3127
void MainWindow::_showKeypad(const std::string& init, const std::string& prompt, bool priv)
62 andreas 3128
{
63 andreas 3129
    DECL_TRACER("MainWindow::_showKeypad(std::string &init, std::string &prompt, bool priv)");
62 andreas 3130
 
3131
    if (prg_stopped)
3132
        return;
3133
 
70 andreas 3134
    doReleaseButton();
63 andreas 3135
    emit sigKeypad(init, prompt, priv);
62 andreas 3136
}
3137
 
63 andreas 3138
void MainWindow::_resetKeyboard()
3139
{
3140
    DECL_TRACER("MainWindow::_resetKeyboard()");
3141
 
3142
    emit sigResetKeyboard();
3143
}
3144
 
64 andreas 3145
void MainWindow::_showSetup()
3146
{
3147
    DECL_TRACER("MainWindow::_showSetup()");
3148
 
3149
    emit sigShowSetup();
3150
}
3151
 
71 andreas 3152
void MainWindow::_playSound(const string& file)
3153
{
3154
    DECL_TRACER("MainWindow::_playSound(const string& file)");
3155
 
3156
    emit sigPlaySound(file);
3157
}
3158
 
141 andreas 3159
void MainWindow::_stopSound()
3160
{
3161
    DECL_TRACER("MainWindow::_stopSound()");
3162
 
3163
    emit sigStopSound();
3164
}
3165
 
3166
void MainWindow::_muteSound(bool state)
3167
{
3168
    DECL_TRACER("MainWindow::_muteSound(bool state)");
3169
 
156 andreas 3170
    emit sigMuteSound(state);
141 andreas 3171
}
3172
 
335 andreas 3173
void MainWindow::_setVolume(int volume)
3174
{
3175
    DECL_TRACER("MainWindow::_setVolume(int volume)");
3176
 
3177
    emit sigSetVolume(volume);
3178
}
3179
 
88 andreas 3180
void MainWindow::_setOrientation(J_ORIENTATION ori)
3181
{
182 andreas 3182
#ifdef Q_OS_ANDROID
88 andreas 3183
    DECL_TRACER("MainWindow::_setOriantation(J_ORIENTATION ori)");
3184
 
134 andreas 3185
    if (ori == O_FACE_UP || ori == O_FACE_DOWN)
3186
        return;
264 andreas 3187
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
88 andreas 3188
    QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
182 andreas 3189
#else
183 andreas 3190
    QJniObject activity = QJniObject::callStaticObjectMethod("org/qtproject/qt/android/QtNative", "activity", "()Landroid/app/Activity;");
182 andreas 3191
#endif
88 andreas 3192
    if ( activity.isValid() )
3193
    {
3194
        activity.callMethod<void>
3195
                ("setRequestedOrientation"  // method name
3196
                 , "(I)V"                   // signature
3197
                 , ori);
131 andreas 3198
 
3199
        switch(ori)
3200
        {
3201
            case O_LANDSCAPE:           mOrientation = Qt::LandscapeOrientation; break;
3202
            case O_PORTRAIT:            mOrientation = Qt::PortraitOrientation; break;
3203
            case O_REVERSE_LANDSCAPE:   mOrientation = Qt::InvertedLandscapeOrientation; break;
3204
            case O_REVERSE_PORTRAIT:    mOrientation = Qt::InvertedPortraitOrientation; break;
3205
            default:
3206
                MSG_WARNING("Orientation is undefined!");
3207
                mOrientation = Qt::PrimaryOrientation;
3208
        }
88 andreas 3209
    }
243 andreas 3210
#elif defined(Q_OS_IOS)
252 andreas 3211
    if (mIosRotate)
264 andreas 3212
    {
252 andreas 3213
        mIosRotate->rotate(ori);
264 andreas 3214
#ifdef QT_DEBUG
3215
        string msg;
3216
 
3217
        switch(ori)
3218
        {
3219
            case O_LANDSCAPE:           msg = "LANDSCAPE"; break;
3220
            case O_PORTRAIT:            msg = "PORTRAIT"; break;
3221
            case O_REVERSE_PORTRAIT:    msg = "INVERTED PORTRAIT"; break;
3222
            case O_REVERSE_LANDSCAPE:   msg = "INVERTED LANDSCAPE"; break;
3223
            default:
3224
                msg = "unknown";
3225
        }
3226
 
3227
        MSG_DEBUG("Rotated to " << msg);
3228
#endif
3229
    }
237 andreas 3230
#else
3231
    Q_UNUSED(ori);
88 andreas 3232
#endif
3233
}
3234
 
111 andreas 3235
void MainWindow::_sendVirtualKeys(const string& str)
3236
{
3237
    DECL_TRACER("MainWindow::_sendVirtualKeys(const string& str)");
3238
 
3239
    emit sigSendVirtualKeys(str);
3240
}
3241
 
140 andreas 3242
void MainWindow::_showPhoneDialog(bool state)
3243
{
3244
    DECL_TRACER("MainWindow::_showPhoneDialog(bool state)");
3245
 
3246
    emit sigShowPhoneDialog(state);
3247
}
3248
 
3249
void MainWindow::_setPhoneNumber(const std::string& number)
3250
{
3251
    DECL_TRACER("MainWindow::_setPhoneNumber(const std::string& number)");
3252
 
3253
    emit sigSetPhoneNumber(number);
3254
}
3255
 
3256
void MainWindow::_setPhoneStatus(const std::string& msg)
3257
{
3258
    DECL_TRACER("MainWindow::_setPhoneStatus(const std::string& msg)");
3259
 
3260
    emit sigSetPhoneStatus(msg);
3261
}
3262
 
141 andreas 3263
void MainWindow::_setPhoneState(int state, int id)
3264
{
3265
    DECL_TRACER("MainWindow::_setPhoneState(int state, int id)");
3266
 
3267
    emit sigSetPhoneState(state, id);
3268
}
3269
 
179 andreas 3270
void MainWindow::_onProgressChanged(int percent)
3271
{
3272
    DECL_TRACER("MainWindow::_onProgressChanged(int percent)");
3273
 
3274
    emit sigOnProgressChanged(percent);
3275
}
3276
 
206 andreas 3277
void MainWindow::_displayMessage(const string &msg, const string &title)
3278
{
3279
    DECL_TRACER("MainWindow::_displayMessage(const string &msg, const string &title)");
3280
 
3281
    emit sigDisplayMessage(msg, title);
3282
}
3283
 
209 andreas 3284
void MainWindow::_fileDialog(ulong handle, const string &path, const std::string& extension, const std::string& suffix)
3285
{
3286
    DECL_TRACER("MainWindow::_fileDialog(ulong handle, const string &path, const std::string& extension, const std::string& suffix)");
3287
 
3288
    if (!handle || path.empty())
3289
    {
3290
        MSG_WARNING("Invalid parameter handle or no path!");
3291
        return;
3292
    }
3293
 
3294
    emit sigFileDialog(handle, path, extension, suffix);
3295
}
252 andreas 3296
 
197 andreas 3297
void MainWindow::_setSizeMainWindow(int width, int height)
3298
{
252 andreas 3299
#if !defined (Q_OS_ANDROID) && !defined(Q_OS_IOS)
197 andreas 3300
    DECL_TRACER("MainWindow::_setSizeMainWindow(int width, int height)");
3301
 
3302
    emit sigSetSizeMainWindow(width, height);
252 andreas 3303
#else
3304
    Q_UNUSED(width);
3305
    Q_UNUSED(height);
3306
#endif
197 andreas 3307
}
252 andreas 3308
 
279 andreas 3309
void MainWindow::_listViewArea(ulong handle, ulong parent, Button::TButton& button, SUBVIEWLIST_T& list)
3310
{
3311
    DECL_TRACER("MainWindow::_listViewArea(ulong handle, ulong parent, Button::TButton *button, SUBVIEWLIST_T& list)");
3312
 
3313
    if (!handle || !parent || list.id <= 0)
3314
    {
3315
        MSG_WARNING("Invalid parameters for scroll area!");
3316
        return;
3317
    }
3318
 
3319
    emit sigListViewArea(handle, parent, button, list);
3320
}
3321
 
70 andreas 3322
void MainWindow::doReleaseButton()
3323
{
3324
    DECL_TRACER("MainWindow::doReleaseButton()");
3325
 
92 andreas 3326
    if (mLastPressX >= 0 && mLastPressX >= 0 && gPageManager)
70 andreas 3327
    {
3328
        MSG_DEBUG("Sending outstanding mouse release event for coordinates x" << mLastPressX << ", y" << mLastPressY);
3329
        int x = mLastPressX;
3330
        int y = mLastPressY;
3331
 
3332
        if (isScaled())
3333
        {
3334
            x = (int)((double)x / mScaleFactor);
3335
            y = (int)((double)y / mScaleFactor);
3336
        }
3337
 
92 andreas 3338
        gPageManager->mouseEvent(x, y, false);
334 andreas 3339
        mLastPressX = mLastPressY = -1;
70 andreas 3340
    }
3341
}
3342
 
142 andreas 3343
/**
3344
 * @brief MainWindow::repaintObjects
3345
 * On a mobile device it is possible that the content, mostly the background, of
3346
 * a window becomes destroyed and it is not repainted. This may be the case at
3347
 * the moment where the device was disconnected from the controller and
3348
 * reconnected while the application was inactive. In such a case usualy the
3349
 * surface is completely repainted. But because of inactivity this was not
3350
 * possible and some components may look destroyed. They are still functional
3351
 * allthough.
3352
 */
3353
void MainWindow::repaintObjects()
3354
{
3355
    DECL_TRACER("MainWindow::repaintObjects()");
3356
 
292 andreas 3357
    TLOCKER(draw_mutex);
154 andreas 3358
#ifdef Q_OS_ANDROID
3359
    std::this_thread::sleep_for(std::chrono::milliseconds(200));    // Necessary for slow devices
3360
#endif
277 andreas 3361
    TObject::OBJECT_t *obj = findFirstWindow();
142 andreas 3362
 
3363
    while (obj)
3364
    {
297 andreas 3365
        if (!obj->remove && !obj->invalid && obj->object.widget)
148 andreas 3366
        {
271 andreas 3367
            MSG_PROTOCOL("Refreshing widget " << handleToString (obj->handle));
154 andreas 3368
            obj->object.widget->repaint();
148 andreas 3369
        }
142 andreas 3370
 
277 andreas 3371
        obj = findNextWindow(obj);
142 andreas 3372
    }
3373
}
3374
 
297 andreas 3375
void MainWindow::refresh(ulong handle)
3376
{
3377
    DECL_TRACER("MainWindow::refresh(ulong handle)");
3378
 
3379
    if (!handle)
3380
        return;
3381
 
3382
    OBJECT_t *obj = findFirstChild(handle);
3383
 
3384
    while (obj)
3385
    {
3386
        MSG_DEBUG("Object " << handleToString(obj->handle) << " of type " << objectToString(obj->type) << ". Invalid: " << (obj->invalid ? "YES" : "NO") << ", Pointer: " << (obj->object.widget ? "YES" : "NO"));
3387
 
3388
        if (obj->type == OBJ_SUBVIEW && !obj->invalid && obj->object.area)
3389
        {
3390
            obj->object.area->setHidden(true);
3391
            obj->object.area->setHidden(false);
3392
            obj->object.area->setEnabled(true);
3393
            MSG_DEBUG("Subview refreshed");
3394
        }
3395
        else if (obj->type == OBJ_LIST && !obj->invalid && obj->object.list)
3396
        {
3397
            if (!obj->object.list->isEnabled())
3398
                obj->object.list->setEnabled(true);
3399
        }
3400
        else if (obj->type == OBJ_BUTTON && !obj->invalid && obj->object.label)
3401
        {
3402
            if (!obj->object.label->isEnabled())
3403
                obj->object.label->setEnabled(true);
3404
        }
3405
        else if ((obj->type == OBJ_SUBPAGE || obj->type == OBJ_PAGE) && !obj->invalid && obj->object.widget)
3406
        {
3407
            if (!obj->object.widget->isEnabled())
3408
                obj->object.widget->setEnabled(true);
3409
        }
3410
 
3411
        obj = findNextChild(obj->handle);
3412
    }
3413
}
3414
 
141 andreas 3415
int MainWindow::calcVolume(int value)
3416
{
3417
    DECL_TRACER("TQtSettings::calcVolume(int value)");
3418
 
3419
    // volumeSliderValue is in the range [0..100]
335 andreas 3420
//#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
141 andreas 3421
    qreal linearVolume = QAudio::convertVolume(value / qreal(100.0),
3422
                                               QAudio::LogarithmicVolumeScale,
3423
                                               QAudio::LinearVolumeScale);
3424
 
3425
    return qRound(linearVolume * 100);
335 andreas 3426
//#else
3427
//    return value;
3428
//#endif
141 andreas 3429
}
197 andreas 3430
 
263 andreas 3431
/**
3432
 * @brief MainWindow::convertMask
3433
 * Converts the AMX mask for input lines into Qt mask sympols for input lines.
3434
 *
3435
 * @param mask  A string containing the AMX mask symbols.
3436
 *
3437
 * @return The converted mask string for Qt input lines.
3438
 */
224 andreas 3439
string MainWindow::convertMask(const string& mask)
3440
{
3441
    DECL_TRACER("MainWindow::convertMask(const string& mask)");
3442
 
3443
    string qMask;
3444
 
3445
    for (size_t i = 0; i < mask.length(); ++i)
3446
    {
3447
        switch (mask[i])
3448
        {
3449
            case '0': qMask += "9"; break;
3450
            case '9': qMask += "0"; break;
3451
            case 'A': qMask += "N"; break;
3452
            case 'a': qMask += "n"; break;
3453
            case 'L': qMask += "X"; break;
3454
            case '?': qMask += "x"; break;
3455
            case '&': qMask += "A"; break;
3456
            case 'C': qMask += "a"; break;
3457
            case '^': qMask += ";"; break;
3458
 
3459
            default:
3460
                qMask += mask[i];
3461
        }
3462
    }
3463
 
3464
    return qMask;
3465
}
3466
 
266 andreas 3467
#ifdef Q_OS_ANDROID
3468
void MainWindow::hideAndroidBars()
3469
{
3470
    DECL_TRACER("MainWindow::hideAndroidBars()");
3471
}
3472
#endif
252 andreas 3473
#ifdef Q_OS_IOS
3474
void MainWindow::setNotch()
3475
{
3476
    DECL_TRACER("MainWindow::setNotch()");
3477
 
264 andreas 3478
    Qt::ScreenOrientation so = getRealOrientation();
3479
 
3480
    if (so == Qt::PrimaryOrientation)
263 andreas 3481
        return;
3482
 
3483
    QMargins margins;
3484
 
264 andreas 3485
    if (mHaveNotchPortrait && (so == Qt::PortraitOrientation || so == Qt::InvertedPortraitOrientation))
263 andreas 3486
        margins = mNotchPortrait;
264 andreas 3487
    else if (mHaveNotchLandscape && (so == Qt::LandscapeOrientation || so == Qt::InvertedLandscapeOrientation))
263 andreas 3488
        margins = mNotchLandscape;
3489
    else
252 andreas 3490
    {
263 andreas 3491
        margins = QASettings::getNotchSize();
3492
 
3493
        if (gPageManager && gPageManager->getSettings()->isPortrait())
3494
        {
264 andreas 3495
            if (so == Qt::LandscapeOrientation)
263 andreas 3496
            {
3497
                int left = margins.left();
3498
                int top = margins.top();
3499
                margins.setTop(margins.right());
3500
                margins.setLeft(top);
3501
                margins.setRight(margins.bottom());
3502
                margins.setBottom(left);
3503
            }
264 andreas 3504
            else if (so == Qt::InvertedLandscapeOrientation)
263 andreas 3505
            {
3506
                int right = margins.right();
3507
                int top = margins.top();
3508
                margins.setTop(margins.left());
3509
                margins.setLeft(top);
3510
                margins.setRight(margins.bottom());
3511
                margins.setBottom(right);
3512
            }
3513
        }
264 andreas 3514
        else if (gPageManager && gPageManager->getSettings()->isLandscape())
263 andreas 3515
        {
264 andreas 3516
            if (so == Qt::PortraitOrientation)
3517
            {
3518
                int top = margins.top();
3519
                int right = margins.right();
3520
                margins.setTop(margins.left());
3521
                margins.setLeft(top);
3522
                margins.setRight(margins.bottom());
3523
                margins.setBottom(right);
3524
            }
3525
            else if (so == Qt::InvertedPortraitOrientation)
3526
            {
3527
                int top = margins.top();
3528
                int left = margins.left();
3529
                margins.setTop(margins.right());
3530
                margins.setLeft(margins.bottom());
3531
                margins.setRight(top);
3532
                margins.setBottom(left);
3533
            }
263 andreas 3534
        }
252 andreas 3535
    }
263 andreas 3536
#ifdef QT_DEBUG
264 andreas 3537
    MSG_DEBUG("Notch top: " << margins.top() << ", bottom: " << margins.bottom() << ", left: " << margins.left() << ", right: " << margins.right() << ", Orientation real: " << orientationToString(so) << ", estimated: " << orientationToString(mOrientation));
263 andreas 3538
#endif
3539
    if (gPageManager)
252 andreas 3540
    {
264 andreas 3541
        // If the real orientation "so" differs from "mOrientation" then
3542
        // "mOrientation" contains the wanted orientation and not the real one!
263 andreas 3543
        if (gPageManager->getSettings()->isPortrait() &&
3544
            (mOrientation == Qt::PortraitOrientation || mOrientation == Qt::InvertedPortraitOrientation))
3545
        {
3546
            mNotchPortrait = margins;
3547
            mHaveNotchPortrait = true;
3548
        }
3549
        else if (gPageManager->getSettings()->isLandscape() &&
3550
            (mOrientation == Qt::LandscapeOrientation || mOrientation == Qt::InvertedLandscapeOrientation))
3551
        {
3552
            mNotchLandscape = margins;
3553
            mHaveNotchLandscape = true;
3554
        }
252 andreas 3555
    }
3556
}
3557
 
3558
/**
3559
 * @brief MainWindow::initGeoLocation
3560
 * This method is only used on IOS to let the application run in the background.
3561
 * It is necessary because it is the only way to let an application run in
3562
 * the background.
3563
 * The method initializes the geo position module of Qt. If the app doesn't
3564
 * have the permissions to retrieve the geo positions, it will not run in the
3565
 * background. It stops at the moment the app is not in front or the display is
3566
 * closed. This makes it stop communicate with the NetLinx and looses the
3567
 * network connection. When the app gets the focus again, it must reconnect to
3568
 * the NetLinx.
3569
 */
3570
void MainWindow::initGeoLocation()
3571
{
3572
    DECL_TRACER("MainWindow::initGeoLocation()");
3573
 
3574
    if (mSource)
3575
        return;
3576
 
3577
    mSource = QGeoPositionInfoSource::createDefaultSource(this);
3578
 
3579
    if (mSource)
3580
    {
3581
        connect(mSource, &QGeoPositionInfoSource::positionUpdated, this, &MainWindow::onPositionUpdated);
3582
        connect(mSource, &QGeoPositionInfoSource::errorOccurred, this, &MainWindow::onErrorOccurred);
3583
    }
3584
    else
3585
    {
3586
        MSG_WARNING("Error creating geo positioning source!");
3587
    }
3588
}
263 andreas 3589
 
264 andreas 3590
#ifdef Q_OS_IOS
3591
Qt::ScreenOrientation MainWindow::getRealOrientation()
3592
{
3593
    DECL_TRACER("MainWindow::getRealOrientation()");
3594
 
3595
    QScreen *screen = QGuiApplication::primaryScreen();
3596
 
3597
    if (!screen)
3598
    {
3599
        MSG_ERROR("Couldn't determine the primary screen!")
3600
        return Qt::PrimaryOrientation;
3601
    }
3602
 
3603
    QRect rect = screen->availableGeometry();
3604
 
3605
    if (rect.width() > rect.height())
3606
        return Qt::LandscapeOrientation;
3607
 
3608
    return Qt::PortraitOrientation;
3609
}
3610
#endif
270 andreas 3611
#endif
264 andreas 3612
 
270 andreas 3613
#if defined(QT_DEBUG) && (defined(Q_OS_IOS) || defined(Q_OS_ANDROID))
264 andreas 3614
string MainWindow::orientationToString(Qt::ScreenOrientation ori)
263 andreas 3615
{
3616
    string sori;
3617
 
3618
    switch(ori)
3619
    {
3620
        case Qt::PortraitOrientation:           sori = "PORTRAIT"; break;
3621
        case Qt::InvertedPortraitOrientation:   sori = "INVERTED PORTRAIT"; break;
3622
        case Qt::LandscapeOrientation:          sori = "LANDSCAPE"; break;
3623
        case Qt::InvertedLandscapeOrientation:  sori = "INVERTED LANDSCAPE"; break;
3624
        default:
3625
            sori = "Unknown: ";
3626
            sori.append(intToString(ori));
3627
    }
3628
 
3629
    return sori;
3630
}
252 andreas 3631
#endif
3632
 
14 andreas 3633
/******************* Draw elements *************************/
3634
 
217 andreas 3635
/**
3636
 * @brief Displays an image.
3637
 * The method is a callback function and is called whenever an image should be
3638
 * displayed. It defines a label, set it to the (scaled) \b width and \b height
3639
 * and moves it to the (scaled) position \b left and \b top.
3640
 *
3641
 * @param handle    The unique handle of the object
3642
 * @param parent    The unique handle of the parent object.
3643
 * @param buffer    A byte array containing the image.
3644
 * @param width     The width of the object
3645
 * @param height    The height of the object
3646
 * @param pixline   The number of pixels in one line of the image.
3647
 * @param left      The prosition from the left.
3648
 * @param top       The position from the top.
3649
 */
298 andreas 3650
void MainWindow::displayButton(ulong handle, ulong parent, TBitmap buffer, int width, int height, int left, int top, bool passthrough)
14 andreas 3651
{
298 andreas 3652
    DECL_TRACER("MainWindow::displayButton(ulong handle, TBitmap buffer, size_t size, int width, int height, int left, int top, bool passthrough)");
4 andreas 3653
 
343 andreas 3654
//    TLOCKER(draw_mutex);
107 andreas 3655
 
277 andreas 3656
    TObject::OBJECT_t *obj = findObject(handle);
3657
    TObject::OBJECT_t *par = findObject(parent);
271 andreas 3658
    MSG_TRACE("Processing button " << handleToString(handle) << " from parent " << handleToString(parent));
5 andreas 3659
 
6 andreas 3660
    if (!par)
3661
    {
59 andreas 3662
        if (TStreamError::checkFilter(HLOG_DEBUG))
271 andreas 3663
            MSG_WARNING("Button " << handleToString(handle) << " has no parent (" << handleToString(parent) << ")! Ignoring it.");
332 andreas 3664
#if TESTMODE == 1
334 andreas 3665
        setScreenDone();
332 andreas 3666
#endif
6 andreas 3667
        return;
3668
    }
3669
 
107 andreas 3670
    if (par->animation && !par->aniDirection)
3671
    {
3672
        if (par->animation->state() == QAbstractAnimation::Running)
3673
        {
271 andreas 3674
            MSG_WARNING("Object " << handleToString(parent) << " is busy with an animation!");
107 andreas 3675
            par->animation->stop();
3676
        }
3677
        else
3678
        {
271 andreas 3679
            MSG_WARNING("Object " << handleToString(parent) << " has not finished the animation!");
107 andreas 3680
        }
3681
 
332 andreas 3682
#if TESTMODE == 1
334 andreas 3683
        setScreenDone();
332 andreas 3684
#endif
107 andreas 3685
        return;
3686
    }
3687
    else if (par->remove)
3688
    {
271 andreas 3689
        MSG_WARNING("Object " << handleToString(parent) << " is marked for remove. Will not draw image!");
332 andreas 3690
#if TESTMODE == 1
334 andreas 3691
        setScreenDone();
332 andreas 3692
#endif
107 andreas 3693
        return;
3694
    }
3695
 
5 andreas 3696
    if (!obj)
3697
    {
351 andreas 3698
        if (!par->object.widget)
3699
        {
3700
            MSG_ERROR("Object " << handleToString(parent) << " has no valid widget!");
3701
#if TESTMODE == 1
3702
            setScreenDone();
3703
#endif
3704
            return;
3705
        }
3706
 
271 andreas 3707
        MSG_DEBUG("Adding new object " << handleToString(handle) << " ...");
296 andreas 3708
        OBJECT_t nobj;
5 andreas 3709
 
296 andreas 3710
        nobj.type = TObject::OBJ_BUTTON;
3711
        nobj.handle = handle;
5 andreas 3712
 
198 andreas 3713
        if (gPageManager->isSetupActive())
3714
        {
296 andreas 3715
            nobj.width = scaleSetup(width);
3716
            nobj.height = scaleSetup(height);
3717
            nobj.left = scaleSetup(left);
3718
            nobj.top = scaleSetup(top);
198 andreas 3719
        }
3720
        else
3721
        {
296 andreas 3722
            nobj.width = scale(width);
3723
            nobj.height = scale(height);
3724
            nobj.left = scale(left);
3725
            nobj.top = scale(top);
198 andreas 3726
        }
3727
 
351 andreas 3728
        nobj.object.label = new QLabel(par->object.widget);
296 andreas 3729
        nobj.object.label->setObjectName(QString("Label_") + handleToString(handle).c_str());
323 andreas 3730
 
3731
        if (mGestureFilter)
3732
        {
3733
            nobj.object.label->installEventFilter(mGestureFilter);
3734
            nobj.object.label->grabGesture(Qt::PinchGesture);
3735
            nobj.object.label->grabGesture(Qt::SwipeGesture);
3736
        }
3737
 
344 andreas 3738
        nobj.object.label->setGeometry(nobj.left, nobj.top, nobj.width, nobj.height);
3739
//        nobj.object.label->move(nobj.left, nobj.top);
3740
//        nobj.object.label->setFixedSize(nobj.width, nobj.height);
296 andreas 3741
 
298 andreas 3742
        if (passthrough)
3743
            nobj.object.label->setAttribute(Qt::WA_TransparentForMouseEvents);
3744
 
296 andreas 3745
        if (!addObject(nobj))
3746
        {
3747
            MSG_ERROR("Unable to add the new object " << handleToString(handle) << "!");
334 andreas 3748
#if TESTMODE == 1
3749
            setScreenDone();
3750
#endif
296 andreas 3751
            return;
3752
        }
3753
 
3754
        obj = findObject(handle);
6 andreas 3755
    }
14 andreas 3756
    else
297 andreas 3757
    {
271 andreas 3758
        MSG_DEBUG("Object " << handleToString(handle) << " of type " << TObject::objectToString(obj->type) << " found!");
5 andreas 3759
 
298 andreas 3760
        if (passthrough && obj->object.label)
3761
            obj->object.label->setAttribute(Qt::WA_TransparentForMouseEvents);
3762
 
297 andreas 3763
        if (!enableObject(handle))
3764
        {
3765
            MSG_ERROR("Object " << handleToString(handle) << " of type " << objectToString(obj->type) << " couldn't be enabled!");
332 andreas 3766
#if TESTMODE == 1
334 andreas 3767
            setScreenDone();
332 andreas 3768
#endif
297 andreas 3769
            return;
3770
        }
334 andreas 3771
 
3772
        // In case the dimensions or position has changed we calculate the
3773
        // position and size again.
3774
        int lt, tp, wt, ht;
3775
 
3776
        if (gPageManager->isSetupActive())
3777
        {
3778
            wt = scaleSetup(width);
3779
            ht = scaleSetup(height);
3780
            lt = scaleSetup(left);
3781
            tp = scaleSetup(top);
3782
        }
3783
        else
3784
        {
3785
            wt = scale(width);
3786
            ht = scale(height);
3787
            lt = scale(left);
3788
            tp = scale(top);
3789
        }
3790
 
3791
        if (obj->type != OBJ_INPUT && (wt != obj->width || ht != obj->height || lt != obj->left || tp != obj->top))
3792
        {
3793
            MSG_DEBUG("Scaled button with new size: lt: " << obj->left << ", tp: " << obj->top << ", wt: " << obj->width << ", ht: " << obj->height);
344 andreas 3794
/*
334 andreas 3795
            if (obj->left != lt || obj->top != tp)
3796
                obj->object.label->move(lt, tp);
3797
 
3798
            if (obj->width != wt || obj->height != ht)
3799
                obj->object.label->setFixedSize(wt, ht);
344 andreas 3800
*/
3801
//            if (obj->left != lt || obj->top != tp || obj->width != wt || obj->height != ht)
3802
                obj->object.label->setGeometry(lt, tp, wt, ht);
334 andreas 3803
 
3804
            obj->left = lt;
3805
            obj->top = tp;
3806
            obj->width = wt;
3807
            obj->height = ht;
3808
        }
297 andreas 3809
    }
3810
 
334 andreas 3811
    if (obj->type != OBJ_INPUT)
6 andreas 3812
    {
187 andreas 3813
        try
75 andreas 3814
        {
289 andreas 3815
            if (buffer.getSize() > 0 && buffer.getPixline() > 0)
107 andreas 3816
            {
271 andreas 3817
                MSG_DEBUG("Setting image for " << handleToString(handle) << " ...");
289 andreas 3818
                QPixmap pixmap = scaleImage((unsigned char *)buffer.getBitmap(), buffer.getWidth(), buffer.getHeight(), buffer.getPixline());
107 andreas 3819
 
187 andreas 3820
                if (obj->object.label)
332 andreas 3821
                {
187 andreas 3822
                    obj->object.label->setPixmap(pixmap);
344 andreas 3823
                    if (obj->handle == (((588 << 16) & 0xffff0000) | 6))
3824
                        pixmap.save("px588-6.png");
332 andreas 3825
#if TESTMODE == 1
3826
                    __success = true;
3827
#endif
3828
                }
187 andreas 3829
                else
3830
                {
271 andreas 3831
                    MSG_WARNING("Object " << handleToString(handle) << " does not exist any more!");
187 andreas 3832
                }
107 andreas 3833
            }
75 andreas 3834
        }
187 andreas 3835
        catch(std::exception& e)
3836
        {
271 andreas 3837
            MSG_ERROR("Error drawing button " << handleToString(handle) << ": " << e.what());
187 andreas 3838
        }
3839
        catch(...)
3840
        {
3841
            MSG_ERROR("Unexpected exception occured [MainWindow::displayButton()]");
3842
        }
5 andreas 3843
    }
334 andreas 3844
#if TESTMODE == 1
3845
    setScreenDone();
3846
#endif
4 andreas 3847
}
3848
 
289 andreas 3849
void MainWindow::displayViewButton(ulong handle, ulong parent, bool vertical, TBitmap buffer, int width, int height, int left, int top, int space, TColor::COLOR_T fillColor)
279 andreas 3850
{
289 andreas 3851
    DECL_TRACER("MainWindow::displayViewButton(ulong handle, TBitmap buffer, size_t size, int width, int height, int left, int top)");
279 andreas 3852
 
343 andreas 3853
//    TLOCKER(draw_mutex);
279 andreas 3854
 
3855
    TObject::OBJECT_t *obj = findObject(handle);
3856
    TObject::OBJECT_t *par = findObject(parent);
3857
    MSG_TRACE("Processing button " << handleToString(handle) << " from parent " << handleToString(parent));
3858
 
3859
    if (!par)
3860
    {
3861
        if (TStreamError::checkFilter(HLOG_DEBUG))
3862
            MSG_WARNING("Button " << handleToString(handle) << " has no parent (" << handleToString(parent) << ")! Ignoring it.");
3863
 
332 andreas 3864
#if TESTMODE == 1
334 andreas 3865
        setScreenDone();
332 andreas 3866
#endif
279 andreas 3867
        return;
3868
    }
3869
 
3870
    if (par->animation && !par->aniDirection)
3871
    {
3872
        if (par->animation->state() == QAbstractAnimation::Running)
3873
        {
3874
            MSG_WARNING("Object " << handleToString(parent) << " is busy with an animation!");
3875
            par->animation->stop();
3876
        }
3877
        else
3878
        {
3879
            MSG_WARNING("Object " << handleToString(parent) << " has not finished the animation!");
3880
        }
3881
 
332 andreas 3882
#if TESTMODE == 1
334 andreas 3883
        setScreenDone();
332 andreas 3884
#endif
279 andreas 3885
        return;
3886
    }
3887
    else if (par->remove)
3888
    {
3889
        MSG_WARNING("Object " << handleToString(parent) << " is marked for remove. Will not draw image!");
332 andreas 3890
#if TESTMODE == 1
334 andreas 3891
        setScreenDone();
332 andreas 3892
#endif
279 andreas 3893
        return;
3894
    }
3895
 
3896
    if (!obj)
3897
    {
351 andreas 3898
        if (!par->object.widget)
3899
        {
3900
#if TESTMODE == 1
3901
            MSG_ERROR("Object " << handleToString(parent) << " has no valid object!");
3902
#endif
3903
            return;
3904
        }
3905
 
279 andreas 3906
        MSG_DEBUG("Adding new object " << handleToString(handle) << " ...");
296 andreas 3907
        OBJECT_t nobj;
279 andreas 3908
 
296 andreas 3909
        nobj.type = OBJ_SUBVIEW;
3910
        nobj.handle = handle;
3911
 
3912
        nobj.width = scale(width);
3913
        nobj.height = scale(height);
3914
        nobj.left = scale(left);
3915
        nobj.top = scale(top);
3916
 
3917
        nobj.object.area = new TQScrollArea(par->object.widget, nobj.width, nobj.height, vertical);
3918
        nobj.object.area->setObjectName(QString("View_") + handleToString(handle).c_str());
3919
        nobj.object.area->setScaleFactor(mScaleFactor);
3920
        nobj.object.area->setSpace(space);
3921
        nobj.object.area->move(nobj.left, nobj.top);
321 andreas 3922
        nobj.connected = true;
296 andreas 3923
        connect(nobj.object.area, &TQScrollArea::objectClicked, this, &MainWindow::onSubViewItemClicked);
3924
 
3925
        if (!addObject(nobj))
279 andreas 3926
        {
296 andreas 3927
            MSG_ERROR("Couldn't add the object " << handleToString(handle) << "!");
332 andreas 3928
#if TESTMODE == 1
334 andreas 3929
            setScreenDone();
332 andreas 3930
#endif
279 andreas 3931
            return;
3932
        }
3933
 
296 andreas 3934
        obj = findObject(handle);
279 andreas 3935
    }
3936
    else if (obj->type != OBJ_SUBVIEW)
3937
    {
3938
        MSG_ERROR("Object " << handleToString(handle) << " is of wrong type " << objectToString(obj->type) << "!");
332 andreas 3939
#if TESTMODE == 1
334 andreas 3940
        setScreenDone();
332 andreas 3941
#endif
279 andreas 3942
        return;
3943
    }
3944
    else
297 andreas 3945
    {
279 andreas 3946
        MSG_DEBUG("Object " << handleToString(handle) << " of type " << TObject::objectToString(obj->type) << " found!");
3947
 
321 andreas 3948
        if (obj->object.area && !obj->connected)
297 andreas 3949
            reconnectArea(obj->object.area);
3950
    }
3951
 
279 andreas 3952
    try
3953
    {
289 andreas 3954
        // Set background color
286 andreas 3955
        if (obj->object.area)
289 andreas 3956
        {
297 andreas 3957
            QColor color;
3958
 
3959
            if (fillColor.alpha == 0)
3960
                color = Qt::transparent;
3961
            else
3962
                color = qRgba(fillColor.red, fillColor.green, fillColor.blue, fillColor.alpha);
3963
 
289 andreas 3964
            obj->object.area->setBackGroundColor(color);
3965
        }
286 andreas 3966
 
289 andreas 3967
        if (buffer.getSize() > 0 && buffer.getPixline() > 0)
279 andreas 3968
        {
3969
            MSG_DEBUG("Setting image for " << handleToString(handle) << " ...");
289 andreas 3970
            QPixmap pixmap = scaleImage((unsigned char *)buffer.getBitmap(), buffer.getWidth(), buffer.getHeight(), buffer.getPixline());
279 andreas 3971
 
285 andreas 3972
            if (pixmap.isNull())
279 andreas 3973
            {
3974
                MSG_ERROR("Unable to create a pixmap out of an image!");
332 andreas 3975
#if TESTMODE == 1
334 andreas 3976
                setScreenDone();
332 andreas 3977
#endif
279 andreas 3978
                return;
3979
            }
3980
 
285 andreas 3981
            if (obj->object.area)
279 andreas 3982
            {
285 andreas 3983
                obj->object.area->setBackgroundImage(pixmap);
279 andreas 3984
            }
3985
            else
3986
            {
3987
                MSG_WARNING("Object " << handleToString(handle) << " does not exist any more!");
3988
            }
3989
        }
3990
    }
3991
    catch(std::exception& e)
3992
    {
3993
        MSG_ERROR("Error drawing button " << handleToString(handle) << ": " << e.what());
3994
    }
3995
    catch(...)
3996
    {
3997
        MSG_ERROR("Unexpected exception occured [MainWindow::displayViewButton()]");
3998
    }
3999
}
4000
 
285 andreas 4001
void MainWindow::addViewButtonItems(ulong parent, vector<PGSUBVIEWITEM_T> items)
279 andreas 4002
{
285 andreas 4003
    DECL_TRACER("MainWindow::addViewButtonItems(ulong parent, vector<PGSUBVIEWITEM_T> items)");
279 andreas 4004
 
285 andreas 4005
    if (items.empty())
4006
        return;
279 andreas 4007
 
285 andreas 4008
    OBJECT_t *par = findObject(parent);
279 andreas 4009
 
285 andreas 4010
    if (!par || par->type != OBJ_SUBVIEW || !par->object.area)
279 andreas 4011
    {
285 andreas 4012
        MSG_ERROR("No object with handle " << handleToString(parent) << " found or object is not a subview list!");
279 andreas 4013
        return;
4014
    }
4015
 
297 andreas 4016
    if (par->invalid)
4017
    {
4018
        if (!enableObject(parent))
4019
        {
4020
            MSG_ERROR("Object " << handleToString(parent) << " of type " << objectToString(par->type) << " couldn't be enabled!");
332 andreas 4021
#if TESTMODE == 1
334 andreas 4022
            setScreenDone();
332 andreas 4023
#endif
297 andreas 4024
            return;
4025
        }
4026
    }
4027
 
300 andreas 4028
    if (!items.empty())
4029
    {
4030
        par->object.area->setScrollbar(items[0].scrollbar);
4031
        par->object.area->setScrollbarOffset(items[0].scrollbarOffset);
4032
        par->object.area->setAnchor(items[0].position);
4033
    }
4034
 
285 andreas 4035
    par->object.area->addItems(items);
280 andreas 4036
}
4037
 
300 andreas 4038
void MainWindow::updateViewButton(ulong handle, ulong parent, TBitmap buffer, TColor::COLOR_T fillColor)
4039
{
4040
    DECL_TRACER("MainWindow::updateViewButton(ulong handle, ulong parent, TBitmap buffer, TColor::COLOR_T fillColor)");
4041
 
4042
    OBJECT_t *par = findObject(parent);
4043
 
4044
    if (!par || par->type != OBJ_SUBVIEW || !par->object.area)
4045
    {
4046
        MSG_ERROR("No object with handle " << handleToString(parent) << " found for update or object is not a subview list!");
332 andreas 4047
#if TESTMODE == 1
334 andreas 4048
        setScreenDone();
332 andreas 4049
#endif
300 andreas 4050
        return;
4051
    }
4052
 
4053
    PGSUBVIEWITEM_T item;
4054
    item.handle = handle;
4055
    item.parent = parent;
4056
    item.image = buffer;
4057
    item.bgcolor = fillColor;
4058
    par->object.area->updateItem(item);
4059
}
4060
 
4061
void MainWindow::updateViewButtonItem(PGSUBVIEWITEM_T& item, ulong parent)
4062
{
4063
    DECL_TRACER("MainWindow::updateViewButtonItem(PGSUBVIEWITEM_T& item, ulong parent)");
4064
 
4065
    OBJECT_t *par = findObject(parent);
4066
 
4067
    if (!par || par->type != OBJ_SUBVIEW || !par->object.area)
4068
    {
4069
        MSG_ERROR("No object with handle " << handleToString(parent) << " found for update or object is not a subview list!");
332 andreas 4070
#if TESTMODE == 1
334 andreas 4071
        setScreenDone();
332 andreas 4072
#endif
300 andreas 4073
        return;
4074
    }
4075
 
4076
    par->object.area->updateItem(item);
4077
}
4078
 
4079
void MainWindow::showViewButtonItem(ulong handle, ulong parent, int position, int timer)
4080
{
4081
    DECL_TRACER("MainWindow::showViewButtonItem(ulong handle, ulong parent, int position, int timer)");
4082
 
4083
    Q_UNUSED(timer);
4084
 
4085
    OBJECT_t *par = findObject(parent);
4086
 
4087
    if (!par || par->type != OBJ_SUBVIEW || !par->object.area)
4088
    {
4089
        MSG_ERROR("No object with handle " << handleToString(parent) << " found for update or object is not a subview list!");
332 andreas 4090
#if TESTMODE == 1
334 andreas 4091
        setScreenDone();
332 andreas 4092
#endif
300 andreas 4093
        return;
4094
    }
4095
 
4096
    par->object.area->showItem(handle, position);
4097
}
4098
 
318 andreas 4099
void MainWindow::toggleViewButtonItem(ulong handle, ulong parent, int position, int timer)
4100
{
4101
    DECL_TRACER("MainWindow::toggleViewButtonItem(ulong handle, ulong parent, int position, int timer)");
4102
 
4103
    Q_UNUSED(timer);
4104
 
4105
    OBJECT_t *par = findObject(parent);
4106
 
4107
    if (!par || par->type != OBJ_SUBVIEW || !par->object.area)
4108
    {
4109
        MSG_ERROR("No object with handle " << handleToString(parent) << " found for update or object is not a subview list!");
332 andreas 4110
#if TESTMODE == 1
334 andreas 4111
        setScreenDone();
332 andreas 4112
#endif
318 andreas 4113
        return;
4114
    }
4115
 
4116
    par->object.area->showItem(handle, position);
4117
}
4118
 
4119
void MainWindow::hideAllViewItems(ulong handle)
4120
{
4121
    DECL_TRACER("MainWindow::hideAllViewItems(ulong handle)");
4122
 
4123
    OBJECT_t *obj = findObject(handle);
4124
 
4125
    if (!obj || obj->type != OBJ_SUBVIEW || !obj->object.area)
332 andreas 4126
    {
4127
#if TESTMODE == 1
334 andreas 4128
        setScreenDone();
332 andreas 4129
#endif
318 andreas 4130
        return;
332 andreas 4131
    }
318 andreas 4132
 
4133
    obj->object.area->hideAllItems();
4134
}
4135
 
4136
void MainWindow::hideViewItem(ulong handle, ulong parent)
4137
{
4138
    DECL_TRACER("MainWindow::hideViewItem(ulong handle, ulong parent)");
4139
 
4140
    OBJECT_t *obj = findObject(parent);
4141
 
4142
    if (!obj || obj->type != OBJ_SUBVIEW || !obj->object.area)
332 andreas 4143
    {
4144
#if TESTMODE == 1
334 andreas 4145
        setScreenDone();
332 andreas 4146
#endif
318 andreas 4147
        return;
332 andreas 4148
    }
318 andreas 4149
 
4150
    obj->object.area->hideItem(handle);
4151
}
4152
 
98 andreas 4153
void MainWindow::SetVisible(ulong handle, bool state)
4154
{
4155
    DECL_TRACER("MainWindow::SetVisible(ulong handle, bool state)");
4156
 
318 andreas 4157
    OBJECT_t *obj = findObject(handle);
98 andreas 4158
 
4159
    if (!obj)
4160
    {
271 andreas 4161
        MSG_ERROR("Object " << handleToString(handle) << " not found!");
332 andreas 4162
#if TESTMODE == 1
334 andreas 4163
        setScreenDone();
332 andreas 4164
#endif
98 andreas 4165
        return;
4166
    }
4167
 
318 andreas 4168
    if (obj->type == OBJ_BUTTON && obj->object.label)
99 andreas 4169
    {
98 andreas 4170
        obj->object.label->setVisible(state);
297 andreas 4171
        obj->invalid = false;
4172
        obj->remove = false;
99 andreas 4173
    }
318 andreas 4174
    else if (obj->type == OBJ_SUBVIEW && obj->object.area)
4175
    {
4176
        obj->object.area->setVisible(state);
4177
        obj->invalid = false;
4178
        obj->remove = false;
4179
    }
99 andreas 4180
    else
4181
    {
271 andreas 4182
        MSG_DEBUG("Ignoring non button object " << handleToString(handle));
99 andreas 4183
    }
98 andreas 4184
}
4185
 
318 andreas 4186
void MainWindow::setSubViewPadding(ulong handle, int padding)
4187
{
4188
    DECL_TRACER("MainWindow::setSubViewPadding(ulong handle, int padding)");
4189
 
4190
    OBJECT_t *obj = findObject(handle);
4191
 
4192
    if (!obj)
4193
    {
4194
        MSG_ERROR("Object " << handleToString(handle) << " not found!");
332 andreas 4195
#if TESTMODE == 1
334 andreas 4196
        setScreenDone();
332 andreas 4197
#endif
318 andreas 4198
        return;
4199
    }
4200
 
4201
    if (obj->type != OBJ_SUBVIEW || !obj->object.area)
332 andreas 4202
    {
4203
#if TESTMODE == 1
334 andreas 4204
        setScreenDone();
332 andreas 4205
#endif
318 andreas 4206
        return;
332 andreas 4207
    }
318 andreas 4208
 
4209
    obj->object.area->setSpace(padding);
4210
}
4211
 
215 andreas 4212
/**
217 andreas 4213
 * @brief Prepares a new object.
215 andreas 4214
 * The method first checks whether there exists a background widget or not. If
4215
 * not it creates a background widget with a black background. On Android this
4216
 * image is the size of the whole screen while on a desktop it is only the size
4217
 * of a page plus the task bar, if any.
217 andreas 4218
 * If the background image (centralWidget()) already exists, it set's only the
4219
 * credentials of the object.
4220
 * It makes sure that all child objects of the central widget are destroyed.
215 andreas 4221
 *
4222
 * @param handle    The handle of the new widget
4223
 * @param width     The original width of the page
4224
 * @param heoght    The original height of the page
4225
 */
5 andreas 4226
void MainWindow::setPage(ulong handle, int width, int height)
4227
{
4228
    DECL_TRACER("MainWindow::setPage(ulong handle, int width, int height)");
4 andreas 4229
 
277 andreas 4230
    if (handle == mActualPageHandle)
332 andreas 4231
    {
4232
#if TESTMODE == 1
334 andreas 4233
        setScreenDone();
332 andreas 4234
#endif
277 andreas 4235
        return;
332 andreas 4236
    }
277 andreas 4237
 
292 andreas 4238
    TLOCKER(draw_mutex);
277 andreas 4239
    QWidget *wBackground = centralWidget();
4240
 
4241
    if (!wBackground)
107 andreas 4242
    {
277 andreas 4243
        MSG_ERROR("No central widget!");
332 andreas 4244
#if TESTMODE == 1
334 andreas 4245
        setScreenDone();
332 andreas 4246
#endif
107 andreas 4247
        return;
4248
    }
4249
 
277 andreas 4250
    if (!mCentralWidget)
4251
    {
4252
        MSG_ERROR("Stack for pages not initialized!");
332 andreas 4253
#if TESTMODE == 1
334 andreas 4254
        setScreenDone();
332 andreas 4255
#endif
215 andreas 4256
        return;
277 andreas 4257
    }
215 andreas 4258
 
4259
    // The following should be true only for the first time this method is called.
277 andreas 4260
    if (!mCentralInitialized)
215 andreas 4261
    {
4262
        QSize qs = menuBar()->sizeHint();
198 andreas 4263
 
215 andreas 4264
        if (gPageManager && gPageManager->isSetupActive())
4265
            setMinimumSize(scaleSetup(width), scaleSetup(height) + qs.height());
4266
        else
4267
            setMinimumSize(scale(width), scale(height) + qs.height());
265 andreas 4268
 
4269
        mCentralInitialized = true;
215 andreas 4270
    }
198 andreas 4271
 
296 andreas 4272
    OBJECT_t *obj = findObject(handle);
5 andreas 4273
 
4274
    if (!obj)
4275
    {
271 andreas 4276
        MSG_DEBUG("Adding new object " << handleToString(handle));
296 andreas 4277
        OBJECT_t nobj;
5 andreas 4278
 
296 andreas 4279
        nobj.handle = handle;
4280
        nobj.type = OBJ_PAGE;
4281
        nobj.object.widget = nullptr;
5 andreas 4282
 
217 andreas 4283
        if (gPageManager && gPageManager->isSetupActive())
4284
        {
296 andreas 4285
            nobj.height = scaleSetup(height);
4286
            nobj.width = scaleSetup(width);
217 andreas 4287
        }
4288
        else
4289
        {
296 andreas 4290
            nobj.height = scale(height);
4291
            nobj.width = scale(width);
217 andreas 4292
        }
296 andreas 4293
 
4294
        if (!addObject(nobj))
4295
        {
4296
            MSG_ERROR("Error crating an object for handle " << handleToString(handle));
332 andreas 4297
#if TESTMODE == 1
334 andreas 4298
            setScreenDone();
332 andreas 4299
#endif
296 andreas 4300
            return;
4301
        }
4302
 
4303
        obj = findObject(handle);
5 andreas 4304
    }
296 andreas 4305
    else
271 andreas 4306
    {
296 andreas 4307
        if (obj->type != OBJ_PAGE)
4308
        {
4309
            MSG_WARNING("Object " << handleToString(handle) << " is not a page! Will not reuse it as a page.");
332 andreas 4310
#if TESTMODE == 1
334 andreas 4311
            setScreenDone();
332 andreas 4312
#endif
296 andreas 4313
            return;
4314
        }
4315
 
4316
        if (obj->object.widget && obj->object.widget->isHidden() && mCentralWidget->indexOf(obj->object.widget) >= 0)
271 andreas 4317
            obj->object.widget->setParent(wBackground);
220 andreas 4318
 
277 andreas 4319
        obj->invalid = false;
296 andreas 4320
        obj->remove = false;
277 andreas 4321
        MSG_DEBUG("Hidden object " << handleToString(handle) << " was reactivated.");
217 andreas 4322
    }
227 andreas 4323
 
277 andreas 4324
    if (!obj->object.widget)
264 andreas 4325
    {
277 andreas 4326
        obj->object.widget = new QWidget;
271 andreas 4327
        obj->object.widget->setObjectName(QString("Page_") + handleToString(handle).c_str());
323 andreas 4328
 
4329
        if (mGestureFilter)
4330
        {
4331
            obj->object.widget->installEventFilter(mGestureFilter);
4332
            obj->object.widget->grabGesture(Qt::PinchGesture);
4333
            obj->object.widget->grabGesture(Qt::SwipeGesture);
4334
        }
4335
 
277 andreas 4336
        obj->object.widget->setAutoFillBackground(true);
271 andreas 4337
        obj->invalid = false;
275 andreas 4338
        obj->object.widget->move(0, 0);
266 andreas 4339
#if defined(Q_OS_IOS) || defined(Q_OS_ANDROID)
275 andreas 4340
        obj->object.widget->setFixedSize(obj->width, obj->height);
266 andreas 4341
#else
4342
        obj->object.widget->setGeometry(geometry());
4343
#endif
277 andreas 4344
        mCentralWidget->addWidget(obj->object.widget);
264 andreas 4345
    }
4346
 
213 andreas 4347
    mActualPageHandle = handle;
271 andreas 4348
    MSG_PROTOCOL("Current page: " << handleToString(handle));
5 andreas 4349
}
4350
 
217 andreas 4351
void MainWindow::setSubPage(ulong handle, ulong parent, int left, int top, int width, int height, ANIMATION_t animate)
5 andreas 4352
{
4353
    DECL_TRACER("MainWindow::setSubPage(ulong handle, int left, int top, int width, int height)");
38 andreas 4354
 
343 andreas 4355
//    TLOCKER(draw_mutex);
296 andreas 4356
    OBJECT_t *par = findObject(parent);
107 andreas 4357
 
296 andreas 4358
    if (!par || par->type != OBJ_PAGE)
38 andreas 4359
    {
264 andreas 4360
        if (!par)
4361
        {
271 andreas 4362
            MSG_ERROR("Subpage " << handleToString(handle) << " has no parent! Ignoring it.");
264 andreas 4363
        }
4364
        else
4365
        {
271 andreas 4366
            MSG_ERROR("Subpage " << handleToString(handle) << " has invalid parent " << handleToString(parent) << " which is no page! Ignoring it.");
264 andreas 4367
        }
332 andreas 4368
#if TESTMODE == 1
334 andreas 4369
        setScreenDone();
332 andreas 4370
#endif
217 andreas 4371
        return;
38 andreas 4372
    }
4373
 
278 andreas 4374
    if (!par->object.widget)
4375
    {
297 andreas 4376
        MSG_ERROR("Parent page " << handleToString(parent) << " has no widget defined!");
332 andreas 4377
#if TESTMODE == 1
334 andreas 4378
        setScreenDone();
332 andreas 4379
#endif
278 andreas 4380
        return;
4381
    }
4382
 
4383
    if (mCentralWidget && mCentralWidget->currentWidget() != par->object.widget)
4384
    {
4385
        MSG_WARNING("The parent page " << handleToString(parent) << " is not the current page " << handleToString(handle) << "!");
332 andreas 4386
#if TESTMODE == 1
334 andreas 4387
        setScreenDone();
332 andreas 4388
#endif
278 andreas 4389
        return;
4390
    }
4391
 
296 andreas 4392
    OBJECT_t *obj = findObject(handle);
4393
    OBJECT_t nobj;
4394
    bool shouldAdd = false;
272 andreas 4395
 
296 andreas 4396
    if (obj && obj->type != OBJ_SUBPAGE)
271 andreas 4397
    {
4398
        MSG_WARNING("Object " << handleToString(handle) << " exists but is not a subpage! Refusing to create a new page with this handle.");
332 andreas 4399
#if TESTMODE == 1
334 andreas 4400
        setScreenDone();
332 andreas 4401
#endif
271 andreas 4402
        return;
4403
    }
4404
    else if (!obj)
5 andreas 4405
    {
296 andreas 4406
        obj = &nobj;
4407
        shouldAdd = true;
5 andreas 4408
    }
297 andreas 4409
    else
4410
    {
4411
        obj->invalid = false;
4412
        obj->remove = false;
4413
    }
5 andreas 4414
 
296 andreas 4415
    int scLeft = 0;
4416
    int scTop = 0;
4417
    int scWidth = 0;
4418
    int scHeight = 0;
38 andreas 4419
 
198 andreas 4420
    if (gPageManager && gPageManager->isSetupActive())
4421
    {
4422
        scLeft = scaleSetup(left);
4423
        scTop = scaleSetup(top);
4424
        scWidth = scaleSetup(width);
4425
        scHeight = scaleSetup(height);
4426
    }
4427
    else
4428
    {
4429
        scLeft = scale(left);
4430
        scTop = scale(top);
4431
        scWidth = scale(width);
4432
        scHeight = scale(height);
4433
    }
4434
 
296 andreas 4435
    obj->type = OBJ_SUBPAGE;
5 andreas 4436
    obj->handle = handle;
271 andreas 4437
 
4438
    if (!obj->object.widget)
278 andreas 4439
    {
4440
        obj->object.widget = new QWidget(par->object.widget);
4441
        obj->object.widget->setObjectName(QString("Subpage_%1").arg(handleToString(handle).c_str()));
4442
    }
271 andreas 4443
    else
278 andreas 4444
        obj->object.widget->setParent(par->object.widget);
271 andreas 4445
 
6 andreas 4446
    obj->object.widget->setAutoFillBackground(true);
275 andreas 4447
    obj->object.widget->move(scLeft, scTop);
38 andreas 4448
    obj->object.widget->setFixedSize(scWidth, scHeight);
4449
    obj->left = scLeft;
4450
    obj->top = scTop;
4451
    obj->width = scWidth;
4452
    obj->height = scHeight;
271 andreas 4453
    obj->invalid = false;
296 andreas 4454
    obj->remove = false;
15 andreas 4455
    // filter move event
323 andreas 4456
    if (mGestureFilter)
4457
    {
4458
        obj->object.widget->installEventFilter(mGestureFilter);
4459
        obj->object.widget->grabGesture(Qt::PinchGesture);
4460
        obj->object.widget->grabGesture(Qt::SwipeGesture);
4461
    }
4462
 
107 andreas 4463
    obj->aniDirection = true;
277 andreas 4464
    obj->animate = animate;
296 andreas 4465
 
4466
    if (shouldAdd)
4467
    {
4468
        if (!addObject(nobj))
4469
        {
4470
            MSG_ERROR("Couldn't add the object " << handleToString(handle) << "!");
4471
            nobj.object.widget->close();
332 andreas 4472
#if TESTMODE == 1
334 andreas 4473
            setScreenDone();
332 andreas 4474
#endif
296 andreas 4475
        }
4476
    }
5 andreas 4477
}
4478
 
265 andreas 4479
#ifdef _OPAQUE_SKIA_
289 andreas 4480
void MainWindow::setBackground(ulong handle, TBitmap image, int width, int height, ulong color)
262 andreas 4481
#else
289 andreas 4482
void MainWindow::setBackground(ulong handle, TBitmap image, int width, int height, ulong color, int opacity)
262 andreas 4483
#endif
5 andreas 4484
{
292 andreas 4485
    TLOCKER(draw_mutex);
289 andreas 4486
    DECL_TRACER("MainWindow::setBackground(ulong handle, TBitmap image, ulong color [, int opacity])");
5 andreas 4487
 
277 andreas 4488
    if (!mCentralWidget)
107 andreas 4489
    {
277 andreas 4490
        MSG_ERROR("The internal page stack is not initialized!");
332 andreas 4491
#if TESTMODE == 1
334 andreas 4492
        setScreenDone();
332 andreas 4493
#endif
107 andreas 4494
        return;
4495
    }
5 andreas 4496
 
296 andreas 4497
    OBJECT_t *obj = findObject(handle);
107 andreas 4498
 
296 andreas 4499
    if (!obj || obj->remove)
5 andreas 4500
    {
271 andreas 4501
#ifdef QT_DEBUG
349 andreas 4502
        if (obj)
4503
        {
4504
            MSG_WARNING("No object " << handleToString(handle) << " found! (Flag remove: " << (obj->remove ? "TRUE" : "FALSE") << ")");
4505
        }
4506
        else
4507
        {
4508
            MSG_WARNING("No object " << handleToString(handle) << " found!");
4509
        }
271 andreas 4510
#else
4511
        MSG_WARNING("No object " << handleToString(handle) << " found!");
4512
#endif
332 andreas 4513
#if TESTMODE == 1
334 andreas 4514
        setScreenDone();
332 andreas 4515
#endif
5 andreas 4516
        return;
4517
    }
296 andreas 4518
    else if (obj->invalid)
297 andreas 4519
    {
4520
        if (!enableObject(handle))
4521
        {
4522
            MSG_ERROR("Object " << handleToString(handle) << " of type " << objectToString(obj->type) << " couldn't be anabled!");
332 andreas 4523
#if TESTMODE == 1
334 andreas 4524
            setScreenDone();
332 andreas 4525
#endif
297 andreas 4526
            return;
4527
        }
4528
    }
5 andreas 4529
 
297 andreas 4530
    if (obj->type != OBJ_SUBPAGE && obj->type != OBJ_BUTTON && obj->type != OBJ_PAGE)
4531
    {
4532
        MSG_ERROR("Method does not support object type " << objectToString(obj->type) << " for object " << handleToString(handle) << "!");
332 andreas 4533
#if TESTMODE == 1
334 andreas 4534
        setScreenDone();
332 andreas 4535
#endif
297 andreas 4536
        return;
4537
    }
4538
 
277 andreas 4539
    MSG_DEBUG("Object " << handleToString(handle) << " of type " << objectToString(obj->type) << " found!");
5 andreas 4540
 
296 andreas 4541
    if (obj->type == OBJ_BUTTON || obj->type == OBJ_SUBPAGE)
5 andreas 4542
    {
277 andreas 4543
        MSG_DEBUG("Processing object " << objectToString(obj->type));
198 andreas 4544
 
296 andreas 4545
        if (obj->type == OBJ_BUTTON && !obj->object.label)
198 andreas 4546
        {
271 andreas 4547
            MSG_ERROR("The label of the object " << handleToString(handle) << " was not initialized!");
332 andreas 4548
#if TESTMODE == 1
334 andreas 4549
            setScreenDone();
332 andreas 4550
#endif
198 andreas 4551
            return;
4552
        }
296 andreas 4553
        else if (obj->type == OBJ_SUBPAGE && !obj->object.widget)
198 andreas 4554
        {
271 andreas 4555
            MSG_ERROR("The widget of the object " << handleToString(handle) << " was not initialized!");
332 andreas 4556
#if TESTMODE == 1
334 andreas 4557
            setScreenDone();
332 andreas 4558
#endif
198 andreas 4559
            return;
4560
        }
4561
 
6 andreas 4562
        QPixmap pix(obj->width, obj->height);
5 andreas 4563
 
291 andreas 4564
        if (TColor::getAlpha(color) == 0)
4565
            pix.fill(Qt::transparent);
4566
        else
4567
            pix.fill(QColor::fromRgba(qRgba(TColor::getRed(color),TColor::getGreen(color),TColor::getBlue(color),TColor::getAlpha(color))));
4568
 
289 andreas 4569
        if (image.isValid() > 0)
5 andreas 4570
        {
289 andreas 4571
            MSG_DEBUG("Setting image of size " << image.getSize() << " (" << image.getWidth() << " x " << image.getHeight() << ")");
4572
            QImage img(image.getBitmap(), image.getWidth(), image.getHeight(), image.getPixline(), QImage::Format_ARGB32);
38 andreas 4573
 
198 andreas 4574
            if (isScaled() || (gPageManager && gPageManager->isSetupActive()))
38 andreas 4575
            {
4576
                QSize size(obj->width, obj->height);
4577
                pix.convertFromImage(img.scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
4578
            }
4579
            else
4580
                pix.convertFromImage(img);
6 andreas 4581
        }
4582
 
107 andreas 4583
        if (obj->type == TObject::OBJ_BUTTON)
5 andreas 4584
            obj->object.label->setPixmap(pix);
6 andreas 4585
        else
5 andreas 4586
        {
271 andreas 4587
            MSG_DEBUG("Setting image as background for subpage " << handleToString(handle));
296 andreas 4588
            QPalette palette(obj->object.widget->palette());
262 andreas 4589
#ifndef _OPAQUE_SKIA_
4590
            qreal oo;
4591
 
4592
            if (opacity < 0)
4593
                oo = 0.0;
4594
            else if (opacity > 255)
4595
                oo = 1.0;
4596
            else
4597
                oo = 1.0 / 255.0 * (qreal)opacity;
4598
 
4599
            if (oo < 1.0)
4600
            {
272 andreas 4601
                QPixmap image(pix.size());      //Image with given size and format.
4602
                image.fill(Qt::transparent);    //fills with transparent
262 andreas 4603
                QPainter p(&image);
272 andreas 4604
                p.setOpacity(oo);               // set opacity from 0.0 to 1.0, where 0.0 is fully transparent and 1.0 is fully opaque.
4605
                p.drawPixmap(0, 0, pix);        // given pixmap into the paint device.
278 andreas 4606
                p.end();
277 andreas 4607
                palette.setBrush(QPalette::Window, QBrush(image));
262 andreas 4608
                MSG_DEBUG("Opacity was set to " << oo);
4609
            }
277 andreas 4610
            else
4611
                palette.setBrush(QPalette::Window, QBrush(pix));
4612
#else
4613
            palette.setBrush(QPalette::Window, QBrush(pix));
262 andreas 4614
#endif
198 andreas 4615
            obj->object.widget->setPalette(palette);
5 andreas 4616
        }
4617
    }
107 andreas 4618
    else if (obj->type == TObject::OBJ_PAGE)
5 andreas 4619
    {
297 andreas 4620
        MSG_DEBUG("Processing object of type PAGE ...");
277 andreas 4621
        QWidget *central = obj->object.widget;
222 andreas 4622
 
265 andreas 4623
        if (!central)
5 andreas 4624
        {
271 andreas 4625
            MSG_ERROR("There is no page widget initialized for page " << handleToString(handle));
332 andreas 4626
#if TESTMODE == 1
334 andreas 4627
            setScreenDone();
332 andreas 4628
#endif
215 andreas 4629
            displayMessage("Can't set a background without an active page!", "Internal error");
4630
            return;
5 andreas 4631
        }
264 andreas 4632
 
277 andreas 4633
        QWidget *current = mCentralWidget->currentWidget();
272 andreas 4634
        int index = -1;
4635
 
4636
        if (current && central != current)
264 andreas 4637
        {
272 andreas 4638
            if ((index = mCentralWidget->indexOf(central)) < 0)
4639
            {
277 andreas 4640
                QString obName = QString("Page_%1").arg(handleToString(handle).c_str());
4641
 
4642
                for (int i = 0; i < mCentralWidget->count(); ++i)
4643
                {
4644
                    QWidget *w = mCentralWidget->widget(i);
4645
                    MSG_DEBUG("Checking widget " << w->objectName().toStdString());
4646
 
4647
                    if (w->objectName() == obName)
4648
                    {
4649
                        index = i;
4650
                        break;
4651
                    }
4652
                }
4653
 
4654
                if (index < 0)
4655
                {
291 andreas 4656
                    MSG_WARNING("Missing page " << handleToString(handle) << " on stack! Will add it to the stack.");
277 andreas 4657
                    index = mCentralWidget->addWidget(central);
4658
                    MSG_DEBUG("Number pages on stack: " << mCentralWidget->count());
4659
                    QRect geomMain = geometry();
4660
                    QRect geomCent = mCentralWidget->geometry();
4661
                    MSG_DEBUG("Geometry MainWindow: left: " << geomMain.left() << ", right: " << geomMain.right() << ", top: " << geomMain.top() << ", bottom: " << geomMain.bottom());
4662
                    MSG_DEBUG("Geometry CentWindow: left: " << geomCent.left() << ", right: " << geomCent.right() << ", top: " << geomCent.top() << ", bottom: " << geomCent.bottom());
4663
                }
272 andreas 4664
            }
264 andreas 4665
        }
277 andreas 4666
        else
272 andreas 4667
            index = mCentralWidget->indexOf(central);
4668
 
6 andreas 4669
        QPixmap pix(obj->width, obj->height);
291 andreas 4670
        QColor backgroundColor;
4671
 
4672
        if (TColor::getAlpha(color) == 0)
4673
            backgroundColor = Qt::transparent;
4674
        else
4675
            backgroundColor = QColor::fromRgba(qRgba(TColor::getRed(color),TColor::getGreen(color),TColor::getBlue(color),TColor::getAlpha(color)));
4676
 
221 andreas 4677
        pix.fill(backgroundColor);
265 andreas 4678
        MSG_DEBUG("Filled background of size " << pix.width() << "x" << pix.height() << " with color #" << std::setfill('0') << std::setw(8) << std::hex << color);
6 andreas 4679
 
289 andreas 4680
        if (width > 0 && image.isValid())
5 andreas 4681
        {
289 andreas 4682
            QImage img(image.getBitmap(), image.getWidth(), image.getHeight(), image.getPixline(), QImage::Format_ARGB32);
221 andreas 4683
            bool valid = false;
38 andreas 4684
 
222 andreas 4685
            if (!img.isNull())
4686
            {
4687
                if (isScaled())
4688
                {
296 andreas 4689
                    QImage bgImage = img.scaled(obj->width, obj->height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
223 andreas 4690
                    valid = pix.convertFromImage(bgImage);
277 andreas 4691
                    MSG_DEBUG("Scaled image from " << width << "x" << height << " to " << obj->width << "x" << obj->height);
222 andreas 4692
                }
4693
                else
4694
                {
4695
                    valid = pix.convertFromImage(img);
277 andreas 4696
                    MSG_DEBUG("Converted image to pixmap.");
222 andreas 4697
                }
4698
            }
221 andreas 4699
 
4700
            if (!valid || pix.isNull())
4701
            {
222 andreas 4702
                if (pix.isNull())
4703
                    pix = QPixmap(obj->width, obj->height);
4704
 
221 andreas 4705
                pix.fill(backgroundColor);
289 andreas 4706
                MSG_WARNING("Error converting an image! Size raw data: " << image.getSize() << ", Width: " << image.getWidth() << ", Height: " << image.getHeight() << ", Bytes per row: " << image.getPixline());
221 andreas 4707
            }
6 andreas 4708
        }
5 andreas 4709
 
223 andreas 4710
        QPalette palette(central->palette());
222 andreas 4711
        palette.setBrush(QPalette::Window, QBrush(pix));
4712
        central->setPalette(palette);
6 andreas 4713
 
272 andreas 4714
        if (index >= 0)
4715
            mCentralWidget->setCurrentIndex(index);
275 andreas 4716
        else if (mCentralWidget)
4717
        {
4718
            index = mCentralWidget->addWidget(central);
4719
            mCentralWidget->setCurrentIndex(index);
4720
            MSG_DEBUG("Page widget " << handleToString(handle) << " was added at index " << index);
4721
        }
272 andreas 4722
 
6 andreas 4723
        MSG_DEBUG("Background set");
5 andreas 4724
    }
4725
}
4726
 
296 andreas 4727
void MainWindow::disconnectArea(TQScrollArea* area)
4728
{
4729
    DECL_TRACER("MainWindow::disconnectArea(TQScrollArea* area)");
4730
 
297 andreas 4731
    if (!area)
4732
        return;
4733
 
296 andreas 4734
    disconnect(area, &TQScrollArea::objectClicked, this, &MainWindow::onSubViewItemClicked);
4735
}
4736
 
4737
void MainWindow::disconnectList(QListWidget* list)
4738
{
4739
    DECL_TRACER("MainWindow::disconnectList(QListWidget* list)");
4740
 
297 andreas 4741
    if (!list)
4742
        return;
4743
 
296 andreas 4744
    disconnect(list, &QListWidget::currentItemChanged, this, &MainWindow::onTListCallbackCurrentItemChanged);
4745
}
4746
 
297 andreas 4747
void MainWindow::reconnectArea(TQScrollArea * area)
4748
{
4749
    DECL_TRACER("MainWindow::reconnectArea(TQScrollArea * area)");
4750
 
4751
    if (!area)
4752
        return;
4753
 
4754
    connect(area, &TQScrollArea::objectClicked, this, &MainWindow::onSubViewItemClicked);
4755
}
4756
 
4757
void MainWindow::reconnectList(QListWidget *list)
4758
{
4759
    DECL_TRACER("MainWindow::reconnectList(QListWidget *list)");
4760
 
4761
    if (!list)
4762
        return;
4763
 
4764
    connect(list, &QListWidget::currentItemChanged, this, &MainWindow::onTListCallbackCurrentItemChanged);
4765
}
4766
 
296 andreas 4767
/**
4768
 * @brief MainWindow::dropPage - Marks a page invalid
4769
 * This method marks a page and all the object on the page as invalid. They are
4770
 * not deleted. Instead the page, a QWidget representing a page, is set hidden.
4771
 * With it all childs of the QWidget are also hidden. So the page can be
4772
 * displayed later when it is used again.
4773
 *
4774
 * @param handle    The handle of the page.
4775
 */
11 andreas 4776
void MainWindow::dropPage(ulong handle)
4777
{
343 andreas 4778
//    TLOCKER(draw_mutex);
11 andreas 4779
    DECL_TRACER("MainWindow::dropPage(ulong handle)");
7 andreas 4780
 
271 andreas 4781
    MSG_PROTOCOL("Dropping page " << handleToString(handle));
265 andreas 4782
 
294 andreas 4783
    OBJECT_t *obj = findObject(handle);
264 andreas 4784
 
296 andreas 4785
    if (!obj)
4786
    {
4787
        MSG_WARNING("Object " << handleToString(handle) << " (Page) does not exist. Ignoring!");
348 andreas 4788
#if TESTMODE == 1
4789
        setScreenDone();
4790
#endif
296 andreas 4791
        return;
4792
    }
271 andreas 4793
 
296 andreas 4794
    if (obj->type != OBJ_PAGE)
271 andreas 4795
    {
296 andreas 4796
        MSG_WARNING("Object " << handleToString(handle) << " is not a page!");
348 andreas 4797
#if TESTMODE == 1
4798
        setScreenDone();
4799
#endif
296 andreas 4800
        return;
271 andreas 4801
    }
296 andreas 4802
 
4803
    MSG_DEBUG("Dropping page " << handleToString(handle));
4804
    invalidateAllSubObjects(handle);
4805
 
297 andreas 4806
    if (obj->object.widget)
348 andreas 4807
    {
296 andreas 4808
        obj->object.widget->setHidden(true);
348 andreas 4809
#if TESTMODE == 1
4810
        __success = true;
4811
#endif
4812
    }
4813
#if TESTMODE == 1
4814
    setScreenDone();
4815
#endif
11 andreas 4816
}
4817
 
350 andreas 4818
void MainWindow::dropSubPage(ulong handle, ulong parent)
11 andreas 4819
{
350 andreas 4820
    DECL_TRACER("MainWindow::dropSubPage(ulong handle, ulong parent)");
11 andreas 4821
 
294 andreas 4822
    OBJECT_t *obj = findObject(handle);
350 andreas 4823
    OBJECT_t *par = findObject(parent);
11 andreas 4824
 
4825
    if (!obj)
4826
    {
296 andreas 4827
        MSG_WARNING("Object " << handleToString(handle) << " (SubPage) does not exist. Ignoring!");
335 andreas 4828
#if TESTMODE == 1
4829
        setScreenDone();
4830
#endif
11 andreas 4831
        return;
4832
    }
4833
 
350 andreas 4834
    if (!par)
4835
    {
4836
        MSG_DEBUG("Parent object " << handleToString(parent) << " not found!");
4837
#if TESTMODE == 1
4838
        setScreenDone();
4839
#endif
4840
        return;
4841
    }
4842
 
297 andreas 4843
    if (obj->type != OBJ_SUBPAGE)
296 andreas 4844
    {
4845
        MSG_WARNING("Object " << handleToString(handle) << " is not a SubPage!");
335 andreas 4846
#if TESTMODE == 1
4847
        setScreenDone();
4848
#endif
217 andreas 4849
        return;
296 andreas 4850
    }
217 andreas 4851
 
350 andreas 4852
    QWidget *w = par->object.widget->findChild<QWidget *>(QString("Subpage_%1").arg(handleToString(handle).c_str()));
4853
 
4854
    if (!w)
4855
    {
4856
        MSG_DEBUG("Parent object " << handleToString(parent) << " has no child " << handleToString(handle) << "!");
4857
        obj->object.widget = nullptr;
4858
        obj->remove = true;
4859
        obj->invalid = true;
4860
#if TESTMODE == 1
4861
        setScreenDone();
4862
#endif
4863
        return;
4864
    }
4865
 
296 andreas 4866
    MSG_DEBUG("Dropping subpage " << handleToString(handle));
4867
    invalidateAllSubObjects(handle);
107 andreas 4868
    obj->aniDirection = false;
217 andreas 4869
 
4870
    if (gPageManager && gPageManager->isSetupActive())
4871
        obj->animate.hideEffect = SE_NONE;
4872
 
298 andreas 4873
    bool ret = startAnimation(obj, obj->animate, false);
43 andreas 4874
 
298 andreas 4875
    if (obj->animate.hideEffect == SE_NONE || !ret || !mLastObject)
42 andreas 4876
    {
297 andreas 4877
        obj->invalid = true;
4878
        obj->remove = false;
296 andreas 4879
 
4880
        if (obj->object.widget)
335 andreas 4881
        {
297 andreas 4882
            obj->object.widget->hide();
335 andreas 4883
#if TESTMODE == 1
4884
            __success = true;
4885
#endif
4886
        }
42 andreas 4887
    }
348 andreas 4888
#if TESTMODE == 1
4889
    setScreenDone();
4890
#endif
11 andreas 4891
}
4892
 
98 andreas 4893
void MainWindow::dropButton(ulong handle)
4894
{
343 andreas 4895
//    TLOCKER(draw_mutex);
98 andreas 4896
    DECL_TRACER("MainWindow::dropButton(ulong handle)");
4897
 
294 andreas 4898
    OBJECT_t *obj = findObject(handle);
98 andreas 4899
 
4900
    if (!obj)
4901
    {
271 andreas 4902
        MSG_WARNING("Object " << handleToString(handle) << " does not exist. Ignoring!");
98 andreas 4903
        return;
4904
    }
4905
 
294 andreas 4906
    if (obj->type == OBJ_PAGE || obj->type == OBJ_SUBPAGE)
217 andreas 4907
        return;
4908
 
296 andreas 4909
    invalidateObject(handle);
98 andreas 4910
}
252 andreas 4911
 
197 andreas 4912
void MainWindow::setSizeMainWindow(int width, int height)
4913
{
252 andreas 4914
#if !defined (Q_OS_ANDROID) && !defined(Q_OS_IOS)
197 andreas 4915
    DECL_TRACER("MainWindow::setSizeMainWindow(int width, int height)");
4916
 
198 andreas 4917
    QRect geo = geometry();
4918
    setGeometry(geo.x(), geo.y(), width, height + menuBar()->height());
252 andreas 4919
#else
4920
    Q_UNUSED(width);
4921
    Q_UNUSED(height);
4922
#endif
197 andreas 4923
}
252 andreas 4924
 
21 andreas 4925
void MainWindow::playVideo(ulong handle, ulong parent, int left, int top, int width, int height, const string& url, const string& user, const string& pw)
4926
{
343 andreas 4927
//    TLOCKER(draw_mutex);
21 andreas 4928
    DECL_TRACER("MainWindow::playVideo(ulong handle, const string& url, const string& user, const string& pw))");
4929
 
277 andreas 4930
    TObject::OBJECT_t *obj = findObject(handle);
4931
    TObject::OBJECT_t *par = findObject(parent);
271 andreas 4932
    MSG_TRACE("Processing button " << handleToString(handle) << " from parent " << handleToString(parent));
107 andreas 4933
 
21 andreas 4934
    if (!par)
4935
    {
4936
        MSG_WARNING("Button has no parent! Ignoring it.");
4937
        return;
4938
    }
4939
 
4940
    if (!obj)
4941
    {
4942
        MSG_DEBUG("Adding new video object ...");
296 andreas 4943
        OBJECT_t nobj;
21 andreas 4944
 
296 andreas 4945
        nobj.type = TObject::OBJ_VIDEO;
4946
        nobj.handle = handle;
21 andreas 4947
 
198 andreas 4948
        if (gPageManager && gPageManager->isSetupActive())
4949
        {
296 andreas 4950
            nobj.width = scaleSetup(width);
4951
            nobj.height = scaleSetup(height);
4952
            nobj.left = scaleSetup(left);
4953
            nobj.top = scaleSetup(top);
198 andreas 4954
        }
4955
        else
4956
        {
296 andreas 4957
            nobj.width = scale(width);
4958
            nobj.height = scale(height);
4959
            nobj.left = scale(left);
4960
            nobj.top = scale(top);
198 andreas 4961
        }
4962
 
296 andreas 4963
        nobj.object.vwidget = new QVideoWidget(par->object.widget);
4964
        nobj.object.vwidget->installEventFilter(this);
4965
 
4966
        if (!addObject(nobj))
4967
        {
4968
            MSG_ERROR("Error creating a video object!");
4969
            return;
4970
        }
4971
 
4972
        obj = findObject(handle);
21 andreas 4973
    }
4974
    else
277 andreas 4975
        MSG_DEBUG("Object " << handleToString(handle) << " of type " << objectToString(obj->type) << " found!");
21 andreas 4976
 
264 andreas 4977
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
21 andreas 4978
    QMediaPlaylist *playlist = new QMediaPlaylist;
181 andreas 4979
#endif
21 andreas 4980
    QUrl qurl(url.c_str());
4981
 
4982
    if (!user.empty())
4983
        qurl.setUserName(user.c_str());
4984
 
4985
    if (!pw.empty())
4986
        qurl.setPassword(pw.c_str());
4987
 
264 andreas 4988
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
21 andreas 4989
    playlist->addMedia(qurl);
181 andreas 4990
#endif
21 andreas 4991
    obj->player = new QMediaPlayer;
264 andreas 4992
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
21 andreas 4993
    obj->player->setPlaylist(playlist);
181 andreas 4994
#else
4995
    obj->player->setSource(qurl);
4996
#endif
21 andreas 4997
    obj->player->setVideoOutput(obj->object.vwidget);
31 andreas 4998
 
107 andreas 4999
    obj->object.vwidget->show();
5000
    obj->player->play();
21 andreas 5001
}
5002
 
291 andreas 5003
void MainWindow::inputText(Button::TButton *button, QByteArray buf, int width, int height, int frame, size_t pixline)
50 andreas 5004
{
279 andreas 5005
    DECL_TRACER("MainWindow::inputText(Button::TButton& button, QByteArray buf, int width, int height, int frame, size_t pixline)");
50 andreas 5006
 
310 andreas 5007
    if (!button)
5008
    {
5009
        MSG_WARNING("Method was called with no button!");
5010
        return;
5011
    }
5012
 
343 andreas 5013
//    TLOCKER(draw_mutex);
291 andreas 5014
    ulong handle = button->getHandle();
5015
    ulong parent = button->getParent();
277 andreas 5016
    TObject::OBJECT_t *obj = findObject(handle);
5017
    TObject::OBJECT_t *par = findObject(parent);
271 andreas 5018
    MSG_TRACE("Processing button " << handleToString(handle) << " from parent " << handleToString(parent) << " with frame width " << frame);
50 andreas 5019
 
5020
    if (!par)
5021
    {
5022
        MSG_WARNING("Button has no parent! Ignoring it.");
5023
        return;
5024
    }
5025
 
310 andreas 5026
    int instance = button->getActiveInstance();
5027
    MSG_DEBUG("Instance: " << instance);
5028
 
50 andreas 5029
    if (!obj)
5030
    {
5031
        MSG_DEBUG("Adding new input object ...");
296 andreas 5032
        OBJECT_t nobj;
50 andreas 5033
 
296 andreas 5034
        nobj.type = TObject::OBJ_INPUT;
5035
        nobj.handle = handle;
50 andreas 5036
 
198 andreas 5037
        if (gPageManager && gPageManager->isSetupActive())
5038
        {
296 andreas 5039
            nobj.width = scaleSetup(width);
5040
            nobj.height = scaleSetup(height);
5041
            nobj.left = scaleSetup(button->getLeftPosition());
5042
            nobj.top = scaleSetup(button->getTopPosition());
198 andreas 5043
        }
5044
        else
5045
        {
296 andreas 5046
            nobj.width = scale(width);
5047
            nobj.height = scale(height);
5048
            nobj.left = scale(button->getLeftPosition());
5049
            nobj.top = scale(button->getTopPosition());
198 andreas 5050
        }
5051
 
310 andreas 5052
        string text = button->getText(instance);
291 andreas 5053
        string mask = button->getInputMask();
191 andreas 5054
 
296 andreas 5055
        nobj.object.plaintext = new TQEditLine(text, par->object.widget, button->isMultiLine());
5056
        nobj.object.plaintext->setObjectName(string("EditLine_") + handleToString(handle));
5057
        nobj.object.plaintext->setHandle(handle);
5058
        nobj.object.plaintext->move(nobj.left, nobj.top);
5059
        nobj.object.plaintext->setFixedSize(nobj.width, nobj.height);
5060
        nobj.object.plaintext->setPadding(frame, frame, frame, frame);
5061
        nobj.object.plaintext->setWordWrapMode(button->getTextWordWrap());
5062
        nobj.object.plaintext->setPasswordChar(button->getPasswordChar());
5063
        nobj.wid = nobj.object.plaintext->winId();
192 andreas 5064
 
213 andreas 5065
        if (gPageManager->isSetupActive())
5066
        {
5067
            int ch = 0;
5068
 
291 andreas 5069
            if (button->getAddressPort() == 0 && button->getAddressChannel() > 0)
5070
                ch = button->getAddressChannel();
5071
            else if (button->getChannelPort() == 0 && button->getChannelNumber() > 0)
5072
                ch = button->getChannelNumber();
213 andreas 5073
 
5074
            switch(ch)
5075
            {
5076
                case SYSTEM_ITEM_SIPPORT:
5077
                case SYSTEM_ITEM_NETLINX_PORT:
296 andreas 5078
                    nobj.object.plaintext->setInputMask("000000");
5079
                    nobj.object.plaintext->setNumericInput();
213 andreas 5080
                break;
5081
 
5082
                case SYSTEM_ITEM_NETLINX_CHANNEL:
296 andreas 5083
                    nobj.object.plaintext->setInputMask("99999");
5084
                    nobj.object.plaintext->setNumericInput();
213 andreas 5085
                break;
5086
            }
5087
        }
224 andreas 5088
        else if (!mask.empty())
296 andreas 5089
            nobj.object.plaintext->setInputMask(convertMask(mask));
213 andreas 5090
 
52 andreas 5091
        if (!buf.size() || pixline == 0)
51 andreas 5092
        {
5093
            MSG_ERROR("No image!");
5094
            TError::setError();
5095
            return;
5096
        }
5097
 
52 andreas 5098
        MSG_DEBUG("Background image size: " << width << " x " << height << ", rowBytes: " << pixline);
5099
        QPixmap pix(width, height);
5100
        QImage img((uchar *)buf.data(), width, height, QImage::Format_ARGB32);
50 andreas 5101
 
198 andreas 5102
        if (gPageManager && gPageManager->isSetupActive())
5103
            pix.convertFromImage(img.scaled(scaleSetup(width), scaleSetup(height), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
5104
        else if (isScaled())
52 andreas 5105
            pix.convertFromImage(img.scaled(scale(width), scale(height), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
50 andreas 5106
        else
5107
            pix.convertFromImage(img);
5108
 
5109
        // Load the font
291 andreas 5110
        FONT_T font = button->getFont();
51 andreas 5111
        vector<string> fontList = TFont::getFontPathList();
5112
        vector<string>::iterator iter;
5113
        string ffile;
50 andreas 5114
 
51 andreas 5115
        for (iter = fontList.begin(); iter != fontList.end(); ++iter)
50 andreas 5116
        {
51 andreas 5117
            TValidateFile vf;
50 andreas 5118
 
51 andreas 5119
            if (!vf.isValidFile(*iter + "/" + font.file))
5120
                continue;
5121
 
5122
            ffile = *iter + "/" + font.file;
5123
            break;
50 andreas 5124
        }
5125
 
51 andreas 5126
        if (ffile.empty())
5127
        {
5128
            MSG_ERROR("Font " << font.file << " doesn't exists!");
5129
            return;
5130
        }
5131
 
213 andreas 5132
        if (QFontDatabase::addApplicationFont(ffile.c_str()) == -1)
50 andreas 5133
        {
5134
            MSG_ERROR("Font " << ffile << " could not be loaded!");
5135
            TError::setError();
5136
            return;
5137
        }
5138
 
5139
        QFont ft;
5140
        ft.setFamily(font.name.c_str());
198 andreas 5141
 
5142
        if (gPageManager && gPageManager->isSetupActive())
303 andreas 5143
        {
5144
            int eighty = (int)((double)button->getHeight() / 100.0 * 85.0);
213 andreas 5145
            ft.setPixelSize(scaleSetup(eighty - frame * 2));
303 andreas 5146
        }
198 andreas 5147
        else
303 andreas 5148
            ft.setPixelSize(scale(font.size));
198 andreas 5149
 
213 andreas 5150
        MSG_DEBUG("Using font \"" << font.name << "\" with size " << font.size << "px.");
50 andreas 5151
 
291 andreas 5152
        switch (button->getFontStyle())
50 andreas 5153
        {
5154
            case FONT_BOLD:     ft.setBold(true); break;
5155
            case FONT_ITALIC:   ft.setItalic(true); break;
5156
            case FONT_BOLD_ITALIC:
5157
                ft.setBold(true);
5158
                ft.setItalic(true);
5159
                break;
5160
 
5161
            default:
5162
                ft.setBold(false);
5163
                ft.setItalic(false);
5164
        }
5165
 
303 andreas 5166
        QPalette palette(this->palette());
310 andreas 5167
        TColor::COLOR_T textColor = TColor::getAMXColor(button->getTextColor(instance));
5168
        TColor::COLOR_T fillColor = TColor::getAMXColor(button->getFillColor(instance));
51 andreas 5169
        QColor txcolor(QColor::fromRgba(qRgba(textColor.red, textColor.green, textColor.blue, textColor.alpha)));
52 andreas 5170
        QColor cfcolor(QColor::fromRgba(qRgba(fillColor.red, fillColor.green, fillColor.blue, fillColor.alpha)));
303 andreas 5171
        palette.setColor(QPalette::Window, cfcolor);
52 andreas 5172
        palette.setColor(QPalette::Base, cfcolor);
51 andreas 5173
        palette.setColor(QPalette::Text, txcolor);
303 andreas 5174
        palette.setBrush(QPalette::Window, QBrush(pix));
188 andreas 5175
 
296 andreas 5176
        nobj.object.plaintext->setFont(ft);
5177
        nobj.object.plaintext->setPalette(palette);
310 andreas 5178
        nobj.object.plaintext->setTextColor(txcolor);
296 andreas 5179
 
5180
        if (!addObject(nobj))
5181
        {
5182
            MSG_ERROR("Error creating an input object!");
5183
            return;
5184
        }
303 andreas 5185
 
5186
        connect(nobj.object.plaintext, &TQEditLine::inputChanged, this, &MainWindow::onInputChanged);
309 andreas 5187
        connect(nobj.object.plaintext, &TQEditLine::cursorPositionChanged, this, &MainWindow::onCursorChanged);
5188
        connect(nobj.object.plaintext, &TQEditLine::focusChanged, this, &MainWindow::onFocusChanged);
50 andreas 5189
    }
5190
    else
206 andreas 5191
    {
277 andreas 5192
        MSG_DEBUG("Object " << handleToString(handle) << " of type " << objectToString(obj->type) << " found!");
206 andreas 5193
 
310 andreas 5194
        string text = button->getText(instance);
291 andreas 5195
        string mask = button->getInputMask();
206 andreas 5196
        obj->object.plaintext->setText(text);
224 andreas 5197
 
5198
        if (!mask.empty())
5199
            obj->object.plaintext->setInputMask(convertMask(mask));
5200
 
310 andreas 5201
        QPixmap pix(obj->width, obj->height);
5202
        QImage img((uchar *)buf.data(), width, height, QImage::Format_ARGB32);
5203
 
5204
        if (isScaled())
5205
            pix.convertFromImage(img.scaled(obj->width, obj->height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
5206
        else
5207
            pix.convertFromImage(img);
5208
 
5209
        QPalette palette(this->palette());
5210
        TColor::COLOR_T textColor = TColor::getAMXColor(button->getTextColor(instance));
5211
        TColor::COLOR_T fillColor = TColor::getAMXColor(button->getFillColor(instance));
5212
        QColor txcolor(QColor::fromRgba(qRgba(textColor.red, textColor.green, textColor.blue, textColor.alpha)));
5213
        QColor cfcolor(QColor::fromRgba(qRgba(fillColor.red, fillColor.green, fillColor.blue, fillColor.alpha)));
5214
        palette.setColor(QPalette::Window, cfcolor);
5215
        palette.setColor(QPalette::Base, cfcolor);
5216
        palette.setColor(QPalette::Text, txcolor);
5217
        palette.setBrush(QPalette::Window, QBrush(pix));
5218
 
5219
        obj->object.plaintext->setPalette(palette);
5220
        obj->object.plaintext->setTextColor(txcolor);
206 andreas 5221
    }
50 andreas 5222
}
62 andreas 5223
 
291 andreas 5224
void MainWindow::listBox(Button::TButton *button, QByteArray buffer, int width, int height, int frame, size_t pixline)
200 andreas 5225
{
279 andreas 5226
    DECL_TRACER("MainWindow::listBox(Button::TButton& button, QByteArray buffer, int width, int height, int frame, size_t pixline)");
200 andreas 5227
 
291 andreas 5228
    ulong handle = button->getHandle();
5229
    ulong parent = button->getParent();
277 andreas 5230
    TObject::OBJECT_t *obj = findObject(handle);
5231
    TObject::OBJECT_t *par = findObject(parent);
271 andreas 5232
    MSG_TRACE("Processing list " << handleToString(handle) << " from parent " << handleToString(parent) << " with frame width " << frame);
200 andreas 5233
 
5234
    if (!par)
5235
    {
5236
        MSG_WARNING("List has no parent! Ignoring it.");
5237
        return;
5238
    }
5239
 
5240
    if (!obj)
5241
    {
5242
        MSG_DEBUG("Adding new list object ...");
296 andreas 5243
        OBJECT_t nobj;
200 andreas 5244
 
296 andreas 5245
        nobj.type = TObject::OBJ_LIST;
5246
        nobj.handle = handle;
5247
        nobj.rows = button->getListNumRows();
5248
        nobj.cols = button->getListNumCols();
200 andreas 5249
 
5250
        if (gPageManager && gPageManager->isSetupActive())
5251
        {
296 andreas 5252
            nobj.width = scaleSetup(width);
5253
            nobj.height = scaleSetup(height);
5254
            nobj.left = scaleSetup(button->getLeftPosition());
5255
            nobj.top = scaleSetup(button->getTopPosition());
200 andreas 5256
        }
5257
        else
5258
        {
296 andreas 5259
            nobj.width = scale(width);
5260
            nobj.height = scale(height);
5261
            nobj.left = scale(button->getLeftPosition());
5262
            nobj.top = scale(button->getTopPosition());
200 andreas 5263
        }
5264
 
291 andreas 5265
        vector<string> listContent = button->getListContent();
200 andreas 5266
 
217 andreas 5267
        if (par->type == TObject::OBJ_PAGE)
296 andreas 5268
            nobj.object.list = new QListWidget(par->object.widget ? par->object.widget : centralWidget());
217 andreas 5269
        else
296 andreas 5270
            nobj.object.list = new QListWidget(par->object.widget);
200 andreas 5271
 
296 andreas 5272
        nobj.object.list->move(nobj.left, nobj.top);
5273
        nobj.object.list->setFixedSize(nobj.width, nobj.height);
5274
        connect(nobj.object.list, &QListWidget::currentItemChanged, this, &MainWindow::onTListCallbackCurrentItemChanged);
200 andreas 5275
 
5276
        if (!buffer.size() || pixline == 0)
5277
        {
5278
            MSG_ERROR("No image!");
5279
            TError::setError();
5280
            return;
5281
        }
5282
 
5283
        MSG_DEBUG("Background image size: " << width << " x " << height << ", rowBytes: " << pixline);
5284
        QPixmap pix(width, height);
5285
        QImage img((uchar *)buffer.data(), width, height, QImage::Format_ARGB32);
5286
 
5287
        if (gPageManager && gPageManager->isSetupActive())
5288
            pix.convertFromImage(img.scaled(scaleSetup(width), scaleSetup(height), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
5289
        else if (isScaled())
5290
            pix.convertFromImage(img.scaled(scale(width), scale(height), Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
5291
        else
5292
            pix.convertFromImage(img);
5293
 
5294
        // Load the font
291 andreas 5295
        FONT_T font = button->getFont();
200 andreas 5296
        vector<string> fontList = TFont::getFontPathList();
5297
        vector<string>::iterator iter;
5298
        string ffile;
5299
 
5300
        for (iter = fontList.begin(); iter != fontList.end(); ++iter)
5301
        {
5302
            TValidateFile vf;
5303
 
5304
            if (!vf.isValidFile(*iter + "/" + font.file))
5305
                continue;
5306
 
5307
            ffile = *iter + "/" + font.file;
5308
            break;
5309
        }
5310
 
5311
        if (ffile.empty())
5312
        {
5313
            MSG_ERROR("Font " << font.file << " doesn't exists!");
5314
            return;
5315
        }
5316
 
213 andreas 5317
        if (QFontDatabase::addApplicationFont(ffile.c_str()) == -1)
200 andreas 5318
        {
5319
            MSG_ERROR("Font " << ffile << " could not be loaded!");
5320
            TError::setError();
5321
            return;
5322
        }
5323
 
5324
        QFont ft;
5325
        ft.setFamily(font.name.c_str());
5326
 
213 andreas 5327
        if (gPageManager && gPageManager->isSetupActive())
5328
            ft.setPointSize(scaleSetup(font.size));
5329
        else
200 andreas 5330
            ft.setPointSize(font.size);
5331
 
5332
        MSG_DEBUG("Using font \"" << font.name << "\" with size " << font.size << "pt.");
5333
 
291 andreas 5334
        switch (button->getFontStyle())
200 andreas 5335
        {
5336
            case FONT_BOLD:     ft.setBold(true); break;
5337
            case FONT_ITALIC:   ft.setItalic(true); break;
5338
            case FONT_BOLD_ITALIC:
5339
                ft.setBold(true);
5340
                ft.setItalic(true);
5341
            break;
5342
 
5343
            default:
5344
                ft.setBold(false);
5345
                ft.setItalic(false);
5346
        }
5347
 
5348
        QPalette palette;
291 andreas 5349
        TColor::COLOR_T textColor = TColor::getAMXColor(button->getTextColor());
5350
        TColor::COLOR_T fillColor = TColor::getAMXColor(button->getFillColor());
200 andreas 5351
        QColor txcolor(QColor::fromRgba(qRgba(textColor.red, textColor.green, textColor.blue, textColor.alpha)));
5352
        QColor cfcolor(QColor::fromRgba(qRgba(fillColor.red, fillColor.green, fillColor.blue, fillColor.alpha)));
5353
        palette.setColor(QPalette::Base, cfcolor);
5354
        palette.setColor(QPalette::Text, txcolor);
217 andreas 5355
//        pix.save("frame.png");
200 andreas 5356
        palette.setBrush(QPalette::Base, QBrush(pix));
5357
 
296 andreas 5358
        nobj.object.list->setFont(ft);
5359
        nobj.object.list->setPalette(palette);
200 andreas 5360
        // Add content
5361
        if (!listContent.empty())
5362
        {
5363
            vector<string>::iterator iter;
5364
 
296 andreas 5365
            if (nobj.object.list->count() > 0)
5366
                nobj.object.list->clear();
205 andreas 5367
 
5368
            MSG_DEBUG("Adding " << listContent.size() << " entries to list.");
5369
            string selected = gPageManager->getSelectedItem(handle);
5370
            int index = 0;
5371
 
200 andreas 5372
            for (iter = listContent.begin(); iter != listContent.end(); ++iter)
205 andreas 5373
            {
296 andreas 5374
                nobj.object.list->addItem(iter->c_str());
205 andreas 5375
 
5376
                if (selected == *iter)
296 andreas 5377
                    nobj.object.list->setCurrentRow(index);
205 andreas 5378
 
5379
                index++;
5380
            }
200 andreas 5381
        }
205 andreas 5382
        else
5383
            MSG_DEBUG("No items for list!");
200 andreas 5384
 
296 andreas 5385
//        nobj.object.list->show();
5386
 
5387
        if (!addObject(nobj))
5388
        {
5389
            MSG_ERROR("Error creating a list object!");
5390
            return;
5391
        }
200 andreas 5392
    }
5393
    else
297 andreas 5394
    {
277 andreas 5395
        MSG_DEBUG("Object " << handleToString(handle) << " of type " << objectToString(obj->type) << " found!");
297 andreas 5396
        enableObject(handle);
5397
    }
200 andreas 5398
}
5399
 
63 andreas 5400
void MainWindow::showKeyboard(const std::string& init, const std::string& prompt, bool priv)
31 andreas 5401
{
63 andreas 5402
    DECL_TRACER("MainWindow::showKeyboard(std::string &init, std::string &prompt, bool priv)");
31 andreas 5403
 
63 andreas 5404
    if (mKeyboard)
5405
        return;
5406
 
5407
    mQKeyboard = new TQKeyboard(init, prompt, this);
5408
    mKeyboard = true;
62 andreas 5409
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
65 andreas 5410
    mQKeyboard->setScaleFactor(mScaleFactor);
62 andreas 5411
#endif
63 andreas 5412
    mQKeyboard->setPrivate(priv);
5413
    mQKeyboard->doResize();
5414
    mQKeyboard->setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::FramelessWindowHint);
5415
    int ret = mQKeyboard->exec();
120 andreas 5416
    string text = "KEYB-";
31 andreas 5417
 
62 andreas 5418
    if (ret == QDialog::Accepted)
63 andreas 5419
        text.append(mQKeyboard->getText());
62 andreas 5420
    else
120 andreas 5421
        text = "KEYB-ABORT";
62 andreas 5422
 
120 andreas 5423
    if (gPageManager)
5424
        gPageManager->sendKeyboard(text);
62 andreas 5425
 
63 andreas 5426
    delete mQKeyboard;
5427
    mQKeyboard = nullptr;
5428
    mKeyboard = false;
31 andreas 5429
}
62 andreas 5430
 
63 andreas 5431
void MainWindow::showKeypad(const std::string& init, const std::string& prompt, bool priv)
62 andreas 5432
{
63 andreas 5433
    DECL_TRACER("MainWindow::showKeypad(std::string& init, std::string& prompt, bool priv)");
62 andreas 5434
 
65 andreas 5435
    if (mKeypad)
63 andreas 5436
        return;
5437
 
5438
    mQKeypad = new TQKeypad(init, prompt, this);
65 andreas 5439
    mKeypad = true;
62 andreas 5440
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
63 andreas 5441
    mQKeypad->setScaleFactor(mScaleFactor);
65 andreas 5442
#endif
5443
    mQKeypad->setPrivate(priv);
111 andreas 5444
    mQKeypad->setMaxLength(50);     // Standard maximum length
63 andreas 5445
    mQKeypad->doResize();
5446
    mQKeypad->setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::FramelessWindowHint);
65 andreas 5447
    int ret = mQKeypad->exec();
62 andreas 5448
 
5449
    if (ret == QDialog::Accepted)
5450
    {
5451
        string text = "KEYP-";
63 andreas 5452
        text.append(mQKeypad->getText());
62 andreas 5453
 
5454
        if (gPageManager)
5455
            gPageManager->sendKeypad(text);
5456
    }
5457
    else
5458
    {
5459
        string text = "KEYP-ABORT";
5460
 
5461
        if (gPageManager)
5462
            gPageManager->sendKeypad(text);
5463
    }
5464
 
63 andreas 5465
    delete mQKeypad;
5466
    mQKeypad = nullptr;
65 andreas 5467
    mKeypad = false;
62 andreas 5468
}
5469
 
63 andreas 5470
void MainWindow::resetKeyboard()
5471
{
5472
    DECL_TRACER("MainWindow::resetKeyboard()");
5473
 
5474
    if (mQKeyboard)
5475
        mQKeyboard->reject();
5476
 
5477
    if (mQKeypad)
211 andreas 5478
        mQKeypad->reject();
63 andreas 5479
}
5480
 
111 andreas 5481
void MainWindow::sendVirtualKeys(const string& str)
5482
{
5483
    DECL_TRACER("MainWindow::sendVirtualKeys(const string& str)");
5484
 
5485
    if (mKeyboard && mQKeyboard)
5486
        mQKeyboard->setString(str);
5487
    else if (mKeypad && mQKeypad)
5488
        mQKeypad->setString(str);
5489
}
5490
 
64 andreas 5491
void MainWindow::showSetup()
5492
{
5493
    DECL_TRACER("MainWindow::showSetup()");
251 andreas 5494
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
211 andreas 5495
    settings();
5496
#else
250 andreas 5497
#ifndef Q_OS_IOS
197 andreas 5498
    if (gPageManager)
5499
        gPageManager->showSetup();
250 andreas 5500
#else
259 andreas 5501
    mIOSSettingsActive = true;
250 andreas 5502
    QASettings::openSettings();
5503
#endif  // Q_OS_IOS
251 andreas 5504
#endif  // !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
64 andreas 5505
}
5506
 
71 andreas 5507
void MainWindow::playSound(const string& file)
5508
{
5509
    DECL_TRACER("MainWindow::playSound(const string& file)");
5510
 
5511
    MSG_DEBUG("Playing file " << file);
141 andreas 5512
 
5513
    if (TConfig::getMuteState())
326 andreas 5514
    {
5515
#if TESTMODE == 1
5516
        __success = true;
334 andreas 5517
        setAllDone();
326 andreas 5518
#endif
141 andreas 5519
        return;
326 andreas 5520
    }
141 andreas 5521
 
5522
    if (!mMediaPlayer)
181 andreas 5523
    {
141 andreas 5524
        mMediaPlayer = new QMediaPlayer;
264 andreas 5525
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
181 andreas 5526
        mAudioOutput = new QAudioOutput;
335 andreas 5527
        mMediaPlayer->setAudioOutput(mAudioOutput);
181 andreas 5528
#endif
5529
    }
141 andreas 5530
 
264 andreas 5531
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
141 andreas 5532
    mMediaPlayer->setMedia(QUrl::fromLocalFile(file.c_str()));
5533
    mMediaPlayer->setVolume(calcVolume(TConfig::getSystemVolume()));
335 andreas 5534
 
342 andreas 5535
    if (mMediaPlayer->state() != QMediaPlayer::StoppedState)
335 andreas 5536
        mMediaPlayer->stop();
346 andreas 5537
#else   // QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
181 andreas 5538
    mMediaPlayer->setSource(QUrl::fromLocalFile(file.c_str()));
335 andreas 5539
    mAudioOutput->setVolume(calcVolume(TConfig::getSystemVolume()));
5540
 
5541
    if (!mMediaPlayer->isAvailable())
5542
    {
5543
        MSG_WARNING("No audio modul found!");
5544
#if TESTMODE == 1
5545
        setAllDone();
181 andreas 5546
#endif
335 andreas 5547
        return;
5548
    }
5549
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
5550
    if (mMediaPlayer->isPlaying())
5551
        mMediaPlayer->stop();
5552
#else
5553
    if (mMediaPlayer->playbackState() != QMediaPlayer::StoppedState)
5554
        mMediaPlayer->stop();
5555
#endif  // #if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
346 andreas 5556
    mMediaPlayer->setPosition(0);
335 andreas 5557
#endif  // QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
141 andreas 5558
    mMediaPlayer->play();
326 andreas 5559
#if TESTMODE == 1
335 andreas 5560
    QMediaPlayer::Error err = QMediaPlayer::NoError;
5561
 
5562
    if ((err = mMediaPlayer->error()) != QMediaPlayer::NoError)
5563
    {
5564
        MSG_ERROR("Error playing \"" << file << "\": " << mMediaPlayer->errorString().toStdString());
5565
    }
5566
    else
5567
        __success = true;
5568
 
334 andreas 5569
    setAllDone();
326 andreas 5570
#endif
71 andreas 5571
}
5572
 
141 andreas 5573
void MainWindow::stopSound()
5574
{
5575
    DECL_TRACER("MainWindow::stopSound()");
5576
 
5577
    if (mMediaPlayer)
5578
        mMediaPlayer->stop();
5579
}
5580
 
5581
void MainWindow::muteSound(bool state)
5582
{
5583
    DECL_TRACER("MainWindow::muteSound(bool state)");
5584
 
264 andreas 5585
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
141 andreas 5586
    if (mMediaPlayer)
5587
        mMediaPlayer->setMuted(state);
181 andreas 5588
#else
5589
    if (mAudioOutput)
5590
        mAudioOutput->setMuted(state);
5591
#endif
335 andreas 5592
#if TESTMODE == 1
5593
    __success = true;
5594
    setAllDone();
5595
#endif
141 andreas 5596
}
5597
 
335 andreas 5598
void MainWindow::setVolume(int volume)
5599
{
5600
    DECL_TRACER("MainWindow::setVolume(int volume)");
5601
 
5602
    if (!mMediaPlayer)
5603
    {
5604
#if TESTMODE == 1
5605
        setAllDone();
5606
#endif
5607
        return;
5608
    }
5609
 
5610
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
5611
    mMediaPlayer->setVolume(calcVolume(volume));
5612
#else
5613
    if (!mMediaPlayer || !mAudioOutput)
5614
    {
5615
#if TESTMODE == 1
5616
        setAllDone();
5617
#endif
5618
        return;
5619
    }
5620
 
5621
    mAudioOutput->setVolume(calcVolume(volume));
5622
#if TESTMODE == 1
5623
    MSG_DEBUG("Volume was set to " << volume);
5624
    __success = true;
5625
    setAllDone();
5626
#endif
5627
#endif  // QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
5628
}
5629
 
31 andreas 5630
void MainWindow::playShowList()
5631
{
5632
    DECL_TRACER("MainWindow::playShowList()");
5633
 
61 andreas 5634
    _EMIT_TYPE_t etype = getNextType();
5635
 
5636
    while (etype != ET_NONE)
5637
    {
5638
        ulong handle = 0;
5639
        ulong parent = 0;
5640
        int left = 0;
5641
        int top = 0;
5642
        int width = 0;
5643
        int height = 0;
192 andreas 5644
        int frame = 0;
289 andreas 5645
        TBitmap image;
61 andreas 5646
        ulong color = 0;
289 andreas 5647
        int space{0};
5648
        bool vertical = false;
5649
        TColor::COLOR_T fillColor;
298 andreas 5650
        bool pth = false;
262 andreas 5651
#ifndef _OPAQUE_SKIA_
5652
        int opacity = 255;
5653
#endif
61 andreas 5654
        ANIMATION_t animate;
5655
        std::string url;
5656
        std::string user;
5657
        std::string pw;
5658
        Button::TButton *button;
5659
        Button::BITMAP_t bm;
5660
 
5661
        switch(etype)
5662
        {
5663
            case ET_BACKGROUND:
262 andreas 5664
#ifdef _OPAQUE_SKIA_
289 andreas 5665
                if (getBackground(&handle, &image, &width, &height, &color))
262 andreas 5666
#else
289 andreas 5667
                if (getBackground(&handle, &image, &width, &height, &color, &opacity))
262 andreas 5668
#endif
61 andreas 5669
                {
271 andreas 5670
                    MSG_PROTOCOL("Replay: BACKGROUND of object " << handleToString(handle));
262 andreas 5671
#ifdef _OPAQUE_SKIA_
289 andreas 5672
                    emit sigSetBackground(handle, image, width, height, color);
262 andreas 5673
#else
289 andreas 5674
                    emit sigSetBackground(handle, image, width, height, color, opacity);
262 andreas 5675
#endif
61 andreas 5676
                }
5677
            break;
5678
 
5679
            case ET_BUTTON:
298 andreas 5680
                if (getButton(&handle, &parent, &image, &left, &top, &width, &height, &pth))
61 andreas 5681
                {
271 andreas 5682
                    MSG_PROTOCOL("Replay: BUTTON object " << handleToString(handle));
298 andreas 5683
                    emit sigDisplayButton(handle, parent, image, width, height, left, top, pth);
61 andreas 5684
                }
5685
            break;
5686
 
5687
            case ET_INTEXT:
192 andreas 5688
                if (getInText(&handle, &button, &bm, &frame))
61 andreas 5689
                {
5690
                    QByteArray buf;
5691
 
5692
                    if (bm.buffer && bm.rowBytes > 0)
5693
                    {
363 andreas 5694
                        size_t s = (size_t)bm.width * (size_t)bm.height * (size_t)(bm.rowBytes / bm.width);
286 andreas 5695
                        buf.insert(0, (const char *)bm.buffer, s);
61 andreas 5696
                    }
5697
 
271 andreas 5698
                    MSG_PROTOCOL("Replay: INTEXT object " << handleToString(handle));
291 andreas 5699
                    emit sigInputText(button, buf, bm.width, bm.height, bm.rowBytes, frame);
61 andreas 5700
                }
5701
            break;
5702
 
5703
            case ET_PAGE:
5704
                if (getPage(&handle, &width, &height))
5705
                {
271 andreas 5706
                    MSG_PROTOCOL("Replay: PAGE object " << handleToString(handle));
217 andreas 5707
 
61 andreas 5708
                    if (isDeleted())
5709
                        emit sigDropPage(handle);
5710
                    else
5711
                        emit sigSetPage(handle, width, height);
5712
                }
5713
            break;
5714
 
5715
            case ET_SUBPAGE:
217 andreas 5716
                if (getSubPage(&handle, &parent, &left, &top, &width, &height, &animate))
61 andreas 5717
                {
271 andreas 5718
                    MSG_PROTOCOL("Replay: SUBPAGE object " << handleToString(handle));
217 andreas 5719
 
61 andreas 5720
                    if (isDeleted())
350 andreas 5721
                        emit sigDropSubPage(handle, parent);
61 andreas 5722
                    else
217 andreas 5723
                        emit sigSetSubPage(handle, parent, left, top, width, height, animate);
61 andreas 5724
                }
5725
            break;
5726
 
289 andreas 5727
            case ET_SUBVIEW:
5728
                if (getViewButton(&handle, &parent, &vertical, &image, &left, &top, &width, &height, &space, &fillColor))
5729
                {
5730
                    MSG_PROTOCOL("Replay: SUBVIEW object " << handleToString(handle));
5731
                    emit sigDisplayViewButton(handle, parent, vertical, image, width, height, left, top, space, fillColor);
5732
                }
5733
            break;
5734
 
61 andreas 5735
            case ET_VIDEO:
5736
                if (getVideo(&handle, &parent, &left, &top, &width, &height, &url, &user, &pw))
5737
                {
271 andreas 5738
                    MSG_PROTOCOL("Replay: VIDEO object " << handleToString(handle));
61 andreas 5739
                    emit sigPlayVideo(handle, parent, left, top, width, height, url, user, pw);
5740
                }
5741
            break;
5742
 
5743
            default:
5744
                MSG_WARNING("Type " << etype << " is currently not supported!");
5745
        }
5746
 
5747
        dropType(etype);
5748
        etype = getNextType();
5749
    }
5750
/*
31 andreas 5751
    std::map<ulong, QWidget *>::iterator iter;
5752
 
5753
    for (iter = mToShow.begin(); iter != mToShow.end(); ++iter)
5754
    {
5755
        OBJECT_t *obj = findObject(iter->first);
5756
 
5757
        if (obj)
5758
            iter->second->show();
5759
    }
5760
 
5761
    mToShow.clear();
61 andreas 5762
*/
31 andreas 5763
}
5764
 
42 andreas 5765
 
38 andreas 5766
int MainWindow::scale(int value)
5767
{
262 andreas 5768
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
5769
    double s = gScale;
5770
#else
5771
    double s = mScaleFactor;
5772
#endif
5773
    if (value <= 0 || s == 1.0 || s < 0.0)
38 andreas 5774
        return value;
5775
 
262 andreas 5776
    return (int)((double)value * s);
38 andreas 5777
}
5778
 
198 andreas 5779
int MainWindow::scaleSetup(int value)
5780
{
5781
    DECL_TRACER("MainWindow::scaleSetup(int value)");
5782
 
212 andreas 5783
    if (value <= 0 || mSetupScaleFactor == 1.0 || mSetupScaleFactor <= 0.0)
198 andreas 5784
        return value;
5785
 
5786
    double val = (double)value * mSetupScaleFactor;
5787
 
5788
    return (int)val;
5789
}
5790
 
259 andreas 5791
bool MainWindow::isScaled()
5792
{
275 andreas 5793
    DECL_TRACER("MainWindow::isScaled()");
5794
 
262 andreas 5795
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
5796
    double s = gScale;
5797
#else
5798
    double s = mScaleFactor;
5799
#endif
5800
    if (s > 0.0 && s != 1.0 && gPageManager && TConfig::getScale())
259 andreas 5801
        return true;
5802
 
5803
    return false;
5804
}
5805
 
198 andreas 5806
bool MainWindow::isSetupScaled()
5807
{
5808
    DECL_TRACER("MainWindow::isSetupScaled()");
5809
 
5810
    return (mSetupScaleFactor > 0.0 && mSetupScaleFactor != 1.0);
5811
}
5812
 
298 andreas 5813
bool MainWindow::startAnimation(TObject::OBJECT_t* obj, ANIMATION_t& ani, bool in)
42 andreas 5814
{
5815
    DECL_TRACER("MainWindow::startAnimation(OBJECT_t* obj, ANIMATION_t& ani)");
43 andreas 5816
 
107 andreas 5817
    if (!obj)
5818
    {
5819
        MSG_ERROR("Got no object to start the animation!");
298 andreas 5820
        return false;
107 andreas 5821
    }
5822
 
296 andreas 5823
    TLOCKER(anim_mutex);
42 andreas 5824
    int scLeft = obj->left;
5825
    int scTop = obj->top;
5826
    int scWidth = obj->width;
5827
    int scHeight = obj->height;
298 andreas 5828
    int duration = (in ? ani.showTime : ani.hideTime);
5829
    SHOWEFFECT_t effect = (in ? ani.showEffect : ani.hideEffect);
42 andreas 5830
    mLastObject = nullptr;
43 andreas 5831
 
298 andreas 5832
    if (effect == SE_NONE || duration <= 0 || (obj->type != OBJ_SUBPAGE && obj->type != OBJ_PAGE))
5833
        return false;
42 andreas 5834
 
298 andreas 5835
    if (!obj->object.widget)
5836
    {
5837
        MSG_WARNING("Object " << handleToString(obj->handle) << " has no widget defined! Ignoring fade effect.");
332 andreas 5838
#if TESTMODE == 1
334 andreas 5839
        setScreenDone();
332 andreas 5840
#endif
298 andreas 5841
        return false;
5842
    }
43 andreas 5843
 
58 andreas 5844
    if (effect == SE_FADE)
5845
    {
271 andreas 5846
        MSG_DEBUG("Fading object " << handleToString(obj->handle) << (in ? " IN" : " OUT"));
58 andreas 5847
        QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect(obj->object.widget);
289 andreas 5848
 
5849
        if (effect)
5850
        {
298 andreas 5851
            obj->object.widget->setGraphicsEffect(effect);
289 andreas 5852
            obj->animation = new QPropertyAnimation(effect, "opacity");
5853
        }
58 andreas 5854
    }
5855
    else
5856
    {
271 andreas 5857
        MSG_DEBUG("Moving object " << handleToString(obj->handle) << (in ? " IN" : " OUT"));
58 andreas 5858
        obj->animation = new QPropertyAnimation(obj->object.widget);
5859
        obj->animation->setTargetObject(obj->object.widget);
5860
    }
42 andreas 5861
 
298 andreas 5862
    obj->animation->setDuration(duration * 100);    // convert 10th of seconds into milliseconds
296 andreas 5863
    MSG_DEBUG("Processing animation effect " << effect << " with a duration of " << obj->animation->duration() << "ms");
43 andreas 5864
 
54 andreas 5865
    switch(effect)
5866
    {
5867
        case SE_SLIDE_BOTTOM_FADE:
5868
        case SE_SLIDE_BOTTOM:
5869
            obj->animation->setPropertyName("geometry");
42 andreas 5870
 
54 andreas 5871
            if (in)
5872
            {
5873
                obj->animation->setStartValue(QRect(scLeft, scTop + (scHeight * 2), scWidth, scHeight));
5874
                obj->animation->setEndValue(QRect(scLeft, scTop, scWidth, scHeight));
295 andreas 5875
                connect(obj->animation, &QPropertyAnimation::finished, this, &MainWindow::animationInFinished);
296 andreas 5876
                obj->object.widget->show();
54 andreas 5877
            }
5878
            else
5879
            {
5880
                obj->animation->setStartValue(QRect(scLeft, scTop, scWidth, scHeight));
5881
                obj->animation->setEndValue(QRect(scLeft, scTop + (scHeight * 2), scWidth, scHeight));
5882
                obj->remove = true;
5883
                connect(obj->animation, &QPropertyAnimation::finished, this, &MainWindow::animationFinished);
5884
            }
42 andreas 5885
 
296 andreas 5886
            mLastObject = obj;
5887
            mAnimObjects.insert(pair<ulong, OBJECT_t *>(obj->handle, obj));
54 andreas 5888
            obj->animation->start();
295 andreas 5889
            MSG_DEBUG("Animation SLIDE BOTTOM started.");
54 andreas 5890
        break;
43 andreas 5891
 
54 andreas 5892
        case SE_SLIDE_LEFT_FADE:
5893
        case SE_SLIDE_LEFT:
5894
            obj->animation->setPropertyName("geometry");
43 andreas 5895
 
54 andreas 5896
            if (in)
5897
            {
5898
                obj->animation->setStartValue(QRect(scLeft - scWidth, scTop, scWidth, scHeight));
5899
                obj->animation->setEndValue(QRect(scLeft, scTop, scWidth, scHeight));
295 andreas 5900
                connect(obj->animation, &QPropertyAnimation::finished, this, &MainWindow::animationInFinished);
296 andreas 5901
                obj->object.widget->show();
54 andreas 5902
            }
5903
            else
5904
            {
5905
                obj->animation->setStartValue(QRect(scLeft, scTop, scWidth, scHeight));
5906
                obj->animation->setEndValue(QRect(scLeft - scWidth, scTop, scWidth, scHeight));
5907
                obj->remove = true;
5908
                connect(obj->animation, &QPropertyAnimation::finished, this, &MainWindow::animationFinished);
5909
            }
42 andreas 5910
 
296 andreas 5911
            mLastObject = obj;
5912
            mAnimObjects.insert(pair<ulong, OBJECT_t *>(obj->handle, obj));
54 andreas 5913
            obj->animation->start();
5914
        break;
43 andreas 5915
 
54 andreas 5916
        case SE_SLIDE_RIGHT_FADE:
5917
        case SE_SLIDE_RIGHT:
5918
            obj->animation->setPropertyName("geometry");
43 andreas 5919
 
54 andreas 5920
            if (in)
5921
            {
5922
                obj->animation->setStartValue(QRect(scLeft + scWidth, scTop, scWidth, scHeight));
5923
                obj->animation->setEndValue(QRect(scLeft, scTop, scWidth, scHeight));
295 andreas 5924
                connect(obj->animation, &QPropertyAnimation::finished, this, &MainWindow::animationInFinished);
296 andreas 5925
                obj->object.widget->show();
54 andreas 5926
            }
5927
            else
5928
            {
5929
                obj->animation->setStartValue(QRect(scLeft, scTop, scWidth, scHeight));
5930
                obj->animation->setEndValue(QRect(scLeft + scWidth, scTop, scWidth, scHeight));
5931
                obj->remove = true;
5932
                connect(obj->animation, &QPropertyAnimation::finished, this, &MainWindow::animationFinished);
5933
            }
42 andreas 5934
 
296 andreas 5935
            mLastObject = obj;
5936
            mAnimObjects.insert(pair<ulong, OBJECT_t *>(obj->handle, obj));
54 andreas 5937
            obj->animation->start();
5938
        break;
43 andreas 5939
 
54 andreas 5940
        case SE_SLIDE_TOP_FADE:
5941
        case SE_SLIDE_TOP:
5942
            obj->animation->setPropertyName("geometry");
43 andreas 5943
 
54 andreas 5944
            if (in)
5945
            {
5946
                obj->animation->setStartValue(QRect(scLeft, scTop - scHeight, scWidth, scHeight));
5947
                obj->animation->setEndValue(QRect(scLeft, scTop, scWidth, scHeight));
295 andreas 5948
                connect(obj->animation, &QPropertyAnimation::finished, this, &MainWindow::animationInFinished);
296 andreas 5949
                obj->object.widget->show();
54 andreas 5950
            }
5951
            else
5952
            {
5953
                obj->animation->setStartValue(QRect(scLeft, scTop, scWidth, scHeight));
5954
                obj->animation->setEndValue(QRect(scLeft, scTop - scHeight, scWidth, scHeight));
5955
                obj->remove = true;
5956
                connect(obj->animation, &QPropertyAnimation::finished, this, &MainWindow::animationFinished);
5957
            }
43 andreas 5958
 
296 andreas 5959
            mLastObject = obj;
5960
            mAnimObjects.insert(pair<ulong, OBJECT_t *>(obj->handle, obj));
54 andreas 5961
            obj->animation->start();
5962
        break;
5963
 
58 andreas 5964
        case SE_FADE:
5965
            if (in)
5966
            {
289 andreas 5967
                if (obj->object.widget)
5968
                {
5969
                    obj->object.widget->setWindowOpacity(0.0);
5970
                    obj->object.widget->show();
5971
                }
5972
 
58 andreas 5973
                obj->animation->setStartValue(0.0);
5974
                obj->animation->setEndValue(1.0);
295 andreas 5975
                connect(obj->animation, &QPropertyAnimation::finished, this, &MainWindow::animationInFinished);
296 andreas 5976
                obj->object.widget->show();
58 andreas 5977
            }
5978
            else
5979
            {
5980
                obj->animation->setStartValue(1.0);
5981
                obj->animation->setEndValue(0.0);
5982
                obj->remove = true;
5983
                connect(obj->animation, &QPropertyAnimation::finished, this, &MainWindow::animationFinished);
5984
            }
5985
 
296 andreas 5986
            mLastObject = obj;
5987
            mAnimObjects.insert(pair<ulong, OBJECT_t *>(obj->handle, obj));
58 andreas 5988
            obj->animation->setEasingCurve(QEasingCurve::Linear);
5989
            obj->animation->start();
5990
        break;
5991
 
54 andreas 5992
        default:
5993
            MSG_WARNING("Subpage effect " << ani.showEffect << " is not supported.");
298 andreas 5994
            delete obj->animation;
5995
            obj->animation = nullptr;
5996
            return false;
42 andreas 5997
    }
298 andreas 5998
 
5999
    return true;
42 andreas 6000
}
6001
 
179 andreas 6002
void MainWindow::downloadBar(const string &msg, QWidget *parent)
6003
{
6004
    DECL_TRACER("void MainWindow::downloadBar(const string &msg, QWidget *parent)");
6005
 
6006
    if (mBusy)
6007
        return;
6008
 
6009
    mBusy = true;
6010
    mDownloadBar = new TqDownload(msg, parent);
6011
#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
6012
    mDownloadBar->setScaleFactor(gScale);
6013
    mDownloadBar->doResize();
6014
#endif
6015
    mDownloadBar->show();
6016
}
6017
 
120 andreas 6018
void MainWindow::runEvents()
6019
{
6020
    DECL_TRACER("MainWindow::runEvents()");
6021
 
6022
    QApplication::processEvents();
6023
}
6024
 
289 andreas 6025
void MainWindow::onSubViewItemClicked(ulong handle, bool pressed)
6026
{
6027
    DECL_TRACER("MainWindow::onSubViewItemClicked(ulong handle)");
6028
 
6029
    if (!handle)
6030
        return;
6031
 
6032
    // Create a thread and call the base program function.
6033
    // We create a thread to not interrupt the QT framework longer then
6034
    // necessary.
6035
    if (gPageManager)
299 andreas 6036
        gPageManager->mouseEvent(handle, pressed);
289 andreas 6037
}
6038
 
303 andreas 6039
void MainWindow::onInputChanged(ulong handle, string& text)
6040
{
6041
    DECL_TRACER("MainWindow::onInputChanged(ulong handle, string& text)");
6042
 
6043
    try
6044
    {
6045
        std::thread thr = std::thread([=] {
6046
            if (gPageManager)
6047
                gPageManager->inputButtonFinished(handle, text);
6048
        });
6049
 
6050
        thr.detach();
6051
    }
6052
    catch (std::exception& e)
6053
    {
6054
        MSG_ERROR("Error starting a thread to handle input line finish: " << e.what());
6055
    }
6056
}
6057
 
309 andreas 6058
void MainWindow::onFocusChanged(ulong handle, bool in)
6059
{
6060
    DECL_TRACER("MainWindow::onFocusChanged(ulong handle, bool in)");
6061
 
6062
    if (gPageManager)
6063
        gPageManager->inputFocusChanged(handle, in);
6064
}
6065
 
6066
void MainWindow::onCursorChanged(ulong handle, int oldPos, int newPos)
6067
{
6068
    DECL_TRACER("MainWindow::onCursorChanged(ulong handle, int oldPos, int newPos)");
6069
 
6070
    if (gPageManager)
6071
        gPageManager->inputCursorPositionChanged(handle, oldPos, newPos);
6072
}
6073
 
323 andreas 6074
void MainWindow::onGestureEvent(QObject *obj, QGestureEvent *event)
6075
{
6076
    DECL_TRACER("MainWindow::onGestureEvent(QObject *obj, QGestureEvent *event)");
6077
 
6078
    Q_UNUSED(obj);
6079
    gestureEvent(event);
6080
}
6081
 
285 andreas 6082
QPixmap MainWindow::scaleImage(QPixmap& pix)
6083
{
6084
    DECL_TRACER("MainWindow::scaleImage(QPixmap& pix)");
6085
 
6086
    int width = scale(pix.width());
6087
    int height = scale(pix.height());
6088
 
6089
    return pix.scaled(width, height);
6090
}
6091
 
6092
QPixmap MainWindow::scaleImage(unsigned char* buffer, int width, int height, int pixline)
6093
{
6094
    DECL_TRACER("MainWindow::scaleImage(unsigned char* buffer, int width, int height, int pixline)");
6095
 
300 andreas 6096
    if (!buffer || width < 1 || height < 1 || pixline < (width * 4))
6097
    {
6098
        MSG_ERROR("Invalid image for scaling!");
6099
        return QPixmap();
6100
    }
6101
 
285 andreas 6102
    QImage img(buffer, width, height, pixline, QImage::Format_ARGB32);  // Original size
6103
 
6104
    if (img.isNull() || !img.valid(width-1, height-1))
6105
    {
6106
        MSG_ERROR("Unable to create a valid image!");
6107
        return QPixmap();
6108
    }
6109
 
6110
    QSize size(scale(width), scale(height));
6111
    QPixmap pixmap;
6112
    bool ret = false;
6113
 
6114
    if (isScaled())
6115
        ret = pixmap.convertFromImage(img.scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); // Scaled size
6116
    else
6117
        ret = pixmap.convertFromImage(img);
6118
 
6119
    if (!ret || pixmap.isNull())
6120
    {
6121
        MSG_ERROR("Unable to create a pixmap out of an image!");
6122
    }
6123
 
6124
    return pixmap;
6125
}
6126
 
2 andreas 6127
#ifndef QT_NO_SESSIONMANAGER
6128
void MainWindow::commitData(QSessionManager &manager)
6129
{
5 andreas 6130
    if (manager.allowsInteraction())
6131
    {
6132
        if (!settingsChanged)
6133
            manager.cancel();
6134
    }
6135
    else
6136
    {
6137
        if (settingsChanged)
6138
            writeSettings();
6139
    }
2 andreas 6140
}
5 andreas 6141
#endif