feat: show only the translated subtitle once translation finishes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
triandClaude Opus 5.5 committed 2026-10-28 00:17:34 +07:00
1 parent 6b5742b2ca
commit 169cb1360b
5 files changed
+22 -15

No files matched your search

+13 -7
View File
@@ -194,7 +194,7 @@ void MpvItem::addSubtitleFile(const QUrl &url)
commandAsync(QStringList{QStringLiteral("sub-add"), localPathOrUrl(url), QStringLiteral("select")}); commandAsync(QStringList{QStringLiteral("sub-add"), localPathOrUrl(url), QStringLiteral("select")});
} }
void MpvItem::showSecondarySubtitleFile(const QUrl &url, const QString &title) void MpvItem::showTranslatedSubtitleFile(const QUrl &url, const QString &title)
{ {
if (!url.isValid()) { if (!url.isValid()) {
return; return;
@@ -203,15 +203,21 @@ void MpvItem::showSecondarySubtitleFile(const QUrl &url, const QString &title)
for (const QVariant &entry : std::as_const(m_subtitleTracks)) { for (const QVariant &entry : std::as_const(m_subtitleTracks)) {
const QVariantMap track = entry.toMap(); const QVariantMap track = entry.toMap();
if (track.value(QStringLiteral("externalFilename")).toString() == path) { if (track.value(QStringLiteral("externalFilename")).toString() == path) {
setSecondarySubtitleId(track.value(QStringLiteral("id")).toInt()); showOnlySubtitle(track.value(QStringLiteral("id")).toInt());
return; return;
} }
} }
// "auto" adds the track without selecting it; updateTracks() selects it as secondary. // "auto" adds the track without selecting it; updateTracks() selects it once mpv lists it.
m_pendingSecondaryPath = path; m_pendingTranslatedPath = path;
commandAsync(QStringList{QStringLiteral("sub-add"), path, QStringLiteral("auto"), title}); commandAsync(QStringList{QStringLiteral("sub-add"), path, QStringLiteral("auto"), title});
} }
void MpvItem::showOnlySubtitle(int id)
{
setSubtitleId(id);
setSecondarySubtitleId(0);
}
void MpvItem::showText(const QString &text) void MpvItem::showText(const QString &text)
{ {
commandAsync(QStringList{QStringLiteral("show-text"), text}); commandAsync(QStringList{QStringLiteral("show-text"), text});
@@ -330,9 +336,9 @@ void MpvItem::updateTracks(const QVariantList &trackList)
{QStringLiteral("ffIndex"), track.value(QStringLiteral("ff-index"), -1).toInt()}, {QStringLiteral("ffIndex"), track.value(QStringLiteral("ff-index"), -1).toInt()},
}); });
if (!m_pendingSecondaryPath.isEmpty() && track.value(QStringLiteral("external-filename")).toString() == m_pendingSecondaryPath) { if (!m_pendingTranslatedPath.isEmpty() && track.value(QStringLiteral("external-filename")).toString() == m_pendingTranslatedPath) {
m_pendingSecondaryPath.clear(); m_pendingTranslatedPath.clear();
setSecondarySubtitleId(id); showOnlySubtitle(id);
} }
} }
+6 -5
View File
@@ -66,9 +66,9 @@ public:
Q_INVOKABLE void seek(double seconds); Q_INVOKABLE void seek(double seconds);
// Adds an external subtitle file and makes it the primary subtitle. // Adds an external subtitle file and makes it the primary subtitle.
Q_INVOKABLE void addSubtitleFile(const QUrl &url); Q_INVOKABLE void addSubtitleFile(const QUrl &url);
// Shows \a url as the secondary subtitle, adding it first (named \a title) unless it // Makes \a url the only subtitle shown, adding it first (named \a title) unless it is
// is already loaded. // already loaded.
Q_INVOKABLE void showSecondarySubtitleFile(const QUrl &url, const QString &title); Q_INVOKABLE void showTranslatedSubtitleFile(const QUrl &url, const QString &title);
// Shows a short message on mpv's on-screen display. // Shows a short message on mpv's on-screen display.
Q_INVOKABLE void showText(const QString &text); Q_INVOKABLE void showText(const QString &text);
@@ -90,6 +90,7 @@ private:
void setFileLoaded(bool loaded); void setFileLoaded(bool loaded);
void updateTracks(const QVariantList &trackList); void updateTracks(const QVariantList &trackList);
void applyUserConfig(); void applyUserConfig();
void showOnlySubtitle(int id);
bool m_ready = false; bool m_ready = false;
QUrl m_pendingUrl; QUrl m_pendingUrl;
@@ -103,8 +104,8 @@ private:
int m_subtitleId = 0; int m_subtitleId = 0;
int m_secondarySubtitleId = 0; int m_secondarySubtitleId = 0;
double m_subtitleDelay = 0.0; double m_subtitleDelay = 0.0;
// File added by showSecondarySubtitleFile() that should become secondary once mpv lists it. // File added by showTranslatedSubtitleFile() that should be shown once mpv lists it.
QString m_pendingSecondaryPath; QString m_pendingTranslatedPath;
bool m_loadUserConfig = false; bool m_loadUserConfig = false;
QStringList m_loadedUserConfigFiles; QStringList m_loadedUserConfigFiles;
QStringList m_skippedUserOptions; QStringList m_skippedUserOptions;
+1 -1
View File
@@ -141,7 +141,7 @@ Kirigami.Page {
TranslationService { TranslationService {
id: translator id: translator
onTranslated: (subtitleUrl, fromCache) => { onTranslated: (subtitleUrl, fromCache) => {
mpv.showSecondarySubtitleFile(subtitleUrl, mpv.showTranslatedSubtitleFile(subtitleUrl,
KI18n.i18nc("@item:inlistbox subtitle track, %1 is a language", "%1 (translated)", QtPotatoSettings.targetLanguage)) KI18n.i18nc("@item:inlistbox subtitle track, %1 is a language", "%1 (translated)", QtPotatoSettings.targetLanguage))
mpv.showText(fromCache mpv.showText(fromCache
? KI18n.i18nc("@info:osd", "Loaded the saved translation") ? KI18n.i18nc("@info:osd", "Loaded the saved translation")
+1 -1
View File
@@ -116,7 +116,7 @@ QQC2.Popup {
text: KI18n.i18nc("@action:button", "Translate Subtitle into %1", QtPotatoSettings.targetLanguage) text: KI18n.i18nc("@action:button", "Translate Subtitle into %1", QtPotatoSettings.targetLanguage)
onClicked: popup.translator.translate(popup.mpv.currentUrl, popup.currentTrack, popup.mpv.mediaTitle) onClicked: popup.translator.translate(popup.mpv.currentUrl, popup.currentTrack, popup.mpv.mediaTitle)
QQC2.ToolTip.text: KI18n.i18nc("@info:tooltip", "Translates the subtitle chosen above and shows the result as the secondary subtitle") QQC2.ToolTip.text: KI18n.i18nc("@info:tooltip", "Translates the subtitle chosen above and shows the result in its place")
QQC2.ToolTip.visible: hovered QQC2.ToolTip.visible: hovered
QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay
} }
+1 -1
View File
@@ -17,7 +17,7 @@
class QNetworkAccessManager; class QNetworkAccessManager;
// Translates a whole subtitle track with an LLM, batch by batch, and caches the result // Translates a whole subtitle track with an LLM, batch by batch, and caches the result
// as an .srt file that mpv can load as the secondary subtitle. // as an .srt file that mpv can load as a subtitle track.
class TranslationService : public QObject class TranslationService : public QObject
{ {
Q_OBJECT Q_OBJECT