Skip to content
Snippets Groups Projects
main.cpp 9.73 KiB
Newer Older
  • Learn to ignore specific revisions
  • SeeLook's avatar
    SeeLook committed
    /***************************************************************************
    
     *   Copyright (C) 2011-2021 by Tomasz Bojczuk                             *
    
     *   seelook@gmail.com                                                     *
    
    SeeLook's avatar
    SeeLook committed
     *                                                                         *
     *   This program is free software; you can redistribute it and/or modify  *
     *   it under the terms of the GNU General Public License as published by  *
     *   the Free Software Foundation; either version 3 of the License, or     *
     *   (at your option) any later version.                                   *
     *                                                                         *
     *   This program is distributed in the hope that it will be useful,       *
     *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
     *   GNU General Public License for more details.                          *
     *                                                                         *
     *  You should have received a copy of the GNU General Public License      *
     *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
     ***************************************************************************/
    
    
    SeeLook's avatar
    SeeLook committed
    #include <tinitcorelib.h>
    
    #include <tglobals.h>
    
    #include <tpath.h>
    #include <tnootkaqml.h>
    
    #include "dialogs/tdialogloaderobject.h"
    
    #if defined (Q_OS_ANDROID)
      #include <Android/tandroid.h>
    
      #include "mobile/tmobilemenu.h"
    
    #else
      #include "nootini/taudioanalyzeitem.h"
    
    #include <QtWidgets/qapplication.h>
    #include <QtGui/qicon.h>
    
    #include <QtGui/qpalette.h>
    
    #include <QtQml/qqmlapplicationengine.h>
    #include <QtQml/qqmlcontext.h>
    #include <QtCore/qtranslator.h>
    #include <QtCore/qdatetime.h>
    #include <QtCore/qpointer.h>
    #include <QtCore/qfile.h>
    #include <QtCore/qsettings.h>
    
    SeeLook's avatar
    SeeLook committed
    #include <QtCore/qelapsedtimer.h>
    
    #include <QtCore/qloggingcategory.h>
    
    #include <QtCore/qdebug.h>
    
    
    static QString logFile;
    
    SeeLook's avatar
    SeeLook committed
    
    
    /**
     * It allows to grab all debug messages into nootka-log.txt file
     */
    
    void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
    
      Q_UNUSED(type)
    
    #if defined (Q_OS_ANDROID)
    
        QFile outFile(logFile);
    
    #else
        QFile outFile(QStringLiteral("nootka-log.txt"));
    #endif
    
        outFile.open(QIODevice::WriteOnly | QIODevice::Append);
        QTextStream ts(&outFile);
    
        ts << msg << "\n";
    
    SeeLook's avatar
    SeeLook committed
    int main(int argc, char *argv[])
    
    #if defined (Q_OS_WIN)
      // It works under Win
      QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    #endif
    
      // It mutes QML warnings about connections syntax introduced in Qt 5.15
      // TODO when Qt version requirements will rise to 5.15 or above, change syntax and remove that
      QLoggingCategory::setFilterRules(QStringLiteral("qt.qml.connections=false"));
    
    
    #if defined (Q_OS_ANDROID)
    
      qputenv("QT_ANDROID_VOLUME_KEYS", "1"); // Handle volume keys by Qt, lock native Android behavior
    
    
      // log to any writable storage
    
     logFile = Tandroid::getExternalPath() + QStringLiteral("/nootka-log.txt");
    
      if (QFile::exists(logFile))
        QFile::remove(logFile);
    
    SeeLook's avatar
    SeeLook committed
      qInstallMessageHandler(myMessageOutput);
    
      qDebug() << "==== NOOTKA LOG =======\n" << QDateTime::currentDateTime().toString();
    
    #else
      qputenv("QT_QUICK_CONTROLS_STYLE", ""); // reset style environment var - other styles can cause crashes
    
    SeeLook's avatar
    SeeLook committed
      QElapsedTimer startElapsed;
      startElapsed.start();
    
    
      QTranslator qtTranslator;
      QTranslator nooTranslator;
    
      QQmlApplicationEngine *e = nullptr;
    
      auto nooObj = new TnootkaQML();
    
      QString confFile;
      do {
    
        if (nooObj->resetConfig()) { // delete config file - new Nootka instance will start with first run wizard
    
        nooObj->setResetConfig(false);
    
        gl = new Tglobals();
        gl->path = Tglobals::getInstPath(qApp->applicationDirPath());
        confFile = gl->config->fileName();
        if (!initCoreLibrary())
          return 110;
        prepareTranslations(a, qtTranslator, nooTranslator);
        if (!loadNootkaFont(a))
          return 111;
    
    
        f.setPixelSize(qRound(static_cast<qreal>(f.pixelSize()) * gl->guiScale()));
    
        auto pal = qApp->palette();
    
    SeeLook's avatar
    SeeLook committed
        pal.setColor(QPalette::Active, QPalette::Highlight, QColor(0, 160, 160)); // Teal color of highlight for Android
    
        pal.setColor(QPalette::Active, QPalette::Shadow, QColor(120, 120, 120)); // Dark gray for shadow
    
        pal.setColor(QPalette::Active, QPalette::Button, QColor(240, 240, 240)); // Very light gray for button
        pal.setColor(QPalette::Active, QPalette::AlternateBase, QColor(245, 255, 255)); // Very light teal for alternate base
    
        qApp->setPalette(pal);
    
    #elif defined (Q_OS_WIN)
        QSettings accent(QStringLiteral("HKEY_USERS\\.DEFAULT\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Accent"),
                         QSettings::NativeFormat);
        if (accent.contains(QLatin1String("StartColorMenu"))) {
          int color = accent.value(QLatin1String("StartColorMenu")).toInt();
          int r = color & 0xff;
          int g = (color >> 8) & 0xff;
          int b = (color >> 16) & 0xff;
          auto pal = qApp->palette();
          pal.setColor(QPalette::Active, QPalette::Highlight, QColor(r, g, b, 127)); // alpha 127
          qApp->setPalette(pal);
        }
        f.setPointSizeF(f.pointSizeF() * gl->guiScale());
    
    #else
        f.setPointSizeF(f.pointSizeF() * gl->guiScale());
    
        a->setWindowIcon(QIcon(Tpath::img("nootka")));
    
        auto sound = new Tsound();
    
        e->rootContext()->setContextProperty(QStringLiteral("GLOB"), gl);
    
        e->rootContext()->setContextProperty(QStringLiteral("Noo"), nooObj);
        e->rootContext()->setContextProperty(QStringLiteral("SOUND"), sound);
    
        TmainHelp* hlp = nullptr; // keep help object live after wizard, Qt deletes it with some delay
    
          hlp = new TmainHelp();
          e->rootContext()->setContextProperty(QStringLiteral("HELP"), hlp);
    
          nooObj->setQmlEngine(e);
    
          e->load(QUrl(QStringLiteral("qrc:/wizard/Wizard.qml")));
    
    #if defined (Q_OS_ANDROID)
            qDebug() << "Nootka wizard launch time" << startElapsed.nsecsElapsed() / 1000000.0 << " [ms]";
    #else
    
            QTextStream o(stdout);
            o << "\033[01;35m Nootka wizard launch time: " << startElapsed.nsecsElapsed() / 1000000.0 << " [ms]\033[01;00m\n";
    
          e->deleteLater(); // Android crashes without a delayed destroy
          qApp->quit();
    
          e->rootContext()->setContextProperty(QStringLiteral("GLOB"), gl);
    
          e->rootContext()->setContextProperty(QStringLiteral("Noo"), nooObj);
          e->rootContext()->setContextProperty(QStringLiteral("SOUND"), sound);
    
          gl->config->setValue(QLatin1String("version"), gl->version);
          // TODO: storing current version in settings avoids opening 'about' window next launch
          //       but if there will be another window - delete line above
    
        nooObj->setQmlEngine(e);
    
        qmlRegisterType<TnameItem>("Nootka.Main", 1, 0, "TnameItem");
    
        qmlRegisterType<TmainScoreObject>("Nootka.Main", 1, 0, "TmainScoreObject");
    
        qmlRegisterType<TdialogLoaderObject>("Nootka.Dialogs", 1, 0, "TdialogObject");
    
    #if defined (Q_OS_ANDROID)
        qmlRegisterType<TmobileMenu>("Nootka", 1, 0, "TmobileMenu");
    #endif
    
    
        e->load(QUrl(QStringLiteral("qrc:/MainWindow.qml")));
    
    SeeLook's avatar
    SeeLook committed
    
    
         QString androidArg = Tandroid::getRunArgument();
         if (!androidArg.isEmpty())
    
           nooObj->openFile(androidArg);
    
          if (argc > 1) {
            if (QString::fromLocal8Bit(argv[argc - 1]).contains(QLatin1String("--nootini")) && !e->rootObjects().isEmpty()) {
                qmlRegisterType<TaudioAnalyzeItem>("Nootka.Main", 1, 0, "TaudioAnalyzeItem");
                QMetaObject::invokeMethod(e->rootObjects().first(), "audioAnalyze");
            } else
                nooObj->openFile(QString::fromLocal8Bit(argv[argc - 1]));
          }
    
    #if defined (Q_OS_ANDROID)
          qDebug() << "NOOTKA LAUNCH TIME" << startElapsed.nsecsElapsed() / 1000000.0 << " [ms]";
    #else
    
    SeeLook's avatar
    SeeLook committed
          QTextStream o(stdout);
    
          o << "\033[01;35m Nootka launch time: " << startElapsed.nsecsElapsed() / 1000000.0 << " [ms]\033[01;00m\n";
    
    SeeLook's avatar
    SeeLook committed
        }
    
        sound->init();
    
        if (firstLoop && !wasFirstRun) {
          // show some dialog when version was changed (news or other info)
          if (!TdialogLoaderObject::checkVersion(e->rootObjects().first())) {
            // or check updates if no version changed
            if (gl->config->value(QLatin1String("Updates/enableUpdates"), true).toBool())
              TdialogLoaderObject::updateCheckInBackground();
          }
        }
    
        exitCode = a->exec();
    
        if (hlp)
          delete hlp;
    
        delete sound;
    
    #if defined (Q_OS_ANDROID)
    
        if (nooObj->resetConfig()) { // delete config file - new Nootka instance will start with first run wizard
    
            Tandroid::restartNootka(); // and call Nootka after delay
    
        nooObj->setResetConfig(false); // do - while loop doesn't work with Android
    
    SeeLook's avatar
    SeeLook committed
        qApp->quit(); // HACK: calling QApplication::quick() solves hang on x86 when Qt uses native (usually obsolete) SSL libraries
    
      } while (nooObj->resetConfig());
    
      delete nooObj;
      qInstallMessageHandler(0);
    
      delete a;
    
      return exitCode;
    
    SeeLook's avatar
    SeeLook committed
    }