]>
vaikene.ee Git - evaf/blob - src/apps/ScosTime/gui.cpp
ed7f09841dbc2f0d0a729d50aa22b8e3490be1ee
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 if (mDateTime
.isValid())
96 qint64 diff
= mEpoch
.msecsTo(epoch
);
97 mDateTime
= mDateTime
.addMSecs(diff
);
103 Internal::DateTime::Type
Internal::DateTime::getDateTimeType(QString
const & s
) const
105 if (mRxIso
->exactMatch(s
))
109 else if (mRxAsd
->exactMatch(s
))
113 else if (mRxCUC
->exactMatch(s
))
120 QDateTime
Internal::DateTime::strToDateTime(QString
const & s
, Type type
) const
126 // Using the yyyy.ddd.hh.mm.ss[.zzz] format
127 QStringList tok
= s
.split(QChar('.'));
134 int year
= tok
.at(0).toInt(&ok
);
139 int days
= tok
.at(1).toInt(&ok
);
144 int hours
= tok
.at(2).toInt(&ok
);
149 int minutes
= tok
.at(3).toInt(&ok
);
158 secs
= tok
.at(4).toInt(&ok
);
165 msecs
= tok
.at(5).toInt(&ok
);
173 QDate
dt(year
, 1, 1);
174 dt
= dt
.addDays(days
- 1);
176 return QDateTime(dt
, QTime(hours
, minutes
, secs
, msecs
), Qt::UTC
);
181 // Using the ISO format yyyy-MM-ddThh:mm:ss[.zzz]
183 if (tmp
.length() < 19)
187 QDateTime dt
= QDateTime::fromString(tmp
, Qt::ISODate
);
188 dt
.setTimeSpec(Qt::UTC
);
194 // Get the CUC coarse and fine values
196 int coarse
= s
.left(8).toLong(&ok
, 16);
204 fine
= s
.mid(8, 4).toLong(&ok
, 16);
211 // Get the date/time value
212 QDateTime tm
= mEpoch
.addSecs(coarse
);
213 tm
= tm
.addMSecs(rint((double(fine
) / 58.0 * 885.0) / 1000.0));
225 QString
Internal::DateTime::asISOstring() const
227 if (mDateTime
.isValid())
229 return mDateTime
.toString("yyyy-MM-ddThh:mm:ss.zzz");
234 QString
Internal::DateTime::asASDstring() const
236 if (mDateTime
.isValid())
238 return QString("%1.%2.%3.%4.%5.%6")
239 .arg(mDateTime
.date().year(), 4, 10, QChar('0'))
240 .arg(mDateTime
.date().dayOfYear(), 3, 10, QChar('0'))
241 .arg(mDateTime
.time().hour(), 2, 10, QChar('0'))
242 .arg(mDateTime
.time().minute(), 2, 10, QChar('0'))
243 .arg(mDateTime
.time().second(), 2, 10, QChar('0'))
244 .arg(mDateTime
.time().msec(), 3, 10, QChar('0'));
249 QString
Internal::DateTime::asCUChexString() const
251 if (!mDateTime
.isValid())
256 // Convert to CUC coarse and fine values
257 qint64 msecs
= mEpoch
.msecsTo(mDateTime
);
258 quint32 coarse
= quint32(msecs
/ 1000);
259 quint32 fine
= quint32(rint(1000.0 * double(msecs
% 1000) * 58.0 / 885.0));
261 // Set the CUC hex string
262 return QString("%1%2").arg(coarse
, 8, 16, QChar('0')).arg(fine
, 4, 16, QChar('0'));
266 //-------------------------------------------------------------------
271 , mLastDateTimeType(Internal::DateTime::Invalid
)
273 setObjectName(QString("%1.%2").arg(VER_MODULE_NAME_STR
).arg(__FUNCTION__
));
275 EVAF_INFO("%s created", qPrintable(objectName()));
280 EVAF_INFO("%s destroyed", qPrintable(objectName()));
283 bool Module::init(QString
const & args
)
287 // Get the main window interface and fill it with the widgets
288 SdiWindow::iSdiWindow
* win
= evafQueryInterface
<SdiWindow::iSdiWindow
>("iSdiWindow");
289 EVAF_TEST_X(win
, "No iSdiWindow interface");
291 Gui::Panel
* panel
= new Gui::Panel
;
292 win
->addPanel("PswGen", panel
);
294 QVBoxLayout
* v
= new QVBoxLayout
;
297 QGridLayout
* g
= new QGridLayout
;
299 g
->setColumnStretch(1, 2);
301 QLabel
* l
= new QLabel(tr("&Epoch:", VER_MODULE_NAME_STR
));
302 l
->setAlignment(Qt::AlignRight
| Qt::AlignVCenter
);
303 g
->addWidget(l
, 0, 0);
305 wEpoch
= new QComboBox
;
307 wEpoch
->setEditable(true);
309 QRegExp
rx("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d{3})?$");
310 wEpoch
->setValidator(new QRegExpValidator(rx
, wEpoch
));
312 wEpoch
->addItems(QStringList() << "1970-01-01T00:00:00.000" << "1999-08-22T00:00:00.000");
313 connect(wEpoch
, SIGNAL(editTextChanged(QString
)), this, SLOT(epochChanged(QString
)));
314 g
->addWidget(wEpoch
, 0, 1);
315 panel
->setFocusProxy(wEpoch
);
317 l
= new QLabel(tr("&Date/time:", VER_MODULE_NAME_STR
));
318 l
->setAlignment(Qt::AlignRight
| Qt::AlignVCenter
);
319 g
->addWidget(l
, 1, 0);
321 wDateTime
= new QLineEdit
;
322 l
->setBuddy(wDateTime
);
324 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})?)$");
325 wDateTime
->setValidator(new QRegExpValidator(rx
, wDateTime
));
327 connect(wDateTime
, SIGNAL(textEdited(QString
)), this, SLOT(dateTimeEdited(QString
)));
328 g
->addWidget(wDateTime
, 1, 1);
330 QPushButton
* btn
= new QPushButton();
331 btn
->setDisabled(true);
332 g
->addWidget(btn
, 1, 2);
333 connect(btn
, SIGNAL(clicked()), this, SLOT(dateTimeClicked()));
334 wConvertDateTime
= btn
;
336 l
= new QLabel(tr("&CUC:", VER_MODULE_NAME_STR
));
337 l
->setAlignment(Qt::AlignRight
| Qt::AlignVCenter
);
338 g
->addWidget(l
, 2, 0);
340 wCucHex
= new QLineEdit
;
341 l
->setBuddy(wCucHex
);
342 wCucHex
->setMaxLength(12);
344 QRegExp
rx("^[0-9a-f]{8}([0-9a-f]{4})?$", Qt::CaseInsensitive
);
345 wCucHex
->setValidator(new QRegExpValidator(rx
, wCucHex
));
347 connect(wCucHex
, SIGNAL(textEdited(QString
)), this, SLOT(cucHexEdited(QString
)));
348 g
->addWidget(wCucHex
, 2, 1);
352 QHBoxLayout
* h
= new QHBoxLayout
;
356 QAction
* a
= new QAction(panel
);
357 a
->setShortcut(Qt::Key_Escape
);
358 connect(a
, SIGNAL(triggered()), qApp
, SLOT(quit()));
363 EVAF_INFO("%s initialized", qPrintable(objectName()));
372 EVAF_INFO("%s finalized", qPrintable(objectName()));
375 void Module::dateTimeClicked()
377 if (!mDateTime
.isValid())
382 // Convert to another type
383 switch (mLastDateTimeType
)
385 case Internal::DateTime::ISO
:
387 mLastDateTimeType
= Internal::DateTime::ASD
;
388 wDateTime
->setText(mDateTime
.asASDstring());
389 wConvertDateTime
->setText(tr("&to ISO", VER_MODULE_NAME_STR
));
392 case Internal::DateTime::ASD
:
394 mLastDateTimeType
= Internal::DateTime::ISO
;
395 wDateTime
->setText(mDateTime
.asISOstring());
396 wConvertDateTime
->setText(tr("&to ASD", VER_MODULE_NAME_STR
));
404 void Module::dateTimeEdited(QString
const & s
)
406 wConvertDateTime
->setDisabled(true);
407 wConvertDateTime
->setText(QString());
408 mLastDateTimeType
= Internal::DateTime::Invalid
;
415 mDateTime
.setDateTime(s
);
416 if (!mDateTime
.isValid())
421 mLastDateTimeType
= mDateTime
.type();
423 // Set the CUC hex string from this date/time string
424 wCucHex
->setText(mDateTime
.asCUChexString());
426 // Enable the button that converts between ISO and ASD date/time strings
427 if (mDateTime
.type() == Internal::DateTime::ISO
)
429 wConvertDateTime
->setEnabled(true);
430 wConvertDateTime
->setText(tr("&to ASD", VER_MODULE_NAME_STR
));
432 else if (mDateTime
.type() == Internal::DateTime::ISO
)
434 wConvertDateTime
->setEnabled(true);
435 wConvertDateTime
->setText(tr("&to ISO", VER_MODULE_NAME_STR
));
439 void Module::cucHexEdited(QString
const & s
)
441 if (s
.isEmpty() || s
.size() < 8)
446 mDateTime
.setDateTime(s
);
447 if (!mDateTime
.isValid())
452 if (mLastDateTimeType
== Internal::DateTime::Invalid
)
454 mLastDateTimeType
= Internal::DateTime::ASD
;
457 // Set the date/time string in the last used format
458 if (mLastDateTimeType
== Internal::DateTime::ASD
)
460 wDateTime
->setText(mDateTime
.asASDstring());
461 wConvertDateTime
->setEnabled(true);
462 wConvertDateTime
->setText(tr("&to ISO", VER_MODULE_NAME_STR
));
464 else if (mLastDateTimeType
== Internal::DateTime::ISO
)
466 wDateTime
->setText(mDateTime
.asISOstring());
467 wConvertDateTime
->setEnabled(true);
468 wConvertDateTime
->setText(tr("&to ASD", VER_MODULE_NAME_STR
));
472 void Module::epochChanged(QString
const & s
)
476 QRegExp
rx("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d{3})?$");
477 if (!rx
.exactMatch(s
))
480 QDateTime dt
= QDateTime::fromString(s
, Qt::ISODate
);
481 dt
.setTimeSpec(Qt::UTC
);
482 mDateTime
.setEpoch(dt
);
484 // If there is a valid entry, do the conversion
485 switch (mDateTime
.type())
487 case Internal::DateTime::ISO
:
488 case Internal::DateTime::ASD
:
490 wCucHex
->setText(mDateTime
.asCUChexString());
493 case Internal::DateTime::CUC
:
495 if (mLastDateTimeType
== Internal::DateTime::ASD
)
497 wDateTime
->setText(mDateTime
.asASDstring());
499 else if (mLastDateTimeType
== Internal::DateTime::ISO
)
501 wDateTime
->setText(mDateTime
.asISOstring());