2 * This file is part of Jenirok.
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.
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.
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/>.
19 #include <QtGui/QStandardItem>
20 #include "buttonselector.h"
22 ButtonSelector::ButtonSelector(QString const& text, QWidget* parent):
23 QMaemo5ValueButton(text, parent), selector_(0), model_(0)
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_);
33 void ButtonSelector::addItem(QString const& text)
35 addItem(text, QVariant(text));
38 void ButtonSelector::addItem(QString const& text, QVariant const& value)
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);
46 if(selector_->currentIndex() < 0)
48 selector_->setCurrentIndex(0);
52 void ButtonSelector::clear()
57 void ButtonSelector::setCurrentIndex(unsigned int index)
59 selector_->setCurrentIndex(index);
62 int ButtonSelector::indexOfValue(QVariant const& value) const
64 for(int i = 0; i < model_->rowCount(); i++)
66 QStandardItem* item = model_->item(i);
68 if(item && item->data(Qt::UserRole) == value)
77 bool ButtonSelector::selectByValue(QVariant const& value)
79 int index = indexOfValue(value);
86 setCurrentIndex(index);
91 int ButtonSelector::currentIndex() const
93 return selector_->currentIndex();
96 QString ButtonSelector::text() const
98 return selector_->currentValueText();
101 QVariant ButtonSelector::value() const
103 int currentIndex = selector_->currentIndex();
110 QStandardItem* item = model_->item(currentIndex);
112 return item->data(Qt::UserRole);
115 bool ButtonSelector::changeItem(unsigned int index,
118 return changeItem(index, text, QVariant(text));
121 bool ButtonSelector::changeItem(unsigned int index,
123 QVariant const& value)
125 QStandardItem* item = model_->item(index);
133 item->setData(value, Qt::UserRole);
138 bool ButtonSelector::removeItem(unsigned int index)
140 return model_->removeRow(index);
143 void ButtonSelector::onSelected(QString const& text)
145 emit selected(currentIndex(), text, value());