TcpSocket_Client_Server_Exa.../_include/application_config.cpp

171 lines
5.3 KiB
C++

/****************************************************************************
** Copyright (c) 2025 Evgeny Teterin (nayk) <nayk@nxt.ru>
** All right reserved.
**
** Permission is hereby granted, free of charge, to any person obtaining
** a copy of this software and associated documentation files (the
** "Software"), to deal in the Software without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**
****************************************************************************/
#include "application_config.h"
#include "config.h"
#include <QtWidgets/QApplication>
#include <QtCore/QTranslator>
#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QStringList>
#include <QtCore/QDateTime>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
# include <QtCore/QTextCodec>
#endif
//==============================================================================
const char * clDefault[2] { "#111416", "#fdfdfd" };
const char * clLogDate[2] { "#929292", "#333333" };
const char * clLogInf[2] { "#ffffff", "#000000" };
const char * clLogWrn[2] { "#ff9c54", "#8d3c00" };
const char * clLogErr[2] { "#ff4040", "#a50000" };
const char * clLogIn[2] { "#55d864", "#003706" };
//==============================================================================
void Application::initialize()
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
#endif
#if defined (PROG_NAME)
QApplication::setApplicationName( QString(PROG_NAME) );
#endif
#if defined (SOFT_DEVELOPER)
QApplication::setOrganizationName( QString(SOFT_DEVELOPER) );
#endif
#if defined (DEVELOPER_DOMAIN)
QApplication::setOrganizationDomain( QString(DEVELOPER_DOMAIN) );
#endif
#if defined (PROG_VERSION)
QApplication::setApplicationVersion( QString(PROG_VERSION) );
#endif
#if defined (BUILD_DATE)
qApp->setProperty( "buildDate", QString(BUILD_DATE) );
#endif
}
//==============================================================================
QString Application::applicationRootPath()
{
QString path { QApplication::applicationDirPath() };
if ( path.right(1) != "/" ) {
path += "/";
}
if (path.right(5) == "/bin/") {
path.remove( path.length() - 4, 4 );
}
return path;
}
//==============================================================================
void Application::installTranslations(const QString &lng)
{
const QString dirName { applicationRootPath() + "translations/" };
QDir translationsDir { dirName };
QStringList filesList = translationsDir.entryList(
QStringList() << QString("*_%1.qm").arg(lng),
QDir::Files
);
for (const QString &fileName: std::as_const(filesList)) {
QTranslator *translator = new QTranslator(QApplication::instance());
if (translator->load( dirName + fileName )) {
QApplication::instance()->installTranslator( translator );
}
else {
delete translator;
}
}
}
//==============================================================================
void Application::initializeLog(QTextEdit *textEdit)
{
if (!textEdit)
return;
textEdit->setStyleSheet(
QString(
"QTextEdit { "
"color: %1; "
"background-color: %2; "
"font-family: Courier New, Cascadia Mono, Lucida Console, Monospace; "
"font-size: 10pt; "
"}"
)
.arg( clDefault[ 1 ], clDefault[ 0 ] )
);
textEdit->document()->setMaximumBlockCount(10000);
}
//==============================================================================
void Application::addToLog(QTextEdit *textEdit, const QString &text, LogType logType)
{
if (!textEdit)
return;
const QString line {"<font color=\"%1\">[%2]</font> <font color=\"%3\">%4</font>"};
QString cl = QString(clLogInf[ 0 ]);
switch (logType) {
case LogTypeError:
cl = QString(clLogErr[ 0 ]);
break;
case LogTypeWarning:
cl = QString(clLogWrn[ 0 ]);
break;
case LogTypeData:
cl = QString(clLogIn[ 0 ]);
break;
default:
break;
}
textEdit->append(
line.arg(
QString(clLogDate[ 0 ]),
QDateTime::currentDateTime().toString("HH:mm:ss"),
cl,
text.toHtmlEscaped()
)
);
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
}
//==============================================================================