File indexing completed on 2024-05-12 04:33:22

0001 /*
0002  * SPDX-FileCopyrightText: 2007-2011 Kare Sars <kare.sars@iki .fi>
0003  * SPDX-FileCopyrightText: 2014 Gregor Mitsch : port to KDE5 frameworks
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006  */
0007 
0008 // Local includes
0009 
0010 #include "labeledentry.h"
0011 
0012 // Qt includes
0013 
0014 #include <QHBoxLayout>
0015 #include <QLabel>
0016 #include <QLineEdit>
0017 #include <QPushButton>
0018 
0019 // KDE includes
0020 
0021 #include <KLocalizedString>
0022 
0023 namespace KSaneIface
0024 {
0025 
0026 LabeledEntry::LabeledEntry(QWidget *parent, const QString &ltext)
0027     : KSaneOptionWidget(parent, ltext)
0028 {
0029     initEntry();
0030 }
0031 
0032 LabeledEntry::LabeledEntry(QWidget *parent, KSaneCore::Option *option)
0033     : KSaneOptionWidget(parent, option)
0034 {
0035     initEntry();
0036     setToolTip(option->description());
0037     setLabelText(option->title());
0038     connect(this, &LabeledEntry::entryEdited, option, &KSaneCore::Option::setValue);
0039     connect(option, &KSaneCore::Option::valueChanged, this, &LabeledEntry::setValue);
0040     QString value = option->value().toString();
0041     m_entry->setText(value);
0042 }
0043 
0044 LabeledEntry::~LabeledEntry()
0045 {
0046 }
0047 
0048 void LabeledEntry::initEntry()
0049 {
0050     m_entry = new QLineEdit(this);
0051     m_reset = new QPushButton(this);
0052     m_reset->setText(i18nc("Label for button to reset text in a KLineEdit", "Reset"));
0053     m_set = new QPushButton(this);
0054     m_set->setText(i18nc("Label for button to write text in a KLineEdit to sane", "Set"));
0055 
0056     m_layout->addWidget(m_entry, 1, 0, 1, 2);
0057     m_layout->addWidget(m_reset, 1, 2);
0058     m_layout->addWidget(m_set, 1, 3);
0059     m_layout->setColumnStretch(1, 50);
0060 
0061     connect(m_reset, &QPushButton::clicked, this, &LabeledEntry::resetClicked);
0062     connect(m_set, &QPushButton::clicked, this, &LabeledEntry::setClicked);
0063 }
0064 
0065 void LabeledEntry::setText(const QString &text)
0066 {
0067     m_eText = text;
0068     m_entry->setText(text);
0069 }
0070 
0071 void LabeledEntry::setValue(const QVariant &value)
0072 {
0073     const QString text = value.toString();
0074     if (!text.isEmpty()) {
0075         setText(text);
0076     }
0077 }
0078 
0079 void LabeledEntry::resetClicked()
0080 {
0081     m_entry->setText(m_eText);
0082 }
0083 
0084 void LabeledEntry::setClicked()
0085 {
0086     m_eText = m_entry->text();
0087     Q_EMIT entryEdited(m_eText);
0088 }
0089 
0090 }  // NameSpace KSaneIface
0091 
0092 #include "moc_labeledentry.cpp"