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)));
52 emit setUrl(QDir::homePath() + "/.mdictionary/" + "html.html");
56 realParent = qobject_cast<TranslationWidget*>(parent);
57 searchSelectedAction = new QAction(tr("Search"), this);
58 copySelectedAction = new QAction(tr("Copy"), this);
59 selectAllAction = new QAction(tr("Select All"), this);
60 setAcceptDrops(false);
63 installEventFilter(this);
64 property("kineticScroller").value<QAbstractKineticScroller*>()->
69 connect(searchSelectedAction, SIGNAL(triggered()),
70 this, SIGNAL(search()));
71 connect(page(), SIGNAL(selectionChanged()),
72 this, SLOT(selection()));
77 void TranslationView::setHtml(QString htmlString){
78 QString url = QDir::homePath() + "/.mdictionary/" + "html.html";
80 if(file.open(QIODevice::WriteOnly | QIODevice::Text)){
81 QByteArray array = htmlString.toUtf8();
88 void TranslationView::wheelEvent(QWheelEvent *e) {
91 if(e->modifiers() & Qt::ControlModifier) {
101 QWebView::wheelEvent(e);
106 bool TranslationView::eventFilter(QObject *, QEvent *e) {
110 case QEvent::MouseButtonPress:
111 if (static_cast<QMouseEvent *>(e)->button() == Qt::LeftButton)
114 case QEvent::MouseButtonRelease:
115 if (static_cast<QMouseEvent *>(e)->button() == Qt::LeftButton)
116 mousePressed = false;
118 case QEvent::MouseMove:
129 void TranslationView::contextMenuEvent(QContextMenuEvent *e) {
132 QMenu *menu = new QMenu;
133 if(selectedText().isEmpty())
134 searchSelectedAction->setEnabled(false);
136 searchSelectedAction->setEnabled(true);
138 menu->addAction(searchSelectedAction);
139 menu->addSeparator();
140 menu->addAction(pageAction(QWebPage::Copy));
141 menu->addAction(pageAction(QWebPage::SelectAll));
143 menu->exec(e->globalPos());
151 void TranslationView::zoomIn() {
154 if(zoomFactor() >= 3)
156 setZoomFactor(zoomFactor()*1.05);
157 realParent->updateZoom(zoomFactor());
161 void TranslationView::zoomOut() {
164 if(zoomFactor() <= 0.5)
166 setZoomFactor(zoomFactor()*0.95);
167 realParent->updateZoom(zoomFactor());
171 void TranslationView::copy() {
174 pageAction(QWebPage::Copy)->trigger();
178 void TranslationView::selection() {
181 if(selectedText().size())
182 Q_EMIT copyAvailable(true);
184 Q_EMIT copyAvailable(false);
189 void TranslationView::selectAll() {
192 pageAction(QWebPage::SelectAll)->trigger();