support edit/read mode

This commit is contained in:
Le Tan 2022-08-19 21:30:09 +08:00
parent 3c64b86297
commit 08c597513c
7 changed files with 135 additions and 8 deletions

View File

@ -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";
}
}

View File

@ -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

View File

@ -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" : [
{

View File

@ -47,6 +47,20 @@ void NoteManagementPage::setupUI()
connect(m_lineEndingComboBox, QOverload<int>::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<int>::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<int>(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<LineEndingPolicy>(ending));
}
{
auto mode = m_defaultOpenModeComboBox->currentData().toInt();
coreConfig.setDefaultOpenMode(static_cast<ViewWindowMode>(mode));
}
return true;
}

View File

@ -27,6 +27,8 @@ namespace vnotex
QCheckBox *m_perNotebookHistoryCheckBox = nullptr;
QComboBox *m_lineEndingComboBox = nullptr;
QComboBox *m_defaultOpenModeComboBox = nullptr;
};
}

View File

@ -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<FileOpenParameters>::create());
auto paras = QSharedPointer<FileOpenParameters>::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<FileOpenParameters>::create());
auto paras = QSharedPointer<FileOpenParameters>::create();
paras->m_mode = defaultMode;
emit nodeActivated(importedNode.data(), paras);
}
return;
}
// Just open it.
emit fileActivated(p_data.getExternalNode()->fetchAbsolutePath(), QSharedPointer<FileOpenParameters>::create());
auto paras = QSharedPointer<FileOpenParameters>::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<FileOpenParameters>::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<FileOpenParameters>::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();

View File

@ -160,6 +160,8 @@ namespace vnotex
ReloadIndex,
ImportToConfig,
Open,
Edit,
Read,
ExpandAll,
PinToQuickAccess,
Tag