1 /*
2     SPDX-FileCopyrightText: 2007-2009 Sergio Pistone <sergio_pistone@yahoo.com.ar>
3     SPDX-FileCopyrightText: 2010-2018 Mladen Milinkovic <max@smoothware.net>
4 
5     SPDX-License-Identifier: GPL-2.0-or-later
6 */
7 
8 #ifndef USERACTION_H
9 #define USERACTION_H
10 
11 #include <QList>
12 
13 #include <QAction>
14 #include <QExplicitlySharedDataPointer>
15 
16 namespace SubtitleComposer {
17 class Subtitle;
18 class LinesWidget;
19 class CurrentLineWidget;
20 
21 class UserAction : public QObject
22 {
23 	Q_OBJECT
24 
25 public:
26 	typedef enum {
27 		SubClosed        = 0x1,	// Subtitle is not opened
28 		SubOpened        = 0x2,	// Subtitle is opened
29 		SubTrClosed      = 0x4,	// Subtitle is not opened
30 		SubTrOpened      = 0x8,	// Subtitle is opened
31 		SubHasLine       = 0x10,	// Subtitle opened with, at least, one line
32 		SubHasLines      = 0x20,	// Subtitle opened with, at least, two lines
33 		SubPDirty        = 0x40,	// Subtitle opened and has unsaved changes
34 		SubPClean        = 0x80,	// Subtitle opened or closed without unsaved changes
35 		SubSDirty        = 0x100,	// Subtitle opened and has unsaved changes
36 		SubSClean        = 0x200,	// Subtitle opened or closed without unsaved changes
37 		HasSelection     = 0x400,	// Subtitle opened with, at least, one line selected
38 		VideoClosed      = 0x800,
39 		VideoOpened      = 0x1000,
40 		VideoStopped     = 0x2000,
41 		VideoPlaying     = 0x4000,
42 		FullScreenOn     = 0x8000,
43 		FullScreenOff    = 0x10000,
44 		AnchorsNone      = 0x20000,	// None of the subtitles is anchored
45 		AnchorsSome      = 0x40000,	// At least one subtitle is anchored
46 		EditableShowTime = 0x80000,	// Selected line's show time is editable
47 
48 		AnchorsMask = AnchorsNone | AnchorsSome | EditableShowTime,
49 		SubtitleMask = SubClosed | SubOpened | SubTrClosed | SubTrOpened | SubPDirty | SubPClean | SubSDirty | SubSClean | SubHasLine | SubHasLines | AnchorsMask,
50 		SelectionMask = HasSelection,
51 		VideoMask = VideoClosed | VideoOpened | VideoStopped | VideoPlaying,
52 		FullScreenMask = FullScreenOn | FullScreenOff,
53 		AllMask = AnchorsMask | SubtitleMask | SelectionMask | VideoMask | FullScreenMask
54 	} EnableFlag;
55 
56 	explicit UserAction(QAction *action, int enableFlags = SubOpened);
57 
58 	QAction * action();
59 	int enableFlags();
60 
61 	bool isEnabled();
62 	void setActionEnabled(bool enabled);
63 	void setContextFlags(int contextFlags);
64 
65 private:
66 	void updateEnabledState();
67 
68 private slots:
69 	void onActionChanged();
70 
71 private:
72 	QAction *m_action;
73 	int m_enableFlags;
74 
75 	bool m_actionEnabled;
76 	bool m_contextEnabled;
77 
78 	bool m_ignoreActionEnabledSignal;
79 };
80 
81 class UserActionManager : public QObject
82 {
83 	Q_OBJECT
84 
85 public:
86 	static UserActionManager * instance();
87 
88 	void addAction(QAction *action, int enableFlags = UserAction::SubOpened);
89 	void addAction(UserAction *actionSpec);
90 	void removeAction(UserAction *actionSpec);
91 
92 public slots:
93 	void setSubtitle(const Subtitle *subtitle = nullptr);
94 	void setLinesWidget(LinesWidget *linesWidget = nullptr);
95 	void setTranslationMode(bool translationMode);
96 	void setFullScreenMode(bool fullScreenMode);
97 
98 private:
99 	UserActionManager();
100 
101 	void updateActionsContext(int contextFlags);
102 
103 private slots:
104 	void onSubtitleLinesChanged();
105 	void onPrimaryDirtyStateChanged(bool dirty);
106 	void onSecondaryDirtyStateChanged(bool dirty);
107 	void onLinesWidgetSelectionChanged();
108 	void onPlayerStateChanged();
109 	void onSubtitleAnchorsChanged();
110 
111 private:
112 	QList<UserAction *> m_actionSpecs;
113 
114 	QExplicitlySharedDataPointer<const Subtitle> m_subtitle;
115 	const LinesWidget *m_linesWidget;
116 	bool m_translationMode;
117 
118 	int m_contextFlags;
119 };
120 }
121 #endif
122