change VEditor to VEditTab

Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
Le Tan 2016-11-06 14:12:09 +08:00
parent d8aa464653
commit 29a41e8285
7 changed files with 57 additions and 49 deletions

View File

@ -22,7 +22,6 @@ SOURCES += main.cpp\
vfilelist.cpp \ vfilelist.cpp \
dialog/vnewfiledialog.cpp \ dialog/vnewfiledialog.cpp \
vedit.cpp \ vedit.cpp \
veditor.cpp \
vnotefile.cpp \ vnotefile.cpp \
vdocument.cpp \ vdocument.cpp \
utils/vutils.cpp \ utils/vutils.cpp \
@ -41,7 +40,8 @@ SOURCES += main.cpp\
dialog/vinsertimagedialog.cpp \ dialog/vinsertimagedialog.cpp \
vdownloader.cpp \ vdownloader.cpp \
veditarea.cpp \ veditarea.cpp \
veditwindow.cpp veditwindow.cpp \
vedittab.cpp
HEADERS += vmainwindow.h \ HEADERS += vmainwindow.h \
vdirectorytree.h \ vdirectorytree.h \
@ -52,7 +52,6 @@ HEADERS += vmainwindow.h \
vfilelist.h \ vfilelist.h \
dialog/vnewfiledialog.h \ dialog/vnewfiledialog.h \
vedit.h \ vedit.h \
veditor.h \
vconstants.h \ vconstants.h \
vnotefile.h \ vnotefile.h \
vdocument.h \ vdocument.h \
@ -73,7 +72,8 @@ HEADERS += vmainwindow.h \
dialog/vinsertimagedialog.h \ dialog/vinsertimagedialog.h \
vdownloader.h \ vdownloader.h \
veditarea.h \ veditarea.h \
veditwindow.h veditwindow.h \
vedittab.h
RESOURCES += \ RESOURCES += \
vnote.qrc vnote.qrc

View File

@ -1,7 +1,7 @@
#include <QtWidgets> #include <QtWidgets>
#include "veditarea.h" #include "veditarea.h"
#include "veditwindow.h" #include "veditwindow.h"
#include "veditor.h" #include "vedittab.h"
#include "vnote.h" #include "vnote.h"
#include "vconfigmanager.h" #include "vconfigmanager.h"
@ -143,16 +143,16 @@ int VEditArea::openFileInWindow(int windowIndex, const QString &notebook, const
void VEditArea::setCurrentTab(int windowIndex, int tabIndex, bool setFocus) void VEditArea::setCurrentTab(int windowIndex, int tabIndex, bool setFocus)
{ {
setCurrentWindow(windowIndex, setFocus);
VEditWindow *win = getWindow(windowIndex); VEditWindow *win = getWindow(windowIndex);
win->setCurrentIndex(tabIndex); win->setCurrentIndex(tabIndex);
setCurrentWindow(windowIndex, setFocus);
} }
void VEditArea::setCurrentWindow(int windowIndex, bool setFocus) void VEditArea::setCurrentWindow(int windowIndex, bool setFocus)
{ {
if (curWindowIndex == windowIndex) { if (curWindowIndex == windowIndex) {
return; goto out;
} }
qDebug() << "current window" << windowIndex; qDebug() << "current window" << windowIndex;
curWindowIndex = windowIndex; curWindowIndex = windowIndex;
@ -160,7 +160,13 @@ void VEditArea::setCurrentWindow(int windowIndex, bool setFocus)
getWindow(windowIndex)->focusWindow(); getWindow(windowIndex)->focusWindow();
} }
out:
// Update tab status // Update tab status
noticeTabStatus();
}
void VEditArea::noticeTabStatus()
{
QString notebook, relativePath; QString notebook, relativePath;
bool editMode, modifiable; bool editMode, modifiable;
getWindow(curWindowIndex)->getTabStatus(notebook, relativePath, editMode, modifiable); getWindow(curWindowIndex)->getTabStatus(notebook, relativePath, editMode, modifiable);

View File

@ -54,6 +54,7 @@ private:
inline VEditWindow *getWindow(int windowIndex) const; inline VEditWindow *getWindow(int windowIndex) const;
void insertSplitWindow(int idx); void insertSplitWindow(int idx);
void removeSplitWindow(VEditWindow *win); void removeSplitWindow(VEditWindow *win);
void noticeTabStatus();
VNote *vnote; VNote *vnote;
int curWindowIndex; int curWindowIndex;

View File

@ -3,7 +3,7 @@
#include <QWebChannel> #include <QWebChannel>
#include <QWebEngineView> #include <QWebEngineView>
#include <QFileInfo> #include <QFileInfo>
#include "veditor.h" #include "vedittab.h"
#include "vedit.h" #include "vedit.h"
#include "vdocument.h" #include "vdocument.h"
#include "vnote.h" #include "vnote.h"
@ -16,13 +16,13 @@
extern VConfigManager vconfig; extern VConfigManager vconfig;
VEditor::VEditor(const QString &path, bool modifiable, QWidget *parent) VEditTab::VEditTab(const QString &path, bool modifiable, QWidget *parent)
: QStackedWidget(parent), mdConverterType(vconfig.getMdConverterType()) : QStackedWidget(parent), mdConverterType(vconfig.getMdConverterType())
{ {
DocType docType = isMarkdown(path) ? DocType::Markdown : DocType::Html; DocType docType = isMarkdown(path) ? DocType::Markdown : DocType::Html;
QString basePath = QFileInfo(path).path(); QString basePath = QFileInfo(path).path();
QString fileName = QFileInfo(path).fileName(); QString fileName = QFileInfo(path).fileName();
qDebug() << "VEditor basePath" << basePath << "file" << fileName; qDebug() << "VEditTab basePath" << basePath << "file" << fileName;
QString fileText = VUtils::readFileFromDisk(path); QString fileText = VUtils::readFileFromDisk(path);
noteFile = new VNoteFile(basePath, fileName, fileText, noteFile = new VNoteFile(basePath, fileName, fileText,
docType, modifiable); docType, modifiable);
@ -34,17 +34,17 @@ VEditor::VEditor(const QString &path, bool modifiable, QWidget *parent)
showFileReadMode(); showFileReadMode();
connect(qApp, &QApplication::focusChanged, connect(qApp, &QApplication::focusChanged,
this, &VEditor::handleFocusChanged); this, &VEditTab::handleFocusChanged);
} }
VEditor::~VEditor() VEditTab::~VEditTab()
{ {
if (noteFile) { if (noteFile) {
delete noteFile; delete noteFile;
} }
} }
void VEditor::setupUI() void VEditTab::setupUI()
{ {
textEditor = new VEdit(noteFile); textEditor = new VEdit(noteFile);
addWidget(textEditor); addWidget(textEditor);
@ -67,7 +67,7 @@ void VEditor::setupUI()
} }
} }
bool VEditor::isMarkdown(const QString &name) bool VEditTab::isMarkdown(const QString &name)
{ {
const QVector<QString> mdPostfix({"md", "markdown", "mkd"}); const QVector<QString> mdPostfix({"md", "markdown", "mkd"});
@ -84,7 +84,7 @@ bool VEditor::isMarkdown(const QString &name)
return false; return false;
} }
void VEditor::showFileReadMode() void VEditTab::showFileReadMode()
{ {
isEditMode = false; isEditMode = false;
switch (noteFile->docType) { switch (noteFile->docType) {
@ -107,7 +107,7 @@ void VEditor::showFileReadMode()
} }
} }
void VEditor::previewByConverter() void VEditTab::previewByConverter()
{ {
VMarkdownConverter mdConverter; VMarkdownConverter mdConverter;
QString content = noteFile->content; QString content = noteFile->content;
@ -119,7 +119,7 @@ void VEditor::previewByConverter()
webPreviewer->setHtml(completeHtml, QUrl::fromLocalFile(noteFile->basePath + QDir::separator())); webPreviewer->setHtml(completeHtml, QUrl::fromLocalFile(noteFile->basePath + QDir::separator()));
} }
void VEditor::showFileEditMode() void VEditTab::showFileEditMode()
{ {
isEditMode = true; isEditMode = true;
textEditor->beginEdit(); textEditor->beginEdit();
@ -127,13 +127,13 @@ void VEditor::showFileEditMode()
textEditor->setFocus(); textEditor->setFocus();
} }
bool VEditor::requestClose() bool VEditTab::requestClose()
{ {
readFile(); readFile();
return !isEditMode; return !isEditMode;
} }
void VEditor::editFile() void VEditTab::editFile()
{ {
if (isEditMode || !noteFile->modifiable) { if (isEditMode || !noteFile->modifiable) {
return; return;
@ -142,7 +142,7 @@ void VEditor::editFile()
showFileEditMode(); showFileEditMode();
} }
void VEditor::readFile() void VEditTab::readFile()
{ {
if (!isEditMode) { if (!isEditMode) {
return; return;
@ -176,7 +176,7 @@ void VEditor::readFile()
showFileReadMode(); showFileReadMode();
} }
bool VEditor::saveFile() bool VEditTab::saveFile()
{ {
if (!isEditMode || !noteFile->modifiable || !textEditor->isModified()) { if (!isEditMode || !noteFile->modifiable || !textEditor->isModified()) {
return true; return true;
@ -207,7 +207,7 @@ bool VEditor::saveFile()
return true; return true;
} }
void VEditor::setupMarkdownPreview() void VEditTab::setupMarkdownPreview()
{ {
webPreviewer = new QWebEngineView(this); webPreviewer = new QWebEngineView(this);
VPreviewPage *page = new VPreviewPage(this); VPreviewPage *page = new VPreviewPage(this);
@ -224,12 +224,12 @@ void VEditor::setupMarkdownPreview()
addWidget(webPreviewer); addWidget(webPreviewer);
} }
void VEditor::focusTab() void VEditTab::focusTab()
{ {
currentWidget()->setFocus(); currentWidget()->setFocus();
} }
void VEditor::handleFocusChanged(QWidget *old, QWidget *now) void VEditTab::handleFocusChanged(QWidget *old, QWidget *now)
{ {
if (isChild(now)) { if (isChild(now)) {
emit getFocused(); emit getFocused();

View File

@ -1,5 +1,5 @@
#ifndef VEDITOR_H #ifndef VEDITTAB_H
#define VEDITOR_H #define VEDITTAB_H
#include <QStackedWidget> #include <QStackedWidget>
#include <QString> #include <QString>
@ -14,12 +14,12 @@ class QTextBrowser;
class QWebEngineView; class QWebEngineView;
class VNote; class VNote;
class VEditor : public QStackedWidget class VEditTab : public QStackedWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
VEditor(const QString &path, bool modifiable, QWidget *parent = 0); VEditTab(const QString &path, bool modifiable, QWidget *parent = 0);
~VEditor(); ~VEditTab();
bool requestClose(); bool requestClose();
// Enter edit mode // Enter edit mode
void editFile(); void editFile();
@ -56,17 +56,17 @@ private:
MarkdownConverterType mdConverterType; MarkdownConverterType mdConverterType;
}; };
inline bool VEditor::getIsEditMode() const inline bool VEditTab::getIsEditMode() const
{ {
return isEditMode; return isEditMode;
} }
inline bool VEditor::isModified() const inline bool VEditTab::isModified() const
{ {
return textEditor->isModified(); return textEditor->isModified();
} }
inline bool VEditor::isChild(QObject *obj) inline bool VEditTab::isChild(QObject *obj)
{ {
while (obj) { while (obj) {
if (obj == this) { if (obj == this) {
@ -77,4 +77,4 @@ inline bool VEditor::isChild(QObject *obj)
return false; return false;
} }
#endif // VEDITOR_H #endif // VEDITTAB_H

View File

@ -1,7 +1,7 @@
#include <QtWidgets> #include <QtWidgets>
#include <QtDebug> #include <QtDebug>
#include "veditwindow.h" #include "veditwindow.h"
#include "veditor.h" #include "vedittab.h"
#include "vnote.h" #include "vnote.h"
#include "vconfigmanager.h" #include "vconfigmanager.h"
@ -107,6 +107,7 @@ out:
editFile(); editFile();
} }
focusWindow(); focusWindow();
noticeTabStatus(idx);
return idx; return idx;
} }
@ -119,7 +120,7 @@ void VEditWindow::closeFile(const QString &notebook, const QString &relativePath
} }
// Do not check if modified // Do not check if modified
VEditor *editor = getTab(idx); VEditTab *editor = getTab(idx);
Q_ASSERT(editor); Q_ASSERT(editor);
removeTab(idx); removeTab(idx);
delete editor; delete editor;
@ -149,9 +150,9 @@ int VEditWindow::openFileInTab(const QString &notebook, const QString &relativeP
} }
} }
VEditor *editor = new VEditor(QDir::cleanPath(QDir(rootPath).filePath(relativePath)), VEditTab *editor = new VEditTab(QDir::cleanPath(QDir(rootPath).filePath(relativePath)),
modifiable); modifiable);
connect(editor, &VEditor::getFocused, connect(editor, &VEditTab::getFocused,
this, &VEditWindow::getFocused); this, &VEditWindow::getFocused);
QJsonObject tabJson; QJsonObject tabJson;
@ -178,7 +179,7 @@ int VEditWindow::findTabByFile(const QString &notebook, const QString &relativeP
bool VEditWindow::handleTabCloseRequest(int index) bool VEditWindow::handleTabCloseRequest(int index)
{ {
qDebug() << "request closing tab" << index; qDebug() << "request closing tab" << index;
VEditor *editor = getTab(index); VEditTab *editor = getTab(index);
Q_ASSERT(editor); Q_ASSERT(editor);
bool ok = editor->requestClose(); bool ok = editor->requestClose();
if (ok) { if (ok) {
@ -194,7 +195,7 @@ bool VEditWindow::handleTabCloseRequest(int index)
void VEditWindow::readFile() void VEditWindow::readFile()
{ {
VEditor *editor = getTab(currentIndex()); VEditTab *editor = getTab(currentIndex());
Q_ASSERT(editor); Q_ASSERT(editor);
editor->readFile(); editor->readFile();
noticeTabStatus(currentIndex()); noticeTabStatus(currentIndex());
@ -209,7 +210,7 @@ void VEditWindow::saveAndReadFile()
void VEditWindow::editFile() void VEditWindow::editFile()
{ {
VEditor *editor = getTab(currentIndex()); VEditTab *editor = getTab(currentIndex());
Q_ASSERT(editor); Q_ASSERT(editor);
editor->editFile(); editor->editFile();
noticeTabStatus(currentIndex()); noticeTabStatus(currentIndex());
@ -217,7 +218,7 @@ void VEditWindow::editFile()
void VEditWindow::saveFile() void VEditWindow::saveFile()
{ {
VEditor *editor = getTab(currentIndex()); VEditTab *editor = getTab(currentIndex());
Q_ASSERT(editor); Q_ASSERT(editor);
editor->saveFile(); editor->saveFile();
} }
@ -248,7 +249,7 @@ void VEditWindow::noticeTabStatus(int index)
QString notebook = tabJson["notebook"].toString(); QString notebook = tabJson["notebook"].toString();
QString relativePath = tabJson["relative_path"].toString(); QString relativePath = tabJson["relative_path"].toString();
VEditor *editor = getTab(index); VEditTab *editor = getTab(index);
bool editMode = editor->getIsEditMode(); bool editMode = editor->getIsEditMode();
bool modifiable = tabJson["modifiable"].toBool(); bool modifiable = tabJson["modifiable"].toBool();
@ -270,7 +271,7 @@ void VEditWindow::getTabStatus(QString &notebook, QString &relativePath,
Q_ASSERT(!tabJson.isEmpty()); Q_ASSERT(!tabJson.isEmpty());
notebook = tabJson["notebook"].toString(); notebook = tabJson["notebook"].toString();
relativePath = tabJson["relative_path"].toString(); relativePath = tabJson["relative_path"].toString();
VEditor *editor = getTab(idx); VEditTab *editor = getTab(idx);
editMode = editor->getIsEditMode(); editMode = editor->getIsEditMode();
modifiable = tabJson["modifiable"].toBool(); modifiable = tabJson["modifiable"].toBool();
} }

View File

@ -7,7 +7,7 @@
#include <QFileInfo> #include <QFileInfo>
#include <QDir> #include <QDir>
#include "vnotebook.h" #include "vnotebook.h"
#include "veditor.h" #include "vedittab.h"
class VNote; class VNote;
class QPushButton; class QPushButton;
@ -60,7 +60,7 @@ private:
int appendTabWithData(QWidget *page, const QJsonObject &tabData); int appendTabWithData(QWidget *page, const QJsonObject &tabData);
int openFileInTab(const QString &notebook, const QString &relativePath, bool modifiable); int openFileInTab(const QString &notebook, const QString &relativePath, bool modifiable);
inline QString getFileName(const QString &relativePath) const; inline QString getFileName(const QString &relativePath) const;
inline VEditor *getTab(int tabIndex) const; inline VEditTab *getTab(int tabIndex) const;
void noticeTabStatus(int index); void noticeTabStatus(int index);
VNote *vnote; VNote *vnote;
@ -77,9 +77,9 @@ inline QString VEditWindow::getFileName(const QString &path) const
return QFileInfo(QDir::cleanPath(path)).fileName(); return QFileInfo(QDir::cleanPath(path)).fileName();
} }
inline VEditor* VEditWindow::getTab(int tabIndex) const inline VEditTab* VEditWindow::getTab(int tabIndex) const
{ {
return dynamic_cast<VEditor *>(widget(tabIndex)); return dynamic_cast<VEditTab *>(widget(tabIndex));
} }
#endif // VEDITWINDOW_H #endif // VEDITWINDOW_H