Fixed problems with special html characters and entities in Norwegian version.
[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     connect(selector_, SIGNAL(selected(QString const&)), this, SLOT(onSelected(QString const&)));
28     model_ = new QStandardItemModel(0, 1);
29     selector_->setModel(model_);
30     setPickSelector(selector_);
31 }
32
33 void ButtonSelector::addItem(QString const& text)
34 {
35     addItem(text, QVariant(text));
36 }
37
38 void ButtonSelector::addItem(QString const& text, QVariant const& value)
39 {
40     QStandardItem* item = new QStandardItem(text);
41     item->setData(value, Qt::UserRole);
42     item->setTextAlignment(Qt::AlignCenter);
43     item->setEditable(false);
44     model_->appendRow(item);
45
46     if(selector_->currentIndex() < 0)
47     {
48         selector_->setCurrentIndex(0);
49     }
50 }
51
52 void ButtonSelector::clear()
53 {
54     model_->clear();
55 }
56
57 void ButtonSelector::setCurrentIndex(unsigned int index)
58 {
59     selector_->setCurrentIndex(index);
60 }
61
62 int ButtonSelector::indexOfValue(QVariant const& value) const
63 {
64     for(int i = 0; i < model_->rowCount(); i++)
65     {
66         QStandardItem* item = model_->item(i);
67
68         if(item && item->data(Qt::UserRole) == value)
69         {
70             return i;
71         }
72     }
73
74     return -1;
75 }
76
77 bool ButtonSelector::selectByValue(QVariant const& value)
78 {
79     int index = indexOfValue(value);
80
81     if(index < 0)
82     {
83         return false;
84     }
85
86     setCurrentIndex(index);
87
88     return true;
89 }
90
91 int ButtonSelector::currentIndex() const
92 {
93     return selector_->currentIndex();
94 }
95
96 QString ButtonSelector::text() const
97 {
98     return selector_->currentValueText();
99 }
100
101 QVariant ButtonSelector::value() const
102 {
103     int currentIndex = selector_->currentIndex();
104
105     if(currentIndex < 0)
106     {
107         return QVariant("");
108     }
109
110     QStandardItem* item = model_->item(currentIndex);
111
112     return item->data(Qt::UserRole);
113 }
114
115 bool ButtonSelector::changeItem(unsigned int index,
116                                 QString const& text)
117 {
118     return changeItem(index, text, QVariant(text));
119 }
120
121 bool ButtonSelector::changeItem(unsigned int index,
122                                 QString const& text,
123                                 QVariant const& value)
124 {
125     QStandardItem* item = model_->item(index);
126
127     if(!item)
128     {
129         return false;
130     }
131
132     item->setText(text);
133     item->setData(value, Qt::UserRole);
134
135     return true;
136 }
137
138 bool ButtonSelector::removeItem(unsigned int index)
139 {
140     return model_->removeRow(index);
141 }
142
143 void ButtonSelector::onSelected(QString const& text)
144 {
145     emit selected(currentIndex(), text, value());
146 }