diff --git a/src/core/coreconfig.cpp b/src/core/coreconfig.cpp index cadaa8dd..132f6c29 100644 --- a/src/core/coreconfig.cpp +++ b/src/core/coreconfig.cpp @@ -82,12 +82,16 @@ void CoreConfig::init(const QJsonObject &p_app, m_perNotebookHistoryEnabled = READBOOL(QStringLiteral("per_notebook_history")); - { auto lineEnding = READSTR(QStringLiteral("line_ending")); m_lineEnding = stringToLineEndingPolicy(lineEnding); } + { + auto mode = READSTR(QStringLiteral("default_open_mode")); + m_defaultOpenMode = stringToViewWindowMode(mode); + } + loadFileTypeSuffixes(appObj, userObj); loadUnitedEntry(appObj, userObj); @@ -109,6 +113,7 @@ QJsonObject CoreConfig::toJson() const obj[QStringLiteral("line_ending")] = lineEndingPolicyToString(m_lineEnding); obj[QStringLiteral("file_type_suffixes")] = saveFileTypeSuffixes(); obj[QStringLiteral("united_entry")] = saveUnitedEntry(); + obj[QStringLiteral("default_open_mode")] = viewWindowModeToString(m_defaultOpenMode); return obj; } @@ -362,3 +367,33 @@ void CoreConfig::setUnitedEntryAlias(const QJsonArray &p_alias) { updateConfig(m_unitedEntryAlias, p_alias, this); } + +ViewWindowMode CoreConfig::getDefaultOpenMode() const +{ + return m_defaultOpenMode; +} + +void CoreConfig::setDefaultOpenMode(ViewWindowMode p_mode) +{ + updateConfig(m_defaultOpenMode, p_mode, this); +} + +ViewWindowMode CoreConfig::stringToViewWindowMode(const QString &p_mode) +{ + if (p_mode == "edit") { + return ViewWindowMode::Edit; + } + + return ViewWindowMode::Read; +} + +QString CoreConfig::viewWindowModeToString(ViewWindowMode p_mode) +{ + switch (p_mode) { + case ViewWindowMode::Edit: + return "edit"; + + default: + return "read"; + } +} diff --git a/src/core/coreconfig.h b/src/core/coreconfig.h index ad15a85f..7e13f365 100644 --- a/src/core/coreconfig.h +++ b/src/core/coreconfig.h @@ -146,6 +146,9 @@ namespace vnotex const QJsonArray &getUnitedEntryAlias() const; void setUnitedEntryAlias(const QJsonArray &p_alias); + ViewWindowMode getDefaultOpenMode() const; + void setDefaultOpenMode(ViewWindowMode p_mode); + private: friend class MainConfig; @@ -163,6 +166,9 @@ namespace vnotex QJsonObject saveUnitedEntry() const; + static ViewWindowMode stringToViewWindowMode(const QString &p_mode); + static QString viewWindowModeToString(ViewWindowMode p_mode); + // Theme name. QString m_theme; @@ -200,6 +206,8 @@ namespace vnotex QJsonArray m_unitedEntryAlias; + ViewWindowMode m_defaultOpenMode = ViewWindowMode::Read; + static QStringList s_availableLocales; }; } // ns vnotex diff --git a/src/data/core/vnotex.json b/src/data/core/vnotex.json index 6c45a7dc..1926eefe 100644 --- a/src/data/core/vnotex.json +++ b/src/data/core/vnotex.json @@ -109,6 +109,8 @@ "per_notebook_history" : false, "//comment" : "Line ending policy for config files, platform/lf/crlf/cr", "line_ending" : "lf", + "//comment" : "read/edit", + "default_open_mode" : "read", "united_entry" : { "alias" : [ { diff --git a/src/widgets/dialogs/settings/notemanagementpage.cpp b/src/widgets/dialogs/settings/notemanagementpage.cpp index 6f96829a..4104efb4 100644 --- a/src/widgets/dialogs/settings/notemanagementpage.cpp +++ b/src/widgets/dialogs/settings/notemanagementpage.cpp @@ -47,6 +47,20 @@ void NoteManagementPage::setupUI() connect(m_lineEndingComboBox, QOverload::of(&QComboBox::currentIndexChanged), this, &NoteManagementPage::pageIsChanged); } + + { + m_defaultOpenModeComboBox = WidgetsFactory::createComboBox(this); + m_defaultOpenModeComboBox->setToolTip(tr("Default mode when opening notes")); + + m_defaultOpenModeComboBox->addItem(tr("Read"), (int)ViewWindowMode::Read); + m_defaultOpenModeComboBox->addItem(tr("Edit"), (int)ViewWindowMode::Edit); + + const QString label(tr("Default open mode:")); + mainLayout->addRow(label, m_defaultOpenModeComboBox); + addSearchItem(label, m_defaultOpenModeComboBox->toolTip(), m_defaultOpenModeComboBox); + connect(m_defaultOpenModeComboBox, QOverload::of(&QComboBox::currentIndexChanged), + this, &NoteManagementPage::pageIsChanged); + } } void NoteManagementPage::loadInternal() @@ -62,6 +76,14 @@ void NoteManagementPage::loadInternal() } m_lineEndingComboBox->setCurrentIndex(idx); } + + { + int idx = m_defaultOpenModeComboBox->findData(static_cast(coreConfig.getDefaultOpenMode())); + if (idx == -1) { + idx = 0; + } + m_defaultOpenModeComboBox->setCurrentIndex(idx); + } } bool NoteManagementPage::saveInternal() @@ -75,6 +97,11 @@ bool NoteManagementPage::saveInternal() coreConfig.setLineEndingPolicy(static_cast(ending)); } + { + auto mode = m_defaultOpenModeComboBox->currentData().toInt(); + coreConfig.setDefaultOpenMode(static_cast(mode)); + } + return true; } diff --git a/src/widgets/dialogs/settings/notemanagementpage.h b/src/widgets/dialogs/settings/notemanagementpage.h index b7afcb3b..7cf33e15 100644 --- a/src/widgets/dialogs/settings/notemanagementpage.h +++ b/src/widgets/dialogs/settings/notemanagementpage.h @@ -27,6 +27,8 @@ namespace vnotex QCheckBox *m_perNotebookHistoryCheckBox = nullptr; QComboBox *m_lineEndingComboBox = nullptr; + + QComboBox *m_defaultOpenModeComboBox = nullptr; }; } diff --git a/src/widgets/notebooknodeexplorer.cpp b/src/widgets/notebooknodeexplorer.cpp index 4cb7a8c3..babf13d6 100644 --- a/src/widgets/notebooknodeexplorer.cpp +++ b/src/widgets/notebooknodeexplorer.cpp @@ -291,23 +291,32 @@ void NotebookNodeExplorer::activateItemNode(const NodeData &p_data) return; } + const auto &coreConfig = ConfigMgr::getInst().getCoreConfig(); + auto defaultMode = coreConfig.getDefaultOpenMode(); + if (p_data.isNode()) { if (checkInvalidNode(p_data.getNode())) { return; } - emit nodeActivated(p_data.getNode(), QSharedPointer::create()); + auto paras = QSharedPointer::create(); + paras->m_mode = defaultMode; + emit nodeActivated(p_data.getNode(), paras); } else if (p_data.isExternalNode()) { // Import to config first. if (m_autoImportExternalFiles) { auto importedNode = importToIndex(p_data.getExternalNode()); if (importedNode) { - emit nodeActivated(importedNode.data(), QSharedPointer::create()); + auto paras = QSharedPointer::create(); + paras->m_mode = defaultMode; + emit nodeActivated(importedNode.data(), paras); } return; } // Just open it. - emit fileActivated(p_data.getExternalNode()->fetchAbsolutePath(), QSharedPointer::create()); + auto paras = QSharedPointer::create(); + paras->m_mode = defaultMode; + emit fileActivated(p_data.getExternalNode()->fetchAbsolutePath(), paras); } } @@ -946,7 +955,9 @@ void NotebookNodeExplorer::createContextMenuOnNode(QMenu *p_menu, const Node *p_ { const int selectedSize = p_master ? m_masterExplorer->selectedItems().size() : m_slaveExplorer->selectedItems().size(); - createAndAddAction(Action::Open, p_menu, p_master); + createAndAddAction(Action::Edit, p_menu, p_master); + + createAndAddAction(Action::Read, p_menu, p_master); addOpenWithMenu(p_menu, p_master); @@ -1009,7 +1020,9 @@ void NotebookNodeExplorer::createContextMenuOnExternalNode(QMenu *p_menu, const const int selectedSize = p_master ? m_masterExplorer->selectedItems().size() : m_slaveExplorer->selectedItems().size(); - createAndAddAction(Action::Open, p_menu, p_master); + createAndAddAction(Action::Edit, p_menu, p_master); + + createAndAddAction(Action::Read, p_menu, p_master); addOpenWithMenu(p_menu, p_master); @@ -1194,7 +1207,7 @@ QAction *NotebookNodeExplorer::createAction(Action p_act, QObject *p_parent, boo break; case Action::RemoveFromConfig: - act = new QAction(tr("&Remove From Index"), p_parent); + act = new QAction(tr("Remo&ve From Index"), p_parent); connect(act, &QAction::triggered, this, [this, p_master]() { removeSelectedNodesFromConfig(p_master); @@ -1247,6 +1260,8 @@ QAction *NotebookNodeExplorer::createAction(Action p_act, QObject *p_parent, boo break; case Action::Open: + // Use Edit and Read instead. + Q_ASSERT(false); act = new QAction(tr("&Open"), p_parent); connect(act, &QAction::triggered, this, [this, p_master]() { @@ -1271,8 +1286,44 @@ QAction *NotebookNodeExplorer::createAction(Action p_act, QObject *p_parent, boo }); break; + case Action::Edit: + Q_FALLTHROUGH(); + case Action::Read: + { + const bool isEdit = p_act == Action::Edit; + act = new QAction(isEdit ? tr("&Edit") : tr("&Read"), p_parent); + connect(act, &QAction::triggered, + this, [this, p_master, isEdit]() { + // Support nodes and external nodes. + // Do nothing for folders. + auto selectedNodes = p_master ? getMasterSelectedNodesAndExternalNodes() : getSlaveSelectedNodesAndExternalNodes(); + for (const auto &externalNode : selectedNodes.second) { + if (!externalNode->isFolder()) { + auto paras = QSharedPointer::create(); + paras->m_mode = isEdit ? ViewWindowMode::Edit : ViewWindowMode::Read; + paras->m_forceMode = true; + emit fileActivated(externalNode->fetchAbsolutePath(), paras); + } + } + + for (const auto &node : selectedNodes.first) { + if (checkInvalidNode(node)) { + continue; + } + + if (node->hasContent()) { + auto paras = QSharedPointer::create(); + paras->m_mode = isEdit ? ViewWindowMode::Edit : ViewWindowMode::Read; + paras->m_forceMode = true; + emit nodeActivated(node, paras); + } + } + }); + break; + } + case Action::ExpandAll: - act = new QAction(tr("&Expand All\t*"), p_parent); + act = new QAction(tr("E&xpand All\t*"), p_parent); connect(act, &QAction::triggered, this, [this]() { auto item = m_masterExplorer->currentItem(); diff --git a/src/widgets/notebooknodeexplorer.h b/src/widgets/notebooknodeexplorer.h index 1e6a211d..10f194fe 100644 --- a/src/widgets/notebooknodeexplorer.h +++ b/src/widgets/notebooknodeexplorer.h @@ -160,6 +160,8 @@ namespace vnotex ReloadIndex, ImportToConfig, Open, + Edit, + Read, ExpandAll, PinToQuickAccess, Tag