Added spanish translation
[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     // Insert sort
92     int i = 0;
93     const int row = index.row();
94     while(i < selected_rows.size() && selected_rows.at(i) < row) ++i;
95     selected_rows.insert(i, row);
96   }
97   emit selectedDays(selected_rows);
98   close();
99 }
100
101 void QMaemo5WeekDaysPickWidget::ensureConsistentSelection(QModelIndex index) {
102
103   qDebug("Received a click");
104   switch(index.row()) {
105   case NEVER:
106     if(!daysList->selectionModel()->isSelected(index)) {
107       // Prevent unselect
108       daysList->selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
109       return;
110     }
111     daysList->selectionModel()->clearSelection();
112     daysList->selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
113     break;
114   case EVERY_DAY:
115     if(!daysList->selectionModel()->isSelected(index)) {
116       // Prevent unselect
117       daysList->selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
118       return;
119     }
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);
123     }
124     // Select all days
125     for(int i=MON; i<EVERY_DAY; ++i) {
126       daysList->selectionModel()->select(daysList->model()->index(i, 0), QItemSelectionModel::Select | QItemSelectionModel::Rows);
127     }
128     break;
129   default:
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);
133     }
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);
138       }
139       if(!daysList->selectionModel()->hasSelection()) {
140         // Select NEVER item
141         daysList->selectionModel()->select(daysList->model()->index(NEVER, 0), QItemSelectionModel::Select | QItemSelectionModel::Rows);
142       }
143     } else {
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;
149           break;
150         }
151       }
152       if(all_days_selected)
153         daysList->selectionModel()->select(daysList->model()->index(EVERY_DAY, 0), QItemSelectionModel::Select | QItemSelectionModel::Rows);
154     }
155   }
156 }