Incidenceeditor

kweekdaycheckcombo.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Casey Link <unnamedrambler@gmail.com>
3 SPDX-FileCopyrightText: 2010 Bertjan Broeksema <broeksema@kde.org>
4 SPDX-FileCopyrightText: 2009-2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "kweekdaycheckcombo.h"
10
11#include <QLocale>
12
13using namespace IncidenceEditorNG;
14
16 : KCheckComboBox(parent)
17{
18 const int weekStart = QLocale().firstDayOfWeek();
20 for (int i = 0; i < 7; ++i) {
21 // i is the nr of the combobox, not the day of week!
22 const int dayOfWeek = (i + weekStart + 6) % 7;
23
24 const QString weekDayName = QLocale::system().dayName(dayOfWeek + 1, QLocale::ShortFormat);
25 addItem(weekDayName);
26 // by default Monday - Friday should be checked
27 // which corresponds to index 0 - 4;
28 if (first5Checked && dayOfWeek < 5) {
29 checkedItems << weekDayName;
30 }
31 }
32 if (first5Checked) {
34 }
35}
36
37KWeekdayCheckCombo::~KWeekdayCheckCombo() = default;
38
40{
41 QBitArray days(7);
42 const int weekStart = QLocale().firstDayOfWeek();
43
44 for (int i = 0; i < 7; ++i) {
45 // i is the nr of the combobox, not the day of week!
46 const int index = (1 + i + (7 - weekStart)) % 7;
48 }
49
50 return days;
51}
52
54{
55 if (!date.isValid()) {
56 return -1;
57 }
58 const int weekStart = QLocale().firstDayOfWeek();
59 const int dayOfWeek = date.dayOfWeek() - 1; // Values 1 - 7, we need 0 - 6
60
61 // qCDebug(INCIDENCEEDITOR_LOG) << "dayOfWeek = " << dayOfWeek << " weekStart = " << weekStart
62 // << "; result " << ( ( dayOfWeek + weekStart ) % 7 ) << "; date = " << date;
63 return (1 + dayOfWeek + (7 - weekStart)) % 7;
64}
65
66void KWeekdayCheckCombo::setDays(const QBitArray &days, const QBitArray &disableDays)
67{
68 Q_ASSERT(count() == 7); // The combobox must be filled.
69
70 QStringList checkedDays;
71 const int weekStart = QLocale().firstDayOfWeek();
72 for (int i = 0; i < 7; ++i) {
73 // i is the nr of the combobox, not the day of week!
74 const int index = (1 + i + (7 - weekStart)) % 7;
75
76 // qCDebug(INCIDENCEEDITOR_LOG) << "Checking for i = " << i << "; index = " << index << days.testBit( i );
77 // qCDebug(INCIDENCEEDITOR_LOG) << "Disabling? for i = " << i << "; index = " << index << !disableDays.testBit( i );
78
79 if (days.testBit(i)) {
80 checkedDays << itemText(index);
81 }
82 if (!disableDays.isEmpty()) {
83 setItemEnabled(index, !disableDays.testBit(i));
84 }
85 }
86 setCheckedItems(checkedDays);
87}
88
89#include "moc_kweekdaycheckcombo.cpp"
QBitArray days() const
Retrieve the checked days.
void setDays(const QBitArray &days, const QBitArray &disableDays=QBitArray())
Set the checked days on this combobox.
KWeekdayCheckCombo(QWidget *parent=nullptr, bool first5Checked=false)
int weekdayIndex(const QDate &date) const
Returns the index of the weekday represented by the QDate object.
void setItemEnabled(int index, bool enabled=true)
QStringList checkedItems(int role=Qt::DisplayRole) const
Qt::CheckState itemCheckState(int index) const
void setCheckedItems(const QStringList &items, int role=Qt::DisplayRole)
bool isEmpty() const const
void setBit(qsizetype i)
bool testBit(qsizetype i) const const
void addItem(const QIcon &icon, const QString &text, const QVariant &userData)
QString itemText(int index) const const
int dayOfWeek() const const
bool isValid(int year, int month, int day)
QString dayName(int day, FormatType type) const const
Qt::DayOfWeek firstDayOfWeek() const const
QLocale system()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:58:15 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.