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