File indexing completed on 2024-04-28 04:36:29
0001 /* 0002 SPDX-FileCopyrightText: 2001-2002 Matthias Hoelzer-Kluepfel <hoelzer@kde.org> 0003 SPDX-FileCopyrightText: 2001-2002 Bernd Gehrmann <bernd@kdevelop.org> 0004 SPDX-FileCopyrightText: 2001 Sandy Meier <smeier@kdevelop.org> 0005 SPDX-FileCopyrightText: 2002 Daniel Engelschalt <daniel.engelschalt@gmx.net> 0006 SPDX-FileCopyrightText: 2002 Simon Hausmann <hausmann@kde.org> 0007 SPDX-FileCopyrightText: 2002-2003 Roberto Raggi <roberto@kdevelop.org> 0008 SPDX-FileCopyrightText: 2003 Mario Scalas <mario.scalas@libero.it> 0009 SPDX-FileCopyrightText: 2003 Harald Fernengel <harry@kdevelop.org> 0010 SPDX-FileCopyrightText: 2003, 2006 Hamish Rodda <rodda@kde.org> 0011 SPDX-FileCopyrightText: 2004 Alexander Dymo <adymo@kdevelop.org> 0012 SPDX-FileCopyrightText: 2006 Adam Treat <treat@kde.org> 0013 SPDX-FileCopyrightText: 2007 Andreas Pakulat <apaku@gmx.org> 0014 0015 SPDX-License-Identifier: LGPL-2.0-or-later 0016 */ 0017 0018 #ifndef KDEVPLATFORM_CONTEXT_H 0019 #define KDEVPLATFORM_CONTEXT_H 0020 0021 #include "interfacesexport.h" 0022 0023 #include <QUrl> 0024 #include <QScopedPointer> 0025 0026 class QMimeType; 0027 template <typename T> class QList; 0028 0029 namespace KDevelop 0030 { 0031 class ProjectBaseItem; 0032 class ContextPrivate; 0033 class FileContextPrivate; 0034 class ProjectItemContextPrivate; 0035 class OpenWithContextPrivate; 0036 0037 /** 0038 Base class for every context. 0039 Think of a Context-based class as "useful information associated with a context menu". 0040 0041 When a context menu with a certain "context" associated appears, the platform's 0042 PluginController requests all plugins to return a list of QActions* they want to add 0043 to the context menu and a QString that should be used as the submenu entry. 0044 For example, a SVN plugin could add "commit" and "update" actions to the context 0045 menu of a document in a submenu called "Subversion". 0046 0047 The plugin that originally gets the contextmenu event shouldn't add its own 0048 actions directly to the menu but instead use the same mechanism. 0049 0050 <b>How to show a context menu from a plugin:</b> 0051 -# Create a QMenu in context menu event handler: @code QMenu menu(this); @endcode 0052 -# Create a context: @code FileContext context(list). @endcode 0053 -# Query for plugins: 0054 @code QList<ContextMenuExtension> extensions = 0055 ICore::self()->pluginController()->queryPluginsForContextMenuExtensions(context, &menu); @endcode 0056 -# Populate the menu: 0057 @code ContextMenuExtension::populateMenu(menu, extensions); @endcode 0058 -# Show the popup menu: @code menu.exec(mapToGlobal(pos)); @endcode 0059 0060 <b>How to fill a context menu from a plugin:</b> 0061 -# Implement the @code contextMenuExtension(Context*, QWidget*) @endcode 0062 function in your plugin class. 0063 -# Depending on the context fill the returned ContextMenuExtension with actions:\n 0064 @code 0065 ContextMenuExtension ext; 0066 if (context->hasType(Context::EditorContext)) 0067 { 0068 ext.addAction(ContextMenuExtension::EditorGroup, new QAction(...)); 0069 } 0070 else if context->hasType(Context::FileContext)) 0071 { 0072 ext.addAction(ContextMenuExtension::FileGroup, new QAction(...)); 0073 ... 0074 } 0075 return ext; 0076 @endcode 0077 */ 0078 class KDEVPLATFORMINTERFACES_EXPORT Context 0079 { 0080 public: 0081 /**Destructor.*/ 0082 virtual ~Context(); 0083 0084 /**Pre-defined context types. More may be added so it is possible to add custom 0085 contexts. <strong>We reserve enum values until 1000 (yeah, it is one thousand ) 0086 for kdevplatform official context types.</strong>*/ 0087 enum Type 0088 { 0089 FileContext, /**<File menu.*/ 0090 CodeContext, /**<Code context menu(DeclarationContext or DUContextContext)*/ 0091 EditorContext, /**<Editor menu.*/ 0092 ProjectItemContext, /**<ProjectItem context menu.*/ 0093 OpenWithContext /**<Open With context menu.*/ 0094 }; 0095 0096 /**Implement this in the context so we can provide rtti.*/ 0097 virtual int type() const = 0; 0098 0099 /** 0100 * Convenience method for accessing the urls associated with this context 0101 * 0102 * For example, returns the selected urls in a project item context 0103 */ 0104 virtual QList<QUrl> urls() const = 0; 0105 0106 /**@return The type of this Context, so clients can discriminate 0107 between different file contexts.*/ 0108 bool hasType( int type ) const; 0109 0110 protected: 0111 /**Constructor.*/ 0112 Context(); 0113 explicit Context(ContextPrivate* d); 0114 0115 private: 0116 const QScopedPointer<class ContextPrivate> d_ptr; 0117 Q_DECLARE_PRIVATE(Context) 0118 Q_DISABLE_COPY(Context) 0119 friend class FileContext; 0120 friend class ProjectItemContext; 0121 friend class OpenWithContext; 0122 }; 0123 0124 /** 0125 A context for the a list of selected urls. 0126 */ 0127 class KDEVPLATFORMINTERFACES_EXPORT FileContext : public Context 0128 { 0129 public: 0130 /**Builds the file context using a @ref QList<QUrl> 0131 @param urls The list of selected url.*/ 0132 explicit FileContext( const QList<QUrl> &urls ); 0133 0134 /**Destructor.*/ 0135 ~FileContext() override; 0136 0137 int type() const override; 0138 0139 /**@return A reference to the selected URLs.*/ 0140 QList<QUrl> urls() const override; 0141 0142 private: 0143 Q_DECLARE_PRIVATE(FileContext) 0144 Q_DISABLE_COPY(FileContext) 0145 }; 0146 0147 /** 0148 A context for ProjectItem's. 0149 */ 0150 class KDEVPLATFORMINTERFACES_EXPORT ProjectItemContext : public Context 0151 { 0152 public: 0153 /**Builds the context. 0154 @param items The items to build the context from.*/ 0155 explicit ProjectItemContext( const QList<ProjectBaseItem*> &items ); 0156 0157 /**Destructor.*/ 0158 ~ProjectItemContext() override; 0159 0160 int type() const override; 0161 0162 /** 0163 * @return The project model items for the selected items. 0164 */ 0165 QList<ProjectBaseItem*> items() const; 0166 0167 private: 0168 Q_DECLARE_PRIVATE(ProjectItemContext) 0169 Q_DISABLE_COPY(ProjectItemContext) 0170 }; 0171 0172 /** 0173 * Context menu to open files with custom applications. 0174 */ 0175 class KDEVPLATFORMINTERFACES_EXPORT OpenWithContext : public Context 0176 { 0177 public: 0178 /** 0179 * @p url The files to open. 0180 * @p mimeType The mime type of said file. 0181 */ 0182 OpenWithContext(const QList<QUrl>& urls, const QMimeType& mimeType); 0183 0184 ~OpenWithContext() override; 0185 0186 /** 0187 * @return Context::OpenWithContext 0188 */ 0189 int type() const override; 0190 0191 /** 0192 * @return The files to open. 0193 */ 0194 QList<QUrl> urls() const override; 0195 0196 /** 0197 * @return The mimetype of the url to open. 0198 */ 0199 QMimeType mimeType() const; 0200 0201 private: 0202 Q_DECLARE_PRIVATE(OpenWithContext) 0203 Q_DISABLE_COPY(OpenWithContext) 0204 }; 0205 0206 } 0207 #endif 0208