Commit b837392d authored by Milian Wolff's avatar Milian Wolff
Browse files

keep track of what we add to Kate's menu and remove it when the menu get's...

keep track of what we add to Kate's menu and remove it when the menu get's hidden, to better support the main case of dynamic actions for kdevelop
parent be29d29f
......@@ -73,7 +73,8 @@ private:
SnippetPlugin::SnippetPlugin(QObject *parent, const QVariantList &)
: KDevelop::IPlugin(SnippetFactory::componentData(), parent)
: KDevelop::IPlugin(SnippetFactory::componentData(), parent),
m_createFromContextMenu(0)
{
SnippetStore::init(this);
......@@ -146,10 +147,12 @@ KDevelop::ContextMenuExtension SnippetPlugin::contextMenuExtension(KDevelop::Con
if ( context->type() == KDevelop::Context::EditorContext ) {
KDevelop::EditorContext *econtext = dynamic_cast<KDevelop::EditorContext*>(context);
if ( econtext->view()->selection() ) {
QAction* action = new QAction(KIcon("document-new"), i18n("Create Snippet from Selection"), this);
connect(action, SIGNAL(triggered(bool)), this, SLOT(createSnippetFromSelection()));
action->setData(QVariant::fromValue<void *>(econtext->view()));
extension.addAction(KDevelop::ContextMenuExtension::ExtensionGroup, action);
if ( !m_createFromContextMenu ) {
m_createFromContextMenu = new KAction(KIcon("document-new"), i18n("Create Snippet from Selection"), this);
connect(m_createFromContextMenu, SIGNAL(triggered(bool)), this, SLOT(createSnippetFromSelection()));
}
m_createFromContextMenu->setData(QVariant::fromValue<void *>(econtext->view()));
extension.addAction(KDevelop::ContextMenuExtension::ExtensionGroup, m_createFromContextMenu);
}
}
......
......@@ -64,6 +64,7 @@ public slots:
private:
class SnippetViewFactory *m_factory;
class SnippetCompletionModel* m_model;
KAction* m_createFromContextMenu;
};
#endif
......
......@@ -61,11 +61,12 @@ namespace KDevelop {
struct TextDocumentPrivate {
TextDocumentPrivate(TextDocument *textDocument)
: encoding(""), m_textDocument(textDocument)
, m_loaded(false)
, m_loaded(false), m_addedContextMenu(0)
{
document = 0;
state = IDocument::Clean;
}
QPointer<KTextEditor::Document> document;
IDocument::DocumentState state;
QString encoding;
......@@ -90,7 +91,29 @@ struct TextDocumentPrivate {
QList<ContextMenuExtension> extensions = Core::self()->pluginController()->queryPluginsForContextMenuExtensions( c );
menu->addSeparator();
ContextMenuExtension::populateMenu(menu, extensions);
// first we fill our container and than add those menus and actoins to the actual context menu
Q_ASSERT(m_addedContextMenu == 0);
m_addedContextMenu = new QMenu();
ContextMenuExtension::populateMenu(m_addedContextMenu, extensions);
foreach ( QAction* action, m_addedContextMenu->actions() ) {
menu->addAction(action);
}
}
/**
* Kate uses a static menu, comes from a limitation of the KXMLGUI stuff.
* Hence we have to remove every action we added by hand (in KDevelop we have much
* more dynamic actions than static ones).
*/
void clearContextMenu()
{
QMenu* menu = qobject_cast<QMenu*>(m_textDocument->sender());
Q_ASSERT(menu);
foreach ( QAction* action, m_addedContextMenu->actions() ) {
menu->removeAction(action);
}
delete m_addedContextMenu;
m_addedContextMenu = 0;
}
void modifiedOnDisk(KTextEditor::Document *document, bool /*isModified*/,
......@@ -150,7 +173,7 @@ struct TextDocumentPrivate {
EditorIntegrator::addDocument( m_textDocument->textDocument() );
m_textDocument->notifyLoaded();
}
void documentSaved(KTextEditor::Document* document, bool saveAs)
{
Q_UNUSED(document);
......@@ -162,6 +185,8 @@ struct TextDocumentPrivate {
private:
TextDocument *m_textDocument;
bool m_loaded;
// we want to remove the added stuff when the menu hides
QMenu* m_addedContextMenu;
};
class TextViewPrivate
......@@ -267,6 +292,8 @@ QWidget *TextDocument::createViewWidget(QWidget *parent)
if (view) {
view->setContextMenu( view->defaultContextMenu() );
connect(view, SIGNAL(contextMenuAboutToShow(KTextEditor::View*,QMenu*)), this, SLOT(populateContextMenu(KTextEditor::View*,QMenu*)));
connect(view->contextMenu(), SIGNAL(aboutToHide()),
this, SLOT(clearContextMenu()));
}
if (KTextEditor::CodeCompletionInterface* cc = dynamic_cast<KTextEditor::CodeCompletionInterface*>(view))
......
......@@ -76,6 +76,7 @@ protected:
private:
Q_PRIVATE_SLOT(d, void newDocumentStatus(KTextEditor::Document*))
Q_PRIVATE_SLOT(d, void populateContextMenu(KTextEditor::View*, QMenu*))
Q_PRIVATE_SLOT(d, void clearContextMenu())
Q_PRIVATE_SLOT(d, void textChanged(KTextEditor::Document*))
Q_PRIVATE_SLOT(d, void modifiedOnDisk(KTextEditor::Document *, bool, KTextEditor::ModificationInterface::ModifiedOnDiskReason))
Q_PRIVATE_SLOT(d, void documentUrlChanged(KTextEditor::Document*))
......
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