1 /* BEGIN_COMMON_COPYRIGHT_HEADER
2  * (c)LGPL2+
3  *
4  * LXQt - a lightweight, Qt based, desktop toolset
5  * https://lxqt.org
6  *
7  * Copyright: 2014 LXQt team
8  * Authors:
9  *   Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
10  *
11  * This program or library is free software; you can redistribute it
12  * and/or modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20 
21  * You should have received a copy of the GNU Lesser General
22  * Public License along with this library; if not, write to the
23  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24  * Boston, MA 02110-1301 USA
25  *
26  * END_COMMON_COPYRIGHT_HEADER */
27 
28 #include "datetime.h"
29 #include "ui_datetime.h"
30 #include <QTime>
31 #include <QTimer>
32 #include <QTextCharFormat>
33 
DateTimePage(bool useNtp,bool localRtc,QWidget * parent)34 DateTimePage::DateTimePage(bool useNtp, bool localRtc, QWidget *parent) :
35     QWidget(parent),
36     ui(new Ui::DateTime),
37     mUseNtp(useNtp),
38     mLocalRtc(localRtc)
39 {
40     ui->setupUi(this);
41     mTimer = new QTimer(this);
42     connect(mTimer, &QTimer::timeout, this, &DateTimePage::timeout);
43 
44     //highlight today
45     QDate date = QDate::currentDate();
46     QTextCharFormat format = ui->calendar->dateTextFormat(date);
47     QBrush brush;
48     brush.setColor(Qt::green);
49     format.setBackground(brush);
50     ui->calendar->setDateTextFormat(date,format);
51 
52     reload();
53 }
54 
~DateTimePage()55 DateTimePage::~DateTimePage()
56 {
57     delete ui;
58 }
59 
timeout()60 void DateTimePage::timeout()
61 {
62     ui->edit_time->blockSignals(true);
63     ui->edit_time->setTime(QTime::currentTime());
64     ui->edit_time->blockSignals(false);
65 }
66 
reload()67 void DateTimePage::reload()
68 {
69     ui->calendar->setSelectedDate(QDate::currentDate());
70     ui->edit_time->setTime(QTime::currentTime());
71 
72     ui->localRTC->setChecked(mLocalRtc);
73 #if !defined(NO_SYSTEMD) || defined(__FreeBSD__)
74     ui->ntp->setChecked(mUseNtp);
75 #else
76     ui->ntp->setChecked(false);
77     ui->ntp->setVisible(false);
78 #endif
79 
80     mTimer->start(1000);
81 
82     mModified = DateTimePage::ModifiedFlags();
83     emit changed();
84 }
85 
on_edit_time_userTimeChanged(const QTime &)86 void DateTimePage::on_edit_time_userTimeChanged(const QTime & /*time*/)
87 {
88     mModified |= M_TIME;
89     mTimer->stop();
90     emit changed();
91 }
92 
dateTime() const93 QDateTime DateTimePage::dateTime() const
94 {
95     QDateTime dt(ui->calendar->selectedDate(),ui->edit_time->time());
96     return dt;
97 }
98 
useNtp() const99 bool DateTimePage::useNtp() const
100 {
101     return ui->ntp->isChecked();
102 }
103 
localRtc() const104 bool DateTimePage::localRtc() const
105 {
106     return ui->localRTC->isChecked();
107 }
108 
on_calendar_selectionChanged()109 void DateTimePage::on_calendar_selectionChanged()
110 {
111     QDate date = ui->calendar->selectedDate();
112     if (date != QDate::currentDate())
113     {
114         mModified |= M_DATE;
115     }
116     else
117     {
118         mModified &= ~M_DATE;
119     }
120     emit changed();
121 }
122 
on_ntp_toggled(bool toggled)123 void DateTimePage::on_ntp_toggled(bool toggled)
124 {
125     if(toggled != mUseNtp)
126     {
127         mModified |= M_NTP;
128     }
129     else
130     {
131         mModified &= ~M_NTP;
132     }
133     emit changed();
134 }
135 
on_localRTC_toggled(bool toggled)136 void DateTimePage::on_localRTC_toggled(bool toggled)
137 {
138     if(toggled != mLocalRtc)
139     {
140         mModified |= M_LOCAL_RTC;
141     }
142     else
143     {
144         mModified &= ~M_LOCAL_RTC;
145     }
146     emit changed();
147 }
148 
149