Fix bug with app crash on exit. Add keyboard support in wordList, DictTypeSelectDialo...
[mdictionary] / src / mdictionary / gui / WordListWidget.h
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 /*! \file WordListwidget.h
22     \brief Displays list of words found in dictionaries
23
24     \author Mateusz Półrola <mateusz.polrola@comarch.pl>
25 */
26
27 #ifndef WORDLISTWIDGET_H
28 #define WORDLISTWIDGET_H
29
30 #include <QtGui>
31 #include <QStringListModel>
32 #include <QVBoxLayout>
33 #include "../backbone/backbone.h"
34 #include "WordListProxyStyle.h"
35 #include "WordListModel.h"
36 #include <QtDeclarative/QDeclarativeView>
37 #include <QtDeclarative/QDeclarativeContext>
38
39 /*!
40     It allows user to select word to see its translation or to mark or unmark
41     it as "star" (add/remove from bookmarks). It inherits from QTreeView
42     to allow to display two columns, one with words and second with stars.
43     Star is normal checkable item. To get effect of star we need to set
44     style (WordListProxyStyle) for this widget.
45   */
46 class WordListWidget : public QTreeView {
47     Q_OBJECT
48 public:
49     explicit WordListWidget(QWidget *parent = 0);
50     ~WordListWidget();
51
52
53 Q_SIGNALS:
54     //! Requests to show translation which is described by passed translations
55     //! objects
56     void showTranslation(QList<Translation*>);
57
58
59     //! Requests to add selected word to bookmarks
60     void addBookmark(QList<Translation*>);
61
62     //! Requests to remove selected word from bookmarks
63     void removeBookmark(QList<Translation*>);
64
65     void setWordListState(QVariant state);
66     void setWordListEmpty(QVariant state);
67
68 #ifndef Q_WS_MAEMO_5
69     void setFocusOnQML();
70 #endif
71
72
73 public Q_SLOTS:
74     //! Shows search results
75     /*!
76       \param hash of found words and its translations objects
77     */
78     void showSearchResults(QHash<QString, QList<Translation*> >);
79
80     //! Locks words list, while backbone is doing something in background
81     void lockList();
82
83     //! Unlocks words list
84     void unlockList();
85
86     //! clears list
87     void clear();
88
89     void bookmarkModeActive();
90
91 #ifndef Q_WS_MAEMO_5
92     void setFocusOnElement();
93 #endif
94
95 protected:
96 #ifdef Q_WS_MAEMO_5
97     //! Reimplemented standard mouseReleaseEvent to check if user clicked on
98     //! a word or on its star to emit suitable signal
99     void mouseReleaseEvent(QMouseEvent *event);
100
101     //! Resizes the size of columns to assure that stars are always on right
102     //! side next to scroll bar
103     void resizeEvent(QResizeEvent *event);
104
105     //! Checks if user press return and if so displays translation of selected word
106     void keyPressEvent( QKeyEvent * event);
107 #endif
108 private Q_SLOTS:
109 #ifdef Q_WS_MAEMO_5
110     //! Emits signal to show translation of clicked item. Signal is emitted
111     //! only when a word was clicked.
112     void wordClicked(QModelIndex index);
113
114     //! Emits signal to show add or remove word from bookmarks.
115     //! Signal is emitted only when a star was clicked.
116     void wordChecked(QModelIndex index);
117 #else
118     //! Emits signal to show translation of clicked item. Signal is emitted
119     //! only when a word was clicked.
120     void wordClicked(QString word);
121
122     //! Emits signal to show translation of clicked item. Signal is emitted
123     //! only when a word was clicked.
124     void wordClickedByIndex(int index);
125
126     //! Emits signal to show add word from bookmarks.
127     //! Signal is emitted only when a star was clicked.
128     void addToBookmarks(QString word);
129
130     //! Emits signal to show remove word from bookmarks.
131     //! Signal is emitted only when a star was clicked.
132     void removeFromBookmarks(QString word);
133 #endif
134
135
136 private:
137     //! Adds word to model. Row is row in the model
138     void addWord(QString word, int row);
139
140     QStandardItemModel* model;
141
142     //! Describes width of a star checkbox in pixels
143     int checkBoxWidth;
144
145     //! Resizes sizes of colums after adding new words or after resize event.
146     void resizeColumns();
147
148     //! Association between words and their translations
149     QHash<QString, QList<Translation*> > searchResult;
150     WordListProxyStyle* proxyStyle;
151
152     bool _isBookmarkModeActive;
153
154 #ifndef Q_WS_MAEMO_5
155     QVBoxLayout* verticalLayout;
156     QDeclarativeView* qmlView;
157     QDeclarativeContext* ctxt;
158     WordListModel* listModel;
159 #endif
160 };
161
162 #endif // WORDLISTWIDGET_H