Commit 444f2526 authored by Volker Krause's avatar Volker Krause
Browse files

Use KDatePickerPopup from KWidgetAddons

parent 32edd671
Pipeline #177628 passed with stage
in 3 minutes and 14 seconds
......@@ -86,7 +86,6 @@ target_sources(KF5ContactEditor PRIVATE
editor/addresseditor/selectaddresstypecombobox.cpp
editor/personaleditor/personaleditorwidget.cpp
editor/personaleditor/dateeditwidget.cpp
editor/personaleditor/kdatepickerpopup.cpp
editor/businesseditor/businesseditorwidget.cpp
editor/businesseditor/freebusyeditwidget.cpp
editor/contacteditorwidget.cpp
......@@ -135,7 +134,6 @@ target_sources(KF5ContactEditor PRIVATE
editor/customfieldeditor/customfieldeditorwidget.h
editor/personaleditor/dateeditwidget.h
editor/personaleditor/personaleditorwidget.h
editor/personaleditor/kdatepickerpopup_p.h
editor/widgets/categorieseditabstractwidget.h
editor/widgets/imagewidget.h
editor/widgets/kwidgetlister_p.h
......
......@@ -8,9 +8,8 @@
#include "dateeditwidget.h"
#include "kdatepickerpopup_p.h"
#include <KDatePicker>
#include <KDatePickerPopup>
#include <KLocalizedString>
#include <QContextMenuEvent>
......
/*
This file is part of Contact Editor.
SPDX-FileCopyrightText: 2004 Bram Schoenmakers <bramschoenmakers@kde.nl>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "kdatepickerpopup_p.h"
#include <KDatePicker>
#include <KLocalizedString>
#include <QDateTime>
#include <QWidgetAction>
class KDatePickerAction : public QWidgetAction
{
Q_OBJECT
public:
KDatePickerAction(KDatePicker *widget, QObject *parent)
: QWidgetAction(parent)
, mDatePicker(widget)
, mOriginalParent(widget->parentWidget())
{
}
protected:
QWidget *createWidget(QWidget *parent) override
{
mDatePicker->setParent(parent);
return mDatePicker;
}
void deleteWidget(QWidget *widget) override
{
if (widget != mDatePicker) {
return;
}
mDatePicker->setParent(mOriginalParent);
}
private:
KDatePicker *const mDatePicker;
QWidget *const mOriginalParent;
};
KDatePickerPopup::KDatePickerPopup(Items items, const QDate &date, QWidget *parent)
: QMenu(parent)
{
mItems = items;
mDate = date;
mDatePicker = new KDatePicker(this);
mDatePicker->setCloseButton(false);
connect(mDatePicker, &KDatePicker::dateEntered, this, &KDatePickerPopup::slotDateChanged);
connect(mDatePicker, &KDatePicker::dateSelected, this, &KDatePickerPopup::slotDateChanged);
mDatePicker->setDate(date);
buildMenu();
}
void KDatePickerPopup::buildMenu()
{
if (isVisible()) {
return;
}
clear();
if (mItems & DatePicker) {
addAction(new KDatePickerAction(mDatePicker, this));
if ((mItems & NoDate) || (mItems & Words)) {
addSeparator();
}
}
if (mItems & Words) {
addAction(i18nc("@option today", "&Today"), this, [this]() {
slotToday();
});
addAction(i18nc("@option tomorrow", "To&morrow"), this, [this]() {
slotTomorrow();
});
addAction(i18nc("@option next week", "Next &Week"), this, [this]() {
slotNextWeek();
});
addAction(i18nc("@option next month", "Next M&onth"), this, [this]() {
slotNextMonth();
});
if (mItems & NoDate) {
addSeparator();
}
}
if (mItems & NoDate) {
addAction(i18nc("@option do not specify a date", "No Date"), this, [this]() {
slotNoDate();
});
}
}
KDatePicker *KDatePickerPopup::datePicker() const
{
return mDatePicker;
}
void KDatePickerPopup::setDate(const QDate &date)
{
mDatePicker->setDate(date);
}
void KDatePickerPopup::slotDateChanged(const QDate &date)
{
if (date != mDate) {
Q_EMIT dateChanged(date);
}
hide();
}
void KDatePickerPopup::slotToday()
{
Q_EMIT dateChanged(QDate::currentDate());
}
void KDatePickerPopup::slotTomorrow()
{
Q_EMIT dateChanged(QDate::currentDate().addDays(1));
}
void KDatePickerPopup::slotNoDate()
{
Q_EMIT dateChanged(QDate());
}
void KDatePickerPopup::slotNextWeek()
{
Q_EMIT dateChanged(QDate::currentDate().addDays(7));
}
void KDatePickerPopup::slotNextMonth()
{
Q_EMIT dateChanged(QDate::currentDate().addMonths(1));
}
#include "kdatepickerpopup.moc"
#include "moc_kdatepickerpopup_p.cpp"
/*
This file is part of Contact Editor.
SPDX-FileCopyrightText: 2004 Bram Schoenmakers <bramschoenmakers@kde.nl>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#pragma once
#include <QDateTime>
#include <QMenu>
class KDatePicker;
/**
@short This menu helps the user to select a date quickly.
This menu helps the user to select a date quickly. It offers various
ways of selecting, e.g. with a KDatePicker or with words like "Tomorrow".
The available items are:
@li NoDate: A menu-item with "No Date". If chosen, the datepicker will emit
a null QDate.
@li DatePicker: Show a KDatePicker-widget.
@li Words: Show items like "Today", "Tomorrow" or "Next Week".
When supplying multiple items, separate each item with a bitwise OR.
@author Bram Schoenmakers <bram_s@softhome.net>
*/
class KDatePickerPopup : public QMenu
{
Q_OBJECT
public:
enum ItemFlag { NoDate = 1, DatePicker = 2, Words = 4 };
Q_DECLARE_FLAGS(Items, ItemFlag)
/**
A constructor for the KDatePickerPopup.
@param items List of all desirable items, separated with a bitwise OR.
@param date Initial date of datepicker-widget.
@param parent The object's parent.
*/
explicit KDatePickerPopup(Items items = DatePicker, const QDate &date = QDate::currentDate(), QWidget *parent = nullptr);
/**
@return A pointer to the private variable mDatePicker, an instance of
KDatePicker.
*/
KDatePicker *datePicker() const;
void setDate(const QDate &date);
/** @return Returns the bitwise result of the active items in the popup. */
int items() const
{
return mItems;
}
Q_SIGNALS:
/**
This signal emits the new date (selected with datepicker or other
menu-items).
@param date changed date
*/
void dateChanged(const QDate &date);
protected Q_SLOTS:
void slotDateChanged(const QDate &date);
void slotToday();
void slotTomorrow();
void slotNextWeek();
void slotNextMonth();
void slotNoDate();
private:
void buildMenu();
KDatePicker *mDatePicker = nullptr;
Items mItems;
QDate mDate;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(KDatePickerPopup::Items)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment