Browse Source

Modify document tree

Kandrashin Denis 12 years ago
parent
commit
4ca0ace1bd
6 changed files with 98 additions and 43 deletions
  1. 20 6
      source/fb2html.cpp
  2. 1 0
      source/fb2html.h
  3. 18 1
      source/fb2text.cpp
  4. 3 2
      source/fb2text.hpp
  5. 55 34
      source/fb2tree.cpp
  6. 1 0
      source/fb2tree.hpp

+ 20 - 6
source/fb2html.cpp

@@ -140,6 +140,20 @@ void FbTextElement::getChildren(FbElementList &list)
     }
 }
 
+int FbTextElement::childIndex() const
+{
+    FbElementList list;
+    parent().getChildren(list);
+
+    int result = 0;
+    FbElementList::const_iterator it;
+    for (it = list.constBegin(); it != list.constEnd(); ++it) {
+        if (*it == *this) return result;
+        result++;
+    }
+    return -1;
+}
+
 bool FbTextElement::hasScheme() const
 {
     return subtypes();
@@ -210,7 +224,7 @@ bool FbTextElement::hasChild(const QString &style) const
 {
     FbTextElement child = firstChild();
     while (!child.isNull()) {
-        if (child.nodeName() == style) return true;
+        if (child.tagName() == style) return true;
         child = child.nextSibling();
     }
     return false;
@@ -218,27 +232,27 @@ bool FbTextElement::hasChild(const QString &style) const
 
 bool FbTextElement::isBody() const
 {
-    return nodeName() == "body";
+    return tagName() == "FB:BODY";
 }
 
 bool FbTextElement::isSection() const
 {
-    return nodeName() == "section";
+    return tagName() == "FB:SECTION";
 }
 
 bool FbTextElement::isTitle() const
 {
-    return nodeName() == "title";
+    return tagName() == "FB:TITLE";
 }
 
 bool FbTextElement::isStanza() const
 {
-    return nodeName() == "stanza";
+    return tagName() == "FB:STANZA";
 }
 
 bool FbTextElement::hasTitle() const
 {
-    return FbTextElement(firstChild()).isTitle();
+    return hasChild("FB:TITLE");
 }
 
 int FbTextElement::index() const

+ 1 - 0
source/fb2html.h

@@ -66,6 +66,7 @@ public:
     bool hasSubtype(const QString &style) const;
     bool hasScheme() const;
     QString location();
+    int childIndex() const;
     int index() const;
 
 public:

+ 18 - 1
source/fb2text.cpp

@@ -116,6 +116,11 @@ bool FbTextPage::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest
     return QWebPage::acceptNavigationRequest(frame, request, type);
 }
 
+QString FbTextPage::block(const QString &name)
+{
+    return block(name, p());
+}
+
 QString FbTextPage::block(const QString &name, const QString &text)
 {
     return QString("<fb:%1>%2</fb:%1>").arg(name).arg(text);
@@ -149,7 +154,7 @@ void FbTextPage::update()
     emit selectionChanged();
 }
 
-void FbTextPage::appendSection(const FbTextElement &parent)
+FbTextElement FbTextPage::appendSection(const FbTextElement &parent)
 {
     QString html = block("section", block("title", p()) + p());
     FbTextElement element = parent;
@@ -157,6 +162,18 @@ void FbTextPage::appendSection(const FbTextElement &parent)
     element = parent.lastChild();
     QUndoCommand * command = new FbInsertCmd(element);
     push(command, tr("Append section"));
+    return element;
+}
+
+FbTextElement FbTextPage::appendTitle(const FbTextElement &parent)
+{
+    QString html = block("title", p());
+    FbTextElement element = parent;
+    element.prependInside(html);
+    element = parent.firstChild();
+    QUndoCommand * command = new FbInsertCmd(element);
+    push(command, tr("Append section"));
+    return element;
 }
 
 void FbTextPage::insertBody()

+ 3 - 2
source/fb2text.hpp

@@ -78,7 +78,6 @@ class FbTextPage : public QWebPage
 
 public:
     explicit FbTextPage(QObject *parent = 0);
-
     FbNetworkAccessManager *temp();
     void push(QUndoCommand * command, const QString &text = QString());
     FbTextElement element(const QString &location);
@@ -89,7 +88,8 @@ public:
     FbTextElement body();
     FbTextElement doc();
 
-    void appendSection(const FbTextElement &parent);
+    FbTextElement appendSection(const FbTextElement &parent);
+    FbTextElement appendTitle(const FbTextElement &parent);
 
 public slots:
     void insertBody();
@@ -112,6 +112,7 @@ protected:
     void createBlock(const QString &name);
 
 protected:
+    static QString block(const QString &name);
     static QString block(const QString &name, const QString &text);
     static QString p(const QString &text = "<br/>");
     void update();

+ 55 - 34
source/fb2tree.cpp

@@ -535,38 +535,38 @@ void FbTreeView::contextMenu(const QPoint &pos)
     QMenu menu;
     menu.addAction(actionSection);
 
-    QString n = e.nodeName();
+    QString tag = e.tagName();
 
-    if (n == "body") {
-        if (!e.hasChild("image")) menu.addAction(actionImage);
-        if (!e.hasChild("title")) menu.addAction(actionTitle);
+    if (tag == "FB:BODY") {
+        if (!e.hasChild("IMG")) menu.addAction(actionImage);
+        if (!e.hasTitle()) menu.addAction(actionTitle);
         menu.addAction(actionEpigraph);
     }
 
-    if (n == "section") {
-        if (!e.hasChild("title")) menu.addAction(actionTitle);
+    if (tag == "FB:SECTION") {
+        if (!e.hasTitle()) menu.addAction(actionTitle);
         menu.addAction(actionEpigraph);
-        if (!e.hasChild("image")) menu.addAction(actionImage);
-        if (!e.hasChild("annotetion")) menu.addAction(actionAnnot);
+        if (!e.hasChild("IMG")) menu.addAction(actionImage);
+        if (!e.hasChild("FB:ANNOTATION")) menu.addAction(actionAnnot);
     }
 
-    if (n == "poem") {
-        if (!e.hasChild("title")) menu.addAction(actionTitle);
+    if (tag == "FB:POEM") {
+        if (!e.hasTitle()) menu.addAction(actionTitle);
         menu.addAction(actionEpigraph);
         menu.addAction(actionStanza);
         menu.addAction(actionAuthor);
         if (!e.hasChild("date")) menu.addAction(actionDate);
     }
 
-    if (n == "stanza") {
-        if (!e.hasChild("title")) menu.addAction(actionTitle);
+    if (tag == "FB:STANZA") {
+        if (!e.hasTitle()) menu.addAction(actionTitle);
     }
 
-    if (n == "epigraph") {
+    if (tag == "FB:EPIGRAPH") {
         menu.addAction(actionAuthor);
     }
 
-    if (n == "cite") {
+    if (tag == "FB:CITE") {
         menu.addAction(actionAuthor);
     }
 
@@ -629,7 +629,8 @@ QModelIndex FbTreeModel::append(const QModelIndex &parent, FbTextElement element
 {
     FbTreeItem * owner = item(parent);
     if (!owner || owner == m_root) return QModelIndex();
-    int row = owner->count();
+
+    int row = element.childIndex();
     FbTreeItem * child = new FbTreeItem(element);
     beginInsertRows(parent, row, row);
     owner->insert(child, row);
@@ -637,33 +638,53 @@ QModelIndex FbTreeModel::append(const QModelIndex &parent, FbTextElement element
     return createIndex(row, 0, (void*)child);
 }
 
+void FbTreeView::append(const QModelIndex &parent, FbTextElement element)
+{
+    FbTreeModel * m = model();
+    if (!m) return;
+
+    QModelIndex current = currentIndex();
+    QModelIndex index = m->append(parent, element);
+    setCurrentIndex(index);
+    emit QTreeView::currentChanged(index, current);
+    emit QTreeView::activated(index);
+    scrollTo(index);
+}
+
 void FbTreeView::insertSection()
 {
-    if (FbTreeModel * m = model()) {
-        QModelIndex index = currentIndex();
-        FbTreeItem * item = m->item(index);
-        if (!item) return;
-
-        FbTextElement element = item->element();
-        while (!element.isNull()) {
-            if (element.isSection() || element.isBody()) {
-                m_view.page()->appendSection(element);
-                QModelIndex result = m->append(index, element.lastChild());
-                if (!result.isValid()) return;
-                setCurrentIndex(result);
-                emit QTreeView::currentChanged(result, index);
-                emit QTreeView::activated(result);
-                scrollTo(result);
-                break;
-            }
-            element = element.parent();
-            index = m->parent(index);
+    FbTreeModel * m = model();
+    if (!m) return;
+
+    QModelIndex index = currentIndex();
+    FbTreeItem * item = m->item(index);
+    if (!item) return;
+
+    FbTextElement element = item->element();
+    while (!element.isNull()) {
+        if (element.isBody() || element.isSection()) {
+            element = m_view.page()->appendSection(element);
+            append(index, element);
+            break;
         }
+        element = element.parent();
+        index = m->parent(index);
     }
 }
 
 void FbTreeView::insertTitle()
 {
+    FbTreeModel * m = model();
+    if (!m) return;
+
+    QModelIndex index = currentIndex();
+    FbTreeItem * item = m->item(index);
+    if (!item) return;
+
+    FbTextElement element = item->element();
+    if (element.hasTitle()) return;
+    element = m_view.page()->appendTitle(element);
+    append(index, element);
 }
 
 void FbTreeView::insertAuthor()

+ 1 - 0
source/fb2tree.hpp

@@ -166,6 +166,7 @@ protected:
     void keyPressEvent(QKeyEvent *event);
 
 private:
+    void append(const QModelIndex &parent, FbTextElement element);
     void moveCurrent(int dx, int dy);
     FbTreeModel * model() const;