Browse Source

VMdTab: use splitter instead of tab layout to hold editor and web view

Prepare for live preview.
Le Tan 7 years ago
parent
commit
051088be31

+ 1 - 1
src/resources/export_template.html

@@ -1,4 +1,4 @@
-<!doctype html>
+<!DOCTYPE html>
 <html>
 <meta charset="utf-8">
 <head>

+ 1 - 1
src/resources/markdown_template.html

@@ -1,4 +1,4 @@
-<!doctype html>
+<!DOCTYPE html>
 <html>
 <meta charset="utf-8">
 <head>

+ 1 - 1
src/resources/simple_template.html

@@ -1,4 +1,4 @@
-<!doctype html>
+<!DOCTYPE html>
 <html>
 <meta charset="utf-8">
 <head>

+ 4 - 2
src/resources/themes/v_moonlight/v_moonlight.css

@@ -164,13 +164,14 @@ table tr th :last-child, table tr td :last-child {
 }
 
 div.mermaid-diagram {
-    width: fit-content;
-    overflow: hidden;
+    margin: 16px 0px 16px 0px;
+    overflow-y: hidden;
     background: #B0BEC5;
     color: #6C6C6C;
 }
 
 div.flowchart-diagram {
+    margin: 16px 0px 16px 0px;
     width: fit-content;
     overflow: hidden;
     background: #B0BEC5;
@@ -178,6 +179,7 @@ div.flowchart-diagram {
 }
 
 div.plantuml-diagram {
+    margin: 16px 0px 16px 0px;
     width: fit-content;
     overflow: hidden;
     background: #B0BEC5;

+ 4 - 2
src/resources/themes/v_native/v_native.css

@@ -169,16 +169,18 @@ table tr th :last-child, table tr td :last-child {
 }
 
 div.mermaid-diagram {
-    width: fit-content;
-    overflow: hidden;
+    margin: 16px 0px 16px 0px;
+    overflow-y: hidden;
 }
 
 div.flowchart-diagram {
+    margin: 16px 0px 16px 0px;
     width: fit-content;
     overflow: hidden;
 }
 
 div.plantuml-diagram {
+    margin: 16px 0px 16px 0px;
     width: fit-content;
     overflow: hidden;
 }

+ 4 - 2
src/resources/themes/v_pure/v_pure.css

@@ -170,16 +170,18 @@ table tr th :last-child, table tr td :last-child {
 }
 
 div.mermaid-diagram {
-    width: fit-content;
-    overflow: hidden;
+    margin: 16px 0px 16px 0px;
+    overflow-y: hidden;
 }
 
 div.flowchart-diagram {
+    margin: 16px 0px 16px 0px;
     width: fit-content;
     overflow: hidden;
 }
 
 div.plantuml-diagram {
+    margin: 16px 0px 16px 0px;
     width: fit-content;
     overflow: hidden;
 }

+ 1 - 2
src/vlinenumberarea.cpp

@@ -33,8 +33,7 @@ int VLineNumberArea::calculateWidth() const
         ++digits;
     }
 
-    int width = m_digitWidth * (digits + 1);
-    const_cast<VLineNumberArea *>(this)->m_width = width;
+    const_cast<VLineNumberArea *>(this)->m_width = m_digitWidth * digits + 3;
 
     return m_width;
 }

+ 44 - 7
src/vmdtab.cpp

@@ -68,14 +68,18 @@ VMdTab::VMdTab(VFile *p_file, VEditArea *p_editArea,
 
 void VMdTab::setupUI()
 {
-    m_stacks = new QStackedLayout(this);
+    m_splitter = new QSplitter(this);
+    m_splitter->setOrientation(Qt::Horizontal);
 
     setupMarkdownViewer();
 
     // Setup editor when we really need it.
     m_editor = NULL;
 
-    setLayout(m_stacks);
+    QVBoxLayout *layout = new QVBoxLayout();
+    layout->addWidget(m_splitter);
+    layout->setContentsMargins(0, 0, 0, 0);
+    setLayout(layout);
 }
 
 void VMdTab::showFileReadMode()
@@ -87,7 +91,8 @@ void VMdTab::showFileReadMode()
 
     updateWebView();
 
-    m_stacks->setCurrentWidget(m_webViewer);
+    setCurrentMode(Mode::Read);
+
     clearSearchedWordHighlight();
 
     updateStatus();
@@ -216,7 +221,8 @@ void VMdTab::showFileEditMode()
 
     VMdEditor *mdEdit = getEditor();
 
-    m_stacks->setCurrentWidget(mdEdit);
+    setCurrentMode(Mode::Edit);
+
     mdEdit->beginEdit();
 
     // If editor is not init, we need to wait for it to init headers.
@@ -444,7 +450,7 @@ void VMdTab::setupMarkdownViewer()
     m_webViewer->setHtml(VUtils::generateHtmlTemplate(m_mdConType),
                          m_file->getBaseUrl());
 
-    m_stacks->addWidget(m_webViewer);
+    m_splitter->addWidget(m_webViewer);
 }
 
 void VMdTab::setupMarkdownEditor()
@@ -503,7 +509,7 @@ void VMdTab::setupMarkdownEditor()
 
     enableHeadingSequence(m_enableHeadingSequence);
     m_editor->reloadFile();
-    m_stacks->addWidget(m_editor);
+    m_splitter->insertWidget(0, m_editor);
 }
 
 void VMdTab::updateOutlineFromHtml(const QString &p_tocHtml)
@@ -781,7 +787,11 @@ MarkdownConverterType VMdTab::getMarkdownConverterType() const
 
 void VMdTab::focusChild()
 {
-    m_stacks->currentWidget()->setFocus();
+    if (m_mode == Mode::Read) {
+        m_webViewer->setFocus();
+    } else {
+        m_editor->setFocus();
+    }
 }
 
 void VMdTab::requestUpdateVimStatus()
@@ -1303,3 +1313,30 @@ VWordCountInfo VMdTab::fetchWordCountInfo(bool p_editMode) const
 
     return VWordCountInfo();
 }
+
+void VMdTab::setCurrentMode(Mode p_mode)
+{
+    switch (p_mode) {
+    case Mode::Read:
+        m_webViewer->show();
+        if (m_editor) {
+            m_editor->hide();
+        }
+
+        break;
+
+    case Mode::EditPreview:
+    case Mode::Edit:
+        Q_ASSERT(m_editor);
+        m_editor->show();
+        m_webViewer->hide();
+        break;
+
+    default:
+        break;
+    }
+
+    m_mode = p_mode;
+
+    focusChild();
+}

+ 8 - 2
src/vmdtab.h

@@ -9,12 +9,12 @@
 #include "vconfigmanager.h"
 
 class VWebView;
-class QStackedLayout;
 class VDocument;
 class VMdEditor;
 class VInsertSelector;
 class QTimer;
 class QWebEngineDownloadItem;
+class QSplitter;
 
 class VMdTab : public VEditTab
 {
@@ -145,6 +145,8 @@ private slots:
 private:
     enum TabReady { None = 0, ReadMode = 0x1, EditMode = 0x2 };
 
+    enum Mode { Read = 0, Edit, EditPreview };
+
     // Setup UI.
     void setupUI();
 
@@ -213,6 +215,8 @@ private:
     // Update web view by current content.
     void updateWebView();
 
+    void setCurrentMode(Mode p_mode);
+
     VMdEditor *m_editor;
     VWebView *m_webViewer;
     VDocument *m_document;
@@ -221,7 +225,7 @@ private:
     // Whether heading sequence is enabled.
     bool m_enableHeadingSequence;
 
-    QStackedLayout *m_stacks;
+    QSplitter *m_splitter;
 
     // Timer to write backup file when content has been changed.
     QTimer *m_backupTimer;
@@ -232,6 +236,8 @@ private:
     VHeaderPointer m_headerFromEditMode;
 
     VVim::SearchItem m_lastSearchItem;
+
+    Mode m_mode;
 };
 
 inline VMdEditor *VMdTab::getEditor()