Files
qt5-legatus-legacy/Lts/Tools/DdsView/rw/rwwidgets.cpp
T
2026-08-13 18:33:10 +02:00

358 lines
8.2 KiB
C++
Executable File

/**
* @RDE_HEADER@
*
* Widgets that change from Label to * if clicked (and not read only)
*
* $Id$
* $HeadURL$
* $Author$
*/
#include "rwwidgets.h"
#include <QStackedWidget>
#include <QSpinBox>
#include <QVBoxLayout>
#include <QCheckBox>
#include <QDoubleSpinBox>
#include <QLineEdit>
#include <QComboBox>
#include <LtsWidgets/lts_bitfield_widget.h>
void MyLabel::mousePressEvent(QMouseEvent *ev)
{
Q_UNUSED(ev)
emit clicked();
}
/****************************************************************/
ResizableStackedWidget::ResizableStackedWidget(QWidget *parent)
: QStackedWidget(parent)
{
connect(this, &ResizableStackedWidget::currentChanged,
this, &ResizableStackedWidget::onCurrentChanged);
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
}
int ResizableStackedWidget::addWigdet(QWidget *widget)
{
widget->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
return QStackedWidget::addWidget(widget);
}
int ResizableStackedWidget::insertWidget(int index, QWidget *widget)
{
widget->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
return QStackedWidget::insertWidget(index, widget);
}
void ResizableStackedWidget::onCurrentChanged(int index)
{
QWidget *w = widget(index);
Q_ASSERT(w);
w->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
w->adjustSize();
adjustSize();
}
/****************************************************************/
RWWidget::RWWidget(QWidget *parent)
: QWidget(parent)
, m_label(new MyLabel(this))
, m_stackedWidget(new ResizableStackedWidget(this))
, m_editableWidget(nullptr)
{
QVBoxLayout *l = new QVBoxLayout(this);
l->setMargin(0);
l->setSpacing(0);
l->addWidget(m_stackedWidget);
m_stackedWidget->insertWidget(0, m_label);
connect(m_label, &MyLabel::clicked, this, &RWWidget::onSetWritablePrivate);
}
void RWWidget::setToolTip(const QString &string)
{
m_label->setToolTip(string);
if (m_editableWidget)
m_editableWidget->setToolTip(string);
}
void RWWidget::onSetWritable()
{
if (m_editableWidget)
m_stackedWidget->setCurrentWidget(m_editableWidget);
}
void RWWidget::onSetWritablePrivate()
{
onSetWritable();
if (m_editableWidget)
emit writableRequested();
}
/****************************************************************/
RWSpinBox::RWSpinBox(RwMode readOnly, QWidget *parent)
: RWWidget(parent)
, m_spinBox(readOnly == RwMode::ReadOnly ? nullptr : new QSpinBox(this))
{
if(!m_spinBox)
return;
m_editableWidget = m_spinBox;
m_spinBox->setMinimum(std::numeric_limits<int>::min());
m_spinBox->setMaximum(std::numeric_limits<int>::max());
m_stackedWidget->insertWidget(1, m_editableWidget);
onSetWritable();
}
void RWSpinBox::setMinimum(int min)
{
if (m_spinBox)
m_spinBox->setMinimum(min);
}
void RWSpinBox::setMaximum(int max)
{
if (m_spinBox)
m_spinBox->setMaximum(max);
}
void RWSpinBox::setValue(int val)
{
m_label->setText(QString::number(val));
if (m_spinBox)
m_spinBox->setValue(val);
}
int RWSpinBox::value() const
{
return m_spinBox ? m_spinBox->value() : m_label->text().toInt();
}
/*****************************************************/
RWDoubleSpinBox::RWDoubleSpinBox(RwMode readOnly, QWidget *parent)
: RWWidget(parent)
, m_doubleSpinBox(readOnly == RwMode::ReadOnly ? nullptr : new QDoubleSpinBox(this))
, m_precision(8)
{
if(!m_doubleSpinBox)
return;
m_editableWidget = m_doubleSpinBox;
m_stackedWidget->insertWidget(1, m_editableWidget);
setDecimals(m_precision);
connect(m_doubleSpinBox, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &RWDoubleSpinBox::valueChanged);
onSetWritable();
}
void RWDoubleSpinBox::setMinimum(double min)
{
if (m_doubleSpinBox)
m_doubleSpinBox->setMinimum(min);
}
void RWDoubleSpinBox::setMaximum(double max)
{
if (m_doubleSpinBox)
m_doubleSpinBox->setMaximum(max);
}
void RWDoubleSpinBox::setDecimals(int prec)
{
m_precision = prec;
if (m_doubleSpinBox)
m_doubleSpinBox->setDecimals(prec);
m_label->setText(QString::number(m_label->text().toDouble(), 'f', prec));
}
void RWDoubleSpinBox::setValue(double val)
{
m_label->setText(QString::number(val, 'f', m_precision));
if (m_doubleSpinBox)
m_doubleSpinBox->setValue(val);
emit valueChanged();
}
double RWDoubleSpinBox::value() const
{
return m_doubleSpinBox ? m_doubleSpinBox->value() : m_label->text().toDouble();
}
/*****************************************************/
RWCheckBox::RWCheckBox(RwMode readOnly, QWidget *parent)
: RWWidget(parent)
, m_checkBox(readOnly == RwMode::ReadOnly ? nullptr : new QCheckBox(this))
{
if(!m_checkBox)
return;
m_editableWidget = m_checkBox;
m_stackedWidget->insertWidget(1, m_editableWidget);
onSetWritable();
}
void RWCheckBox::setChecked(bool val)
{
m_label->setText(val ? tr("Yes") : tr("No"));
if (m_checkBox)
m_checkBox->setChecked(val);
}
bool RWCheckBox::isChecked() const
{
return m_checkBox ? m_checkBox->isChecked() : (m_label->text() == tr("Yes"));
}
/*****************************************************/
RWLineEdit::RWLineEdit(RwMode readOnly, QWidget *parent)
: RWWidget(parent)
, m_lineEdit(readOnly == RwMode::ReadOnly ? nullptr : new QLineEdit(this))
{
if(!m_lineEdit)
return;
m_editableWidget = m_lineEdit;
m_stackedWidget->insertWidget(1, m_editableWidget);
onSetWritable();
}
void RWLineEdit::setMaxLength(int length)
{
if (m_lineEdit)
m_lineEdit->setMaxLength(length);
}
void RWLineEdit::setText(const QString &val)
{
m_label->setText(val);
if (m_lineEdit)
m_lineEdit->setText(val);
}
QString RWLineEdit::text() const
{
return m_lineEdit ? m_lineEdit->text() : m_label->text();
}
/*****************************************************/
RWComboBox::RWComboBox(RwMode readOnly, QWidget *parent)
: RWWidget(parent)
, m_comboBox(readOnly == RwMode::ReadOnly ? nullptr : new QComboBox(this))
{
if(!m_comboBox)
return;
m_editableWidget = m_comboBox;
m_stackedWidget->insertWidget(1, m_editableWidget);
onSetWritable();
}
void RWComboBox::addItem(const QString &text, int ordinal)
{
if (m_comboBox)
m_comboBox->addItem(text, ordinal);
m_values.insert(ordinal, text);
}
void RWComboBox::insertItem(int index, const QString &text, int ordinal)
{
if (m_comboBox)
m_comboBox->insertItem(index, text, ordinal);
m_values.insert(ordinal, text);
}
int RWComboBox::count() const
{
return (m_comboBox ? m_comboBox->count() : 0);
}
//int RWComboBox::currentIndex() const
//{
// return (m_comboBox ? m_comboBox->currentIndex() : 0);
//}
int RWComboBox::currentValue() const
{
return (m_comboBox ? m_comboBox->itemData(m_comboBox->currentIndex()).toInt() : m_values.key(m_label->text()));
}
QString RWComboBox::currentText() const
{
return (m_comboBox ? m_comboBox->currentText() : m_label->text());
}
//void RWComboBox::setCurrentIndex(int index)
//{
// if (m_comboBox)
// {
// m_comboBox->setCurrentIndex(index);
// m_label->setText(m_comboBox->currentText());
// }
// // Impossible for readOnly! Map is not indexed...
//}
void RWComboBox::setCurrentValue(int ordinal)
{
if (m_comboBox)
m_comboBox->setCurrentIndex(m_comboBox->findData(ordinal));
m_label->setText(m_values.value(ordinal));
}
/*****************************************************/
RWBitfieldWidget::RWBitfieldWidget(RwMode readOnly, QWidget *parent)
: RWWidget(parent)
, m_bitfield(readOnly == RwMode::ReadOnly ? nullptr : new LtsWidgets::LtsBitfieldWidget(parent, 0))
{
if(!m_bitfield)
return;
m_editableWidget = m_bitfield;
m_stackedWidget->insertWidget(1, m_editableWidget);
connect(m_bitfield, &LtsWidgets::LtsBitfieldWidget::valueChanged , this, &RWBitfieldWidget::valueChanged);
connect(m_bitfield, &LtsWidgets::LtsBitfieldWidget::hasCaptionChanged, this, &RWBitfieldWidget::hasCaptionChanged);
onSetWritable();
}
void RWBitfieldWidget::setValue(quint8 val)
{
m_label->setText(QLatin1Literal("0b") + QString::number(val,2));
if (m_bitfield)
m_bitfield->setValue(val);
}
quint8 RWBitfieldWidget::value()
{
if(!m_bitfield)
return m_label->text().remove(0,2).toUInt(nullptr,2);
return m_bitfield->value();
}
void RWBitfieldWidget::setHasCaption(bool option)
{
if (m_bitfield)
m_bitfield->setHasCaption(option);
}
bool RWBitfieldWidget::hasCaption()
{
return m_bitfield ? m_bitfield->hasCaption() : false;
}