Browse Source

Merge branch 'master' of http://git.gitorious.org/fb2edit/lintest

Kandrashin Denis 13 năm trước cách đây
mục cha
commit
56e735d330
4 tập tin đã thay đổi với 44 bổ sung6 xóa
  1. 12 0
      source/fb2main.cpp
  2. 2 0
      source/fb2main.hpp
  3. 28 2
      source/fb2view.cpp
  4. 2 4
      source/fb2view.hpp

+ 12 - 0
source/fb2main.cpp

@@ -464,6 +464,16 @@ void Fb2MainWindow::selectionChanged()
     statusBar()->showMessage(textEdit->status());
 }
 
+void Fb2MainWindow::canUndoChanged(bool canUndo)
+{
+    actionUndo->setEnabled(canUndo);
+}
+
+void Fb2MainWindow::canRedoChanged(bool canRedo)
+{
+    actionRedo->setEnabled(canRedo);
+}
+
 void Fb2MainWindow::undoChanged()
 {
     actionUndo->setEnabled(textEdit->UndoEnabled());
@@ -654,6 +664,8 @@ void Fb2MainWindow::viewText()
     viewTree();
 
     connect(textEdit->page()->undoStack(), SIGNAL(cleanChanged(bool)), SLOT(cleanChanged(bool)));
+    connect(textEdit->page()->undoStack(), SIGNAL(canUndoChanged(bool)), SLOT(canUndoChanged(bool)));
+    connect(textEdit->page()->undoStack(), SIGNAL(canRedoChanged(bool)), SLOT(canRedoChanged(bool)));
     connect(textEdit->page(), SIGNAL(selectionChanged()), SLOT(selectionChanged()));
 
     connect(textEdit->pageAction(QWebPage::Undo), SIGNAL(changed()), SLOT(undoChanged()));

+ 2 - 0
source/fb2main.hpp

@@ -52,6 +52,8 @@ private slots:
     void viewTree();
 
     void cleanChanged(bool clean);
+    void canUndoChanged(bool canUndo);
+    void canRedoChanged(bool canRedo);
     void status(const QString &text);
     void clipboardDataChanged();
     void selectionChanged();

+ 28 - 2
source/fb2view.cpp

@@ -11,6 +11,7 @@
 #include <QFileDialog>
 #include <QNetworkRequest>
 #include <QToolTip>
+#include <QUndoCommand>
 #include <QUndoStack>
 #include <QWebElement>
 #include <QWebInspector>
@@ -98,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();
 }
 
@@ -369,7 +393,9 @@ void Fb2WebView::loadFinished()
 
 void Fb2WebView::insertTitle()
 {
+    page()->undoStack()->beginMacro("Insert title");
     static const QString javascript = FB2::read(":/js/insert_title.js");
     page()->mainFrame()->evaluateJavaScript(javascript);
+    page()->undoStack()->endMacro();
 }
 

+ 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