-
Notifications
You must be signed in to change notification settings - Fork 2
/
VSLapp.cpp
158 lines (133 loc) · 4.55 KB
/
VSLapp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "VSLapp.h"
#include <QLockFile>
#include <QDir>
#include <QApplication>
#include <QSettings>
#include <QDateTime>
#include <QMessageBox>
#include <QMainWindow>
#include <QTextStream>
#ifdef __unix
#include <unistd.h>
#include <sys/types.h>
#endif
void VSLapp::applicationSetup(const char *organizationName)
{
// set up application name
QFileInfo applicationFile(QApplication::applicationFilePath());
// These allow us to simply construct a "QSettings" object without arguments
qApp->setOrganizationDomain("mil.army.arl");
qApp->setApplicationName(applicationFile.baseName());
qApp->setOrganizationName(organizationName);
qApp->setApplicationVersion(__DATE__ __TIME__);
// Look up the last directory where the user opened files.
// set it if it hasn't been set.
QSettings settings;
if (!settings.allKeys().contains("app/currentDirectory"))
settings.setValue("app/currentDirectory", applicationFile.absoluteDir().absolutePath());
// log this launch of the program to track usage.
logApplicationLaunch(applicationFile); // comes after settingsRead so we can set a preference
}
void VSLapp::logApplicationLaunch(QFileInfo appFile)
{
QSettings settings;
settings.value("logUsage", QVariant(true));
qApp->applicationDirPath();
// obtain the string we will log to the file
QString message =
QDateTime::currentDateTime().toString("yyyyMMddhhmmss");
#ifdef __unix
message.append(getuid());
#endif
message.append("\n");
QString nameOfLogFile = QString("%1%2").arg(appFile.absoluteFilePath()).arg(".log");
QString nameOfLockFile = QString("%1%2").arg(nameOfLogFile).arg(".lck");
QLockFile lockFile(nameOfLockFile);
if (lockFile.tryLock(1000)) {
// got the lock write the data
QFile usageLogFile(nameOfLogFile);
if (usageLogFile.open(QIODevice::Append)) {
usageLogFile.write(message.toStdString().c_str());
usageLogFile.close();
}
lockFile.unlock();
}
}
QString VSLapp::getApplicationDir()
{
QDir applicationDir(qApp->applicationDirPath());
#if defined(Q_OS_WIN)
if (applicationDir.dirName().toLower() == "debug" ||
applicationDir.dirName().toLower() == "release"
# ifdef CMAKE_INTDIR
|| applicationDir.dirName() == CMAKE_INTDIR
# endif
)
applicationDir.cdUp();
#elif defined(Q_OS_MAC)
if (applicationDir.dirName() == "MacOS") {
applicationDir.cdUp();
applicationDir.cdUp();
applicationDir.cdUp();
}
#endif
return applicationDir.absolutePath();
}
#include <QTextEdit>
class QMessageBoxResize: public QMessageBox
{
public:
QMessageBoxResize(QWidget *parent = 0) : QMessageBox(parent) {
setMouseTracking(true);
setSizeGripEnabled(true);
}
private:
virtual bool event(QEvent *e) {
bool res = QMessageBox::event(e);
switch (e->type()) {
case QEvent::MouseMove:
case QEvent::MouseButtonPress:
setSizeGripEnabled(true);
setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
if (QWidget *textEdit = findChild<QTextEdit *>()) {
textEdit->setMaximumHeight(QWIDGETSIZE_MAX);
}
default:
break;
}
return res;
}
};
void VSLapp::showAboutDialog(QWidget *parent)
{
QMessageBoxResize msgBox(parent);
QString message;
QTextStream stream(&message);
stream << "Built " __DATE__ " " __TIME__ << endl
<< "By " BUILT_BY_USER " on " BUILT_ON_MACHINE << endl;
msgBox.setInformativeText(message); msgBox.setDetailedText(QString("Binary:%1").arg(VSLapp::getApplicationDir()));
msgBox.setWindowTitle(QString("About %1").arg(qApp->applicationName()));
msgBox.setText(QString("This is <b>%1</b> from <br><b>%2</b>")
.arg(qApp->applicationName())
.arg(qApp->organizationName()));
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
}
#define MainWindowGeometry "MainWindow/geometry"
#define MainWindowDoRestore "MainWindow/restore"
#define UI_VERSION 1
void VSLapp::mainWindowSetup(QMainWindow *mw)
{
mw->setWindowTitle(qApp->applicationName());
// set the geometry
QSettings settings;
if (settings.allKeys().contains(MainWindowGeometry)) {
mw->setGeometry(settings.value(MainWindowGeometry).toRect());
}
}
void VSLapp::mainWindowSave(QMainWindow *mw)
{
// stash things that we will want on startup.
QSettings settings;
settings.setValue(MainWindowGeometry, mw->geometry());
}