فهرست منبع

Move the text cursor when the user select the document tree item

Kandrashin Denis 13 سال پیش
والد
کامیت
83178ee3b1

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 1 - 0
3rdparty/jQuery/jquery.js


+ 6 - 3
fb2edit.pro

@@ -9,7 +9,8 @@ HEADERS = \
     source/fb2read.hpp \
     source/fb2read.hpp \
     source/fb2temp.hpp \
     source/fb2temp.hpp \
     source/fb2tree.hpp \
     source/fb2tree.hpp \
-    source/fb2view.hpp
+    source/fb2view.hpp \
+    source/fb2tool.h
 
 
 SOURCES = \
 SOURCES = \
     source/fb2app.cpp \
     source/fb2app.cpp \
@@ -22,11 +23,13 @@ SOURCES = \
     source/fb2tree.cpp \
     source/fb2tree.cpp \
     source/fb2view.cpp \
     source/fb2view.cpp \
     source/fb2xml.cpp \
     source/fb2xml.cpp \
-    source/fb2xml2.cpp
+    source/fb2xml2.cpp \
+    source/fb2tool.cpp
 
 
 RESOURCES = \
 RESOURCES = \
     3rdparty/gnome/gnome.qrc \
     3rdparty/gnome/gnome.qrc \
-    source/res/fb2edit.qrc
+    source/res/fb2edit.qrc \
+    source/js/javascript.qrc
 
 
 TARGET = fb2edit
 TARGET = fb2edit
 
 

+ 4 - 11
source/fb2main.cpp

@@ -88,7 +88,9 @@ void Fb2MainWindow::treeActivated(const QModelIndex &index)
 {
 {
     if (!treeView) return;
     if (!treeView) return;
     Fb2TreeModel *model = dynamic_cast<Fb2TreeModel*>(treeView->model());
     Fb2TreeModel *model = dynamic_cast<Fb2TreeModel*>(treeView->model());
-    if (model) model->select(index);
+    if (!model) return;
+    model->select(index);
+    selectionChanged();
 }
 }
 
 
 void Fb2MainWindow::treeDestroyed()
 void Fb2MainWindow::treeDestroyed()
@@ -473,16 +475,7 @@ void Fb2MainWindow::selectionChanged()
     actionTextSub->setChecked(textEdit->SubChecked());
     actionTextSub->setChecked(textEdit->SubChecked());
     actionTextSup->setChecked(textEdit->SupChecked());
     actionTextSup->setChecked(textEdit->SupChecked());
 
 
-    QString script = "\
-    (f = function(node){\
-        var tag = node.tagName;\
-        if (tag == 'BODY') return '';\
-        if (tag == 'DIV') tag = node.getAttribute('CLASS');\
-        return f(node.parentNode) + '/' + tag;\
-    })(document.getSelection().baseNode.parentNode);";
-
-    QString message = textEdit->page()->mainFrame()->evaluateJavaScript(script).toString();
-    statusBar()->showMessage(message);
+    statusBar()->showMessage(textEdit->status());
 }
 }
 
 
 void Fb2MainWindow::undoChanged()
 void Fb2MainWindow::undoChanged()

+ 7 - 0
source/fb2read.cpp

@@ -99,6 +99,13 @@ Fb2ReadHandler::RootHandler::RootHandler(Fb2ReadHandler &owner, const QString &n
 {
 {
     writer().writeStartDocument();
     writer().writeStartDocument();
     writer().writeStartElement("html");
     writer().writeStartElement("html");
+    writer().writeStartElement("head");
+    writer().writeStartElement("script");
+    writer().writeAttribute("type", "text/javascript");
+    writer().writeAttribute("src", "qrc:/js/jquery.js");
+    writer().writeCharacters(" ");
+    writer().writeEndElement();
+    writer().writeEndElement();
     writer().writeStartElement("body");
     writer().writeStartElement("body");
 }
 }
 
 

+ 27 - 0
source/fb2tool.cpp

@@ -0,0 +1,27 @@
+#include "fb2tool.h"
+
+#include <QFile>
+#include <QTextStream>
+
+namespace FB2 {
+
+QString read(const QString &filename)
+{
+    // TODO: throw an exception instead of
+    // returning an empty string
+    QFile file( filename );
+    if (!file.open( QFile::ReadOnly)) return QString();
+
+    QTextStream in( &file );
+
+    // Input should be UTF-8
+    in.setCodec( "UTF-8" );
+
+    // This will automatically switch reading from
+    // UTF-8 to UTF-16 if a BOM is detected
+    in.setAutoDetectUnicode( true );
+
+    return in.readAll();
+}
+
+}

+ 12 - 0
source/fb2tool.h

@@ -0,0 +1,12 @@
+#ifndef FB2TOOL_H
+#define FB2TOOL_H
+
+#include <QString>
+
+namespace FB2 {
+
+QString read(const QString &filename);
+
+}
+
+#endif // FB2TOOL_H

+ 31 - 0
source/fb2tree.cpp

@@ -7,6 +7,8 @@
 #include <QTreeView>
 #include <QTreeView>
 #include <QUrl>
 #include <QUrl>
 
 
+#include "fb2tool.h"
+
 Fb2TreeItem::Fb2TreeItem(QWebElement &element, Fb2TreeItem *parent)
 Fb2TreeItem::Fb2TreeItem(QWebElement &element, Fb2TreeItem *parent)
     : QObject(parent)
     : QObject(parent)
     , m_element(element)
     , m_element(element)
@@ -78,6 +80,29 @@ QString Fb2TreeItem::text() const
     return QString("<%1> %2").arg(m_name).arg(m_text);
     return QString("<%1> %2").arg(m_name).arg(m_text);
 }
 }
 
 
+QString Fb2TreeItem::selector() const
+{
+    QString text = "";
+    QString selector = ".get(0)";
+    QWebElement element = m_element;
+    QWebElement parent = element.parent();
+    while (!parent.isNull()) {
+        text.prepend(element.tagName()).prepend("/");
+        QWebElement child = parent.firstChild();
+        int index = -1;
+        while (!child.isNull()) {
+            index++;
+            if (child == element) break;
+            child = child.nextSibling();
+        }
+        if (index == -1) return QString();
+        selector.prepend(QString(".children().eq(%1)").arg(index));
+        element = parent;
+        parent = element.parent();
+    }
+    return selector.prepend("$('html')");
+}
+
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
 //  Fb2TreeModel
 //  Fb2TreeModel
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
@@ -166,4 +191,10 @@ void Fb2TreeModel::select(const QModelIndex &index)
     Fb2TreeItem *node = item(index);
     Fb2TreeItem *node = item(index);
     QWebFrame *frame = m_view.page()->mainFrame();
     QWebFrame *frame = m_view.page()->mainFrame();
     if (node) frame->scroll(0, node->pos().y() - frame->scrollPosition().y());
     if (node) frame->scroll(0, node->pos().y() - frame->scrollPosition().y());
+
+    static QString setCursor = FB2::read(":/js/set_cursor.js");
+    QString javascript = QString("var element=%1;").arg(node->selector()) + setCursor;
+    frame->evaluateJavaScript(javascript);
+
+    m_view.setFocus();
 }
 }

+ 2 - 0
source/fb2tree.hpp

@@ -44,6 +44,8 @@ public:
         return m_element.geometry().topLeft();
         return m_element.geometry().topLeft();
     }
     }
 
 
+    QString selector() const;
+
 private:
 private:
     QString static title(const QWebElement &element);
     QString static title(const QWebElement &element);
     void addChildren(QWebElement &parent);
     void addChildren(QWebElement &parent);

+ 10 - 3
source/fb2view.cpp

@@ -1,6 +1,7 @@
 #include "fb2view.hpp"
 #include "fb2view.hpp"
 #include "fb2read.hpp"
 #include "fb2read.hpp"
 #include "fb2save.h"
 #include "fb2save.h"
+#include "fb2tool.h"
 #include "fb2xml2.h"
 #include "fb2xml2.h"
 
 
 #include <QAction>
 #include <QAction>
@@ -247,7 +248,13 @@ void Fb2WebView::insertImage()
 
 
 void Fb2WebView::execCommand(const QString &cmd, const QString &arg)
 void Fb2WebView::execCommand(const QString &cmd, const QString &arg)
 {
 {
-    QWebFrame *frame = page()->mainFrame();
-    QString js = QString("document.execCommand(\"%1\", false, \"%2\")").arg(cmd).arg(arg);
-    frame->evaluateJavaScript(js);
+    QString javascript = QString("document.execCommand(\"%1\",false,\"%2\")").arg(cmd).arg(arg);
+    page()->mainFrame()->evaluateJavaScript(javascript);
+}
+
+QString Fb2WebView::status()
+{
+    static QString javascript = FB2::read(":/js/get_status.js");
+    return page()->mainFrame()->evaluateJavaScript(javascript).toString();
+    return QString();
 }
 }

+ 1 - 0
source/fb2view.hpp

@@ -64,6 +64,7 @@ public:
     bool save(QIODevice *device);
     bool save(QIODevice *device);
     bool save(QString *string);
     bool save(QString *string);
     QString toBodyXml();
     QString toBodyXml();
+    QString status();
 
 
     bool UndoEnabled();
     bool UndoEnabled();
     bool RedoEnabled();
     bool RedoEnabled();

+ 7 - 0
source/js/get_status.js

@@ -0,0 +1,7 @@
+(f = function(node){
+    var tag = node.tagName;
+    if (tag === 'BODY') return '';
+    if (tag === 'DIV') tag = node.getAttribute('CLASS');
+    return f(node.parentNode) + '/' + tag;
+})(document.getSelection().baseNode.parentNode);
+

+ 7 - 0
source/js/javascript.qrc

@@ -0,0 +1,7 @@
+<RCC>
+    <qresource prefix="/js">
+        <file alias="jquery.js">../../3rdparty/jQuery/jquery.js</file>
+        <file>set_cursor.js</file>
+        <file>get_status.js</file>
+    </qresource>
+</RCC>

+ 6 - 0
source/js/set_cursor.js

@@ -0,0 +1,6 @@
+var range = document.createRange();
+range.setStart(element, 0);
+range.setEnd(element, 0);
+var selection = window.getSelection();
+selection.removeAllRanges();
+selection.addRange(range);

+ 1 - 0
source/res/fb2edit.qrc

@@ -8,5 +8,6 @@
         <file>blank.fb2</file>
         <file>blank.fb2</file>
         <file>style.css</file>
         <file>style.css</file>
         <file alias="ts/ru.qm">../ts/ru.qm</file>
         <file alias="ts/ru.qm">../ts/ru.qm</file>
+        <file alias="js/jquery.js">../../3rdparty/jQuery/jquery.js</file>
     </qresource>
     </qresource>
 </RCC>
 </RCC>

برخی فایل ها در این مقایسه diff نمایش داده نمی شوند زیرا تعداد فایل ها بسیار زیاد است