File indexing completed on 2024-05-12 04:42:03

0001 /*
0002     SPDX-FileCopyrightText: 2015 Gregor Mi <codestruct@posteo.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #ifndef KMORETOOLSMENUFACTORY_H
0008 #define KMORETOOLSMENUFACTORY_H
0009 
0010 #include <QMenu>
0011 #include <QString>
0012 #include <QUrl>
0013 
0014 #include <memory>
0015 
0016 #include <kmoretools_export.h>
0017 
0018 class KMoreTools;
0019 class KMoreToolsMenuFactoryPrivate;
0020 
0021 /**
0022  * This is the class with the highest abstraction in KMoreTools.
0023  *
0024  * Creates a QMenu from a list of grouping names. For details on available
0025  * grouping names see KMoreToolsPresets::registerServicesByGroupingNames.
0026  */
0027 class KMORETOOLS_EXPORT KMoreToolsMenuFactory
0028 {
0029 public:
0030     /**
0031      * @param uniqueId defines the config section name where the user
0032      * settings done by the Configure dialog will be stored.
0033      *
0034      * For more information about the parameter see KMoreTools::KMoreTools.
0035      */
0036     KMoreToolsMenuFactory(const QString &uniqueId);
0037 
0038     ~KMoreToolsMenuFactory();
0039 
0040     KMoreToolsMenuFactory(const KMoreToolsMenuFactory &) = delete;
0041     KMoreToolsMenuFactory &operator=(const KMoreToolsMenuFactory &) = delete;
0042 
0043     /**
0044      * For each grouping name menu items will be created an appended to a
0045      * lazy menu which is returned. The menu is lazy in a sense that the
0046      * items are not added until the menu is about to be shown.
0047      * NOTE: This means if the menu is not shown (as would be by e.g.
0048      * calling exec()) then the menu stays empty.
0049      *
0050      * For details on available grouping names see
0051      * KMoreToolsPresets::registerServicesByGroupingNames.
0052      *
0053      * For each grouping name there might be special handlings that take the
0054      * optional given @p url into account if needed. By default the url is
0055      * empty.
0056      *
0057      * Furthermore, some selected menu items will be put into the "More"
0058      * menu section by default.
0059      *
0060      * The "more:" grouping name
0061      * -------------------------
0062      * There is a special grouping name "more:" (note the colon). If this name
0063      * is given in the list, all further groupings are put into the More
0064      * section by default.
0065      *
0066      * NOTE that this method overrides a previously created QMenu* instance
0067      * of the same KMoreToolsMenuFactory instance. The reason is that the
0068      * internal KMoreTools pointer is reused.
0069      * (todo: solve this or rename the class?)
0070      *
0071      * @returns the created QMenu which includes a Main and (maybe) a More
0072      * section and an item that starts configure dialog where the user can
0073      * configure the menu (see KMoreTools).
0074      */
0075     QMenu *createMenuFromGroupingNames(const QStringList &groupingNames, const QUrl &url = QUrl());
0076 
0077     /**
0078      * See createMenuFromGroupingNames except that the menu is not created
0079      * but you have to provide one yourself. This is useful to create
0080      * lazy menus by connecting QMenu::aboutToShow.
0081      *
0082      * WARN 1: KMoreToolsMenuFactory must live as long as you would like to use
0083      * the menu.
0084      *
0085      * WARN 2: You must NOT reuse an existing KMoreToolsMenuFactory instance
0086      * to create a different menu.
0087      */
0088     void fillMenuFromGroupingNames(QMenu *menu, const QStringList &groupingNames, const QUrl &url = QUrl());
0089 
0090     /**
0091      * Set @p widget as the parent widget of the QMenu that will be created
0092      * by createMenuFromGroupingNames().
0093      * @see createMenuFromGroupingNames()
0094      */
0095     void setParentWidget(QWidget *widget);
0096 
0097 private:
0098     const std::unique_ptr<KMoreToolsMenuFactoryPrivate> d;
0099 };
0100 
0101 #endif