diff options
author | Kandrashin Denis <mail@lintest.ru> | 2012-10-18 17:17:34 +0400 |
---|---|---|
committer | Kandrashin Denis <mail@lintest.ru> | 2012-10-18 17:17:34 +0400 |
commit | 4ca0ace1bd424182662731e8a007aa81f170cda5 (patch) | |
tree | a8c4b6f4c857022d4d69d73d0bf24359c34e51cc | |
parent | 25942b59c20ea1d3e7b803daafc5f3d9cb5a044c (diff) |
Modify document tree
-rw-r--r-- | source/fb2html.cpp | 26 | ||||
-rw-r--r-- | source/fb2html.h | 1 | ||||
-rw-r--r-- | source/fb2text.cpp | 19 | ||||
-rw-r--r-- | source/fb2text.hpp | 5 | ||||
-rw-r--r-- | source/fb2tree.cpp | 89 | ||||
-rw-r--r-- | source/fb2tree.hpp | 1 |
6 files changed, 98 insertions, 43 deletions
diff --git a/source/fb2html.cpp b/source/fb2html.cpp index d81773c..fd087a9 100644 --- a/source/fb2html.cpp +++ b/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 diff --git a/source/fb2html.h b/source/fb2html.h index 70b6eed..90eaac5 100644 --- a/source/fb2html.h +++ b/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: diff --git a/source/fb2text.cpp b/source/fb2text.cpp index 652cb01..fd888c4 100644 --- a/source/fb2text.cpp +++ b/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() diff --git a/source/fb2text.hpp b/source/fb2text.hpp index f548231..1f73733 100644 --- a/source/fb2text.hpp +++ b/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(); diff --git a/source/fb2tree.cpp b/source/fb2tree.cpp index 5866875..a333c14 100644 --- a/source/fb2tree.cpp +++ b/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() diff --git a/source/fb2tree.hpp b/source/fb2tree.hpp index 52dd44d..a336803 100644 --- a/source/fb2tree.hpp +++ b/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; |