File indexing completed on 2024-04-28 05:11:37

0001 /*
0002     SPDX-FileCopyrightText: 2010 Casey Link <unnamedrambler@gmail.com>
0003     SPDX-FileCopyrightText: 2010 Bertjan Broeksema <broeksema@kde.org>
0004     SPDX-FileCopyrightText: 2009-2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "kweekdaycheckcombo.h"
0010 
0011 #include <QLocale>
0012 
0013 using namespace IncidenceEditorNG;
0014 
0015 KWeekdayCheckCombo::KWeekdayCheckCombo(QWidget *parent, bool first5Checked)
0016     : KCheckComboBox(parent)
0017 {
0018     const int weekStart = QLocale().firstDayOfWeek();
0019     QStringList checkedItems;
0020     for (int i = 0; i < 7; ++i) {
0021         // i is the nr of the combobox, not the day of week!
0022         const int dayOfWeek = (i + weekStart + 6) % 7;
0023 
0024         const QString weekDayName = QLocale::system().dayName(dayOfWeek + 1, QLocale::ShortFormat);
0025         addItem(weekDayName);
0026         // by default Monday - Friday should be checked
0027         // which corresponds to index 0 - 4;
0028         if (first5Checked && dayOfWeek < 5) {
0029             checkedItems << weekDayName;
0030         }
0031     }
0032     if (first5Checked) {
0033         setCheckedItems(checkedItems);
0034     }
0035 }
0036 
0037 KWeekdayCheckCombo::~KWeekdayCheckCombo() = default;
0038 
0039 QBitArray KWeekdayCheckCombo::days() const
0040 {
0041     QBitArray days(7);
0042     const int weekStart = QLocale().firstDayOfWeek();
0043 
0044     for (int i = 0; i < 7; ++i) {
0045         // i is the nr of the combobox, not the day of week!
0046         const int index = (1 + i + (7 - weekStart)) % 7;
0047         days.setBit(i, itemCheckState(index) == Qt::Checked);
0048     }
0049 
0050     return days;
0051 }
0052 
0053 int KWeekdayCheckCombo::weekdayIndex(const QDate &date) const
0054 {
0055     if (!date.isValid()) {
0056         return -1;
0057     }
0058     const int weekStart = QLocale().firstDayOfWeek();
0059     const int dayOfWeek = date.dayOfWeek() - 1; // Values 1 - 7, we need 0 - 6
0060 
0061     // qCDebug(INCIDENCEEDITOR_LOG) << "dayOfWeek = " << dayOfWeek << " weekStart = " << weekStart
0062     // << "; result " << ( ( dayOfWeek + weekStart ) % 7 ) << "; date = " << date;
0063     return (1 + dayOfWeek + (7 - weekStart)) % 7;
0064 }
0065 
0066 void KWeekdayCheckCombo::setDays(const QBitArray &days, const QBitArray &disableDays)
0067 {
0068     Q_ASSERT(count() == 7); // The combobox must be filled.
0069 
0070     QStringList checkedDays;
0071     const int weekStart = QLocale().firstDayOfWeek();
0072     for (int i = 0; i < 7; ++i) {
0073         // i is the nr of the combobox, not the day of week!
0074         const int index = (1 + i + (7 - weekStart)) % 7;
0075 
0076         // qCDebug(INCIDENCEEDITOR_LOG) << "Checking for i = " << i << "; index = " << index << days.testBit( i );
0077         // qCDebug(INCIDENCEEDITOR_LOG) << "Disabling? for i = " << i << "; index = " << index << !disableDays.testBit( i );
0078 
0079         if (days.testBit(i)) {
0080             checkedDays << itemText(index);
0081         }
0082         if (!disableDays.isEmpty()) {
0083             setItemEnabled(index, !disableDays.testBit(i));
0084         }
0085     }
0086     setCheckedItems(checkedDays);
0087 }
0088 
0089 #include "moc_kweekdaycheckcombo.cpp"