diff --git a/src/resources/icons/create_note_tb.svg b/src/resources/icons/create_note_tb.svg new file mode 100644 index 00000000..4b810d0e --- /dev/null +++ b/src/resources/icons/create_note_tb.svg @@ -0,0 +1,10 @@ + + + + + + diff --git a/src/resources/icons/discard_exit.svg b/src/resources/icons/discard_exit.svg new file mode 100644 index 00000000..686e46a6 --- /dev/null +++ b/src/resources/icons/discard_exit.svg @@ -0,0 +1,9 @@ + + + + + + diff --git a/src/resources/icons/edit_note.svg b/src/resources/icons/edit_note.svg new file mode 100644 index 00000000..21eb048e --- /dev/null +++ b/src/resources/icons/edit_note.svg @@ -0,0 +1,14 @@ + + + + + + + + + + + + diff --git a/src/resources/icons/save_exit.svg b/src/resources/icons/save_exit.svg new file mode 100644 index 00000000..3f14a4de --- /dev/null +++ b/src/resources/icons/save_exit.svg @@ -0,0 +1,10 @@ + + + + + + diff --git a/src/resources/icons/save_note.svg b/src/resources/icons/save_note.svg new file mode 100644 index 00000000..4635da5f --- /dev/null +++ b/src/resources/icons/save_note.svg @@ -0,0 +1,12 @@ + + + + + + + + + diff --git a/src/veditor.h b/src/veditor.h index 051423c6..45de4a13 100644 --- a/src/veditor.h +++ b/src/veditor.h @@ -8,9 +8,9 @@ #include "vdocument.h" #include "vmarkdownconverter.h" #include "vconfigmanager.h" +#include "vedit.h" class QTextBrowser; -class VEdit; class QWebEngineView; class VNote; @@ -27,6 +27,9 @@ public: // Save file bool saveFile(); + inline bool getIsEditMode() const; + inline bool isModified() const; + private: bool isMarkdown(const QString &name); void setupUI(); @@ -44,4 +47,14 @@ private: MarkdownConverterType mdConverterType; }; +inline bool VEditor::getIsEditMode() const +{ + return isEditMode; +} + +inline bool VEditor::isModified() const +{ + return textEditor->isModified(); +} + #endif // VEDITOR_H diff --git a/src/vmainwindow.cpp b/src/vmainwindow.cpp index b0ed9774..e455bc54 100644 --- a/src/vmainwindow.cpp +++ b/src/vmainwindow.cpp @@ -1,4 +1,5 @@ #include +#include #include "vmainwindow.h" #include "vdirectorytree.h" #include "vnote.h" @@ -81,16 +82,23 @@ void VMainWindow::setupUI() // Editor tab widget tabs = new VTabWidget(vnote); tabs->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); - tabs->setTabBarAutoHide(true); // Main Splitter mainSplitter = new QSplitter(); mainSplitter->addWidget(nbContainer); mainSplitter->addWidget(fileList); mainSplitter->addWidget(tabs); + QList sizes; + int sa = nbContainer->minimumSizeHint().width(); + int sb = fileList->minimumSizeHint().width(); + int sc = qMax(mainSplitter->sizeHint().width() - sa - sb, sa + sb); + sizes.append(sa); + sizes.append(sb); + sizes.append(sc); + mainSplitter->setSizes(sizes); mainSplitter->setStretchFactor(0, 1); mainSplitter->setStretchFactor(1, 1); - mainSplitter->setStretchFactor(2, 10); + mainSplitter->setStretchFactor(2, 200); // Signals connect(notebookComboBox, SIGNAL(currentIndexChanged(int)), this, @@ -111,6 +119,8 @@ void VMainWindow::setupUI() tabs, &VTabWidget::openFile); connect(vnote, &VNote::notebooksRenamed, tabs, &VTabWidget::handleNotebookRenamed); + connect(tabs, &VTabWidget::tabModeChanged, + this, &VMainWindow::updateToolbarFromTabChage); connect(newNotebookBtn, &QPushButton::clicked, this, &VMainWindow::onNewNotebookBtnClicked); @@ -146,17 +156,32 @@ void VMainWindow::setupUI() void VMainWindow::initActions() { - editNoteAct = new QAction(tr("&Edit"), this); + newNoteAct = new QAction(QIcon(":/resources/icons/create_note_tb.svg"), + tr("&New note"), this); + newNoteAct->setStatusTip(tr("Create a new note")); + connect(newNoteAct, &QAction::triggered, + fileList, &VFileList::newFile); + + editNoteAct = new QAction(QIcon(":/resources/icons/edit_note.svg"), + 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, + discardExitAct = new QAction(QIcon(":/resources/icons/discard_exit.svg"), + tr("Discard changes and exit"), this); + discardExitAct->setStatusTip(tr("Discard changes and exit edit mode")); + connect(discardExitAct, &QAction::triggered, tabs, &VTabWidget::readFile); - saveNoteAct = new QAction(tr("&Save"), this); + saveExitAct = new QAction(QIcon(":/resources/icons/save_exit.svg"), + tr("Save changes and exit"), this); + saveExitAct->setStatusTip(tr("Save changes and exit edit mode")); + connect(saveExitAct, &QAction::triggered, + tabs, &VTabWidget::saveAndReadFile); + + saveNoteAct = new QAction(QIcon(":/resources/icons/save_note.svg"), + tr("&Save"), this); saveNoteAct->setStatusTip(tr("Save current note")); saveNoteAct->setShortcut(QKeySequence::Save); connect(saveNoteAct, &QAction::triggered, @@ -223,9 +248,16 @@ void VMainWindow::initToolBar() { QToolBar *fileToolBar = addToolBar(tr("Note")); fileToolBar->setMovable(false); + fileToolBar->addAction(newNoteAct); fileToolBar->addAction(editNoteAct); - fileToolBar->addAction(readNoteAct); + fileToolBar->addAction(saveExitAct); + fileToolBar->addAction(discardExitAct); fileToolBar->addAction(saveNoteAct); + + editNoteAct->setVisible(false); + saveExitAct->setVisible(false); + discardExitAct->setVisible(false); + saveNoteAct->setVisible(false); } void VMainWindow::initMenuBar() @@ -588,3 +620,25 @@ void VMainWindow::setRenderBackgroundColor(QAction *action) vconfig.setCurRenderBackgroundColor(action->data().toString()); vnote->updateTemplate(); } + +void VMainWindow::updateToolbarFromTabChage(const QString ¬ebook, const QString &relativePath, + bool editMode, bool modifiable) +{ + if (notebook.isEmpty() || !modifiable) { + editNoteAct->setVisible(false); + saveExitAct->setVisible(false); + discardExitAct->setVisible(false); + saveNoteAct->setVisible(false); + return; + } else if (editMode) { + editNoteAct->setVisible(false); + saveExitAct->setVisible(true); + discardExitAct->setVisible(true); + saveNoteAct->setVisible(true); + } else { + editNoteAct->setVisible(true); + saveExitAct->setVisible(false); + discardExitAct->setVisible(false); + saveNoteAct->setVisible(false); + } +} diff --git a/src/vmainwindow.h b/src/vmainwindow.h index 7736d3d2..3c2f5ec8 100644 --- a/src/vmainwindow.h +++ b/src/vmainwindow.h @@ -44,6 +44,8 @@ private slots: void setTabStopWidth(QAction *action); void setEditorBackgroundColor(QAction *action); void setRenderBackgroundColor(QAction *action); + void updateToolbarFromTabChage(const QString ¬ebook, const QString &relativePath, + bool editMode, bool modifiable); signals: void curNotebookChanged(const QString ¬ebookName); @@ -77,9 +79,11 @@ private: VNote *vnote; // Actions + QAction *newNoteAct; QAction *editNoteAct; QAction *saveNoteAct; - QAction *readNoteAct; + QAction *saveExitAct; + QAction *discardExitAct; QAction *importNoteAct; QActionGroup *converterAct; QAction *markedAct; diff --git a/src/vnote.qrc b/src/vnote.qrc index 24f42089..18189661 100644 --- a/src/vnote.qrc +++ b/src/vnote.qrc @@ -45,5 +45,10 @@ resources/icons/create_note.png resources/icons/delete_note.png resources/icons/note_info.png + resources/icons/create_note_tb.svg + resources/icons/save_note.svg + resources/icons/edit_note.svg + resources/icons/save_exit.svg + resources/icons/discard_exit.svg diff --git a/src/vtabwidget.cpp b/src/vtabwidget.cpp index 5601439e..f713116b 100644 --- a/src/vtabwidget.cpp +++ b/src/vtabwidget.cpp @@ -12,10 +12,10 @@ VTabWidget::VTabWidget(VNote *vnote, QWidget *parent) { setTabsClosable(true); setMovable(true); - connect(tabBar(), &QTabBar::tabCloseRequested, + connect(this, &VTabWidget::tabCloseRequested, this, &VTabWidget::handleTabCloseRequest); - - openWelcomePage(); + connect(this, &VTabWidget::currentChanged, + this, &VTabWidget::onCurrentChanged); } void VTabWidget::openWelcomePage() @@ -33,6 +33,8 @@ int VTabWidget::insertTabWithData(int index, QWidget *page, QTabBar *tabs = tabBar(); tabs->setTabData(idx, tabData); + // Need to update again with tabData + onCurrentChanged(idx); return idx; } @@ -112,6 +114,7 @@ int VTabWidget::openFileInTab(const QString ¬ebook, const QString &relativePa QJsonObject tabJson; tabJson["notebook"] = notebook; tabJson["relative_path"] = relativePath; + tabJson["modifiable"] = modifiable; return appendTabWithData(editor, tabJson); } @@ -146,6 +149,14 @@ void VTabWidget::readFile() VEditor *editor = dynamic_cast(currentWidget()); Q_ASSERT(editor); editor->readFile(); + onCurrentChanged(currentIndex()); +} + +void VTabWidget::saveAndReadFile() +{ + saveFile(); + readFile(); + onCurrentChanged(currentIndex()); } void VTabWidget::editFile() @@ -153,6 +164,7 @@ void VTabWidget::editFile() VEditor *editor = dynamic_cast(currentWidget()); Q_ASSERT(editor); editor->editFile(); + onCurrentChanged(currentIndex()); } void VTabWidget::saveFile() @@ -175,3 +187,26 @@ void VTabWidget::handleNotebookRenamed(const QVector ¬ebooks, } } } + +void VTabWidget::onCurrentChanged(int index) +{ + if (index == -1) { + emit tabModeChanged("", "", false, false); + return; + } + + QJsonObject tabJson = tabBar()->tabData(index).toJsonObject(); + if (tabJson.isEmpty()) { + // Maybe the tab data has not been set yet + return; + } + + QString notebook = tabJson["notebook"].toString(); + QString relativePath = tabJson["relative_path"].toString(); + VEditor *editor = (VEditor *)widget(index); + bool editMode = editor->getIsEditMode(); + bool modifiable = tabJson["modifiable"].toBool(); + + emit tabModeChanged(notebook, relativePath, + editMode, modifiable); +} diff --git a/src/vtabwidget.h b/src/vtabwidget.h index bc3f970a..e082228a 100644 --- a/src/vtabwidget.h +++ b/src/vtabwidget.h @@ -17,6 +17,8 @@ public: explicit VTabWidget(VNote *vnote, QWidget *parent = 0); signals: + void tabModeChanged(const QString ¬ebook, const QString &relativePath, + bool editMode, bool modifiable); public slots: void openFile(QJsonObject fileJson); @@ -25,11 +27,13 @@ public slots: void editFile(); void saveFile(); void readFile(); + void saveAndReadFile(); void handleNotebookRenamed(const QVector ¬ebooks, const QString &oldName, const QString &newName); private slots: void handleTabCloseRequest(int index); + void onCurrentChanged(int index); private: void openWelcomePage();