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);
69 qDebug("QMaemo5WeekDaysPickWidget constructed");
72 void QMaemo5WeekDaysPickWidget::setSelected(QList<int> days) {
74 daysList->selectionModel()->select(daysList->model()->index(NEVER, 0), QItemSelectionModel::Select | QItemSelectionModel::Rows);
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);
82 ensureConsistentSelection(daysList->model()->index(d, 0));
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) {
93 const int row = index.row();
94 while(i < selected_rows.size() && selected_rows.at(i) < row) ++i;
95 selected_rows.insert(i, row);
97 emit selectedDays(selected_rows);
101 void QMaemo5WeekDaysPickWidget::ensureConsistentSelection(QModelIndex index) {
103 qDebug("Received a click");
104 switch(index.row()) {
106 if(!daysList->selectionModel()->isSelected(index)) {
108 daysList->selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
111 daysList->selectionModel()->clearSelection();
112 daysList->selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
115 if(!daysList->selectionModel()->isSelected(index)) {
117 daysList->selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
120 if(daysList->selectionModel()->isRowSelected(NEVER, daysList->rootIndex())) {
121 // Unselect NEVER item
122 daysList->selectionModel()->select(daysList->model()->index(NEVER, 0), QItemSelectionModel::Deselect | QItemSelectionModel::Rows);
125 for(int i=MON; i<EVERY_DAY; ++i) {
126 daysList->selectionModel()->select(daysList->model()->index(i, 0), QItemSelectionModel::Select | QItemSelectionModel::Rows);
130 if(daysList->selectionModel()->isRowSelected(NEVER, daysList->rootIndex())) {
131 // Unselect NEVER item
132 daysList->selectionModel()->select(daysList->model()->index(NEVER, 0), QItemSelectionModel::Deselect | QItemSelectionModel::Rows);
134 if(!daysList->selectionModel()->isSelected(index)) {
135 if(daysList->selectionModel()->isRowSelected(EVERY_DAY, daysList->rootIndex())) {
136 // A Work day was unselected, unselect EVERY_DAY item
137 daysList->selectionModel()->select(daysList->model()->index(EVERY_DAY, 0), QItemSelectionModel::Deselect | QItemSelectionModel::Rows);
139 if(!daysList->selectionModel()->hasSelection()) {
141 daysList->selectionModel()->select(daysList->model()->index(NEVER, 0), QItemSelectionModel::Select | QItemSelectionModel::Rows);
144 // If all days are selected, select EVERY_DAY
145 bool all_days_selected = true;
146 for(int i=MON; i<EVERY_DAY; ++i) {
147 if(!daysList->selectionModel()->isRowSelected(i, daysList->rootIndex())) {
148 all_days_selected = false;
152 if(all_days_selected)
153 daysList->selectionModel()->select(daysList->model()->index(EVERY_DAY, 0), QItemSelectionModel::Select | QItemSelectionModel::Rows);