2021-02-26 15:25:48 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copyright (C) 2021 Jon Beniston, M7RCE //
|
|
|
|
// //
|
|
|
|
// This program 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 as version 3 of the License, or //
|
|
|
|
// (at your option) any later version. //
|
|
|
|
// //
|
|
|
|
// This program 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 V3 for more details. //
|
|
|
|
// //
|
|
|
|
// You should have received a copy of the GNU General Public License //
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "wrappingdatetimeedit.h"
|
|
|
|
|
|
|
|
WrappingDateTimeEdit::WrappingDateTimeEdit(QWidget *parent) :
|
|
|
|
QDateTimeEdit(parent)
|
|
|
|
{
|
2021-10-12 06:31:14 -04:00
|
|
|
// We need to set this true in order for stepBy to be called
|
|
|
|
// when minutes are at 59
|
|
|
|
// However, this also causes dates/times to wrap from max to min
|
|
|
|
// which we don't want, so in stepBy, we clip to max/min ourselves
|
2021-02-26 15:25:48 -05:00
|
|
|
setWrapping(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WrappingDateTimeEdit::stepBy(int steps)
|
|
|
|
{
|
|
|
|
if (currentSection() == QDateTimeEdit::MonthSection)
|
2021-10-12 06:31:14 -04:00
|
|
|
{
|
|
|
|
clipAndSetDate(date().addMonths(steps));
|
|
|
|
}
|
2021-02-26 15:25:48 -05:00
|
|
|
else if (currentSection() == QDateTimeEdit::DaySection)
|
2021-10-12 06:31:14 -04:00
|
|
|
{
|
|
|
|
clipAndSetDate(date().addDays(steps));
|
|
|
|
}
|
2021-02-26 15:25:48 -05:00
|
|
|
else if (currentSection() == QDateTimeEdit::HourSection)
|
|
|
|
{
|
2021-10-12 06:31:14 -04:00
|
|
|
clipAndSetDateTime(dateTime().addSecs(steps*3600));
|
2021-02-26 15:25:48 -05:00
|
|
|
}
|
|
|
|
else if (currentSection() == QDateTimeEdit::MinuteSection)
|
|
|
|
{
|
2021-10-12 06:31:14 -04:00
|
|
|
clipAndSetDateTime(dateTime().addSecs(steps*60));
|
2021-02-26 15:25:48 -05:00
|
|
|
}
|
|
|
|
else if (currentSection() == QDateTimeEdit::SecondSection)
|
|
|
|
{
|
2021-10-12 06:31:14 -04:00
|
|
|
clipAndSetDateTime(dateTime().addSecs(steps));
|
|
|
|
}
|
|
|
|
}
|
2021-02-26 15:25:48 -05:00
|
|
|
|
2021-10-12 06:31:14 -04:00
|
|
|
void WrappingDateTimeEdit::clipAndSetDate(QDate date)
|
|
|
|
{
|
|
|
|
QDate max = maximumDate();
|
|
|
|
QDate min = minimumDate();
|
|
|
|
if (date > max) {
|
|
|
|
setDate(max);
|
|
|
|
} else if (date < min) {
|
|
|
|
setDate(min);
|
|
|
|
} else {
|
|
|
|
setDate(date);
|
|
|
|
}
|
|
|
|
}
|
2021-02-26 15:25:48 -05:00
|
|
|
|
2021-10-12 06:31:14 -04:00
|
|
|
void WrappingDateTimeEdit::clipAndSetDateTime(QDateTime dateTime)
|
|
|
|
{
|
|
|
|
// We have set wrapping as described in the constructor, but we don't want
|
|
|
|
// to wrap from max to min, so clip to this outself
|
|
|
|
QDateTime max = maximumDateTime();
|
|
|
|
QDateTime min = minimumDateTime();
|
|
|
|
if (dateTime > max) {
|
|
|
|
setDateTime(max);
|
|
|
|
} else if (dateTime < min) {
|
|
|
|
setDateTime(min);
|
|
|
|
} else {
|
|
|
|
setDateTime(dateTime);
|
2021-02-26 15:25:48 -05:00
|
|
|
}
|
|
|
|
}
|