Skip to content

Commit

Permalink
add shortcuts for tab navigation in ViewSplit
Browse files Browse the repository at this point in the history
  • Loading branch information
tamlok committed Jun 2, 2021
1 parent 79abddd commit 1f3b00e
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 11 deletions.
14 changes: 14 additions & 0 deletions src/core/coreconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ namespace vnotex
Quit,
FlashPage,
QuickAccess,
ActivateTab1,
ActivateTab2,
ActivateTab3,
ActivateTab4,
ActivateTab5,
ActivateTab6,
ActivateTab7,
ActivateTab8,
ActivateTab9,
AlternateTab,
ActivateNextTab,
ActivatePreviousTab,
MaxShortcut
};
Q_ENUM(Shortcut)
Expand Down Expand Up @@ -70,6 +82,8 @@ namespace vnotex
void setRecoverLastSessionOnStartEnabled(bool p_enabled);

private:
friend class MainConfig;

void loadShortcuts(const QJsonObject &p_app, const QJsonObject &p_user);

void loadNoteManagement(const QJsonObject &p_app, const QJsonObject &p_user);
Expand Down
6 changes: 6 additions & 0 deletions src/core/mainconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,10 @@ QString MainConfig::getVersion(const QJsonObject &p_jobj)
void MainConfig::doVersionSpecificOverride()
{
// In a new version, we may want to change one value by force.
m_coreConfig->m_shortcuts[CoreConfig::Shortcut::NavigationDock] = "Ctrl+G, A";
m_coreConfig->m_shortcuts[CoreConfig::Shortcut::OutlineDock] = "Ctrl+G, U";
m_coreConfig->m_shortcuts[CoreConfig::Shortcut::SearchDock] = "Ctrl+G, S";
m_coreConfig->m_shortcuts[CoreConfig::Shortcut::LocationListDock] = "Ctrl+G, L";
m_coreConfig->m_shortcuts[CoreConfig::Shortcut::NewWorkspace] = "Ctrl+G, M";
m_coreConfig->writeToSettings();
}
24 changes: 18 additions & 6 deletions src/data/core/vnotex.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
"Settings" : "Ctrl+Alt+P",
"NewNote" : "Ctrl+Alt+N",
"CloseTab" : "Ctrl+G, X",
"NavigationDock" : "Ctrl+G, 1",
"OutlineDock" : "Ctrl+G, 2",
"SearchDock" : "Ctrl+G, 3",
"LocationListDock" : "Ctrl+G, 4",
"NavigationDock" : "Ctrl+G, A",
"OutlineDock" : "Ctrl+G, U",
"SearchDock" : "Ctrl+G, S",
"LocationListDock" : "Ctrl+G, L",
"Search" : "Ctrl+Alt+F",
"NavigationMode" : "Ctrl+G, W",
"LocateNode" : "Ctrl+G, D",
Expand All @@ -27,11 +27,23 @@
"MaximizeSplit" : "Ctrl+G, Shift+\\",
"DistributeSplits" : "Ctrl+G, =",
"RemoveSplitAndWorkspace" : "Ctrl+G, R",
"NewWorkspace" : "Ctrl+G, N",
"NewWorkspace" : "Ctrl+G, M",
"Export" : "Ctrl+G, T",
"Quit" : "Ctrl+Q",
"FlashPage" : "Ctrl+Alt+L",
"QuickAccess" : "Ctrl+Alt+I"
"QuickAccess" : "Ctrl+Alt+I",
"ActivateTab1" : "Ctrl+G, 1",
"ActivateTab2" : "Ctrl+G, 2",
"ActivateTab3" : "Ctrl+G, 3",
"ActivateTab4" : "Ctrl+G, 4",
"ActivateTab5" : "Ctrl+G, 5",
"ActivateTab6" : "Ctrl+G, 6",
"ActivateTab7" : "Ctrl+G, 7",
"ActivateTab8" : "Ctrl+G, 8",
"ActivateTab9" : "Ctrl+G, 9",
"AlternateTab" : "Ctrl+G, 0",
"ActivateNextTab" : "Ctrl+G, N",
"ActivatePreviousTab" : "Ctrl+G, P"
},
"toolbar_icon_size" : 16,
"note_management" : {
Expand Down
172 changes: 171 additions & 1 deletion src/widgets/viewsplit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ void ViewSplit::focusCurrentViewWindow()
} else {
setFocus();
}

m_lastViewWindow = m_currentViewWindow;
m_currentViewWindow = win;
}

void ViewSplit::setupCornerWidget()
Expand Down Expand Up @@ -407,7 +410,7 @@ void ViewSplit::updateWindowList(QMenu *p_menu)
int cnt = getViewWindowCount();
if (cnt == 0) {
// Add a dummy entry.
auto act = p_menu->addAction(tr("No window to show here"));
auto act = p_menu->addAction(tr("No Window To Show"));
act->setEnabled(false);
return;
}
Expand Down Expand Up @@ -742,6 +745,136 @@ void ViewSplit::setupShortcuts()
});
}
}

// ActivateTab1.
{
auto shortcut = WidgetUtils::createShortcut(coreConfig.getShortcut(CoreConfig::ActivateTab1), this, Qt::WidgetWithChildrenShortcut);
if (shortcut) {
connect(shortcut, &QShortcut::activated,
this, [this]() {
setCurrentViewWindow(0);
});
}
}

// ActivateTab2.
{
auto shortcut = WidgetUtils::createShortcut(coreConfig.getShortcut(CoreConfig::ActivateTab2), this, Qt::WidgetWithChildrenShortcut);
if (shortcut) {
connect(shortcut, &QShortcut::activated,
this, [this]() {
setCurrentViewWindow(1);
});
}
}

// ActivateTab3.
{
auto shortcut = WidgetUtils::createShortcut(coreConfig.getShortcut(CoreConfig::ActivateTab3), this, Qt::WidgetWithChildrenShortcut);
if (shortcut) {
connect(shortcut, &QShortcut::activated,
this, [this]() {
setCurrentViewWindow(2);
});
}
}

// ActivateTab4.
{
auto shortcut = WidgetUtils::createShortcut(coreConfig.getShortcut(CoreConfig::ActivateTab4), this, Qt::WidgetWithChildrenShortcut);
if (shortcut) {
connect(shortcut, &QShortcut::activated,
this, [this]() {
setCurrentViewWindow(3);
});
}
}

// ActivateTab5.
{
auto shortcut = WidgetUtils::createShortcut(coreConfig.getShortcut(CoreConfig::ActivateTab5), this, Qt::WidgetWithChildrenShortcut);
if (shortcut) {
connect(shortcut, &QShortcut::activated,
this, [this]() {
setCurrentViewWindow(4);
});
}
}

// ActivateTab6.
{
auto shortcut = WidgetUtils::createShortcut(coreConfig.getShortcut(CoreConfig::ActivateTab6), this, Qt::WidgetWithChildrenShortcut);
if (shortcut) {
connect(shortcut, &QShortcut::activated,
this, [this]() {
setCurrentViewWindow(5);
});
}
}

// ActivateTab7.
{
auto shortcut = WidgetUtils::createShortcut(coreConfig.getShortcut(CoreConfig::ActivateTab7), this, Qt::WidgetWithChildrenShortcut);
if (shortcut) {
connect(shortcut, &QShortcut::activated,
this, [this]() {
setCurrentViewWindow(6);
});
}
}

// ActivateTab8.
{
auto shortcut = WidgetUtils::createShortcut(coreConfig.getShortcut(CoreConfig::ActivateTab8), this, Qt::WidgetWithChildrenShortcut);
if (shortcut) {
connect(shortcut, &QShortcut::activated,
this, [this]() {
setCurrentViewWindow(7);
});
}
}

// ActivateTab9.
{
auto shortcut = WidgetUtils::createShortcut(coreConfig.getShortcut(CoreConfig::ActivateTab9), this, Qt::WidgetWithChildrenShortcut);
if (shortcut) {
connect(shortcut, &QShortcut::activated,
this, [this]() {
setCurrentViewWindow(8);
});
}
}

// AlternateTab.
{
auto shortcut = WidgetUtils::createShortcut(coreConfig.getShortcut(CoreConfig::AlternateTab), this, Qt::WidgetWithChildrenShortcut);
if (shortcut) {
connect(shortcut, &QShortcut::activated,
this, &ViewSplit::alternateTab);
}
}

// ActivateNextTab.
{
auto shortcut = WidgetUtils::createShortcut(coreConfig.getShortcut(CoreConfig::ActivateNextTab), this, Qt::WidgetWithChildrenShortcut);
if (shortcut) {
connect(shortcut, &QShortcut::activated,
this, [this]() {
activateNextTab(false);
});
}
}

// ActivatePreviousTab.
{
auto shortcut = WidgetUtils::createShortcut(coreConfig.getShortcut(CoreConfig::ActivatePreviousTab), this, Qt::WidgetWithChildrenShortcut);
if (shortcut) {
connect(shortcut, &QShortcut::activated,
this, [this]() {
activateNextTab(true);
});
}
}
}

void ViewSplit::focus()
Expand All @@ -759,3 +892,40 @@ void ViewSplit::setCurrentViewWindow(int p_idx)
auto win = getViewWindow(p_idx);
setCurrentViewWindow(win);
}

void ViewSplit::alternateTab()
{
if (!m_lastViewWindow) {
return;
}

// It is fine even when m_lastViewWindow is a wild pointer. The implementation will just
// compare its value without dereferencing it.
if (-1 != indexOf(m_lastViewWindow)) {
setCurrentViewWindow(m_lastViewWindow);
} else {
m_lastViewWindow = nullptr;
}
}

void ViewSplit::activateNextTab(bool p_backward)
{
int idx = currentIndex();
if (idx == -1 || count() == 1) {
return;
}

if (p_backward) {
--idx;
if (idx < 0) {
idx = count() - 1;
}
} else {
++idx;
if (idx >= count()) {
idx = 0;
}
}

setCurrentViewWindow(idx);
}
8 changes: 8 additions & 0 deletions src/widgets/viewsplit.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ namespace vnotex

void focusCurrentViewWindow();

void alternateTab();

void activateNextTab(bool p_backward);

ID m_id = 0;

const QVector<QSharedPointer<ViewWorkspace>> &m_allWorkspaces;
Expand All @@ -143,6 +147,10 @@ namespace vnotex

QActionGroup *m_workspaceActionGroup = nullptr;

// Used for AlternateTab.
ViewWindow *m_currentViewWindow = nullptr;
ViewWindow *m_lastViewWindow = nullptr;

static QIcon s_windowListIcon;

static QIcon s_windowListActiveIcon;
Expand Down
3 changes: 0 additions & 3 deletions src/widgets/viewwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,11 @@ void ViewWindow::detachFromBuffer(bool p_quiet)

QIcon ViewWindow::getIcon() const
{
/*
if (m_buffer) {
return m_buffer->isModified() ? s_modifiedIcon : s_savedIcon;
} else {
return s_savedIcon;
}
*/
return QIcon();
}

QString ViewWindow::getName() const
Expand Down

0 comments on commit 1f3b00e

Please sign in to comment.