]> vaikene.ee Git - evaf/blob - src/apps/ScosTime/gui.cpp
1a978afa34612aafc6ac15ce13bfbd3472596a04
[evaf] / src / apps / ScosTime / gui.cpp
1 /**
2 * @file ScosTime/gui.cpp
3 * @brief GUI for the ScosTime application
4 * @author Enar Vaikene
5 *
6 * Copyright (c) 2012 Enar Vaikene
7 *
8 * This file is part of the eVaf C++ cross-platform application development framework.
9 *
10 * This file can be used under the terms of the GNU General Public License
11 * version 3.0 as published by the Free Software Foundation and appearing in
12 * the file LICENSE included in the packaging of this file. Please review the
13 * the following information to ensure the GNU General Public License version
14 * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
15 *
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
18 */
19
20 #include "gui.h"
21
22 #include <Common/Globals>
23 #include <Common/iLogger>
24 #include <Common/iRegistry>
25 #include <SdiWindow/iSdiWindow>
26 #include <Gui/Panel>
27
28 #include <QtWidgets>
29
30
31 VER_EXPORT_VERSION_INFO()
32
33
34 //-------------------------------------------------------------------
35
36 using namespace eVaf;
37 using namespace eVaf::ScosTime;
38
39 Module::Module()
40 : Plugins::iPlugin()
41 , mReady(false)
42 {
43 setObjectName(QString("%1.%2").arg(VER_MODULE_NAME_STR).arg(__FUNCTION__));
44
45 EVAF_INFO("%s created", qPrintable(objectName()));
46 }
47
48 Module::~Module()
49 {
50 EVAF_INFO("%s destroyed", qPrintable(objectName()));
51 }
52
53 bool Module::init(QString const & args)
54 {
55 Q_UNUSED(args);
56
57 // Get the main window interface and fill it with the widgets
58 SdiWindow::iSdiWindow * win = evafQueryInterface<SdiWindow::iSdiWindow>("iSdiWindow");
59 EVAF_TEST_X(win, "No iSdiWindow interface");
60
61 Gui::Panel * panel = new Gui::Panel;
62 win->addPanel("PswGen", panel);
63
64 QVBoxLayout * v = new QVBoxLayout;
65 panel->setLayout(v);
66
67 QGridLayout * g = new QGridLayout;
68 v->addLayout(g);
69 g->setColumnStretch(1, 2);
70
71 QLabel * l = new QLabel(tr("&Epoch:", VER_MODULE_NAME_STR));
72 l->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
73 g->addWidget(l, 0, 0);
74
75 wEpoch = new QComboBox;
76 l->setBuddy(wEpoch);
77 wEpoch->setEditable(true);
78 wEpoch->addItems(QStringList() << "1970-01-01T00:00:00.000" << "1999-08-22T00:00:00.000");
79 g->addWidget(wEpoch, 0, 1);
80 panel->setFocusProxy(wEpoch);
81
82 l = new QLabel(tr("&Date/time:", VER_MODULE_NAME_STR));
83 l->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
84 g->addWidget(l, 1, 0);
85
86 wDateTime = new QLineEdit;
87 l->setBuddy(wDateTime);
88 QRegExp rxDateTime("^\\d{4}\\.\\d{3}\\.\\d{2}\\.\\d{2}\\.\\d{2}(\\.\\d{3})?$");
89 wDateTime->setValidator(new QRegExpValidator(rxDateTime, wDateTime));
90 g->addWidget(wDateTime, 1, 1);
91
92 QPushButton * btn = new QPushButton(tr("Convert", VER_MODULE_NAME_STR));
93 g->addWidget(btn, 1, 2);
94 connect(btn, SIGNAL(clicked()), this, SLOT(dateTimeClicked()));
95
96 l = new QLabel(tr("&HEX:", VER_MODULE_NAME_STR));
97 l->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
98 g->addWidget(l, 2, 0);
99
100 wHex = new QLineEdit;
101 l->setBuddy(wHex);
102 wHex->setMaxLength(12);
103 wHex->setInputMask(">HHHHHHHHhhhh");
104 g->addWidget(wHex, 2, 1);
105
106 btn = new QPushButton(tr("Convert", VER_MODULE_NAME_STR));
107 g->addWidget(btn, 2, 2);
108 connect(btn, SIGNAL(clicked()), this, SLOT(hexClicked()));
109
110 v->addStretch();
111
112 QHBoxLayout * h = new QHBoxLayout;
113 h->addStretch();
114 v->addLayout(h);
115
116 QAction * a = new QAction(panel);
117 a->setShortcut(Qt::Key_Escape);
118 connect(a, SIGNAL(triggered()), qApp, SLOT(quit()));
119 panel->addAction(a);
120
121 mReady = true;
122
123 EVAF_INFO("%s initialized", qPrintable(objectName()));
124
125 return true;
126 }
127
128 void Module::done()
129 {
130 mReady = false;
131
132 EVAF_INFO("%s finalized", qPrintable(objectName()));
133 }
134
135 QDateTime Module::strToDateTime(QString const & s) const
136 {
137 QStringList tok = s.split(QChar('.'));
138 if (tok.size() < 5)
139 return QDateTime();
140
141 bool ok = false;
142 int year = tok.at(0).toInt(&ok);
143 if (!ok)
144 return QDateTime();
145 int days = tok.at(1).toInt(&ok);
146 if (!ok)
147 return QDateTime();
148 int hours = tok.at(2).toInt(&ok);
149 if (!ok)
150 return QDateTime();
151 int minutes = tok.at(3).toInt(&ok);
152 if (!ok)
153 return QDateTime();
154 int secs = tok.at(4).toInt(&ok);
155 if (!ok)
156 return QDateTime();
157 int msecs = 0;
158 if (tok.size() > 5) {
159 msecs = tok.at(5).toInt(&ok);
160 if (!ok)
161 return QDateTime();
162 }
163
164 QDate dt(year, 1, 1);
165 dt = dt.addDays(days - 1);
166
167 return QDateTime(dt, QTime(hours, minutes, secs, msecs), Qt::UTC);
168 }
169
170 void Module::dateTimeClicked()
171 {
172 QDateTime epoch = QDateTime::fromString(wEpoch->currentText(), Qt::ISODate);
173 epoch.setTimeSpec(Qt::UTC);
174 QDateTime tm = strToDateTime(wDateTime->text());
175 qint64 msecs = epoch.msecsTo(tm);
176 quint32 coarse = quint32(msecs / 1000);
177 quint32 fine = quint32(rint(1000.0 * double(msecs % 1000) * 58.0 / 885.0));
178 wHex->setText(QString("%1%2").arg(coarse, 8, 16, QChar('0')).arg(fine, 4, 16, QChar('0')));
179 }
180
181 void Module::hexClicked()
182 {
183 bool ok = false;
184 int coarse = wHex->text().left(8).toLong(&ok, 16);
185 if (!ok)
186 return;
187 int fine = 0;
188 if (wHex->text().size() > 8)
189 fine = wHex->text().mid(8, 4).toLong(&ok, 16);
190 if (!ok)
191 return;
192
193 QDateTime epoch = QDateTime::fromString(wEpoch->currentText(), Qt::ISODate);
194 epoch.setTimeSpec(Qt::UTC);
195 QDateTime tm = epoch.addSecs(coarse);
196 tm = tm.addMSecs(rint((double(fine) / 58.0 * 885.0) / 1000.0));
197 wDateTime->setText(QString("%1.%2.%3.%4.%5.%6")
198 .arg(tm.date().year(), 4, 10, QChar('0'))
199 .arg(tm.date().dayOfYear(), 3, 10, QChar('0'))
200 .arg(tm.time().hour(), 2, 10, QChar('0'))
201 .arg(tm.time().minute(), 2, 10, QChar('0'))
202 .arg(tm.time().second(), 2, 10, QChar('0'))
203 .arg(tm.time().msec(), 3, 10, QChar('0')));
204 }