[Companion] Adjust source value ranges and editors in LS/CF/Telemetry (#5437)

* [Companion] RawSourceRange: Fix for timers and RTC; Remove unused "single precision" flag and logic; Add logic for "short" timer handling.

* [Companion] Add TimerEdit class for editing negative times; Implement TimerEdit in LS, CF, and Telemetry (bars) editors;

* [Companion] Refactor LogicalSwitchesPanel for fewer & more efficient updates; Fix newly-added LS not being available in source lists; Fix bug with timer/rtc comparison values getting reset to value of spin box instead of time editor.

* Add forgotten #include.

* [Companion] LogicalSwitchesPanel: Update raw sources list upon screen refresh (fixes #5374); Improve data entry in spin boxes (fixes #5375).

* Make gcc happy.

* [Companion] Set proper RawSourceRange for GVars, trims, mixer scripts, and sticks/switches/cyclic/MAX.

* [Companion] Fix backwards precision/range logic on telemetry sensors offset spinbox; Fix other minor range issues with ratio & offset.

* [Companion] Remove short limit on min/max timer values for LS & telem. bars.

* Switch to TimerEdit in maxLibQt.

* Fix gcc build.

* Remove unused files.

* Caps fix.
This commit is contained in:
Max Paperno
2017-12-19 12:17:49 +01:00
committed by Bertrand Songis
parent b69f36afe4
commit 76ce186bd4
17 changed files with 741 additions and 257 deletions
@@ -13,14 +13,14 @@ project(maxLibQtWidgets)
set(SRCS
"${CMAKE_CURRENT_LIST_DIR}/ExportableTableView.cpp"
# "${CMAKE_CURRENT_LIST_DIR}/ScrollableMessageBox.cpp"
# "${CMAKE_CURRENT_LIST_DIR}/TimerEdit.cpp"
"${CMAKE_CURRENT_LIST_DIR}/TimerEdit.cpp"
# "${CMAKE_CURRENT_LIST_DIR}/TreeComboBox.cpp"
)
set(HDRS
"${CMAKE_CURRENT_LIST_DIR}/ExportableTableView.h"
# "${CMAKE_CURRENT_LIST_DIR}/ScrollableMessageBox.h"
# "${CMAKE_CURRENT_LIST_DIR}/TimerEdit.h"
"${CMAKE_CURRENT_LIST_DIR}/TimerEdit.h"
# "${CMAKE_CURRENT_LIST_DIR}/TreeComboBox.h"
)
@@ -0,0 +1 @@
#include "TimerEdit.h"
@@ -0,0 +1,281 @@
/*
TimerEdit
COPYRIGHT: (c)2017 Maxim Paperno; All Right Reserved.
Contact: http://www.WorldDesign.com/contact
LICENSE:
Commercial License Usage
Licensees holding valid commercial licenses may use this file in
accordance with the terms contained in a written agreement between
you and the copyright holder.
GNU General Public License Usage
Alternatively, this file may be used 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.
A copy of the GNU General Public License is available at <http://www.gnu.org/licenses/>.
*/
#include "TimerEdit.h"
#include <cmath>
TimerEdit::TimerEdit(QWidget *parent) :
QLineEdit(parent),
m_showSeconds(true),
m_minTime(0),
m_maxTime(24 * 3600 - 1),
m_singleStep(1),
m_pageStep(60),
m_hourDigits(2),
m_validator(new QRegularExpressionValidator(this))
{
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
setupFormat();
setToolTip(tr("Use mouse scroll or UP/DOWN arrow keys to change time in small steps.\nCTRL + scroll or PAGE UP/DOWN keys to change time in larger steps."));
connect(this, &TimerEdit::textEdited, this, &TimerEdit::textEditedHandler);
}
int TimerEdit::timeInSeconds() const
{
int ret = 0;
ret += timepart(text(), TimeParts::Hours) * 3600;
ret += timepart(text(), TimeParts::Minutes) * 60;
ret += timepart(text(), TimeParts::Seconds);
ret *= timepart(text(), TimeParts::Polarity);
return ret;
}
int TimerEdit::timepart(const QString &input, TimerEdit::TimeParts part) const
{
int ret = 0;
QRegularExpressionMatch match = m_validator->regularExpression().match(input, 0, QRegularExpression::NormalMatch);
if (match.hasMatch()) {
switch (part) {
case Hours:
if (!match.captured("hrs").isEmpty())
ret = match.captured("hrs").toInt();
break;
case Minutes:
if (!match.captured("mins").isEmpty())
ret = match.captured("mins").toInt();
break;
case Seconds:
if (!match.captured("secs").isEmpty())
ret = match.captured("secs").toInt();
break;
case Polarity:
if (!match.captured("pol").isEmpty() && !match.captured("pol").compare("-"))
ret = -1;
else
ret = 1;
break;
default:
break;
}
}
return ret;
}
//// Slots ////
void TimerEdit::setTime(int seconds)
{
if (seconds > m_maxTime)
seconds = m_maxTime;
else if (seconds < m_minTime)
seconds = m_minTime;
div_t qrM = div(abs(seconds), 60);
div_t qrH = div(qrM.quot, 60);
QString val = QString("%1").arg(qrH.rem, 2, 10, QChar('0'));
if (m_hourDigits)
val.prepend(QString("%1:").arg(qrH.quot, m_hourDigits, 10, QChar('0')));
if (m_showSeconds)
val.append(QString(":%1").arg(qrM.rem, 2, 10, QChar('0')));
if (m_minTime < 0) {
if (seconds < 0)
val.prepend("-");
else
val.prepend(" ");
}
if (text().compare(val)) {
setText(val);
}
}
void TimerEdit::incrDecr(int seconds)
{
if (!seconds)
return;
const int cs = timeInSeconds();
int s = seconds + cs;
if (s < m_minTime)
s = m_minTime;
if (s > m_maxTime)
s = m_maxTime;
if (s != cs) {
setTime(s);
emitValueChanged();
}
}
void TimerEdit::setTimeRange(int minSeconds, int maxSeconds)
{
if (minSeconds <= maxSeconds && (m_minTime != minSeconds || m_maxTime != maxSeconds)) {
int digits = 0;
if (maxSeconds >= 3600)
digits = floorf(logf(round(maxSeconds / 3600)) / logf(10.0f)) + 1;
bool mod = (m_hourDigits != digits || std::signbit((float)m_minTime) != std::signbit((float)minSeconds));
m_minTime = minSeconds;
m_maxTime = maxSeconds;
m_hourDigits = digits;
if (mod)
setupFormat();
}
}
void TimerEdit::setMinimumTime(int minSeconds)
{
setTimeRange(minSeconds, (m_maxTime > minSeconds ? m_maxTime : minSeconds));
}
void TimerEdit::setMaximumTime(int maxSeconds)
{
setTimeRange((m_minTime < maxSeconds ? m_minTime : maxSeconds), maxSeconds);
}
void TimerEdit::setShowSeconds(bool showSeconds)
{
if (m_showSeconds != showSeconds) {
m_showSeconds = showSeconds;
setupFormat();
}
}
//// Protected ////
void TimerEdit::textEditedHandler()
{
int sec = timeInSeconds();
if (sec < m_minTime)
setTime(m_minTime);
else if (sec > m_maxTime)
setTime(m_maxTime);
else
return;
emitValueChanged();
}
void TimerEdit::setupFormat()
{
QString inputRe = "(?<mins>[0-5][0-9])";
QString inputMsk = "99";
QString suffx = "\\" % tr("m") % "\\" % tr("m");
if (m_hourDigits) {
inputRe.prepend("(?<hrs>[0-9]*[0-9]):");
inputMsk.prepend(":");
suffx.prepend(":");
for (int i=0; i < m_hourDigits; ++i) {
inputMsk.prepend("9");
suffx.prepend("\\" % tr("h"));
}
}
if (m_showSeconds) {
inputRe.append(":(?<secs>[0-5][0-9])");
inputMsk.append(":99");
suffx.append(":\\" % tr("s") % "\\" % tr("s"));
}
if (m_minTime < 0) {
inputRe.prepend("(?<pol>-|\\+|\\s)?");
inputMsk.prepend("#");
}
inputRe.prepend("^");
inputRe.append("\\s?\\[[^\\]]+\\]$");
inputMsk.append(" \\[" % suffx % "\\]");
m_validator->setRegularExpression(QRegularExpression(inputRe));
setInputMask(inputMsk);
setValidator(m_validator);
}
void TimerEdit::emitValueChanged()
{
emit QLineEdit::textEdited(text());
}
void TimerEdit::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Up:
incrDecr(m_singleStep);
break;
case Qt::Key_Down:
incrDecr(-(int)m_singleStep);
break;
case Qt::Key_PageUp:
incrDecr(m_pageStep);
break;
case Qt::Key_PageDown:
incrDecr(-(int)m_pageStep);
break;
case Qt::Key_Plus:
case Qt::Key_Equal:
case Qt::Key_Minus:
if (m_minTime < 0) {
setTime(-timeInSeconds());
emitValueChanged();
}
break;
default:
QLineEdit::keyPressEvent(event);
break;
}
}
void TimerEdit::wheelEvent(QWheelEvent *event)
{
if (event->angleDelta().isNull()) {
event->ignore();
return;
}
int numSteps = -event->angleDelta().y() / 8 / 15 * -1; // one step per 15deg
numSteps *= (event->modifiers() & Qt::ControlModifier) ? m_pageStep : m_singleStep;
incrDecr(numSteps);
event->accept();
}
@@ -0,0 +1,103 @@
/*
TimerEdit
https://github.com/mpaperno/maxLibQt
COPYRIGHT: (c)2017 Maxim Paperno; All Right Reserved.
Contact: http://www.WorldDesign.com/contact
LICENSE:
Commercial License Usage
Licensees holding valid commercial licenses may use this file in
accordance with the terms contained in a written agreement between
you and the copyright holder.
GNU General Public License Usage
Alternatively, this file may be used 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.
A copy of the GNU General Public License is available at <http://www.gnu.org/licenses/>.
*/
#ifndef TIMEREDIT_H
#define TIMEREDIT_H
#include <QLineEdit>
#include <QRegularExpression>
#include <QRegularExpressionValidator>
#include <QKeyEvent>
#include <QWheelEvent>
/**
\class TimerEdit
\version 1.0.0
\brief A time value line editor which accepts negative and large times (> 23:59:59), suitable for a timer, etc.
Qt's \c QTimeEdit has a limitation in that it can only accept "valid" times, in the range of 0:00:00 to 23:59:59.
The TimerEdit control aims to address that. It allows any amount of time to be entered, as well as negative times.
It provides a mask for data entry, a validator with settable min/max times, and increment/decrement via keyboard
or mouse wheel in configurable steps.
Data I/O is in seconds only, that is you set the time value in total seconds and also read it back in seconds
(there is no conversion from/to a \e QTime object).
*/
class TimerEdit : public QLineEdit
{
Q_OBJECT
Q_PROPERTY(int minimumTime READ minimumTime WRITE setMinimumTime)
Q_PROPERTY(int maximumTime READ maximumTime WRITE setMaximumTime)
Q_PROPERTY(bool showSeconds READ showSeconds WRITE setShowSeconds)
Q_PROPERTY(unsigned int singleStep READ singleStep WRITE setSingleStep)
Q_PROPERTY(unsigned int pageStep READ pageStep WRITE setPageStep)
public:
enum TimeParts {Whole, Polarity, Hours, Minutes, Seconds};
TimerEdit(QWidget *parent = Q_NULLPTR);
int timeInSeconds() const;
int timepart(const QString &input, TimeParts part) const;
bool showSeconds() const { return m_showSeconds; }
int minimumTime() const { return m_minTime; }
int maximumTime() const { return m_maxTime; }
unsigned int singleStep() const { return m_singleStep; }
unsigned int pageStep() const { return m_pageStep; }
public slots:
void setTime(int seconds);
void incrDecr(int seconds);
void setTimeRange(int minSeconds, int maxSeconds);
void setMinimumTime(int minSeconds);
void setMaximumTime(int maxSeconds);
void setShowSeconds(bool showSeconds);
void setSingleStep(unsigned int step) { m_singleStep = step; }
void setPageStep(unsigned int pageStep) { m_pageStep = pageStep; }
protected:
void textEditedHandler();
void setupFormat();
void emitValueChanged();
void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;
void wheelEvent(QWheelEvent *event) Q_DECL_OVERRIDE;
bool m_showSeconds;
int m_minTime; // seconds
int m_maxTime; // seconds
unsigned m_singleStep; // seconds
unsigned m_pageStep; // seconds
short m_hourDigits;
QRegularExpressionValidator * m_validator;
};
#endif // TIMEREDIT_H