Parcourir la source

Append, remove header nodes

Kandrashin Denis il y a 13 ans
Parent
commit
6e12244173
3 fichiers modifiés avec 52 ajouts et 19 suppressions
  1. 44 15
      source/fb2head.cpp
  2. 6 2
      source/fb2head.hpp
  3. 2 2
      source/fb2tree.hpp

+ 44 - 15
source/fb2head.cpp

@@ -202,10 +202,9 @@ Fb2HeadItem::~Fb2HeadItem()
 
 Fb2HeadItem * Fb2HeadItem::append(const QString name)
 {
-    QWebElement element;
-    element.setOuterXml("<div>text</div>");
+    m_element.appendInside("<div></div>");
+    QWebElement element = m_element.lastChild();
     element.addClass(name);
-    m_element.appendInside(element);
     Fb2HeadItem * child = new Fb2HeadItem(element, this);
     m_list << child;
     return child;
@@ -309,6 +308,13 @@ Fb2Scheme Fb2HeadItem::scheme() const
     return parent.element(m_name);
 }
 
+bool Fb2HeadItem::remove(int row)
+{
+    if (row < 0 || row >= count()) return false;
+    m_list[row]->m_element.removeFromDocument();
+    m_list.removeAt(row);
+}
+
 //---------------------------------------------------------------------------
 //  Fb2HeadModel
 //---------------------------------------------------------------------------
@@ -465,7 +471,7 @@ Fb2HeadView::Fb2HeadView(Fb2WebView &view, QWidget *parent)
     act->setShortcutContext(Qt::WidgetShortcut);
     act->setShortcut(QKeySequence("Insert"));
     act->setPriority(QAction::LowPriority);
-    connect(act, SIGNAL(triggered()), SLOT(insertNode()));
+    connect(act, SIGNAL(triggered()), SLOT(appendNode()));
     addAction(act);
 
     actionModify = act = new QAction(FB2::icon("list-add"), tr("&Modify"), this);
@@ -475,7 +481,7 @@ Fb2HeadView::Fb2HeadView(Fb2WebView &view, QWidget *parent)
     act->setShortcutContext(Qt::WidgetShortcut);
     act->setShortcut(QKeySequence("Delete"));
     act->setPriority(QAction::LowPriority);
-    connect(act, SIGNAL(triggered()), SLOT(deleteNode()));
+    connect(act, SIGNAL(triggered()), SLOT(removeNode()));
     addAction(act);
 
     //setItemDelegate(new QItemDelegate(this));
@@ -543,7 +549,7 @@ void Fb2HeadView::collapsed(const QModelIndex &index)
     }
 }
 
-void Fb2HeadView::insertNode()
+void Fb2HeadView::appendNode()
 {
     Fb2HeadModel * m = qobject_cast<Fb2HeadModel*>(model());
     if (!m) return;
@@ -553,29 +559,52 @@ void Fb2HeadView::insertNode()
     if (!item) return;
 
     QString name = item->name().toLower();
-    if (name == "annotation") {
-        item = item->parent();
-    } else if (name == "history") {
-        item = item->parent();
+    if (name == "annotation" || name == "history") {
+        current = m->parent(current);
+        item = m->item(current);
     }
 
     QStringList list;
     item->scheme().items(list);
     if (list.count() == 0) {
-        item = item->parent();
+        current = m->parent(current);
+        item = m->item(current);
         if (!item) return;
         item->scheme().items(list);
     }
 
     Fb2NodeDlg dlg(this, item->scheme(), list);
-    int res = dlg.exec();
-    if (res) {
-        item->append(dlg.value());
+    if (dlg.exec()) {
+        current = m->append(current, dlg.value());
+        if (current.isValid()) setCurrentIndex(current);
     }
 }
 
-void Fb2HeadView::deleteNode()
+void Fb2HeadView::removeNode()
+{
+    Fb2HeadModel * m = qobject_cast<Fb2HeadModel*>(model());
+    if (m) m->remove(currentIndex());
+}
+
+QModelIndex Fb2HeadModel::append(const QModelIndex &parent, const QString &name)
+{
+    Fb2HeadItem * owner = item(parent);
+    if (!owner) return QModelIndex();
+    int row = owner->count();
+    beginInsertRows(parent, row, row);
+    Fb2HeadItem * item = owner->append(name);
+    endInsertRows();
+    return createIndex(row, 0, (void*)item);
+}
+
+void Fb2HeadModel::remove(const QModelIndex &index)
 {
+    int r = index.row();
+    QModelIndex p = parent(index);
+    beginRemoveRows(p, r, r + 1);
+    Fb2HeadItem * i = item(p);
+    if (i) i->remove(r);
+    endRemoveRows();
 }
 
 //---------------------------------------------------------------------------

+ 6 - 2
source/fb2head.hpp

@@ -67,6 +67,8 @@ public:
 
     Fb2HeadItem * append(const QString name);
 
+    bool remove(int row);
+
     Fb2HeadItem * item(const QModelIndex &index) const;
 
     Fb2HeadItem * item(int row) const;
@@ -130,6 +132,8 @@ public:
     void select(const QModelIndex &index);
     void expand(QTreeView *view);
     Fb2HeadItem * item(const QModelIndex &index) const;
+    QModelIndex append(const QModelIndex &parent, const QString &name);
+    void remove(const QModelIndex &index);
 
 public:
     virtual Qt::ItemFlags flags(const QModelIndex &index) const;
@@ -164,8 +168,8 @@ public slots:
 private slots:
     void activated(const QModelIndex &index);
     void collapsed(const QModelIndex &index);
-    void insertNode();
-    void deleteNode();
+    void appendNode();
+    void removeNode();
 
 protected:
     void currentChanged(const QModelIndex &current, const QModelIndex &previous);

+ 2 - 2
source/fb2tree.hpp

@@ -32,8 +32,8 @@ public:
         return m_list.size();
     }
 
-    const QWebElement element() const {
-        return element();
+    QWebElement element() const {
+        return m_element;
     }
 
     Fb2TreeItem * parent() const {