File indexing completed on 2024-12-22 04:40:00

0001 /*
0002     SPDX-FileCopyrightText: 2007-2009 Sergio Pistone <sergio_pistone@yahoo.com.ar>
0003     SPDX-FileCopyrightText: 2010-2022 Mladen Milinkovic <max@smoothware.net>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "kcodecactionext.h"
0009 
0010 #include <QMenu>
0011 #include <QStringBuilder>
0012 #include <QTextCodec>
0013 #include <QVariant>
0014 
0015 #include <KActionCollection>
0016 #include <KCharsets>
0017 #include <KEncodingProber>
0018 #include <KLocalizedString>
0019 
0020 #include "application.h"
0021 #include "scconfig.h"
0022 
0023 void
0024 KCodecActionExt::init()
0025 {
0026     setToolBarMode(MenuMode);
0027 
0028     if(m_mode == Save) {
0029         m_defaultCodecAction = addAction(i18nc("Encodings menu", "Default: %1", SCConfig::defaultSubtitlesEncoding()));
0030         m_defaultCodecAction->setCheckable(false);
0031         connect(m_defaultCodecAction, &QAction::triggered, this, [&](bool){
0032             emit triggered(QTextCodec::codecForName(SCConfig::defaultSubtitlesEncoding().toUtf8()));
0033         });
0034 
0035         m_currentCodecAction = addAction(i18nc("Encodings menu", "Current: %1", SCConfig::defaultSubtitlesEncoding()));
0036         m_currentCodecAction->setCheckable(false);
0037         connect(m_currentCodecAction, &QAction::triggered, this, [&](bool){
0038             emit triggered(nullptr);
0039         });
0040     } else {
0041         m_autodetectAction = addAction(i18nc("Encodings menu", "Autodetect"));
0042         m_autodetectAction->setCheckable(false);
0043         connect(m_autodetectAction, &QAction::triggered, this, [&](bool){
0044             emit triggered(nullptr);
0045         });
0046     }
0047 
0048     menu()->addSeparator();
0049 
0050     const auto encodings = KCharsets::charsets()->encodingsByScript();
0051     for(const QStringList &encodingsForScript: encodings) {
0052         KSelectAction *group = new KSelectAction(encodingsForScript.at(0), this);
0053         for(int i = 1; i < encodingsForScript.size(); ++i)
0054             group->addAction(encodingsForScript.at(i).toUpper())->setCheckable(m_mode == Open);
0055 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
0056         connect(group, &KSelectAction::actionTriggered, this, [=](QAction *a){
0057 #else
0058         connect(group, QOverload<QAction *>::of(&KSelectAction::triggered), this, [=](QAction *a){
0059 #endif
0060             emit triggered(QTextCodec::codecForName(a->text().toUtf8()));
0061         });
0062         group->setCheckable(m_mode == Open);
0063         addAction(group);
0064     }
0065 }
0066 
0067 bool
0068 KCodecActionExt::setCurrentCodec(QTextCodec *codec)
0069 {
0070     if(!codec)
0071         codec = QTextCodec::codecForName(SCConfig::defaultSubtitlesEncoding().toUtf8());
0072 
0073     for(int i = 0, m = actions().size(); i < m; i++) {
0074         KSelectAction *menuAction = qobject_cast<KSelectAction *>(actions().at(i));
0075         if(!menuAction)
0076             continue;
0077         const QMenu *menu = menuAction->menu();
0078         for(int j = 0, n = menu->actions().size(); j < n; j++) {
0079             QAction *action = menu->actions().at(j);
0080             if(codec == QTextCodec::codecForName(action->text().toUtf8())) {
0081                 if(m_mode == Save)
0082                     m_currentCodecAction->setText(i18nc("Encodings menu", "Current: %1", action->text()));
0083                 if(action->isCheckable()) {
0084                     setCurrentAction(menuAction);
0085                     menuAction->setCurrentAction(action);
0086                 }
0087                 return true;
0088             }
0089         }
0090     }
0091     return false;
0092 }
0093 
0094 KCodecActionExt::KCodecActionExt(QObject *parent, Mode mode)
0095     : KSelectAction(parent),
0096       m_mode(mode)
0097 {
0098     init();
0099 }
0100 
0101 KCodecActionExt::KCodecActionExt(const QString &text, QObject *parent, Mode mode)
0102     : KSelectAction(text, parent),
0103       m_mode(mode)
0104 {
0105     init();
0106 }
0107 
0108 KCodecActionExt::KCodecActionExt(const QIcon &icon, const QString &text, QObject *parent, Mode mode)
0109     : KSelectAction(icon, text, parent),
0110       m_mode(mode)
0111 {
0112     init();
0113 }