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