WSL/SLF GitLab Repository

Datepicker.cc 6.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*****************************************************************************/
/*  Copyright 2020 ALPsolut.eu                                               */
/*****************************************************************************/
/* This file is part of INIshell.
   INIshell 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.

   INIshell 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 INIshell.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "Datepicker.h"
#include "Label.h"
21
#include "src/main/colors.h"
22
23
24
25
26

#ifdef DEBUG
	#include <iostream>
#endif //def DEBUG

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
 * @class DateKeyPressFilter
 * @brief Key press event listener for the Datepicker panel.
 * @details We can not override 'event' in the panel itself because we want to
 * listen to the events of a child widget.
 * @param[in] object Object the event stems from (the QDateTimeEdit).
 * @param[in] event The type of event.
 * @return True if the event was accepted.
 */
bool DateKeyPressFilter::eventFilter(QObject *object, QEvent *event)
{
	if (event->type() == QEvent::KeyPress) {
		const QKeyEvent *key_event = static_cast<QKeyEvent *>(event);
		if (key_event->key() != Qt::Key_Tab) {
			if (object->property("empty").toBool()) {
				/*
				 * A Datepicker panel's value may be hidden to mark the panel as not set.
				 * If a user starts entering something we reveal the value as the current date.
				 */
				object->setProperty("empty", "false");
				auto *panel = qobject_cast<QDateTimeEdit*>(object);
				panel->parent()->setProperty("ini_value", QDateTime::currentDateTime().toString());
				panel->style()->unpolish(panel);
				panel->style()->polish(panel);
			} //endif property
		} //endif key_event
	}
	return QObject::eventFilter(object, event); //pass to actual event of the object
}

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
 * @class Datepicker
 * @brief Default constructor for a Datepicker.
 * @details A Datepicker is used to select/enter a date and time.
 * @param[in] key INI key corresponding to the value that is being controlled by this Datepicker.
 * @param[in] options XML node responsible for this panel with all options and children.
 * @param[in] no_spacers Keep a tight layout for this panel.
 * @param[in] parent The parent widget.
 */
Datepicker::Datepicker(const QString &section, const QString &key, const QDomNode &options, const bool &no_spacers,
    QWidget *parent) : Atomic(section, key, parent)
{
	/* label and text box */
	auto *key_label( new Label(QString(), QString(), options, no_spacers, key_, this) );
	datepicker_ = new QDateTimeEdit; 
	setPrimaryWidget(datepicker_);
	datepicker_->setCalendarPopup(true);
	datepicker_->setToolTip(tr("Pick a date/time"));
75
	QTimer::singleShot(1, this, [=]{ setEmpty(true); });
76
77
	connect(datepicker_, &QDateTimeEdit::dateTimeChanged, this, &Datepicker::checkValue);

78
79
80
	key_filter_ = new DateKeyPressFilter;
	datepicker_->installEventFilter(key_filter_);

81
	/* main layout */
82
83
84
85
86
87
88
89
90
91
92
	auto *datepicker_layout( new QHBoxLayout );
	setLayoutMargins(datepicker_layout);
	datepicker_layout->addWidget(key_label, 0, Qt::AlignLeft);
	datepicker_layout->addWidget(datepicker_, 0, Qt::AlignLeft);
	if (!no_spacers)
		datepicker_layout->addSpacerItem(buildSpacer());
	addHelp(datepicker_layout, options, no_spacers);
	this->setLayout(datepicker_layout);
	setOptions(options);
}

93
94
95
96
97
98
99
100
/**
 * @brief The destructor with minimal cleanup.
 */
Datepicker::~Datepicker()
{
	delete key_filter_;
}

101
102
103
104
105
106
/**
 * @brief Parse options for a Datepicker from XML.
 * @param[in] options XML node holding the Datepicker.
 */
void Datepicker::setOptions(const QDomNode &options)
{
107
	const QString user_date_format( options.toElement().attribute("format") );
108
109
110
	if (!user_date_format.isEmpty())
		date_format_ = user_date_format;
	datepicker_->setDisplayFormat(date_format_);
111
112
113
114
115
116
117

	/* allow to set "empty" via the property system */
	QString bg_color( colors::getQColor("app_bg").name() );
	//find font color to use for hidden datepicker dependent on background color:
	if (options.toElement().attribute("optional").toLower() == "false")
		bg_color = colors::getQColor("mandatory").name();
	datepicker_->setStyleSheet("* [empty=\"true\"] {color: " + bg_color + "}");
118
119
120
121
122
123
124
125
126
127
128
129
}

/**
 * @brief Event listener for changed INI values.
 * @details The "ini_value" property is set when parsing default values and potentially again
 * when setting INI keys while parsing a file.
 */
void Datepicker::onPropertySet()
{
	const QString text_to_set( this->property("ini_value").toString() );
	if (ini_value_ == text_to_set)
		return;
130
131
132

	/*
	 * In checkValue() we always receive a date because the widget can't be empty.
133
134
	 * So we catch this case early here (where an empty value can come from XML
	 * for example) and style "empty" manually.
135
136
137
138
139
140
141
	 */
	if (text_to_set.isEmpty()) {
		QTimer::singleShot(1, this, [=]{ setEmpty(true); });
		setIniValue(QString());
		setDefaultPanelStyles(QString());
		return;
	}
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
	
	QDateTime dt( QDateTime::fromString(text_to_set, date_format_) );
	if (!dt.isValid()) {
		dt.setDate(QDate::currentDate());
		dt.setTime(QTime()); //round to start of current day
	}

	datepicker_->setDateTime(dt);
	checkValue(dt);
}

/**
 * @brief Sets the entered date to the ini.
 * @param[in] text The current text to check.
 */
void Datepicker::checkValue(const QDateTime &datetime)
{
	const QString date_text( datetime.toString(date_format_) ); //retrieve in user format
	setDefaultPanelStyles(date_text);
	setIniValue(date_text);
162
	QTimer::singleShot(1, this, [=]{ setEmpty(false); });
163
164
}

165
166
167
168
169
170
171
172
173
174
175
/**
 * @brief Set an empty value for the QDateTimeEdit.
 * @details See the same function in Number.cc.
 * @param[in] is_empty Hide text if true, show if false.
 */
void Datepicker::setEmpty(const bool &is_empty)
{
	datepicker_->setProperty("empty", is_empty);
	this->style()->unpolish(datepicker_);
	this->style()->polish(datepicker_);
}