QTfrontend/main.cpp
changeset 14318 d688455cad8f
parent 14279 20b08e59730d
child 14321 0752148afc65
equal deleted inserted replaced
14317:15f40350ede9 14318:d688455cad8f
   133 #endif
   133 #endif
   134     if (engine != NULL)
   134     if (engine != NULL)
   135     {
   135     {
   136         delete engine;
   136         delete engine;
   137         engine = NULL;
   137         engine = NULL;
       
   138     }
       
   139 }
       
   140 
       
   141 // Simple Message handler that suppresses Qt debug and info messages (qDebug, qInfo).
       
   142 // Used when printing command line help (--help) or related error to keep console clean.
       
   143 void restrictedMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
       
   144 {
       
   145     Q_UNUSED(context)
       
   146     QByteArray localMsg = msg.toLocal8Bit();
       
   147     switch (type) {
       
   148     case QtWarningMsg:
       
   149     case QtCriticalMsg:
       
   150     case QtFatalMsg:
       
   151         fprintf(stderr, "%s\n", localMsg.constData());
       
   152         break;
       
   153     default:
       
   154         break;
   138     }
   155     }
   139 }
   156 }
   140 
   157 
   141 QString getUsage()
   158 QString getUsage()
   142 {
   159 {
   181     app.setAttribute(Qt::AA_DontShowIconsInMenus,false);
   198     app.setAttribute(Qt::AA_DontShowIconsInMenus,false);
   182 
   199 
   183     // file engine, to be initialized later
   200     // file engine, to be initialized later
   184     engine = NULL;
   201     engine = NULL;
   185 
   202 
       
   203     /*
       
   204     This is for messages frelated to translatable command-line arguments.
       
   205     If it is non-zero, will print out a message after loading locale
       
   206     and exit.
       
   207     */
       
   208     enum cmdMsgStateEnum {
       
   209         cmdMsgNone,
       
   210         cmdMsgHelp,
       
   211         cmdMsgMalformedArg,
       
   212         cmdMsgUnknownArg,
       
   213     };
       
   214     enum cmdMsgStateEnum cmdMsgState = cmdMsgNone;
       
   215     QString cmdMsgStateStr;
       
   216 
   186     // parse arguments
   217     // parse arguments
   187 
       
   188     QStringList arguments = app.arguments();
   218     QStringList arguments = app.arguments();
   189     QMap<QString, QString> parsedArgs;
   219     QMap<QString, QString> parsedArgs;
   190     {
   220     {
   191         QList<QString>::iterator i = arguments.begin();
   221         QList<QString>::iterator i = arguments.begin();
   192         while(i != arguments.end())
   222         while(i != arguments.end())
   203             else
   233             else
   204             {
   234             {
   205                 if(arg.startsWith("--")) {
   235                 if(arg.startsWith("--")) {
   206                     if(arg == "--help")
   236                     if(arg == "--help")
   207                     {
   237                     {
   208                         printf("%s", getUsage().toUtf8().constData());
   238                         cmdMsgState = cmdMsgHelp;
   209                         return 0;
   239                         qInstallMessageHandler(restrictedMessageHandler);
   210                     }
   240                     }
   211                     // argument is something wrong
   241                     else
   212                     fprintf(stderr, "%s\n\n%s",
   242                     {
   213                         HWApplication::tr("Malformed option argument: %1", "command-line").arg(arg).toUtf8().constData(),
   243                         // argument is something wrong
   214                         getUsage().toUtf8().constData());
   244                         cmdMsgState = cmdMsgMalformedArg;
   215                     return 1;
   245                         cmdMsgStateStr = arg;
       
   246                         qInstallMessageHandler(restrictedMessageHandler);
       
   247                         break;
       
   248                     }
   216                 }
   249                 }
   217 
   250 
   218                 // if not starting with --, then always skip
   251                 // if not starting with --, then always skip
   219                 // (because we can't determine if executable path/call or not - on windows)
   252                 // (because we can't determine if executable path/call or not - on windows)
   220                 ++i;
   253                 ++i;
   221             }
   254             }
   222         }
   255         }
   223     }
   256     }
   224 
   257 
   225     if(parsedArgs.contains("data-dir"))
   258     if(cmdMsgState == 0)
   226     {
   259     {
   227         QFileInfo f(parsedArgs["data-dir"]);
   260         if(parsedArgs.contains("data-dir"))
   228         parsedArgs.remove("data-dir");
   261         {
   229         if(!f.exists())
   262             QFileInfo f(parsedArgs["data-dir"]);
   230         {
   263             parsedArgs.remove("data-dir");
   231             qWarning() << "WARNING: Cannot open DATA_PATH=" << f.absoluteFilePath();
   264             if(!f.exists())
   232         }
   265             {
   233         *cDataDir = f.absoluteFilePath();
   266                 qWarning() << "WARNING: Cannot open data-dir=" << f.absoluteFilePath();
   234         custom_data = true;
   267             }
   235     }
   268             *cDataDir = f.absoluteFilePath();
   236 
   269             custom_data = true;
   237     if(parsedArgs.contains("config-dir"))
   270         }
   238     {
   271 
   239         QFileInfo f(parsedArgs["config-dir"]);
   272         if(parsedArgs.contains("config-dir"))
   240         parsedArgs.remove("config-dir");
   273         {
   241         cfgdir->setPath(f.absoluteFilePath());
   274             QFileInfo f(parsedArgs["config-dir"]);
   242         custom_config = true;
   275             parsedArgs.remove("config-dir");
   243     }
   276             cfgdir->setPath(f.absoluteFilePath());
   244     else
   277             custom_config = true;
   245     {
   278         }
   246         cfgdir->setPath(QDir::homePath());
   279         else
   247         custom_config = false;
   280         {
   248     }
   281             cfgdir->setPath(QDir::homePath());
   249 
   282             custom_config = false;
   250     if (!parsedArgs.isEmpty()) {
   283         }
   251         foreach (const QString & key, parsedArgs.keys())
   284 
   252         {
   285         if (!parsedArgs.isEmpty())
   253             fprintf(stderr, "%s\n", HWApplication::tr("Unknown option argument: %1", "command-line").arg(QString("--") + key).toUtf8().constData());
   286         {
   254         }
   287             cmdMsgState = cmdMsgUnknownArg;
   255         fprintf(stderr, "\n%s", getUsage().toUtf8().constData());
   288             qInstallMessageHandler(restrictedMessageHandler);
   256         return 1;
   289         }
   257     }
   290 
   258 
   291         // end of parameter parsing
   259     // end of parameter parsing
   292 
   260 
   293         // Select Qt style
   261     // Select Qt style
   294         /* Qt5 Base removed Motif, Plastique. These are now in the Qt style plugins
   262     /* Qt5 Base removed Motif, Plastique. These are now in the Qt style plugins
   295         (Ubuntu: qt5-style-plugins, which was NOT backported by Debian/Ubuntu to stable/LTS).
   263     (Ubuntu: qt5-style-plugins, which was NOT backported by Debian/Ubuntu to stable/LTS).
   296         Windows appears to render best of the remaining options but still isn't quite right. */
   264     Windows appears to render best of the remaining options but still isn't quite right. */
   297 
   265 
   298         // Try setting Plastique if available
   266     // Try setting Plastique if available
   299         QStyle* coreStyle;
   267     QStyle* coreStyle;
   300         coreStyle = QStyleFactory::create("Plastique");
   268     coreStyle = QStyleFactory::create("Plastique");
       
   269     if(coreStyle != 0) {
       
   270         QApplication::setStyle(coreStyle);
       
   271         qDebug("Qt style set: Plastique");
       
   272     } else {
       
   273         // Use Windows as fallback.
       
   274         // FIXME: Under Windows style, some widgets like scrollbars don't render as nicely
       
   275         coreStyle = QStyleFactory::create("Windows");
       
   276         if(coreStyle != 0) {
   301         if(coreStyle != 0) {
   277             QApplication::setStyle(coreStyle);
   302             QApplication::setStyle(coreStyle);
   278             qDebug("Qt style set: Windows");
   303             qDebug("Qt style set: Plastique");
   279         } else {
   304         } else {
   280             // Windows style should not be missing in Qt5 Base. If it does, something went terribly wrong!
   305             // Use Windows as fallback.
   281             qWarning("No Qt style could be set! Using the default one.");
   306             // FIXME: Under Windows style, some widgets like scrollbars don't render as nicely
       
   307             coreStyle = QStyleFactory::create("Windows");
       
   308             if(coreStyle != 0) {
       
   309                 QApplication::setStyle(coreStyle);
       
   310                 qDebug("Qt style set: Windows");
       
   311             } else {
       
   312                 // Windows style should not be missing in Qt5 Base. If it does, something went terribly wrong!
       
   313                 qWarning("No Qt style could be set! Using the default one.");
       
   314             }
   282         }
   315         }
   283     }
   316     }
   284 
   317 
   285 #ifdef Q_OS_WIN
   318 #ifdef Q_OS_WIN
       
   319     // Splash screen for Windows
   286     QPixmap pixmap(":/res/splash.png");
   320     QPixmap pixmap(":/res/splash.png");
   287     QSplashScreen splash(pixmap);
   321     QSplashScreen splash(pixmap);
   288     splash.show();
   322     if(cmdMsgState == cmdMsgNone)
       
   323     {
       
   324         splash.show();
       
   325     }
   289 #endif
   326 #endif
   290 
   327 
   291     QDateTime now = QDateTime::currentDateTime();
   328     QDateTime now = QDateTime::currentDateTime();
   292     srand(now.toTime_t());
   329     srand(now.toTime_t());
   293     rand();
   330     rand();
   360     QTranslator TranslatorQt;
   397     QTranslator TranslatorQt;
   361     {
   398     {
   362         QSettings settings(DataManager::instance().settingsFileName(), QSettings::IniFormat);
   399         QSettings settings(DataManager::instance().settingsFileName(), QSettings::IniFormat);
   363         settings.setIniCodec("UTF-8");
   400         settings.setIniCodec("UTF-8");
   364 
   401 
   365         // Heuristic to figure out if the user is (probably) a first-time player.
       
   366         // If nickname is not set, then probably yes.
       
   367         // The hidden setting firstLaunch is, if present, used to force HW to
       
   368         // treat iself as if it were launched the first time.
       
   369         QString nick = settings.value("net/nick", QString()).toString();
       
   370         if (settings.contains("frontend/firstLaunch"))
       
   371         {
       
   372             isProbablyNewPlayer = settings.value("frontend/firstLaunch").toBool();
       
   373         }
       
   374         else
       
   375         {
       
   376             isProbablyNewPlayer = nick.isNull();
       
   377         }
       
   378 
       
   379         // Set firstLaunch to false to make sure we remember we have been launched before.
       
   380         settings.setValue("frontend/firstLaunch", false);
       
   381 
       
   382         QString cc = settings.value("misc/locale", QString()).toString();
   402         QString cc = settings.value("misc/locale", QString()).toString();
   383         if (cc.isEmpty())
   403         if (cc.isEmpty())
   384         {
   404         {
   385             cc = QLocale::system().name();
   405             cc = QLocale::system().name();
   386             qDebug("Detected system locale: %s", qPrintable(cc));
   406             qDebug("Detected system locale: %s", qPrintable(cc));
   406                 qWarning("Failed to install Qt translation (%s)", qPrintable(defaultLocaleName));
   426                 qWarning("Failed to install Qt translation (%s)", qPrintable(defaultLocaleName));
   407             app.installTranslator(&TranslatorHedgewars);
   427             app.installTranslator(&TranslatorHedgewars);
   408             app.installTranslator(&TranslatorQt);
   428             app.installTranslator(&TranslatorQt);
   409         }
   429         }
   410         app.setLayoutDirection(QLocale().textDirection());
   430         app.setLayoutDirection(QLocale().textDirection());
       
   431 
       
   432         // Handle command line messages
       
   433         switch(cmdMsgState)
       
   434         {
       
   435             case cmdMsgHelp:
       
   436             {
       
   437                 printf("%s", getUsage().toUtf8().constData());
       
   438                 return 0;
       
   439             }
       
   440             case cmdMsgMalformedArg:
       
   441             {
       
   442                 fprintf(stderr, "%s\n\n%s",
       
   443                     HWApplication::tr("Malformed option argument: %1", "command-line").arg(cmdMsgStateStr).toUtf8().constData(),
       
   444                     getUsage().toUtf8().constData());
       
   445                 return 1;
       
   446             }
       
   447             case cmdMsgUnknownArg:
       
   448             {
       
   449                 foreach (const QString & key, parsedArgs.keys())
       
   450                 {
       
   451                     fprintf(stderr, "%s\n",
       
   452                         HWApplication::tr("Unknown option argument: %1", "command-line").arg(QString("--") + key).toUtf8().constData());
       
   453                 }
       
   454                 fprintf(stderr, "\n%s", getUsage().toUtf8().constData());
       
   455                 return 1;
       
   456             }
       
   457             default:
       
   458             {
       
   459                 break;
       
   460             }
       
   461         }
       
   462 
       
   463         // Heuristic to figure out if the user is (probably) a first-time player.
       
   464         // If nickname is not set, then probably yes.
       
   465         // The hidden setting firstLaunch is, if present, used to force HW to
       
   466         // treat iself as if it were launched the first time.
       
   467         QString nick = settings.value("net/nick", QString()).toString();
       
   468         if (settings.contains("frontend/firstLaunch"))
       
   469         {
       
   470             isProbablyNewPlayer = settings.value("frontend/firstLaunch").toBool();
       
   471         }
       
   472         else
       
   473         {
       
   474             isProbablyNewPlayer = nick.isNull();
       
   475         }
       
   476 
       
   477         // Set firstLaunch to false to make sure we remember we have been launched before.
       
   478         settings.setValue("frontend/firstLaunch", false);
   411     }
   479     }
   412 
   480 
   413 #ifdef _WIN32
   481 #ifdef _WIN32
   414     // Win32 registry setup (used for external software detection etc.
   482     // Win32 registry setup (used for external software detection etc.
   415     // don't set it if running in "portable" mode with a custom config dir)
   483     // don't set it if running in "portable" mode with a custom config dir)
   462 
   530 
   463     qWarning("Starting Hedgewars %s-r%d (%s)", qPrintable(*cVersionString), cRevisionString->toInt(), qPrintable(*cHashString));
   531     qWarning("Starting Hedgewars %s-r%d (%s)", qPrintable(*cVersionString), cRevisionString->toInt(), qPrintable(*cHashString));
   464 
   532 
   465     app.form = new HWForm(NULL, style);
   533     app.form = new HWForm(NULL, style);
   466 #ifdef Q_OS_WIN
   534 #ifdef Q_OS_WIN
   467     splash.finish(app.form);
   535     if(cmdMsgState == 0)
       
   536         splash.finish(app.form);
   468 #endif
   537 #endif
   469     app.form->show();
   538     app.form->show();
   470 
   539 
   471     // Show welcome message for (suspected) first-time player and
   540     // Show welcome message for (suspected) first-time player and
   472     // point towards the Training menu.
   541     // point towards the Training menu.