mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
add basic logics for handling html file
Add edit/read/save logics for html file. Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
parent
504e3323cf
commit
3f8c87a325
125
vedit.cpp
125
vedit.cpp
@ -1,20 +1,52 @@
|
|||||||
#include <QtWidgets>
|
#include <QtWidgets>
|
||||||
#include "vedit.h"
|
#include "vedit.h"
|
||||||
|
|
||||||
VEdit::VEdit(const QString &path, const QString &name,
|
VEdit::VEdit(const QString &path, const QString &name, bool modifiable,
|
||||||
QWidget *parent) : QTextEdit(parent), path(path), name(name)
|
QWidget *parent)
|
||||||
|
: QTextEdit(parent), path(path), name(name), modifiable(modifiable)
|
||||||
{
|
{
|
||||||
fileText = readFile(QDir(path).filePath(name));
|
docType = isMarkdown() ? DocType::Markdown : DocType::Html;
|
||||||
showTextReadMode();
|
fileText = readFileFromDisk(QDir(path).filePath(name));
|
||||||
|
showFileReadMode();
|
||||||
|
fileLoaded = true;
|
||||||
|
qDebug() << "VEdit:" << name << (docType == DocType::Markdown ? "Markdown" : "Html");
|
||||||
}
|
}
|
||||||
|
|
||||||
void VEdit::showTextReadMode()
|
void VEdit::showFileReadMode()
|
||||||
{
|
{
|
||||||
setText(fileText);
|
|
||||||
setReadOnly(true);
|
setReadOnly(true);
|
||||||
|
switch (docType) {
|
||||||
|
case DocType::Html:
|
||||||
|
if (!fileLoaded) {
|
||||||
|
setHtml(fileText);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case DocType::Markdown:
|
||||||
|
setText(fileText);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
qWarning() << "error: unknown doc type" << int(docType);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QString VEdit::readFile(const QString &filePath)
|
void VEdit::showFileEditMode()
|
||||||
|
{
|
||||||
|
setReadOnly(false);
|
||||||
|
switch (docType) {
|
||||||
|
case DocType::Html:
|
||||||
|
if (!fileLoaded) {
|
||||||
|
setHtml(fileText);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case DocType::Markdown:
|
||||||
|
setText(fileText);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
qWarning() << "error: unknown doc type" << int(docType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QString VEdit::readFileFromDisk(const QString &filePath)
|
||||||
{
|
{
|
||||||
QFile file(filePath);
|
QFile file(filePath);
|
||||||
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||||
@ -23,10 +55,11 @@ QString VEdit::readFile(const QString &filePath)
|
|||||||
}
|
}
|
||||||
QString fileText(file.readAll());
|
QString fileText(file.readAll());
|
||||||
file.close();
|
file.close();
|
||||||
|
qDebug() << "read file content:" << filePath;
|
||||||
return fileText;
|
return fileText;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool VEdit::writeFile(const QString &filePath, const QString &text)
|
bool VEdit::writeFileToDisk(const QString &filePath, const QString &text)
|
||||||
{
|
{
|
||||||
QFile file(filePath);
|
QFile file(filePath);
|
||||||
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||||
@ -36,10 +69,86 @@ bool VEdit::writeFile(const QString &filePath, const QString &text)
|
|||||||
QTextStream stream(&file);
|
QTextStream stream(&file);
|
||||||
stream << text;
|
stream << text;
|
||||||
file.close();
|
file.close();
|
||||||
|
qDebug() << "write file content:" << filePath;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool VEdit::requestClose()
|
bool VEdit::requestClose()
|
||||||
{
|
{
|
||||||
|
readFile();
|
||||||
|
return isReadOnly();
|
||||||
|
}
|
||||||
|
|
||||||
|
void VEdit::editFile()
|
||||||
|
{
|
||||||
|
if (!modifiable || !isReadOnly()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
showFileEditMode();
|
||||||
|
}
|
||||||
|
|
||||||
|
void VEdit::readFile()
|
||||||
|
{
|
||||||
|
if (isReadOnly()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (document()->isModified()) {
|
||||||
|
QMessageBox msgBox(QMessageBox::Information, tr("Exit edit mode"),
|
||||||
|
QString("Note has been changed. Do you want to save it before exit?"));
|
||||||
|
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::No | QMessageBox::Cancel);
|
||||||
|
msgBox.setDefaultButton(QMessageBox::Save);
|
||||||
|
int ret = msgBox.exec();
|
||||||
|
switch (ret) {
|
||||||
|
case QMessageBox::Save:
|
||||||
|
saveFile();
|
||||||
|
// Fall through
|
||||||
|
case QMessageBox::No:
|
||||||
|
showFileReadMode();
|
||||||
|
break;
|
||||||
|
case QMessageBox::Cancel:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
qWarning() << "error: wrong return value from QMessageBox:" << ret;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
showFileReadMode();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void VEdit::saveFile()
|
||||||
|
{
|
||||||
|
if (!modifiable || !document()->isModified()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (docType) {
|
||||||
|
case DocType::Html:
|
||||||
|
fileText = toHtml();
|
||||||
|
writeFileToDisk(QDir(path).filePath(name), fileText);
|
||||||
|
break;
|
||||||
|
case DocType::Markdown:
|
||||||
|
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
qWarning() << "error: unknown doc type" << int(docType);
|
||||||
|
}
|
||||||
|
|
||||||
|
document()->setModified(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool VEdit::isMarkdown()
|
||||||
|
{
|
||||||
|
const QVector<QString> mdPostfix({"md", "markdown", "mkd"});
|
||||||
|
|
||||||
|
QStringList list = name.split('.', QString::SkipEmptyParts);
|
||||||
|
if (list.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const QString &postfix = list.last();
|
||||||
|
for (int i = 0; i < mdPostfix.size(); ++i) {
|
||||||
|
if (postfix == mdPostfix[i]) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
20
vedit.h
20
vedit.h
@ -8,9 +8,15 @@ class VEdit : public QTextEdit
|
|||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit VEdit(const QString &path, const QString &name,
|
explicit VEdit(const QString &path, const QString &name, bool modifiable = false,
|
||||||
QWidget *parent = 0);
|
QWidget *parent = 0);
|
||||||
bool requestClose();
|
bool requestClose();
|
||||||
|
// Enter edit mode
|
||||||
|
void editFile();
|
||||||
|
// Enter read mode
|
||||||
|
void readFile();
|
||||||
|
// Save file
|
||||||
|
void saveFile();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
|
|
||||||
@ -18,13 +24,19 @@ public slots:
|
|||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QString readFile(const QString &filePath);
|
enum class DocType { Html, Markdown };
|
||||||
bool writeFile(const QString &filePath, const QString &text);
|
QString readFileFromDisk(const QString &filePath);
|
||||||
void showTextReadMode();
|
bool writeFileToDisk(const QString &filePath, const QString &text);
|
||||||
|
void showFileReadMode();
|
||||||
|
void showFileEditMode();
|
||||||
|
bool isMarkdown();
|
||||||
|
|
||||||
QString path;
|
QString path;
|
||||||
QString name;
|
QString name;
|
||||||
QString fileText;
|
QString fileText;
|
||||||
|
DocType docType;
|
||||||
|
bool modifiable;
|
||||||
|
bool fileLoaded;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // VEDIT_H
|
#endif // VEDIT_H
|
||||||
|
@ -9,7 +9,8 @@ VMainWindow::VMainWindow(QWidget *parent)
|
|||||||
: QMainWindow(parent)
|
: QMainWindow(parent)
|
||||||
{
|
{
|
||||||
setupUI();
|
setupUI();
|
||||||
|
initActions();
|
||||||
|
initToolBar();
|
||||||
vnote = new VNote();
|
vnote = new VNote();
|
||||||
vnote->readGlobalConfig();
|
vnote->readGlobalConfig();
|
||||||
updateNotebookComboBox();
|
updateNotebookComboBox();
|
||||||
@ -74,6 +75,33 @@ void VMainWindow::setupUI()
|
|||||||
statusBar();
|
statusBar();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void VMainWindow::initActions()
|
||||||
|
{
|
||||||
|
editNoteAct = new QAction(tr("&Edit"), this);
|
||||||
|
editNoteAct->setStatusTip(tr("Edit current note"));
|
||||||
|
connect(editNoteAct, &QAction::triggered,
|
||||||
|
tabs, &VTabWidget::editFile);
|
||||||
|
|
||||||
|
readNoteAct = new QAction(tr("&Read"), this);
|
||||||
|
readNoteAct->setStatusTip(tr("Open current note in read mode"));
|
||||||
|
connect(readNoteAct, &QAction::triggered,
|
||||||
|
tabs, &VTabWidget::readFile);
|
||||||
|
|
||||||
|
saveNoteAct = new QAction(tr("&Save"), this);
|
||||||
|
saveNoteAct->setStatusTip(tr("Save current note"));
|
||||||
|
connect(saveNoteAct, &QAction::triggered,
|
||||||
|
tabs, &VTabWidget::saveFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
void VMainWindow::initToolBar()
|
||||||
|
{
|
||||||
|
fileToolBar = addToolBar(tr("Note"));
|
||||||
|
fileToolBar->setMovable(false);
|
||||||
|
fileToolBar->addAction(editNoteAct);
|
||||||
|
fileToolBar->addAction(readNoteAct);
|
||||||
|
fileToolBar->addAction(saveNoteAct);
|
||||||
|
}
|
||||||
|
|
||||||
void VMainWindow::updateNotebookComboBox()
|
void VMainWindow::updateNotebookComboBox()
|
||||||
{
|
{
|
||||||
const QVector<VNotebook> ¬ebooks = vnote->getNotebooks();
|
const QVector<VNotebook> ¬ebooks = vnote->getNotebooks();
|
||||||
|
@ -9,9 +9,11 @@ class VDirectoryTree;
|
|||||||
class QSplitter;
|
class QSplitter;
|
||||||
class QListWidget;
|
class QListWidget;
|
||||||
class QTabWidget;
|
class QTabWidget;
|
||||||
|
class QToolBar;
|
||||||
class VNote;
|
class VNote;
|
||||||
class VFileList;
|
class VFileList;
|
||||||
class VTabWidget;
|
class VTabWidget;
|
||||||
|
class QAction;
|
||||||
|
|
||||||
class VMainWindow : public QMainWindow
|
class VMainWindow : public QMainWindow
|
||||||
{
|
{
|
||||||
@ -32,6 +34,8 @@ private:
|
|||||||
void setupUI();
|
void setupUI();
|
||||||
// Update notebookComboBox according to vnote
|
// Update notebookComboBox according to vnote
|
||||||
void updateNotebookComboBox();
|
void updateNotebookComboBox();
|
||||||
|
void initActions();
|
||||||
|
void initToolBar();
|
||||||
|
|
||||||
QLabel *notebookLabel;
|
QLabel *notebookLabel;
|
||||||
QComboBox *notebookComboBox;
|
QComboBox *notebookComboBox;
|
||||||
@ -40,6 +44,12 @@ private:
|
|||||||
VTabWidget *tabs;
|
VTabWidget *tabs;
|
||||||
QSplitter *mainSplitter;
|
QSplitter *mainSplitter;
|
||||||
VNote *vnote;
|
VNote *vnote;
|
||||||
|
QToolBar *fileToolBar;
|
||||||
|
|
||||||
|
// Actions
|
||||||
|
QAction *editNoteAct;
|
||||||
|
QAction *saveNoteAct;
|
||||||
|
QAction *readNoteAct;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // VMAINWINDOW_H
|
#endif // VMAINWINDOW_H
|
||||||
|
@ -15,7 +15,7 @@ VTabWidget::VTabWidget(const QString &welcomePageUrl, QWidget *parent)
|
|||||||
|
|
||||||
void VTabWidget::openWelcomePage()
|
void VTabWidget::openWelcomePage()
|
||||||
{
|
{
|
||||||
int idx = openFileInTab(welcomePageUrl, "");
|
int idx = openFileInTab(welcomePageUrl, "", false);
|
||||||
setTabText(idx, "Welcome to VNote");
|
setTabText(idx, "Welcome to VNote");
|
||||||
setTabToolTip(idx, "VNote");
|
setTabToolTip(idx, "VNote");
|
||||||
}
|
}
|
||||||
@ -52,14 +52,14 @@ void VTabWidget::openFile(QJsonObject fileJson)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
idx = openFileInTab(path, name);
|
idx = openFileInTab(path, name, true);
|
||||||
|
|
||||||
setCurrentIndex(idx);
|
setCurrentIndex(idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
int VTabWidget::openFileInTab(const QString &path, const QString &name)
|
int VTabWidget::openFileInTab(const QString &path, const QString &name, bool modifiable)
|
||||||
{
|
{
|
||||||
VEdit *edit = new VEdit(path, name);
|
VEdit *edit = new VEdit(path, name, modifiable);
|
||||||
QJsonObject tabJson;
|
QJsonObject tabJson;
|
||||||
tabJson["path"] = path;
|
tabJson["path"] = path;
|
||||||
tabJson["name"] = name;
|
tabJson["name"] = name;
|
||||||
@ -86,9 +86,31 @@ void VTabWidget::handleTabCloseRequest(int index)
|
|||||||
{
|
{
|
||||||
qDebug() << "request closing tab" << index;
|
qDebug() << "request closing tab" << index;
|
||||||
VEdit *edit = dynamic_cast<VEdit *>(widget(index));
|
VEdit *edit = dynamic_cast<VEdit *>(widget(index));
|
||||||
|
Q_ASSERT(edit);
|
||||||
bool ok = edit->requestClose();
|
bool ok = edit->requestClose();
|
||||||
if (ok) {
|
if (ok) {
|
||||||
removeTab(index);
|
removeTab(index);
|
||||||
delete edit;
|
delete edit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void VTabWidget::readFile()
|
||||||
|
{
|
||||||
|
VEdit *edit = dynamic_cast<VEdit *>(currentWidget());
|
||||||
|
Q_ASSERT(edit);
|
||||||
|
edit->readFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
void VTabWidget::editFile()
|
||||||
|
{
|
||||||
|
VEdit *edit = dynamic_cast<VEdit *>(currentWidget());
|
||||||
|
Q_ASSERT(edit);
|
||||||
|
edit->editFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
void VTabWidget::saveFile()
|
||||||
|
{
|
||||||
|
VEdit *edit = dynamic_cast<VEdit *>(currentWidget());
|
||||||
|
Q_ASSERT(edit);
|
||||||
|
edit->saveFile();
|
||||||
|
}
|
||||||
|
@ -15,6 +15,9 @@ signals:
|
|||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void openFile(QJsonObject fileJson);
|
void openFile(QJsonObject fileJson);
|
||||||
|
void editFile();
|
||||||
|
void saveFile();
|
||||||
|
void readFile();
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void handleTabCloseRequest(int index);
|
void handleTabCloseRequest(int index);
|
||||||
@ -24,7 +27,7 @@ private:
|
|||||||
int insertTabWithData(int index, QWidget *page, const QString &label, const QJsonObject &tabData);
|
int insertTabWithData(int index, QWidget *page, const QString &label, const QJsonObject &tabData);
|
||||||
int appendTabWithData(QWidget *page, const QString &label, const QJsonObject &tabData);
|
int appendTabWithData(QWidget *page, const QString &label, const QJsonObject &tabData);
|
||||||
int findTabByFile(const QString &path, const QString &name);
|
int findTabByFile(const QString &path, const QString &name);
|
||||||
int openFileInTab(const QString &path, const QString &name);
|
int openFileInTab(const QString &path, const QString &name, bool modifiable);
|
||||||
QString welcomePageUrl;
|
QString welcomePageUrl;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user