فهرست منبع

Optimize Fb2TreeModel update

Kandrashin Denis 13 سال پیش
والد
کامیت
f1311067e9
4فایلهای تغییر یافته به همراه52 افزوده شده و 69 حذف شده
  1. 16 0
      source/fb2html.cpp
  2. 5 0
      source/fb2html.h
  3. 23 62
      source/fb2tree.cpp
  4. 8 7
      source/fb2tree.hpp

+ 16 - 0
source/fb2html.cpp

@@ -7,6 +7,22 @@
 //  Fb2TextElement
 //---------------------------------------------------------------------------
 
+void Fb2TextElement::getChildren(Fb2ElementList &list)
+{
+    Fb2TextElement child = firstChild();
+    while (!child.isNull()) {
+        QString tag = child.tagName().toLower();
+        if (tag == "div") {
+            if (child.hasAttribute("class")) list << child;
+        } else if (tag == "img") {
+            list << child;
+        } else {
+            child.getChildren(list);
+        }
+        child = child.nextSibling();
+    }
+}
+
 QString Fb2TextElement::location()
 {
     static const QString javascript = FB2::read(":/js/get_location.js").prepend("var element=this;");

+ 5 - 0
source/fb2html.h

@@ -6,12 +6,17 @@
 
 class Fb2TextPage;
 
+class Fb2TextElement;
+
+typedef QList<Fb2TextElement> Fb2ElementList;
+
 class Fb2TextElement : public QWebElement
 {
 public:
     Fb2TextElement() {}
     Fb2TextElement(const QWebElement &x) : QWebElement(x) {}
     Fb2TextElement &operator=(const QWebElement &x) { QWebElement::operator=(x); return *this; }
+    void getChildren(Fb2ElementList &list);
     QString location();
     bool isSection() const;
     bool isTitle() const;

+ 23 - 62
source/fb2tree.cpp

@@ -25,7 +25,6 @@ Fb2TreeItem::Fb2TreeItem(QWebElement &element, Fb2TreeItem *parent, int number)
     , m_number(number)
 {
     init();
-    addChildren(element);
 }
 
 Fb2TreeItem::~Fb2TreeItem()
@@ -62,33 +61,6 @@ QString Fb2TreeItem::title()
     return m_element.toPlainText().left(255).simplified();
 }
 
-void Fb2TreeItem::addChildren(QWebElement &parent, bool direct, bool header)
-{
-    int number = 0;
-    QWebElement child = parent.firstChild();
-    while (!child.isNull()) {
-        QString tag = child.tagName().toLower();
-        if (tag == "div") {
-            bool h = header;
-            QString style = child.attribute("class").toLower();
-            h = h || style == "description" || style == "stylesheet";
-            if (!h || style == "annotation" || style == "history") {
-                m_list << new Fb2TreeItem(child, this, direct ? number : -1);
-            } else {
-                addChildren(child, false, h);
-            }
-        } else if (header) {
-            // skip
-        } else if (tag == "img") {
-            m_list << new Fb2TreeItem(child, this, direct ? number : -1);
-        } else {
-            addChildren(child, false, header);
-        }
-        child = child.nextSibling();
-        number++;
-    }
-}
-
 Fb2TreeItem * Fb2TreeItem::item(const QModelIndex &index) const
 {
     int row = index.row();
@@ -132,16 +104,13 @@ QString Fb2TreeItem::selector() const
     return selector.prepend("$('html')");
 }
 
-Fb2TreeItem * Fb2TreeItem::content(const Fb2TreeModel &model, int number, QModelIndex &index) const
+Fb2TreeItem * Fb2TreeItem::content(const Fb2TreeModel &model, int number) const
 {
-    int row = 0;
-    QList<Fb2TreeItem*>::const_iterator i;
-    for (i = m_list.constBegin(); i != m_list.constEnd(); ++i) {
-        if ((*i)->m_number == number) {
-            index = model.index(row, 0, index);
-            return *i;
-        }
-        row++;
+    Fb2TextElement element = m_element.firstChild();
+    while (number-- > 0) element = element.nextSibling();
+    Fb2TreeList::const_iterator it;
+    for (it = m_list.constBegin(); it != m_list.constEnd(); it++) {
+        if ((*it)->element() == element) return *it;
     }
     return 0;
 }
@@ -233,7 +202,7 @@ void Fb2TreeModel::selectText(const QModelIndex &index)
 
 QModelIndex Fb2TreeModel::index(const QString &location) const
 {
-    QModelIndex index;
+    QModelIndex result;
     Fb2TreeItem * parent = m_root;
     QStringList list = location.split(",");
     QStringListIterator iterator(list);
@@ -241,10 +210,11 @@ QModelIndex Fb2TreeModel::index(const QString &location) const
         QString str = iterator.next();
         if (str.left(5) == "HTML=") continue;
         int key = str.mid(str.indexOf("=")+1).toInt();
-        Fb2TreeItem * child = parent->content(*this, key, index);
+        Fb2TreeItem * child = parent->content(*this, key);
+        if (child) result = index(child);
         parent = child;
     }
-    return index;
+    return result;
 }
 
 QModelIndex Fb2TreeModel::move(const QModelIndex &index, int dx, int dy)
@@ -339,32 +309,15 @@ bool Fb2TreeModel::removeRows(int row, int count, const QModelIndex &parent)
     return true;
 }
 
-void Fb2TreeModel::children(QWebElement parent, Fb2ElementList &list)
-{
-    QWebElement child = parent.firstChild();
-    while (!child.isNull()) {
-        QString tag = child.tagName().toLower();
-        if (tag == "div") {
-            list << child;
-        } else if (tag == "img") {
-            list << child;
-        } else {
-            children(child, list);
-        }
-        child = child.nextSibling();
-    }
-}
-
-
 void Fb2TreeModel::update(Fb2TreeItem &owner)
 {
     owner.init();
     Fb2ElementList list;
-    children(owner.element(), list);
+    owner.element().getChildren(list);
 
     int pos = 0;
     QModelIndex index = this->index(&owner);
-    for (QList<QWebElement>::iterator it = list.begin(); it != list.end(); it++) {
+    for (Fb2ElementList::iterator it = list.begin(); it != list.end(); it++) {
         Fb2TreeItem * child = 0;
         QWebElement element = *it;
         int count = owner.count();
@@ -391,6 +344,7 @@ void Fb2TreeModel::update(Fb2TreeItem &owner)
             beginInsertRows(index, pos, pos);
             owner.insert(child, pos);
             endInsertRows();
+            update(*child);
         }
         pos++;
     }
@@ -405,11 +359,17 @@ void Fb2TreeModel::update(Fb2TreeItem &owner)
 
 void Fb2TreeModel::update()
 {
-    if (!m_root) return;
     QWebElement doc = m_view.page()->mainFrame()->documentElement();
     QWebElement body = doc.findFirst("body");
-    if (m_root->element() != body) *m_root = body;
-    update(*m_root);
+    if (m_root) {
+        if (m_root->element() != body) *m_root = body;
+        update(*m_root);
+    } else {
+        if (!body.isNull()) {
+            m_root = new Fb2TreeItem(body);
+            update(*m_root);
+        }
+    }
 }
 
 //---------------------------------------------------------------------------
@@ -566,6 +526,7 @@ void Fb2TreeView::updateTree()
         m->update();
     } else {
         m = new Fb2TreeModel(m_view, this);
+        m->update();
         setModel(m);
     }
     selectTree();

+ 8 - 7
source/fb2tree.hpp

@@ -15,8 +15,12 @@ QT_END_NAMESPACE
 
 class Fb2TextEdit;
 
+class Fb2TreeItem;
+
 class Fb2TreeModel;
 
+typedef QList<Fb2TreeItem*> Fb2TreeList;
+
 class Fb2TreeItem: public QObject
 {
     Q_OBJECT
@@ -60,8 +64,6 @@ public:
         return m_parent;
     }
 
-    QString text() const;
-
     const QString & name() const {
         return m_name;
     }
@@ -70,18 +72,19 @@ public:
         return m_element.geometry().topLeft();
     }
 
+    Fb2TreeItem * content(const Fb2TreeModel &model, int number) const;
+
     QString selector() const;
 
-    Fb2TreeItem * content(const Fb2TreeModel &model, int number, QModelIndex &index) const;
+    QString text() const;
 
     void init();
 
 private:
-    void addChildren(QWebElement &parent, bool direct = true, bool header = false);
     QString title();
 
 private:
-    QList<Fb2TreeItem*> m_list;
+    Fb2TreeList m_list;
     QWebElement m_element;
     QString m_name;
     QString m_text;
@@ -115,8 +118,6 @@ public:
     virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
 
 private:
-    typedef QList<QWebElement> Fb2ElementList;
-    void children(QWebElement parent, Fb2ElementList &list);
     void update(Fb2TreeItem &item);
 
 private: