⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cc

📁 c++的guiQt做的开发
💻 CC
字号:
/** @file Main function Handle commandline options and then open window(s) - unless commandline parameters specify something different @author Martin Petricek*/#include "main.h"#include "mainwindow.h"#include "version.h"#include "config.h"#include "lookandfeel.h"#include <args.h>#include <lang.h>#include <util.h>#include <settings.h>#include <iostream>#include <QMetaType>#include <QApplication>#include <QDir>#include <QTranslator>#include <QSet>#include <QStringList>#include <stdlib.h>using namespace std;using namespace gui;using namespace util;/** handle --help parameter<br> Prtin help to STDOUT and exit*/void handleHelp(){ printHeader(); cout << (QObject::tr("Usage:")+ " " + binName + " [" + QObject::tr("option(s)") + "] [" + QObject::tr("files(s)") + "]").toAscii().data() << endl; handleHelpOptions();}/** handle --version parameter<br> Print version to STDOUT and exit */void handleVersion(){ cout << VERSION << endl; exit(0);}/** Try to load and install translation for given language @param tr_lang Code of language to load @return true if successful, false if unsuccessful */bool loadTranslation(const char *tr_lang) { static QTranslator *translator=NULL; if (translator) {  qApp->removeTranslator(translator);  delete translator;  translator=NULL; } QString lang=QString("mpv_")+tr_lang; translator=new QTranslator(); //Look for translation file in config directory in $HOME if (!translator->load(lang,QDir::home().path()+"/"+CONFIG_DIR+"/lang")) {#ifndef WINDOWS_SYSTEM  //look for translation file in DATA_PATH (unix-like systems only)  if (!translator->load(lang,QString(DATA_PATH)+"/lang")) {#endif   //Look in binary path - testing compilations and windows builds   if (!translator->load(lang,Settings::appPath()+"/lang")) {    warn("Translation file " + lang + " not found");    delete translator;    translator=NULL;    return false;   }#ifndef WINDOWS_SYSTEM  }#endif } qApp->installTranslator(translator); return true;}/** Return list of available translations found in system*/QStringList listTranslations() { //Look for translation file in config directory in $HOME QStringList res=listTranslationsInDir(QDir::home().path()+"/"+CONFIG_DIR+"/lang","mpv_");#ifndef WINDOWS_SYSTEM //look for translation file in DATA_PATH (unix-like systems only) res+=listTranslationsInDir(QString(DATA_PATH)+"/lang","mpv_");#endif   //Look in binary path - testing compilations and windows builds res+=listTranslationsInDir(Settings::appPath()+"/lang","mpv_"); //Weed out duplicates  QSet<QString> set=res.toSet(); res=set.toList(); //Sort res.sort(); return res;}/** Load new translation according to environment variables*/void loadLanguageEnv() { //Try LC_ALL, LC_MESSAGES and LANG - first one that is set is used const char *env_lang=getenv("LC_ALL"); if (!env_lang) env_lang=getenv("LC_MESSAGES"); if (!env_lang) env_lang=getenv("LANG"); if (env_lang) {//LC_ALL/LC_MESSAGES/LANG variable is present in environment -> attempt to load localization  loadTranslation(env_lang); }}/** Load new translation @param language new language. Empty string or NULL string will result in querying the environment*/void loadLanguage(const QString &language) { if (language.isNull() || language=="") {  loadLanguageEnv(); } loadTranslation(language.toUtf8());}/** Initialize system to manage settings*/void loadSettingSystem(){ //load settings Settings::setAppKey("MPView");#ifdef WINDOWS_SYSTEM Settings::setPaths(Settings::appPath(),CONFIG_DIR,CONFIG_FILE);#else Settings::setPaths(DATA_PATH,CONFIG_DIR,CONFIG_FILE);#endif Settings::getInstance(); Settings::installExitHandler();}/** Install callback functions used to process commandline options @param progname Name of this binary (usually argv[0])*/void installOptionHandlers(const char *progname) { /*  Whole name of one parameter should not be prefix of another parameter, as unpredictable behaviour can occur,  for example -d and -def, in this case there is no way to recognize between -def option and -d option  with 'ef' as parameter and it is undefined what case of these two will be recognized */ setHeaderLine(APP_NAME " " VERSION); setBadParamError(QObject::tr("Use '%1 --help' to see list of commandline options and their parameters").arg(progname)); optionHandler("--help",handleHelp,QObject::tr("Print help and exit")); optionHandler("--version",handleVersion,QObject::tr("Print version and exit")); optionHandler("--",handleStopOpt,QObject::tr("Stop processing options"));}/** Main - load settings, load translation and launch main window(s) according to settings and commandline parameters @param argc Argument count @param argv Commandline arguments @return Executable return code*/int main(int argc, char *argv[]){ QApplication app(argc,argv); //Load translation from environment loadLanguageEnv(); loadSettingSystem(); // Load different translation if overridden in settings QString overrideLang=globalSettings->read("gui/language",""); if (overrideLang.length()) {  loadLanguage(overrideLang); } //parse commandline parameters installOptionHandlers(app.argv()[0]); QStringList params=handleParams(app.argc(),app.argv()); //style QString style=globalSettings->read("gui/style",""); if (style!="") {  if (!app.setStyle(style)) globalSettings->write("gui/style",""); //No such style -> reset } //font applyLookAndFeel(); //open editor windows(s) int nFiles=params.size(); if (nFiles) { //open files from cmdline  for (QStringList::Iterator it=params.begin();it!=params.end();++it) {   MainWindow::create(*it);  } } else { //no parameters  MainWindow::create(); } QObject::connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit())); return app.exec();}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -