804f907483f616e140d4776c8522efbeca19af5a
[mdictionary] / src / mdictionary / gui / HistoryListDialog.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 HistoryListDialog.cpp
23     \brief Implements history list dialog
24
25     \author Mateusz Półrola <mateusz.polrola@comarch.pl>
26 */
27
28 #include "HistoryListDialog.h"
29
30 HistoryListDialog::HistoryListDialog(QStringList words, QWidget *parent):
31         QDialog(parent) {
32
33     oryginalList = words;
34     verticalLayout = new QVBoxLayout(this);
35     setLayout(verticalLayout);
36     setModal(true);
37     setWindowFlags(Qt::Popup);
38
39 #ifndef Q_WS_MAEMO_5
40
41     HistoryListModel *model=new HistoryListModel(words,this);
42     view= new QDeclarativeView();
43     QDeclarativeContext* ctxt=view->rootContext();
44     ctxt->setContextProperty("historyTypeModel", model);
45     view->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/HistoryListDialog.qml"));
46     view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
47     view->setAlignment(Qt::AlignCenter);
48     view->show();
49
50     view->setMinimumWidth(250);
51     view->setMaximumWidth(250);
52     view->setMinimumHeight(300);
53     view->setWindowFlags(Qt::Popup);
54     verticalLayout->addWidget(view);
55
56     QGraphicsObject *rootObject = view->rootObject();
57
58     connect(rootObject,SIGNAL(selectedRow(int)),
59             this,SLOT(itemClicked(int)));
60
61 #else
62     #ifndef Q_WS_MAEMO_5
63         setWindowFlags(Qt::Popup);
64         QLabel* title = new QLabel(tr("History"));
65         verticalLayout->addWidget(title,0, Qt::AlignCenter);
66     #endif
67     historyListWidget = new QListWidget(this);
68     verticalLayout->addWidget(historyListWidget);
69
70     for(int i=0; i<words.count(); i++) {
71         QListWidgetItem* item = new QListWidgetItem(
72                 QString::number(i+1) + ". " + words[i]);
73         historyListWidget->addItem(item);
74     }
75
76      setWindowTitle(tr("History"));
77
78      setMinimumHeight(300);
79
80      connect(historyListWidget, SIGNAL(activated(QModelIndex)),
81              this, SLOT(itemClicked(QModelIndex)));
82 #endif
83 }
84
85
86 void HistoryListDialog::itemClicked(QModelIndex index) {
87     _selectedWord = oryginalList[index.row()];
88     _selectedRow = index.row();
89     accept();
90 }
91
92 void HistoryListDialog::itemClicked(int index) {
93     _selectedWord = oryginalList[index];
94     _selectedRow = index;
95     accept();
96 }
97
98 QString HistoryListDialog::selectedWord() {
99     return _selectedWord;
100 }
101
102 int HistoryListDialog::selectedRow() {
103     return _selectedRow;
104 }
105
106 int HistoryListDialog::exec() {
107  #ifndef Q_WS_MAEMO_5
108
109  #else
110     historyListWidget->setFocus();
111  #endif
112
113     QDialog::exec();
114 }