File indexing completed on 2024-05-19 04:07:48
0001 /* 0002 SPDX-FileCopyrightText: 2009 Stefan Majewsky <majewsky@gmx.net> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "propertywidget.h" 0008 #include "propertywidget_p.h" 0009 0010 #include <Pala/SlicerProperty> 0011 0012 #include <QHBoxLayout> 0013 0014 Palapeli::PropertyWidget* Palapeli::createPropertyWidget(const Pala::SlicerProperty* property) 0015 { 0016 Palapeli::PropertyWidget* pw; 0017 switch (property->type()) 0018 { 0019 case QMetaType::Bool: 0020 pw = new Palapeli::BooleanPropertyWidget; 0021 break; 0022 case QMetaType::Int: 0023 pw = new Palapeli::IntegerPropertyWidget; 0024 break; 0025 case QMetaType::QString: 0026 pw = new Palapeli::StringPropertyWidget; 0027 break; 0028 default: 0029 return nullptr; 0030 } 0031 pw->initialize(property); 0032 return pw; 0033 } 0034 0035 //BEGIN Palapeli::BooleanPropertyWidget 0036 0037 Palapeli::BooleanPropertyWidget::BooleanPropertyWidget() 0038 : m_checkBox(nullptr) 0039 { 0040 } 0041 0042 void Palapeli::BooleanPropertyWidget::initialize(const Pala::SlicerProperty* property) 0043 { 0044 m_checkBox = new QCheckBox(this); 0045 m_checkBox->setChecked(property->defaultValue().toBool()); 0046 QHBoxLayout* layout = new QHBoxLayout; 0047 layout->addWidget(m_checkBox); 0048 layout->setContentsMargins(0, 0, 0, 0); 0049 setLayout(layout); 0050 } 0051 0052 QVariant Palapeli::BooleanPropertyWidget::propertyValue() const 0053 { 0054 return m_checkBox ? QVariant(m_checkBox->isChecked()) : QVariant(); 0055 } 0056 0057 //END Palapeli::BooleanPropertyWidget 0058 0059 //BEGIN Palapeli::IntegerPropertyWidget 0060 0061 Palapeli::IntegerPropertyWidget::IntegerPropertyWidget() 0062 : m_comboBox(nullptr) 0063 , m_spinBox(nullptr) 0064 , m_slider(nullptr) 0065 { 0066 } 0067 0068 void Palapeli::IntegerPropertyWidget::initialize(const Pala::SlicerProperty* property) 0069 { 0070 const Pala::IntegerProperty* intProperty = static_cast<const Pala::IntegerProperty*>(property); 0071 const QPair<int,int> range = intProperty->range(); 0072 const QVariantList choices = property->choices(); 0073 QWidget* usedWidget; 0074 if (choices.isEmpty()) 0075 { 0076 switch (intProperty->representation()) 0077 { 0078 case Pala::IntegerProperty::SpinBox: 0079 usedWidget = m_spinBox = new QSpinBox(this); 0080 if (range.first != range.second) //only set range if it is not empty 0081 m_spinBox->setRange(range.first, range.second); 0082 m_spinBox->setValue(property->defaultValue().toInt()); 0083 break; 0084 case Pala::IntegerProperty::Slider: 0085 usedWidget = m_slider = new QSlider(Qt::Horizontal, this); 0086 if (range.first != range.second) //only set range if it is not empty 0087 m_slider->setRange(range.first, range.second); 0088 m_slider->setValue(property->defaultValue().toInt()); 0089 break; 0090 } 0091 } 0092 else 0093 { 0094 usedWidget = m_comboBox = new KComboBox(false, this); //false = not editable (only given choices can be used) 0095 for (const QVariant& choice : choices) 0096 m_comboBox->addItem(choice.toString()); 0097 } 0098 QHBoxLayout* layout = new QHBoxLayout; 0099 layout->addWidget(usedWidget); 0100 usedWidget->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed)); 0101 layout->setContentsMargins(0, 0, 0, 0); 0102 setLayout(layout); 0103 } 0104 0105 QVariant Palapeli::IntegerPropertyWidget::propertyValue() const 0106 { 0107 if (m_spinBox) 0108 return m_spinBox->value(); 0109 else if (m_slider) 0110 return m_slider->value(); 0111 else if (m_comboBox) 0112 return m_comboBox->currentText(); 0113 else //This may never happen. 0114 return QVariant(); 0115 } 0116 0117 //END Palapeli::IntegerPropertyWidget 0118 0119 //BEGIN Palapeli::StringPropertyWidget 0120 0121 Palapeli::StringPropertyWidget::StringPropertyWidget() 0122 : m_comboBox(nullptr) 0123 , m_lineEdit(nullptr) 0124 { 0125 } 0126 0127 void Palapeli::StringPropertyWidget::initialize(const Pala::SlicerProperty* property) 0128 { 0129 const QVariantList choices = property->choices(); 0130 QWidget* usedWidget; 0131 if (choices.isEmpty()) 0132 usedWidget = m_lineEdit = new KLineEdit(property->defaultValue().toString(), this); 0133 else 0134 { 0135 usedWidget = m_comboBox = new KComboBox(false, this); //false = not editable (only given choices can be used) 0136 for (const QVariant& choice : choices) 0137 m_comboBox->addItem(choice.toString()); 0138 } 0139 QHBoxLayout* layout = new QHBoxLayout; 0140 layout->addWidget(usedWidget); 0141 usedWidget->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed)); 0142 layout->setContentsMargins(0, 0, 0, 0); 0143 setLayout(layout); 0144 } 0145 0146 QVariant Palapeli::StringPropertyWidget::propertyValue() const 0147 { 0148 return m_lineEdit ? QVariant(m_lineEdit->text()) : (m_comboBox ? QVariant(m_comboBox->currentText()) : QVariant()); 0149 } 0150 0151 //END Palapeli::StringPropertyWidget