1 /*
2  * Copyright (c) 2016-2021 Meltytech, LLC
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "shotcut_mlt_properties.h"
19 #include "countproducerwidget.h"
20 #include "ui_countproducerwidget.h"
21 #include "util.h"
22 #include "mltcontroller.h"
23 #include <MltProfile.h>
24 
setLength(Mlt::Properties * p,int length)25 void setLength(Mlt::Properties* p, int length)
26 {
27     p->set("length", p->frames_to_time(length, mlt_time_clock));
28     p->set("out", length - 1);
29     p->set("in", 0);
30 }
31 
CountProducerWidget(QWidget * parent)32 CountProducerWidget::CountProducerWidget(QWidget *parent) :
33     QWidget(parent),
34     ui(new Ui::CountProducerWidget)
35 {
36     ui->setupUi(this);
37     Util::setColorsToHighlight(ui->nameLabel);
38 
39     ui->directionCombo->addItem(tr("Down"), QVariant("down"));
40     ui->directionCombo->addItem(tr("Up"), QVariant("up"));
41     ui->directionCombo->setCurrentIndex(0);
42 
43     ui->styleCombo->addItem(tr("Seconds"), QVariant("seconds"));
44     ui->styleCombo->addItem(tr("Seconds + 1"), QVariant("seconds+1"));
45     ui->styleCombo->addItem(tr("Frames"), QVariant("frames"));
46     ui->styleCombo->addItem(tr("Timecode"), QVariant("timecode"));
47     ui->styleCombo->addItem(tr("Clock"), QVariant("clock"));
48     ui->styleCombo->setCurrentIndex(0);
49 
50     ui->soundCombo->addItem(tr("2-Pop"), QVariant("2pop"));
51     ui->soundCombo->addItem(tr("Silent"), QVariant("silent"));
52     ui->soundCombo->addItem(tr("Frame 0"), QVariant("frame0"));
53     ui->soundCombo->setCurrentIndex(0);
54 
55     ui->backgroundCombo->addItem(tr("Clock"), QVariant("clock"));
56     ui->backgroundCombo->addItem(tr("None"), QVariant("none"));
57     ui->backgroundCombo->setCurrentIndex(0);
58 
59     ui->dropCheckBox->setChecked(true);
60 
61     ui->durationSpinBox->setValue(qRound(MLT.profile().fps() * 10.0)); // 10 seconds
62 
63     ui->preset->saveDefaultPreset(getPreset());
64     ui->preset->loadPresets();
65 }
66 
~CountProducerWidget()67 CountProducerWidget::~CountProducerWidget()
68 {
69     delete ui;
70 }
71 
currentDirection() const72 QString CountProducerWidget::currentDirection() const
73 {
74     return ui->directionCombo->itemData(ui->directionCombo->currentIndex()).toString();
75 }
76 
currentStyle() const77 QString CountProducerWidget::currentStyle() const
78 {
79     return ui->styleCombo->itemData(ui->styleCombo->currentIndex()).toString();
80 }
81 
currentSound() const82 QString CountProducerWidget::currentSound() const
83 {
84     return ui->soundCombo->itemData(ui->soundCombo->currentIndex()).toString();
85 }
86 
currentBackground() const87 QString CountProducerWidget::currentBackground() const
88 {
89     return ui->backgroundCombo->itemData(ui->backgroundCombo->currentIndex()).toString();
90 }
91 
newProducer(Mlt::Profile & profile)92 Mlt::Producer* CountProducerWidget::newProducer(Mlt::Profile& profile)
93 {
94     Mlt::Producer* p = new Mlt::Producer(profile, "count:");
95     p->set("direction", currentDirection().toLatin1().constData());
96     p->set("style", currentStyle().toLatin1().constData());
97     p->set("sound", currentSound().toLatin1().constData());
98     p->set("background", currentBackground().toLatin1().constData());
99     p->set("drop", ui->dropCheckBox->isChecked());
100     setLength(p, ui->durationSpinBox->value());
101     p->set(kShotcutCaptionProperty, ui->nameLabel->text().toUtf8().constData());
102     p->set(kShotcutDetailProperty, detail().toUtf8().constData());
103     return p;
104 }
105 
getPreset() const106 Mlt::Properties CountProducerWidget::getPreset() const
107 {
108     Mlt::Properties p;
109     p.set("direction", currentDirection().toLatin1().constData());
110     p.set("style", currentStyle().toLatin1().constData());
111     p.set("sound", currentSound().toLatin1().constData());
112     p.set("background", currentBackground().toLatin1().constData());
113     p.set("drop", ui->dropCheckBox->isChecked());
114     setLength(&p, ui->durationSpinBox->value());
115     return p;
116 }
117 
loadPreset(Mlt::Properties & p)118 void CountProducerWidget::loadPreset(Mlt::Properties& p)
119 {
120     if (!p.get("direction") || !p.get("style")) return;
121     int index = -1;
122 
123     index = ui->directionCombo->findData(QVariant(p.get("direction")));
124     ui->directionCombo->setCurrentIndex(index);
125 
126     index = ui->styleCombo->findData(QVariant(p.get("style")));
127     ui->styleCombo->setCurrentIndex(index);
128 
129     index = ui->soundCombo->findData(QVariant(p.get("sound")));
130     ui->soundCombo->setCurrentIndex(index);
131 
132     index = ui->backgroundCombo->findData(QVariant(p.get("background")));
133     ui->backgroundCombo->setCurrentIndex(index);
134 
135     ui->dropCheckBox->setChecked(p.get("drop"));
136 
137     ui->durationSpinBox->setValue(p.get_int("length"));
138 
139     if (m_producer) {
140         m_producer->set("direction", p.get("direction"));
141         m_producer->set("style", p.get("style"));
142         m_producer->set("sound", p.get("sound"));
143         m_producer->set("background", p.get("background"));
144         m_producer->set("drop", p.get("drop"));
145         setLength(producer(), ui->durationSpinBox->value());
146         m_producer->set(kShotcutDetailProperty, detail().toUtf8().constData());
147         emit producerChanged(producer());
148     }
149 }
150 
on_directionCombo_activated(int)151 void CountProducerWidget::on_directionCombo_activated(int /*index*/)
152 {
153     if (m_producer) {
154         m_producer->set("direction", currentDirection().toLatin1().constData());
155         m_producer->set(kShotcutDetailProperty, detail().toUtf8().constData());
156         emit producerChanged(producer());
157     }
158 }
159 
on_styleCombo_activated(int)160 void CountProducerWidget::on_styleCombo_activated(int /*index*/)
161 {
162     if (m_producer) {
163         m_producer->set("style", currentStyle().toLatin1().constData());
164         m_producer->set(kShotcutDetailProperty, detail().toUtf8().constData());
165         emit producerChanged(producer());
166     }
167 }
168 
on_soundCombo_activated(int)169 void CountProducerWidget::on_soundCombo_activated(int /*index*/)
170 {
171     if (m_producer) {
172         m_producer->set("sound", currentSound().toLatin1().constData());
173         emit producerChanged(producer());
174     }
175 }
176 
on_backgroundCombo_activated(int)177 void CountProducerWidget::on_backgroundCombo_activated(int /*index*/)
178 {
179     if (m_producer) {
180         m_producer->set("background", currentBackground().toLatin1().constData());
181         emit producerChanged(producer());
182     }
183 }
184 
on_dropCheckBox_clicked(bool checked)185 void CountProducerWidget::on_dropCheckBox_clicked(bool checked)
186 {
187     if (m_producer) {
188         m_producer->set("drop", checked);
189         emit producerChanged(producer());
190     }
191 }
192 
on_durationSpinBox_editingFinished()193 void CountProducerWidget::on_durationSpinBox_editingFinished()
194 {
195     if (!m_producer)
196         return;
197     if (ui->durationSpinBox->value() == m_producer->get_length())
198         return;
199     if (m_producer) {
200         setLength(producer(), ui->durationSpinBox->value());
201         MLT.stop();
202         emit producerReopened(false);
203         emit producerChanged(producer());
204         MLT.seek(0);
205     }
206 }
207 
on_preset_selected(void * p)208 void CountProducerWidget::on_preset_selected(void* p)
209 {
210     Mlt::Properties* properties = (Mlt::Properties*) p;
211     loadPreset(*properties);
212     delete properties;
213 }
214 
on_preset_saveClicked()215 void CountProducerWidget::on_preset_saveClicked()
216 {
217     ui->preset->savePreset(getPreset());
218 }
219 
detail() const220 QString CountProducerWidget::detail() const
221 {
222     return QString(tr("Count: %1 %2")).arg(ui->directionCombo->currentText()).arg(ui->styleCombo->currentText());
223 }
224