Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/lintest/fb2edit.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKandrashin Denis <mail@lintest.ru>2012-11-10 23:48:28 +0400
committerKandrashin Denis <mail@lintest.ru>2012-11-10 23:48:28 +0400
commitc9f99c61471b0d0f6bbfac932ce206d1fcdb2d35 (patch)
tree3279945a67babffcab8ba926c0792a7306345f6d
parentb7cd1e55211a41929c19fb1aaf5d560a76ccfa43 (diff)
Save cursor position when document is switched to code
-rw-r--r--source/fb2dock.cpp19
-rw-r--r--source/fb2save.cpp36
-rw-r--r--source/fb2save.hpp11
-rw-r--r--source/fb2text.cpp11
-rw-r--r--source/fb2text.hpp2
-rw-r--r--source/js/export.js2
6 files changed, 60 insertions, 21 deletions
diff --git a/source/fb2dock.cpp b/source/fb2dock.cpp
index d11bc99..a28f2ea 100644
--- a/source/fb2dock.cpp
+++ b/source/fb2dock.cpp
@@ -74,15 +74,22 @@ void FbMainDock::switchMode(Fb::Mode mode)
default: ;
}
} else {
- QString xml;
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: ;
}
- if (!xml.isEmpty()) {
- m_code->setPlainText(xml);
- }
}
setMode(mode);
}
diff --git a/source/fb2save.cpp b/source/fb2save.cpp
index b0a86b1..609cfd6 100644
--- a/source/fb2save.cpp
+++ b/source/fb2save.cpp
@@ -139,6 +139,9 @@ void FbHtmlHandler::onEnd(const QString &name)
FbSaveWriter::FbSaveWriter(FbTextEdit &view, QByteArray *array)
: QXmlStreamWriter(array)
, m_view(view)
+ , m_string(0)
+ , m_anchor(0)
+ , m_focus(0)
{
if (QWebFrame * frame = m_view.page()->mainFrame()) {
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)
: QXmlStreamWriter(device)
, m_view(view)
+ , m_string(0)
+ , m_anchor(0)
+ , m_focus(0)
{
}
FbSaveWriter::FbSaveWriter(FbTextEdit &view, QString *string)
: QXmlStreamWriter(string)
, 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)
{
if (level) writeLineEnd();
@@ -288,6 +306,16 @@ void FbSaveWriter::writeContentType(const QString &name, QByteArray &data)
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
//---------------------------------------------------------------------------
@@ -495,8 +523,6 @@ void FbSaveHandler::ParagHandler::start()
FbSaveHandler::FbSaveHandler(FbSaveWriter &writer)
: FbHtmlHandler()
, m_writer(writer)
- , m_anchor(-1)
- , m_focus(-1)
{
}
@@ -508,12 +534,12 @@ bool FbSaveHandler::comment(const QString& ch)
void FbSaveHandler::onAnchor(int offset)
{
- m_anchor = offset;
+ m_writer.setAnchor(offset);
}
void FbSaveHandler::onFocus(int offset)
{
- m_focus = offset;
+ m_writer.setFocus(offset);
}
FbXmlHandler::NodeHandler * FbSaveHandler::CreateRoot(const QString &name, const QXmlAttributes &atts)
@@ -551,8 +577,8 @@ bool FbSaveHandler::save()
QWebFrame *frame = page->mainFrame();
if (!frame) return false;
- if (page->isModified()) setDocumentInfo(frame);
m_writer.writeStartDocument();
+ if (page->isModified()) setDocumentInfo(frame);
QString javascript = jScript("export.js");
frame->addToJavaScriptWindowObject("handler", this);
frame->evaluateJavaScript(javascript);
diff --git a/source/fb2save.hpp b/source/fb2save.hpp
index bde4eea..524f7ee 100644
--- a/source/fb2save.hpp
+++ b/source/fb2save.hpp
@@ -71,12 +71,18 @@ public:
explicit FbSaveWriter(FbTextEdit &view, QString *string);
FbTextEdit & view() { return m_view; }
QString filename(const QString &src);
+ void writeStartDocument();
void writeStartElement(const QString &name, int level);
void writeEndElement(int level);
void writeComment(const QString &ch);
void writeLineEnd();
void writeFiles();
void writeStyle();
+public:
+ int anchor() const { return m_anchor; }
+ int focus() const { return m_focus; }
+ void setAnchor(int offset);
+ void setFocus(int offset);
private:
QByteArray downloadFile(const QUrl &url);
void writeContentType(const QString &name, QByteArray &data);
@@ -84,7 +90,10 @@ private:
private:
FbTextEdit &m_view;
QStringList m_names;
+ QString *m_string;
QString m_style;
+ int m_anchor;
+ int m_focus;
};
class FbSaveHandler : public FbHtmlHandler
@@ -186,8 +195,6 @@ private:
private:
FbSaveWriter & m_writer;
- int m_anchor;
- int m_focus;
};
#endif // FB2SAVE_H
diff --git a/source/fb2text.cpp b/source/fb2text.cpp
index 62d5986..80cac74 100644
--- a/source/fb2text.cpp
+++ b/source/fb2text.cpp
@@ -529,13 +529,12 @@ bool FbTextEdit::save(QByteArray *array)
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;
}
diff --git a/source/fb2text.hpp b/source/fb2text.hpp
index a29b294..00310db 100644
--- a/source/fb2text.hpp
+++ b/source/fb2text.hpp
@@ -77,8 +77,8 @@ public:
FbTextPage *page();
FbStore *store();
bool save(QIODevice *device, const QString &codec = QString());
+ bool save(QString *string, int &anchor, int &focus);
bool save(QByteArray *array);
- bool save(QString *string);
QString toHtml();
QAction * act(Fb::Actions index) const;
diff --git a/source/js/export.js b/source/js/export.js
index a23b22d..0e9ae8d 100644
--- a/source/js/export.js
+++ b/source/js/export.js
@@ -1,6 +1,6 @@
var selection = document.getSelection();
var anchorNode = selection.anchorNode;
-var focusNode = selection.baseNode;
+var focusNode = selection.focusNode;
(f = function(node) {
if (node.nodeName === "#text") {
if (anchorNode === node) handler.onAnchor(selection.anchorOffset);