Kandrashin Denis 13 лет назад
Родитель
Сommit
5833ae3125
8 измененных файлов с 80 добавлено и 8 удалено
  1. 2 2
      source/fb2head.cpp
  2. 9 0
      source/fb2html.cpp
  3. 2 0
      source/fb2html.h
  4. 1 0
      source/fb2main.cpp
  5. 14 5
      source/fb2tree.cpp
  6. 3 0
      source/fb2tree.hpp
  7. 46 1
      source/fb2view.cpp
  8. 3 0
      source/fb2view.hpp

+ 2 - 2
source/fb2head.cpp

@@ -465,7 +465,7 @@ Fb2HeadView::Fb2HeadView(Fb2WebView &view, QWidget *parent)
 
     actionInsert = act = new QAction(FB2::icon("list-add"), tr("&Append"), this);
     act->setShortcutContext(Qt::WidgetShortcut);
-    act->setShortcut(QKeySequence("Insert"));
+    act->setShortcut(Qt::Key_Insert);
     act->setPriority(QAction::LowPriority);
     connect(act, SIGNAL(triggered()), SLOT(appendNode()));
     addAction(act);
@@ -475,7 +475,7 @@ Fb2HeadView::Fb2HeadView(Fb2WebView &view, QWidget *parent)
 
     actionDelete = act = new QAction(FB2::icon("list-remove"), tr("&Delete"), this);
     act->setShortcutContext(Qt::WidgetShortcut);
-    act->setShortcut(QKeySequence("Delete"));
+    act->setShortcut(Qt::Key_Delete);
     act->setPriority(QAction::LowPriority);
     connect(act, SIGNAL(triggered()), SLOT(removeNode()));
     addAction(act);

+ 9 - 0
source/fb2html.cpp

@@ -11,3 +11,12 @@ void Fb2WebElement::select()
     evaluateJavaScript(javascript);
 }
 
+bool Fb2WebElement::isSection() const
+{
+    return tagName() == "DIV" && attribute("class").toLower() == "section";
+}
+
+bool Fb2WebElement::isTitle() const
+{
+    return tagName() == "DIV" && attribute("class").toLower() == "title";
+}

+ 2 - 0
source/fb2html.h

@@ -9,6 +9,8 @@ public:
     Fb2WebElement() {}
     Fb2WebElement(const QWebElement &x) : QWebElement(x) {}
     Fb2WebElement &operator=(const QWebElement &x) { QWebElement::operator=(x); return *this; }
+    bool isSection() const;
+    bool isTitle() const;
 
 public:
     void select();

+ 1 - 0
source/fb2main.cpp

@@ -693,6 +693,7 @@ void Fb2MainWindow::viewText()
     connect(actionNote, SIGNAL(triggered()), textEdit, SLOT(insertNote()));
     connect(actionLink, SIGNAL(triggered()), textEdit, SLOT(insertLink()));
     connect(actionTitle, SIGNAL(triggered()), textEdit, SLOT(insertTitle()));
+    connect(actionSubtitle, SIGNAL(triggered()), textEdit, SLOT(insertSubtitle()));
     connect(actionBody, SIGNAL(triggered()), textEdit->page(), SLOT(insertBody()));
 
     connect(actionZoomIn, SIGNAL(triggered()), textEdit, SLOT(zoomIn()));

+ 14 - 5
source/fb2tree.cpp

@@ -357,7 +357,7 @@ void Fb2TreeView::initActions(QToolBar *toolbar)
 
     act = new QAction(FB2::icon("list-add"), tr("&Insert"), this);
     act->setShortcutContext(Qt::WidgetShortcut);
-    act->setShortcut(QKeySequence("Insert"));
+    act->setShortcut(Qt::Key_Insert);
     act->setPriority(QAction::LowPriority);
     connect(act, SIGNAL(triggered()), SLOT(insertNode()));
     toolbar->addAction(act);
@@ -365,7 +365,7 @@ void Fb2TreeView::initActions(QToolBar *toolbar)
 
     act = new QAction(FB2::icon("list-remove"), tr("&Delete"), this);
     act->setShortcutContext(Qt::WidgetShortcut);
-    act->setShortcut(QKeySequence("Delete"));
+    act->setShortcut(Qt::Key_Delete);
     act->setPriority(QAction::LowPriority);
     connect(act, SIGNAL(triggered()), SLOT(deleteNode()));
     toolbar->addAction(act);
@@ -425,6 +425,17 @@ void Fb2TreeView::initActions(QToolBar *toolbar)
     m_menu.addAction(act);
 }
 
+void Fb2TreeView::keyPressEvent(QKeyEvent *event)
+{
+    if (event->modifiers() == Qt::NoModifier) {
+        switch (event->key()) {
+            case Qt::Key_Insert: insertNode(); return;
+            case Qt::Key_Delete: deleteNode(); return;
+        }
+    }
+    QTreeView::keyPressEvent(event);
+}
+
 void Fb2TreeView::contextMenu(const QPoint &pos)
 {
     m_menu.exec(QCursor::pos());
@@ -452,9 +463,7 @@ void Fb2TreeView::selectTree()
 {
     if (qApp->focusWidget() == this) return;
     if (Fb2TreeModel * m = model()) {
-        QWebFrame * frame = m->view().page()->mainFrame();
-        static const QString javascript = FB2::read(":/js/get_location.js");
-        QString location = frame->evaluateJavaScript(javascript).toString();
+        QString location = m->view().location();
         QModelIndex index = m->index(location);
         if (!index.isValid()) return;
         setCurrentIndex(index);

+ 3 - 0
source/fb2tree.hpp

@@ -136,6 +136,9 @@ private slots:
     void moveLeft();
     void moveRight();
 
+protected:
+    void keyPressEvent(QKeyEvent *event);
+
 private:
     void moveCurrent(int dx, int dy);
     Fb2TreeModel * model();

+ 46 - 1
source/fb2view.cpp

@@ -370,11 +370,34 @@ void Fb2WebView::execCommand(const QString &cmd, const QString &arg)
     page()->mainFrame()->evaluateJavaScript(javascript);
 }
 
+QWebElement Fb2WebView::current()
+{
+    QStringList list = location().split(",");
+    QStringListIterator iterator(list);
+    QWebElement result = doc();
+    while (iterator.hasNext()) {
+        QString str = iterator.next();
+        int pos = str.indexOf("=");
+        QString tag = str.left(pos);
+        int key = str.mid(pos + 1).toInt();
+        if (key < 0) break;
+        result = result.firstChild();
+        while (0 < key--) result = result.nextSibling();
+        if (tag == "P") break;
+    }
+    return result;
+}
+
+QString Fb2WebView::location()
+{
+    static const QString javascript = FB2::read(":/js/get_location.js");
+    return page()->mainFrame()->evaluateJavaScript(javascript).toString();
+}
+
 QString Fb2WebView::status()
 {
     static const QString javascript = FB2::read(":/js/get_status.js");
     return page()->mainFrame()->evaluateJavaScript(javascript).toString();
-    return QString();
 }
 
 void Fb2WebView::loadFinished()
@@ -392,6 +415,28 @@ void Fb2WebView::insertTitle()
     page()->undoStack()->endMacro();
 }
 
+void Fb2WebView::insertSubtitle()
+{
+    Fb2WebElement element = current();
+    while (!element.isNull()) {
+        Fb2WebElement parent = element.parent();
+        if (parent.isSection()) {
+            Fb2WebElement subtitle;
+            QString html = "<div class=subtitle><p><br/></p></div>";
+            if (element.isTitle()) {
+                element.appendOutside(html);
+                subtitle = element.nextSibling();
+            } else {
+                element.prependOutside(html);
+                subtitle = element.previousSibling();
+            }
+            subtitle.select();
+            break;
+        }
+        element = parent;
+    }
+}
+
 //---------------------------------------------------------------------------
 //  Fb2WebFrame
 //---------------------------------------------------------------------------

+ 3 - 0
source/fb2view.hpp

@@ -77,6 +77,8 @@ public:
     bool save(QIODevice *device, const QString &codec = QString());
     bool save(QByteArray *array);
     bool save(QString *string);
+    QWebElement current();
+    QString location();
     QString status();
 
     bool UndoEnabled();
@@ -100,6 +102,7 @@ public slots:
     void insertNote();
     void insertLink();
     void insertTitle();
+    void insertSubtitle();
     void zoomIn();
     void zoomOut();
     void zoomReset();