Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

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