Move webshortcut model to timeline

This commit is contained in:
James Graham
2025-04-16 20:04:20 +01:00
parent f57004601d
commit d81478ac97
4 changed files with 1 additions and 2 deletions

View File

@@ -0,0 +1,135 @@
// SPDX-FileCopyrightText: 2010 Eike Hein <hein@kde.org>
// SPDX-FileCopyrightText: 2021 Carl Schwan <carl@carlschwan.eu>
// SPDX-License-Identifier: LGPL-2.0-or-later
#include "webshortcutmodel.h"
#ifdef HAVE_KIO
#include <KIO/CommandLauncherJob>
#include <KUriFilter>
#endif
#include <KStringHandler>
using namespace Qt::StringLiterals;
struct WebShortcutModelPrivate {
QString selectedText;
#ifdef HAVE_KIO
KUriFilterData filterData;
#endif
QStringList searchProviders;
};
WebShortcutModel::WebShortcutModel(QObject *parent)
: QAbstractListModel(parent)
, d(new WebShortcutModelPrivate)
{
}
WebShortcutModel::~WebShortcutModel()
{
}
QString WebShortcutModel::selectedText() const
{
return d->selectedText;
}
QString WebShortcutModel::trunkatedSearchText() const
{
return KStringHandler::rsqueeze(d->selectedText, 21);
}
bool WebShortcutModel::enabled() const
{
#ifdef HAVE_KIO
return true;
#else
return false;
#endif
}
void WebShortcutModel::setSelectedText(const QString &selectedText)
{
if (d->selectedText == selectedText) {
return;
}
#ifdef HAVE_KIO
beginResetModel();
d->selectedText = selectedText;
if (selectedText.isEmpty()) {
endResetModel();
return;
}
QString searchText = selectedText;
searchText = searchText.replace(QLatin1Char('\n'), QLatin1Char(' ')).replace(QLatin1Char('\r'), QLatin1Char(' ')).simplified();
if (searchText.isEmpty()) {
endResetModel();
return;
}
d->filterData.setData(searchText);
d->filterData.setSearchFilteringOptions(KUriFilterData::RetrievePreferredSearchProvidersOnly);
if (KUriFilter::self()->filterSearchUri(d->filterData, KUriFilter::NormalTextFilter)) {
d->searchProviders = d->filterData.preferredSearchProviders();
}
endResetModel();
#else
d->selectedText = selectedText;
#endif
Q_EMIT selectedTextChanged();
}
int WebShortcutModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
#ifdef HAVE_KIO
if (!d->selectedText.isEmpty()) {
return d->searchProviders.count();
}
#endif
return 0;
}
QVariant WebShortcutModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
return {};
}
#ifdef HAVE_KIO
switch (role) {
case Qt::DisplayRole:
return d->searchProviders[index.row()];
case Qt::DecorationRole:
return d->filterData.iconNameForPreferredSearchProvider(d->searchProviders[index.row()]);
case Qt::EditRole:
return d->filterData.queryForPreferredSearchProvider(d->searchProviders[index.row()]);
}
#endif
return {};
}
void WebShortcutModel::trigger(const QString &data)
{
#ifdef HAVE_KIO
KUriFilterData filterData(data);
if (KUriFilter::self()->filterSearchUri(filterData, KUriFilter::WebShortcutFilter)) {
Q_EMIT openUrl(filterData.uri());
}
#else
Q_UNUSED(data);
#endif
}
void WebShortcutModel::configureWebShortcuts()
{
#ifdef HAVE_KIO
auto job = new KIO::CommandLauncherJob(u"kcmshell6"_s, QStringList() << u"webshortcuts"_s, this);
job->exec();
#endif
}
#include "moc_webshortcutmodel.cpp"

View File

@@ -0,0 +1,109 @@
// SPDX-FileCopyrightText: 2021 Carl Schwan <carl@carlschwan.eu>
// SPDX-License-Identifier: LGPL-2.0-or-later
#pragma once
#include <QAbstractListModel>
#include <QQmlEngine>
#include <memory>
struct WebShortcutModelPrivate;
/**
* @class WebShortcutModel
*
* This class defines the model for listing web shortcuts for a specified selectedText.
*
* This can be used as follows in your QML code:
*
* ```qml
* QQC2.Menu {
* id: webshortcutmenu
*
* title: i18n("Search for '%1'", webshortcutmodel.trunkatedSearchText)
* property bool isVisible: selectedText && selectedText.length > 0 && webshortcutmodel.enabled
* Component.onCompleted: webshortcutmenu.parent.visible = isVisible
* onIsVisibleChanged: webshortcutmenu.parent.visible = isVisible
* Instantiator {
* model: WebShortcutModel {
* id: webshortcutmodel
* selectedText: loadRoot.selectedText
* onOpenUrl: Qt.openUrlExternally(url)
* }
* delegate: QQC2.MenuItem {
* text: model.display
* icon.name: model.decoration
* onTriggered: webshortcutmodel.trigger(model.edit)
* }
* onObjectAdded: webshortcutmenu.insertItem(0, object)
* }
* QQC2.MenuSeparator {}
* QQC2.MenuItem {
* text: i18n("Configure Web Shortcuts…")
* icon.name: "configure"
* onTriggered: webshortcutmodel.configureWebShortcuts()
* }
* }
* ```
*/
class WebShortcutModel : public QAbstractListModel
{
Q_OBJECT
QML_ELEMENT
/**
* @brief The text to find web shortcuts for.
*/
Q_PROPERTY(QString selectedText READ selectedText WRITE setSelectedText NOTIFY selectedTextChanged)
/**
* @brief The selectedText elided at a set width.
*/
Q_PROPERTY(QString trunkatedSearchText READ trunkatedSearchText NOTIFY selectedTextChanged)
/**
* @brief Whether web shortcuts are available.
*/
Q_PROPERTY(bool enabled READ enabled CONSTANT)
public:
explicit WebShortcutModel(QObject *parent = nullptr);
~WebShortcutModel();
QString selectedText() const;
void setSelectedText(const QString &selectedText);
QString trunkatedSearchText() const;
bool enabled() const;
/**
* @brief Get the given role value at the given index.
*
* @sa QAbstractItemModel::data
*/
QVariant data(const QModelIndex &index, int role) const override;
/**
* @brief Number of rows in the model.
*
* @sa QAbstractItemModel::rowCount
*/
int rowCount(const QModelIndex &parent) const override;
/**
* @brief Trigger the openUrl signal for the given web shortcut.
*/
Q_INVOKABLE void trigger(const QString &data);
/**
* @brief Request the menu for configuring web shortcut settings be opened.
*/
Q_INVOKABLE void configureWebShortcuts();
Q_SIGNALS:
void selectedTextChanged();
void openUrl(const QUrl &url);
private:
std::unique_ptr<WebShortcutModelPrivate> d;
};