]>
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.
22 #include <Common/Globals>
23 #include <Common/iLogger>
24 #include <Common/iRegistry>
25 #include <SdiWindow/iSdiWindow>
31 VER_EXPORT_VERSION_INFO()
34 //-------------------------------------------------------------------
37 using namespace eVaf::ScosTime
;
40 //-------------------------------------------------------------------
42 Internal::DateTime::DateTime()
44 , mEpoch(QDateTime(QDate(1970, 1, 1), QTime(0, 0), Qt::UTC
))
45 , mRxIso(new QRegExp("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}(:\\d{2}(\\.\\d{3})?)?$"))
46 , mRxAsd(new QRegExp("^\\d{4}\\.\\d{3}\\.\\d{2}\\.\\d{2}(\\.\\d{2}(\\.\\d{3})?)?$"))
47 , mRxCUC(new QRegExp("^[0-9a-f]{8}([0-9a-f]{4})?$", Qt::CaseInsensitive
))
50 Internal::DateTime::~DateTime()
57 Internal::DateTime::Type
Internal::DateTime::setDateTime(QString
const & s
, QDateTime
const & epoch
)
60 return setDateTime(s
);
63 Internal::DateTime::Type
Internal::DateTime::setDateTime(QString
const & s
)
65 // Detect the type of the date/time string
66 mType
= getDateTimeType(s
);
72 // Convert the string to a date/time value
73 mDateTime
= strToDateTime(s
, mType
);
74 if (!mDateTime
.isValid())
83 void Internal::DateTime::setEpoch(QDateTime
const & epoch
)
92 // Adjust the date/time if the value was set from a CUC HEX string
93 if (mType
== CUC
&& mDateTime
.isValid())
95 qint64 diff
= mEpoch
.msecsTo(epoch
);
96 mDateTime
= mDateTime
.addMSecs(diff
);
102 Internal::DateTime::Type
Internal::DateTime::getDateTimeType(QString
const & s
) const
104 if (mRxIso
->exactMatch(s
))
108 else if (mRxAsd
->exactMatch(s
))
112 else if (mRxCUC
->exactMatch(s
))
119 QDateTime
Internal::DateTime::strToDateTime(QString
const & s
, Type type
) const
125 // Using the yyyy.ddd.hh.mm.ss[.zzz] format
126 QStringList tok
= s
.split(QChar('.'));
133 int year
= tok
.at(0).toInt(&ok
);
138 int days
= tok
.at(1).toInt(&ok
);
143 int hours
= tok
.at(2).toInt(&ok
);
148 int minutes
= tok
.at(3).toInt(&ok
);
157 secs
= tok
.at(4).toInt(&ok
);
164 msecs
= tok
.at(5).toInt(&ok
);
172 QDate
dt(year
, 1, 1);
173 dt
= dt
.addDays(days
- 1);
175 return QDateTime(dt
, QTime(hours
, minutes
, secs
, msecs
), Qt::UTC
);
180 // Using the ISO format yyyy-MM-ddThh:mm:ss[.zzz]
182 if (tmp
.length() < 19)
186 QDateTime dt
= QDateTime::fromString(tmp
, Qt::ISODate
);
187 dt
.setTimeSpec(Qt::UTC
);
193 // Get the CUC coarse and fine values
195 int coarse
= s
.left(8).toLong(&ok
, 16);
203 fine
= s
.mid(8, 4).toLong(&ok
, 16);
210 // Get the date/time value
211 QDateTime tm
= mEpoch
.addSecs(coarse
);
212 tm
= tm
.addMSecs(rint((double(fine
) / 58.0 * 885.0) / 1000.0));
224 QString
Internal::DateTime::asISOstring() const
226 if (mDateTime
.isValid())
228 return mDateTime
.toString("yyyy-MM-ddThh:mm:ss.zzz");
233 QString
Internal::DateTime::asASDstring() const
235 if (mDateTime
.isValid())
237 return QString("%1.%2.%3.%4.%5.%6")
238 .arg(mDateTime
.date().year(), 4, 10, QChar('0'))
239 .arg(mDateTime
.date().dayOfYear(), 3, 10, QChar('0'))
240 .arg(mDateTime
.time().hour(), 2, 10, QChar('0'))
241 .arg(mDateTime
.time().minute(), 2, 10, QChar('0'))
242 .arg(mDateTime
.time().second(), 2, 10, QChar('0'))
243 .arg(mDateTime
.time().msec(), 3, 10, QChar('0'));
248 QString
Internal::DateTime::asCUChexString() const
250 if (!mDateTime
.isValid())
255 // Convert to CUC coarse and fine values
256 qint64 msecs
= mEpoch
.msecsTo(mDateTime
);
257 quint32 coarse
= quint32(msecs
/ 1000);
258 quint32 fine
= quint32(rint(1000.0 * double(msecs
% 1000) * 58.0 / 885.0));
260 // Set the CUC hex string
261 return QString("%1%2").arg(coarse
, 8, 16, QChar('0')).arg(fine
, 4, 16, QChar('0'));
265 //-------------------------------------------------------------------
270 , mLastDateTimeType(Internal::DateTime::Invalid
)
272 setObjectName(QString("%1.%2").arg(VER_MODULE_NAME_STR
).arg(__FUNCTION__
));
274 EVAF_INFO("%s created", qPrintable(objectName()));
279 EVAF_INFO("%s destroyed", qPrintable(objectName()));
282 bool Module::init(QString
const & args
)
286 // Get the main window interface and fill it with the widgets
287 SdiWindow::iSdiWindow
* win
= evafQueryInterface
<SdiWindow::iSdiWindow
>("iSdiWindow");
288 EVAF_TEST_X(win
, "No iSdiWindow interface");
290 Gui::Panel
* panel
= new Gui::Panel
;
291 win
->addPanel("PswGen", panel
);
293 QVBoxLayout
* v
= new QVBoxLayout
;
296 QGridLayout
* g
= new QGridLayout
;
298 g
->setColumnStretch(1, 2);
300 QLabel
* l
= new QLabel(tr("&Epoch:", VER_MODULE_NAME_STR
));
301 l
->setAlignment(Qt::AlignRight
| Qt::AlignVCenter
);
302 g
->addWidget(l
, 0, 0);
304 wEpoch
= new QComboBox
;
306 wEpoch
->setEditable(true);
308 QRegExp
rx("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d{3})?$");
309 wEpoch
->setValidator(new QRegExpValidator(rx
, wEpoch
));
311 wEpoch
->addItems(QStringList() << "1970-01-01T00:00:00.000" << "1999-08-22T00:00:00.000");
312 connect(wEpoch
, SIGNAL(editTextChanged(QString
)), this, SLOT(epochChanged(QString
)));
313 g
->addWidget(wEpoch
, 0, 1);
314 panel
->setFocusProxy(wEpoch
);
316 l
= new QLabel(tr("&Date/time:", VER_MODULE_NAME_STR
));
317 l
->setAlignment(Qt::AlignRight
| Qt::AlignVCenter
);
318 g
->addWidget(l
, 1, 0);
320 wDateTime
= new QLineEdit
;
321 l
->setBuddy(wDateTime
);
323 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})?)$");
324 wDateTime
->setValidator(new QRegExpValidator(rx
, wDateTime
));
326 connect(wDateTime
, SIGNAL(textEdited(QString
)), this, SLOT(dateTimeEdited(QString
)));
327 g
->addWidget(wDateTime
, 1, 1);
329 QPushButton
* btn
= new QPushButton();
330 btn
->setDisabled(true);
331 g
->addWidget(btn
, 1, 2);
332 connect(btn
, SIGNAL(clicked()), this, SLOT(dateTimeClicked()));
333 wConvertDateTime
= btn
;
335 l
= new QLabel(tr("&CUC:", VER_MODULE_NAME_STR
));
336 l
->setAlignment(Qt::AlignRight
| Qt::AlignVCenter
);
337 g
->addWidget(l
, 2, 0);
339 wCucHex
= new QLineEdit
;
340 l
->setBuddy(wCucHex
);
341 wCucHex
->setMaxLength(12);
343 QRegExp
rx("^[0-9a-f]{8}([0-9a-f]{4})?$", Qt::CaseInsensitive
);
344 wCucHex
->setValidator(new QRegExpValidator(rx
, wCucHex
));
346 connect(wCucHex
, SIGNAL(textEdited(QString
)), this, SLOT(cucHexEdited(QString
)));
347 g
->addWidget(wCucHex
, 2, 1);
351 QHBoxLayout
* h
= new QHBoxLayout
;
355 QAction
* a
= new QAction(panel
);
356 a
->setShortcut(Qt::Key_Escape
);
357 connect(a
, SIGNAL(triggered()), qApp
, SLOT(quit()));
362 EVAF_INFO("%s initialized", qPrintable(objectName()));
371 EVAF_INFO("%s finalized", qPrintable(objectName()));
374 void Module::dateTimeClicked()
376 if (!mDateTime
.isValid())
381 // Convert to another type
382 switch (mLastDateTimeType
)
384 case Internal::DateTime::ISO
:
386 mLastDateTimeType
= Internal::DateTime::ASD
;
387 wDateTime
->setText(mDateTime
.asASDstring());
388 wConvertDateTime
->setText(tr("&to ISO", VER_MODULE_NAME_STR
));
391 case Internal::DateTime::ASD
:
393 mLastDateTimeType
= Internal::DateTime::ISO
;
394 wDateTime
->setText(mDateTime
.asISOstring());
395 wConvertDateTime
->setText(tr("&to ASD", VER_MODULE_NAME_STR
));
403 void Module::dateTimeEdited(QString
const & s
)
405 wConvertDateTime
->setDisabled(true);
406 wConvertDateTime
->setText(QString());
407 mLastDateTimeType
= Internal::DateTime::Invalid
;
414 mDateTime
.setDateTime(s
);
415 if (!mDateTime
.isValid())
420 mLastDateTimeType
= mDateTime
.type();
422 // Set the CUC hex string from this date/time string
423 wCucHex
->setText(mDateTime
.asCUChexString());
425 // Enable the button that converts between ISO and ASD date/time strings
426 if (mDateTime
.type() == Internal::DateTime::ISO
)
428 wConvertDateTime
->setEnabled(true);
429 wConvertDateTime
->setText(tr("&to ASD", VER_MODULE_NAME_STR
));
431 else if (mDateTime
.type() == Internal::DateTime::ASD
)
433 wConvertDateTime
->setEnabled(true);
434 wConvertDateTime
->setText(tr("&to ISO", VER_MODULE_NAME_STR
));
438 void Module::cucHexEdited(QString
const & s
)
440 if (s
.isEmpty() || s
.size() < 8)
445 mDateTime
.setDateTime(s
);
446 if (!mDateTime
.isValid())
451 if (mLastDateTimeType
== Internal::DateTime::Invalid
)
453 mLastDateTimeType
= Internal::DateTime::ASD
;
456 // Set the date/time string in the last used format
457 if (mLastDateTimeType
== Internal::DateTime::ASD
)
459 wDateTime
->setText(mDateTime
.asASDstring());
460 wConvertDateTime
->setEnabled(true);
461 wConvertDateTime
->setText(tr("&to ISO", VER_MODULE_NAME_STR
));
463 else if (mLastDateTimeType
== Internal::DateTime::ISO
)
465 wDateTime
->setText(mDateTime
.asISOstring());
466 wConvertDateTime
->setEnabled(true);
467 wConvertDateTime
->setText(tr("&to ASD", VER_MODULE_NAME_STR
));
471 void Module::epochChanged(QString
const & s
)
475 QRegExp
rx("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d{3})?$");
476 if (!rx
.exactMatch(s
))
479 QDateTime dt
= QDateTime::fromString(s
, Qt::ISODate
);
480 dt
.setTimeSpec(Qt::UTC
);
481 mDateTime
.setEpoch(dt
);
483 // If there is a valid entry, do the conversion
484 switch (mDateTime
.type())
486 case Internal::DateTime::ISO
:
487 case Internal::DateTime::ASD
:
489 wCucHex
->setText(mDateTime
.asCUChexString());
492 case Internal::DateTime::CUC
:
494 if (mLastDateTimeType
== Internal::DateTime::ASD
)
496 wDateTime
->setText(mDateTime
.asASDstring());
498 else if (mLastDateTimeType
== Internal::DateTime::ISO
)
500 wDateTime
->setText(mDateTime
.asISOstring());