瀏覽代碼

Use QUndoStack: insert body element

Kandrashin Denis 13 年之前
父節點
當前提交
15729f5be8
共有 2 個文件被更改,包括 27 次插入11 次删除
  1. 25 7
      source/fb2view.cpp
  2. 2 4
      source/fb2view.hpp

+ 25 - 7
source/fb2view.cpp

@@ -99,10 +99,33 @@ QWebElement Fb2WebPage::doc()
     return mainFrame()->documentElement();
 }
 
+class Fb2InsertBodyCommand : public QUndoCommand
+{
+public:
+    explicit Fb2InsertBodyCommand(Fb2WebPage &page, QUndoCommand *parent = 0) : QUndoCommand(parent), m_page(page) {}
+    virtual void undo();
+    virtual void redo();
+private:
+    Fb2WebPage & m_page;
+};
+
+void Fb2InsertBodyCommand::undo()
+{
+    m_page.body().lastChild().removeFromDocument();
+    Fb2WebElement(m_page.body().lastChild()).select();
+}
+
+void Fb2InsertBodyCommand::redo()
+{
+    m_page.body().appendInside("<div class=body><div class=section><p>text</p></div></div>");
+    Fb2WebElement(m_page.body().lastChild()).select();
+}
+
 void Fb2WebPage::insertBody()
 {
-    body().appendInside("<div class=body><div class=section><p>text</p></div></div>");
-    QWebElement element = body().lastChild();
+    undoStack()->beginMacro("Insert title");
+    undoStack()->push(new Fb2InsertBodyCommand(*this));
+    undoStack()->endMacro();
     emit contentsChanged();
 }
 
@@ -368,11 +391,6 @@ void Fb2WebView::loadFinished()
     element.select();
 }
 
-class Fb2UndoCommand : public QUndoCommand
-{
-
-};
-
 void Fb2WebView::insertTitle()
 {
     page()->undoStack()->beginMacro("Insert title");

+ 2 - 4
source/fb2view.hpp

@@ -52,16 +52,14 @@ class Fb2WebPage : public QWebPage
 
 public:
     explicit Fb2WebPage(QObject *parent = 0);
+    QWebElement body();
+    QWebElement doc();
 
 public slots:
     void insertBody();
 
 protected:
     virtual bool acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, NavigationType type);
-
-private:
-    QWebElement body();
-    QWebElement doc();
 };
 
 class Fb2WebView : public Fb2BaseWebView