Commit 1b54009e authored by Milian Wolff's avatar Milian Wolff
Browse files

make extern scripts available through context menu

Now, the context menu in the project manager view, tab and editor
show a submenu "external scripts" which holds the available scripts
which can then be executed for the selected file(s).

scripts that depend on a user selection are only shown in
the editor context menu.

thanks to Ghislain Mary for the patch

REVIEW: 104133
BUG: 257703
parent d8485628
......@@ -21,7 +21,7 @@ kde4_add_plugin(kdevexternalscript ${kdevexternalscript_PART_SRCS})
target_link_libraries(kdevexternalscript
${KDE4_KTEXTEDITOR_LIBS} ${KDE4_KIO_LIBS} ${KDE4_KPARTS_LIBS} ${KDE4_KNEWSTUFF3_LIBS}
kdevplatformlanguage kdevplatforminterfaces
kdevplatformlanguage kdevplatforminterfaces kdevplatformproject
kdevplatformutil kdevplatformoutputview
)
......
......@@ -29,18 +29,27 @@
#include <interfaces/icore.h>
#include <interfaces/iuicontroller.h>
#include <interfaces/iruncontroller.h>
#include <interfaces/idocumentcontroller.h>
#include <interfaces/contextmenuextension.h>
#include <interfaces/context.h>
#include <interfaces/isession.h>
#include <outputview/outputjob.h>
#include <outputview/outputmodel.h>
#include <project/projectmodel.h>
#include <language/interfaces/editorcontext.h>
#include <KPluginFactory>
#include <KAboutData>
#include <KAction>
#include <KProcess>
#include <QStandardItemModel>
#include <KAction>
#include <interfaces/isession.h>
#include <QDBusConnection>
#include <KProcess>
#include <QMenu>
K_PLUGIN_FACTORY( ExternalScriptFactory, registerPlugin<ExternalScriptPlugin>(); )
K_EXPORT_PLUGIN( ExternalScriptFactory( KAboutData( "kdevexternalscript", "kdevexternalscript", ki18n( "External Scripts" ),
......@@ -144,6 +153,58 @@ ExternalScriptPlugin::~ExternalScriptPlugin()
m_self = 0;
}
KDevelop::ContextMenuExtension ExternalScriptPlugin::contextMenuExtension( KDevelop::Context* context )
{
m_urls.clear();
if ( context->type() == KDevelop::Context::FileContext ) {
KDevelop::FileContext* filectx = dynamic_cast<KDevelop::FileContext*>( context );
m_urls = filectx->urls();
} else if ( context->type() == KDevelop::Context::ProjectItemContext ) {
KDevelop::ProjectItemContext* projctx = dynamic_cast<KDevelop::ProjectItemContext*>( context );
foreach( KDevelop::ProjectBaseItem* item, projctx->items() ) {
if ( item->file() ) {
m_urls << item->file()->url();
}
}
} else if ( context->type() == KDevelop::Context::EditorContext ) {
KDevelop::EditorContext *econtext = dynamic_cast<KDevelop::EditorContext*>(context);
m_urls << econtext->url();
}
if ( !m_urls.isEmpty() ) {
KDevelop::ContextMenuExtension ext;
QMenu* menu = new QMenu();
menu->setTitle( i18n("External Scripts") );
for ( int row = 0; row < m_model->rowCount(); ++row ) {
ExternalScriptItem* item = dynamic_cast<ExternalScriptItem*>( m_model->item( row ) );
Q_ASSERT( item );
if (context->type() != KDevelop::Context::EditorContext) {
// filter scripts that depend on an opened document
// if the context menu was not requested inside the editor
if (item->performParameterReplacement() && item->command().contains("%s")) {
continue;
} else if (item->inputMode() == ExternalScriptItem::InputSelectionOrNone) {
continue;
}
}
KAction* scriptAction = new KAction( item->text(), this );
scriptAction->setData( QVariant::fromValue<ExternalScriptItem*>( item ));
connect( scriptAction, SIGNAL( triggered() ), SLOT( executeScriptFromContextMenu() ) );
menu->addAction( scriptAction );
}
ext.addAction( KDevelop::ContextMenuExtension::ExtensionGroup, menu->menuAction() );
return ext;
}
return KDevelop::IPlugin::contextMenuExtension( context );
}
void ExternalScriptPlugin::unload()
{
core()->uiController()->removeToolView( m_factory );
......@@ -215,6 +276,20 @@ void ExternalScriptPlugin::executeScriptFromActionData() const
execute( item );
}
void ExternalScriptPlugin::executeScriptFromContextMenu() const
{
KAction* action = dynamic_cast<KAction*>( sender() );
Q_ASSERT( action );
ExternalScriptItem* item = action->data().value<ExternalScriptItem*>();
Q_ASSERT( item );
foreach( const KUrl& url, m_urls) {
KDevelop::ICore::self()->documentController()->openDocument( url );
execute( item );
}
}
void ExternalScriptPlugin::rowsInserted( const QModelIndex& /*parent*/, int start, int end )
{
for ( int i = start; i <= end; ++i ) {
......
......@@ -42,6 +42,7 @@ public:
virtual ~ExternalScriptPlugin();
virtual void unload();
virtual KDevelop::ContextMenuExtension contextMenuExtension(KDevelop::Context* context);
static ExternalScriptPlugin* self();
......@@ -79,12 +80,14 @@ public slots:
private slots:
void rowsRemoved( const QModelIndex& parent, int start, int end );
void rowsInserted( const QModelIndex& parent, int start, int end );
void executeScriptFromContextMenu() const;
private:
/// @param item row in the model for the item to save
void saveItemForRow( int row );
QStandardItemModel* m_model;
KUrl::List m_urls;
static ExternalScriptPlugin* m_self;
class ExternalScriptViewFactory *m_factory;
......
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