Commit bf39f91d authored by David Faure's avatar David Faure

Dict applet: implement config dialog for choosing a dictionary

Summary:
This request plasma-workspace 1abcb79 (5.11) or master for some
fixes in the dict engine.

Test Plan: Tested in plasmoidviewer

Reviewers: davidedmundson, broulik, anthonyfieroni

Reviewed By: davidedmundson

Subscribers: #plasma

Differential Revision: https://phabricator.kde.org/D9003
parent 2e0fd299
......@@ -17,6 +17,7 @@ add_definitions(-DTRANSLATION_DOMAIN="plasma_applet_org.kde.plasma.dict")
set(dict_SRCS
plugin/dict_plugin.cpp
plugin/dict_object.cpp
plugin/dictionariesmodel.cpp
)
add_library(dictplugin SHARED ${dict_SRCS})
......
import QtQuick 2.1
import org.kde.plasma.configuration 2.0
ConfigModel {
ConfigCategory {
name: i18n("Dictionaries")
icon: "image"
source: "ConfigDictionaries.qml"
}
}
/*
* Copyright 2017 David Faure <faure@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 2.010-1301, USA.
*/
import QtQuick 2.1
import QtQuick.Controls 1.1
import QtQuick.Layouts 1.1
import org.kde.plasma.private.dict 1.0
Item {
id: root
property string cfg_dictionary: ""
DictionariesModel {
id: dictionariesModel
}
SystemPalette {
id: syspal
}
ColumnLayout {
width: parent.width
height: parent.height
Label {
Layout.fillWidth: true
text: i18n("Available dictionaries:")
}
ScrollView {
Layout.fillWidth: true
Layout.fillHeight: true
frameVisible: true
ListView {
width: parent.width
model: dictionariesModel
delegate: Item {
width: parent.width
height: pathText.height
Rectangle {
id: highlight
anchors.fill: parent
visible: model.id == root.cfg_dictionary
color: syspal.highlight
}
RowLayout {
id: textLayout
anchors.fill: parent
/*Text { text: model.id }*/
Text {
id: pathText
Layout.fillWidth: true
text: model.description
color: model.id == root.cfg_dictionary ? syspal.highlightedText : syspal.text
}
}
MouseArea {
anchors.fill: parent
onClicked: root.cfg_dictionary = model.id
}
}
}
}
}
}
......@@ -10,23 +10,30 @@ ColumnLayout {
DictObject {
id: dict
selectedDictionary: plasmoid.configuration.dictionary
onSearchInProgress: web.loadHtml(i18n("Looking up definition..."));
onDefinitionFound: web.loadHtml(html);
}
PlasmaComponents.TextField {
id: input
placeholderText: i18n("Enter word to define here")
implicitWidth: units.gridUnit * 40
RowLayout {
Layout.fillWidth: true
onAccepted: {
if (input.text === "") {
web.visible = false;
} else {
web.visible = true;
dict.lookup(input.text);
PlasmaComponents.TextField {
id: input
placeholderText: i18n("Enter word to define here")
implicitWidth: units.gridUnit * 40
onAccepted: {
if (input.text === "") {
web.visible = false;
} else {
web.visible = true;
dict.lookup(input.text);
}
}
}
PlasmaComponents.Button {
iconSource: "configure"
onClicked: plasmoid.action("configure").trigger();
}
}
WebEngineView {
......
......@@ -47,16 +47,15 @@ signals:
DictObject::DictObject(QObject *parent)
: QObject(parent),
m_dataEngine(QString::fromLatin1(defaultDataEngine))
m_dataEngineName(QString::fromLatin1(defaultDataEngine))
{
dataEngine(m_dataEngine)->connectSource(QLatin1String("list-dictionaries"), this);
m_selectedDict = QStringLiteral("wn"); // for testing, use e.g. fd-fra-eng
m_selectedDict = QStringLiteral("wn");
m_webProfile = new QQuickWebEngineProfile(this);
auto *schemeHandler = new DictSchemeHandler(this);
connect(schemeHandler, &DictSchemeHandler::wordClicked,
this, &DictObject::lookup);
m_webProfile->installUrlSchemeHandler("dict", schemeHandler);
m_dataEngine = dataEngine(m_dataEngineName); // Load it upfront so the config dialog can reuse this one
}
void DictObject::lookup(const QString &word)
......@@ -64,34 +63,37 @@ void DictObject::lookup(const QString &word)
const QString newSource = m_selectedDict + ':' + word;
if (!m_source.isEmpty()) {
dataEngine(m_dataEngine)->disconnectSource(m_source, this);
m_dataEngine->disconnectSource(m_source, this);
}
if (!newSource.isEmpty()) {
// Look up new definition
emit searchInProgress();
m_source = newSource;
dataEngine(m_dataEngine)->connectSource(m_source, this);
m_dataEngine->connectSource(m_source, this);
}
}
void DictObject::dataUpdated(const QString &sourceName, const Plasma::DataEngine::Data &data)
{
if (sourceName == QLatin1String("list-dictionaries")) {
m_availableDicts = {};
m_availableDicts.resize(data.count());
int i = 0;
for (auto it = data.begin(), end = data.end(); it != end; ++it, ++i) {
m_availableDicts[i] = AvailableDict{it.key(), it.value().toString()};
}
} else {
const QString html = data.value(QStringLiteral("text")).toString();
if (!html.isEmpty()) {
emit definitionFound(html);
}
Q_UNUSED(sourceName); // always == m_source
const QString html = data.value(QStringLiteral("text")).toString();
if (!html.isEmpty()) {
emit definitionFound(html);
}
}
QString DictObject::selectedDictionary() const
{
return m_selectedDict;
}
void DictObject::setSelectedDictionary(const QString& dict)
{
m_selectedDict = dict;
}
QQuickWebEngineProfile* DictObject::webProfile() const
{
return m_webProfile;
......
......@@ -21,19 +21,22 @@
#include <Plasma/DataEngineConsumer>
#include <Plasma/DataEngine>
#include <QObject>
#include <vector>
class QQuickWebEngineProfile;
class DictObject : public QObject, public Plasma::DataEngineConsumer
{
Q_OBJECT
Q_PROPERTY(QQuickWebEngineProfile* webProfile READ webProfile CONSTANT)
Q_PROPERTY(QString selectedDictionary READ selectedDictionary WRITE setSelectedDictionary)
public:
explicit DictObject(QObject *parent = nullptr);
QQuickWebEngineProfile* webProfile() const;
QString selectedDictionary() const;
void setSelectedDictionary(const QString &dict);
public Q_SLOTS:
void lookup(const QString &word);
......@@ -46,14 +49,10 @@ Q_SIGNALS:
private:
QString m_source;
QString m_dataEngine;
struct AvailableDict {
QString id;
QString description;
};
std::vector<AvailableDict> m_availableDicts;
QString m_dataEngineName;
QString m_selectedDict;
Plasma::DataEngine* m_dataEngine;
QQuickWebEngineProfile* m_webProfile;
};
......
......@@ -18,6 +18,7 @@
*/
#include "dict_plugin.h"
#include "dict_object.h"
#include "dictionariesmodel.h"
#include <QtQml>
......@@ -25,4 +26,5 @@ void DictPlugin::registerTypes (const char *uri)
{
Q_ASSERT(uri == QLatin1String("org.kde.plasma.private.dict"));
qmlRegisterType<DictObject>(uri, 1, 0, "DictObject");
qmlRegisterType<DictionariesModel>(uri, 1, 0, "DictionariesModel");
}
/*
* Copyright (C) 2017 David Faure <faure@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "dictionariesmodel.h"
#include <Plasma/DataEngine>
#include <Plasma/DataContainer>
#include <QDebug>
DictionariesModel::DictionariesModel(QObject* parent)
: QAbstractListModel(parent)
{
Plasma::DataEngine* dataengine = dataEngine(QStringLiteral("dict"));
const QString source = QLatin1String("list-dictionaries");
Plasma::DataContainer *container = dataengine->containerForSource(source);
if (container) { // in practice this never seems to happen, this source is only used here, so never shared
setAvailableDicts(container->data());
}
dataengine->connectSource(source, this);
}
QVariant DictionariesModel::data(const QModelIndex& index, int role) const
{
const int row = index.row();
switch (role) {
case Qt::DisplayRole:
return m_availableDicts[row].description;
case Qt::EditRole:
return m_availableDicts[row].id;
default:
break;
}
return QVariant();
}
int DictionariesModel::rowCount(const QModelIndex& index) const
{
if (index.isValid())
return 0; // flat model
return m_availableDicts.size();
}
QHash<int, QByteArray> DictionariesModel::roleNames() const
{
return { { Qt::DisplayRole, "description" }, { Qt::EditRole, "id" } };
}
void DictionariesModel::dataUpdated(const QString& sourceName, const Plasma::DataEngine::Data& data)
{
Q_ASSERT(sourceName == QLatin1String("list-dictionaries"));
beginResetModel();
setAvailableDicts(data);
endResetModel();
}
void DictionariesModel::setAvailableDicts(const QVariantMap& data)
{
m_availableDicts = {};
m_availableDicts.resize(data.count());
int i = 0;
for (auto it = data.begin(), end = data.end(); it != end; ++it, ++i) {
m_availableDicts[i] = AvailableDict{it.key(), it.value().toString()};
}
}
/*
* Copyright (C) 2017 David Faure <faure@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef DICTIONARIES_MODEL_H
#define DICTIONARIES_MODEL_H
#include <QAbstractListModel>
#include <Plasma/DataEngineConsumer>
#include <Plasma/DataEngine>
#include <vector>
class DictionariesModel : public QAbstractListModel, public Plasma::DataEngineConsumer
{
Q_OBJECT
public:
explicit DictionariesModel(QObject *parent = nullptr);
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
int rowCount(const QModelIndex &index = QModelIndex()) const override;
QHash<int, QByteArray> roleNames() const override;
private Q_SLOTS:
void dataUpdated(const QString &sourceName, const Plasma::DataEngine::Data &data);
private:
void setAvailableDicts(const QVariantMap &data);
struct AvailableDict {
QString id;
QString description;
};
std::vector<AvailableDict> m_availableDicts;
};
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment