QtLingo 1
QtLingo is an Application to make Qt Computer Translations easier
main.cpp
Go to the documentation of this file.
1#include <QDateTime>
2#include <QStandardPaths>
3#include <QFileInfo>
4#include <QIcon>
5#include <QStyleHints>
6#include <QUrl>
7#include <QScreen>
8#ifdef MY_QML
9#include <QGuiApplication>
10#include <QQmlApplicationEngine>
11#include <QQmlContext>
12#else
13#include "MainWindow.h"
14#endif
15//
16#include "MyLanguageModel.h"
17// STD
18#include <iostream>
19#include <assert.h>
20#include <algorithm>
21#include <sstream>
22#include <stdio.h>
23#include <stdlib.h>
24//
25#if Q_OS_MSDOS || defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
26#define VERSION "1.0.0"
27#else
28#define VERSION "1.0"
29#endif
30//
31QString myLogFile = "";
32
36QString getMessageLogFile(const QString &thisAppDataLocation)
37{
38 return QString("%1%2%3").arg(thisAppDataLocation, QDir::separator(), "messageLog.txt");
39}
40
44QString getFileErrorMessage(const QString &thisAppDataLocation)
45{
46 return QString("%1: %2").arg(QObject::tr("Failed to open log file"), thisAppDataLocation);
47}
48
52bool setMessageLogFile(const QString &thisAppName, const QString &thisAppDataLocation)
53{
54 QString theFullFilePath = getMessageLogFile(thisAppDataLocation);
55 if (QFileInfo::exists(theFullFilePath) && QFileInfo(theFullFilePath).isFile())
56 { QFile::remove(theFullFilePath); }
57 QFile theFile(theFullFilePath);
58 if(!theFile.open(QFile::WriteOnly | QFile::Text | QIODevice::Truncate))
59 {
60 QString theErrorMessage = QString("%1: %2").arg(QObject::tr("Could not open log file"), theFullFilePath);
61 std::cout << theErrorMessage.toStdString() << std::endl;
62 return false;
63 }
64 // Write to log file
65 QTextStream theFileStream(&theFile);
66 const QDateTime theDateTimeStamp = QDateTime::currentDateTime();
67 QString theDateStamp = QString("%1: %2 - %3 %4").arg(QObject::tr("Log File"), thisAppName, theDateTimeStamp.toString("dd MMM yyyy hh:mm:ss"), theDateTimeStamp.timeZoneAbbreviation());
68 theFileStream << theDateStamp << '\n';
69 theFile.flush();
70 theFile.close();
71 if (QFileInfo::exists(theFullFilePath) && QFileInfo(theFullFilePath).isFile())
72 {
73 myLogFile = theFullFilePath;
74 return true;
75 }
76 else
77 { return false; }
78}
79
83void logEvents(const QString &thisMessage)
84{
85 QFile theFileHandle(myLogFile);
86 if(!theFileHandle.open( QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text ))
87 {
88 // Do not use qDebug() since that is a loop
89 std::cout << QObject::tr("Log File failed to open").toStdString() << ": " << myLogFile.toStdString() << std::endl;
90 return;
91 }
92 QTextStream theFileStream(&theFileHandle);
93 theFileStream << thisMessage;
94 theFileStream.flush();
95 theFileHandle.close();
96}
97
101void myMessageHandler(QtMsgType thisType, const QMessageLogContext &thisContext, const QString &thisMsg)
102{
103 QByteArray theLocalMsg = thisMsg.toLocal8Bit();
104 const char *theFile = thisContext.file ? thisContext.file : "";
105 const char *theFunction = thisContext.function ? thisContext.function : "";
106 //
107 switch (thisType)
108 {
109 case QtDebugMsg:
110 logEvents(QString("Debug: %1 (%2:%3, %4)\n").arg(theLocalMsg.constData(), theFile, QString::number(thisContext.line), theFunction));
111 fprintf(stderr, "Debug: %s (%s:%u, %s)\n", theLocalMsg.constData(), theFile, thisContext.line, theFunction);
112 break;
113 case QtInfoMsg:
114 logEvents(QString("Info: %1 (%2:%3, %4)\n").arg(theLocalMsg.constData(), theFile, QString::number(thisContext.line), theFunction));
115 fprintf(stderr, "Info: %s (%s:%u, %s)\n", theLocalMsg.constData(), theFile, thisContext.line, theFunction);
116 break;
117 case QtWarningMsg:
118 logEvents(QString("Warning: %1 (%2:%3, %4)\n").arg(theLocalMsg.constData(), theFile, QString::number(thisContext.line), theFunction));
119 fprintf(stderr, "Warning: %s (%s:%u, %s)\n", theLocalMsg.constData(), theFile, thisContext.line, theFunction);
120 break;
121 case QtCriticalMsg:
122 logEvents(QString("Critical: %1 (%2:%3, %4)\n").arg(theLocalMsg.constData(), theFile, QString::number(thisContext.line), theFunction));
123 fprintf(stderr, "Critical: %s (%s:%u, %s)\n", theLocalMsg.constData(), theFile, thisContext.line, theFunction);
124 break;
125 case QtFatalMsg:
126 logEvents(QString("Fatal: %1 (%2:%3, %4)\n").arg(theLocalMsg.constData(), theFile, QString::number(thisContext.line), theFunction));
127 fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", theLocalMsg.constData(), theFile, thisContext.line, theFunction);
128 break;
129 }
130}
131
136class Utils : public QObject
137{
138 Q_OBJECT
139 public:
140 Utils(QObject* parent = nullptr) : QObject(parent) { }
141 Q_INVOKABLE static QUrl fromUserInput(const QString& userInput);
142};
143
147QUrl Utils::fromUserInput(const QString& userInput)
148{
149 if (userInput.isEmpty()) return QUrl::fromUserInput("about:blank");
150 const QUrl result = QUrl::fromUserInput(userInput);
151 return result.isValid() ? result : QUrl::fromUserInput("about:blank");
152}
153
157#include "main.moc"
158
162int main(int argc, char *argv[])
163{
164 QString theAppName = "QtLingo";
165 QString theAppDisplayName = "Qt Lingo";
166 bool isDebugMessage = true;
167 //
168 QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
169 //
170 #ifdef MY_QML
171 QGuiApplication theApplication(argc, argv);
172 QGuiApplication::setWindowIcon(QIcon(":/images/logo32.png"));
173 QQmlApplicationEngine theEngine;
174 QQmlContext *theContext = theEngine.rootContext();
175 //
176 QGuiApplication::setApplicationDisplayName(QCoreApplication::translate("main", theAppDisplayName.toLocal8Bit()));
177 #else
178 // Load Resource File QtLingo.qrc before creating Application
179 Q_INIT_RESOURCE(QtLingo);
180 QApplication theApplication(argc, argv);
181 QApplication::setWindowIcon(QIcon(":/images/logo32.png"));
182 #endif
183
184 // Setup the Application for MyOrgSettings
185 theApplication.setOrganizationName(theAppName);
186 theApplication.setApplicationName(theAppName);
187 theApplication.setApplicationDisplayName(theAppDisplayName);
188 theApplication.setApplicationVersion(VERSION);
189 //
190 #ifdef MY_QML
191 MyLanguageModel *theLanguageModel = new MyLanguageModel(theEngine, qApp);
192 #else
193 MyLanguageModel *theLanguageModel = new MyLanguageModel(qApp);
194 #endif
195 theLanguageModel->setTransFilePrefix(theAppName);
196 theLanguageModel->setTranslationSource("translations");
197 theLanguageModel->setHelpSource("help");
198 theLanguageModel->setLanguages();
199 //
200 theLanguageModel->mySetting->setVersion(VERSION);
201 theLanguageModel->mySetting->setIniFileName(theAppName);
202 //
203 //
204 // Run after the Application is created and given a name and before creating MainWindow
205 qInstallMessageHandler(myMessageHandler);
206 // FIXME what if error
207 setMessageLogFile(theApplication.applicationName(), theLanguageModel->mySetting->getAppDataLocation());
208//
209 #ifdef MY_QML
210 //
211 theContext->setContextProperty("thisLanguageList", theLanguageModel->getLanguageList());
212 // thisDefaultLanguageName
213 // FIXME function
214 QString theLanguageName =theLanguageModel->mySetting->readSettings(theLanguageModel->mySetting->MY_LOCALE_LANG_NAME, theLanguageModel->getDefaultLanguageCode());
215 theContext->setContextProperty("thisDefaultLanguageName", theLanguageName);
216 // ComboBox Model
217 theContext->setContextProperty("thisLanguageModel", theLanguageModel);
218 // isDebugMessage
219 theContext->setContextProperty("isDebugMessage", isDebugMessage);
220 //
221 const QUrl theQmlUrl(QStringLiteral("qrc:/qml/main.qml"));
222 QObject::connect(&theEngine, &QQmlApplicationEngine::objectCreated, &theApplication, [theQmlUrl](QObject *obj, const QUrl &objUrl)
223 {
224 if (!obj && theQmlUrl == objUrl)
225 { QCoreApplication::exit(-1); }
226 }, Qt::QueuedConnection);
227 theEngine.load(theQmlUrl);
228 if (theEngine.rootObjects().isEmpty()) return -1;
229 #endif
230
231 #ifndef MY_QML
232 #ifdef MYLANG
233 MainWindow *theMainWindow = new MainWindow(theLanguageModel);
234 #else
235 MainWindow *theMainWindow = new MainWindow();
236 theMainWindow->setLanguageModel(theLanguageModel);
237 #endif
238 theMainWindow->show();
239 #endif
240 return theApplication.exec();
241}
242
Main Window Constructor. /class MainWindow.
Definition: MainWindow.h:77
void setLanguageModel(MyLanguageModel *thisLanguageModel)
set Language Model
Definition: MainWindow.cpp:111
Abstact Model used for ComboBox. /class MyLanguageModel.
void setTransFilePrefix(const QString &thisTransFilePrefix)
set TransFile Prefix
Q_INVOKABLE void setLanguages()
set Languages
QString getDefaultLanguageCode()
get Default Language Code
void setTranslationSource(const QString &thisTranslationSource)
set Translation Source
Q_INVOKABLE QStringList & getLanguageList()
get Language List
void setHelpSource(const QString &thisHelpSource)
set Help Source
MyOrgSettings * mySetting
mySetting
const QString MY_LOCALE_LANG_NAME
MY_LOCALE_LANG_NAME
Definition: MyOrgSettings.h:94
QString getAppDataLocation()
get AppData Location
void setIniFileName(const QString &thisIniFileName)
set IniFile
void setVersion(const QString &thisVersion)
set Version
QString readSettings(const QString &thisSetting, const QString &thisDefault)
read Settings
Utils. Utils Workaround: As of Qt 5.4 QtQuick does not expose QUrl::fromUserInput.
Definition: main.cpp:137
static Q_INVOKABLE QUrl fromUserInput(const QString &userInput)
fromUserInput. fromUserInput
Definition: main.cpp:147
Utils(QObject *parent=nullptr)
Definition: main.cpp:140
int main(int argc, char *argv[])
main.moc. main.moc must be included below to define above
Definition: main.cpp:162
#define VERSION
Definition: main.cpp:28
bool setMessageLogFile(const QString &thisAppName, const QString &thisAppDataLocation)
Definition: main.cpp:52
QString myLogFile
Definition: main.cpp:31
QString getMessageLogFile(const QString &thisAppDataLocation)
Definition: main.cpp:36
void myMessageHandler(QtMsgType thisType, const QMessageLogContext &thisContext, const QString &thisMsg)
Definition: main.cpp:101
QString getFileErrorMessage(const QString &thisAppDataLocation)
Definition: main.cpp:44
void logEvents(const QString &thisMessage)
Definition: main.cpp:83