2 * This file is part of jSpeed.
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.
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.
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/>.
19 #include <QtGui/QStandardItem>
20 #include <QtCore/QDebug>
21 #include "buttonselector.h"
23 ButtonSelector::ButtonSelector(QString const& text, QWidget* parent):
24 QMaemo5ValueButton(text, parent), selector_(0), model_(0)
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_);
34 void ButtonSelector::addItem(QString const& text)
36 addItem(text, QVariant(text));
39 void ButtonSelector::addItem(QString const& text, QVariant const& value)
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);
47 if(selector_->currentIndex() < 0)
49 selector_->setCurrentIndex(0);
54 void ButtonSelector::clear()
59 void ButtonSelector::setCurrentIndex(unsigned int index)
61 selector_->setCurrentIndex(index);
64 int ButtonSelector::indexOfValue(QVariant const& value) const
66 for(int i = 0; i < model_->rowCount(); i++)
68 QStandardItem* item = model_->item(i);
70 if(item && item->data(Qt::UserRole) == value)
79 bool ButtonSelector::selectByValue(QVariant const& value)
81 int index = indexOfValue(value);
88 setCurrentIndex(index);
93 int ButtonSelector::currentIndex() const
95 return selector_->currentIndex();
98 QString ButtonSelector::text() const
100 return selector_->currentValueText();
103 QVariant ButtonSelector::value() const
105 int currentIndex = selector_->currentIndex();
112 QStandardItem* item = model_->item(currentIndex);
114 return item->data(Qt::UserRole);
117 bool ButtonSelector::changeItem(unsigned int index,
120 return changeItem(index, text, QVariant(text));
123 bool ButtonSelector::changeItem(unsigned int index,
125 QVariant const& value)
127 QStandardItem* item = model_->item(index);
135 item->setData(value, Qt::UserRole);
140 bool ButtonSelector::removeItem(unsigned int index)
142 return model_->removeRow(index);
145 int ButtonSelector::size() const
147 return model_->rowCount();
150 void ButtonSelector::onSelected(QString const& text)
152 emit selected(currentIndex(), text, value());