1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-09-27 15:26:33 -04:00

ValueDial fixes

This commit is contained in:
f4exb 2019-04-26 11:34:35 +02:00
parent c0919b1129
commit 105873bfda
2 changed files with 422 additions and 319 deletions

View File

@ -82,7 +82,6 @@ RemoteInputGui::RemoteInputGui(DeviceUISet *deviceUISet, QWidget* parent) :
ui->centerFrequencyHz->setColorMapper(ColorMapper(ColorMapper::GrayGold)); ui->centerFrequencyHz->setColorMapper(ColorMapper(ColorMapper::GrayGold));
ui->centerFrequencyHz->setValueRange(3, 0, 999U); ui->centerFrequencyHz->setValueRange(3, 0, 999U);
ui->centerFrequencyHz->setValue(1);
CRightClickEnabler *startStopRightClickEnabler = new CRightClickEnabler(ui->startStop); CRightClickEnabler *startStopRightClickEnabler = new CRightClickEnabler(ui->startStop);
connect(startStopRightClickEnabler, SIGNAL(rightClick(const QPoint &)), this, SLOT(openDeviceSettingsDialog(const QPoint &))); connect(startStopRightClickEnabler, SIGNAL(rightClick(const QPoint &)), this, SLOT(openDeviceSettingsDialog(const QPoint &)));

View File

@ -16,15 +16,15 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. // // along with this program. If not, see <http://www.gnu.org/licenses/>. //
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
#include <QPainter>
#include <QMouseEvent>
#include <QWheelEvent>
#include <QKeyEvent> #include <QKeyEvent>
#include <QLocale> #include <QLocale>
#include <QMouseEvent>
#include <QPainter>
#include <QWheelEvent>
#include "gui/valuedial.h" #include "gui/valuedial.h"
ValueDial::ValueDial(QWidget* parent, ColorMapper colorMapper) : ValueDial::ValueDial(QWidget *parent, ColorMapper colorMapper) :
QWidget(parent), QWidget(parent),
m_animationState(0), m_animationState(0),
m_colorMapper(colorMapper) m_colorMapper(colorMapper)
@ -42,7 +42,7 @@ ValueDial::ValueDial(QWidget* parent, ColorMapper colorMapper) :
ColorMapper::colormap::const_iterator cmit = m_colorMapper.getDialBackgroundColorMap().begin(); ColorMapper::colormap::const_iterator cmit = m_colorMapper.getDialBackgroundColorMap().begin();
ColorMapper::colormap::const_iterator cmitEnd = m_colorMapper.getDialBackgroundColorMap().end(); ColorMapper::colormap::const_iterator cmitEnd = m_colorMapper.getDialBackgroundColorMap().end();
for (; cmit != cmitEnd; ++ cmit) { for (; cmit != cmitEnd; ++cmit) {
m_background.setColorAt(cmit->first, cmit->second); m_background.setColorAt(cmit->first, cmit->second);
} }
@ -60,22 +60,23 @@ ValueDial::ValueDial(QWidget* parent, ColorMapper colorMapper) :
m_text = formatText(m_value); m_text = formatText(m_value);
m_cursorState = false; m_cursorState = false;
const QLocale & cLocale = QLocale::c(); const QLocale &cLocale = QLocale::c();
m_groupSeparator = cLocale.groupSeparator(); m_groupSeparator = cLocale.groupSeparator();
connect(&m_animationTimer, SIGNAL(timeout()), this, SLOT(animate())); connect(&m_animationTimer, SIGNAL(timeout()), this, SLOT(animate()));
connect(&m_blinkTimer, SIGNAL(timeout()), this, SLOT(blink())); connect(&m_blinkTimer, SIGNAL(timeout()), this, SLOT(blink()));
} }
void ValueDial::setFont(const QFont& font) void ValueDial::setFont(const QFont &font)
{ {
QWidget::setFont(font); QWidget::setFont(font);
QFontMetrics fm(font); QFontMetrics fm(font);
m_digitWidth = fm.width('0'); m_digitWidth = fm.width('0');
m_digitHeight = fm.ascent(); m_digitHeight = fm.ascent();
if(m_digitWidth < m_digitHeight) if (m_digitWidth < m_digitHeight) {
m_digitWidth = m_digitHeight; m_digitWidth = m_digitHeight;
}
setFixedWidth((m_numDigits + m_numDecimalPoints) * m_digitWidth + 2); setFixedWidth((m_numDigits + m_numDecimalPoints) * m_digitWidth + 2);
setFixedHeight(m_digitHeight * 2 + 2); setFixedHeight(m_digitHeight * 2 + 2);
} }
@ -94,24 +95,29 @@ void ValueDial::setColorMapper(ColorMapper colorMapper)
ColorMapper::colormap::const_iterator cmit = m_colorMapper.getDialBackgroundColorMap().begin(); ColorMapper::colormap::const_iterator cmit = m_colorMapper.getDialBackgroundColorMap().begin();
ColorMapper::colormap::const_iterator cmitEnd = m_colorMapper.getDialBackgroundColorMap().end(); ColorMapper::colormap::const_iterator cmitEnd = m_colorMapper.getDialBackgroundColorMap().end();
for (; cmit != cmitEnd; ++ cmit) { for (; cmit != cmitEnd; ++cmit) {
m_background.setColorAt(cmit->first, cmit->second); m_background.setColorAt(cmit->first, cmit->second);
} }
} }
void ValueDial::setValue(quint64 value) void ValueDial::setValue(quint64 value)
{ {
m_valueNew = value; m_valueNew = value;
if(m_valueNew < m_valueMin)
if (m_valueNew < m_valueMin) {
m_valueNew = m_valueMin; m_valueNew = m_valueMin;
else if(m_valueNew > m_valueMax) } else if (m_valueNew > m_valueMax) {
m_valueNew = m_valueMax; m_valueNew = m_valueMax;
if(m_valueNew < m_value) }
if (m_valueNew < m_value) {
m_animationState = 1; m_animationState = 1;
else if(m_valueNew > m_value) } else if (m_valueNew > m_value) {
m_animationState = -1; m_animationState = -1;
else return; } else {
return;
}
m_animationTimer.start(20); m_animationTimer.start(20);
m_textNew = formatText(m_valueNew); m_textNew = formatText(m_valueNew);
} }
@ -119,16 +125,18 @@ void ValueDial::setValue(quint64 value)
void ValueDial::setValueRange(uint numDigits, quint64 min, quint64 max) void ValueDial::setValueRange(uint numDigits, quint64 min, quint64 max)
{ {
m_numDigits = numDigits; m_numDigits = numDigits;
//m_numDecimalPoints = m_numDigits / 3; m_numDecimalPoints = m_numDigits < 3 ? 0 : (m_numDigits % 3) == 0 ? (m_numDigits / 3) - 1 : m_numDigits / 3;
m_numDecimalPoints = m_numDigits < 3 ? 0 : (m_numDigits%3) == 0 ? (m_numDigits/3)-1 : m_numDigits/3;
m_valueMin = min; m_valueMin = min;
m_valueMax = max; m_valueMax = max;
if(m_value < min) m_text = formatText(m_value);
if (m_value < min) {
setValue(min); setValue(min);
else if(m_value > max) } else if (m_value > max) {
setValue(max); setValue(max);
}
setFixedWidth((m_numDigits + m_numDecimalPoints) * m_digitWidth + 2); setFixedWidth((m_numDigits + m_numDecimalPoints) * m_digitWidth + 2);
} }
@ -137,21 +145,31 @@ quint64 ValueDial::findExponent(int digit)
quint64 e = 1; quint64 e = 1;
int d = (m_numDigits + m_numDecimalPoints) - digit; int d = (m_numDigits + m_numDecimalPoints) - digit;
d = d - (d / 4) - 1; d = d - (d / 4) - 1;
for(int i = 0; i < d; i++)
for (int i = 0; i < d; i++) {
e *= 10; e *= 10;
}
return e; return e;
} }
QChar ValueDial::digitNeigh(QChar c, bool dir) QChar ValueDial::digitNeigh(QChar c, bool dir)
{ {
if(dir) { if (dir)
if(c == QChar('0')) {
if (c == QChar('0')) {
return QChar('9'); return QChar('9');
else return QChar::fromLatin1(c.toLatin1() - 1);
} else { } else {
if(c == QChar('9')) return QChar::fromLatin1(c.toLatin1() - 1);
}
}
else
{
if (c == QChar('9')) {
return QChar('0'); return QChar('0');
else return QChar::fromLatin1(c.toLatin1() + 1); } else {
return QChar::fromLatin1(c.toLatin1() + 1);
}
} }
} }
@ -159,12 +177,13 @@ QString ValueDial::formatText(quint64 value)
{ {
QString str = QString("%1").arg(value, m_numDigits, 10, QChar('0')); QString str = QString("%1").arg(value, m_numDigits, 10, QChar('0'));
for(int i = 0; i < m_numDecimalPoints; i++) for (int i = 0; i < m_numDecimalPoints; i++)
{ {
int ipoint = m_numDigits - 3 - 3 * i; int ipoint = m_numDigits - 3 - 3 * i;
if (ipoint != 0) { // do not insert leading point if (ipoint != 0)
const QLocale & cLocale = QLocale::c(); { // do not insert leading point
const QLocale &cLocale = QLocale::c();
str.insert(ipoint, cLocale.groupSeparator()); str.insert(ipoint, cLocale.groupSeparator());
} }
} }
@ -172,7 +191,7 @@ QString ValueDial::formatText(quint64 value)
return str; return str;
} }
void ValueDial::paintEvent(QPaintEvent*) void ValueDial::paintEvent(QPaintEvent *)
{ {
QPainter painter(this); QPainter painter(this);
@ -214,18 +233,24 @@ void ValueDial::paintEvent(QPaintEvent*)
if (m_animationState == 0) if (m_animationState == 0)
{ {
for(int i = 0; i < m_text.length(); i++) { for (int i = 0; i < m_text.length(); i++)
{
painter.setClipRect(1 + i * m_digitWidth, 1, m_digitWidth, m_digitHeight * 2); painter.setClipRect(1 + i * m_digitWidth, 1, m_digitWidth, m_digitHeight * 2);
painter.setPen(m_colorMapper.getSecondaryForegroundColor()); painter.setPen(m_colorMapper.getSecondaryForegroundColor());
painter.drawText(QRect(1 + i * m_digitWidth, m_digitHeight * 0.6, m_digitWidth, m_digitHeight), Qt::AlignCenter, m_text.mid(i, 1)); painter.drawText(QRect(1 + i * m_digitWidth, m_digitHeight * 0.6, m_digitWidth, m_digitHeight), Qt::AlignCenter, m_text.mid(i, 1));
if(m_text[i] != m_groupSeparator) {
if (m_text[i] != m_groupSeparator)
{
painter.setPen(m_colorMapper.getForegroundColor()); painter.setPen(m_colorMapper.getForegroundColor());
painter.drawText(QRect(1 + i * m_digitWidth, m_digitHeight * -0.7, m_digitWidth, m_digitHeight), Qt::AlignCenter, digitNeigh(m_text[i], true)); painter.drawText(QRect(1 + i * m_digitWidth, m_digitHeight * -0.7, m_digitWidth, m_digitHeight), Qt::AlignCenter, digitNeigh(m_text[i], true));
painter.drawText(QRect(1 + i * m_digitWidth, m_digitHeight * 1.9, m_digitWidth, m_digitHeight), Qt::AlignCenter, digitNeigh(m_text[i], false)); painter.drawText(QRect(1 + i * m_digitWidth, m_digitHeight * 1.9, m_digitWidth, m_digitHeight), Qt::AlignCenter, digitNeigh(m_text[i], false));
} }
} }
painter.setClipping(false); painter.setClipping(false);
if((m_cursor >= 0) && (m_cursorState)) {
if ((m_cursor >= 0) && (m_cursorState))
{
painter.setPen(Qt::NoPen); painter.setPen(Qt::NoPen);
painter.setBrush(m_colorMapper.getSecondaryForegroundColor()); painter.setBrush(m_colorMapper.getSecondaryForegroundColor());
painter.drawRect(4 + m_cursor * m_digitWidth, 1 + m_digitHeight * 1.5, m_digitWidth - 5, m_digitHeight / 6); painter.drawRect(4 + m_cursor * m_digitWidth, 1 + m_digitHeight * 1.5, m_digitWidth - 5, m_digitHeight / 6);
@ -266,17 +291,17 @@ void ValueDial::paintEvent(QPaintEvent*)
} }
} }
void ValueDial::mousePressEvent(QMouseEvent* event) void ValueDial::mousePressEvent(QMouseEvent *event)
{ {
int i; int i;
i = (event->x() - 1) / m_digitWidth; i = (event->x() - 1) / m_digitWidth;
if(m_text[i] == m_groupSeparator) if (m_text[i] == m_groupSeparator)
{ {
i++; i++;
if(i > m_numDigits + m_numDecimalPoints) { if (i > m_numDigits + m_numDecimalPoints) {
return; return;
} }
} }
@ -285,7 +310,7 @@ void ValueDial::mousePressEvent(QMouseEvent* event)
if (mouseButton == Qt::RightButton) // ceil value at current digit if (mouseButton == Qt::RightButton) // ceil value at current digit
{ {
if(m_cursor >= 0) if (m_cursor >= 0)
{ {
m_cursor = -1; m_cursor = -1;
m_blinkTimer.stop(); m_blinkTimer.stop();
@ -308,75 +333,94 @@ void ValueDial::mousePressEvent(QMouseEvent* event)
} }
} }
void ValueDial::mouseMoveEvent(QMouseEvent* event) void ValueDial::mouseMoveEvent(QMouseEvent *event)
{ {
int i; int i;
i = (event->x() - 1) / m_digitWidth; i = (event->x() - 1) / m_digitWidth;
if(m_text[i] == m_groupSeparator)
i = -1;
if(i != m_hightlightedDigit) { if (m_text[i] == m_groupSeparator) {
i = -1;
}
if (i != m_hightlightedDigit)
{
m_hightlightedDigit = i; m_hightlightedDigit = i;
update(); update();
} }
} }
void ValueDial::wheelEvent(QWheelEvent* event) void ValueDial::wheelEvent(QWheelEvent *event)
{ {
int i; int i;
i = (event->x() - 1) / m_digitWidth; i = (event->x() - 1) / m_digitWidth;
if(m_text[i] != m_groupSeparator)
m_hightlightedDigit = i;
else
return;
if(m_cursor >= 0) { if (m_text[i] != m_groupSeparator) {
m_hightlightedDigit = i;
} else {
return;
}
if (m_cursor >= 0)
{
m_cursor = -1; m_cursor = -1;
m_blinkTimer.stop(); m_blinkTimer.stop();
update(); update();
} }
quint64 e = findExponent(m_hightlightedDigit); quint64 e = findExponent(m_hightlightedDigit);
if(m_animationState == 0) { if (m_animationState == 0)
if(event->delta() < 0) { {
if (event->delta() < 0)
{
if (event->modifiers() & Qt::ShiftModifier) { if (event->modifiers() & Qt::ShiftModifier) {
e *= 5; e *= 5;
} else if (event->modifiers() & Qt::ControlModifier) { } else if (event->modifiers() & Qt::ControlModifier) {
e *= 2; e *= 2;
} }
if(m_value < e)
if (m_value < e) {
m_valueNew = m_valueMin; m_valueNew = m_valueMin;
else m_valueNew = m_value - e;
} else { } else {
m_valueNew = m_value - e;
}
}
else
{
if (event->modifiers() & Qt::ShiftModifier) { if (event->modifiers() & Qt::ShiftModifier) {
e *= 5; e *= 5;
} else if (event->modifiers() & Qt::ControlModifier) { } else if (event->modifiers() & Qt::ControlModifier) {
e *= 2; e *= 2;
} }
if(m_valueMax - m_value < e)
if (m_valueMax - m_value < e) {
m_valueNew = m_valueMax; m_valueNew = m_valueMax;
else m_valueNew = m_value + e; } else {
m_valueNew = m_value + e;
} }
}
setValue(m_valueNew); setValue(m_valueNew);
emit changed(m_valueNew); emit changed(m_valueNew);
event->accept(); event->accept();
} }
} }
void ValueDial::leaveEvent(QEvent*) void ValueDial::leaveEvent(QEvent *)
{ {
if(m_hightlightedDigit != -1) { if (m_hightlightedDigit != -1)
{
m_hightlightedDigit = -1; m_hightlightedDigit = -1;
update(); update();
} }
} }
void ValueDial::keyPressEvent(QKeyEvent* value) void ValueDial::keyPressEvent(QKeyEvent *value)
{ {
if(m_cursor >= 0) { if (m_cursor >= 0)
if((value->key() == Qt::Key_Return) || (value->key() == Qt::Key_Enter) || (value->key() == Qt::Key_Escape)) { {
if ((value->key() == Qt::Key_Return) || (value->key() == Qt::Key_Enter) || (value->key() == Qt::Key_Escape))
{
m_cursor = -1; m_cursor = -1;
m_cursorState = false; m_cursorState = false;
m_blinkTimer.stop(); m_blinkTimer.stop();
@ -385,102 +429,156 @@ void ValueDial::keyPressEvent(QKeyEvent* value)
} }
} }
if((m_cursor < 0) && (m_hightlightedDigit >= 0)) { if ((m_cursor < 0) && (m_hightlightedDigit >= 0))
{
m_cursor = m_hightlightedDigit; m_cursor = m_hightlightedDigit;
if(m_text[m_cursor] == m_groupSeparator)
if (m_text[m_cursor] == m_groupSeparator) {
m_cursor++; m_cursor++;
if(m_cursor >= m_numDigits + m_numDecimalPoints) }
if (m_cursor >= m_numDigits + m_numDecimalPoints) {
return; return;
}
m_cursorState = true; m_cursorState = true;
m_blinkTimer.start(400); m_blinkTimer.start(400);
update(); update();
} }
if(m_cursor < 0) if (m_cursor < 0) {
return; return;
}
if((value->key() == Qt::Key_Left) || (value->key() == Qt::Key_Backspace)) { if ((value->key() == Qt::Key_Left) || (value->key() == Qt::Key_Backspace))
if(m_cursor > 0) { {
if (m_cursor > 0)
{
m_cursor--; m_cursor--;
if(m_text[m_cursor] == m_groupSeparator)
if (m_text[m_cursor] == m_groupSeparator) {
m_cursor--; m_cursor--;
if(m_cursor < 0) }
if (m_cursor < 0) {
m_cursor++; m_cursor++;
}
m_cursorState = true; m_cursorState = true;
update(); update();
return; return;
} }
} else if(value->key() == Qt::Key_Right) { }
if(m_cursor < m_numDecimalPoints + m_numDigits) { else if (value->key() == Qt::Key_Right)
{
if (m_cursor < m_numDecimalPoints + m_numDigits)
{
m_cursor++; m_cursor++;
if(m_text[m_cursor] == m_groupSeparator)
if (m_text[m_cursor] == m_groupSeparator) {
m_cursor++; m_cursor++;
if(m_cursor >= m_numDecimalPoints + m_numDigits) }
if (m_cursor >= m_numDecimalPoints + m_numDigits) {
m_cursor--; m_cursor--;
}
m_cursorState = true; m_cursorState = true;
update(); update();
return; return;
} }
} else if(value->key() == Qt::Key_Up) { }
else if (value->key() == Qt::Key_Up)
{
quint64 e = findExponent(m_cursor); quint64 e = findExponent(m_cursor);
if (value->modifiers() & Qt::ShiftModifier) { if (value->modifiers() & Qt::ShiftModifier) {
e *= 5; e *= 5;
} else if (value->modifiers() & Qt::ControlModifier) { } else if (value->modifiers() & Qt::ControlModifier) {
e *= 2; e *= 2;
} }
if(m_animationState != 0)
if (m_animationState != 0) {
m_value = m_valueNew; m_value = m_valueNew;
if(m_valueMax - m_value < e) }
if (m_valueMax - m_value < e) {
m_valueNew = m_valueMax; m_valueNew = m_valueMax;
else m_valueNew = m_value + e; } else {
m_valueNew = m_value + e;
}
setValue(m_valueNew); setValue(m_valueNew);
emit changed(m_valueNew); emit changed(m_valueNew);
} else if(value->key() == Qt::Key_Down) { }
else if (value->key() == Qt::Key_Down)
{
quint64 e = findExponent(m_cursor); quint64 e = findExponent(m_cursor);
if (value->modifiers() & Qt::ShiftModifier) { if (value->modifiers() & Qt::ShiftModifier) {
e *= 5; e *= 5;
} else if (value->modifiers() & Qt::ControlModifier) { } else if (value->modifiers() & Qt::ControlModifier) {
e *= 2; e *= 2;
} }
if(m_animationState != 0)
if (m_animationState != 0) {
m_value = m_valueNew; m_value = m_valueNew;
if(m_value < e) }
if (m_value < e) {
m_valueNew = m_valueMin; m_valueNew = m_valueMin;
else m_valueNew = m_value - e; } else {
m_valueNew = m_value - e;
}
setValue(m_valueNew); setValue(m_valueNew);
emit changed(m_valueNew); emit changed(m_valueNew);
} }
if(value->text().length() != 1) if (value->text().length() != 1) {
return; return;
}
QChar c = value->text()[0]; QChar c = value->text()[0];
if(c >= QChar('0') && (c <= QChar('9'))) { if (c >= QChar('0') && (c <= QChar('9')))
{
int d = c.toLatin1() - '0'; int d = c.toLatin1() - '0';
quint64 e = findExponent(m_cursor); quint64 e = findExponent(m_cursor);
quint64 v = (m_value / e) % 10; quint64 v = (m_value / e) % 10;
if(m_animationState != 0)
if (m_animationState != 0) {
m_value = m_valueNew; m_value = m_valueNew;
}
v = m_value - v * e; v = m_value - v * e;
v += d * e; v += d * e;
setValue(v); setValue(v);
emit changed(m_valueNew); emit changed(m_valueNew);
m_cursor++; m_cursor++;
if(m_text[m_cursor] == m_groupSeparator)
if (m_text[m_cursor] == m_groupSeparator) {
m_cursor++; m_cursor++;
if(m_cursor >= m_numDigits + m_numDecimalPoints) { }
if (m_cursor >= m_numDigits + m_numDecimalPoints)
{
m_cursor = -1; m_cursor = -1;
m_blinkTimer.stop(); m_blinkTimer.stop();
} else { }
else
{
m_cursorState = true; m_cursorState = true;
} }
update(); update();
} }
} }
void ValueDial::focusInEvent(QFocusEvent*) void ValueDial::focusInEvent(QFocusEvent *)
{ {
if(m_cursor == -1) { if (m_cursor == -1)
{
m_cursor = 0; m_cursor = 0;
m_cursorState = true; m_cursorState = true;
m_blinkTimer.start(400); m_blinkTimer.start(400);
@ -488,7 +586,7 @@ void ValueDial::focusInEvent(QFocusEvent*)
} }
} }
void ValueDial::focusOutEvent(QFocusEvent*) void ValueDial::focusOutEvent(QFocusEvent *)
{ {
m_cursor = -1; m_cursor = -1;
m_blinkTimer.stop(); m_blinkTimer.stop();
@ -499,17 +597,22 @@ void ValueDial::animate()
{ {
update(); update();
if(m_animationState > 0) if (m_animationState > 0)
{
m_animationState++; m_animationState++;
else if(m_animationState < 0) }
else if (m_animationState < 0) {
m_animationState--; m_animationState--;
else { }
else
{
m_animationTimer.stop(); m_animationTimer.stop();
m_animationState = 0; m_animationState = 0;
return; return;
} }
if(abs(m_animationState) >= 4) { if (abs(m_animationState) >= 4)
{
m_animationState = 0; m_animationState = 0;
m_animationTimer.stop(); m_animationTimer.stop();
m_value = m_valueNew; m_value = m_valueNew;
@ -519,7 +622,8 @@ void ValueDial::animate()
void ValueDial::blink() void ValueDial::blink()
{ {
if(m_cursor >= 0) { if (m_cursor >= 0)
{
m_cursorState = !m_cursorState; m_cursorState = !m_cursorState;
update(); update();
} }