79a6c149f468c9c117a0cac2e675bbe96d5f6362
[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 //! \author Mateusz Półrola <mateusz.polrola@comarch.pl>
24
25 #include <QtGui>
26 #include "TranslationView.h"
27 #include "TranslationWidget.h"
28
29 TranslationView::TranslationView(QWidget *parent) :
30     QWebView(parent)
31 {
32     realParent = qobject_cast<TranslationWidget*>(parent);
33     searchSelectedAction = new QAction(tr("Search"), this);
34     copySelectedAction = new QAction(tr("Copy"), this);
35     selectAllAction = new QAction(tr("Select All"), this);
36     setAcceptDrops(false);
37
38     #ifdef Q_WS_MAEMO_5
39         installEventFilter(this);
40         property("kineticScroller").value<QAbstractKineticScroller*>()->
41                 setEnabled(true);
42     #endif
43
44
45     connect(searchSelectedAction, SIGNAL(triggered()),
46             this, SIGNAL(search()));
47     connect(page(), SIGNAL(selectionChanged()), this, SLOT(selection()));
48 }
49
50 void TranslationView::wheelEvent(QWheelEvent *e) {
51     if(e->modifiers() & Qt::ControlModifier) {
52         if(e->delta()>0) {
53             zoomIn();
54         }
55         else {
56             zoomOut();
57         }
58         e->ignore();
59     }
60     else {
61         QWebView::wheelEvent(e);
62     }
63 }
64
65 bool TranslationView::eventFilter(QObject *, QEvent *e) {
66     switch (e->type()) {
67     case QEvent::MouseButtonPress:
68         if (static_cast<QMouseEvent *>(e)->button() == Qt::LeftButton)
69             mousePressed = true;
70         break;
71     case QEvent::MouseButtonRelease:
72         if (static_cast<QMouseEvent *>(e)->button() == Qt::LeftButton)
73             mousePressed = false;
74         break;
75     case QEvent::MouseMove:
76         if (mousePressed)
77             return true;
78     default:
79         break;
80     }
81     return false;
82 }
83
84
85 void TranslationView::contextMenuEvent(QContextMenuEvent *e) {
86     QMenu *menu = new QMenu;
87     if(selectedText().isEmpty())
88         searchSelectedAction->setEnabled(false);
89     else
90         searchSelectedAction->setEnabled(true);
91
92     menu->addAction(searchSelectedAction);
93     menu->addSeparator();
94     menu->addAction(pageAction(QWebPage::Copy));
95     menu->addAction(pageAction(QWebPage::SelectAll));
96
97     menu->exec(e->globalPos());
98     delete menu;
99
100     e->ignore();
101 }
102
103
104 void TranslationView::zoomIn() {
105     if(zoomFactor() >= 3)
106         return;
107     setZoomFactor(zoomFactor()*1.05);
108     realParent->updateZoom(zoomFactor());
109
110 }
111
112 void TranslationView::zoomOut() {
113     if(zoomFactor() <= 0.5)
114         return;
115     setZoomFactor(zoomFactor()*0.95);
116     realParent->updateZoom(zoomFactor());
117 }
118
119 void TranslationView::copy() {
120       pageAction(QWebPage::Copy)->trigger();
121 }
122
123 void TranslationView::selection() {
124     if(selectedText().size())
125         Q_EMIT copyAvailable(true);
126     else
127         Q_EMIT copyAvailable(false);
128 }
129
130
131 void TranslationView::selectAll() {
132     pageAction(QWebPage::SelectAll)->trigger();
133 }
134
135
136