浏览代码

Move <section> element: left and right

Move <section> element: left and right
Kandrashin Denis 13 年之前
父节点
当前提交
980c06967f
共有 8 个文件被更改,包括 107 次插入21 次删除
  1. 48 0
      source/fb2html.cpp
  2. 23 0
      source/fb2html.h
  3. 2 2
      source/fb2read.cpp
  4. 1 1
      source/fb2read.hpp
  5. 1 1
      source/fb2text.cpp
  6. 1 1
      source/fb2text.hpp
  7. 30 15
      source/fb2tree.cpp
  8. 1 1
      source/fb2tree.hpp

+ 48 - 0
source/fb2html.cpp

@@ -139,3 +139,51 @@ void FbMoveUpCmd::undo()
     subling.appendOutside(m_element.takeFromDocument());
 }
 
+
+//---------------------------------------------------------------------------
+//  FbMoveLeftCmd
+//---------------------------------------------------------------------------
+
+FbMoveLeftCmd::FbMoveLeftCmd(const FbTextElement &element)
+    : QUndoCommand()
+    , m_element(element)
+    , m_subling(element.previousSibling())
+    , m_parent(element.parent())
+{
+}
+
+void FbMoveLeftCmd::redo()
+{
+    m_parent.appendOutside(m_element.takeFromDocument());
+}
+
+void FbMoveLeftCmd::undo()
+{
+    if (m_subling.isNull()) {
+        m_parent.prependInside(m_element.takeFromDocument());
+    } else {
+        m_subling.appendOutside(m_element.takeFromDocument());
+    }
+}
+
+//---------------------------------------------------------------------------
+//  FbMoveRightCmd
+//---------------------------------------------------------------------------
+
+FbMoveRightCmd::FbMoveRightCmd(const FbTextElement &element)
+    : QUndoCommand()
+    , m_element(element)
+    , m_subling(element.previousSibling())
+{
+}
+
+void FbMoveRightCmd::redo()
+{
+    m_subling.appendInside(m_element.takeFromDocument());
+}
+
+void FbMoveRightCmd::undo()
+{
+    m_subling.appendOutside(m_element.takeFromDocument());
+}
+

+ 23 - 0
source/fb2html.h

@@ -70,4 +70,27 @@ private:
     FbTextElement m_element;
 };
 
+class FbMoveLeftCmd : public QUndoCommand
+{
+public:
+    explicit FbMoveLeftCmd(const FbTextElement &element);
+    virtual void undo();
+    virtual void redo();
+private:
+    FbTextElement m_element;
+    FbTextElement m_subling;
+    FbTextElement m_parent;
+};
+
+class FbMoveRightCmd : public QUndoCommand
+{
+public:
+    explicit FbMoveRightCmd(const FbTextElement &element);
+    virtual void undo();
+    virtual void redo();
+private:
+    FbTextElement m_element;
+    FbTextElement m_subling;
+};
+
 #endif // FB2HTML_H

+ 2 - 2
source/fb2read.cpp

@@ -14,7 +14,7 @@ FbReadThread::FbReadThread(QObject *parent, const QString &filename, const QStri
     , m_xml(xml)
     , m_abort(false)
 {
-    connect(this, SIGNAL(html(QString, QString)), parent, SLOT(html(QString, QString)));
+    connect(this, SIGNAL(html(QString)), parent, SLOT(html(QString)));
 }
 
 FbReadThread::~FbReadThread()
@@ -32,7 +32,7 @@ void FbReadThread::stop()
 
 void FbReadThread::run()
 {
-    if (parse()) emit html(m_filename, m_html);
+    if (parse()) emit html(m_html);
 }
 
 #ifdef FB2_USE_LIBXML2

+ 1 - 1
source/fb2read.hpp

@@ -17,7 +17,7 @@ public:
     QString * data() { return &m_html; }
 
 signals:
-    void html(QString name, QString html);
+    void html(QString name);
 
 public slots:
     void stop();

+ 1 - 1
source/fb2text.cpp

@@ -432,7 +432,7 @@ void FbTextEdit::data(QString name, QByteArray data)
     m_files.set(name, data);
 }
 
-void FbTextEdit::html(QString name, QString html)
+void FbTextEdit::html(QString html)
 {
     static int number = 0;
     setHtml(html, QUrl(QString("fb2:/%1/").arg(number++)));

+ 1 - 1
source/fb2text.hpp

@@ -116,7 +116,7 @@ protected:
 
 public slots:
     void data(QString name, QByteArray data);
-    void html(QString name, QString html);
+    void html(QString html);
     void linkHovered(const QString &link, const QString &title, const QString &textContent);
     void insertImage();
     void insertNote();

+ 30 - 15
source/fb2tree.cpp

@@ -3,7 +3,6 @@
 #include <QtDebug>
 #include <QAction>
 #include <QApplication>
-#include <QCursor>
 #include <QVBoxLayout>
 #include <QUndoStack>
 #include <QWebFrame>
@@ -105,7 +104,7 @@ QString FbTreeItem::selector() const
     return selector.prepend("$('html')");
 }
 
-FbTreeItem * FbTreeItem::content(const FbTreeModel &model, int number) const
+FbTreeItem * FbTreeItem::content(int number) const
 {
     FbTextElement element = m_element.firstChild();
     while (number-- > 0) element = element.nextSibling();
@@ -211,7 +210,7 @@ QModelIndex FbTreeModel::index(const QString &location) const
         QString str = iterator.next();
         if (str.left(5) == "HTML=") continue;
         int key = str.mid(str.indexOf("=")+1).toInt();
-        FbTreeItem * child = parent->content(*this, key);
+        FbTreeItem * child = parent->content(key);
         if (child) result = index(child);
         parent = child;
     }
@@ -233,33 +232,39 @@ QModelIndex FbTreeModel::move(const QModelIndex &index, int dx, int dy)
     switch (dx) {
         case -1: {
             if (!owner || owner == m_root) return QModelIndex();
-            if (child->name() != "section") return QModelIndex();
-            if (owner->name() != "section") return QModelIndex();
+            if (!child->element().isSection()) return QModelIndex();
+            if (!owner->element().isSection()) return QModelIndex();
+
             QModelIndex target = this->parent(parent);
             int to = parent.row() + 1;
             result = createIndex(to, 0, (void*)child);
+
             beginMoveRows(parent, from, from, target, to);
-            QWebElement element = child->element().takeFromDocument();
-            owner->element().appendOutside(element);
             owner->takeAt(from);
             owner->parent()->insert(child, to);
             endMoveRows();
+
+            QUndoCommand * command = new FbMoveLeftCmd(child->element());
+            m_view.page()->push(command, tr("Move section"));
         } break;
 
         case +1: {
             if (from == 0) return QModelIndex();
             FbTreeItem * brother = owner->item(from - 1);
-            if (child->name() != "section") return QModelIndex();
-            if (brother->name() != "section") return QModelIndex();
+            if (!child->element().isSection()) return QModelIndex();
+            if (!brother->element().isSection()) return QModelIndex();
+
             QModelIndex target = createIndex(from - 1, 0, (void*)brother);
             int to = rowCount(target);
             result = createIndex(to, 0, (void*)child);
+
             beginMoveRows(parent, from, from, target, to);
-            QWebElement element = child->element().takeFromDocument();
-            brother->element().appendInside(element);
             owner->takeAt(from);
             brother->insert(child, to);
             endMoveRows();
+
+            QUndoCommand * command = new FbMoveRightCmd(child->element());
+            m_view.page()->push(command, tr("Move section"));
         } break;
 
         default: {
@@ -477,18 +482,28 @@ void FbTreeView::initActions(QToolBar *toolbar)
 
 void FbTreeView::keyPressEvent(QKeyEvent *event)
 {
-    if (event->modifiers() == Qt::NoModifier) {
+    switch (event->modifiers()) {
+        case Qt::NoModifier:
+            switch (event->key()) {
+                case Qt::Key_Insert: insertNode(); return;
+                case Qt::Key_Delete: deleteNode(); return;
+            }
+            break;
+    case Qt::ControlModifier:
         switch (event->key()) {
-            case Qt::Key_Insert: insertNode(); return;
-            case Qt::Key_Delete: deleteNode(); return;
+            case Qt::Key_Left  : moveCurrent(-1, 0); return;
+            case Qt::Key_Right : moveCurrent(+1, 0); return;
+            case Qt::Key_Up    : moveCurrent(0, -1); return;
+            case Qt::Key_Down  : moveCurrent(0, +1); return;
         }
+        break;
     }
     QTreeView::keyPressEvent(event);
 }
 
 void FbTreeView::contextMenu(const QPoint &pos)
 {
-    m_menu.exec(QCursor::pos());
+    m_menu.exec(mapToGlobal(pos));
 }
 
 void FbTreeView::selectionChanged()

+ 1 - 1
source/fb2tree.hpp

@@ -72,7 +72,7 @@ public:
         return m_element.geometry().topLeft();
     }
 
-    FbTreeItem * content(const FbTreeModel &model, int number) const;
+    FbTreeItem * content(int number) const;
 
     QString selector() const;