1 /* ============================================================
2  *
3  * SPDX-FileCopyrightText: 2011 Kare Sars <kare.sars@iki.fi>
4  *
5  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6  *
7  * ============================================================ */
8 
9 #include "ksaneoptionwidget.h"
10 
11 // KDE includes
12 
13 #include <KLocalizedString>
14 
15 namespace KSaneIface
16 {
17 
KSaneOptionWidget(QWidget * parent,const QString & labelText)18 KSaneOptionWidget::KSaneOptionWidget(QWidget *parent, const QString &labelText)
19     : QWidget(parent)
20 {
21     m_label = new QLabel(this);
22     setLabelText(labelText);
23     initWidget();
24 }
25 
KSaneOptionWidget(QWidget * parent,KSaneOption * option)26 KSaneOptionWidget::KSaneOptionWidget(QWidget *parent, KSaneOption *option)
27     : QWidget(parent)
28 {
29     m_option = option;
30     m_label = new QLabel;
31     connect(option, &KSaneOption::optionReloaded, this, &KSaneOptionWidget::updateVisibility);
32     initWidget();
33 }
34 
~KSaneOptionWidget()35 KSaneOptionWidget::~KSaneOptionWidget()
36 {
37 }
38 
initWidget()39 void KSaneOptionWidget::initWidget()
40 {
41     m_layout = new QGridLayout(this);
42     m_layout->addWidget(m_label, 0, 0, Qt::AlignRight);
43     m_layout->setColumnStretch(0, 0);
44     m_layout->setContentsMargins(0, 0, 0, 0);
45     updateVisibility();
46 
47 }
48 
updateVisibility()49 void KSaneOptionWidget::updateVisibility()
50 {
51     if (!m_option) {
52         return;
53     }
54 
55     if (m_option->state() == KSaneOption::StateHidden) {
56         hide();
57     } else {
58         show();
59         setEnabled(m_option->state() == KSaneOption::StateActive);
60     }
61 }
62 
setLabelText(const QString & text)63 void KSaneOptionWidget::setLabelText(const QString &text)
64 {
65     if (text.isEmpty()) {
66         m_label->clear();
67     } else {
68         m_label->setText(i18nc("Label for a scanner option", "%1:", text));
69     }
70 }
71 
labelWidthHint()72 int KSaneOptionWidget::labelWidthHint()
73 {
74     return m_label->sizeHint().width();
75 }
76 
setLabelWidth(int labelWidth)77 void KSaneOptionWidget::setLabelWidth(int labelWidth)
78 {
79     m_layout->setColumnMinimumWidth(0, labelWidth);
80 }
81 
82 }  // NameSpace KSaneIface
83