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("src/mdictionary/qml/TranslationView.qml"));
40     view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
41     view->setAlignment(Qt::AlignCenter);
42     view->show();
43
44 #else
45
46     realParent = qobject_cast<TranslationWidget*>(parent);
47     searchSelectedAction = new QAction(tr("Search"), this);
48     copySelectedAction = new QAction(tr("Copy"), this);
49     selectAllAction = new QAction(tr("Select All"), this);
50     setAcceptDrops(false);
51
52     #ifdef Q_WS_MAEMO_5
53         installEventFilter(this);
54         property("kineticScroller").value<QAbstractKineticScroller*>()->
55                 setEnabled(true);
56     #endif
57
58
59     connect(searchSelectedAction, SIGNAL(triggered()),
60             this, SIGNAL(search()));
61     connect(page(), SIGNAL(selectionChanged()), this, SLOT(selection()));
62
63 #endif
64 }
65
66 void TranslationView::wheelEvent(QWheelEvent *e) {
67     if(e->modifiers() & Qt::ControlModifier) {
68         if(e->delta()>0) {
69             zoomIn();
70         }
71         else {
72             zoomOut();
73         }
74         e->ignore();
75     }
76     else {
77         QWebView::wheelEvent(e);
78     }
79 }
80
81 bool TranslationView::eventFilter(QObject *, QEvent *e) {
82     switch (e->type()) {
83     case QEvent::MouseButtonPress:
84         if (static_cast<QMouseEvent *>(e)->button() == Qt::LeftButton)
85             mousePressed = true;
86         break;
87     case QEvent::MouseButtonRelease:
88         if (static_cast<QMouseEvent *>(e)->button() == Qt::LeftButton)
89             mousePressed = false;
90         break;
91     case QEvent::MouseMove:
92         if (mousePressed)
93             return true;
94     default:
95         break;
96     }
97     return false;
98 }
99
100
101 void TranslationView::contextMenuEvent(QContextMenuEvent *e) {
102     QMenu *menu = new QMenu;
103     if(selectedText().isEmpty())
104         searchSelectedAction->setEnabled(false);
105     else
106         searchSelectedAction->setEnabled(true);
107
108     menu->addAction(searchSelectedAction);
109     menu->addSeparator();
110     menu->addAction(pageAction(QWebPage::Copy));
111     menu->addAction(pageAction(QWebPage::SelectAll));
112
113     menu->exec(e->globalPos());
114     delete menu;
115
116     e->ignore();
117 }
118
119
120 void TranslationView::zoomIn() {
121     if(zoomFactor() >= 3)
122         return;
123     setZoomFactor(zoomFactor()*1.05);
124     realParent->updateZoom(zoomFactor());
125
126 }
127
128 void TranslationView::zoomOut() {
129     if(zoomFactor() <= 0.5)
130         return;
131     setZoomFactor(zoomFactor()*0.95);
132     realParent->updateZoom(zoomFactor());
133 }
134
135 void TranslationView::copy() {
136       pageAction(QWebPage::Copy)->trigger();
137 }
138
139 void TranslationView::selection() {
140     if(selectedText().size())
141         Q_EMIT copyAvailable(true);
142     else
143         Q_EMIT copyAvailable(false);
144 }
145
146
147 void TranslationView::selectAll() {
148     pageAction(QWebPage::SelectAll)->trigger();
149 }
150
151
152