Add keyboard support to comboBox, HistoryList. Fix some issues with keyboard support.
[mdictionary] / src / include / ComboBoxModel.cpp
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
5     mDictionary is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     mDictionary is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22 /*! \file ComboBoxModel.cpp
23     \brief Contains data for ComboBox QML component
24
25     \author Marcin Kaźmierczak <marcin.kazmierczak@comarch.pl>
26 */
27
28 #include "ComboBoxModel.h"
29
30 ComboBoxModel::ComboBoxModel(QList<QString> contents, QObject *parent) :
31     QAbstractListModel(parent)
32 {
33     QHash<int, QByteArray> roles;
34     roles[ContentRole] = "content";
35     roles[NumberRole] = "number";
36     setRoleNames(roles);
37     setContents(contents);
38 }
39
40 int ComboBoxModel::rowCount(const QModelIndex &parent) const
41 {
42     return _contents.count();
43 }
44
45 QVariant ComboBoxModel::data(const QModelIndex &index, int role) const
46 {
47     if (index.row() < 0 || index.row() > _contents.count())
48         return QVariant();
49
50     QString item = _contents[index.row()];
51     if (role == ContentRole)
52         return item;
53     if (role == NumberRole)
54         return index.row();
55     return QVariant();
56 }
57
58 QString ComboBoxModel::selectedItem()
59 {
60     return _selectedItem;
61 }
62
63 int ComboBoxModel::selectedIndex()
64 {
65     return _selectedIndex;
66 }
67
68 void ComboBoxModel::setSelectedItem(QString item)
69 {
70     _selectedItem = item;
71 }
72
73 void ComboBoxModel::setSelectedIndex(int index)
74 {
75     _selectedIndex = index;
76 }
77
78 void ComboBoxModel::setContents(QList<QString> contents)
79 {
80     foreach (QString item, contents)
81     {
82         addItem(item);
83     }
84 }
85
86 void ComboBoxModel::addItem(QString item)
87 {
88     beginInsertRows(QModelIndex(), rowCount(), rowCount());
89     _contents << item;
90     endInsertRows();
91 }
92
93 QString ComboBoxModel::valueOnPosition(int index)
94 {
95     return _contents[index];
96 }