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 model_ = new QStandardItemModel(0, 1);
28 selector_->setModel(model_);
29 setPickSelector(selector_);
32 void ButtonSelector::addItem(QString const& text)
34 addItem(text, QVariant(text));
37 void ButtonSelector::addItem(QString const& text, QVariant const& value)
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);
45 if(selector_->currentIndex() < 0)
47 selector_->setCurrentIndex(0);
51 void ButtonSelector::clear()
56 void ButtonSelector::setCurrentIndex(unsigned int index)
58 selector_->setCurrentIndex(index);
61 int ButtonSelector::indexOfValue(QVariant const& value) const
63 for(int i = 0; i < model_->rowCount(); i++)
65 QStandardItem* item = model_->item(i);
67 if(item && item->data(Qt::UserRole) == value)
76 bool ButtonSelector::selectByValue(QVariant const& value)
78 int index = indexOfValue(value);
85 setCurrentIndex(index);
90 int ButtonSelector::currentIndex() const
92 return selector_->currentIndex();
95 QString ButtonSelector::text() const
97 return selector_->currentValueText();
100 QVariant ButtonSelector::value() const
102 int currentIndex = selector_->currentIndex();
109 QStandardItem* item = model_->item(currentIndex);
111 return item->data(Qt::UserRole);
114 bool ButtonSelector::changeItem(unsigned int index,
117 changeItem(index, text, QVariant(text));
120 bool ButtonSelector::changeItem(unsigned int index,
122 QVariant const& value)
124 QStandardItem* item = model_->item(index);
132 item->setData(value, Qt::UserRole);
137 bool ButtonSelector::removeItem(unsigned int index)
139 return model_->removeRow(index);