1 /* QWinFF - a qt4 gui frontend for ffmpeg
2 * Copyright (C) 2011-2013 Timothy Lin <lzh9102@gmail.com>
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 <QCheckBox>
19 #include <QTimeEdit>
20 #include <QLayout>
21 #include <QVBoxLayout>
22 #include <QHBoxLayout>
23
24 #include "timerangeedit.h"
25
26 #define SECS_TO_QTIME(s) QTime(0, 0).addSecs(s)
27 #define QTIME_TO_SECS(t) (t).hour()*3600 + (t).minute()*60 + (t).second()
28
TimeRangeEdit(QWidget * parent)29 TimeRangeEdit::TimeRangeEdit(QWidget *parent) :
30 QWidget(parent),
31 m_timeBegin(new QTimeEdit(this)),
32 m_timeEnd(new QTimeEdit(this)),
33 m_chkFromBegin(new QCheckBox(this)),
34 m_chkToEnd(new QCheckBox(this))
35 {
36 m_chkFromBegin->setText(tr("From Begin"));
37 m_chkToEnd->setText(tr("To End"));
38 m_timeBegin->setDisplayFormat("hh:mm:ss");
39 m_timeBegin->setSelectedSection(QTimeEdit::SecondSection);
40 m_timeEnd->setDisplayFormat("hh:mm:ss");
41 m_timeEnd->setSelectedSection(QTimeEdit::SecondSection);
42
43 QHBoxLayout *main_layout = new QHBoxLayout(this);
44 QVBoxLayout *left_layout = new QVBoxLayout(this);
45 QVBoxLayout *right_layout = new QVBoxLayout(this);
46
47 left_layout->addWidget(m_chkFromBegin);
48 left_layout->addWidget(m_timeBegin);
49 right_layout->addWidget(m_chkToEnd);
50 right_layout->addWidget(m_timeEnd);
51
52 main_layout->addLayout(left_layout);
53 main_layout->addLayout(right_layout);
54
55 setLayout(main_layout);
56
57 connect(m_timeBegin, SIGNAL(timeChanged(QTime)), SLOT(time_changed()));
58 connect(m_timeEnd, SIGNAL(timeChanged(QTime)), SLOT(time_changed()));
59 connect(m_chkFromBegin, SIGNAL(toggled(bool)), SLOT(time_changed()));
60 connect(m_chkToEnd, SIGNAL(toggled(bool)), SLOT(time_changed()));
61
62 setFromBegin(true);
63 setToEnd(true);
64 }
65
maxTime() const66 int TimeRangeEdit::maxTime() const
67 {
68 return QTIME_TO_SECS(m_timeEnd->maximumTime());
69 }
70
beginTime() const71 int TimeRangeEdit::beginTime() const
72 {
73 if (m_chkFromBegin->isChecked())
74 return QTIME_TO_SECS(m_timeBegin->minimumTime());
75 else
76 return QTIME_TO_SECS(m_timeBegin->time());
77 }
78
endTime() const79 int TimeRangeEdit::endTime() const
80 {
81 if (m_chkToEnd->isChecked())
82 return QTIME_TO_SECS(m_timeEnd->maximumTime());
83 else
84 return QTIME_TO_SECS(m_timeEnd->time());
85 }
86
fromBegin() const87 bool TimeRangeEdit::fromBegin() const
88 {
89 return m_chkFromBegin->isChecked();
90 }
91
toEnd() const92 bool TimeRangeEdit::toEnd() const
93 {
94 return m_chkToEnd->isChecked();
95 }
96
setMaxTime(int sec)97 void TimeRangeEdit::setMaxTime(int sec)
98 {
99 m_timeBegin->setMinimumTime(SECS_TO_QTIME(0));
100 m_timeEnd->setMaximumTime(SECS_TO_QTIME(sec));
101 emit valueChanged();
102 }
103
setBeginTime(int sec)104 void TimeRangeEdit::setBeginTime(int sec)
105 {
106 if (sec != beginTime()) {
107 m_timeBegin->setTime(SECS_TO_QTIME(sec));
108 m_chkFromBegin->setChecked((sec == 0));
109 }
110 }
111
setEndTime(int sec)112 void TimeRangeEdit::setEndTime(int sec)
113 {
114 if (sec != endTime()) {
115 m_timeEnd->setTime(SECS_TO_QTIME(sec));
116 m_chkToEnd->setChecked((sec == QTIME_TO_SECS(m_timeEnd->maximumTime())));
117 }
118 }
119
setFromBegin(bool from_begin)120 void TimeRangeEdit::setFromBegin(bool from_begin)
121 {
122 m_chkFromBegin->setChecked(from_begin);
123 }
124
setToEnd(bool to_end)125 void TimeRangeEdit::setToEnd(bool to_end)
126 {
127 m_chkToEnd->setChecked(to_end);
128 }
129
time_changed()130 void TimeRangeEdit::time_changed()
131 {
132 // the latest allowed begin time is end time
133 QTime begin_max = m_timeEnd->time().addSecs(-1);
134 if (m_chkToEnd->isChecked())
135 begin_max = m_timeEnd->maximumTime(); // end time is media duration
136
137 // the earliest allowed end time is begin time
138 QTime end_min = m_timeBegin->time().addSecs(1);
139 if (m_chkFromBegin->isChecked())
140 end_min = SECS_TO_QTIME(0); // begin time is zero
141
142 m_timeBegin->setMaximumTime(begin_max);
143 m_timeEnd->setMinimumTime(end_min);
144 m_timeBegin->setDisabled(m_chkFromBegin->isChecked());
145 m_timeEnd->setDisabled(m_chkToEnd->isChecked());
146 emit valueChanged();
147 }
148