00a3746b02c61053308b6fb2755c6e828e68f50d
[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   qDebug("QMaemo5WeekDaysPickWidget constructed");
70 }
71
72 void QMaemo5WeekDaysPickWidget::setSelected(QList<int> days) {
73   if(days.empty()) {
74     daysList->selectionModel()->select(daysList->model()->index(NEVER, 0), QItemSelectionModel::Select | QItemSelectionModel::Rows);
75     return;
76   }
77   daysList->selectionModel()->clearSelection();
78   qDebug("setSelected, %d items", days.size());
79   foreach(const int &d, days) {
80     daysList->selectionModel()->select(daysList->model()->index(d, 0), QItemSelectionModel::Select | QItemSelectionModel::Rows);
81     if(d == EVERY_DAY)
82       ensureConsistentSelection(daysList->model()->index(d, 0));
83   }
84 }
85
86 void QMaemo5WeekDaysPickWidget::emitSelectionAndClose() {
87   qDebug("in emitSelectionAndClose()");
88   QList<int> selected_rows;
89   QModelIndexList selected_indexes = daysList->selectionModel()->selectedRows();
90   foreach(QModelIndex index, selected_indexes) {
91     selected_rows << index.row();
92   }
93   emit selectedDays(selected_rows);
94   close();
95 }
96
97 void QMaemo5WeekDaysPickWidget::ensureConsistentSelection(QModelIndex index) {
98   qDebug("Received a click");
99   switch(index.row()) {
100   case NEVER:
101     if(!daysList->selectionModel()->isSelected(index)) {
102       // Prevent unselect
103       daysList->selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
104       return;
105     }
106     daysList->selectionModel()->clearSelection();
107     daysList->selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
108     break;
109   case EVERY_DAY:
110     if(!daysList->selectionModel()->isSelected(index)) {
111       // Prevent unselect
112       daysList->selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
113       return;
114     }
115     if(daysList->selectionModel()->isRowSelected(NEVER, daysList->rootIndex())) {
116       // Unselect NEVER item
117       daysList->selectionModel()->select(daysList->model()->index(NEVER, 0), QItemSelectionModel::Deselect | QItemSelectionModel::Rows);
118     }
119     // Select all days
120     for(int i=MON; i<EVERY_DAY; ++i) {
121       daysList->selectionModel()->select(daysList->model()->index(i, 0), QItemSelectionModel::Select | QItemSelectionModel::Rows);
122     }
123     break;
124   default:
125     if(daysList->selectionModel()->isRowSelected(NEVER, daysList->rootIndex())) {
126       // Unselect NEVER item
127       daysList->selectionModel()->select(daysList->model()->index(NEVER, 0), QItemSelectionModel::Deselect | QItemSelectionModel::Rows);
128     }
129     if(!daysList->selectionModel()->isSelected(index)) {
130       if(daysList->selectionModel()->isRowSelected(EVERY_DAY, daysList->rootIndex())) {
131         // A Work day was unselected, unselect EVERY_DAY item
132         daysList->selectionModel()->select(daysList->model()->index(EVERY_DAY, 0), QItemSelectionModel::Deselect | QItemSelectionModel::Rows);
133       }
134       if(!daysList->selectionModel()->hasSelection()) {
135         // Select NEVER item
136         daysList->selectionModel()->select(daysList->model()->index(NEVER, 0), QItemSelectionModel::Select | QItemSelectionModel::Rows);
137       }
138     }
139   }
140 }