File indexing completed on 2024-05-05 05:01:27

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