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

0001 /*
0002  * SPDX-FileCopyrightText: 2011 Kare Sars <kare.sars@iki.fi>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 #include "ksaneoptionwidget.h"
0008 
0009 // KDE includes
0010 
0011 #include <KLocalizedString>
0012 
0013 namespace KSaneIface
0014 {
0015 
0016 KSaneOptionWidget::KSaneOptionWidget(QWidget *parent, const QString &labelText)
0017     : QWidget(parent)
0018 {
0019     m_label = new QLabel(this);
0020     setLabelText(labelText);
0021     initWidget();
0022 }
0023 
0024 KSaneOptionWidget::KSaneOptionWidget(QWidget *parent, KSaneCore::Option *option)
0025     : QWidget(parent)
0026 {
0027     m_option = option;
0028     m_label = new QLabel;
0029     connect(option, &KSaneCore::Option::optionReloaded, this, &KSaneOptionWidget::updateVisibility);
0030     initWidget();
0031 }
0032 
0033 KSaneOptionWidget::~KSaneOptionWidget()
0034 {
0035 }
0036 
0037 void KSaneOptionWidget::initWidget()
0038 {
0039     m_layout = new QGridLayout(this);
0040     m_layout->addWidget(m_label, 0, 0, Qt::AlignRight);
0041     m_layout->setColumnStretch(0, 0);
0042     m_layout->setContentsMargins(0, 0, 0, 0);
0043     updateVisibility();
0044 
0045 }
0046 
0047 void KSaneOptionWidget::updateVisibility()
0048 {
0049     if (!m_option) {
0050         return;
0051     }
0052 
0053     if (m_option->state() == KSaneCore::Option::StateHidden) {
0054         hide();
0055     } else {
0056         show();
0057         setEnabled(m_option->state() == KSaneCore::Option::StateActive);
0058     }
0059 }
0060 
0061 void KSaneOptionWidget::setLabelText(const QString &text)
0062 {
0063     if (text.isEmpty()) {
0064         m_label->clear();
0065     } else {
0066         m_label->setText(i18nc("Label for a scanner option", "%1:", text));
0067     }
0068 }
0069 
0070 int KSaneOptionWidget::labelWidthHint()
0071 {
0072     return m_label->sizeHint().width();
0073 }
0074 
0075 void KSaneOptionWidget::setLabelWidth(int labelWidth)
0076 {
0077     m_layout->setColumnMinimumWidth(0, labelWidth);
0078 }
0079 
0080 }  // NameSpace KSaneIface
0081 
0082 #include "moc_ksaneoptionwidget.cpp"