1 /*******************************************************************************
3 This file is part of mDictionary.
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.
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.
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/>.
18 Copyright 2010 Comarch S.A.
20 *******************************************************************************/
22 /*! \file TranslationView.cpp
23 \brief Web view with translations
25 \author Mateusz Półrola <mateusz.polrola@comarch.pl>
29 #include "TranslationView.h"
30 #include "TranslationWidget.h"
34 TranslationView::TranslationView(QWidget *parent) : QWebView(parent) {
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);
44 mainLayout = new QVBoxLayout;
45 mainLayout->addWidget(view);
46 setLayout(mainLayout);
48 QGraphicsObject *rootObject = view->rootObject();
50 connect(this, SIGNAL(setUrl(QVariant)),
51 rootObject, SLOT(setUrl(QVariant)));
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);
61 installEventFilter(this);
62 property("kineticScroller").value<QAbstractKineticScroller*>()->
67 connect(searchSelectedAction, SIGNAL(triggered()),
68 this, SIGNAL(search()));
69 connect(page(), SIGNAL(selectionChanged()), this, SLOT(selection()));
74 void TranslationView::setHtml(QString htmlString){
75 QString url = QDir::homePath() + "/.mdictionary/" + "html.html";
77 if(file.open(QIODevice::WriteOnly | QIODevice::Text)){
78 QByteArray array = htmlString.toUtf8();
85 void TranslationView::wheelEvent(QWheelEvent *e) {
88 if(e->modifiers() & Qt::ControlModifier) {
98 QWebView::wheelEvent(e);
103 bool TranslationView::eventFilter(QObject *, QEvent *e) {
107 case QEvent::MouseButtonPress:
108 if (static_cast<QMouseEvent *>(e)->button() == Qt::LeftButton)
111 case QEvent::MouseButtonRelease:
112 if (static_cast<QMouseEvent *>(e)->button() == Qt::LeftButton)
113 mousePressed = false;
115 case QEvent::MouseMove:
126 void TranslationView::contextMenuEvent(QContextMenuEvent *e) {
129 QMenu *menu = new QMenu;
130 if(selectedText().isEmpty())
131 searchSelectedAction->setEnabled(false);
133 searchSelectedAction->setEnabled(true);
135 menu->addAction(searchSelectedAction);
136 menu->addSeparator();
137 menu->addAction(pageAction(QWebPage::Copy));
138 menu->addAction(pageAction(QWebPage::SelectAll));
140 menu->exec(e->globalPos());
148 void TranslationView::zoomIn() {
151 if(zoomFactor() >= 3)
153 setZoomFactor(zoomFactor()*1.05);
154 realParent->updateZoom(zoomFactor());
158 void TranslationView::zoomOut() {
161 if(zoomFactor() <= 0.5)
163 setZoomFactor(zoomFactor()*0.95);
164 realParent->updateZoom(zoomFactor());
168 void TranslationView::copy() {
171 pageAction(QWebPage::Copy)->trigger();
175 void TranslationView::selection() {
178 if(selectedText().size())
179 Q_EMIT copyAvailable(true);
181 Q_EMIT copyAvailable(false);
186 void TranslationView::selectAll() {
189 pageAction(QWebPage::SelectAll)->trigger();