Browse Source

Save cursor position when document is switched to code

Kandrashin Denis 12 years ago
parent
commit
c9f99c6147
6 changed files with 60 additions and 21 deletions
  1. 13 6
      source/fb2dock.cpp
  2. 31 5
      source/fb2save.cpp
  3. 9 2
      source/fb2save.hpp
  4. 5 6
      source/fb2text.cpp
  5. 1 1
      source/fb2text.hpp
  6. 1 1
      source/js/export.js

+ 13 - 6
source/fb2dock.cpp

@@ -74,15 +74,22 @@ void FbMainDock::switchMode(Fb::Mode mode)
             default: ;
             default: ;
         }
         }
     } else {
     } else {
-        QString xml;
         switch (mode) {
         switch (mode) {
-            case Fb::Code: m_text->save(&xml); break;
-            case Fb::Html: xml = m_text->toHtml(); break;
+            case Fb::Code: {
+                QString xml; int anchor, focus;
+                m_text->save(&xml, anchor, focus);
+                m_code->setPlainText(xml);
+                QTextCursor cursor = m_code->textCursor();
+                if (anchor > 0) cursor.setPosition(anchor, QTextCursor::MoveAnchor);
+                if (focus > 0) cursor.setPosition(focus, QTextCursor::KeepAnchor);
+                m_code->setTextCursor(cursor);
+            } break;
+            case Fb::Html: {
+                QString html = m_text->toHtml();
+                m_code->setPlainText(html);
+            } break;
             default: ;
             default: ;
         }
         }
-        if (!xml.isEmpty()) {
-            m_code->setPlainText(xml);
-        }
     }
     }
     setMode(mode);
     setMode(mode);
 }
 }

+ 31 - 5
source/fb2save.cpp

@@ -139,6 +139,9 @@ void FbHtmlHandler::onEnd(const QString &name)
 FbSaveWriter::FbSaveWriter(FbTextEdit &view, QByteArray *array)
 FbSaveWriter::FbSaveWriter(FbTextEdit &view, QByteArray *array)
     : QXmlStreamWriter(array)
     : QXmlStreamWriter(array)
     , m_view(view)
     , m_view(view)
+    , m_string(0)
+    , m_anchor(0)
+    , m_focus(0)
 {
 {
     if (QWebFrame * frame = m_view.page()->mainFrame()) {
     if (QWebFrame * frame = m_view.page()->mainFrame()) {
         m_style = frame->findFirstElement("html>head>style#origin").toPlainText();
         m_style = frame->findFirstElement("html>head>style#origin").toPlainText();
@@ -148,12 +151,18 @@ FbSaveWriter::FbSaveWriter(FbTextEdit &view, QByteArray *array)
 FbSaveWriter::FbSaveWriter(FbTextEdit &view, QIODevice *device)
 FbSaveWriter::FbSaveWriter(FbTextEdit &view, QIODevice *device)
     : QXmlStreamWriter(device)
     : QXmlStreamWriter(device)
     , m_view(view)
     , m_view(view)
+    , m_string(0)
+    , m_anchor(0)
+    , m_focus(0)
 {
 {
 }
 }
 
 
 FbSaveWriter::FbSaveWriter(FbTextEdit &view, QString *string)
 FbSaveWriter::FbSaveWriter(FbTextEdit &view, QString *string)
     : QXmlStreamWriter(string)
     : QXmlStreamWriter(string)
     , m_view(view)
     , m_view(view)
+    , m_string(string)
+    , m_anchor(0)
+    , m_focus(0)
 {
 {
 }
 }
 
 
@@ -164,6 +173,15 @@ void FbSaveWriter::writeComment(const QString &ch)
 
 
 }
 }
 
 
+void FbSaveWriter::writeStartDocument()
+{
+    if (device()) {
+        QXmlStreamWriter::writeStartDocument();
+    } else if (m_string) {
+        m_string->append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
+    }
+}
+
 void FbSaveWriter::writeStartElement(const QString &name, int level)
 void FbSaveWriter::writeStartElement(const QString &name, int level)
 {
 {
     if (level) writeLineEnd();
     if (level) writeLineEnd();
@@ -288,6 +306,16 @@ void FbSaveWriter::writeContentType(const QString &name, QByteArray &data)
     writeAttribute("content-type", type);
     writeAttribute("content-type", type);
 }
 }
 
 
+void FbSaveWriter::setAnchor(int offset)
+{
+    if (m_string) m_anchor = m_string->length() + offset;
+}
+
+void FbSaveWriter::setFocus(int offset)
+{
+    if (m_string) m_focus = m_string->length() + offset;
+}
+
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
 //  FbSaveHandler::TextHandler
 //  FbSaveHandler::TextHandler
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
@@ -495,8 +523,6 @@ void FbSaveHandler::ParagHandler::start()
 FbSaveHandler::FbSaveHandler(FbSaveWriter &writer)
 FbSaveHandler::FbSaveHandler(FbSaveWriter &writer)
     : FbHtmlHandler()
     : FbHtmlHandler()
     , m_writer(writer)
     , m_writer(writer)
-    , m_anchor(-1)
-    , m_focus(-1)
 {
 {
 }
 }
 
 
@@ -508,12 +534,12 @@ bool FbSaveHandler::comment(const QString& ch)
 
 
 void FbSaveHandler::onAnchor(int offset)
 void FbSaveHandler::onAnchor(int offset)
 {
 {
-    m_anchor = offset;
+    m_writer.setAnchor(offset);
 }
 }
 
 
 void FbSaveHandler::onFocus(int offset)
 void FbSaveHandler::onFocus(int offset)
 {
 {
-    m_focus = offset;
+    m_writer.setFocus(offset);
 }
 }
 
 
 FbXmlHandler::NodeHandler * FbSaveHandler::CreateRoot(const QString &name, const QXmlAttributes &atts)
 FbXmlHandler::NodeHandler * FbSaveHandler::CreateRoot(const QString &name, const QXmlAttributes &atts)
@@ -551,8 +577,8 @@ bool FbSaveHandler::save()
     QWebFrame *frame = page->mainFrame();
     QWebFrame *frame = page->mainFrame();
     if (!frame) return false;
     if (!frame) return false;
 
 
-    if (page->isModified()) setDocumentInfo(frame);
     m_writer.writeStartDocument();
     m_writer.writeStartDocument();
+    if (page->isModified()) setDocumentInfo(frame);
     QString javascript = jScript("export.js");
     QString javascript = jScript("export.js");
     frame->addToJavaScriptWindowObject("handler", this);
     frame->addToJavaScriptWindowObject("handler", this);
     frame->evaluateJavaScript(javascript);
     frame->evaluateJavaScript(javascript);

+ 9 - 2
source/fb2save.hpp

@@ -71,12 +71,18 @@ public:
     explicit FbSaveWriter(FbTextEdit &view, QString *string);
     explicit FbSaveWriter(FbTextEdit &view, QString *string);
     FbTextEdit & view() { return m_view; }
     FbTextEdit & view() { return m_view; }
     QString filename(const QString &src);
     QString filename(const QString &src);
+    void writeStartDocument();
     void writeStartElement(const QString &name, int level);
     void writeStartElement(const QString &name, int level);
     void writeEndElement(int level);
     void writeEndElement(int level);
     void writeComment(const QString &ch);
     void writeComment(const QString &ch);
     void writeLineEnd();
     void writeLineEnd();
     void writeFiles();
     void writeFiles();
     void writeStyle();
     void writeStyle();
+public:
+    int anchor() const { return m_anchor; }
+    int focus() const { return m_focus; }
+    void setAnchor(int offset);
+    void setFocus(int offset);
 private:
 private:
     QByteArray downloadFile(const QUrl &url);
     QByteArray downloadFile(const QUrl &url);
     void writeContentType(const QString &name, QByteArray &data);
     void writeContentType(const QString &name, QByteArray &data);
@@ -84,7 +90,10 @@ private:
 private:
 private:
     FbTextEdit &m_view;
     FbTextEdit &m_view;
     QStringList m_names;
     QStringList m_names;
+    QString *m_string;
     QString m_style;
     QString m_style;
+    int m_anchor;
+    int m_focus;
 };
 };
 
 
 class FbSaveHandler : public FbHtmlHandler
 class FbSaveHandler : public FbHtmlHandler
@@ -186,8 +195,6 @@ private:
 
 
 private:
 private:
     FbSaveWriter & m_writer;
     FbSaveWriter & m_writer;
-    int m_anchor;
-    int m_focus;
 };
 };
 
 
 #endif // FB2SAVE_H
 #endif // FB2SAVE_H

+ 5 - 6
source/fb2text.cpp

@@ -529,13 +529,12 @@ bool FbTextEdit::save(QByteArray *array)
     return FbSaveHandler(writer).save();
     return FbSaveHandler(writer).save();
 }
 }
 
 
-bool FbTextEdit::save(QString *string)
+bool FbTextEdit::save(QString *string, int &anchor, int &focus)
 {
 {
-    // Use class QByteArray instead QString
-    // to store information about encoding.
-    QByteArray data;
-    bool ok = save(&data);
-    if (ok) *string = QString::fromUtf8(data.data());
+    FbSaveWriter writer(*this, string);
+    bool ok = FbSaveHandler(writer).save();
+    anchor = writer.anchor();
+    focus = writer.focus();
     return ok;
     return ok;
 }
 }
 
 

+ 1 - 1
source/fb2text.hpp

@@ -77,8 +77,8 @@ public:
     FbTextPage *page();
     FbTextPage *page();
     FbStore *store();
     FbStore *store();
     bool save(QIODevice *device, const QString &codec = QString());
     bool save(QIODevice *device, const QString &codec = QString());
+    bool save(QString *string, int &anchor, int &focus);
     bool save(QByteArray *array);
     bool save(QByteArray *array);
-    bool save(QString *string);
     QString toHtml();
     QString toHtml();
 
 
     QAction * act(Fb::Actions index) const;
     QAction * act(Fb::Actions index) const;

+ 1 - 1
source/js/export.js

@@ -1,6 +1,6 @@
 var selection = document.getSelection();
 var selection = document.getSelection();
 var anchorNode = selection.anchorNode;
 var anchorNode = selection.anchorNode;
-var focusNode = selection.baseNode;
+var focusNode = selection.focusNode;
 (f = function(node) {
 (f = function(node) {
     if (node.nodeName === "#text") {
     if (node.nodeName === "#text") {
         if (anchorNode === node) handler.onAnchor(selection.anchorOffset);
         if (anchorNode === node) handler.onAnchor(selection.anchorOffset);