diff --git a/ClockWidget.cxx b/ClockWidget.cxx
index 07b5e0f..6b23f80 100644
--- a/ClockWidget.cxx
+++ b/ClockWidget.cxx
@@ -1,236 +1,243 @@
/*
- Copyright 2017 Martin Koller, kollix@aon.at
+ Copyright 2017,2018 Martin Koller, kollix@aon.at
This file is part of liquidshell.
liquidshell is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
liquidshell is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with liquidshell. If not, see .
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//--------------------------------------------------------------------------------
CalendarPopup::CalendarPopup(QWidget *parent)
: QFrame(parent)
{
setWindowFlags(windowFlags() | Qt::Popup);
setFrameShape(QFrame::StyledPanel);
QVBoxLayout *vbox = new QVBoxLayout(this);
vbox->setContentsMargins(QMargins());
cal = new QCalendarWidget;
vbox->addWidget(cal);
QPushButton *today = new QPushButton(QIcon::fromTheme("go-jump-today"), QString());
vbox->addWidget(today);
connect(today, &QPushButton::clicked, this, &CalendarPopup::goToday);
}
//--------------------------------------------------------------------------------
void CalendarPopup::goToday()
{
cal->showToday();
cal->setSelectedDate(QDate::currentDate());
}
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
ClockWidget::ClockWidget(DesktopPanel *parent)
: QFrame(parent), calendar(nullptr)
{
+ ensurePolished(); // make sure we already have the css applied
+
timer = new QTimer(this);
- timer->setInterval(5000);
+
+ // if seconds are shown, update every second, else less often
+ if ( timeFormat.contains('s') )
+ timer->setInterval(1000);
+ else
+ timer->setInterval(5000);
+
timer->start();
connect(timer, &QTimer::timeout, this, &ClockWidget::tick);
connect(parent, &DesktopPanel::rowsChanged, this, &ClockWidget::fill);
timeLabel = new QLabel(this);
dayLabel = new QLabel(this);
dateLabel = new QLabel(this);
timeLabel->setObjectName("time");
dayLabel->setObjectName("day");
dateLabel->setObjectName("date");
timeLabel->setContentsMargins(QMargins(0, -5, 0, -5));
dayLabel->setContentsMargins(QMargins(0, -5, 0, -5));
dateLabel->setContentsMargins(QMargins(0, -5, 0, -5));
timeLabel->setAlignment(Qt::AlignCenter);
dayLabel->setAlignment(Qt::AlignCenter);
dateLabel->setAlignment(Qt::AlignCenter);
QFont f = font();
f.setPointSizeF(fontInfo().pointSizeF() * 1.5);
f.setBold(true);
timeLabel->setFont(f);
fill();
- ensurePolished(); // make sure we already have the css applied
timeLabel->setVisible(!timeFormat.isEmpty());
dayLabel->setVisible(!dayFormat.isEmpty());
dateLabel->setVisible(!dateFormat.isEmpty());
// context menu
QAction *action = new QAction(this);
action->setIcon(QIcon::fromTheme("configure"));
action->setText(i18n("Select Timezones..."));
addAction(action);
connect(action, &QAction::triggered,
[this]()
{
ClockWidgetConfigureDialog dialog(parentWidget(), timeZoneIds);
dialog.setWindowTitle(i18n("Select Timezones"));
dialog.resize(600, 400);
if ( dialog.exec() == QDialog::Accepted )
{
timeZoneIds = dialog.getSelectedTimeZoneIds();
KConfig config;
KConfigGroup group = config.group("ClockWidget");
QStringList list;
for (const QByteArray &id : timeZoneIds)
list.append(id);
group.writeEntry("timeZoneIds", list);
tick(); // update tooltip
}
}
);
action = new QAction(this);
action->setIcon(QIcon::fromTheme("preferences-system-time"));
action->setText(i18n("Configure Date & Time..."));
addAction(action);
connect(action, &QAction::triggered,
[this]()
{
auto dialog = new KCMultiDialog(parentWidget());
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->setWindowTitle(i18n("Date & Time"));
dialog->addModule("clock");
dialog->adjustSize();
dialog->show();
}
);
setContextMenuPolicy(Qt::ActionsContextMenu);
// load config
KConfig config;
KConfigGroup group = config.group("ClockWidget");
QStringList list;
list = group.readEntry("timeZoneIds", QStringList());
for (const QString &id : list)
timeZoneIds.append(id.toLatin1());
tick();
}
//--------------------------------------------------------------------------------
void ClockWidget::fill()
{
delete layout();
const int MAX_ROWS = qobject_cast(parentWidget())->getRows();
QBoxLayout *box;
if ( MAX_ROWS >= 2 )
{
box = new QVBoxLayout(this);
box->setSpacing(0);
}
else
{
box = new QHBoxLayout(this);
}
box->setContentsMargins(QMargins());
box->addWidget(timeLabel);
box->addWidget(dayLabel);
box->addWidget(dateLabel);
}
//--------------------------------------------------------------------------------
void ClockWidget::tick()
{
QDateTime dateTimeUtc = QDateTime::currentDateTimeUtc();
QDateTime dateTime = dateTimeUtc.toLocalTime();
timeLabel->setText(dateTime.time().toString(timeFormat));
dayLabel->setText(dateTime.date().toString(dayFormat));
dateLabel->setText(dateTime.date().toString(dateFormat));
if ( !timeZoneIds.isEmpty() )
{
QString tip = "";
for (const QByteArray &id : timeZoneIds)
{
QTimeZone timeZone(id);
QDateTime dt = dateTimeUtc;
dateTime = dt.toTimeZone(timeZone);
tip += QString("%1 | %2 | %3 | %4 |
")
.arg(QLatin1String(id))
.arg(dateTime.time().toString(timeFormat))
.arg(dateTime.date().toString(dayFormat))
.arg(dateTime.date().toString(dateFormat));
}
tip += "
";
setToolTip(tip);
}
}
//--------------------------------------------------------------------------------
void ClockWidget::mousePressEvent(QMouseEvent *event)
{
if ( event->button() == Qt::LeftButton )
{
if ( !calendar )
calendar = new CalendarPopup(this);
calendar->goToday();
QPoint point = mapToGlobal(pos());
QRect screen = QApplication::desktop()->availableGeometry(this);
point.setX(std::min(point.x(), screen.x() + screen.width() - calendar->sizeHint().width()));
point.setY(point.y() - calendar->sizeHint().height());
calendar->move(point);
calendar->show();
}
}
//--------------------------------------------------------------------------------