Commit 301a258b authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Christoph Cullmann
Browse files

Allow hiding tab buttons in sidebars

Sometimes when you enable a plugin you don't want its whole toolview
to be around all the time snippets for e.g., is such a plugin whose
toolview is rarely a used thing for me. However, the toolview and its
respective button in the sidebar is always there increasing clutter.

This change introduces a feature which allows a user to hide whatever
buttons they want. This allows kate to be even more minimal.

I haven't coupled "tool view showing" with the "button show/hide" as
both can be used independent of each other. For e.g., you can hide the
"Documents" button in the sidebar but still use a shortcut to see the
toolview. This will allow people to even completely hide the sidebars
and still be able to use all the plugin features and toolviews on demand

One more thing that can be done as a follow up is to create actions
that hide a sidebar completely.
parent 804e2aa3
Pipeline #186089 failed with stage
in 10 minutes and 8 seconds
......@@ -91,6 +91,9 @@ GUIClient::GUIClient(MainWindow *mw)
setXML(completeDescription, false /*merge*/);
}
m_sidebarButtonsMenu = new KActionMenu(i18n("Sidebar Buttons"), this);
actionCollection()->addAction(QStringLiteral("kate_mdi_show_sidebar_buttons"), m_sidebarButtonsMenu);
m_toolMenu = new KActionMenu(i18n("Tool &Views"), this);
actionCollection()->addAction(QStringLiteral("kate_mdi_toolview_menu"), m_toolMenu);
m_showSidebarsAction = new KToggleAction(i18n("Show Side&bars"), this);
......@@ -135,40 +138,58 @@ void GUIClient::registerToolView(ToolView *tv)
QString aname = QLatin1String("kate_mdi_toolview_") + tv->id;
// try to read the action shortcut
QList<QKeySequence> shortcuts;
KSharedConfigPtr cfg = KSharedConfig::openConfig();
QString shortcutString = cfg->group("Shortcuts").readEntry(aname, QString());
const auto shortcutStrings = shortcutString.split(QLatin1Char(';'));
for (const QString &shortcut : shortcutStrings) {
shortcuts << QKeySequence::fromString(shortcut);
}
auto shortcutsForActionName = [](const QString &aname) {
QList<QKeySequence> shortcuts;
KSharedConfigPtr cfg = KSharedConfig::openConfig();
const QString shortcutString = cfg->group("Shortcuts").readEntry(aname, QString());
const auto shortcutStrings = shortcutString.split(QLatin1Char(';'));
for (const QString &shortcut : shortcutStrings) {
shortcuts << QKeySequence::fromString(shortcut);
}
return shortcuts;
};
/** Show ToolView Action **/
KToggleAction *a = new ToggleToolViewAction(i18n("Show %1", tv->text), tv, this);
actionCollection()->setDefaultShortcuts(a, shortcuts);
actionCollection()->setDefaultShortcuts(a, shortcutsForActionName(aname));
actionCollection()->addAction(aname, a);
m_toolViewActions.push_back(a);
m_toolMenu->addAction(a);
m_toolToAction.emplace(tv, a);
auto &actionsForTool = m_toolToActions[tv];
actionsForTool.push_back(a);
/** Show Tab button in sidebar action **/
aname = QStringLiteral("kate_mdi_show_toolview_button_") + tv->id;
a = new KToggleAction(i18n("Show %1 Button", tv->text), this);
a->setChecked(true);
actionCollection()->setDefaultShortcuts(a, shortcutsForActionName(aname));
actionCollection()->addAction(aname, a);
connect(a, &KToggleAction::toggled, this, [toolview = QPointer<ToolView>(tv)](bool checked) {
if (toolview) {
const QSignalBlocker b(toolview);
toolview->sidebar()->showToolviewTab(toolview, checked);
}
});
connect(tv, &ToolView::tabButtonVisibleChanged, a, &QAction::setChecked);
m_sidebarButtonsMenu->addAction(a);
actionsForTool.push_back(a);
updateActions();
}
void GUIClient::unregisterToolView(ToolView *tv)
{
QAction *a = m_toolToAction[tv];
if (!a) {
auto &actionsForTool = m_toolToActions[tv];
if (actionsForTool.empty())
return;
}
m_toolViewActions.erase(std::remove(m_toolViewActions.begin(), m_toolViewActions.end(), a), m_toolViewActions.end());
delete a;
m_toolToAction.erase(tv);
for (auto *a : actionsForTool) {
delete a;
}
m_toolToActions.erase(tv);
updateActions();
}
......@@ -190,6 +211,7 @@ void GUIClient::updateActions()
QList<QAction *> addList;
addList.append(m_toolMenu);
addList.append(m_sidebarButtonsMenu);
plugActionList(actionListName, addList);
}
......@@ -238,6 +260,16 @@ QSize ToolView::minimumSizeHint() const
return QSize(160, 160);
}
bool ToolView::tabButtonVisible() const
{
return isTabButtonVisible;
}
void ToolView::setTabButtonVisible(bool visible)
{
isTabButtonVisible = visible;
}
ToolView::~ToolView()
{
m_mainWin->toolViewDeleted(this);
......@@ -529,6 +561,23 @@ bool Sidebar::hideWidget(ToolView *widget)
return true;
}
void Sidebar::showToolviewTab(ToolView *widget, bool show)
{
auto it = m_widgetToId.find(widget);
if (it == m_widgetToId.end()) {
qWarning() << Q_FUNC_INFO << "Unexpected no id for widget " << widget;
return;
}
auto *tab = this->tab(it->second);
if (widget->tabButtonVisible() == show) {
return;
} else {
widget->setTabButtonVisible(show);
tab->setVisible(show);
Q_EMIT widget->tabButtonVisibleChanged(show);
}
}
bool Sidebar::isCollapsed()
{
if (!m_splitter) {
......@@ -671,6 +720,8 @@ bool Sidebar::eventFilter(QObject *obj, QEvent *ev)
w->persistent ? i18n("Make Non-Persistent") : i18n("Make Persistent"))
->setData(10);
menu.addAction(i18n("Hide Button"))->setData(11);
menu.addSection(QIcon::fromTheme(QStringLiteral("move")), i18n("Move To"));
if (position() != 0) {
......@@ -746,6 +797,10 @@ void Sidebar::buttonPopupActivate(QAction *a)
}
}
}
if (id == 11) {
showToolviewTab(w, false);
}
}
void Sidebar::updateLastSize()
......@@ -842,13 +897,16 @@ void Sidebar::restoreSession(KConfigGroup &config)
anyVis = tv->toolVisible();
}
setTab(m_widgetToId[tv], tv->toolVisible());
auto id = m_widgetToId[tv];
setTab(id, tv->toolVisible());
if (tv->toolVisible()) {
tv->show();
} else {
tv->hide();
}
bool showTab = config.readEntry(QStringLiteral("Kate-MDI-ToolView-%1-Show-Button-In-Sidebar").arg(tv->id), true);
showToolviewTab(tv, showTab);
}
if (anyVis) {
......@@ -876,6 +934,12 @@ void Sidebar::saveSession(KConfigGroup &config)
config.writeEntry(QStringLiteral("Kate-MDI-ToolView-%1-Sidebar-Position").arg(tv->id), i);
config.writeEntry(QStringLiteral("Kate-MDI-ToolView-%1-Visible").arg(tv->id), tv->toolVisible());
config.writeEntry(QStringLiteral("Kate-MDI-ToolView-%1-Persistent").arg(tv->id), tv->persistent);
auto it = m_widgetToId.find(tv);
if (it == m_widgetToId.end()) {
qWarning() << Q_FUNC_INFO << "Unexpected no tab id for toolview: " << tv;
continue;
}
config.writeEntry(QStringLiteral("Kate-MDI-ToolView-%1-Show-Button-In-Sidebar").arg(tv->id), tv->tabButtonVisible());
}
}
......
......@@ -75,10 +75,10 @@ private Q_SLOTS:
private:
MainWindow *m_mw;
KToggleAction *m_showSidebarsAction;
std::vector<QAction *> m_toolViewActions;
std::unordered_map<ToolView *, QAction *> m_toolToAction;
std::unordered_map<ToolView *, std::vector<QAction *>> m_toolToActions;
KActionMenu *m_toolMenu;
QAction *m_hideToolViews;
KActionMenu *m_sidebarButtonsMenu;
};
class ToolView : public QFrame
......@@ -116,6 +116,8 @@ Q_SIGNALS:
*/
void toolVisibleChanged(bool visible);
void tabButtonVisibleChanged(bool visible);
/**
* some internal methodes needed by the main window and the sidebars
*/
......@@ -137,6 +139,13 @@ public:
QSize sizeHint() const override;
QSize minimumSizeHint() const override;
/**
* Whether the tab button for this toolview is visible
* in the sidebar or hidden
*/
bool tabButtonVisible() const;
void setTabButtonVisible(bool visible);
protected:
void childEvent(QChildEvent *ev) override;
void actionEvent(QActionEvent *event) override;
......@@ -159,6 +168,11 @@ private:
*/
bool m_toolVisible;
/**
* Is the button visible in sidebar
*/
bool isTabButtonVisible = true;
/**
* is this view persistent?
*/
......@@ -193,6 +207,8 @@ public:
bool showWidget(ToolView *widget);
bool hideWidget(ToolView *widget);
void showToolviewTab(ToolView *widget, bool show);
bool isCollapsed();
void handleCollapse(int pos, int index);
void expandSidebar(ToolView *widget);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment