File indexing completed on 2024-04-28 15:29:00

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 <KNS3/knewstuff_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  * @since 5.10
0028  */
0029 class KNEWSTUFF_EXPORT KMoreToolsMenuFactory
0030 {
0031 public:
0032     /**
0033      * @param uniqueId defines the config section name where the user
0034      * settings done by the Configure dialog will be stored.
0035      *
0036      * For more information about the parameter see KMoreTools::KMoreTools.
0037      */
0038     KMoreToolsMenuFactory(const QString &uniqueId);
0039 
0040     ~KMoreToolsMenuFactory();
0041 
0042     KMoreToolsMenuFactory(const KMoreToolsMenuFactory &) = delete;
0043     KMoreToolsMenuFactory &operator=(const KMoreToolsMenuFactory &) = delete;
0044 
0045     /**
0046      * For each grouping name menu items will be created an appended to a
0047      * lazy menu which is returned. The menu is lazy in a sense that the
0048      * items are not added until the menu is about to be shown.
0049      * NOTE: This means if the menu is not shown (as would be by e.g.
0050      * calling exec()) then the menu stays empty.
0051      *
0052      * For details on available grouping names see
0053      * KMoreToolsPresets::registerServicesByGroupingNames.
0054      *
0055      * For each grouping name there might be special handlings that take the
0056      * optional given @p url into account if needed. By default the url is
0057      * empty.
0058      *
0059      * Furthermore, some selected menu items will be put into the "More"
0060      * menu section by default.
0061      *
0062      * The "more:" grouping name
0063      * -------------------------
0064      * There is a special grouping name "more:" (note the colon). If this name
0065      * is given in the list, all further groupings are put into the More
0066      * section by default.
0067      *
0068      * NOTE that this method overrides a previously created QMenu* instance
0069      * of the same KMoreToolsMenuFactory instance. The reason is that the
0070      * internal KMoreTools pointer is reused.
0071      * (todo: solve this or rename the class?)
0072      *
0073      * @returns the created QMenu which includes a Main and (maybe) a More
0074      * section and an item that starts configure dialog where the user can
0075      * configure the menu (see KMoreTools).
0076      */
0077     QMenu *createMenuFromGroupingNames(const QStringList &groupingNames, const QUrl &url = QUrl());
0078 
0079     /**
0080      * See createMenuFromGroupingNames except that the menu is not created
0081      * but you have to provide one yourself. This is useful to create
0082      * lazy menus by connecting QMenu::aboutToShow.
0083      *
0084      * WARN 1: KMoreToolsMenuFactory must live as long as you would like to use
0085      * the menu.
0086      *
0087      * WARN 2: You must NOT reuse an existing KMoreToolsMenuFactory instance
0088      * to create a different menu.
0089      *
0090      * @since 5.11
0091      */
0092     void fillMenuFromGroupingNames(QMenu *menu, const QStringList &groupingNames, const QUrl &url = QUrl());
0093 
0094     /**
0095      * Set @p widget as the parent widget of the QMenu that will be created
0096      * by createMenuFromGroupingNames().
0097      * @see createMenuFromGroupingNames()
0098      * @since 5.37
0099      */
0100     void setParentWidget(QWidget *widget);
0101 
0102 private:
0103     /*
0104      * TODO KF6: Not used, remove in the KF6 transition.
0105      * Preserves object size to counteract BIC introduced with
0106      * 3ecc3701f7e1aa83104b06fa90ea07eeca47f93d.
0107      */
0108     KMoreTools *m_off = nullptr;
0109 
0110     const std::unique_ptr<KMoreToolsMenuFactoryPrivate> d;
0111 };
0112 
0113 #endif