Browse Source

Small changes

Kandrashin Denis 13 năm trước cách đây
mục cha
commit
a9c13a32f5
7 tập tin đã thay đổi với 106 bổ sung22 xóa
  1. 2 1
      fb2edit.pro
  2. 9 6
      source/fb2main.cpp
  3. 1 0
      source/fb2main.hpp
  4. 53 6
      source/fb2tree.cpp
  5. 27 7
      source/fb2tree.hpp
  6. 11 0
      source/js/get_location.js
  7. 3 2
      source/js/javascript.qrc

+ 2 - 1
fb2edit.pro

@@ -48,7 +48,8 @@ OTHER_FILES += \
     source/res/blank.fb2 \
     source/js/export.js \
     source/js/set_cursor.js \
-    source/js/get_status.js
+    source/js/get_status.js \
+    source/js/get_location.js
 
 if (unix) {
 

+ 9 - 6
source/fb2main.cpp

@@ -191,12 +191,9 @@ void Fb2MainWindow::documentWasModified()
     if (isWindowModified()) return;
     QFileInfo info = windowFilePath();
     QString title = info.fileName();
-    title += QString("[*]");
-    title += QString(" - ") += qApp->applicationName();
+    title += QString("[*]") += appTitle();
     setWindowTitle(title);
     setWindowModified(true);
-    if (codeEdit) disconnect(codeEdit, SIGNAL(textChanged()), this, SLOT(documentWasModified()));
-    if (textEdit) disconnect(textEdit->page(), SIGNAL(contentsChanged()), this, SLOT(documentWasModified()));
 }
 
 void Fb2MainWindow::createActions()
@@ -431,8 +428,9 @@ void Fb2MainWindow::openSettings()
 void Fb2MainWindow::createTree()
 {
     if (treeView) return;
-    treeView = new QTreeView(this);
+    treeView = new Fb2TreeView(this);
     treeView->setHeaderHidden(true);
+    connect(textEdit->page(), SIGNAL(selectionChanged()), treeView, SLOT(select()));
     connect(treeView, SIGNAL(activated(QModelIndex)), SLOT(treeActivated(QModelIndex)));
     connect(treeView, SIGNAL(destroyed()), SLOT(treeDestroyed()));
     dockTree = new QDockWidget(tr("Contents"), this);
@@ -557,13 +555,18 @@ void Fb2MainWindow::setCurrentFile(const QString &filename)
         curFile = info.canonicalFilePath();
         title = info.fileName();
     }
-    title += QString(" - ") += qApp->applicationName() += QString(" ") += qApp->applicationVersion();
+    title += appTitle();
 
     setWindowModified(false);
     setWindowFilePath(curFile);
     setWindowTitle(title);
 }
 
+QString Fb2MainWindow::appTitle() const
+{
+    return QString(" - ") += qApp->applicationName() += QString(" ") += qApp->applicationVersion();
+}
+
 Fb2MainWindow *Fb2MainWindow::findFb2MainWindow(const QString &fileName)
 {
     QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();

+ 1 - 0
source/fb2main.hpp

@@ -59,6 +59,7 @@ private slots:
 
 private:
     bool loadXML(const QString &filename);
+    QString appTitle() const;
 
 private:
     void init();

+ 53 - 6
source/fb2tree.cpp

@@ -3,11 +3,11 @@
 #include <QtDebug>
 #include <QWebFrame>
 #include <QWebPage>
-#include <QWebView>
 #include <QTreeView>
 #include <QUrl>
 
 #include "fb2utils.h"
+#include "fb2view.hpp"
 
 Fb2TreeItem::Fb2TreeItem(QWebElement &element, Fb2TreeItem *parent)
     : QObject(parent)
@@ -46,17 +46,21 @@ QString Fb2TreeItem::title(const QWebElement &element)
     return element.toPlainText().left(255).simplified();
 }
 
-void Fb2TreeItem::addChildren(QWebElement &parent)
+void Fb2TreeItem::addChildren(QWebElement &parent, bool direct)
 {
     QWebElement child = parent.firstChild();
     while (!child.isNull()) {
         QString tag = child.tagName().toLower();
         if (tag == "div") {
-            m_list << new Fb2TreeItem(child, this);
+            Fb2TreeItem * item = new Fb2TreeItem(child, this);
+            m_list << item;
+            if (direct) m_content << item;
         } else if (tag == "img") {
-            m_list << new Fb2TreeItem(child, this);
+            Fb2TreeItem * item = new Fb2TreeItem(child, this);
+            m_list << item;
+            if (direct) m_content << item;
         } else {
-            addChildren(child);
+            addChildren(child, false);
         }
         child = child.nextSibling();
     }
@@ -103,11 +107,16 @@ QString Fb2TreeItem::selector() const
     return selector.prepend("$('html')");
 }
 
+Fb2TreeItem * Fb2TreeItem::content(int index) const
+{
+    return (0 <= index && index < m_content.count()) ? m_content[index] : 0;
+}
+
 //---------------------------------------------------------------------------
 //  Fb2TreeModel
 //---------------------------------------------------------------------------
 
-Fb2TreeModel::Fb2TreeModel(QWebView &view, QObject *parent)
+Fb2TreeModel::Fb2TreeModel(Fb2WebView &view, QObject *parent)
     : QAbstractItemModel(parent)
     , m_view(view)
     , m_root(NULL)
@@ -198,3 +207,41 @@ void Fb2TreeModel::select(const QModelIndex &index)
 
     m_view.setFocus();
 }
+
+QModelIndex Fb2TreeModel::index(const QString &location) const
+{
+    QModelIndex index;
+    Fb2TreeItem * parent = m_root;
+    QStringList list = location.split(",");
+    QStringListIterator iterator(list);
+    while (parent && iterator.hasNext()) {
+        QString str = iterator.next();
+        if (str.left(5) == "HTML=") continue;
+        int key = str.mid(str.indexOf("=")+1).toInt();
+        Fb2TreeItem * child = parent->content(key);
+        if (child) index = this->index(key, 0, index);
+        parent = child;
+    }
+    return QModelIndex();
+}
+
+//---------------------------------------------------------------------------
+//  Fb2TreeView
+//---------------------------------------------------------------------------
+
+void Fb2TreeView::select()
+{
+    return;
+    if (model() == 0) return;
+    Fb2TreeModel * model = static_cast<Fb2TreeModel*>(this->model());
+    QWebFrame * frame = model->view().page()->mainFrame();
+    static const QString javascript = FB2::read(":/js/get_location.js");
+    QString location = frame->evaluateJavaScript(javascript).toString();
+    QModelIndex index = model->index(location);
+    setCurrentIndex(index);
+    this->expand(index);
+}
+
+void Fb2TreeView::update()
+{
+}

+ 27 - 7
source/fb2tree.hpp

@@ -2,12 +2,10 @@
 #define FB2TREE_H
 
 #include <QAbstractItemModel>
+#include <QTreeView>
 #include <QWebElement>
-#include <QWebView>
 
-QT_BEGIN_NAMESPACE
-class QTreeView;
-QT_END_NAMESPACE
+class Fb2WebView;
 
 class Fb2TreeItem: public QObject
 {
@@ -30,6 +28,10 @@ public:
         return m_list.size();
     }
 
+    const QWebElement element() const {
+        return element();
+    }
+
     Fb2TreeItem * parent() const {
         return m_parent;
     }
@@ -46,12 +48,15 @@ public:
 
     QString selector() const;
 
+    Fb2TreeItem * content(int index) const;
+
 private:
     QString static title(const QWebElement &element);
-    void addChildren(QWebElement &parent);
+    void addChildren(QWebElement &parent, bool direct = true);
 
 private:
     QList<Fb2TreeItem*> m_list;
+    QList<Fb2TreeItem*> m_content;
     QWebElement m_element;
     QString m_name;
     QString m_text;
@@ -63,8 +68,10 @@ class Fb2TreeModel: public QAbstractItemModel
     Q_OBJECT
 
 public:
-    explicit Fb2TreeModel(QWebView &view, QObject *parent = 0);
+    explicit Fb2TreeModel(Fb2WebView &view, QObject *parent = 0);
     virtual ~Fb2TreeModel();
+    QModelIndex index(const QString &location) const;
+    Fb2WebView & view() { return m_view; }
     void select(const QModelIndex &index);
     void expand(QTreeView *view);
 
@@ -79,8 +86,21 @@ protected:
     Fb2TreeItem * item(const QModelIndex &index) const;
 
 private:
-    QWebView & m_view;
+    Fb2WebView & m_view;
     Fb2TreeItem * m_root;
 };
 
+class Fb2TreeView : public QTreeView
+{
+    Q_OBJECT
+
+public:
+    explicit Fb2TreeView(QWidget *parent = 0) : QTreeView(parent) {}
+
+public slots:
+    void select();
+    void update();
+
+};
+
 #endif // FB2TREE_H

+ 11 - 0
source/js/get_location.js

@@ -0,0 +1,11 @@
+var startNode = document.getSelection().anchorNode;
+// create a reverse function for jQuery
+$.fn.reverse = [].reverse; 
+var hierarchy = $( startNode ).parents().reverse().add( $( startNode ) ) ;
+hierarchy.map(function () { 
+    if ( undefined !== $( this ).parent().get( 0 ).tagName )
+	{
+		var first_part = $( this ).parent().get( 0 ).tagName + "=";
+		return first_part + jQuery.inArray( this, $( this ).parent().contents() );
+	}
+}).get().join(",");

+ 3 - 2
source/js/javascript.qrc

@@ -1,8 +1,9 @@
 <RCC>
     <qresource prefix="/js">
         <file alias="jquery.js">../../3rdparty/jQuery/jquery.js</file>
-        <file>set_cursor.js</file>
-        <file>get_status.js</file>
         <file>export.js</file>
+        <file>get_status.js</file>
+        <file>get_location.js</file>
+        <file>set_cursor.js</file>
     </qresource>
 </RCC>