KTextAddons

voskspeechtotextmodel.cpp
1/*
2 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "voskspeechtotextmodel.h"
8#include "libvoskspeechtotext_debug.h"
9#include <KFormat>
10#include <KLocalizedString>
11#include <QColor>
12#include <QDir>
13
14VoskSpeechToTextModel::VoskSpeechToTextModel(QObject *parent)
15 : QAbstractListModel{parent}
16{
17 updateInstalledLanguage();
18}
19
20VoskSpeechToTextModel::~VoskSpeechToTextModel() = default;
21
22int VoskSpeechToTextModel::rowCount(const QModelIndex &parent) const
23{
24 Q_UNUSED(parent);
25 return mSpeechToTextInfos.count();
26}
27
28QVariant VoskSpeechToTextModel::headerData(int section, Qt::Orientation orientation, int role) const
29{
30 if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
31 switch (static_cast<VoskRoles>(section)) {
32 case VoskRoles::Identifier:
33 case VoskRoles::SizeByte:
34 case VoskRoles::Url:
35 case VoskRoles::Name:
36 case VoskRoles::NeedToUpdateLanguage:
37 case VoskRoles::CheckSum:
38 return {};
39 case VoskRoles::LangText:
40 return i18n("Language");
41 case VoskRoles::Installed:
42 return i18n("Installed");
43 case VoskRoles::InstalledVersion:
44 return i18n("Installed Version");
45 case VoskRoles::AvailableVersion:
46 return i18n("Available Version");
47 case VoskRoles::Size:
48 return i18n("Size");
49 }
50 }
51 return {};
52}
53
54QVariant VoskSpeechToTextModel::data(const QModelIndex &index, int role) const
55{
56 if (index.row() < 0 || index.row() >= mSpeechToTextInfos.count()) {
57 return {};
58 }
59 const auto speechToTextInfo = mSpeechToTextInfos.at(index.row());
60 const int col = index.column();
61 if (role == Qt::BackgroundRole) {
62 if (needToUpdateLanguageModel(speechToTextInfo)) {
63 if (static_cast<VoskRoles>(col) == VoskRoles::InstalledVersion) {
64 return QColor(Qt::red);
65 }
66 }
67 return {};
68 } else if (role == Qt::DisplayRole) {
69 switch (static_cast<VoskRoles>(col)) {
70 case VoskRoles::InstalledVersion: {
71 const QString name{speechToTextInfo.name()};
72 if (isInstalled(name)) {
73 return versionInstalled(name);
74 }
75 return {};
76 }
77 case VoskRoles::AvailableVersion: {
78 return speechToTextInfo.version();
79 }
80 case VoskRoles::NeedToUpdateLanguage: {
81 return needToUpdateLanguageModel(speechToTextInfo);
82 }
83 case VoskRoles::Installed: {
84 if (isInstalled(speechToTextInfo.name())) {
85 return i18n("Installed");
86 }
87 return {};
88 }
89 case VoskRoles::Identifier: {
90 return speechToTextInfo.identifier();
91 }
92 case VoskRoles::LangText: {
93 return speechToTextInfo.langText();
94 }
95 case VoskRoles::Size: {
96 KFormat format;
97 return format.formatByteSize(speechToTextInfo.size());
98 }
99 case VoskRoles::SizeByte: {
100 return speechToTextInfo.size();
101 }
102 case VoskRoles::Url: {
103 return speechToTextInfo.url();
104 }
105 case VoskRoles::CheckSum: {
106 return speechToTextInfo.md5();
107 }
108 case VoskRoles::Name: {
109 return speechToTextInfo.name();
110 }
111 }
112 }
113 return {};
114}
115
116bool VoskSpeechToTextModel::needToUpdateLanguageModel(const VoskSpeechToTextInfo &language) const
117{
118 const QString shortName{language.identifier()};
119 if (isInstalled(shortName)) {
120 if (versionInstalled(shortName) != language.version()) {
121 return true;
122 }
123 }
124 return false;
125}
126
127void VoskSpeechToTextModel::clear()
128{
129 if (rowCount() != 0) {
131 mSpeechToTextInfos.clear();
133 }
134 updateInstalledLanguage();
135}
136
137void VoskSpeechToTextModel::updateInstalledLanguage()
138{
139 mLanguageInstalled = VoskEngineUtils::languageLocallyStored();
140 qCDebug(LIBVOSKSPEECHTOTEXT_LOG) << "mLanguageInstalled " << mLanguageInstalled;
141}
142
143void VoskSpeechToTextModel::removeLanguage(const QString &name)
144{
145 auto index = std::find_if(mLanguageInstalled.begin(), mLanguageInstalled.end(), [name](const VoskEngineUtils::LanguageInstalled &installed) {
146 return (name == installed.name);
147 });
148 if (index != mLanguageInstalled.end()) {
149 const QString absoluteLanguageModelPath = (*index).absoluteLanguageModelPath;
150 if (!QDir(absoluteLanguageModelPath).removeRecursively()) {
151 qCDebug(LIBVOSKSPEECHTOTEXT_LOG) << "Impossible to delete " << absoluteLanguageModelPath;
152 return;
153 }
154 mLanguageInstalled.removeAll(*index);
157 }
158 updateInstalledLanguage();
159}
160
161QVector<VoskSpeechToTextInfo> VoskSpeechToTextModel::speechToTextInfos() const
162{
163 return mSpeechToTextInfos;
164}
165
166void VoskSpeechToTextModel::setSpeechToTextInfos(const QVector<VoskSpeechToTextInfo> &newSpeechToTextInfo)
167{
168 if (rowCount() != 0) {
170 mSpeechToTextInfos.clear();
172 }
173 if (!newSpeechToTextInfo.isEmpty()) {
174 beginInsertRows(QModelIndex(), 0, newSpeechToTextInfo.count() - 1);
175 mSpeechToTextInfos = newSpeechToTextInfo;
177 }
178 updateInstalledLanguage();
179}
180
181int VoskSpeechToTextModel::columnCount(const QModelIndex &parent) const
182{
183 Q_UNUSED(parent)
184 return static_cast<int>(VoskRoles::LastColumn) + 1;
185}
186
187bool VoskSpeechToTextModel::isInstalled(const QString &name) const
188{
189 for (const auto &lang : std::as_const(mLanguageInstalled)) {
190 if (lang.name == name) {
191 return true;
192 }
193 }
194 return false;
195}
196
197QString VoskSpeechToTextModel::versionInstalled(const QString &shortName) const
198{
199 for (const auto &lang : std::as_const(mLanguageInstalled)) {
200 if (lang.name == shortName) {
201 return lang.versionStr;
202 }
203 }
204 return {};
205}
206
207#include "moc_voskspeechtotextmodel.cpp"
QString formatByteSize(double size, int precision=1, KFormat::BinaryUnitDialect dialect=KFormat::DefaultBinaryDialect, KFormat::BinarySizeUnits units=KFormat::DefaultBinaryUnits) const
QString i18n(const char *text, const TYPE &arg...)
QString name(GameStandardAction id)
void beginInsertRows(const QModelIndex &parent, int first, int last)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
qsizetype count() const const
bool isEmpty() const const
int column() const const
int row() const const
QObject * parent() const const
DisplayRole
Orientation
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:29:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.