mirror of
https://github.com/arcan1s/qtadds.git
synced 2025-04-24 15:27:20 +00:00
init
This commit is contained in:
parent
ae18b94456
commit
36c4ded89a
@ -2,3 +2,10 @@ qtadds
|
||||
======
|
||||
|
||||
Some additional Qt/C++ headers
|
||||
|
||||
|
||||
Description
|
||||
-----------
|
||||
|
||||
* `language` - class which can be used to define application language using command line options, configuration file or system locale
|
||||
* `pdebug` - inline which returns `[CLASS::Method]`
|
||||
|
115
language/language.cpp
Normal file
115
language/language.cpp
Normal file
@ -0,0 +1,115 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2014 Evgeniy Alekseev *
|
||||
* *
|
||||
* This library is free software; you can redistribute it and/or *
|
||||
* modify it under the terms of the GNU Lesser General Public *
|
||||
* License as published by the Free Software Foundation; either *
|
||||
* version 3.0 of the License, or (at your option) any later version. *
|
||||
* *
|
||||
* This library 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 *
|
||||
* Lesser General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU Lesser General Public *
|
||||
* License along with this library. *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#include "language.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QLocale>
|
||||
|
||||
|
||||
Language::Language()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
QString Language::checkLanguage(const QString language, const QString defaultLanguage)
|
||||
{
|
||||
QStringList availableLanguages = getAvailableLanguages();
|
||||
for (int i=0; i<availableLanguages.count(); i++)
|
||||
if (language == availableLanguages[i])
|
||||
return availableLanguages[i];
|
||||
for (int i=0; i<availableLanguages.count(); i++)
|
||||
if (language.contains(availableLanguages[i] + QChar('_')))
|
||||
return availableLanguages[i];
|
||||
|
||||
return defaultLanguage;
|
||||
}
|
||||
|
||||
|
||||
QString Language::defineLanguage(const QString configPath, const QString options)
|
||||
{
|
||||
QMap<QString, QString> optionsDict = parseOptions(options);
|
||||
if (optionsDict.contains(QString("LANGUAGE")))
|
||||
if (getAvailableLanguages().contains(optionsDict[QString("LANGUAGE")]))
|
||||
return optionsDict[QString("LANGUAGE")];
|
||||
|
||||
QString language;
|
||||
language = defineLanguageFromFile(configPath);
|
||||
if (language.isEmpty())
|
||||
language = defineLanguageFromLocale();
|
||||
language = checkLanguage(language, QString("en"));
|
||||
|
||||
return language;
|
||||
}
|
||||
|
||||
|
||||
QString Language::defineLanguageFromFile(const QString configPath)
|
||||
{
|
||||
QMap<QString, QString> settings;
|
||||
if (configPath.isEmpty())
|
||||
return QString("");
|
||||
QFile configFile(configPath);
|
||||
QString fileStr;
|
||||
if (!configFile.open(QIODevice::ReadOnly))
|
||||
return QString("");
|
||||
while (true) {
|
||||
fileStr = QString(configFile.readLine()).trimmed();
|
||||
if ((fileStr.isEmpty()) && (!configFile.atEnd())) continue;
|
||||
if ((fileStr[0] == QChar('#')) && (!configFile.atEnd())) continue;
|
||||
if ((fileStr[0] == QChar(';')) && (!configFile.atEnd())) continue;
|
||||
if (fileStr.contains(QChar('=')))
|
||||
settings[fileStr.split(QChar('='))[0]] = fileStr.split(QChar('='))[1];
|
||||
if (configFile.atEnd()) break;
|
||||
}
|
||||
configFile.close();
|
||||
|
||||
if (settings.contains(QString("LANGUAGE")))
|
||||
return settings[QString("LANGUAGE")];
|
||||
else
|
||||
return QString("");
|
||||
}
|
||||
|
||||
|
||||
QString Language::defineLanguageFromLocale()
|
||||
{
|
||||
return QLocale::system().name();
|
||||
}
|
||||
|
||||
|
||||
QStringList Language::getAvailableLanguages()
|
||||
{
|
||||
QStringList languages;
|
||||
languages.append(QString("en"));
|
||||
// put your languages here
|
||||
|
||||
return languages;
|
||||
}
|
||||
|
||||
|
||||
QMap<QString, QString> Language::parseOptions(const QString options)
|
||||
{
|
||||
QMap<QString, QString> optionsDict;
|
||||
for (int i=0; i<options.split(QChar(',')).count(); i++) {
|
||||
if (options.split(QChar(','))[i].split(QChar('=')).count() < 2)
|
||||
continue;
|
||||
optionsDict[options.split(QChar(','))[i].split(QChar('='))[0]] =
|
||||
options.split(QChar(','))[i].split(QChar('='))[1];
|
||||
}
|
||||
|
||||
return optionsDict;
|
||||
}
|
43
language/language.h
Normal file
43
language/language.h
Normal file
@ -0,0 +1,43 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2014 Evgeniy Alekseev *
|
||||
* *
|
||||
* This library is free software; you can redistribute it and/or *
|
||||
* modify it under the terms of the GNU Lesser General Public *
|
||||
* License as published by the Free Software Foundation; either *
|
||||
* version 3.0 of the License, or (at your option) any later version. *
|
||||
* *
|
||||
* This library 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 *
|
||||
* Lesser General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU Lesser General Public *
|
||||
* License along with this library. *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef LANGUAGE_H
|
||||
#define LANGUAGE_H
|
||||
|
||||
#include <QStringList>
|
||||
|
||||
|
||||
class Language : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit Language();
|
||||
static QString checkLanguage(const QString language,
|
||||
const QString defaultLanguage = QString("en"));
|
||||
static QString defineLanguage(const QString configPath,
|
||||
const QString options = QString("OPTIONS"));
|
||||
static QString defineLanguageFromFile(const QString configPath);
|
||||
static QString defineLanguageFromLocale();
|
||||
static QStringList getAvailableLanguages();
|
||||
|
||||
private:
|
||||
static QMap<QString, QString> parseOptions(const QString options);
|
||||
};
|
||||
|
||||
|
||||
#endif /* LANGUAGE_H */
|
45
pdebug/pdebug.h
Normal file
45
pdebug/pdebug.h
Normal file
@ -0,0 +1,45 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2014 Evgeniy Alekseev *
|
||||
* *
|
||||
* This library is free software; you can redistribute it and/or *
|
||||
* modify it under the terms of the GNU Lesser General Public *
|
||||
* License as published by the Free Software Foundation; either *
|
||||
* version 3.0 of the License, or (at your option) any later version. *
|
||||
* *
|
||||
* This library 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 *
|
||||
* Lesser General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU Lesser General Public *
|
||||
* License along with this library. *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#ifndef PRETTY_DEBUG_H
|
||||
#define PRETTY_DEBUG_H
|
||||
|
||||
|
||||
inline const char *pDebug(const std::string prettyFunction)
|
||||
{
|
||||
return prettyFunction.c_str();
|
||||
}
|
||||
|
||||
|
||||
inline std::string pFuncInfo(const std::string prettyFunction)
|
||||
{
|
||||
size_t colons = prettyFunction.find("::");
|
||||
// workaround for functions which are not belong to any class
|
||||
if (colons == std::string::npos)
|
||||
colons = prettyFunction.rfind("(");
|
||||
size_t begin = prettyFunction.substr(0, colons).rfind(" ") + 1;
|
||||
size_t end = prettyFunction.rfind("(") - begin;
|
||||
|
||||
return "[" + prettyFunction.substr(begin, end) + "]";
|
||||
}
|
||||
|
||||
|
||||
#define PDEBUG pDebug(pFuncInfo(__PRETTY_FUNCTION__))
|
||||
|
||||
|
||||
#endif /* PRETTY_DEBUG_H */
|
Loading…
Reference in New Issue
Block a user