add focus in qml
[mdictionary] / src / mdictionary / gui / TranslationView.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 TranslationView.cpp
23     \brief Web view with translations
24
25     \author Mateusz Półrola <mateusz.polrola@comarch.pl>
26 */
27
28 #include <QtGui>
29 #include "TranslationView.h"
30 #include "TranslationWidget.h"
31
32
33
34 TranslationView::TranslationView(QWidget *parent) : QWebView(parent) {
35
36 #ifndef Q_WS_MAEMO_5
37
38     view= new QDeclarativeView();
39     view->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/TranslationView.qml"));
40     view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
41     view->setAlignment(Qt::AlignCenter);
42     view->show();
43
44     mainLayout = new QVBoxLayout;
45     mainLayout->addWidget(view);
46     setLayout(mainLayout);
47
48     QGraphicsObject *rootObject = view->rootObject();
49
50     connect(this, SIGNAL(setUrl(QVariant)),
51            rootObject, SLOT(setUrl(QVariant)));
52     emit setUrl(QDir::homePath() + "/.mdictionary/" + "html.html");
53
54 #else
55
56     realParent = qobject_cast<TranslationWidget*>(parent);
57     searchSelectedAction = new QAction(tr("Search"), this);
58     copySelectedAction = new QAction(tr("Copy"), this);
59     selectAllAction = new QAction(tr("Select All"), this);
60     setAcceptDrops(false);
61
62     #ifdef Q_WS_MAEMO_5
63         installEventFilter(this);
64         property("kineticScroller").value<QAbstractKineticScroller*>()->
65                 setEnabled(true);
66     #endif
67
68
69     connect(searchSelectedAction, SIGNAL(triggered()),
70             this, SIGNAL(search()));
71     connect(page(), SIGNAL(selectionChanged()),
72             this, SLOT(selection()));
73
74 #endif
75 }
76
77 void TranslationView::setHtml(QString htmlString){
78     QString url = QDir::homePath() + "/.mdictionary/" + "html.html";
79     QFile file(url);
80     if(file.open(QIODevice::WriteOnly | QIODevice::Text)){
81         QByteArray array = htmlString.toUtf8();
82         file.write(array);
83         file.close();
84         emit setUrl(url);
85     }
86 }
87
88 void TranslationView::wheelEvent(QWheelEvent *e) {
89 #ifndef Q_WS_MAEMO_5
90 #else
91     if(e->modifiers() & Qt::ControlModifier) {
92         if(e->delta()>0) {
93             zoomIn();
94         }
95         else {
96             zoomOut();
97         }
98         e->ignore();
99     }
100     else {
101         QWebView::wheelEvent(e);
102     }
103 #endif
104 }
105
106 bool TranslationView::eventFilter(QObject *, QEvent *e) {
107 #ifndef Q_WS_MAEMO_5
108 #else
109     switch (e->type()) {
110     case QEvent::MouseButtonPress:
111         if (static_cast<QMouseEvent *>(e)->button() == Qt::LeftButton)
112             mousePressed = true;
113         break;
114     case QEvent::MouseButtonRelease:
115         if (static_cast<QMouseEvent *>(e)->button() == Qt::LeftButton)
116             mousePressed = false;
117         break;
118     case QEvent::MouseMove:
119         if (mousePressed)
120             return true;
121     default:
122         break;
123     }
124     return false;
125 #endif
126 }
127
128
129 void TranslationView::contextMenuEvent(QContextMenuEvent *e) {
130 #ifndef Q_WS_MAEMO_5
131 #else
132     QMenu *menu = new QMenu;
133     if(selectedText().isEmpty())
134         searchSelectedAction->setEnabled(false);
135     else
136         searchSelectedAction->setEnabled(true);
137
138     menu->addAction(searchSelectedAction);
139     menu->addSeparator();
140     menu->addAction(pageAction(QWebPage::Copy));
141     menu->addAction(pageAction(QWebPage::SelectAll));
142
143     menu->exec(e->globalPos());
144     delete menu;
145
146     e->ignore();
147 #endif
148 }
149
150
151 void TranslationView::zoomIn() {
152 #ifndef Q_WS_MAEMO_5
153 #else
154     if(zoomFactor() >= 3)
155         return;
156     setZoomFactor(zoomFactor()*1.05);
157     realParent->updateZoom(zoomFactor());
158 #endif
159 }
160
161 void TranslationView::zoomOut() {
162 #ifndef Q_WS_MAEMO_5
163 #else
164     if(zoomFactor() <= 0.5)
165         return;
166     setZoomFactor(zoomFactor()*0.95);
167     realParent->updateZoom(zoomFactor());
168 #endif
169 }
170
171 void TranslationView::copy() {
172 #ifndef Q_WS_MAEMO_5
173 #else
174       pageAction(QWebPage::Copy)->trigger();
175 #endif
176 }
177
178 void TranslationView::selection() {
179 #ifndef Q_WS_MAEMO_5
180 #else
181     if(selectedText().size())
182         Q_EMIT copyAvailable(true);
183     else
184         Q_EMIT copyAvailable(false);
185 #endif
186 }
187
188
189 void TranslationView::selectAll() {
190 #ifndef Q_WS_MAEMO_5
191 #else
192     pageAction(QWebPage::SelectAll)->trigger();
193 #endif
194 }
195
196
197