File indexing completed on 2024-05-19 04:41:22
0001 /* 0002 SPDX-FileCopyrightText: 2019 Daniel Mensinger <daniel@mensinger-ka.de> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "mesonrewriterinput.h" 0008 #include "mesonoptionbaseview.h" 0009 #include "rewriter/mesonkwargsinfo.h" 0010 #include "rewriter/mesonkwargsmodify.h" 0011 #include "ui_mesonrewriterinput.h" 0012 #include "ui_mesonrewriteroptioncontainer.h" 0013 0014 #include <KColorScheme> 0015 #include <QLineEdit> 0016 #include <debug.h> 0017 0018 MesonRewriterInputBase::MesonRewriterInputBase(const QString& name, const QString& kwarg, QWidget* parent) 0019 : QWidget(parent) 0020 , m_name(name) 0021 , m_kwarg(kwarg) 0022 { 0023 m_ui = new Ui::MesonRewriterInputBase; 0024 m_ui->setupUi(this); 0025 m_ui->l_name->setText(m_name + QLatin1Char(':')); 0026 0027 connect(this, &MesonRewriterInputBase::configChanged, this, &MesonRewriterInputBase::updateUi); 0028 } 0029 0030 MesonRewriterInputBase::~MesonRewriterInputBase() {} 0031 0032 int MesonRewriterInputBase::nameWidth() 0033 { 0034 // Make the name a bit (by 25) wider than it actually is to create a margin. Maybe do 0035 // something smarter in the future (TODO) 0036 return m_ui->l_name->fontMetrics().boundingRect(m_ui->l_name->text()).width() + 25; 0037 } 0038 0039 void MesonRewriterInputBase::setMinNameWidth(int width) 0040 { 0041 m_ui->l_name->setMinimumWidth(width); 0042 } 0043 0044 void MesonRewriterInputBase::setInputWidget(QWidget* input) 0045 { 0046 QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); 0047 sizePolicy.setHorizontalStretch(0); 0048 sizePolicy.setVerticalStretch(0); 0049 sizePolicy.setHeightForWidth(input->sizePolicy().hasHeightForWidth()); 0050 input->setSizePolicy(sizePolicy); 0051 m_ui->layout->insertWidget(1, input); 0052 updateUi(); 0053 } 0054 0055 void MesonRewriterInputBase::updateUi() 0056 { 0057 KColorScheme scheme(QPalette::Normal); 0058 KColorScheme::ForegroundRole role; 0059 0060 if (hasChanged()) { 0061 m_ui->l_name->setStyleSheet(QStringLiteral("font-weight: bold")); 0062 m_ui->b_reset->setDisabled(false || !m_enabled); 0063 role = KColorScheme::NeutralText; 0064 } else { 0065 m_ui->l_name->setStyleSheet(QString()); 0066 m_ui->b_reset->setDisabled(true); 0067 role = KColorScheme::NormalText; 0068 } 0069 0070 role = m_enabled ? role : KColorScheme::InactiveText; 0071 0072 QPalette pal = m_ui->l_name->palette(); 0073 pal.setColor(QPalette::WindowText, scheme.foreground(role).color()); 0074 m_ui->l_name->setPalette(pal); 0075 0076 m_ui->l_name->setDisabled(!m_enabled); 0077 inputWidget()->setDisabled(!m_enabled); 0078 m_ui->b_add->setHidden(m_enabled); 0079 m_ui->b_delete->setHidden(!m_enabled); 0080 } 0081 0082 void MesonRewriterInputBase::reset() 0083 { 0084 doReset(); 0085 emit configChanged(); 0086 } 0087 0088 void MesonRewriterInputBase::remove() 0089 { 0090 m_enabled = false; 0091 reset(); 0092 } 0093 0094 void MesonRewriterInputBase::add() 0095 { 0096 m_enabled = true; 0097 reset(); 0098 } 0099 0100 void MesonRewriterInputBase::resetFromAction(MesonKWARGSInfo* action) 0101 { 0102 resetValue(action->get(m_kwarg)); 0103 m_default_enabled = m_enabled = action->hasKWARG(m_kwarg); 0104 if (m_enabled) { 0105 add(); 0106 } else { 0107 remove(); 0108 } 0109 } 0110 0111 void MesonRewriterInputBase::writeToAction(MesonKWARGSModify* action) 0112 { 0113 action->set(m_kwarg, value()); 0114 } 0115 0116 bool MesonRewriterInputBase::hasChanged() const 0117 { 0118 return hasValueChanged() || (m_default_enabled != m_enabled); 0119 } 0120 0121 bool MesonRewriterInputBase::isEnabled() const 0122 { 0123 return m_enabled; 0124 } 0125 0126 // String input class 0127 0128 MesonRewriterInputString::MesonRewriterInputString(const QString& name, const QString& kwarg, QWidget* parent) 0129 : MesonRewriterInputBase(name, kwarg, parent) 0130 { 0131 m_lineEdit = new QLineEdit(this); 0132 connect(m_lineEdit, &QLineEdit::textChanged, this, [this]() { emit configChanged(); }); 0133 setInputWidget(m_lineEdit); 0134 } 0135 0136 MesonRewriterInputString::~MesonRewriterInputString() {} 0137 0138 MesonRewriterInputBase::Type MesonRewriterInputString::type() const 0139 { 0140 return STRING; 0141 } 0142 0143 bool MesonRewriterInputString::hasValueChanged() const 0144 { 0145 return m_lineEdit->text() != m_initialValue; 0146 } 0147 0148 QWidget* MesonRewriterInputString::inputWidget() 0149 { 0150 return m_lineEdit; 0151 } 0152 0153 void MesonRewriterInputString::doReset() 0154 { 0155 m_lineEdit->setText(m_initialValue); 0156 } 0157 0158 void MesonRewriterInputString::resetValue(const QJsonValue& val) 0159 { 0160 m_initialValue = val.toString(); 0161 } 0162 0163 QJsonValue MesonRewriterInputString::value() 0164 { 0165 return QJsonValue(m_lineEdit->text()); 0166 } 0167 0168 // Options container 0169 0170 MesonRewriterOptionContainer::MesonRewriterOptionContainer(MesonOptViewPtr optView, QWidget* parent) 0171 : QWidget(parent) 0172 , m_optView(optView) 0173 { 0174 m_ui = new Ui::MesonRewriterOptionContainer; 0175 m_ui->setupUi(this); 0176 m_ui->h_layout->insertWidget(0, m_optView.get()); 0177 0178 connect(optView.get(), &MesonOptionBaseView::configChanged, this, [this]() { emit configChanged(); }); 0179 } 0180 0181 void MesonRewriterOptionContainer::deleteMe() 0182 { 0183 m_markedForDeletion = true; 0184 emit configChanged(); 0185 } 0186 0187 bool MesonRewriterOptionContainer::shouldDelete() const 0188 { 0189 return m_markedForDeletion; 0190 } 0191 0192 bool MesonRewriterOptionContainer::hasChanged() const 0193 { 0194 return m_optView->option()->isUpdated(); 0195 } 0196 0197 MesonOptViewPtr MesonRewriterOptionContainer::view() 0198 { 0199 return m_optView; 0200 } 0201 0202 #include "moc_mesonrewriterinput.cpp"