Browse Source

propagate key press event from QWebEngineView to VDocument

Signed-off-by: Le Tan <[email protected]>
Le Tan 9 years ago
parent
commit
fb1a172acf
7 changed files with 38 additions and 6 deletions
  1. 1 0
      src/resources/pre_template.html
  2. 1 0
      src/resources/template.html
  3. 6 2
      src/vdocument.cpp
  4. 2 3
      src/vdocument.h
  5. 23 1
      src/vedittab.cpp
  6. 4 0
      src/vedittab.h
  7. 1 0
      src/veditwindow.cpp

+ 1 - 0
src/resources/pre_template.html

@@ -136,6 +136,7 @@
                 return;
 
             default:
+                content.keyPressEvent(key);
                 keyState = 0;
                 return;
         }

+ 1 - 0
src/resources/template.html

@@ -253,6 +253,7 @@
                 return;
 
             default:
+                content.keyPressEvent(key);
                 keyState = 0;
                 return;
         }

+ 6 - 2
src/vdocument.cpp

@@ -66,6 +66,10 @@ void VDocument::setHtml(const QString &html)
 void VDocument::setLog(const QString &p_log)
 {
     qDebug() << "JS:" << p_log;
-    m_log = p_log;
-    emit logChanged(m_log);
+    emit logChanged(p_log);
+}
+
+void VDocument::keyPressEvent(int p_key)
+{
+    emit keyPressed(p_key);
 }

+ 2 - 3
src/vdocument.h

@@ -10,7 +10,6 @@ class VDocument : public QObject
     Q_PROPERTY(QString text MEMBER m_text NOTIFY textChanged)
     Q_PROPERTY(QString toc MEMBER m_toc NOTIFY tocChanged)
     Q_PROPERTY(QString html MEMBER m_html NOTIFY htmlChanged)
-    Q_PROPERTY(QString log MEMBER m_log NOTIFY logChanged)
 
 public:
     explicit VDocument(QObject *parent = 0);
@@ -26,6 +25,7 @@ public slots:
     void setToc(const QString &toc);
     void setHeader(const QString &anchor);
     void setLog(const QString &p_log);
+    void keyPressEvent(int p_key);
 
 signals:
     void textChanged(const QString &text);
@@ -34,14 +34,13 @@ signals:
     void headerChanged(const QString &anchor);
     void htmlChanged(const QString &html);
     void logChanged(const QString &p_log);
+    void keyPressed(int p_key);
 
 private:
     QString m_text;
     QString m_toc;
     QString m_header;
     QString m_html;
-    // Used for debugging
-    QString m_log;
 };
 
 #endif // VDOCUMENT_H

+ 23 - 1
src/vedittab.cpp

@@ -16,12 +16,14 @@
 #include "vtoc.h"
 #include "vmdedit.h"
 #include "dialog/vfindreplacedialog.h"
+#include "veditarea.h"
 
 extern VConfigManager vconfig;
 
 VEditTab::VEditTab(VFile *p_file, OpenFileMode p_mode, QWidget *p_parent)
     : QStackedWidget(p_parent), m_file(p_file), isEditMode(false),
-      mdConverterType(vconfig.getMdConverterType()), m_fileModified(false)
+      mdConverterType(vconfig.getMdConverterType()), m_fileModified(false),
+      m_editArea(NULL)
 {
     tableOfContent.filePath = p_file->retrivePath();
     curHeader.filePath = p_file->retrivePath();
@@ -44,6 +46,11 @@ VEditTab::~VEditTab()
     }
 }
 
+void VEditTab::init(VEditArea *p_editArea)
+{
+    m_editArea = p_editArea;
+}
+
 void VEditTab::setupUI()
 {
     switch (m_file->getDocType()) {
@@ -262,6 +269,8 @@ void VEditTab::setupMarkdownPreview()
             this, &VEditTab::updateTocFromHtml);
     connect(&document, SIGNAL(headerChanged(const QString&)),
             this, SLOT(updateCurHeader(const QString &)));
+    connect(&document, &VDocument::keyPressed,
+            this, &VEditTab::handleWebKeyPressed);
     page->setWebChannel(channel);
 
     if (mdConverterType == MarkdownConverterType::Marked) {
@@ -538,3 +547,16 @@ bool VEditTab::checkToc()
     return ret;
 }
 
+void VEditTab::handleWebKeyPressed(int p_key)
+{
+    switch (p_key) {
+    // Esc
+    case 27:
+        m_editArea->getFindReplaceDialog()->closeDialog();
+        break;
+
+    default:
+        break;
+    }
+}
+

+ 4 - 0
src/vedittab.h

@@ -15,6 +15,7 @@
 class QWebEngineView;
 class VNote;
 class QXmlStreamReader;
+class VEditArea;
 
 class VEditTab : public QStackedWidget
 {
@@ -22,6 +23,7 @@ class VEditTab : public QStackedWidget
 public:
     VEditTab(VFile *p_file, OpenFileMode p_mode, QWidget *p_parent = 0);
     ~VEditTab();
+    void init(VEditArea *p_editArea);
     bool closeFile(bool p_forced);
     // Enter edit mode
     void editFile();
@@ -63,6 +65,7 @@ private slots:
     void updateTocFromHeaders(const QVector<VHeader> &headers);
     void handleTextChanged();
     void noticeStatusChanged();
+    void handleWebKeyPressed(int p_key);
 
 private:
     void setupUI();
@@ -90,6 +93,7 @@ private:
     VToc tableOfContent;
     VAnchor curHeader;
     bool m_fileModified;
+    VEditArea *m_editArea;
 };
 
 inline bool VEditTab::getIsEditMode() const

+ 1 - 0
src/veditwindow.cpp

@@ -242,6 +242,7 @@ bool VEditWindow::closeAllFiles(bool p_forced)
 int VEditWindow::openFileInTab(VFile *p_file, OpenFileMode p_mode)
 {
     VEditTab *editor = new VEditTab(p_file, p_mode);
+    editor->init(m_editArea);
     connect(editor, &VEditTab::getFocused,
             this, &VEditWindow::getFocused);
     connect(editor, &VEditTab::outlineChanged,