draft of settingWidget 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::fromLocalFile("/usr/share/mdictionary/qml/TranslationView.qml"));
40     view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
41     view->setAlignment(Qt::AlignCenter);
42     view->show();
43
44     mainLayout = new QVBoxLayout;
45     mainLayout->addWidget(view);
46     setLayout(mainLayout);
47
48     QGraphicsObject *rootObject = view->rootObject();
49
50     connect(this, SIGNAL(setUrl(QVariant)),
51            rootObject, SLOT(setUrl(QVariant)));
52 #else
53
54     realParent = qobject_cast<TranslationWidget*>(parent);
55     searchSelectedAction = new QAction(tr("Search"), this);
56     copySelectedAction = new QAction(tr("Copy"), this);
57     selectAllAction = new QAction(tr("Select All"), this);
58     setAcceptDrops(false);
59
60     #ifdef Q_WS_MAEMO_5
61         installEventFilter(this);
62         property("kineticScroller").value<QAbstractKineticScroller*>()->
63                 setEnabled(true);
64     #endif
65
66
67     connect(searchSelectedAction, SIGNAL(triggered()),
68             this, SIGNAL(search()));
69     connect(page(), SIGNAL(selectionChanged()), this, SLOT(selection()));
70
71 #endif
72 }
73
74 void TranslationView::setHtml(QString htmlString){
75     QString url = QDir::homePath() + "/.mdictionary/" + "html.html";
76     QFile file(url);
77     if(file.open(QIODevice::WriteOnly | QIODevice::Text)){
78         QByteArray array = htmlString.toUtf8();
79         file.write(array);
80         file.close();
81         emit setUrl(url);
82     }
83 }
84
85 void TranslationView::wheelEvent(QWheelEvent *e) {
86 #ifndef Q_WS_MAEMO_5
87 #else
88     if(e->modifiers() & Qt::ControlModifier) {
89         if(e->delta()>0) {
90             zoomIn();
91         }
92         else {
93             zoomOut();
94         }
95         e->ignore();
96     }
97     else {
98         QWebView::wheelEvent(e);
99     }
100 #endif
101 }
102
103 bool TranslationView::eventFilter(QObject *, QEvent *e) {
104 #ifndef Q_WS_MAEMO_5
105 #else
106     switch (e->type()) {
107     case QEvent::MouseButtonPress:
108         if (static_cast<QMouseEvent *>(e)->button() == Qt::LeftButton)
109             mousePressed = true;
110         break;
111     case QEvent::MouseButtonRelease:
112         if (static_cast<QMouseEvent *>(e)->button() == Qt::LeftButton)
113             mousePressed = false;
114         break;
115     case QEvent::MouseMove:
116         if (mousePressed)
117             return true;
118     default:
119         break;
120     }
121     return false;
122 #endif
123 }
124
125
126 void TranslationView::contextMenuEvent(QContextMenuEvent *e) {
127 #ifndef Q_WS_MAEMO_5
128 #else
129     QMenu *menu = new QMenu;
130     if(selectedText().isEmpty())
131         searchSelectedAction->setEnabled(false);
132     else
133         searchSelectedAction->setEnabled(true);
134
135     menu->addAction(searchSelectedAction);
136     menu->addSeparator();
137     menu->addAction(pageAction(QWebPage::Copy));
138     menu->addAction(pageAction(QWebPage::SelectAll));
139
140     menu->exec(e->globalPos());
141     delete menu;
142
143     e->ignore();
144 #endif
145 }
146
147
148 void TranslationView::zoomIn() {
149 #ifndef Q_WS_MAEMO_5
150 #else
151     if(zoomFactor() >= 3)
152         return;
153     setZoomFactor(zoomFactor()*1.05);
154     realParent->updateZoom(zoomFactor());
155 #endif
156 }
157
158 void TranslationView::zoomOut() {
159 #ifndef Q_WS_MAEMO_5
160 #else
161     if(zoomFactor() <= 0.5)
162         return;
163     setZoomFactor(zoomFactor()*0.95);
164     realParent->updateZoom(zoomFactor());
165 #endif
166 }
167
168 void TranslationView::copy() {
169 #ifndef Q_WS_MAEMO_5
170 #else
171       pageAction(QWebPage::Copy)->trigger();
172 #endif
173 }
174
175 void TranslationView::selection() {
176 #ifndef Q_WS_MAEMO_5
177 #else
178     if(selectedText().size())
179         Q_EMIT copyAvailable(true);
180     else
181         Q_EMIT copyAvailable(false);
182 #endif
183 }
184
185
186 void TranslationView::selectAll() {
187 #ifndef Q_WS_MAEMO_5
188 #else
189     pageAction(QWebPage::SelectAll)->trigger();
190 #endif
191 }
192
193
194