1 // Copyright (C) 2012-2019 The VPaint Developers.
2 // See the COPYRIGHT file at the top-level directory of this distribution
3 // and at https://github.com/dalboris/vpaint/blob/master/COPYRIGHT
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef TIMELINE_H
18 #define TIMELINE_H
19 
20 #include <QWidget>
21 #include <QDialog>
22 #include <QComboBox>
23 #include <QElapsedTimer>
24 #include <QList>
25 #include <QSet>
26 #include <QColor>
27 #include "TimeDef.h"
28 
29 class QPushButton;
30 class QSpinBox;
31 class QTimer;
32 class QHBoxLayout;
33 class QCheckBox;
34 class View;
35 class Scene;
36 class XmlStreamWriter;
37 class XmlStreamReader;
38 class QAction;
39 
40 // Paint the top timeline bar.
41 // It's a friend of Timeline: can access all members of it
42 class Timeline;
43 class Timeline_HBar: public QWidget
44 {
45 public:
46     Timeline_HBar(Timeline * w);
47 
48 protected:
49     virtual void paintEvent (QPaintEvent * event);
50     virtual void mouseMoveEvent (QMouseEvent * event);
51     virtual void mousePressEvent (QMouseEvent * event);
52     virtual void mouseReleaseEvent (QMouseEvent * event);
53     virtual void leaveEvent (QEvent * event);
54 
55 private:
56     Timeline * w_;
57     bool isScrolling_;
58     int scrollingInitialX_;
59     int scrollingInitialFrame_;
60     int scrollingInitialOffset_;
61 
62     bool hasHighlightedFrame_;
63     int highlightedFrame_;
64 
65     QList<QColor> colors_;
66 };
67 
68 class PlaybackSettings
69 {
70 public:
71     PlaybackSettings();
72     void setDefaultValues(); // Restore default values / Reset
73 
74     enum PlayMode {
75         NORMAL = 0,
76         LOOP,
77         BOUNCE
78     };
79     static QString playModeToString(PlayMode mode);
80     PlayMode stringToPlayMode(const QString & str);
81 
82     int firstFrame() const;
83     int lastFrame() const;
84     int fps() const;
85     PlayMode playMode() const;
86     bool subframeInbetweening() const;
87 
88     void setFirstFrame(int f);
89     void setLastFrame(int f);
90     void setFps(int n) ;
91     void setPlayMode(PlayMode mode);
92     void setSubframeInbetweening(bool b);
93 
94     void read(XmlStreamReader & xml);
95     void write(XmlStreamWriter & xml) const;
96 
97 private:
98     int firstFrame_;
99     int lastFrame_;
100     int fps_;
101     PlayMode playMode_;
102     bool subframeInbetweening_;
103 };
104 
105 class PlaybackSettingsDialog: public QDialog
106 {
107 public:
108     PlaybackSettingsDialog(const PlaybackSettings & settings = PlaybackSettings());
109 
110     PlaybackSettings playbackSettings() const;
111     void setPlaybackSettings(const PlaybackSettings & settings);
112 
113 private:
114     mutable PlaybackSettings settings_;
115 
116     QSpinBox * fpsSpinBox_;
117     QCheckBox * subframeCheckBox_;
118     QComboBox * playModeSpinBox_;
119 };
120 
121 
122 class Timeline : public QWidget
123 {
124     Q_OBJECT
125 
126 public:
127     Timeline(Scene * scene, QWidget *parent = 0);
128     ~Timeline();
129 
130     void read(XmlStreamReader & xml);
131     void write(XmlStreamWriter & xml) const;
132 
133     // Set info about the selected cell
134     void setSelectionType(int type); // 0 for no selection; 1 for at least one key cells; 2 for only inbetween cells
135     void setT(double t);
136     void setT1(double t1);
137     void setT2(double t2);
138 
139     // Set which View's time this timeline control
140     void addView(View * view);
141     void removeView(View * view);
142 
143     // Get playback settings
144     int firstFrame() const;
145     int lastFrame() const;
146     int fps() const;
147     PlaybackSettings::PlayMode playMode() const;
148     bool subframeInbetweening() const;
149 
150     // Current state
151     bool isPlaying() const;
152     QSet<View*> playedViews() const;
153 
154     // Visualization
155     int firstVisibleFrame() const;
156     int lastVisibleFrame() const;
157 
158     QAction * actionGoToFirstFrame() const;
159     QAction * actionGoToPreviousFrame() const;
160     QAction * actionPlayPause() const;
161     QAction * actionGoToNextFrame() const;
162     QAction * actionGoToLastFrame() const;
163 
164 public slots:
165     void play();
166     void pause();
167     void playPause();
168 
169     void openPlaybackSettingsDialog();
170 
171     void goToFirstFrame();
172     void goToPreviousFrame();
173     void goToNextFrame();
174     void goToLastFrame();
175 
176     void setFirstFrame(int firstFrame);
177     void setLastFrame(int lastFrame);
178     void setFps(int fps);
179     void realTimePlayingChanged();
180 
181 private slots:
182     void goToFirstFrame(View * view);
183     void goToPreviousFrame(View * view);
184     void goToNextFrame(View * view);
185     void goToLastFrame(View * view);
186     void goToFrame(View * view, int frame);
187     void goToFrame(View * view, double frame);
188 
189     void timerTimeout();
190     void roundPlayedViews();
191 
192 signals:
193     void timeChanged();
194     void playingWindowChanged();
195 
196 protected:
197     void paintEvent(QPaintEvent * event);
198 
199 private:
200     // Selected cell info
201     int selectionType_; // 0 for no selection; 1 for at least one key cells; 2 for only inbetween cells
202     double t_;
203     double t1_;
204     double t2_;
205 
206     // Linked scene
207     Scene * scene_;
208 
209     // The views whose times are controlled by this timeline
210     QList<View*> views_;
211     QSet<View*> playedViews_;
212 
213     // Delegate timeline painting and mouse events handling
214     friend class Timeline_HBar;
215     Timeline_HBar * hbar_;
216 
217     // Time control
218     QTimer * timer_;
219     QElapsedTimer elapsedTimer_;
220 
221     // Actions
222     QAction * actionGoToFirstFrame_;
223     QAction * actionGoToPreviousFrame_;
224     QAction * actionPlayPause_;
225     QAction * actionGoToNextFrame_;
226     QAction * actionGoToLastFrame_;
227 
228     // playing properties
229     QPushButton * firstFrameButton_;
230     QPushButton * previousKeyFrameButton_;
231     QPushButton * previousFrameButton_;
232     QPushButton * playPauseButton_;
233     QPushButton * nextFrameButton_;
234     QPushButton * nextKeyFrameButton_;
235     QPushButton * lastFrameButton_;
236     bool isPlaying_;
237 
238     // Settings
239     PlaybackSettings settings_;
240 
241     // Playing window
242     QSpinBox * firstFrameSpinBox_;
243     QSpinBox * lastFrameSpinBox_;
244 
245     // Implementation detail of bounce playback mode
246     bool playingDirection_;
247 
248     // visible window
249     int firstVisibleFrame_;
250     int lastVisibleFrame_;
251     int totalPixelOffset_;
252 };
253 
254 
255 
256 #endif
257