File indexing completed on 2024-11-03 10:41:31

0001 // SPDX-FileCopyrightText: 2021 Carl Schwan <carl@carlschwan.eu>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 
0004 #pragma once
0005 
0006 #include <QAbstractListModel>
0007 #include <memory>
0008 
0009 struct KWebShortcutModelPrivate;
0010 
0011 /**
0012  * @class KWebShortcutModel
0013  *
0014  * This class defines the model for listing web shortcuts for a specified selectedText.
0015  *
0016  * This can be used as follows in your QML code:
0017  *
0018  * ```qml
0019  * QQC2.Menu {
0020  *      id: webshortcutmenu
0021  *
0022  *      title: i18n("Search for '%1'", webshortcutmodel.trunkatedSearchText)
0023  *      property bool isVisible: selectedText && selectedText.length > 0 && webshortcutmodel.enabled
0024  *      Component.onCompleted: webshortcutmenu.parent.visible = isVisible
0025  *      onIsVisibleChanged: webshortcutmenu.parent.visible = isVisible
0026  *      Instantiator {
0027  *          model: WebShortcutModel {
0028  *              id: webshortcutmodel
0029  *              selectedText: loadRoot.selectedText
0030  *              onOpenUrl: Qt.openUrlExternally(url)
0031  *          }
0032  *          delegate: QQC2.MenuItem {
0033  *              text: model.display
0034  *              icon.name: model.decoration
0035  *              onTriggered: webshortcutmodel.trigger(model.edit)
0036  *          }
0037  *          onObjectAdded: webshortcutmenu.insertItem(0, object)
0038  *      }
0039  *      QQC2.MenuSeparator {}
0040  *      QQC2.MenuItem {
0041  *          text: i18n("Configure Web Shortcuts...")
0042  *          icon.name: "configure"
0043  *          onTriggered: webshortcutmodel.configureWebShortcuts()
0044  *      }
0045  *  }
0046  *  ```
0047  */
0048 class KWebShortcutModel : public QAbstractListModel
0049 {
0050     Q_OBJECT
0051 
0052     /**
0053      * @brief The text to find web shortcuts for.
0054      */
0055     Q_PROPERTY(QString selectedText READ selectedText WRITE setSelectedText NOTIFY selectedTextChanged)
0056 
0057     /**
0058      * @brief The selectedText elided at a set width.
0059      */
0060     Q_PROPERTY(QString trunkatedSearchText READ trunkatedSearchText NOTIFY selectedTextChanged)
0061 
0062     /**
0063      * @brief Whether web shortcuts are available.
0064      */
0065     Q_PROPERTY(bool enabled READ enabled CONSTANT)
0066 public:
0067     explicit KWebShortcutModel(QObject *parent = nullptr);
0068     ~KWebShortcutModel();
0069 
0070     QString selectedText() const;
0071     void setSelectedText(const QString &selectedText);
0072 
0073     QString trunkatedSearchText() const;
0074 
0075     bool enabled() const;
0076 
0077     /**
0078      * @brief Get the given role value at the given index.
0079      *
0080      * @sa QAbstractItemModel::data
0081      */
0082     QVariant data(const QModelIndex &index, int role) const override;
0083 
0084     /**
0085      * @brief Number of rows in the model.
0086      *
0087      * @sa QAbstractItemModel::rowCount
0088      */
0089     int rowCount(const QModelIndex &parent) const override;
0090 
0091     /**
0092      * @brief Trigger the openUrl signal for the given web shortcut.
0093      */
0094     Q_INVOKABLE void trigger(const QString &data);
0095 
0096     /**
0097      * @brief Request the menu for configuring web shortcut settings be opened.
0098      */
0099     Q_INVOKABLE void configureWebShortcuts();
0100 
0101 Q_SIGNALS:
0102     void selectedTextChanged();
0103     void openUrl(const QUrl &url);
0104 
0105 private:
0106     std::unique_ptr<KWebShortcutModelPrivate> d;
0107 };