Commit a82db684 authored by Jean-Baptiste Mardelle's avatar Jean-Baptiste Mardelle
Browse files

Allow drag & drop of vosk dictionaries urls

parent edc461ee
/***************************************************************************
* Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
* Copyright (C) 2021 by Jean-Baptiste Mardelle (jb@kdenlive.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 *
......@@ -79,6 +79,31 @@
#include <memory>
#endif
SpeechList::SpeechList(QWidget *parent)
: QListWidget(parent)
{
setAlternatingRowColors(true);
setAcceptDrops(true);
setDropIndicatorShown(true);
viewport()->setAcceptDrops(true);
}
QStringList SpeechList::mimeTypes() const
{
return QStringList() << QStringLiteral("text/uri-list");
}
void SpeechList::dropEvent(QDropEvent *event)
{
const QMimeData *qMimeData = event->mimeData();
if (qMimeData->hasUrls()) {
QList<QUrl> urls = qMimeData->urls();
if (!urls.isEmpty()) {
emit getDictionary(urls.takeFirst());
}
}
}
KdenliveSettingsDialog::KdenliveSettingsDialog(QMap<QString, QString> mappable_actions, bool gpuAllowed, QWidget *parent)
: KConfigDialog(parent, QStringLiteral("settings"), KdenliveSettings::self())
, m_modified(false)
......@@ -1768,6 +1793,11 @@ void KdenliveSettingsDialog::slotUpdateAudioCaptureSampleRate(int index)
void KdenliveSettingsDialog::initSpeechPage()
{
m_voskAction = new QAction(i18n("Install missing dependencies"), this);
m_speechListWidget = new SpeechList(this);
connect(m_speechListWidget, &SpeechList::getDictionary, this, &KdenliveSettingsDialog::getDictionary);
QVBoxLayout *l = new QVBoxLayout(m_configSpeech.list_frame);
l->setContentsMargins(0, 0, 0, 0);
l->addWidget(m_speechListWidget);
m_configSpeech.speech_info->setWordWrap(true);
m_configSpeech.check_vosk->setIcon(QIcon::fromTheme(QStringLiteral("view-refresh")));
m_configSpeech.check_vosk->setToolTip(i18n("Check VOSK installation"));
......@@ -1845,7 +1875,9 @@ void KdenliveSettingsDialog::initSpeechPage()
m_configSpeech.button_delete->setIcon(QIcon::fromTheme(QStringLiteral("edit-delete")));
m_configSpeech.button_add->setToolTip(i18n("Add a new speech model from an archive file"));
m_configSpeech.button_delete->setToolTip(i18n("Delete the selected speech model"));
connect(m_configSpeech.button_add, &QToolButton::clicked, this, &KdenliveSettingsDialog::getDictionary);
connect(m_configSpeech.button_add, &QToolButton::clicked, this, [this] () {
this->getDictionary();
});
connect(m_configSpeech.button_delete, &QToolButton::clicked, this, &KdenliveSettingsDialog::removeDictionary);
connect(this, &KdenliveSettingsDialog::parseDictionaries, this, &KdenliveSettingsDialog::slotParseVoskDictionaries);
slotParseVoskDictionaries();
......@@ -1891,7 +1923,7 @@ void KdenliveSettingsDialog::checkVoskDependencies()
m_configSpeech.speech_info->addAction(m_voskAction);
doShowSpeechMessage(missingModules, KMessageWidget::Warning);
} else {
if (m_configSpeech.listWidget->count() == 0) {
if (m_speechListWidget->count() == 0) {
doShowSpeechMessage(i18n("Please add a speech model."), KMessageWidget::Information);
} else {
if (!m_voskUpdated) {
......@@ -1905,7 +1937,7 @@ void KdenliveSettingsDialog::checkVoskDependencies()
pCore->updateVoskAvailability();
}
} else {
if (m_configSpeech.listWidget->count() == 0) {
if (m_speechListWidget->count() == 0) {
doShowSpeechMessage(i18n("Please add a speech model."), KMessageWidget::Information);
} else {
if (!m_voskUpdated) {
......@@ -1956,9 +1988,9 @@ void KdenliveSettingsDialog::doShowSpeechMessage(const QString &message, int mes
m_configSpeech.speech_info->animatedShow();
}
void KdenliveSettingsDialog::getDictionary()
void KdenliveSettingsDialog::getDictionary(const QUrl sourceUrl)
{
QUrl url = KUrlRequesterDialog::getUrl(QUrl(), this, i18n("Enter url for the new dictionary"));
QUrl url = KUrlRequesterDialog::getUrl(sourceUrl, this, i18n("Enter url for the new dictionary"));
if (url.isEmpty()) {
return;
}
......@@ -1986,10 +2018,10 @@ void KdenliveSettingsDialog::removeDictionary()
return;
}
if (!m_configSpeech.listWidget->currentItem()) {
if (!m_speechListWidget->currentItem()) {
return;
}
QString currentModel = m_configSpeech.listWidget->currentItem()->text();
QString currentModel = m_speechListWidget->currentItem()->text();
if (!currentModel.isEmpty() && dir.cd(currentModel)) {
if (KMessageBox::questionYesNo(this, i18n("Delete folder:\n%1", dir.absolutePath())) == KMessageBox::Yes) {
// Make sure we don't accidentally delete a folder that is not ours
......@@ -2068,7 +2100,7 @@ void KdenliveSettingsDialog::processArchive(const QString archiveFile)
void KdenliveSettingsDialog::slotParseVoskDictionaries()
{
m_configSpeech.listWidget->clear();
m_speechListWidget->clear();
QString modelDirectory = KdenliveSettings::vosk_folder_path();
QDir dir;
if (modelDirectory.isEmpty()) {
......@@ -2090,7 +2122,7 @@ void KdenliveSettingsDialog::slotParseVoskDictionaries()
final << d;
}
}
m_configSpeech.listWidget->addItems(final);
m_speechListWidget->addItems(final);
if (!KdenliveSettings::vosk_folder_path().isEmpty()) {
m_configSpeech.custom_vosk_folder->setChecked(true);
m_configSpeech.vosk_folder->setUrl(QUrl::fromLocalFile(KdenliveSettings::vosk_folder_path()));
......
......@@ -23,6 +23,7 @@
#include <KConfigDialog>
#include <KProcess>
#include <QMap>
#include <QListWidget>
#include "ui_configcapture_ui.h"
#include "ui_configenv_ui.h"
......@@ -39,6 +40,21 @@
class ProfileWidget;
class KJob;
class SpeechList : public QListWidget
{
Q_OBJECT
public:
SpeechList(QWidget *parent = nullptr);
protected:
QStringList mimeTypes() const override;
void dropEvent(QDropEvent *event) override;
signals:
void getDictionary(const QUrl url);
};
class KdenliveSettingsDialog : public KConfigDialog
{
Q_OBJECT
......@@ -92,7 +108,7 @@ private slots:
void slotUpdateAudioCaptureChannels(int index);
void slotUpdateAudioCaptureSampleRate(int index);
void slotParseVoskDictionaries();
void getDictionary();
void getDictionary(const QUrl sourceUrl = QUrl());
void removeDictionary();
void downloadModelFinished(KJob* job);
void processArchive(const QString path);
......@@ -121,6 +137,7 @@ private:
Ui::ConfigProject_UI m_configProject;
Ui::ConfigProxy_UI m_configProxy;
Ui::ConfigSpeech_UI m_configSpeech;
SpeechList *m_speechListWidget;
ProfileWidget *m_pw;
KProcess m_readProcess;
QAction *m_voskAction;
......
......@@ -6,40 +6,33 @@
<rect>
<x>0</x>
<y>0</y>
<width>356</width>
<height>379</height>
<width>331</width>
<height>252</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="6" column="1">
<widget class="QToolButton" name="button_delete">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="4" column="0" colspan="5">
<widget class="QListWidget" name="listWidget">
<property name="acceptDrops">
<bool>true</bool>
<widget class="QFrame" name="list_frame">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
</widget>
</item>
<item row="6" column="2">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>280</width>
<height>31</height>
</size>
<item row="6" column="1">
<widget class="QToolButton" name="button_delete">
<property name="text">
<string>...</string>
</property>
</spacer>
</widget>
</item>
<item row="1" column="0" colspan="5">
<widget class="QCheckBox" name="custom_vosk_folder">
......@@ -48,13 +41,6 @@
</property>
</widget>
</item>
<item row="3" column="0" colspan="5">
<widget class="QLabel" name="models_url">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QToolButton" name="button_add">
<property name="text">
......@@ -69,6 +55,16 @@
</property>
</widget>
</item>
<item row="3" column="0" colspan="5">
<widget class="QLabel" name="models_url">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="7" column="0" colspan="5">
<widget class="KMessageWidget" name="speech_info"/>
</item>
<item row="2" column="0" colspan="5">
<widget class="KUrlRequester" name="vosk_folder">
<property name="enabled">
......@@ -79,10 +75,20 @@
</property>
</widget>
</item>
<item row="7" column="0" colspan="5">
<widget class="KMessageWidget" name="speech_info"/>
<item row="6" column="2">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>280</width>
<height>31</height>
</size>
</property>
</spacer>
</item>
<item row="6" column="3">
<item row="6" column="3" colspan="2">
<widget class="QPushButton" name="check_vosk">
<property name="text">
<string>Check configuration</string>
......
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