QtLingo 1
QtLingo is an Application to make Qt Computer Translations easier
main.cpp File Reference
#include <QDateTime>
#include <QStandardPaths>
#include <QFileInfo>
#include <QIcon>
#include <QStyleHints>
#include <QUrl>
#include <QScreen>
#include "MainWindow.h"
#include "MyLanguageModel.h"
#include <iostream>
#include <assert.h>
#include <algorithm>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include "main.moc"

Go to the source code of this file.

Classes

class  Utils
 Utils. Utils Workaround: As of Qt 5.4 QtQuick does not expose QUrl::fromUserInput. More...
 

Macros

#define VERSION   "1.0"
 

Functions

QString getMessageLogFile (const QString &thisAppDataLocation)
 
QString getFileErrorMessage (const QString &thisAppDataLocation)
 
bool setMessageLogFile (const QString &thisAppName, const QString &thisAppDataLocation)
 
void logEvents (const QString &thisMessage)
 
void myMessageHandler (QtMsgType thisType, const QMessageLogContext &thisContext, const QString &thisMsg)
 
int main (int argc, char *argv[])
 main.moc. main.moc must be included below to define above More...
 

Variables

QString myLogFile = ""
 

Macro Definition Documentation

◆ VERSION

#define VERSION   "1.0"

Definition at line 28 of file main.cpp.

Function Documentation

◆ getFileErrorMessage()

QString getFileErrorMessage ( const QString &  thisAppDataLocation)

Definition at line 44 of file main.cpp.

45{
46 return QString("%1: %2").arg(QObject::tr("Failed to open log file"), thisAppDataLocation);
47}

◆ getMessageLogFile()

QString getMessageLogFile ( const QString &  thisAppDataLocation)

Definition at line 36 of file main.cpp.

37{
38 return QString("%1%2%3").arg(thisAppDataLocation, QDir::separator(), "messageLog.txt");
39}

◆ logEvents()

void logEvents ( const QString &  thisMessage)

Definition at line 83 of file main.cpp.

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}
QString myLogFile
Definition: main.cpp:31

◆ main()

int main ( int  argc,
char *  argv[] 
)

main.moc. main.moc must be included below to define above

main. main

< Prefix of Translation files

< Relative Folder for Translation files

< Relative Folder for Help files

Definition at line 162 of file main.cpp.

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}
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
#define VERSION
Definition: main.cpp:28
bool setMessageLogFile(const QString &thisAppName, const QString &thisAppDataLocation)
Definition: main.cpp:52
void myMessageHandler(QtMsgType thisType, const QMessageLogContext &thisContext, const QString &thisMsg)
Definition: main.cpp:101

◆ myMessageHandler()

void myMessageHandler ( QtMsgType  thisType,
const QMessageLogContext &  thisContext,
const QString &  thisMsg 
)

Definition at line 101 of file main.cpp.

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}
void logEvents(const QString &thisMessage)
Definition: main.cpp:83

◆ setMessageLogFile()

bool setMessageLogFile ( const QString &  thisAppName,
const QString &  thisAppDataLocation 
)

Definition at line 52 of file main.cpp.

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}
QString getMessageLogFile(const QString &thisAppDataLocation)
Definition: main.cpp:36

Variable Documentation

◆ myLogFile

QString myLogFile = ""

Definition at line 31 of file main.cpp.