From 6a10c7ab3c36bf6e16b7760b9906203a23eb4f40 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Mon, 29 May 2017 18:56:43 +0800 Subject: [PATCH] refactor DocType --- src/dialog/vfindreplacedialog.cpp | 3 ++- src/utils/vutils.cpp | 36 +++++++++++++++++-------------- src/utils/vutils.h | 4 +++- src/vconstants.h | 6 +++++- src/vdirectory.cpp | 2 +- src/vedittab.cpp | 3 +++ src/vfile.cpp | 5 ++--- src/vfilelist.cpp | 3 ++- src/vmdedit.cpp | 2 +- src/vorphanfile.cpp | 1 - 10 files changed, 39 insertions(+), 26 deletions(-) diff --git a/src/dialog/vfindreplacedialog.cpp b/src/dialog/vfindreplacedialog.cpp index 355e3558..b83672f8 100644 --- a/src/dialog/vfindreplacedialog.cpp +++ b/src/dialog/vfindreplacedialog.cpp @@ -294,9 +294,10 @@ void VFindReplaceDialog::updateState(DocType p_docType, bool p_editMode) if (p_editMode || p_docType == DocType::Html) { m_wholeWordOnlyCheck->setEnabled(true); m_regularExpressionCheck->setEnabled(true); - } else { + } else if (p_docType == DocType::Markdown) { m_wholeWordOnlyCheck->setEnabled(false); m_regularExpressionCheck->setEnabled(false); } + m_replaceAvailable = p_editMode; } diff --git a/src/utils/vutils.cpp b/src/utils/vutils.cpp index b19a0fc6..9820bf46 100644 --- a/src/utils/vutils.cpp +++ b/src/utils/vutils.cpp @@ -116,22 +116,6 @@ void VUtils::processStyle(QString &style, const QVector } } -bool VUtils::isMarkdown(const QString &p_fileName) -{ - const QVector mdPostfix({"md", "markdown", "mkd"}); - - QFileInfo info(p_fileName); - QString suffix = info.suffix(); - - for (int i = 0; i < mdPostfix.size(); ++i) { - if (suffix == mdPostfix[i]) { - return true; - } - } - - return false; -} - QString VUtils::fileNameFromPath(const QString &p_path) { if (p_path.isEmpty()) { @@ -457,3 +441,23 @@ void VUtils::sleepWait(int p_milliseconds) QCoreApplication::processEvents(); } } + +DocType VUtils::docTypeFromName(const QString &p_name) +{ + static QMap> suffixes; + + if (suffixes.isEmpty()) { + suffixes[(int)DocType::Markdown] = {"md", "markdown", "mkd"}; + suffixes[(int)DocType::List] = {"ls", "list"}; + suffixes[(int)DocType::Container] = {"co", "container", "con"}; + } + + QString suf = QFileInfo(p_name).suffix().toLower(); + for (auto it = suffixes.begin(); it != suffixes.end(); ++it) { + if (it.value().contains(suf)) { + return DocType(it.key()); + } + } + + return DocType::Html; +} diff --git a/src/utils/vutils.h b/src/utils/vutils.h index fb36a5e8..bca41ba3 100644 --- a/src/utils/vutils.h +++ b/src/utils/vutils.h @@ -53,7 +53,6 @@ public: static QString generateCopiedFileName(const QString &p_dirPath, const QString &p_fileName); static QString generateCopiedDirName(const QString &p_parentDirPath, const QString &p_dirName); static void processStyle(QString &style, const QVector > &varMap); - static bool isMarkdown(const QString &p_fileName); // Return the last directory name of @p_path. static inline QString directoryNameFromPath(const QString& p_path); @@ -94,6 +93,9 @@ public: static void sleepWait(int p_milliseconds); + // Return the DocType according to suffix. + static DocType docTypeFromName(const QString &p_name); + // Regular expression for image link. // ![image title]( http://github.com/tamlok/vnote.jpg "alt \" text" ) // Captured texts (need to be trimmed): diff --git a/src/vconstants.h b/src/vconstants.h index 2ad63ed8..f8ad17b9 100644 --- a/src/vconstants.h +++ b/src/vconstants.h @@ -3,8 +3,12 @@ // Html: rich text file; // Markdown: Markdown text file; -enum class DocType { Html, Markdown }; +// List: Infinite list file like WorkFlowy; +// Container: a composite file containing multiple files; +enum class DocType { Html, Markdown, List, Container, Invalid }; +// Normal: note file managed by VNote; +// Orphan: external file; enum class FileType { Normal, Orphan }; enum class ClipboardOpType { Invalid, CopyFile, CopyDir }; diff --git a/src/vdirectory.cpp b/src/vdirectory.cpp index 5f95a0e9..2351b772 100644 --- a/src/vdirectory.cpp +++ b/src/vdirectory.cpp @@ -490,7 +490,7 @@ VFile *VDirectory::copyFile(VDirectory *p_destDir, const QString &p_destName, VDirectory *srcDir = p_srcFile->getDirectory(); DocType docType = p_srcFile->getDocType(); - DocType newDocType = VUtils::isMarkdown(destPath) ? DocType::Markdown : DocType::Html; + DocType newDocType = VUtils::docTypeFromName(destPath); QVector images; if (docType == DocType::Markdown) { diff --git a/src/vedittab.cpp b/src/vedittab.cpp index d07bcd11..6f16c6d1 100644 --- a/src/vedittab.cpp +++ b/src/vedittab.cpp @@ -93,6 +93,7 @@ void VEditTab::setupUI() addWidget(m_textEditor); webPreviewer = NULL; break; + default: qWarning() << "unknown doc type" << int(m_file->getDocType()); Q_ASSERT(false); @@ -122,6 +123,7 @@ void VEditTab::showFileReadMode() case DocType::Html: m_textEditor->setReadOnly(true); break; + case DocType::Markdown: if (mdConverterType == MarkdownConverterType::Hoedown) { previewByConverter(); @@ -133,6 +135,7 @@ void VEditTab::showFileReadMode() clearSearchedWordHighlight(); scrollPreviewToHeader(outlineIndex); break; + default: qWarning() << "unknown doc type" << int(m_file->getDocType()); Q_ASSERT(false); diff --git a/src/vfile.cpp b/src/vfile.cpp index 31b5cafe..197ab5b9 100644 --- a/src/vfile.cpp +++ b/src/vfile.cpp @@ -9,7 +9,7 @@ VFile::VFile(const QString &p_name, QObject *p_parent, FileType p_type, bool p_modifiable) : QObject(p_parent), m_name(p_name), m_opened(false), m_modified(false), - m_docType(VUtils::isMarkdown(p_name) ? DocType::Markdown : DocType::Html), + m_docType(VUtils::docTypeFromName(p_name)), m_type(p_type), m_modifiable(p_modifiable) { } @@ -25,7 +25,6 @@ bool VFile::open() return true; } Q_ASSERT(m_content.isEmpty()); - Q_ASSERT(m_docType == (VUtils::isMarkdown(m_name) ? DocType::Markdown : DocType::Html)); QString path = retrivePath(); qDebug() << "path" << path; m_content = VUtils::readFileFromDisk(path); @@ -116,7 +115,7 @@ void VFile::deleteLocalImages() void VFile::setName(const QString &p_name) { m_name = p_name; - DocType newType = VUtils::isMarkdown(p_name) ? DocType::Markdown : DocType::Html; + DocType newType = VUtils::docTypeFromName(p_name); if (newType != m_docType) { qWarning() << "setName() change the DocType. A convertion should be followed"; } diff --git a/src/vfilelist.cpp b/src/vfilelist.cpp index 6031f960..334aee8f 100644 --- a/src/vfilelist.cpp +++ b/src/vfilelist.cpp @@ -462,9 +462,10 @@ bool VFileList::copyFile(VDirectory *p_destDir, const QString &p_destName, VFile if (srcPath == destPath) { return true; } + // If change the file type, we need to close it first DocType docType = p_file->getDocType(); - DocType newDocType = VUtils::isMarkdown(destPath) ? DocType::Markdown : DocType::Html; + DocType newDocType = VUtils::docTypeFromName(destPath); if (docType != newDocType) { if (editArea->isFileOpened(p_file)) { int ret = VUtils::showMessage(QMessageBox::Warning, tr("Warning"), diff --git a/src/vmdedit.cpp b/src/vmdedit.cpp index 837e6bd1..6eb7c495 100644 --- a/src/vmdedit.cpp +++ b/src/vmdedit.cpp @@ -17,7 +17,7 @@ VMdEdit::VMdEdit(VFile *p_file, VDocument *p_vdoc, MarkdownConverterType p_type, QWidget *p_parent) : VEdit(p_file, p_parent), m_mdHighlighter(NULL) { - Q_ASSERT(p_file->getDocType() == DocType::Markdown); + V_ASSERT(p_file->getDocType() == DocType::Markdown); setAcceptRichText(false); m_mdHighlighter = new HGMarkdownHighlighter(vconfig.getMdHighlightingStyles(), diff --git a/src/vorphanfile.cpp b/src/vorphanfile.cpp index ba6363aa..7a13aa46 100644 --- a/src/vorphanfile.cpp +++ b/src/vorphanfile.cpp @@ -18,7 +18,6 @@ bool VOrphanFile::open() return true; } Q_ASSERT(m_content.isEmpty()); - Q_ASSERT(m_docType == (VUtils::isMarkdown(m_name) ? DocType::Markdown : DocType::Html)); Q_ASSERT(QFileInfo::exists(m_path)); m_content = VUtils::readFileFromDisk(m_path); m_modified = false;