1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2020 LoRd_MuldeR <MuldeR2@GMX.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "string_validator.h"
24 ///////////////////////////////////////////////////////////////////////////////
26 ///////////////////////////////////////////////////////////////////////////////
28 StringValidator::StringValidator(QLabel
*notifier
, QLabel
*icon
)
30 m_notifier(notifier
), m_icon(icon
)
36 void StringValidator::fixup(QString
&input
) const
38 input
= input
.simplified();
42 bool StringValidator::checkParam(const QStringList
&input
, const char *const params
[], const bool &doubleMinus
) const
44 for(QStringList::ConstIterator iter
= input
.constBegin(); iter
!= input
.constEnd(); iter
++)
46 for(size_t k
= 0; params
[k
]; k
++)
48 const QString param
= QLatin1String(params
[k
]);
49 const QString prefix
= ((param
.length() > 1) && doubleMinus
) ? QLatin1String("--") : QLatin1String("-");
50 if(iter
->compare(QString("%1%2").arg(prefix
, param
), Qt::CaseInsensitive
) == 0)
54 m_notifier
->setText(tr("Forbidden parameter: %1").arg(*iter
));
59 if(iter
->startsWith(QLatin1String("--"), Qt::CaseInsensitive
))
61 for(int i
= 2; i
< iter
->length(); i
++)
63 const QChar c
= iter
->at(i
);
64 if((c
== QLatin1Char('=')) && (i
> 2) && (i
+ 1 < iter
->length()))
66 break; /*to allow "--param=value" format*/
68 if((!c
.isLetter()) && ((i
< 3) || ((!c
.isNumber()) && ((i
+ 1 >= iter
->length()) || (c
!= QLatin1Char('-'))))))
72 m_notifier
->setText(tr("Invalid string: %1").arg(*iter
));
83 bool StringValidator::checkPrefix(const QStringList
&input
, const bool &doubleMinus
) const
85 for(QStringList::ConstIterator iter
= input
.constBegin(); iter
!= input
.constEnd(); iter
++)
87 static const char *const c
[3] = { "--", "-", NULL
};
88 for(size_t i
= 0; c
[i
]; i
++)
90 const QString prefix
= QString::fromLatin1(c
[i
]);
91 if(iter
->compare(prefix
, Qt::CaseInsensitive
) == 0)
95 m_notifier
->setText(tr("Invalid parameter: %1").arg(prefix
));
102 ((!doubleMinus
) && iter
->startsWith("--", Qt::CaseInsensitive
)) ||
103 (doubleMinus
&& iter
->startsWith("-", Qt::CaseInsensitive
) && (!iter
->startsWith("--", Qt::CaseInsensitive
)) && (iter
->length() > 2) && (!iter
->at(1).isDigit())) ||
104 (doubleMinus
&& iter
->startsWith("--", Qt::CaseInsensitive
) && (iter
->length() < 4))
109 m_notifier
->setText(tr("Invalid syntax: %1").arg(*iter
));
117 bool StringValidator::checkCharacters(const QStringList
&input
) const
119 static const char c
[] = {'*', '?', '<', '>', '|', NULL
};
121 for(QStringList::ConstIterator iter
= input
.constBegin(); iter
!= input
.constEnd(); iter
++)
123 for(size_t i
= 0; c
[i
]; i
++)
125 if(iter
->indexOf(QLatin1Char(c
[i
])) >= 0)
129 m_notifier
->setText(tr("Invalid character: '%1'").arg(QLatin1Char(c
[i
])));
138 const bool &StringValidator::setStatus(const bool &flag
, const QString
&toolName
) const
144 if(m_notifier
->isHidden()) m_notifier
->show();
145 if(m_icon
) { if(m_icon
->isHidden()) m_icon
->show(); }
146 if(QWidget
*w
= m_notifier
->topLevelWidget()->focusWidget())
148 QToolTip::showText(static_cast<QWidget
*>(w
->parent())->mapToGlobal(w
->pos()), tr("<b>Warning:</b> You entered a parameter that is forbidden. Please note that the GUI will automatically set <i>this</i> parameter for you (if required)."), m_notifier
, QRect());
156 if(m_notifier
->isVisible()) m_notifier
->hide();
157 if(m_icon
) { if(m_icon
->isVisible()) m_icon
->hide(); }
158 QToolTip::hideText();
164 ///////////////////////////////////////////////////////////////////////////////
165 // StringValidatorEncoder
166 ///////////////////////////////////////////////////////////////////////////////
168 QValidator::State
StringValidatorEncoder::validate(QString
&input
, int &pos
) const
170 static const char *const params
[] = { "D", "B", "o", "h", "p", "q", "u", "preset", "tune", "profile", "stdin",
171 "crf", "bitrate", "qp", "pass", "stats", "output", "output-depth", "help", "quiet", "codec", "y4m", NULL
};
173 const QString commandLine
= input
.trimmed();
174 const QStringList tokens
= commandLine
.isEmpty() ? QStringList() : MUtils::OS::crack_command_line(commandLine
);
176 const bool invalid
= checkCharacters(tokens
) || checkPrefix(tokens
, true) || checkParam(tokens
, params
, true);
177 return setStatus(invalid
, "encoder") ? QValidator::Intermediate
: QValidator::Acceptable
;
180 ///////////////////////////////////////////////////////////////////////////////
181 // StringValidatorSource
182 ///////////////////////////////////////////////////////////////////////////////
184 QValidator::State
StringValidatorSource::validate(QString
&input
, int &pos
) const
186 static const char *const params
[] = {"o", "frames", "seek", "raw", "hfyu", "slave", NULL
};
188 const QString commandLine
= input
.trimmed();
189 const QStringList tokens
= commandLine
.isEmpty() ? QStringList() : MUtils::OS::crack_command_line(commandLine
);
191 const bool invalid
= checkCharacters(tokens
) || checkPrefix(tokens
, false) || checkParam(tokens
, params
, false);
192 return setStatus(invalid
, "Avs2YUV") ? QValidator::Intermediate
: QValidator::Acceptable
;