Jelajahi Sumber

Navigation by tree view

Kandrashin Denis 13 tahun lalu
induk
melakukan
8fdafd157b
7 mengubah file dengan 28 tambahan dan 27 penghapusan
  1. 3 11
      source/fb2main.cpp
  2. 11 1
      source/fb2read.cpp
  3. 2 0
      source/fb2read.h
  4. 4 12
      source/fb2tree.cpp
  5. 5 0
      source/fb2tree.h
  6. 2 2
      source/fb2view.cpp
  7. 1 1
      source/fb2view.h

+ 3 - 11
source/fb2main.cpp

@@ -82,19 +82,11 @@ void Fb2MainWindow::logDestroyed()
     messageEdit = NULL;
 }
 
-static QTextFrame * findFrame(QTextFrame *root, const QModelIndex &index)
-{
-    if (!index.isValid()) return root;
-    if (QTextFrame *frame = findFrame(root, index.parent())) {
-        QTextFrame *child = static_cast<QTextFrame*>(index.internalPointer());
-        int i = frame->childFrames().indexOf(child);
-        if (i >= 0) return child;
-    }
-    return NULL;
-}
-
 void Fb2MainWindow::treeActivated(const QModelIndex &index)
 {
+    if (!treeView) return;
+    Fb2TreeModel *model = dynamic_cast<Fb2TreeModel*>(treeView->model());
+    if (model) model->select(index);
 }
 
 void Fb2MainWindow::treeDestroyed()

+ 11 - 1
source/fb2read.cpp

@@ -59,6 +59,7 @@ bool Fb2ReadThread::parse()
 Fb2HtmlWriter::Fb2HtmlWriter(Fb2ReadThread &thread)
     : QXmlStreamWriter(thread.data())
     , m_thread(thread)
+    , m_id(0)
 {
 }
 
@@ -86,6 +87,11 @@ QString Fb2HtmlWriter::getFile(const QString &name)
     }
 }
 
+QString Fb2HtmlWriter::newId()
+{
+    return QString("FB2E%1").arg(++m_id);
+}
+
 //---------------------------------------------------------------------------
 //  Fb2Handler::BaseHandler
 //---------------------------------------------------------------------------
@@ -271,7 +277,11 @@ Fb2Handler::BodyHandler::BodyHandler(Fb2HtmlWriter &writer, const QString &name,
     if (m_tag.isEmpty()) return;
     m_writer.writeStartElement(tag);
     QString id = Value(attributes, "id");
-    if (!id.isEmpty()) m_writer.writeAttribute("id", id);
+    if (!id.isEmpty()) {
+        m_writer.writeAttribute("id", id);
+    } else if (m_tag == "div" || m_tag == "img") {
+        m_writer.writeAttribute("id", m_writer.newId());
+    }
     if (!style.isEmpty()) m_writer.writeAttribute("class", style);
 }
 

+ 2 - 0
source/fb2read.h

@@ -48,10 +48,12 @@ public:
     explicit Fb2HtmlWriter(Fb2ReadThread &thread);
     QString addFile(const QString &name, const QByteArray &data);
     QString getFile(const QString &name);
+    QString newId();
 private:
     typedef QHash<QString, QString> StringHash;
     Fb2ReadThread &m_thread;
     StringHash m_hash;
+    int m_id;
 };
 
 #define FB2_BEGIN_KEYLIST private: enum Keyword {

+ 4 - 12
source/fb2tree.cpp

@@ -20,6 +20,7 @@ Fb2TreeItem::Fb2TreeItem(QWebElement &element, Fb2TreeItem *parent)
     } else if (m_name == "img") {
         m_text = element.attribute("alt");
     }
+    m_id = element.attribute("id");
     QWebElement child = element.firstChild();
     while (!child.isNull()) {
         QString tag = child.tagName().toLower();
@@ -143,16 +144,7 @@ QVariant Fb2TreeModel::data(const QModelIndex &index, int role) const
 
 void Fb2TreeModel::select(const QModelIndex &index)
 {
-/*
-    Fb2TreeItem * i = item(index);
-    if (!i) return;
-
-    QTextFrame * f = i->frame();
-    if (!f) return;
-
-    QTextCursor cursor = m_text.textCursor();
-    cursor.setPosition(f->firstPosition());
-    m_text.moveCursor(QTextCursor::End);
-    m_text.setTextCursor(cursor);
-*/
+    Fb2TreeItem *node = item(index);
+    if (!node || node->id().isEmpty()) return;
+    m_view.page()->mainFrame()->scrollToAnchor(node->id());
 }

+ 5 - 0
source/fb2tree.h

@@ -36,12 +36,17 @@ public:
 
     QString text() const;
 
+    const QString & id() const {
+        return m_id;
+    }
+
 private:
     QList<Fb2TreeItem*> m_list;
     QString m_name;
     QString m_text;
     Fb2TreeItem * m_parent;
     QWebElement m_element;
+    QString m_id;
 };
 
 class Fb2TreeModel: public QAbstractItemModel

+ 2 - 2
source/fb2view.cpp

@@ -42,9 +42,9 @@ void Fb2WebView::fixContents()
     }
 }
 
-bool Fb2WebView::load(const QString &filename)
+void Fb2WebView::load(const QString &filename)
 {
-    if (m_thread) return false;
+    if (m_thread) return;
     m_thread = new Fb2ReadThread(this, filename);
     connect(m_thread, SIGNAL(file(QString, QString)), SLOT(file(QString, QString)));
     connect(m_thread, SIGNAL(html(QString, QString)), SLOT(html(QString, QString)));

+ 1 - 1
source/fb2view.h

@@ -44,7 +44,7 @@ class Fb2WebView : public Fb2BaseWebView
 public:
     explicit Fb2WebView(QWidget *parent = 0);
     virtual ~Fb2WebView();
-    bool load(const QString &filename);
+    void load(const QString &filename);
 
     bool UndoEnabled();
     bool RedoEnabled();