瀏覽代碼

Automatic update and select document tree

Kandrashin Denis 13 年之前
父節點
當前提交
03c177d9cd
共有 6 個文件被更改,包括 77 次插入65 次删除
  1. 1 24
      source/fb2main.cpp
  2. 2 3
      source/fb2main.hpp
  3. 61 11
      source/fb2tree.cpp
  4. 13 5
      source/fb2tree.hpp
  5. 0 15
      source/fb2view.cpp
  6. 0 7
      source/fb2view.hpp

+ 1 - 24
source/fb2main.cpp

@@ -83,15 +83,6 @@ void Fb2MainWindow::logDestroyed()
     messageEdit = NULL;
 }
 
-void Fb2MainWindow::treeActivated(const QModelIndex &index)
-{
-    if (!treeView) return;
-    Fb2TreeModel *model = dynamic_cast<Fb2TreeModel*>(treeView->model());
-    if (!model) return;
-    model->select(index);
-    selectionChanged();
-}
-
 void Fb2MainWindow::treeDestroyed()
 {
     treeView = NULL;
@@ -448,11 +439,7 @@ void Fb2MainWindow::openSettings()
 void Fb2MainWindow::createTree()
 {
     if (treeView) return;
-    treeView = new Fb2TreeView(this);
-    treeView->setHeaderHidden(true);
-    connect(textEdit, SIGNAL(updateTree()), SLOT(updateTree()));
-    connect(textEdit, SIGNAL(selectTree()), treeView, SLOT(select()));
-    connect(treeView, SIGNAL(activated(QModelIndex)), SLOT(treeActivated(QModelIndex)));
+    treeView = new Fb2TreeView(*textEdit, this);
     connect(treeView, SIGNAL(destroyed()), SLOT(treeDestroyed()));
     dockTree = new QDockWidget(tr("Contents"), this);
     dockTree->setAttribute(Qt::WA_DeleteOnClose);
@@ -465,7 +452,6 @@ void Fb2MainWindow::createTree()
 void Fb2MainWindow::loadFinished(bool ok)
 {
     Q_UNUSED(ok);
-    updateTree();
     if (headTree) {
         Fb2HeadModel *model = new Fb2HeadModel(*textEdit, treeView);
         headTree->setModel(model);
@@ -473,15 +459,6 @@ void Fb2MainWindow::loadFinished(bool ok)
     }
 }
 
-void Fb2MainWindow::updateTree()
-{
-    if (treeView) {
-        Fb2TreeModel *model = new Fb2TreeModel(*textEdit, treeView);
-        treeView->setModel(model);
-        model->expand(treeView);
-    }
-}
-
 void Fb2MainWindow::selectionChanged()
 {
     actionCut->setEnabled(textEdit->CutEnabled());

+ 2 - 3
source/fb2main.hpp

@@ -15,6 +15,7 @@ class QWebInspector;
 QT_END_NAMESPACE
 
 class Fb2CodeEdit;
+class Fb2TreeView;
 class Fb2WebView;
 
 class Fb2MainWindow : public QMainWindow
@@ -41,11 +42,9 @@ private slots:
     void about();
     void documentWasModified();
     void checkScintillaUndo();
-    void treeActivated(const QModelIndex &index);
     void treeDestroyed();
     void logDestroyed();
     void logShowed();
-    void updateTree();
     void viewCode();
     void viewText();
     void viewHead();
@@ -82,7 +81,7 @@ private:
     QTextEdit *messageEdit;
     QDockWidget *dockTree;
     Fb2CodeEdit *codeEdit;
-    QTreeView *treeView;
+    Fb2TreeView *treeView;
     QString curFile;
     bool isUntitled;
 

+ 61 - 11
source/fb2tree.cpp

@@ -216,7 +216,7 @@ void Fb2TreeModel::select(const QModelIndex &index)
     m_view.setFocus();
 }
 
-QModelIndex Fb2TreeModel::index(const QString &location) const
+QModelIndex Fb2TreeModel::index(const QString &location, QModelIndex current) const
 {
     QModelIndex index;
     Fb2TreeItem * parent = m_root;
@@ -229,6 +229,12 @@ QModelIndex Fb2TreeModel::index(const QString &location) const
         Fb2TreeItem * child = parent->content(*this, key, index);
         parent = child;
     }
+
+    while (current.isValid()) {
+        if (current == index) return QModelIndex();
+        current = this->parent(current);
+    }
+
     return index;
 }
 
@@ -236,23 +242,67 @@ QModelIndex Fb2TreeModel::index(const QString &location) const
 //  Fb2TreeView
 //---------------------------------------------------------------------------
 
-void Fb2TreeView::select()
+Fb2TreeView::Fb2TreeView(Fb2WebView &view, QWidget *parent)
+    : QTreeView(parent)
+    , m_view(view)
+{
+    setHeaderHidden(true);
+    connect(&m_view, SIGNAL(loadFinished(bool)), SLOT(updateTree()));
+    connect(&m_view, SIGNAL(updateTree()), SLOT(updateTree()));
+    connect(&m_view, SIGNAL(selectTree()), SLOT(selectTree()));
+    connect(this, SIGNAL(activated(QModelIndex)), SLOT(activated(QModelIndex)));
+    connect(m_view.page(), SIGNAL(contentsChanged()), SLOT(contentsChanged()));
+    connect(m_view.page(), SIGNAL(selectionChanged()), SLOT(selectionChanged()));
+
+    m_timerSelect.setInterval(1000);
+    m_timerSelect.setSingleShot(true);
+    connect(&m_timerSelect, SIGNAL(timeout()), SLOT(selectTree()));
+
+    m_timerUpdate.setInterval(1000);
+    m_timerUpdate.setSingleShot(true);
+    connect(&m_timerUpdate, SIGNAL(timeout()), SLOT(updateTree()));
+}
+
+void Fb2TreeView::selectionChanged()
+{
+    m_timerSelect.start();
+}
+
+void Fb2TreeView::contentsChanged()
+{
+    m_timerUpdate.start();
+}
+
+void Fb2TreeView::activated(const QModelIndex &index)
+{
+    if (!model()) return;
+    Fb2TreeModel *model = static_cast<Fb2TreeModel*>(this->model());
+
+    disconnect(m_view.page(), SIGNAL(selectionChanged()), this, SLOT(selectionChanged()));
+    model->select(index);
+    connect(m_view.page(), SIGNAL(selectionChanged()), this, SLOT(selectionChanged()));
+}
+
+void Fb2TreeView::selectTree()
 {
     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);
-    if (index.isValid()) {
-        setCurrentIndex(index);
-        scrollTo(index);
-    }
+    QModelIndex index = model->index(location, currentIndex());
+    if (!index.isValid()) return;
+
+    disconnect(this, SIGNAL(activated(QModelIndex)), this, SLOT(activated(QModelIndex)));
+    setCurrentIndex(index);
+    scrollTo(index);
+    connect(this, SIGNAL(activated(QModelIndex)), this, SLOT(activated(QModelIndex)));
 }
 
-void Fb2TreeView::update()
+void Fb2TreeView::updateTree()
 {
-    if (model() == 0) return;
-    Fb2TreeModel * model = static_cast<Fb2TreeModel*>(this->model());
-    Fb2WebView & view = model->view();
+    Fb2TreeModel * model = new Fb2TreeModel(m_view, this);
+    setModel(model);
+    model->expand(this);
+    selectTree();
 }

+ 13 - 5
source/fb2tree.hpp

@@ -3,6 +3,7 @@
 
 #include <QAbstractItemModel>
 #include <QTreeView>
+#include <QTimer>
 #include <QWebElement>
 
 class Fb2WebView;
@@ -72,7 +73,7 @@ class Fb2TreeModel: public QAbstractItemModel
 public:
     explicit Fb2TreeModel(Fb2WebView &view, QObject *parent = 0);
     virtual ~Fb2TreeModel();
-    QModelIndex index(const QString &location) const;
+    QModelIndex index(const QString &location, QModelIndex current) const;
     Fb2WebView & view() { return m_view; }
     void select(const QModelIndex &index);
     void expand(QTreeView *view);
@@ -97,12 +98,19 @@ class Fb2TreeView : public QTreeView
     Q_OBJECT
 
 public:
-    explicit Fb2TreeView(QWidget *parent = 0) : QTreeView(parent) {}
+    explicit Fb2TreeView(Fb2WebView &view, QWidget *parent = 0);
 
-public slots:
-    void select();
-    void update();
+private slots:
+    void activated(const QModelIndex &index);
+    void contentsChanged();
+    void selectionChanged();
+    void selectTree();
+    void updateTree();
 
+private:
+    Fb2WebView & m_view;
+    QTimer m_timerSelect;
+    QTimer m_timerUpdate;
 };
 
 #endif // FB2TREE_H

+ 0 - 15
source/fb2view.cpp

@@ -93,16 +93,7 @@ Fb2WebView::Fb2WebView(QWidget *parent)
     page()->setNetworkAccessManager(new Fb2NetworkAccessManager(*this));
     page()->setContentEditable(true);
     connect(page(), SIGNAL(contentsChanged()), this, SLOT(fixContents()));
-    connect(page(), SIGNAL(selectionChanged()), this, SLOT(selectionChanged()));
     connect(page(), SIGNAL(linkHovered(QString,QString,QString)), this, SLOT(linkHovered(QString,QString,QString)));
-
-    m_timerSelect.setInterval(1000);
-    m_timerSelect.setSingleShot(true);
-    connect(&m_timerSelect, SIGNAL(timeout()), SIGNAL(selectTree()));
-
-    m_timerUpdate.setInterval(1000);
-    m_timerUpdate.setSingleShot(true);
-    connect(&m_timerUpdate, SIGNAL(timeout()), SIGNAL(updateTree()));
 }
 
 Fb2WebView::~Fb2WebView()
@@ -139,12 +130,6 @@ void Fb2WebView::fixContents()
     foreach (QWebElement span, doc().findAll("span.apple-style-span[style]")) {
         span.removeAttribute("style");
     }
-    m_timerUpdate.start();
- }
-
-void Fb2WebView::selectionChanged()
-{
-    m_timerSelect.start();
 }
 
 void Fb2WebView::mouseMoveEvent(QMouseEvent *event)

+ 0 - 7
source/fb2view.hpp

@@ -85,10 +85,6 @@ public:
 protected:
     virtual void mouseMoveEvent(QMouseEvent *event);
 
-signals:
-    void selectTree();
-    void updateTree();
-
 public slots:
     void data(QString name, QByteArray data);
     void html(QString name, QString html);
@@ -103,7 +99,6 @@ public slots:
     void find();
 
 private slots:
-    void selectionChanged();
     void fixContents();
 
 private:
@@ -113,8 +108,6 @@ private:
     QWebElement doc();
 
 private:
-    QTimer m_timerUpdate;
-    QTimer m_timerSelect;
     QWebInspector * m_inspector;
     Fb2TemporaryList m_files;
     Fb2NoteView *m_noteView;