Exemplo n.º 1
0
Application::Application(int &argc, char **argv) : QApplication(argc,argv)
{
    QTranslator *main_translator=nullptr, *plugin_translator=nullptr;
    QFile ui_style(GlobalAttributes::TMPL_CONFIGURATIONS_DIR +
                   GlobalAttributes::DIR_SEPARATOR +
                   GlobalAttributes::UI_STYLE_CONF +
                   GlobalAttributes::CONFIGURATION_EXT);
    QString plugin_name, plug_lang_dir, plug_lang_file;
    QStringList dir_list;
    QDir dir;

    //Creating the initial user's configuration
    createUserConfiguration();

    //Changing the current working dir to the executable's directory in
    QDir::setCurrent(this->applicationDirPath());

    //Adding paths which executable will find plugins and it's dependecies
    this->addLibraryPath(this->applicationDirPath());
    this->addLibraryPath(GlobalAttributes::PLUGINS_DIR);

    //Try to create plugins dir if it does not exists
    if(!dir.exists(GlobalAttributes::PLUGINS_DIR))
    {
        if(!dir.mkdir(GlobalAttributes::PLUGINS_DIR))
        {
            Messagebox msg;
            msg.show(Exception(Exception::getErrorMessage(ERR_FILE_DIR_NOT_WRITTEN).arg(GlobalAttributes::PLUGINS_DIR),
                               ERR_FILE_DIR_NOT_WRITTEN,__PRETTY_FUNCTION__,__FILE__,__LINE__));
        }
    }

    //Check if the temporary dir exists, if not, creates it.
    if(!dir.exists(GlobalAttributes::TEMPORARY_DIR))
    {
        if(!dir.mkdir(GlobalAttributes::TEMPORARY_DIR))
        {
            Messagebox msg;
            msg.show(Exception(Exception::getErrorMessage(ERR_FILE_DIR_NOT_WRITTEN).arg(GlobalAttributes::TEMPORARY_DIR),
                               ERR_FILE_DIR_NOT_WRITTEN, __PRETTY_FUNCTION__,__FILE__,__LINE__));
        }
    }

    //Tries to load the main ui translation according to the system's locale
    main_translator=new QTranslator;
    main_translator->load(QLocale::system().name(), GlobalAttributes::LANGUAGES_DIR);
    this->installTranslator(main_translator);

    //Trying to load plugins translations
    dir_list=QDir(GlobalAttributes::PLUGINS_DIR +
                  GlobalAttributes::DIR_SEPARATOR,
                  QString("*"), QDir::Name, QDir::AllDirs | QDir::NoDotAndDotDot).entryList();

    while(!dir_list.isEmpty())
    {
        plugin_name=dir_list.front();
        dir_list.pop_front();

        //Configure the path to "lang" subdir at current plugin directory
        plug_lang_dir=GlobalAttributes::PLUGINS_DIR +
                      GlobalAttributes::DIR_SEPARATOR + plugin_name +
                      GlobalAttributes::DIR_SEPARATOR + QString("lang") +
                      GlobalAttributes::DIR_SEPARATOR;

        plug_lang_file=plugin_name + QString(".") + QLocale::system().name();

        //Check if the .qm file exists for the current plugin. If so create and install a translator
        if(QFileInfo(plug_lang_dir + plug_lang_file + QString(".qm")).exists())
        {
            plugin_translator=new QTranslator;
            plugin_translator->load(plug_lang_file, plug_lang_dir);
            this->installTranslator(plugin_translator);
        }
    }

    //Loading app style sheet
    ui_style.open(QFile::ReadOnly);

    //Raises an error if ui style is not found
    if(!ui_style.isOpen())
    {
        Messagebox msg;
        msg.show(Exception(Exception::getErrorMessage(ERR_FILE_DIR_NOT_ACCESSED).arg(ui_style.fileName()),
                           ERR_FILE_DIR_NOT_ACCESSED,__PRETTY_FUNCTION__,__FILE__,__LINE__));
    }
    else
        this->setStyleSheet(ui_style.readAll());
}
Exemplo n.º 2
0
Application::Application(int &argc, char **argv) : QApplication(argc,argv)
{
	QTranslator *main_translator=nullptr, *plugin_translator=nullptr;
	QFile ui_style(GlobalAttributes::TMPL_CONFIGURATIONS_DIR +
				   GlobalAttributes::DIR_SEPARATOR +
				   GlobalAttributes::UI_STYLE_CONF +
				   GlobalAttributes::CONFIGURATION_EXT);
	QString plugin_name, plug_lang_dir, plug_lang_file;
	QStringList dir_list;
	QDir dir;

	//Creating the initial user's configuration
	createUserConfiguration();

	//Changing the current working dir to the executable's directory in
	QDir::setCurrent(this->applicationDirPath());

	//Adding paths which executable will find plugins and it's dependecies
	this->addLibraryPath(this->applicationDirPath());
	this->addLibraryPath(GlobalAttributes::PLUGINS_DIR);

	//Try to create plugins dir if it does not exists
	if(!dir.exists(GlobalAttributes::PLUGINS_DIR))
	{
		if(!dir.mkdir(GlobalAttributes::PLUGINS_DIR))
		{
			Messagebox msg;
			msg.show(Exception(Exception::getErrorMessage(ERR_FILE_DIR_NOT_WRITTEN).arg(GlobalAttributes::PLUGINS_DIR),
							   ERR_FILE_DIR_NOT_WRITTEN,__PRETTY_FUNCTION__,__FILE__,__LINE__));
		}
	}

	//Check if the temporary dir exists, if not, creates it.
	if(!dir.exists(GlobalAttributes::TEMPORARY_DIR))
	{
		if(!dir.mkdir(GlobalAttributes::TEMPORARY_DIR))
		{
			Messagebox msg;
			msg.show(Exception(Exception::getErrorMessage(ERR_FILE_DIR_NOT_WRITTEN).arg(GlobalAttributes::TEMPORARY_DIR),
							   ERR_FILE_DIR_NOT_WRITTEN, __PRETTY_FUNCTION__,__FILE__,__LINE__));
		}
	}

	//Trying to identify if the user defined a custom UI language in the pgmodeler.conf file
	QString conf_file =	GlobalAttributes::CONFIGURATIONS_DIR +
											GlobalAttributes::DIR_SEPARATOR +
											GlobalAttributes::GENERAL_CONF +
											GlobalAttributes::CONFIGURATION_EXT;
	QFile input;
	QString lang_id = QLocale::system().name();

	input.setFileName(conf_file);

	if(input.open(QFile::ReadOnly))
	{
		QString buf = QString(input.readAll());
		QRegExp regexp = QRegExp(QString("(%1)(.*)(=)(\\\")(.)+(\\\")(\\\n)").arg(ParsersAttributes::UI_LANGUAGE));
		int idx =	regexp.indexIn(QString(buf));

		//Extract the value of the ui-language attribute in the conf file
		lang_id = buf.mid(idx, regexp.matchedLength());
		lang_id.remove(ParsersAttributes::UI_LANGUAGE);
		lang_id.remove(QChar('"')).remove(QChar('=')).remove(QChar('\n'));
	}

	//Tries to load the main ui translation according to the system's locale
	main_translator=new QTranslator(this);
	main_translator->load(lang_id, GlobalAttributes::LANGUAGES_DIR);
	this->installTranslator(main_translator);

	//Trying to load plugins translations
	dir_list=QDir(GlobalAttributes::PLUGINS_DIR +
								GlobalAttributes::DIR_SEPARATOR,
								QString("*"), QDir::Name, QDir::AllDirs | QDir::NoDotAndDotDot).entryList();

	while(!dir_list.isEmpty())
	{
		plugin_name=dir_list.front();
		dir_list.pop_front();

		//Configure the path to "lang" subdir at current plugin directory
		plug_lang_dir=GlobalAttributes::PLUGINS_DIR +
					  GlobalAttributes::DIR_SEPARATOR + plugin_name +
					  GlobalAttributes::DIR_SEPARATOR + QString("lang") +
					  GlobalAttributes::DIR_SEPARATOR;

		plug_lang_file=plugin_name + QString(".") + lang_id;

		//Check if the .qm file exists for the current plugin. If so create and install a translator
		if(QFileInfo(plug_lang_dir + plug_lang_file + QString(".qm")).exists())
		{
			plugin_translator=new QTranslator(this);
			plugin_translator->load(plug_lang_file, plug_lang_dir);
			this->installTranslator(plugin_translator);
		}
	}

	//Loading app style sheet
	ui_style.open(QFile::ReadOnly);

	//Raises an error if ui style is not found
	if(!ui_style.isOpen())
	{
		Messagebox msg;
		msg.show(Exception(Exception::getErrorMessage(ERR_FILE_DIR_NOT_ACCESSED).arg(ui_style.fileName()),
						   ERR_FILE_DIR_NOT_ACCESSED,__PRETTY_FUNCTION__,__FILE__,__LINE__));
	}
	else
		this->setStyleSheet(ui_style.readAll());
}