Warning, file /office/skrooge/skgbasegui/skgsimpleperiodedit.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /***************************************************************************
0002  * SPDX-FileCopyrightText: 2022 S. MANKOWSKI stephane@mankowski.fr
0003  * SPDX-FileCopyrightText: 2022 G. DE BURE support@mankowski.fr
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  ***************************************************************************/
0006 /** @file
0007  * A simple period selector.
0008  *
0009  * @author Stephane MANKOWSKI / Guillaume DE BURE
0010  */
0011 #include "skgsimpleperiodedit.h"
0012 #include <skgservices.h>
0013 
0014 
0015 SKGSimplePeriodEdit::SKGSimplePeriodEdit(QWidget* iParent)
0016     : SKGComboBox(iParent), m_Mode(PREVIOUS_MONTHS) {}
0017 
0018 SKGSimplePeriodEdit::~SKGSimplePeriodEdit() = default;
0019 
0020 QDate SKGSimplePeriodEdit::firstDate() const
0021 {
0022     return m_FirstDate;
0023 }
0024 
0025 void SKGSimplePeriodEdit::setFirstDate(QDate iDate)
0026 {
0027     if (m_FirstDate != iDate) {
0028         m_FirstDate = iDate;
0029 
0030         refreshList();
0031         emit changed();
0032     }
0033 }
0034 SKGSimplePeriodEdit::Modes SKGSimplePeriodEdit::mode() const
0035 {
0036     return m_Mode;
0037 }
0038 void SKGSimplePeriodEdit::setMode(SKGSimplePeriodEdit::Modes iMode)
0039 {
0040     if (m_Mode != iMode) {
0041         m_Mode = iMode;
0042 
0043         refreshList();
0044         emit changed();
0045     }
0046 }
0047 
0048 QString SKGSimplePeriodEdit::period() const
0049 {
0050     QString period = currentData().toString();
0051     if (period.isEmpty()) {
0052         // Specific month
0053         period = text();
0054     }
0055     return period;
0056 }
0057 
0058 void SKGSimplePeriodEdit::refreshList()
0059 {
0060     QDate today = QDate::currentDate();
0061     QDate c = SKGServices::periodToDate(SKGServices::dateToPeriod(m_FirstDate, QStringLiteral("M")));
0062     if (!c.isValid()) {
0063         c = today;
0064     }
0065     c = c.addDays(1 - c.day());
0066 
0067     QString smonth = SKGServices::dateToPeriod(today, QStringLiteral("M"));
0068     QString squarter = SKGServices::dateToPeriod(today, QStringLiteral("Q"));
0069     QString ssemester = SKGServices::dateToPeriod(today, QStringLiteral("S"));
0070     QString syear = SKGServices::dateToPeriod(today, QStringLiteral("Y"));
0071 
0072     // Build the list
0073     QStringList list;
0074     do {
0075         QString cmonth = SKGServices::dateToPeriod(c, QStringLiteral("M"));
0076         QString cquarter = SKGServices::dateToPeriod(c, QStringLiteral("Q"));
0077         QString csemester = SKGServices::dateToPeriod(c, QStringLiteral("S"));
0078         QString cyear = SKGServices::dateToPeriod(c, QStringLiteral("Y"));
0079         if ((cmonth != smonth && (((m_Mode & PREVIOUS_MONTHS)) != 0u)) ||
0080             (cmonth == smonth && (((m_Mode & CURRENT_MONTH)) != 0u))) {
0081             list.insert(0, cmonth);
0082         }
0083         if (!list.contains(cquarter) && ((cquarter != squarter && (((m_Mode & PREVIOUS_PERIODS)) != 0u)) ||
0084                                          (cquarter == squarter && (((m_Mode & CURRENT_PERIOD)) != 0u)))) {
0085             list.insert(0, cquarter);
0086         }
0087         if (!list.contains(csemester) && ((csemester != ssemester && (((m_Mode & PREVIOUS_PERIODS)) != 0u)) ||
0088                                           (csemester == ssemester && (((m_Mode & CURRENT_PERIOD)) != 0u)))) {
0089             list.insert(0, csemester);
0090         }
0091         if (!list.contains(cyear) && ((cyear != syear && ((((m_Mode & PREVIOUS_PERIODS)) != 0u) || (((m_Mode & PREVIOUS_YEARS)) != 0u))) ||
0092                                       (cyear == syear && ((((m_Mode & CURRENT_PERIOD)) != 0u) || (((m_Mode & CURRENT_YEAR)) != 0u))))) {
0093             list.insert(0, cyear);
0094         }
0095         if (cmonth == smonth || c >= today) {
0096             break;
0097         }
0098         c = c.addMonths(1);
0099     } while (true);
0100 
0101     // Set the list and the current item
0102     QString current = text();
0103     bool previous = blockSignals(true);
0104     clear();
0105 
0106     // Add dynamic items
0107     if (list.contains(smonth) && (((m_Mode & ALL)) != 0u)) {
0108         addItem(i18nc("A period including all dates", "All dates"), "ALL");
0109     }
0110 
0111     if (list.contains(smonth)) {
0112         addItem(i18nc("The current month", "Current month"), smonth);
0113     }
0114 
0115     if (list.contains(squarter)) {
0116         addItem(i18nc("The current quarter", "Current quarter"), squarter);
0117     }
0118 
0119     if (list.contains(ssemester)) {
0120         addItem(i18nc("The current semester", "Current semester"), ssemester);
0121     }
0122 
0123     if (list.contains(syear)) {
0124         addItem(i18nc("The current year", "Current year"), syear);
0125     }
0126 
0127     QString period = SKGServices::getNeighboringPeriod(smonth);
0128     if (list.contains(period)) {
0129         addItem(i18nc("The month before the current month", "Last month"), period);
0130     }
0131 
0132     period = SKGServices::getNeighboringPeriod(squarter);
0133     if (list.contains(period)) {
0134         addItem(i18nc("The quarter before the current quarter", "Last quarter"), period);
0135     }
0136 
0137     period = SKGServices::getNeighboringPeriod(ssemester);
0138     if (list.contains(period)) {
0139         addItem(i18nc("The semester before the current semester", "Last semester"), period);
0140     }
0141 
0142     period = SKGServices::getNeighboringPeriod(syear);
0143     if (list.contains(period)) {
0144         addItem(i18nc("The year before the current year", "Last year"), period);
0145     }
0146     addItems(list);
0147 
0148     if (!current.isEmpty()) {
0149         setText(current);
0150     } else {
0151         setCurrentIndex(0);
0152     }
0153     blockSignals(previous);
0154 }
0155