Ver Fonte

Fix <span> element

Kandrashin Denis há 13 anos atrás
pai
commit
06b0c23fbe
4 ficheiros alterados com 36 adições e 2 exclusões
  1. 17 2
      source/fb2main.cpp
  2. 2 0
      source/fb2main.h
  3. 14 0
      source/fb2view.cpp
  4. 3 0
      source/fb2view.h

+ 17 - 2
source/fb2main.cpp

@@ -1,6 +1,7 @@
 #include <QtGui>
 #include <QtDebug>
 #include <QTreeView>
+#include <QWebInspector>
 #include <QWebFrame>
 
 #include "fb2main.h"
@@ -368,6 +369,12 @@ void Fb2MainWindow::createActions()
     menu->addAction(act);
     tool->addAction(act);
 
+    menu->addSeparator();
+
+    act = new QAction(tr("Web inspector"), this);
+    connect(act, SIGNAL(triggered()), this, SLOT(showInspector()));
+    menu->addAction(act);
+
     menuBar()->addSeparator();
 
     menu = menuBar()->addMenu(tr("&Help"));
@@ -426,6 +433,7 @@ void Fb2MainWindow::createText()
     connect(actionZoomIn, SIGNAL(triggered()), textEdit, SLOT(zoomIn()));
     connect(actionZoomOut, SIGNAL(triggered()), textEdit, SLOT(zoomOut()));
     connect(actionZoomOrig, SIGNAL(triggered()), textEdit, SLOT(zoomOrig()));
+
 }
 
 void Fb2MainWindow::loadFinished(bool ok)
@@ -444,8 +452,8 @@ void Fb2MainWindow::selectionChanged()
     actionTextSub->setChecked(textEdit->SubChecked());
     actionTextSup->setChecked(textEdit->SupChecked());
 
-    QString script = "document.getSelection().baseNode.parentNode.tagName";
-    qCritical() << textEdit->page()->mainFrame()->evaluateJavaScript(script).toString();
+//    QString script = "document.getSelection().baseNode.parentNode.tagName";
+//    qCritical() << textEdit->page()->mainFrame()->evaluateJavaScript(script).toString();
 }
 
 void Fb2MainWindow::undoChanged()
@@ -630,3 +638,10 @@ void Fb2MainWindow::clipboardDataChanged()
     }
 }
 
+void Fb2MainWindow::showInspector()
+{
+    if (!textEdit) return;
+    QWebInspector *inspector = new QWebInspector();
+    inspector->setPage(textEdit->page());
+    inspector->show();
+}

+ 2 - 0
source/fb2main.h

@@ -51,6 +51,7 @@ private slots:
     void selectionChanged();
     void undoChanged();
     void redoChanged();
+    void showInspector();
 
 private:
     bool loadXML(const QString &filename);
@@ -92,6 +93,7 @@ private:
         *actionTextStrike,
         *actionTextSub,
         *actionTextSup,
+        *actionInspect,
         *actionZoomIn,
         *actionZoomOut,
         *actionZoomOrig

+ 14 - 0
source/fb2view.cpp

@@ -3,6 +3,8 @@
 
 #include <QAction>
 #include <QtDebug>
+#include <QWebElement>
+#include <QWebFrame>
 
 //---------------------------------------------------------------------------
 //  Fb2WebView
@@ -15,12 +17,14 @@ Fb2WebView::Fb2WebView(QWidget *parent)
     page()->setContentEditable(true);
     QWebSettings *settings = page()->settings();
     settings->setAttribute(QWebSettings::AutoLoadImages, true);
+    settings->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
     settings->setAttribute(QWebSettings::JavaEnabled, false);
     settings->setAttribute(QWebSettings::JavascriptEnabled, true);
     settings->setAttribute(QWebSettings::PrivateBrowsingEnabled, true);
     settings->setAttribute(QWebSettings::PluginsEnabled, false);
     settings->setAttribute(QWebSettings::ZoomTextOnly, true);
     settings->setUserStyleSheetUrl(QUrl::fromLocalFile(":/res/style.css"));
+    connect(page(), SIGNAL(contentsChanged()), this, SLOT(fixContents()));
 }
 
 Fb2WebView::~Fb2WebView()
@@ -28,6 +32,16 @@ Fb2WebView::~Fb2WebView()
     foreach (QString value, m_files) QFile::remove(value);
 }
 
+void Fb2WebView::fixContents()
+{
+    QWebElement doc = page()->mainFrame()->documentElement();
+    while (true) {
+        QWebElement span = doc.findFirst("span");
+        if (span.isNull()) break;
+        span.setOuterXml(span.toInnerXml());
+    }
+}
+
 bool Fb2WebView::load(const QString &filename)
 {
     if (m_thread) return false;

+ 3 - 0
source/fb2view.h

@@ -75,6 +75,9 @@ public slots:
     void zoomOut();
     void zoomOrig();
 
+private slots:
+    void fixContents();
+
 private:
     typedef QHash<QString, QString> StringHash;
     StringHash m_files;