]>
vaikene.ee Git - evaf/blob - src/apps/ScosTime/gui.cpp
2 * @file ScosTime/gui.cpp
3 * @brief GUI for the ScosTime application
6 * Copyright (c) 2012 Enar Vaikene
8 * This file is part of the eVaf C++ cross-platform application development framework.
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.
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
23 #include <Common/Globals>
24 #include <Common/iLogger>
25 #include <Common/iRegistry>
26 #include <SdiWindow/iSdiWindow>
32 VER_EXPORT_VERSION_INFO()
33 Q_EXPORT_PLUGIN2(VER_MODULE_NAME_STR
, eVaf::ScosTime::Module
)
36 //-------------------------------------------------------------------
39 using namespace eVaf::ScosTime
;
42 //-------------------------------------------------------------------
44 Internal::DateTime::DateTime()
46 , mEpoch(QDateTime(QDate(1970, 1, 1), QTime(0, 0), Qt::UTC
))
47 , mRxIso(new QRegExp("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}(:\\d{2}(\\.\\d{3})?)?$"))
48 , mRxAsd(new QRegExp("^\\d{4}\\.\\d{3}\\.\\d{2}\\.\\d{2}(\\.\\d{2}(\\.\\d{3})?)?$"))
49 , mRxCUC(new QRegExp("^[0-9a-f]{8}([0-9a-f]{4})?$", Qt::CaseInsensitive
))
52 Internal::DateTime::~DateTime()
59 Internal::DateTime::Type
Internal::DateTime::setDateTime(QString
const & s
, QDateTime
const & epoch
)
62 return setDateTime(s
);
65 Internal::DateTime::Type
Internal::DateTime::setDateTime(QString
const & s
)
67 // Detect the type of the date/time string
68 mType
= getDateTimeType(s
);
74 // Convert the string to a date/time value
75 mDateTime
= strToDateTime(s
, mType
);
76 if (!mDateTime
.isValid())
85 void Internal::DateTime::setEpoch(QDateTime
const & epoch
)
94 // Adjust the date/time if the value was set from a CUC HEX string
95 if (mType
== CUC
&& mDateTime
.isValid())
97 qint64 diff
= mEpoch
.msecsTo(epoch
);
98 mDateTime
= mDateTime
.addMSecs(diff
);
104 Internal::DateTime::Type
Internal::DateTime::getDateTimeType(QString
const & s
) const
106 if (mRxIso
->exactMatch(s
))
110 else if (mRxAsd
->exactMatch(s
))
114 else if (mRxCUC
->exactMatch(s
))
121 QDateTime
Internal::DateTime::strToDateTime(QString
const & s
, Type type
) const
127 // Using the yyyy.ddd.hh.mm.ss[.zzz] format
128 QStringList tok
= s
.split(QChar('.'));
135 int year
= tok
.at(0).toInt(&ok
);
140 int days
= tok
.at(1).toInt(&ok
);
145 int hours
= tok
.at(2).toInt(&ok
);
150 int minutes
= tok
.at(3).toInt(&ok
);
159 secs
= tok
.at(4).toInt(&ok
);
166 msecs
= tok
.at(5).toInt(&ok
);
174 QDate
dt(year
, 1, 1);
175 dt
= dt
.addDays(days
- 1);
177 return QDateTime(dt
, QTime(hours
, minutes
, secs
, msecs
), Qt::UTC
);
182 // Using the ISO format yyyy-MM-ddThh:mm:ss[.zzz]
184 if (tmp
.length() < 19)
188 QDateTime dt
= QDateTime::fromString(tmp
, Qt::ISODate
);
189 dt
.setTimeSpec(Qt::UTC
);
195 // Get the CUC coarse and fine values
197 int coarse
= s
.left(8).toLong(&ok
, 16);
205 fine
= s
.mid(8, 4).toLong(&ok
, 16);
212 // Get the date/time value
213 QDateTime tm
= mEpoch
.addSecs(coarse
);
214 tm
= tm
.addMSecs(rint((double(fine
) / 58.0 * 885.0) / 1000.0));
226 QString
Internal::DateTime::asISOstring() const
228 if (mDateTime
.isValid())
230 return mDateTime
.toString("yyyy-MM-ddThh:mm:ss.zzz");
235 QString
Internal::DateTime::asASDstring() const
237 if (mDateTime
.isValid())
239 return QString("%1.%2.%3.%4.%5.%6")
240 .arg(mDateTime
.date().year(), 4, 10, QChar('0'))
241 .arg(mDateTime
.date().dayOfYear(), 3, 10, QChar('0'))
242 .arg(mDateTime
.time().hour(), 2, 10, QChar('0'))
243 .arg(mDateTime
.time().minute(), 2, 10, QChar('0'))
244 .arg(mDateTime
.time().second(), 2, 10, QChar('0'))
245 .arg(mDateTime
.time().msec(), 3, 10, QChar('0'));
250 QString
Internal::DateTime::asCUChexString() const
252 if (!mDateTime
.isValid())
257 // Convert to CUC coarse and fine values
258 qint64 msecs
= mEpoch
.msecsTo(mDateTime
);
259 quint32 coarse
= quint32(msecs
/ 1000);
260 quint32 fine
= quint32(rint(1000.0 * double(msecs
% 1000) * 58.0 / 885.0));
262 // Set the CUC hex string
263 return QString("%1%2").arg(coarse
, 8, 16, QChar('0')).arg(fine
, 4, 16, QChar('0'));
267 //-------------------------------------------------------------------
272 , mLastDateTimeType(Internal::DateTime::Invalid
)
274 setObjectName(QString("%1.%2").arg(VER_MODULE_NAME_STR
).arg(__FUNCTION__
));
276 EVAF_INFO("%s created", qPrintable(objectName()));
281 EVAF_INFO("%s destroyed", qPrintable(objectName()));
284 bool Module::init(QString
const & args
)
288 // Get the main window interface and fill it with the widgets
289 SdiWindow::iSdiWindow
* win
= evafQueryInterface
<SdiWindow::iSdiWindow
>("iSdiWindow");
290 EVAF_TEST_X(win
, "No iSdiWindow interface");
292 Gui::Panel
* panel
= new Gui::Panel
;
293 win
->addPanel("PswGen", panel
);
295 QVBoxLayout
* v
= new QVBoxLayout
;
298 QGridLayout
* g
= new QGridLayout
;
300 g
->setColumnStretch(1, 2);
302 QLabel
* l
= new QLabel(tr("&Epoch:", VER_MODULE_NAME_STR
));
303 l
->setAlignment(Qt::AlignRight
| Qt::AlignVCenter
);
304 g
->addWidget(l
, 0, 0);
306 wEpoch
= new QComboBox
;
308 wEpoch
->setEditable(true);
310 QRegExp
rx("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d{3})?$");
311 wEpoch
->setValidator(new QRegExpValidator(rx
, wEpoch
));
313 wEpoch
->addItems(QStringList() << "1970-01-01T00:00:00.000" << "1999-08-22T00:00:00.000");
314 connect(wEpoch
, SIGNAL(editTextChanged(QString
)), this, SLOT(epochChanged(QString
)));
315 g
->addWidget(wEpoch
, 0, 1);
316 panel
->setFocusProxy(wEpoch
);
318 l
= new QLabel(tr("&Date/time:", VER_MODULE_NAME_STR
));
319 l
->setAlignment(Qt::AlignRight
| Qt::AlignVCenter
);
320 g
->addWidget(l
, 1, 0);
322 wDateTime
= new QLineEdit
;
323 l
->setBuddy(wDateTime
);
325 QRegExp
rx("^(\\d{4}\\.\\d{3}\\.\\d{2}\\.\\d{2}\\.\\d{2}(\\.\\d{3})?)|(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(.\\d{3})?)$");
326 wDateTime
->setValidator(new QRegExpValidator(rx
, wDateTime
));
328 connect(wDateTime
, SIGNAL(textEdited(QString
)), this, SLOT(dateTimeEdited(QString
)));
329 g
->addWidget(wDateTime
, 1, 1);
331 QPushButton
* btn
= new QPushButton();
332 btn
->setDisabled(true);
333 g
->addWidget(btn
, 1, 2);
334 connect(btn
, SIGNAL(clicked()), this, SLOT(dateTimeClicked()));
335 wConvertDateTime
= btn
;
337 l
= new QLabel(tr("&CUC:", VER_MODULE_NAME_STR
));
338 l
->setAlignment(Qt::AlignRight
| Qt::AlignVCenter
);
339 g
->addWidget(l
, 2, 0);
341 wCucHex
= new QLineEdit
;
342 l
->setBuddy(wCucHex
);
343 wCucHex
->setMaxLength(12);
345 QRegExp
rx("^[0-9a-f]{8}([0-9a-f]{4})?$", Qt::CaseInsensitive
);
346 wCucHex
->setValidator(new QRegExpValidator(rx
, wCucHex
));
348 connect(wCucHex
, SIGNAL(textEdited(QString
)), this, SLOT(cucHexEdited(QString
)));
349 g
->addWidget(wCucHex
, 2, 1);
353 QHBoxLayout
* h
= new QHBoxLayout
;
357 QAction
* a
= new QAction(panel
);
358 a
->setShortcut(Qt::Key_Escape
);
359 connect(a
, SIGNAL(triggered()), qApp
, SLOT(quit()));
364 EVAF_INFO("%s initialized", qPrintable(objectName()));
373 EVAF_INFO("%s finalized", qPrintable(objectName()));
376 void Module::dateTimeClicked()
378 if (!mDateTime
.isValid())
383 // Convert to another type
384 switch (mLastDateTimeType
)
386 case Internal::DateTime::ISO
:
388 mLastDateTimeType
= Internal::DateTime::ASD
;
389 wDateTime
->setText(mDateTime
.asASDstring());
390 wConvertDateTime
->setText(tr("&to ISO", VER_MODULE_NAME_STR
));
393 case Internal::DateTime::ASD
:
395 mLastDateTimeType
= Internal::DateTime::ISO
;
396 wDateTime
->setText(mDateTime
.asISOstring());
397 wConvertDateTime
->setText(tr("&to ASD", VER_MODULE_NAME_STR
));
405 void Module::dateTimeEdited(QString
const & s
)
407 wConvertDateTime
->setDisabled(true);
408 wConvertDateTime
->setText(QString());
409 mLastDateTimeType
= Internal::DateTime::Invalid
;
416 mDateTime
.setDateTime(s
);
417 if (!mDateTime
.isValid())
422 mLastDateTimeType
= mDateTime
.type();
424 // Set the CUC hex string from this date/time string
425 wCucHex
->setText(mDateTime
.asCUChexString());
427 // Enable the button that converts between ISO and ASD date/time strings
428 if (mDateTime
.type() == Internal::DateTime::ISO
)
430 wConvertDateTime
->setEnabled(true);
431 wConvertDateTime
->setText(tr("&to ASD", VER_MODULE_NAME_STR
));
433 else if (mDateTime
.type() == Internal::DateTime::ISO
)
435 wConvertDateTime
->setEnabled(true);
436 wConvertDateTime
->setText(tr("&to ISO", VER_MODULE_NAME_STR
));
440 void Module::cucHexEdited(QString
const & s
)
442 if (s
.isEmpty() || s
.size() < 8)
447 mDateTime
.setDateTime(s
);
448 if (!mDateTime
.isValid())
453 if (mLastDateTimeType
== Internal::DateTime::Invalid
)
455 mLastDateTimeType
= Internal::DateTime::ASD
;
458 // Set the date/time string in the last used format
459 if (mLastDateTimeType
== Internal::DateTime::ASD
)
461 wDateTime
->setText(mDateTime
.asASDstring());
462 wConvertDateTime
->setEnabled(true);
463 wConvertDateTime
->setText(tr("&to ISO", VER_MODULE_NAME_STR
));
465 else if (mLastDateTimeType
== Internal::DateTime::ISO
)
467 wDateTime
->setText(mDateTime
.asISOstring());
468 wConvertDateTime
->setEnabled(true);
469 wConvertDateTime
->setText(tr("&to ASD", VER_MODULE_NAME_STR
));
473 void Module::epochChanged(QString
const & s
)
477 QRegExp
rx("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d{3})?$");
478 if (!rx
.exactMatch(s
))
481 QDateTime dt
= QDateTime::fromString(s
, Qt::ISODate
);
482 dt
.setTimeSpec(Qt::UTC
);
483 mDateTime
.setEpoch(dt
);
485 // If there is a valid entry, do the conversion
486 switch (mDateTime
.type())
488 case Internal::DateTime::ISO
:
489 case Internal::DateTime::ASD
:
491 wCucHex
->setText(mDateTime
.asCUChexString());
494 case Internal::DateTime::CUC
:
496 if (mLastDateTimeType
== Internal::DateTime::ASD
)
498 wDateTime
->setText(mDateTime
.asASDstring());
500 else if (mLastDateTimeType
== Internal::DateTime::ISO
)
502 wDateTime
->setText(mDateTime
.asISOstring());