1 /*
2  * Copyright (C) 2021 Damir Porobic <damir.porobic@gmx.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #include "BoolPicker.h"
21 
22 namespace kImageAnnotator {
23 
BoolPicker(QWidget * parent)24 BoolPicker::BoolPicker(QWidget *parent) :
25 	SettingsPickerWidget(parent),
26 	mToolButton(new ListMenuToolButton(this)),
27 	mLayout(new QHBoxLayout(this)),
28 	mLabel(new QLabel(this))
29 {
30 	initGui();
31 }
32 
setEnabledState(bool enabled)33 void BoolPicker::setEnabledState(bool enabled)
34 {
35 	mToolButton->setCurrentData(static_cast<bool>(enabled));
36 }
37 
enabledState() const38 bool BoolPicker::enabledState() const
39 {
40 	return mToolButton->currentData().value<bool>();
41 }
42 
setToolTip(const QString & toolTip)43 void BoolPicker::setToolTip(const QString &toolTip)
44 {
45 	mLabel->setToolTip(toolTip);
46 	QWidget::setToolTip(toolTip);
47 }
48 
setIcon(const QIcon & icon)49 void BoolPicker::setIcon(const QIcon &icon)
50 {
51 	mLabel->setPixmap(icon.pixmap(ScaledSizeProvider::settingsWidgetIconSize()));
52 }
53 
expandingWidget()54 QWidget *BoolPicker::expandingWidget()
55 {
56 	return mToolButton;
57 }
58 
initGui()59 void BoolPicker::initGui()
60 {
61 	mLayout->setContentsMargins(0, 0, 0, 0);
62 
63 	insertItem(true, QLatin1String("check.svg"), tr("Enabled"));
64 	insertItem(false, QLatin1String("disabled.svg"), tr("Disabled"));
65 
66 	mToolButton->setFocusPolicy(Qt::NoFocus);
67 	connect(mToolButton, &ListMenuToolButton::selectionChanged, this, &BoolPicker::selectionChanged);
68 
69 	mLayout->addWidget(mLabel);
70 	mLayout->addWidget(mToolButton);
71 	mLayout->setAlignment(Qt::AlignLeft);
72 
73 	setLayout(mLayout);
74 }
75 
insertItem(bool enabled,const QString & iconName,const QString & text)76 void BoolPicker::insertItem(bool enabled, const QString &iconName, const QString &text)
77 {
78 	auto icon = IconLoader::load(iconName);
79 	mToolButton->addItem(icon, text, static_cast<bool>(enabled));
80 }
81 
selectionChanged()82 void BoolPicker::selectionChanged()
83 {
84 	auto enabled = mToolButton->currentData().value<bool>();
85 	emit enabledStateChanged(enabled);
86 }
87 
88 } // namespace kImageAnnotator
89