Changed repo structure
[mdictionary] / src / mdictionary / gui / TranslationTextEdit.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 TranslationTextEdit.cpp
23 //! \author Mateusz Półrola <mateusz.polrola@comarch.pl>
24
25 #include <QtGui>
26 #include "TranslationTextEdit.h"
27 #include "TranslationWidget.h"
28
29 TranslationTextEdit::TranslationTextEdit(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
51 bool TranslationTextEdit::eventFilter(QObject *, QEvent *e)
52 {
53     switch (e->type()) {
54     case QEvent::MouseButtonPress:
55         if (static_cast<QMouseEvent *>(e)->button() == Qt::LeftButton)
56             mousePressed = true;
57         break;
58     case QEvent::MouseButtonRelease:
59         if (static_cast<QMouseEvent *>(e)->button() == Qt::LeftButton)
60             mousePressed = false;
61         break;
62     case QEvent::MouseMove:
63         if (mousePressed)
64             return true;
65         break;
66     default:
67         break;
68     }
69     return false;
70 }
71
72
73 void TranslationTextEdit::contextMenuEvent(QContextMenuEvent *e) {
74     QMenu *menu = new QMenu;
75     if(selectedText().isEmpty())
76         searchSelectedAction->setEnabled(false);
77     else
78         searchSelectedAction->setEnabled(true);
79
80     menu->addAction(searchSelectedAction);
81     menu->addSeparator();
82     menu->addAction(pageAction(QWebPage::Copy));
83     menu->addAction(pageAction(QWebPage::SelectAll));
84
85     menu->exec(e->globalPos());
86     delete menu;
87
88     e->ignore();
89 }
90
91
92 void TranslationTextEdit::zoomIn() {
93     setZoomFactor(zoomFactor()*1.05);
94     realParent->updateZoom(zoomFactor());
95
96 }
97
98 void TranslationTextEdit::zoomOut() {
99     setZoomFactor(zoomFactor()*0.95);
100     realParent->updateZoom(zoomFactor());
101 }
102
103 void TranslationTextEdit::copy() {
104       pageAction(QWebPage::Copy)->trigger();
105 }
106
107 void TranslationTextEdit::selection() {
108     if(selectedText().size())
109         Q_EMIT copyAvailable(true);
110     else
111         Q_EMIT copyAvailable(false);
112 }
113
114
115 void TranslationTextEdit::selectAll() {
116     pageAction(QWebPage::SelectAll)->trigger();
117 }
118
119
120