1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
/*
SPDX-FileCopyrightText: 2001-2002 Matthias Hoelzer-Kluepfel <hoelzer@kde.org>
SPDX-FileCopyrightText: 2001-2002 Bernd Gehrmann <bernd@kdevelop.org>
SPDX-FileCopyrightText: 2001 Sandy Meier <smeier@kdevelop.org>
SPDX-FileCopyrightText: 2002 Daniel Engelschalt <daniel.engelschalt@gmx.net>
SPDX-FileCopyrightText: 2002 Simon Hausmann <hausmann@kde.org>
SPDX-FileCopyrightText: 2002-2003 Roberto Raggi <roberto@kdevelop.org>
SPDX-FileCopyrightText: 2003 Mario Scalas <mario.scalas@libero.it>
SPDX-FileCopyrightText: 2003 Harald Fernengel <harry@kdevelop.org>
SPDX-FileCopyrightText: 2003, 2006 Hamish Rodda <rodda@kde.org>
SPDX-FileCopyrightText: 2004 Alexander Dymo <adymo@kdevelop.org>
SPDX-FileCopyrightText: 2006 Adam Treat <treat@kde.org>
SPDX-FileCopyrightText: 2007 Andreas Pakulat <apaku@gmx.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef KDEVPLATFORM_CONTEXT_H
#define KDEVPLATFORM_CONTEXT_H
#include "interfacesexport.h"
#include <QUrl>
#include <QScopedPointer>
class QMimeType;
template <typename T> class QList;
namespace KDevelop
{
class ProjectBaseItem;
class ContextPrivate;
class FileContextPrivate;
class ProjectItemContextPrivate;
class OpenWithContextPrivate;
/**
Base class for every context.
Think of a Context-based class as "useful information associated with a context menu".
When a context menu with a certain "context" associated appears, the platform's
PluginController requests all plugins to return a list of QActions* they want to add
to the context menu and a QString that should be used as the submenu entry.
For example, a SVN plugin could add "commit" and "update" actions to the context
menu of a document in a submenu called "Subversion".
The plugin that originally gets the contextmenu event shouldn't add its own
actions directly to the menu but instead use the same mechanism.
<b>How to show a context menu from a plugin:</b>
-# Create a QMenu in context menu event handler: @code QMenu menu(this); @endcode
-# Create a context: @code FileContext context(list). @endcode
-# Query for plugins:
@code QList<ContextMenuExtension> extensions =
ICore::self()->pluginController()->queryPluginsForContextMenuExtensions(context, &menu); @endcode
-# Populate the menu:
@code ContextMenuExtension::populateMenu(menu, extensions); @endcode
-# Show the popup menu: @code menu.exec(mapToGlobal(pos)); @endcode
<b>How to fill a context menu from a plugin:</b>
-# Implement the @code contextMenuExtension(Context*, QWidget*) @endcode
function in your plugin class.
-# Depending on the context fill the returned ContextMenuExtension with actions:\n
@code
ContextMenuExtension ext;
if (context->hasType(Context::EditorContext))
{
ext.addAction(ContextMenuExtension::EditorGroup, new QAction(...));
}
else if context->hasType(Context::FileContext))
{
ext.addAction(ContextMenuExtension::FileGroup, new QAction(...));
...
}
return ext;
@endcode
*/
class KDEVPLATFORMINTERFACES_EXPORT Context
{
public:
/**Destructor.*/
virtual ~Context();
/**Pre-defined context types. More may be added so it is possible to add custom
contexts. <strong>We reserve enum values until 1000 (yeah, it is one thousand )
for kdevplatform official context types.</strong>*/
enum Type
{
FileContext, /**<File menu.*/
CodeContext, /**<Code context menu(DeclarationContext or DUContextContext)*/
EditorContext, /**<Editor menu.*/
ProjectItemContext, /**<ProjectItem context menu.*/
OpenWithContext /**<Open With context menu.*/
};
/**Implement this in the context so we can provide rtti.*/
virtual int type() const = 0;
/**
* Convenience method for accessing the urls associated with this context
*
* For example, returns the selected urls in a project item context
*/
virtual QList<QUrl> urls() const = 0;
/**@return The type of this Context, so clients can discriminate
between different file contexts.*/
bool hasType( int type ) const;
protected:
/**Constructor.*/
Context();
explicit Context(ContextPrivate* d);
private:
const QScopedPointer<class ContextPrivate> d_ptr;
Q_DECLARE_PRIVATE(Context)
Q_DISABLE_COPY(Context)
friend class FileContext;
friend class ProjectItemContext;
friend class OpenWithContext;
};
/**
A context for the a list of selected urls.
*/
class KDEVPLATFORMINTERFACES_EXPORT FileContext : public Context
{
public:
/**Builds the file context using a @ref QList<QUrl>
@param urls The list of selected url.*/
explicit FileContext( const QList<QUrl> &urls );
/**Destructor.*/
~FileContext() override;
int type() const override;
/**@return A reference to the selected URLs.*/
QList<QUrl> urls() const override;
private:
Q_DECLARE_PRIVATE(FileContext)
Q_DISABLE_COPY(FileContext)
};
/**
A context for ProjectItem's.
*/
class KDEVPLATFORMINTERFACES_EXPORT ProjectItemContext : public Context
{
public:
/**Builds the context.
@param items The items to build the context from.*/
explicit ProjectItemContext( const QList<ProjectBaseItem*> &items );
/**Destructor.*/
~ProjectItemContext() override;
int type() const override;
/**
* @return The project model items for the selected items.
*/
QList<ProjectBaseItem*> items() const;
private:
Q_DECLARE_PRIVATE(ProjectItemContext)
Q_DISABLE_COPY(ProjectItemContext)
};
/**
* Context menu to open files with custom applications.
*/
class KDEVPLATFORMINTERFACES_EXPORT OpenWithContext : public Context
{
public:
/**
* @p url The files to open.
* @p mimeType The mime type of said file.
*/
OpenWithContext(const QList<QUrl>& urls, const QMimeType& mimeType);
~OpenWithContext() override;
/**
* @return Context::OpenWithContext
*/
int type() const override;
/**
* @return The files to open.
*/
QList<QUrl> urls() const override;
/**
* @return The mimetype of the url to open.
*/
QMimeType mimeType() const;
private:
Q_DECLARE_PRIVATE(OpenWithContext)
Q_DISABLE_COPY(OpenWithContext)
};
}
#endif
|