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

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 #ifndef APPLICATION_H
0009 #define APPLICATION_H
0010 
0011 #include "mainwindow.h"
0012 #include "core/subtitle.h"
0013 #include "formats/format.h"
0014 #include "gui/treeview/lineswidget.h"
0015 #include "scconfig.h"
0016 
0017 #include <QApplication>
0018 #include <QAction>
0019 #include <QExplicitlySharedDataPointer>
0020 #include <QMap>
0021 #include <QString>
0022 #include <QKeySequence>
0023 #include <QUrl>
0024 
0025 #include <sonnet/configwidget.h>
0026 
0027 #include <KActionCollection>
0028 
0029 QT_FORWARD_DECLARE_CLASS(QAction)
0030 QT_FORWARD_DECLARE_CLASS(QUndoStack)
0031 QT_FORWARD_DECLARE_CLASS(QLabel)
0032 QT_FORWARD_DECLARE_CLASS(QTextCodec)
0033 
0034 class KComboBox;
0035 class KToggleAction;
0036 class KRecentFilesAction;
0037 class KRecentFilesActionExt;
0038 class KCodecActionExt;
0039 
0040 namespace SubtitleComposer {
0041 class VideoPlayer;
0042 class TextDemux;
0043 class SpeechProcessor;
0044 
0045 class PlayerWidget;
0046 class CurrentLineWidget;
0047 
0048 class ConfigDialog;
0049 
0050 class Finder;
0051 class Replacer;
0052 class ErrorFinder;
0053 class Speller;
0054 class ErrorTracker;
0055 
0056 class ScriptsManager;
0057 
0058 class UndoStack;
0059 
0060 class Application : public QApplication
0061 {
0062     Q_OBJECT
0063 
0064 public:
0065     Application(int &argc, char **argv);
0066     void createMainWindow();
0067     virtual ~Application();
0068 
0069     inline MainWindow * mainWindow() const { return m_mainWindow; }
0070     inline LinesWidget * linesWidget() const { return m_mainWindow->m_linesWidget; }
0071     inline const SpeechProcessor * speechProcessor() const { return m_speechProcessor; }
0072 
0073     inline bool translationMode() const { return m_translationMode; }
0074     inline bool showingLinesContextMenu() const { return m_mainWindow->m_linesWidget->showingContextMenu(); }
0075 
0076     void loadConfig();
0077     void saveConfig();
0078 
0079     inline QAction * action(const char *actionName) const {
0080         return m_mainWindow->actionCollection()->action(actionName);
0081     }
0082 
0083     /**
0084      * @brief triggerAction
0085      * @param keySequence
0086      * @return true if an action was triggered
0087      */
0088     bool triggerAction(const QKeySequence &keySequence);
0089 
0090     inline const QUrl & lastSubtitleDirectory() const { return m_lastSubtitleUrl; }
0091 
0092     const QStringList & availableEncodingNames() const;
0093     static const QString & buildSubtitleFilesFilter(bool openFileFilter = true);
0094     static const QString & buildMediaFilesFilter();
0095 
0096 public slots:
0097     void newSubtitle();
0098     void openSubtitle();
0099     void reopenSubtitleWithCodec(QTextCodec *codec = nullptr);
0100     void demuxTextStream(int textStreamIndex);
0101     void openSubtitle(const QUrl &url, bool warnClashingUrls = true);
0102     bool saveSubtitle(QTextCodec *codec = nullptr);
0103     bool saveSubtitleAs(QTextCodec *codec = nullptr);
0104     bool closeSubtitle();
0105 
0106     void speechImportAudioStream(int audioStreamIndex);
0107 
0108     void newSubtitleTr();
0109     void openSubtitleTr();
0110     void reopenSubtitleTrWithCodec(QTextCodec *codec = nullptr);
0111 
0112     void openSubtitleTr(const QUrl &url, bool warnClashingUrls = true);
0113     bool saveSubtitleTr(QTextCodec *codec = nullptr);
0114     bool saveSubtitleTrAs(QTextCodec *codec = nullptr);
0115     bool closeSubtitleTr();
0116 
0117     void joinSubtitles();
0118     void splitSubtitle();
0119 
0120     void insertBeforeCurrentLine();
0121     void insertAfterCurrentLine();
0122     void removeSelectedLines();
0123 
0124     void joinSelectedLines();
0125     void splitSelectedLines();
0126 
0127     void selectAllLines();
0128     void gotoLine();
0129 
0130     void find();
0131     void findNext();
0132     void findPrevious();
0133     void replace();
0134 
0135     void spellCheck();
0136 
0137     void detectErrors();
0138     void clearErrors();
0139     void selectNextError();
0140     void selectPreviousError();
0141 
0142     void retrocedeCurrentLine();
0143     void advanceCurrentLine();
0144 
0145     void toggleSelectedLinesMark();
0146     void toggleSelectedLinesBold();
0147     void toggleSelectedLinesItalic();
0148     void toggleSelectedLinesUnderline();
0149     void toggleSelectedLinesStrikeThrough();
0150     void changeSelectedLinesColor();
0151 
0152     void shiftLines();
0153     void shiftSelectedLinesForwards();
0154     void shiftSelectedLinesBackwards();
0155     void adjustLines();
0156 
0157     void changeFrameRate();
0158     void enforceDurationLimits();
0159     void setAutoDurations();
0160     void maximizeDurations();
0161     void fixOverlappingLines();
0162     void syncWithSubtitle();
0163 
0164     void breakLines();
0165     void unbreakTexts();
0166     void simplifySpaces();
0167     void changeCase();
0168     void fixPunctuation();
0169 
0170     void openVideo();
0171     void openVideo(const QUrl &url);
0172 
0173     void toggleFullScreenMode();
0174     void setFullScreenMode(bool enabled);
0175 
0176     void seekBackward();
0177     void seekForward();
0178     void stepBackward();
0179     void stepForward();
0180     void playOnlyCurrentLine();
0181     void seekToPrevLine();
0182     void seekToCurrentLine();
0183     void seekToNextLine();
0184 
0185     void playrateIncrease();
0186     void playrateDecrease();
0187 
0188     void setCurrentLineShowTimeFromVideo();
0189     void setCurrentLineHideTimeFromVideo();
0190 
0191     void anchorToggle();
0192     void anchorRemoveAll();
0193 
0194     void shiftToVideoPosition();
0195 
0196 signals:
0197     void subtitleOpened(Subtitle *subtitle);
0198     void subtitleClosed();
0199 
0200     void translationModeChanged(bool value);
0201     void fullScreenModeChanged(bool value);
0202 
0203     void actionsReady();
0204 
0205 private:
0206     void processSubtitleOpened(QTextCodec *codec, const QString &subtitleFormat);
0207     void processTranslationOpened(QTextCodec *codec, const QString &subtitleFormat);
0208 
0209     QTextCodec * codecForEncoding(const QString &encoding);
0210 
0211     bool acceptClashingUrls(const QUrl &subtitleUrl, const QUrl &subtitleTrUrl);
0212 
0213     QUrl saveSplitSubtitle(const Subtitle &subtitle, const QUrl &srcUrl, QString encoding, QString format, bool primary);
0214 
0215     void setupActions();
0216 
0217     Time videoPosition(bool compensate = false);
0218 
0219     void updateActionTexts();
0220 
0221 private slots:
0222     void updateTitle();
0223 
0224     void onWaveformDoubleClicked(Time time);
0225     void onWaveformMiddleMouse(Time time);
0226 
0227     void onLineDoubleClicked(SubtitleLine *line);
0228     void onHighlightLine(SubtitleLine *line, bool primary = true, int firstIndex = -1, int lastIndex = -1);
0229     void onPlayingLineChanged(SubtitleLine *line);
0230     void onLinkCurrentLineToVideoToggled(bool value);
0231 
0232     void onPlayerFileOpened(const QString &filePath);
0233     void onPlayerPlaying();
0234     void onPlayerPaused();
0235     void onPlayerStopped();
0236     void onPlayerTextStreamsChanged(const QStringList &textStreams);
0237     void onPlayerAudioStreamsChanged(const QStringList &audioStreams);
0238     void onPlayerActiveAudioStreamChanged(int audioStream);
0239     void onPlayerMuteChanged(bool muted);
0240 
0241     void onConfigChanged();
0242 
0243     void setActiveSubtitleStream(int subtitleStream);
0244 
0245     void showPreferences();
0246 
0247 private:
0248     QUrl m_subtitleUrl;
0249     QString m_subtitleFileName;
0250     QString m_subtitleEncoding;
0251     QString m_subtitleFormat;
0252 
0253     bool m_translationMode;
0254     QUrl m_subtitleTrUrl;
0255     QString m_subtitleTrFileName;
0256     QString m_subtitleTrEncoding;
0257     QString m_subtitleTrFormat;
0258 
0259     TextDemux *m_textDemux;
0260     SpeechProcessor *m_speechProcessor;
0261 
0262     SubtitleLine *m_lastFoundLine;
0263 
0264     MainWindow *m_mainWindow;
0265 
0266     QUrl m_lastSubtitleUrl;
0267     KRecentFilesActionExt *m_recentSubtitlesAction;
0268     KRecentFilesActionExt *m_recentSubtitlesTrAction;
0269 
0270     KCodecActionExt *m_reopenSubtitleAsAction;
0271     KCodecActionExt *m_saveSubtitleAsAction;
0272     KCodecActionExt *m_reopenSubtitleTrAsAction;
0273     KCodecActionExt *m_saveSubtitleTrAsAction;
0274 
0275     Finder *m_finder;
0276     Replacer *m_replacer;
0277     ErrorFinder *m_errorFinder;
0278     Speller *m_speller;
0279 
0280     ErrorTracker *m_errorTracker;
0281 
0282     QLabel *m_labSubFormat = nullptr;
0283     QLabel *m_labSubEncoding = nullptr;
0284 
0285     ScriptsManager *m_scriptsManager;
0286 
0287     QUrl m_lastVideoUrl;
0288     bool m_linkCurrentLineToPosition;
0289     KRecentFilesAction *m_recentVideosAction;
0290 };
0291 
0292 }
0293 
0294 #endif