propagate key press event from QWebEngineView to VDocument

Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
Le Tan 2017-01-21 09:42:48 +08:00
parent 46988f5bef
commit fb1a172acf
7 changed files with 38 additions and 6 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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