File: datewidget.cpp

package info (click to toggle)
psi-plugins 1.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 4,368 kB
  • sloc: cpp: 42,063; xml: 714; ansic: 84; makefile: 61; sh: 12
file content (111 lines) | stat: -rw-r--r-- 2,831 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <QKeyEvent>
#include <QLineEdit>
#include <QCalendarWidget>
#include <QIcon>
#include <QHBoxLayout>
#include <QRegExp>
#include <QRegExpValidator>
#include <QApplication>
#include <QDesktopWidget>
#include <QLocale>
#include <QDebug>
#include <QKeyEvent>

#include "datewidget.h"

DateWidget::DateWidget(QWidget *parent)
	: LineEditWidget(parent)
	, _tbCalendar(new QToolButton(this))
	, _tbClean(new QToolButton(this))
	, _calendar(new QCalendarWidget(this))
{
	setReadOnly(true);

	_tbClean->setObjectName("brClear");
	_tbClean->setIcon(QIcon(":/icons/clean.png"));
	_tbClean->setContentsMargins(0, 0, 0, 0);
	_tbClean->setFocusPolicy(Qt::NoFocus);
	_tbClean->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
	_tbClean->setIconSize(QSize(16, 16));
	_tbClean->setAutoRaise(true);
	_tbClean->setAutoFillBackground(true);
	_tbClean->setCursor(QCursor(Qt::ArrowCursor));
	_tbClean->resize(0, 0);
	addWidget(_tbClean);

	_tbCalendar->setObjectName("tbCalendar");
	_tbCalendar->setIcon(QIcon(":/icons/calendar.png"));
	_tbCalendar->setContentsMargins(0, 0, 0, 0);
	_tbCalendar->setFocusPolicy(Qt::NoFocus);
	_tbCalendar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
	_tbCalendar->setIconSize(QSize(16, 16));
	_tbCalendar->setAutoRaise(true);
	_tbCalendar->setAutoFillBackground(true);
	_tbCalendar->setCursor(QCursor(Qt::ArrowCursor));
	_tbCalendar->resize(0, 0);
	addWidget(_tbCalendar);

	setPopup(_calendar);

	connect(_calendar, SIGNAL(clicked(const QDate&)), SLOT(closeCalendar(const QDate&)));
	connect(_tbCalendar, SIGNAL(clicked()), SLOT(showPopup()));
	connect(_tbCalendar, SIGNAL(clicked()), SLOT(calendarSetDate()));

	connect(_tbClean, SIGNAL(clicked()), SLOT(disableExpiration()));
}

// Always use format of current locale
inline QString dateFormat()
{
	QString format = QLocale().dateFormat(QLocale::LongFormat);
#ifdef Q_OS_MAC
	// The LongFormat has changed between OS X 10.6 and 10.7.
	// https://qt.gitorious.org/qt/qtbase/commit/8e722eb/diffs
	// https://bugreports.qt-project.org/browse/QTBUG-27790
	if (format.count('y') == 1) {
		format.replace('y', "yyyy");
	}
#endif
	return format;
}

void DateWidget::setDate(const QDate &date)
{
	setText(date.toString(dateFormat()));
}

QDate DateWidget::date() const
{
	return QDate::fromString(text(), dateFormat());
}

void DateWidget::closeCalendar(const QDate &date)
{
	setDate(date);
	hidePopup();
}

void DateWidget::calendarSetDate()
{
	if(date().isValid()) {
		_calendar->setSelectedDate(date());
	}
}

void DateWidget::disableExpiration()
{
	setText(trUtf8("never"));
}

void DateWidget::keyPressEvent(QKeyEvent *event)
{
	if (event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) {
		disableExpiration();
	}
	else if (event->key() == Qt::Key_Space) {
		showPopup();
	}
	else {
		LineEditWidget::keyPressEvent(event);
	}
}