Prechádzať zdrojové kódy

Move tree nodes: only up

Kandrashin Denis 13 rokov pred
rodič
commit
847ebc7597
6 zmenil súbory, kde vykonal 66 pridanie a 17 odobranie
  1. 3 2
      fb2edit.pro
  2. 13 0
      source/fb2html.cpp
  3. 3 3
      source/fb2html.h
  4. 42 10
      source/fb2tree.cpp
  5. 4 1
      source/fb2tree.hpp
  6. 1 1
      source/fb2view.cpp

+ 3 - 2
fb2edit.pro

@@ -12,7 +12,7 @@ HEADERS = \
     source/fb2utils.h \
     source/fb2save.hpp \
     source/fb2dlgs.hpp \
-    source/fb2tree.h
+    source/fb2html.h
 
 SOURCES = \
     source/fb2app.cpp \
@@ -27,7 +27,8 @@ SOURCES = \
     source/fb2xml.cpp \
     source/fb2xml2.cpp \
     source/fb2utils.cpp \
-    source/fb2dlgs.cpp
+    source/fb2dlgs.cpp \
+    source/fb2html.cpp
 
 RESOURCES = \
     3rdparty/gnome/gnome.qrc \

+ 13 - 0
source/fb2html.cpp

@@ -0,0 +1,13 @@
+#include "fb2html.h"
+#include "fb2utils.h"
+
+//---------------------------------------------------------------------------
+//  Fb2WebElement
+//---------------------------------------------------------------------------
+
+void Fb2WebElement::select()
+{
+    static const QString javascript = FB2::read(":/js/set_cursor.js");
+    evaluateJavaScript(javascript);
+}
+

+ 3 - 3
source/fb2tree.h → source/fb2html.h

@@ -1,5 +1,5 @@
-#ifndef FB2TREE_H
-#define FB2TREE_H
+#ifndef FB2HTML_H
+#define FB2HTML_H
 
 #include <QWebElement>
 
@@ -14,4 +14,4 @@ public:
     void select();
 };
 
-#endif // FB2TREE_H
+#endif // FB2HTML_H

+ 42 - 10
source/fb2tree.cpp

@@ -12,16 +12,6 @@
 #include "fb2utils.h"
 #include "fb2view.hpp"
 
-//---------------------------------------------------------------------------
-//  Fb2WebElement
-//---------------------------------------------------------------------------
-
-void Fb2WebElement::select()
-{
-    static const QString javascript = FB2::read(":/js/set_cursor.js");
-    evaluateJavaScript(javascript);
-}
-
 //---------------------------------------------------------------------------
 //  Fb2TreeItem
 //---------------------------------------------------------------------------
@@ -138,6 +128,14 @@ Fb2TreeItem * Fb2TreeItem::content(const Fb2TreeModel &model, int number, QModel
     return 0;
 }
 
+bool Fb2TreeItem::moveUp(Fb2TreeItem * child)
+{
+    int index = this->index(child);
+    if (index == 0) return false;
+    m_list.move(index, index-1);
+    return true;
+}
+
 //---------------------------------------------------------------------------
 //  Fb2TreeModel
 //---------------------------------------------------------------------------
@@ -346,6 +344,40 @@ void Fb2TreeView::deleteNode()
 
 void Fb2TreeView::moveUp()
 {
+    QModelIndex index = currentIndex();
+    if (!index.isValid()) return;
+
+    Fb2TreeModel *model = qobject_cast<Fb2TreeModel*>(this->model());
+    if (!model) return;
+
+    QModelIndex result = model->moveUp(index);
+    if (!result.isValid()) return;
+
+    setCurrentIndex(result);
+    emit currentChanged(result, index);
+    emit QTreeView::activated(result);
+    scrollTo(result);
+}
+
+QModelIndex Fb2TreeModel::moveUp(const QModelIndex &index)
+{
+    int row = index.row();
+    if (!row) return QModelIndex();
+
+    Fb2TreeItem *child = item(index);
+    if (!child) return QModelIndex();
+
+    Fb2TreeItem *owner = child->parent();
+    if (!owner) return QModelIndex();
+
+    QModelIndex parent = this->parent(index);
+
+    QModelIndex result = createIndex(row - 1, 0, (void*)child);
+    beginMoveRows(parent, row, row, parent, row - 1);
+    if (!owner->moveUp(child)) result = QModelIndex();
+    endMoveRows();
+
+    return result;
 }
 
 void Fb2TreeView::moveDown()

+ 4 - 1
source/fb2tree.hpp

@@ -6,7 +6,7 @@
 #include <QTimer>
 #include <QToolBar>
 
-#include "fb2tree.h"
+#include "fb2html.h"
 
 class Fb2WebView;
 
@@ -55,6 +55,8 @@ public:
 
     Fb2TreeItem * content(const Fb2TreeModel &model, int number, QModelIndex &index) const;
 
+    bool moveUp(Fb2TreeItem * child);
+
 private:
     QString static title(const QWebElement &element);
     void addChildren(QWebElement &parent, bool direct = true);
@@ -78,6 +80,7 @@ public:
     QModelIndex index(const QString &location) const;
     Fb2WebView & view() { return m_view; }
     void selectText(const QModelIndex &index);
+    QModelIndex moveUp(const QModelIndex &index);
 
 public:
     virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;

+ 1 - 1
source/fb2view.cpp

@@ -3,7 +3,7 @@
 #include "fb2read.hpp"
 #include "fb2save.hpp"
 #include "fb2utils.h"
-#include "fb2tree.h"
+#include "fb2html.h"
 #include "fb2xml2.h"
 
 #include <QAction>