2a40e523657f1b7bb0f4ccf58d1f13ac303e164a
[timedsilencer] / qmaemo5weekdayspickwidget.cpp
1 /*
2  * This file is part of TimedSilencer.
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 #include <QListView>
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"
29
30 QMaemo5WeekDaysPickWidget::QMaemo5WeekDaysPickWidget(QWidget *parent) :
31     QDialog(parent)
32 {
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)));
51   // Select NEVER item
52   daysList->selectionModel()->select(daysList->model()->index(NEVER, 0), QItemSelectionModel::Select | QItemSelectionModel::Rows);
53   // Height hint
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);
69 }
70
71 void QMaemo5WeekDaysPickWidget::setSelected(QList<int> days) {
72   if(days.empty()) {
73     daysList->selectionModel()->select(daysList->model()->index(NEVER, 0), QItemSelectionModel::Select | QItemSelectionModel::Rows);
74     return;
75   }
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);
80     if(d == EVERY_DAY)
81       ensureConsistentSelection(daysList->model()->index(d, 0));
82   }
83 }
84
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();
91   }
92   emit selectedDays(selected_rows);
93   close();
94 }
95
96 void QMaemo5WeekDaysPickWidget::ensureConsistentSelection(QModelIndex index) {
97   qDebug("Received a click");
98   switch(index.row()) {
99   case NEVER:
100     if(!daysList->selectionModel()->isSelected(index)) {
101       // Prevent unselect
102       daysList->selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
103       return;
104     }
105     daysList->selectionModel()->clearSelection();
106     daysList->selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
107     break;
108   case EVERY_DAY:
109     if(!daysList->selectionModel()->isSelected(index)) {
110       // Prevent unselect
111       daysList->selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
112       return;
113     }
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);
117     }
118     // Select all days
119     for(int i=MON; i<EVERY_DAY; ++i) {
120       daysList->selectionModel()->select(daysList->model()->index(i, 0), QItemSelectionModel::Select | QItemSelectionModel::Rows);
121     }
122     break;
123   default:
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);
127     }
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);
132       }
133       if(!daysList->selectionModel()->hasSelection()) {
134         // Select NEVER item
135         daysList->selectionModel()->select(daysList->model()->index(NEVER, 0), QItemSelectionModel::Select | QItemSelectionModel::Rows);
136       }
137     }
138   }
139 }