File indexing completed on 2024-04-21 15:04:10

0001 /*
0002     SPDX-FileCopyrightText: 2011-2018 Dominik Haumann <dhaumann@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "variableeditor.h"
0008 #include "katehelpbutton.h"
0009 #include "variableitem.h"
0010 
0011 #include <QCheckBox>
0012 #include <QComboBox>
0013 #include <QFontComboBox>
0014 #include <QGridLayout>
0015 #include <QLabel>
0016 #include <QLineEdit>
0017 #include <QPainter>
0018 #include <QSpinBox>
0019 #include <QVariant>
0020 
0021 #include <KColorCombo>
0022 #include <KLocalizedString>
0023 #include <sonnet/dictionarycombobox.h>
0024 
0025 // BEGIN VariableEditor
0026 VariableEditor::VariableEditor(VariableItem *item, QWidget *parent)
0027     : QWidget(parent)
0028     , m_item(item)
0029 {
0030     setAttribute(Qt::WA_Hover);
0031 
0032     setAutoFillBackground(true);
0033     QGridLayout *l = new QGridLayout(this);
0034     l->setContentsMargins(10, 10, 10, 10);
0035 
0036     m_checkBox = new QCheckBox(this);
0037     m_variable = new QLabel(item->variable(), this);
0038     m_variable->setFocusPolicy(Qt::ClickFocus);
0039     m_variable->setFocusProxy(m_checkBox);
0040     m_btnHelp = new KateHelpButton(this);
0041     m_btnHelp->setIconState(KateHelpButton::IconHidden);
0042     m_btnHelp->setEnabled(false);
0043     m_btnHelp->setSection(QLatin1String("variable-") + item->variable());
0044 
0045     m_helpText = new QLabel(item->helpText(), this);
0046     m_helpText->setWordWrap(true);
0047 
0048     l->addWidget(m_checkBox, 0, 0, Qt::AlignLeft);
0049     l->addWidget(m_variable, 0, 1, Qt::AlignLeft);
0050     l->addWidget(m_btnHelp, 0, 3, Qt::AlignRight);
0051     l->addWidget(m_helpText, 1, 1, 1, 3);
0052 
0053     l->setColumnStretch(0, 0);
0054     l->setColumnStretch(1, 1);
0055     l->setColumnStretch(2, 1);
0056     l->setColumnStretch(3, 0);
0057 
0058     connect(m_checkBox, &QCheckBox::toggled, this, &VariableEditor::itemEnabled);
0059     m_checkBox->setChecked(item->isActive());
0060 
0061     connect(m_checkBox, &QCheckBox::toggled, this, &VariableEditor::valueChanged);
0062     setMouseTracking(true);
0063 }
0064 
0065 void VariableEditor::enterEvent(QEvent *event)
0066 {
0067     QWidget::enterEvent(event);
0068     m_btnHelp->setIconState(KateHelpButton::IconColored);
0069     m_btnHelp->setEnabled(true);
0070 
0071     update();
0072 }
0073 
0074 void VariableEditor::leaveEvent(QEvent *event)
0075 {
0076     QWidget::leaveEvent(event);
0077     m_btnHelp->setIconState(KateHelpButton::IconHidden);
0078     m_btnHelp->setEnabled(false);
0079 
0080     update();
0081 }
0082 
0083 void VariableEditor::paintEvent(QPaintEvent *event)
0084 {
0085     QWidget::paintEvent(event);
0086 
0087     // draw highlighting rect like in plasma
0088     if (underMouse()) {
0089         QPainter painter(this);
0090 
0091         painter.setRenderHint(QPainter::Antialiasing);
0092 
0093         QColor cornerColor = palette().color(QPalette::Highlight);
0094         cornerColor.setAlphaF(0.2);
0095 
0096         QColor midColor = palette().color(QPalette::Highlight);
0097         midColor.setAlphaF(0.5);
0098 
0099         QRect highlightRect = rect().adjusted(2, 2, -2, -2);
0100 
0101         QPen outlinePen;
0102         outlinePen.setWidth(2);
0103 
0104         QLinearGradient gradient(highlightRect.topLeft(), highlightRect.topRight());
0105         gradient.setColorAt(0, cornerColor);
0106         gradient.setColorAt(0.3, midColor);
0107         gradient.setColorAt(1, cornerColor);
0108         outlinePen.setBrush(gradient);
0109         painter.setPen(outlinePen);
0110 
0111         const int radius = 5;
0112         painter.drawRoundedRect(highlightRect, radius, radius);
0113     }
0114 }
0115 
0116 void VariableEditor::itemEnabled(bool enabled)
0117 {
0118     if (enabled) {
0119         m_variable->setText(QLatin1String("<b>") + m_item->variable() + QLatin1String("</b>"));
0120     } else {
0121         m_variable->setText(m_item->variable());
0122     }
0123     m_item->setActive(enabled);
0124 }
0125 
0126 void VariableEditor::activateItem()
0127 {
0128     m_checkBox->setChecked(true);
0129 }
0130 
0131 VariableItem *VariableEditor::item() const
0132 {
0133     return m_item;
0134 }
0135 // END VariableEditor
0136 
0137 // BEGIN VariableUintEditor
0138 VariableIntEditor::VariableIntEditor(VariableIntItem *item, QWidget *parent)
0139     : VariableEditor(item, parent)
0140 {
0141     QGridLayout *l = (QGridLayout *)layout();
0142 
0143     m_spinBox = new QSpinBox(this);
0144     m_spinBox->setValue(item->value());
0145     m_spinBox->setMinimum(item->minValue());
0146     m_spinBox->setMaximum(item->maxValue());
0147 
0148     l->addWidget(m_spinBox, 0, 2, Qt::AlignLeft);
0149 
0150     connect(m_spinBox, qOverload<int>(&QSpinBox::valueChanged), this, &VariableIntEditor::valueChanged);
0151     connect(m_spinBox, qOverload<int>(&QSpinBox::valueChanged), this, &VariableIntEditor::activateItem);
0152     connect(m_spinBox, qOverload<int>(&QSpinBox::valueChanged), this, &VariableIntEditor::setItemValue);
0153 }
0154 
0155 void VariableIntEditor::setItemValue(int newValue)
0156 {
0157     static_cast<VariableIntItem *>(item())->setValue(newValue);
0158 }
0159 // END VariableUintEditor
0160 
0161 // BEGIN VariableBoolEditor
0162 VariableBoolEditor::VariableBoolEditor(VariableBoolItem *item, QWidget *parent)
0163     : VariableEditor(item, parent)
0164 {
0165     QGridLayout *l = (QGridLayout *)layout();
0166 
0167     m_comboBox = new QComboBox(this);
0168     m_comboBox->addItem(i18n("true"));
0169     m_comboBox->addItem(i18n("false"));
0170     m_comboBox->setCurrentIndex(item->value() ? 0 : 1);
0171     l->addWidget(m_comboBox, 0, 2, Qt::AlignLeft);
0172 
0173     connect(m_comboBox, qOverload<int>(&QComboBox::currentIndexChanged), this, &VariableBoolEditor::valueChanged);
0174     connect(m_comboBox, qOverload<int>(&QComboBox::currentIndexChanged), this, &VariableBoolEditor::activateItem);
0175     connect(m_comboBox, qOverload<int>(&QComboBox::currentIndexChanged), this, &VariableBoolEditor::setItemValue);
0176 }
0177 
0178 void VariableBoolEditor::setItemValue(int enabled)
0179 {
0180     static_cast<VariableBoolItem *>(item())->setValue(enabled == 0);
0181 }
0182 // END VariableBoolEditor
0183 
0184 // BEGIN VariableStringListEditor
0185 VariableStringListEditor::VariableStringListEditor(VariableStringListItem *item, QWidget *parent)
0186     : VariableEditor(item, parent)
0187 {
0188     QGridLayout *l = (QGridLayout *)layout();
0189 
0190     m_comboBox = new QComboBox(this);
0191     m_comboBox->addItems(item->stringList());
0192     int index = 0;
0193     for (int i = 0; i < item->stringList().size(); ++i) {
0194         if (item->stringList().at(i) == item->value()) {
0195             index = i;
0196             break;
0197         }
0198     }
0199     m_comboBox->setCurrentIndex(index);
0200     l->addWidget(m_comboBox, 0, 2, Qt::AlignLeft);
0201 
0202     connect(m_comboBox, qOverload<int>(&QComboBox::currentIndexChanged), this, &VariableStringListEditor::valueChanged);
0203     connect(m_comboBox, qOverload<int>(&QComboBox::currentIndexChanged), this, &VariableStringListEditor::activateItem);
0204     connect(m_comboBox, &QComboBox::currentTextChanged, this, &VariableStringListEditor::setItemValue);
0205 }
0206 
0207 void VariableStringListEditor::setItemValue(const QString &newValue)
0208 {
0209     static_cast<VariableStringListItem *>(item())->setValue(newValue);
0210 }
0211 // END VariableStringListEditor
0212 
0213 // BEGIN VariableColorEditor
0214 VariableColorEditor::VariableColorEditor(VariableColorItem *item, QWidget *parent)
0215     : VariableEditor(item, parent)
0216 {
0217     QGridLayout *l = (QGridLayout *)layout();
0218 
0219     m_comboBox = new KColorCombo(this);
0220     m_comboBox->setColor(item->value());
0221     l->addWidget(m_comboBox, 0, 2, Qt::AlignLeft);
0222 
0223     connect(m_comboBox, &KColorCombo::activated, this, &VariableColorEditor::valueChanged);
0224     connect(m_comboBox, &KColorCombo::activated, this, &VariableColorEditor::activateItem);
0225     connect(m_comboBox, &KColorCombo::activated, this, &VariableColorEditor::setItemValue);
0226 }
0227 
0228 void VariableColorEditor::setItemValue(const QColor &newValue)
0229 {
0230     static_cast<VariableColorItem *>(item())->setValue(newValue);
0231 }
0232 // END VariableColorEditor
0233 
0234 // BEGIN VariableFontEditor
0235 VariableFontEditor::VariableFontEditor(VariableFontItem *item, QWidget *parent)
0236     : VariableEditor(item, parent)
0237 {
0238     QGridLayout *l = (QGridLayout *)layout();
0239 
0240     m_comboBox = new QFontComboBox(this);
0241     m_comboBox->setCurrentFont(item->value());
0242     l->addWidget(m_comboBox, 0, 2, Qt::AlignLeft);
0243 
0244     connect(m_comboBox, &QFontComboBox::currentFontChanged, this, &VariableFontEditor::valueChanged);
0245     connect(m_comboBox, &QFontComboBox::currentFontChanged, this, &VariableFontEditor::activateItem);
0246     connect(m_comboBox, &QFontComboBox::currentFontChanged, this, &VariableFontEditor::setItemValue);
0247 }
0248 
0249 void VariableFontEditor::setItemValue(const QFont &newValue)
0250 {
0251     static_cast<VariableFontItem *>(item())->setValue(newValue);
0252 }
0253 // END VariableFontEditor
0254 
0255 // BEGIN VariableStringEditor
0256 VariableStringEditor::VariableStringEditor(VariableStringItem *item, QWidget *parent)
0257     : VariableEditor(item, parent)
0258 {
0259     QGridLayout *l = (QGridLayout *)layout();
0260 
0261     m_lineEdit = new QLineEdit(this);
0262     m_lineEdit->setText(item->value());
0263     l->addWidget(m_lineEdit, 0, 2, Qt::AlignLeft);
0264 
0265     connect(m_lineEdit, &QLineEdit::textChanged, this, &VariableStringEditor::valueChanged);
0266     connect(m_lineEdit, &QLineEdit::textChanged, this, &VariableStringEditor::activateItem);
0267     connect(m_lineEdit, &QLineEdit::textChanged, this, &VariableStringEditor::setItemValue);
0268 }
0269 
0270 void VariableStringEditor::setItemValue(const QString &newValue)
0271 {
0272     static_cast<VariableStringItem *>(item())->setValue(newValue);
0273 }
0274 // END VariableStringEditor
0275 
0276 // BEGIN VariableSpellCheckEditor
0277 VariableSpellCheckEditor::VariableSpellCheckEditor(VariableSpellCheckItem *item, QWidget *parent)
0278     : VariableEditor(item, parent)
0279 {
0280     QGridLayout *l = (QGridLayout *)layout();
0281 
0282     m_dictionaryCombo = new Sonnet::DictionaryComboBox(this);
0283     m_dictionaryCombo->setCurrentByDictionary(item->value());
0284     l->addWidget(m_dictionaryCombo, 0, 2, Qt::AlignLeft);
0285 
0286     connect(m_dictionaryCombo, &Sonnet::DictionaryComboBox::dictionaryNameChanged, this, &VariableSpellCheckEditor::valueChanged);
0287     connect(m_dictionaryCombo, &Sonnet::DictionaryComboBox::dictionaryNameChanged, this, &VariableSpellCheckEditor::activateItem);
0288     connect(m_dictionaryCombo, &Sonnet::DictionaryComboBox::dictionaryChanged, this, &VariableSpellCheckEditor::setItemValue);
0289 }
0290 
0291 void VariableSpellCheckEditor::setItemValue(const QString &newValue)
0292 {
0293     static_cast<VariableSpellCheckItem *>(item())->setValue(newValue);
0294 }
0295 
0296 // END VariableSpellCheckEditor
0297 
0298 // BEGIN VariableRemoveSpacesEditor
0299 VariableRemoveSpacesEditor::VariableRemoveSpacesEditor(VariableRemoveSpacesItem *item, QWidget *parent)
0300     : VariableEditor(item, parent)
0301 {
0302     QGridLayout *l = (QGridLayout *)layout();
0303 
0304     m_comboBox = new QComboBox(this);
0305     m_comboBox->addItem(i18nc("value for variable remove-trailing-spaces", "none"));
0306     m_comboBox->addItem(i18nc("value for variable remove-trailing-spaces", "modified"));
0307     m_comboBox->addItem(i18nc("value for variable remove-trailing-spaces", "all"));
0308     m_comboBox->setCurrentIndex(item->value());
0309     l->addWidget(m_comboBox, 0, 2, Qt::AlignLeft);
0310 
0311     connect(m_comboBox, qOverload<int>(&QComboBox::currentIndexChanged), this, &VariableRemoveSpacesEditor::valueChanged);
0312     connect(m_comboBox, qOverload<int>(&QComboBox::currentIndexChanged), this, &VariableRemoveSpacesEditor::activateItem);
0313     connect(m_comboBox, qOverload<int>(&QComboBox::currentIndexChanged), this, &VariableRemoveSpacesEditor::setItemValue);
0314 }
0315 
0316 void VariableRemoveSpacesEditor::setItemValue(int enabled)
0317 {
0318     static_cast<VariableRemoveSpacesItem *>(item())->setValue(enabled == 0);
0319 }
0320 // END VariableRemoveSpacesEditor
0321 
0322 #include "moc_variableeditor.cpp"