Skip to content

Commit

Permalink
Merge pull request #2 from mohammadhasanzadeh/select_range
Browse files Browse the repository at this point in the history
Added select_range function to the RangeCalendarDialog
  • Loading branch information
mohammadhasanzadeh authored Oct 16, 2021
2 parents 64efb62 + 27fdb9d commit 2b0a0d6
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 1 deletion.
24 changes: 24 additions & 0 deletions RangeCalendarDialog.qml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,29 @@ Dialog
);
}

function select_range(start_date, end_date)
{
if (!UTIL.is_date_valid(start_date, control.system))
{
throw "Not a valid date.";
}

if (!UTIL.is_date_valid(end_date, control.system))
{
throw "Not a valid date.";
}

start_date.index = control.model.index_of(start_date.year, start_date.month);
private_props.m_start_date = start_date;
private_props.m_start_dateChanged();

end_date.index = control.model.index_of(end_date.year, end_date.month);
private_props.m_end_date = end_date;
private_props.m_end_dateChanged();
private_props.select_flag = false;
listview.currentIndex = end_date.index;
}

onLocaleChanged:
{
control.day_headers = UTIL.get_day_headers(control.locale);
Expand Down Expand Up @@ -292,6 +315,7 @@ Dialog
snapMode: ListView.SnapOneItem
highlightRangeMode: ListView.StrictlyEnforceRange
model: control.opened ? control.model.model : undefined
highlightMoveDuration: 500

onModelChanged:
{
Expand Down
11 changes: 10 additions & 1 deletion examples/DateRangePickerPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ Item
text: "Open Calendar"
Layout.preferredWidth: 200
Layout.alignment: Qt.AlignHCenter
onClicked: {
onClicked:
{
range_dialog.open()
}
}
Expand Down Expand Up @@ -89,6 +90,14 @@ Item
locale: range_dialog.locale
}

QuarterRangeAccess
{
parent: range_dialog.contentItem.parent
target: range_dialog
anchors.right: parent.right
anchors.top: parent.top
}

onFinished:
{
if (result === CalendarDialog.Accepted)
Expand Down
89 changes: 89 additions & 0 deletions examples/QuarterRangeAccess.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls.Material 2.15

import yacalendar 1.0

Item
{
id: control
width: row.width
height: 50
z: 1000

property RangeCalendarDialog target: parent
property int selected_quarter: -1

enum Range {
Q1 = 1,
Q2 = 2,
Q3 = 3,
Q4 = 4
}

// q represent of Quarter of year
function calculate_range(q)
{
const current_year = target.system.today().year;
const end_month = calculate_end_month(q);
const start_date = {
year: current_year,
month: end_month - 2,
day: 1
};

const end_date = {
year: current_year,
month: end_month,
day: target.system.days_in_month(end_month, current_year)
};
target.select_range(start_date, end_date);
}

// return end_month based on quarter of year
function calculate_end_month(q) {
switch(q)
{
case QuarterRangeAccess.Range.Q1:
return 3;
case QuarterRangeAccess.Range.Q2:
return 6;
case QuarterRangeAccess.Range.Q3:
return 9;
case QuarterRangeAccess.Range.Q4:
return 12;
default:
throw 'is not valid value for quarter!\nA quarter refers to one-fourth of a year.';
}
}

function quarter_none()
{
control.selected_quarter = -1
}

Row
{
id: row

Repeater
{
id: repeater
model: ["Q1", "Q2", "Q3", "Q4"]
delegate: RoundButton {
width: 50
height: 48
text: modelData
Material.background: Material.color(index, Material.Shade200)
highlighted: true
flat: false
onClicked:
{
calculate_range(index + 1);
control.selected_quarter = index;
}
}
}
}
}
1 change: 1 addition & 0 deletions examples/qml.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
<file>resource/palette.svg</file>
<file>YearMonthPage.qml</file>
<file>DateRangePickerPage.qml</file>
<file>QuarterRangeAccess.qml</file>
</qresource>
</RCC>
13 changes: 13 additions & 0 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,16 @@ function is_lower_equal_than(system, first_date, second_date)
const qdate_second_date = new Date(temp.year, temp.month, temp.day);
return (qdate_first_date <= qdate_second_date);
}

function is_valid_date_object(date)
{
return ("year" in date && "month" in date && "day" in date);
}

function is_date_valid(date, system)
{
if (!is_valid_date_object(date))
return false;

return system.is_date_valid(date.year, date.month, date.day);
}
5 changes: 5 additions & 0 deletions yacalendar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ bool yacalendar::is_between(const QDate &source_date, const QDate &from_date, co
return (source_date >= from_date && source_date <= to_date);
}

bool yacalendar::is_date_valid(int year, int month, int day) const
{
return m_calendar.isDateValid(year, month, day);
}

QString yacalendar::to_gregorian(const QString& date, QChar in_separator, QString out_format) const
{
QStringList year_month_day = date.split(in_separator);
Expand Down
1 change: 1 addition & 0 deletions yacalendar.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class yacalendar : public QObject
Q_INVOKABLE QVariantMap add_month(int year, int month, int day, int n_month);
Q_INVOKABLE QVariantMap add_days(int year, int month, int day, int n_day);
Q_INVOKABLE bool is_between(const QDate& source_date, const QDate& from_date, const QDate& to_date, bool by_boundaries = true);
Q_INVOKABLE bool is_date_valid(int year, int month, int day) const;

Q_PROPERTY(CalendarTypes type READ get_type WRITE set_type NOTIFY type_changed)
Q_PROPERTY(QLocale locale MEMBER m_locale NOTIFY locale_changed)
Expand Down

0 comments on commit 2b0a0d6

Please sign in to comment.