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