qml - webView widget
authorjakub <jakub.jaszczynski@comarch.com>
Wed, 29 Dec 2010 14:07:15 +0000 (15:07 +0100)
committerjakub <jakub.jaszczynski@comarch.com>
Wed, 29 Dec 2010 14:07:15 +0000 (15:07 +0100)
src/mdictionary/gui/AboutWidget.cpp
src/mdictionary/gui/TranslationView.cpp
src/mdictionary/gui/TranslationView.h
src/mdictionary/gui/TranslationWidget.cpp
src/mdictionary/mdictionary.pro
src/mdictionary/qml/TranslationView.qml

index 6a3fd9b..1d0516e 100644 (file)
@@ -40,12 +40,12 @@ AboutWidget::AboutWidget(GUIInterface *parent): QDialog(parent)
     view->setAlignment(Qt::AlignCenter);
     view->show();
 
-    QDeclarativeView *view2 = new QDeclarativeView();
+ /*   QDeclarativeView *view2 = new QDeclarativeView();
     view2->setSource(QUrl("src/mdictionary/qml/TranslationView.qml"));
     view2->setResizeMode(QDeclarativeView::SizeRootObjectToView);
     view2->setAlignment(Qt::AlignCenter);
     view2->show();
-
+*/
     mainLayout = new QVBoxLayout;
     mainLayout->addWidget(view);
 
index 351ed6b..84efd21 100644 (file)
@@ -41,6 +41,14 @@ TranslationView::TranslationView(QWidget *parent) : QWebView(parent) {
     view->setAlignment(Qt::AlignCenter);
     view->show();
 
+    mainLayout = new QVBoxLayout;
+    mainLayout->addWidget(view);
+    setLayout(mainLayout);
+
+    QGraphicsObject *rootObject = view->rootObject();
+
+    connect(this, SIGNAL(setUrl(QVariant)),
+           rootObject, SLOT(setUrl(QVariant)));
 #else
 
     realParent = qobject_cast<TranslationWidget*>(parent);
@@ -63,7 +71,20 @@ TranslationView::TranslationView(QWidget *parent) : QWebView(parent) {
 #endif
 }
 
+void TranslationView::setHtml(QString htmlString){
+    QString url = QDir::homePath() + "/.mdictionary/" + "html.html";
+    QFile file(url);
+    if(file.open(QIODevice::WriteOnly | QIODevice::Text)){
+        QByteArray array = htmlString.toUtf8();
+        file.write(array);
+        file.close();
+        emit setUrl(url);
+    }
+}
+
 void TranslationView::wheelEvent(QWheelEvent *e) {
+#ifndef Q_WS_MAEMO_5
+#else
     if(e->modifiers() & Qt::ControlModifier) {
         if(e->delta()>0) {
             zoomIn();
@@ -76,9 +97,12 @@ void TranslationView::wheelEvent(QWheelEvent *e) {
     else {
         QWebView::wheelEvent(e);
     }
+#endif
 }
 
 bool TranslationView::eventFilter(QObject *, QEvent *e) {
+#ifndef Q_WS_MAEMO_5
+#else
     switch (e->type()) {
     case QEvent::MouseButtonPress:
         if (static_cast<QMouseEvent *>(e)->button() == Qt::LeftButton)
@@ -95,10 +119,13 @@ bool TranslationView::eventFilter(QObject *, QEvent *e) {
         break;
     }
     return false;
+#endif
 }
 
 
 void TranslationView::contextMenuEvent(QContextMenuEvent *e) {
+#ifndef Q_WS_MAEMO_5
+#else
     QMenu *menu = new QMenu;
     if(selectedText().isEmpty())
         searchSelectedAction->setEnabled(false);
@@ -114,38 +141,53 @@ void TranslationView::contextMenuEvent(QContextMenuEvent *e) {
     delete menu;
 
     e->ignore();
+#endif
 }
 
 
 void TranslationView::zoomIn() {
+#ifndef Q_WS_MAEMO_5
+#else
     if(zoomFactor() >= 3)
         return;
     setZoomFactor(zoomFactor()*1.05);
     realParent->updateZoom(zoomFactor());
-
+#endif
 }
 
 void TranslationView::zoomOut() {
+#ifndef Q_WS_MAEMO_5
+#else
     if(zoomFactor() <= 0.5)
         return;
     setZoomFactor(zoomFactor()*0.95);
     realParent->updateZoom(zoomFactor());
+#endif
 }
 
 void TranslationView::copy() {
+#ifndef Q_WS_MAEMO_5
+#else
       pageAction(QWebPage::Copy)->trigger();
+#endif
 }
 
 void TranslationView::selection() {
+#ifndef Q_WS_MAEMO_5
+#else
     if(selectedText().size())
         Q_EMIT copyAvailable(true);
     else
         Q_EMIT copyAvailable(false);
+#endif
 }
 
 
 void TranslationView::selectAll() {
+#ifndef Q_WS_MAEMO_5
+#else
     pageAction(QWebPage::SelectAll)->trigger();
+#endif
 }
 
 
index 7db495d..06c30f1 100644 (file)
@@ -34,6 +34,8 @@
 #include <QEvent>
 #include <QDeclarativeView>
 #include <QVBoxLayout>
+#include <QString>
+#include <QByteArray>
 
 class TranslationWidget;
 
@@ -42,6 +44,7 @@ class TranslationView : public QWebView
     Q_OBJECT
 public:
     explicit TranslationView(QWidget *parent = 0);
+    void setHtml(QString htmlString);
 
 public Q_SLOTS:
     void zoomIn();
@@ -53,6 +56,8 @@ public Q_SLOTS:
 Q_SIGNALS:
     void search();
     void copyAvailable(bool);
+    void setHtmlSignal(QVariant htmlString);
+    void setUrl(QVariant url);
 
 protected:
     void contextMenuEvent(QContextMenuEvent *);
index 5cc4762..e35b92d 100644 (file)
@@ -48,8 +48,8 @@ TranslationWidget::TranslationWidget(QWidget *parent):
 
     setWindowTitle("mDictionary");
 
-    connect(webkit, SIGNAL(search()),
-           this, SLOT(searchSelected()));
+//    connect(webkit, SIGNAL(search()),
+//           this, SLOT(searchSelected()));
 
     QFile file(":/xsl/xsl.xsl");
     if(!file.open(QFile::ReadOnly))
@@ -75,14 +75,16 @@ void TranslationWidget::show(QStringList translations) {
     QString trans= createTranslation(translations);
 
     QString head = "<html><head>";
-    head += "<link rel=\"stylesheet\" type=\"text/css\" "
+    head += "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"> "
+            "<link rel=\"stylesheet\" type=\"text/css\" "
             "href=\"/usr/share/mdictionary/css/" "style.css"
             "\" /></head><body ondragstart=\"return false\">";
     trans = head + trans + "</body></html>";
 
     trans.replace("$STAR$", "/usr/share/mdictionary/staron.png");
 
-    webkit->setHtml(trans, QUrl().fromLocalFile("/"));
+    webkit->setHtml(trans);
+//    webkit->setHtml(trans, QUrl().fromLocalFile("/"));
 }
 
 
@@ -293,7 +295,7 @@ void TranslationWidget::initializeUI() {
     qreal fac = set->value("zoom").toFloat();
     if(!fac)
         fac++;
-    webkit->setZoomFactor(fac);
+//    webkit->setZoomFactor(fac);
     delete set;
 
     QWidget*w = new QWidget(this);
@@ -353,7 +355,7 @@ void TranslationWidget::initializeUI() {
 }
 
 void TranslationWidget::searchSelected() {
-    if(webkit->selectedText().isEmpty()) {
+/*    if(webkit->selectedText().isEmpty()) {
         Q_EMIT notify(Notify::Warning, tr("No text is selected"));
     }
     else {
@@ -362,15 +364,17 @@ void TranslationWidget::searchSelected() {
         #endif
         Q_EMIT search(webkit->selectedText().toLower());
     }
+    */
 }
 
 void TranslationWidget::copySelected() {
-    if(webkit->selectedText().isEmpty()) {
+ /*   if(webkit->selectedText().isEmpty()) {
         Q_EMIT notify(Notify::Warning, tr("No text is selected"));
     }
     else {
         Q_EMIT notify(Notify::Info, tr("Copyied selected text"));
     }
+    */
 }
 
 #ifdef Q_WS_MAEMO_5
@@ -422,12 +426,12 @@ void TranslationWidget::hideEvent(QHideEvent* e) {
 void TranslationWidget::keyPressEvent(QKeyEvent* event) {
     switch (event->key()) {
         case Qt::Key_F7:
-        webkit->zoomIn();
+ //       webkit->zoomIn();
         event->accept();
         break;
 
         case Qt::Key_F8:
-        webkit->zoomOut();
+//        webkit->zoomOut();
         event->accept();
         break;
     }
index 8ee4658..79ddab6 100644 (file)
@@ -90,7 +90,9 @@ OTHER_FILES += \
     qml/DictTypeSelectDialog.qml \
     qml/DictManagerWidget.qml \
     qml/WordListWidget.qml \
-    qml/TranslationView.qml
+    qml/TranslationView.qml \
+    qml/FlickableWebView.qml \
+    qml/ScrollBar.qml
 
 target.path = $$BIN_DIR
 INSTALLS += target
@@ -191,6 +193,8 @@ unix {
         qmls.files += ./qml/DictManagerWidget.qml
         qmls.files += ./qml/WordListWidget.qml
         qmls.files += ./qml/TranslationView.qml
+        qmls.files += ./qml/FlickableWebView.qml
+        qmls.files += ./qml/ScrollBar.qml
     }
        
     INSTALLS += desktop icon64 shared service css css_images qmls
index d020be1..d0181a6 100644 (file)
@@ -2,46 +2,54 @@ import Qt 4.7
 import QtWebKit 1.0
 
 Rectangle {
-    id: rectangle1
+    id: webBrowser
     width: 500
     height: 500
 
+    SystemPalette { id: myPalette; colorGroup: SystemPalette.Active }
+    color: myPalette.window
 
+    function setUrl(string){
+        webView.url = "file:///home/jakub/Pulpit/test.html";
+        webView.url = "file:///home/jakub/.mdictionary/html.html";
+    }
+
+    FlickableWebView {
+        id: webView
+        width:parent.width
+        height:parent.height
+        anchors { top: headerSpace.bottom; left: parent.left; right: parent.right; bottom: parent.bottom }
+    }
 
-    WebView {
-        id: web_view1
-        anchors.fill: parent
-        url: "http://kokos.pl/"
+    Item { id: headerSpace; width: parent.width; height: 1}
+
+    ScrollBar {
+        scrollArea: webView; width: 8
+        anchors { right: parent.right; top: parent.bottom; bottom: parent.bottom }
+    }
 
-        onDoubleClick: {
-            console.log(clickX,clickY);
-            heuristicZoom(clickX,clickY,2)
-        }
-       /* onZoomTo: {
-            console.log(zoom);
-            scale=zoom;x=width/2-centerX;
-            y=height/2-centerY
-       }
-       */
+    ScrollBar {
+        scrollArea: webView; height: 8; orientation: Qt.Horizontal
+        anchors { right: parent.right; rightMargin: 8; left: parent.left; bottom: parent.bottom }
     }
 
     Button {
         id: button1
-        textInButton: "test"
-        width: 200;
-        height: 30;
-        x: 41
-        y: 28
-        onClicked: web_view1.scale=web_view1.scale*1.05;
+        textInButton: "+"
+        width: 20;
+        height: 20;
+        anchors.right: parent.right
+        anchors.bottom: button2.top
+        onClicked: webView.doZoom(1.05);
     }
 
     Button {
         id: button2
-        textInButton: "test2"
-        width: 200;
-        height: 30;
-        x: 41
-        y: 68
-        onClicked: web_view1.scale=web_view1.scale*.95;
+        textInButton: "-"
+        width: 20;
+        height: 20;
+        anchors.right: parent.right
+        anchors.bottom: parent.bottom
+        onClicked: webView.doZoom(.95);
     }
 }