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