Connection handling in daemon improved. Added settings to allow automatic connection...
[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     model_ = new QStandardItemModel(0, 1);
28     selector_->setModel(model_);
29     setPickSelector(selector_);
30 }
31
32 void ButtonSelector::addItem(QString const& text)
33 {
34     addItem(text, QVariant(text));
35 }
36
37 void ButtonSelector::addItem(QString const& text, QVariant const& value)
38 {
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);
44
45     if(selector_->currentIndex() < 0)
46     {
47         selector_->setCurrentIndex(0);
48     }
49 }
50
51 void ButtonSelector::clear()
52 {
53     model_->clear();
54 }
55
56 void ButtonSelector::setCurrentIndex(int index)
57 {
58     selector_->setCurrentIndex(index);
59 }
60
61 bool ButtonSelector::selectByValue(QVariant const& value)
62 {
63     for(int i = 0; i < model_->rowCount(); i++)
64     {
65         QStandardItem* item = model_->item(i);
66
67         if(item && item->data(Qt::UserRole) == value)
68         {
69             setCurrentIndex(i);
70             return true;
71         }
72     }
73
74     return false;
75 }
76
77 int ButtonSelector::currentIndex() const
78 {
79     return selector_->currentIndex();
80 }
81
82 QString ButtonSelector::text() const
83 {
84     return selector_->currentValueText();
85 }
86
87 QVariant ButtonSelector::value() const
88 {
89     int currentIndex = selector_->currentIndex();
90
91     if(currentIndex < 0)
92     {
93         return QVariant("");
94     }
95
96     QStandardItem* item = model_->item(currentIndex);
97
98     return item->data(Qt::UserRole);
99 }