forked from mltframework/shotcut
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimespinbox.cpp
More file actions
105 lines (93 loc) · 3.04 KB
/
timespinbox.cpp
File metadata and controls
105 lines (93 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright (c) 2012-2017 Meltytech, LLC
* Author: Dan Dennedy <dan@dennedy.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "timespinbox.h"
#include "mltcontroller.h"
#include <QRegExpValidator>
#include <QKeyEvent>
#include <QFontDatabase>
#include <QGuiApplication>
TimeSpinBox::TimeSpinBox(QWidget *parent)
: QSpinBox(parent)
{
setLineEdit(new TimeSpinBoxLineEdit);
setRange(0, INT_MAX);
setFixedWidth(this->fontMetrics().width("HHH:MM:SS.MMM"));
setAlignment(Qt::AlignRight);
m_validator = new QRegExpValidator(QRegExp("^\\s*(\\d*:){0,2}(\\d*[.;:])?\\d*\\s*$"), this);
setValue(0);
#ifdef Q_OS_MAC
QFont font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
font.setPointSize(QGuiApplication::font().pointSize());
setFont(font);
#endif
}
QValidator::State TimeSpinBox::validate(QString &input, int &pos) const
{
return m_validator->validate(input, pos);
}
int TimeSpinBox::valueFromText(const QString &text) const
{
if (MLT.producer() && MLT.producer()->is_valid()) {
return MLT.producer()->time_to_frames(text.toLatin1().constData());
} else {
return Mlt::Producer(MLT.profile(), "colour").time_to_frames(text.toLatin1().constData());
}
return 0;
}
QString TimeSpinBox::textFromValue(int val) const
{
if (MLT.producer() && MLT.producer()->is_valid()) {
return MLT.producer()->frames_to_time(val);
} else {
return Mlt::Producer(MLT.profile(), "colour").frames_to_time(val);
}
return QString();
}
void TimeSpinBox::keyPressEvent(QKeyEvent* event)
{
if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return)
clearFocus();
else
QSpinBox::keyPressEvent(event);
}
TimeSpinBoxLineEdit::TimeSpinBoxLineEdit(QWidget *parent)
: QLineEdit(parent)
, m_selectOnMousePress(false)
{
}
void TimeSpinBoxLineEdit::focusInEvent(QFocusEvent *event)
{
QLineEdit::focusInEvent(event);
selectAll();
m_selectOnMousePress = true;
}
void TimeSpinBoxLineEdit::focusOutEvent(QFocusEvent* event)
{
// QLineEdit::focusOutEvent() calls deselect() on OtherFocusReason,
// which prevents using the clipboard actions with the text.
if (event->reason() != Qt::OtherFocusReason)
QLineEdit::focusOutEvent(event);
}
void TimeSpinBoxLineEdit::mousePressEvent(QMouseEvent *event)
{
QLineEdit::mousePressEvent(event);
if (m_selectOnMousePress) {
selectAll();
m_selectOnMousePress = false;
}
}