Connection handling improved again. Added ability to bybass general Maemo connectivit...
[jenirok] / src / gui / buttonselector.cpp
1 /*
2  * This file is part of Jenirok.
3  *
4  * Jenirok 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  * Jenirok 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 Jenirok.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18
19 #include <QtGui/QStandardItem>
20 #include "buttonselector.h"
21
22 ButtonSelector::ButtonSelector(QString const& text, QWidget* parent):
23 QMaemo5ValueButton(text, parent), selector_(0), model_(0)
24 {
25     setValueLayout(QMaemo5ValueButton::ValueBesideText);
26     selector_ = new QMaemo5ListPickSelector(this);
27     model_ = new QStandardItemModel(0, 1);
28     selector_->setModel(model_);
29     setPickSelector(selector_);
30 }
31
32 void ButtonSelector::addItem(QString const& text)
33 {
34     addItem(text, QVariant(text));
35 }
36
37 void ButtonSelector::addItem(QString const& text, QVariant const& value)
38 {
39     QStandardItem* item = new QStandardItem(text);
40     item->setData(value, Qt::UserRole);
41     item->setTextAlignment(Qt::AlignCenter);
42     item->setEditable(false);
43     model_->appendRow(item);
44
45     if(selector_->currentIndex() < 0)
46     {
47         selector_->setCurrentIndex(0);
48     }
49 }
50
51 void ButtonSelector::clear()
52 {
53     model_->clear();
54 }
55
56 void ButtonSelector::setCurrentIndex(unsigned int index)
57 {
58     selector_->setCurrentIndex(index);
59 }
60
61 int ButtonSelector::indexOfValue(QVariant const& value) const
62 {
63     for(int i = 0; i < model_->rowCount(); i++)
64     {
65         QStandardItem* item = model_->item(i);
66
67         if(item && item->data(Qt::UserRole) == value)
68         {
69             return i;
70         }
71     }
72
73     return -1;
74 }
75
76 bool ButtonSelector::selectByValue(QVariant const& value)
77 {
78     int index = indexOfValue(value);
79
80     if(index < 0)
81     {
82         return false;
83     }
84
85     setCurrentIndex(index);
86
87     return true;
88 }
89
90 int ButtonSelector::currentIndex() const
91 {
92     return selector_->currentIndex();
93 }
94
95 QString ButtonSelector::text() const
96 {
97     return selector_->currentValueText();
98 }
99
100 QVariant ButtonSelector::value() const
101 {
102     int currentIndex = selector_->currentIndex();
103
104     if(currentIndex < 0)
105     {
106         return QVariant("");
107     }
108
109     QStandardItem* item = model_->item(currentIndex);
110
111     return item->data(Qt::UserRole);
112 }
113
114 bool ButtonSelector::changeItem(unsigned int index,
115                                 QString const& text)
116 {
117     changeItem(index, text, QVariant(text));
118 }
119
120 bool ButtonSelector::changeItem(unsigned int index,
121                                 QString const& text,
122                                 QVariant const& value)
123 {
124     QStandardItem* item = model_->item(index);
125
126     if(!item)
127     {
128         return false;
129     }
130
131     item->setText(text);
132     item->setData(value, Qt::UserRole);
133
134     return true;
135 }
136
137 bool ButtonSelector::removeItem(unsigned int index)
138 {
139     return model_->removeRow(index);
140 }