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