2 * This file is part of TimedSilencer.
4 * TimedSilencer is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * TimedSilencer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with TimedSilencer. If not, see <http://www.gnu.org/licenses/>.
19 #include <QStandardItemModel>
20 #include <QHeaderView>
21 #include <QSpacerItem>
22 #include <QHBoxLayout>
23 #include <QVBoxLayout>
24 #include <QPushButton>
25 #include <QDialogButtonBox>
26 #include <QItemSelectionModel>
27 #include "qmaemo5weekdayspickwidget.h"
28 #include "checklistdelegate.h"
30 QMaemo5WeekDaysPickWidget::QMaemo5WeekDaysPickWidget(QWidget *parent) :
33 setAttribute(Qt::WA_DeleteOnClose);
34 setWindowTitle(tr("Repeat"));
35 QHBoxLayout *hLayout = new QHBoxLayout(this);
36 QVBoxLayout *vLayoutL = new QVBoxLayout;
37 QStandardItemModel *model = new QStandardItemModel(9, 1);
38 model->setItem(NEVER, 0, new QStandardItem(tr("Never")));
39 model->setItem(MON, 0, new QStandardItem(tr("Monday")));
40 model->setItem(TUE, 0, new QStandardItem(tr("Tuesday")));
41 model->setItem(WED, 0, new QStandardItem(tr("Wednesday")));
42 model->setItem(THU, 0, new QStandardItem(tr("Thursday")));
43 model->setItem(FRI, 0, new QStandardItem(tr("Friday")));
44 model->setItem(SAT, 0, new QStandardItem(tr("Saturday")));
45 model->setItem(SUN, 0, new QStandardItem(tr("Sunday")));
46 model->setItem(EVERY_DAY, 0, new QStandardItem(tr("Every day")));
47 daysList = new QListView;
48 daysList->setModel(model);
49 daysList->setItemDelegate(new CheckListDelegate);
50 connect(daysList, SIGNAL(activated(QModelIndex)), this, SLOT(ensureConsistentSelection(QModelIndex)));
52 daysList->selectionModel()->select(daysList->model()->index(NEVER, 0), QItemSelectionModel::Select | QItemSelectionModel::Rows);
54 if (daysList->sizeHintForRow(0)>0)
55 daysList->setMinimumHeight(daysList->sizeHintForRow(0) * 5);
56 daysList->setSelectionMode(QAbstractItemView::MultiSelection);
57 daysList->setSelectionBehavior(QAbstractItemView::SelectRows);
58 vLayoutL->addWidget(daysList);
59 hLayout->addLayout(vLayoutL);
60 QVBoxLayout *vLayoutR = new QVBoxLayout;
61 vLayoutR->addItem(new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding));
62 button_box = new QDialogButtonBox(Qt::Vertical);
63 QPushButton *done_btn = new QPushButton(tr("Done"));
64 connect(done_btn, SIGNAL(clicked()), this, SLOT(emitSelectionAndClose()));
65 done_btn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
66 button_box->addButton(done_btn, QDialogButtonBox::ActionRole);
67 vLayoutR->addWidget(button_box);
68 hLayout->addLayout(vLayoutR);
71 void QMaemo5WeekDaysPickWidget::setSelected(QList<int> days) {
73 daysList->selectionModel()->select(daysList->model()->index(NEVER, 0), QItemSelectionModel::Select | QItemSelectionModel::Rows);
76 daysList->selectionModel()->clearSelection();
77 qDebug("setSelected, %d items", days.size());
78 foreach(const int &d, days) {
79 daysList->selectionModel()->select(daysList->model()->index(d, 0), QItemSelectionModel::Select | QItemSelectionModel::Rows);
81 ensureConsistentSelection(daysList->model()->index(d, 0));
85 void QMaemo5WeekDaysPickWidget::emitSelectionAndClose() {
86 qDebug("in emitSelectionAndClose()");
87 QList<int> selected_rows;
88 QModelIndexList selected_indexes = daysList->selectionModel()->selectedRows();
89 foreach(QModelIndex index, selected_indexes) {
90 selected_rows << index.row();
92 emit selectedDays(selected_rows);
96 void QMaemo5WeekDaysPickWidget::ensureConsistentSelection(QModelIndex index) {
97 qDebug("Received a click");
100 if(!daysList->selectionModel()->isSelected(index)) {
102 daysList->selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
105 daysList->selectionModel()->clearSelection();
106 daysList->selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
109 if(!daysList->selectionModel()->isSelected(index)) {
111 daysList->selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
114 if(daysList->selectionModel()->isRowSelected(NEVER, daysList->rootIndex())) {
115 // Unselect NEVER item
116 daysList->selectionModel()->select(daysList->model()->index(NEVER, 0), QItemSelectionModel::Deselect | QItemSelectionModel::Rows);
119 for(int i=MON; i<EVERY_DAY; ++i) {
120 daysList->selectionModel()->select(daysList->model()->index(i, 0), QItemSelectionModel::Select | QItemSelectionModel::Rows);
124 if(daysList->selectionModel()->isRowSelected(NEVER, daysList->rootIndex())) {
125 // Unselect NEVER item
126 daysList->selectionModel()->select(daysList->model()->index(NEVER, 0), QItemSelectionModel::Deselect | QItemSelectionModel::Rows);
128 if(!daysList->selectionModel()->isSelected(index)) {
129 if(daysList->selectionModel()->isRowSelected(EVERY_DAY, daysList->rootIndex())) {
130 // A Work day was unselected, unselect EVERY_DAY item
131 daysList->selectionModel()->select(daysList->model()->index(EVERY_DAY, 0), QItemSelectionModel::Deselect | QItemSelectionModel::Rows);
133 if(!daysList->selectionModel()->hasSelection()) {
135 daysList->selectionModel()->select(daysList->model()->index(NEVER, 0), QItemSelectionModel::Select | QItemSelectionModel::Rows);