Commit a1093c7a authored by Kåre Särs's avatar Kåre Särs
Browse files

Add shortcut for showing git commit info

To add a shortcut we need to add a KXMLGUIClient (view) to access the
actionCollection. To not close the tool-tip when the shortcut is
triggered, we need to ignore the shortcut key in the event filter.
parent 46fbd117
......@@ -7,6 +7,7 @@ target_sources(
PRIVATE
kategitblameplugin.cpp
gitblametooltip.cpp
plugin.qrc
)
kcoreaddons_desktop_to_json(kategitblameplugin kategitblameplugin.desktop)
......
......@@ -131,6 +131,10 @@ class GitBlameTooltip::Private : public QTextBrowser
Q_OBJECT
public:
QKeySequence m_ignoreKeySequence;
static const uint64_t ModifierMask =
Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier | Qt::KeypadModifier | Qt::GroupSwitchModifier;
Private() : QTextBrowser(nullptr)
{
......@@ -181,13 +185,13 @@ public:
case QEvent::KeyRelease:
{
QKeyEvent *ke = static_cast<QKeyEvent *>(event);
if (ke->matches(QKeySequence::Copy) ||
ke->matches(QKeySequence::SelectAll) ||
ke->key() == Qt::Key_Control ||
ke->key() == Qt::Key_Alt ||
ke->key() == Qt::Key_Shift ||
ke->key() == Qt::Key_AltGr ||
ke->key() == Qt::Key_Meta) {
int ignoreKey = 0;
if (m_ignoreKeySequence.count() > 0) {
ignoreKey = m_ignoreKeySequence[m_ignoreKeySequence.count() - 1] & ~ModifierMask;
}
if (ke->matches(QKeySequence::Copy) || ke->matches(QKeySequence::SelectAll) || (ignoreKey != 0 && ignoreKey == ke->key())
|| ke->key() == Qt::Key_Control || ke->key() == Qt::Key_Alt || ke->key() == Qt::Key_Shift || ke->key() == Qt::Key_AltGr
|| ke->key() == Qt::Key_Meta) {
event->accept();
return true;
}
......@@ -336,4 +340,9 @@ void GitBlameTooltip::show(const QString &text, QPointer<KTextEditor::View> vie
d->show();
}
void GitBlameTooltip::setIgnoreKeySequence(QKeySequence sequence)
{
d->m_ignoreKeySequence = sequence;
}
#include "gitblametooltip.moc"
......@@ -26,6 +26,8 @@ public:
void show(const QString &text, QPointer<KTextEditor::View> v);
void setIgnoreKeySequence(QKeySequence sequence);
private:
class Private;
Private *const d;
......
......@@ -9,10 +9,13 @@
#include <algorithm>
#include <KActionCollection>
#include <KConfigGroup>
#include <KLocalizedString>
#include <KPluginFactory>
#include <KSharedConfig>
#include <KXMLGUIFactory>
#include <KTextEditor/Editor>
#include <KTextEditor/Document>
#include <KTextEditor/InlineNoteInterface>
......@@ -134,6 +137,38 @@ KateGitBlamePlugin::~KateGitBlamePlugin()
qDeleteAll(m_inlineNoteProviders);
}
KateGitBlamePluginView::KateGitBlamePluginView(KateGitBlamePlugin *plugin, KTextEditor::MainWindow *mainwindow)
: QObject(plugin)
, m_mainWindow(mainwindow)
{
KXMLGUIClient::setComponentName(QStringLiteral("kategitblameplugin"), i18n("Git Blame"));
setXMLFile(QStringLiteral("ui.rc"));
QAction *showBlameAction = actionCollection()->addAction(QStringLiteral("git_blame_show"));
showBlameAction->setText(i18n("Show Git Blame Details"));
actionCollection()->setDefaultShortcut(showBlameAction, Qt::CTRL | Qt::ALT | Qt::Key_G);
connect(showBlameAction, &QAction::triggered, plugin, [this, plugin, showBlameAction]() {
KTextEditor::View *kv = m_mainWindow->activeView();
if (!kv) {
return;
}
KTextEditor::Document *doc = kv->document();
if (!doc) {
return;
}
plugin->setToolTipIgnoreKeySequence(showBlameAction->shortcut());
int lineNr = kv->cursorPosition().line();
const KateGitBlameInfo &info = plugin->blameInfo(lineNr, doc->line(lineNr));
plugin->showCommitInfo(info.commitHash);
});
m_mainWindow->guiFactory()->addClient(this);
}
KateGitBlamePluginView::~KateGitBlamePluginView()
{
m_mainWindow->guiFactory()->removeClient(this);
}
QObject *KateGitBlamePlugin::createView(KTextEditor::MainWindow *mainWindow)
{
m_mainWindow = mainWindow;
......@@ -147,7 +182,7 @@ QObject *KateGitBlamePlugin::createView(KTextEditor::MainWindow *mainWindow)
connect(m_mainWindow, &KTextEditor::MainWindow::viewChanged, this, &KateGitBlamePlugin::viewChanged);
return nullptr;
return new KateGitBlamePluginView(this, mainWindow);
}
void KateGitBlamePlugin::addDocument(KTextEditor::Document *doc)
......@@ -367,6 +402,11 @@ const KateGitBlameInfo &KateGitBlamePlugin::blameGetUpdateInfo(int lineNr)
return info;
}
void KateGitBlamePlugin::setToolTipIgnoreKeySequence(QKeySequence sequence)
{
m_tooltip.setIgnoreKeySequence(sequence);
}
void KateGitBlamePlugin::readConfig()
{
}
......
......@@ -55,6 +55,17 @@ private:
QLocale m_locale;
};
class KateGitBlamePluginView : public QObject, public KXMLGUIClient
{
Q_OBJECT
public:
KateGitBlamePluginView(KateGitBlamePlugin *plugin, KTextEditor::MainWindow *mainwindow);
~KateGitBlamePluginView() override;
private:
KTextEditor::MainWindow *m_mainWindow;
};
class KateGitBlamePlugin : public KTextEditor::Plugin
{
Q_OBJECT
......@@ -72,6 +83,8 @@ public:
void showCommitInfo(const QString &hash);
void setToolTipIgnoreKeySequence(QKeySequence sequence);
private Q_SLOTS:
void viewChanged(KTextEditor::View *view);
......
<!DOCTYPE RCC>
<RCC version="1.0">
<qresource prefix="/kxmlgui5/kategitblameplugin">
<file>ui.rc</file>
</qresource>
</RCC>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE gui SYSTEM "kpartgui.dtd">
<gui name="kategitblameplugin" library="kategitblameplugin" version="1" translationDomain="kategitblameplugin">
<MenuBar>
<Menu name="view">
<text>&amp;View</text>
<Action name="git_blame_show" group="view_operations"/>
</Menu>
</MenuBar>
</gui>
<!-- kate: space-indent on; indent-width 4; replace-tabs on; -->
Markdown is supported
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