Browse Source

Move web elements up end down

Kandrashin Denis 13 years ago
parent
commit
4d1c1b60b1
2 changed files with 67 additions and 27 deletions
  1. 55 24
      source/fb2tree.cpp
  2. 12 3
      source/fb2tree.hpp

+ 55 - 24
source/fb2tree.cpp

@@ -128,16 +128,15 @@ Fb2TreeItem * Fb2TreeItem::content(const Fb2TreeModel &model, int number, QModel
     return 0;
 }
 
-bool Fb2TreeItem::move(Fb2TreeItem * child, int delta)
+void Fb2TreeItem::move(Fb2TreeItem * child, int delta)
 {
     int x = index(child);
     int i = x; if (delta < 0) i += delta;
     int j = x; if (delta > 0) j += delta;
-    if (i < 0 || j >= count()) return false;
-    m_list[i]->m_element = m_list[i]->element().takeFromDocument();
-    m_list[j]->element().appendOutside(m_list[i]->m_element);
+    if (i < 0 || j >= count()) return;
+    QWebElement element = m_list[i]->element().takeFromDocument();
+    m_list[j]->element().appendOutside(element);
     m_list.move(i, j);
-    return true;
 }
 
 //---------------------------------------------------------------------------
@@ -351,7 +350,7 @@ Fb2TreeModel * Fb2TreeView::model()
     return qobject_cast<Fb2TreeModel*>(QTreeView::model());
 }
 
-QModelIndex Fb2TreeModel::move(const QModelIndex &index, int delta)
+QModelIndex Fb2TreeModel::move(const QModelIndex &index, int dx, int dy)
 {
     Fb2TreeItem *child = item(index);
     if (!child) return QModelIndex();
@@ -359,31 +358,61 @@ QModelIndex Fb2TreeModel::move(const QModelIndex &index, int delta)
     Fb2TreeItem *owner = child->parent();
     if (!owner) return QModelIndex();
 
-    QModelIndex parent = this->parent(index);
-
     int from = index.row();
-    int to = from + delta;
-    QModelIndex result = createIndex(to, 0, (void*)child);
+    QModelIndex parent = this->parent(index);
+    QModelIndex result;
+
+    switch (dx) {
+        case -1: {
+            if (owner == m_root) 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();
+        } break;
+
+        case +1: {
+            if (from == 0) return QModelIndex();
+            Fb2TreeItem * brother = owner->item(from - 1);
+            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();
+        } break;
+
+        default: {
+            int to = from + dy;
+            if (to < 0 || rowCount(parent) <= to) return QModelIndex();
+            result = createIndex(to, 0, (void*)child);
+
+            if (dy > 0) {
+                from = to;
+                to = index.row();
+            }
 
-    if (delta > 0) {
-        from = to;
-        to = index.row();
+            beginMoveRows(parent, from, from, parent, to);
+            owner->move(child, dy);
+            endMoveRows();
+        } break;
     }
-
-    if (to < 0 || from >= rowCount(parent)) return QModelIndex();
-
-    beginMoveRows(parent, from, from, parent, to);
-    if (!owner->move(child, delta)) result = QModelIndex();
-    endMoveRows();
-
     return result;
 }
 
-void Fb2TreeView::moveCurrent(int delta)
+void Fb2TreeView::moveCurrent(int dx, int dy)
 {
     if (Fb2TreeModel * m = model()) {
         QModelIndex index = currentIndex();
-        QModelIndex result = m->move(index, delta);
+        QModelIndex result = m->move(index, dx, dy);
         if (result.isValid()) {
             setCurrentIndex(result);
             emit currentChanged(result, index);
@@ -395,20 +424,22 @@ void Fb2TreeView::moveCurrent(int delta)
 
 void Fb2TreeView::moveUp()
 {
-    moveCurrent(-1);
+    moveCurrent(0, -1);
 }
 
 void Fb2TreeView::moveDown()
 {
-    moveCurrent(+1);
+    moveCurrent(0, +1);
 }
 
 void Fb2TreeView::moveLeft()
 {
+    moveCurrent(-1, 0);
 }
 
 void Fb2TreeView::moveRight()
 {
+    moveCurrent(+1, 0);
 }
 
 //---------------------------------------------------------------------------

+ 12 - 3
source/fb2tree.hpp

@@ -29,6 +29,15 @@ public:
         return m_list.indexOf(child);
     }
 
+    void insert(Fb2TreeItem * child, int row) {
+        m_list.insert(row, child);
+        child->m_parent = this;
+    }
+
+    Fb2TreeItem * takeAt(int row) {
+        return m_list.takeAt(row);
+    }
+
     int count() const {
         return m_list.size();
     }
@@ -55,7 +64,7 @@ public:
 
     Fb2TreeItem * content(const Fb2TreeModel &model, int number, QModelIndex &index) const;
 
-    bool move(Fb2TreeItem * child, int delta);
+    void move(Fb2TreeItem * child, int delta);
 
 private:
     QString static title(const QWebElement &element);
@@ -80,7 +89,7 @@ public:
     QModelIndex index(const QString &location) const;
     Fb2WebView & view() { return m_view; }
     void selectText(const QModelIndex &index);
-    QModelIndex move(const QModelIndex &index, int delta);
+    QModelIndex move(const QModelIndex &index, int dx, int dy);
 
 public:
     virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
@@ -123,7 +132,7 @@ private slots:
     void moveRight();
 
 private:
-    void moveCurrent(int delta);
+    void moveCurrent(int dx, int dy);
     Fb2TreeModel * model();
 
 private: