File indexing completed on 2024-04-28 03:59:12

0001 // This file is part of the KDE libraries
0002 // SPDX-FileCopyrightText: 2020 Nicolas Fella <nicolas.fella@gmx.de>
0003 // SPDX-License-Identifier: LGPL-2.1-or-later
0004 
0005 #ifndef KRECENTFILESMENU_H
0006 #define KRECENTFILESMENU_H
0007 
0008 #include <kwidgetsaddons_export.h>
0009 
0010 #include <QMenu>
0011 #include <QUrl>
0012 
0013 #include <memory> // for std::unique_ptr
0014 
0015 class KRecentFilesMenuPrivate;
0016 
0017 /**
0018  * A menu that offers a set of recent files.
0019  *
0020  * @since 5.74
0021  */
0022 class KWIDGETSADDONS_EXPORT KRecentFilesMenu : public QMenu
0023 {
0024     Q_OBJECT
0025 public:
0026     explicit KRecentFilesMenu(const QString &title, QWidget *parent = nullptr);
0027     explicit KRecentFilesMenu(QWidget *parent = nullptr);
0028     ~KRecentFilesMenu() override;
0029 
0030     /**
0031      * The group the URLs are saved to/read from.
0032      * Unless a group is specified by setGroup "RecentFiles" is used.
0033      */
0034     QString group() const;
0035 
0036     /**
0037      * Specify a group for storing the URLs. This allows e.g. storing multiple
0038      * types of recent files.
0039      *
0040      * By default the group "RecentFiles" is used.
0041      *
0042      * @param group the name of the group.
0043      */
0044     void setGroup(const QString &group);
0045 
0046     /**
0047      *  Add URL to recent files list. This will enable this action.
0048      *
0049      *  @param url The URL of the file
0050      *  @param name The user visible pretty name that appears before the URL
0051      */
0052     void addUrl(const QUrl &url, const QString &name = QString());
0053 
0054     /**
0055      *  Remove a URL from the recent files list.
0056      *
0057      *  @param url The URL of the file
0058      */
0059     void removeUrl(const QUrl &url);
0060 
0061     /**
0062      * The maximum number of files this menu can hold.
0063      *
0064      * When the maximum url count is reached and a new URL is added the
0065      * oldest will be replaced.
0066      *
0067      * By default maximum 10 URLs are shown.
0068      */
0069     int maximumItems() const;
0070 
0071     /**
0072      * Set the maximum URL count.
0073      *
0074      * See \ref maximumItems
0075      */
0076     void setMaximumItems(size_t maximumItems);
0077 
0078     /**
0079      * List of URLs of recent files.
0080      *
0081      * See \ref clearRecentFiles
0082      * See \ref recentFilesChanged
0083      *
0084      * @since 5.101
0085      */
0086     QList<QUrl> recentFiles() const;
0087 
0088 public Q_SLOTS:
0089     /**
0090      * Clear recent files list.
0091      *
0092      * See \ref recentFiles
0093      * See \ref recentFilesChanged
0094      *
0095      * @since 5.101
0096      */
0097     void clearRecentFiles();
0098 
0099 Q_SIGNALS:
0100     /**
0101      * emitted when the user clicks on a file action.
0102      * Usually this should result in the specified URL being opened.
0103      *
0104      * @param url The url associated with the triggered action.
0105      */
0106     void urlTriggered(const QUrl &url);
0107 
0108     /**
0109      * Emitted when the recent files list has been changed.
0110      *
0111      * See \ref recentFiles
0112      * See \ref clearRecentFiles
0113      *
0114      * @since 5.101
0115      */
0116     void recentFilesChanged();
0117 
0118 private:
0119     KWIDGETSADDONS_NO_EXPORT void readFromFile();
0120     KWIDGETSADDONS_NO_EXPORT void writeToFile();
0121     KWIDGETSADDONS_NO_EXPORT void rebuildMenu();
0122 
0123     friend class KRecentFilesMenuPrivate;
0124 
0125     std::unique_ptr<KRecentFilesMenuPrivate> const d;
0126 };
0127 
0128 #endif