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