Akonadi Contacts

webwidget.cpp
1 /*
2  This file is part of Contact Editor.
3 
4  SPDX-FileCopyrightText: 2016 eyeOS S.L.U., a Telefonica company, [email protected]
5  SPDX-FileCopyrightText: 2016-2020 Laurent Montel <montel.org>
6 
7  SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9 
10 #include "webwidget.h"
11 #include "webselecttypecombobox.h"
12 #include <KLocalizedString>
13 #include <QHBoxLayout>
14 #include <QToolButton>
15 #include <QUrl>
16 #include <editor/widgets/preferredlineeditwidget.h>
17 using namespace ContactEditor;
18 
19 WebWidget::WebWidget(QWidget *parent)
20  : QWidget(parent)
21  , mWebSiteEdit(new PreferredLineEditWidget(this))
22  , mAddButton(new QToolButton(this))
23  , mRemoveButton(new QToolButton(this))
24  , mWebType(new ContactEditor::WebSelectTypeCombobox(this))
25 {
26  auto layout = new QHBoxLayout(this);
27  layout->setSpacing(0);
28  layout->setContentsMargins({});
29 
30  mWebSiteEdit->setTrapReturnKey(true);
31  mWebSiteEdit->setPlaceholderText(i18n("Add a Web Site"));
32  mWebSiteEdit->setObjectName(QStringLiteral("website"));
33  layout->addWidget(mWebSiteEdit);
34  connect(mWebSiteEdit, &PreferredLineEditWidget::preferredChanged, this, &WebWidget::slotPreferredChanged);
35 
36  mWebType->setObjectName(QStringLiteral("webtype"));
37  layout->addWidget(mWebType);
38 
39  mAddButton->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
40  mAddButton->setObjectName(QStringLiteral("addbutton"));
41  mAddButton->setToolTip(i18n("Add a Web Site"));
42  connect(mAddButton, &QToolButton::clicked, this, &WebWidget::slotAddWeb);
43  layout->addWidget(mAddButton);
44 
45  mRemoveButton->setObjectName(QStringLiteral("removebutton"));
46  mRemoveButton->setIcon(QIcon::fromTheme(QStringLiteral("list-remove")));
47  mRemoveButton->setToolTip(i18n("Remove Web Site"));
48  connect(mRemoveButton, &QToolButton::clicked, this, &WebWidget::slotRemoveWeb);
49  layout->addWidget(mRemoveButton);
50 }
51 
52 WebWidget::~WebWidget() = default;
53 
54 void WebWidget::clearWidget()
55 {
56  mWebSiteEdit->clear();
57  mWebType->setCurrentIndex(0);
58 }
59 
60 void WebWidget::updateAddRemoveButton(bool addButtonEnabled)
61 {
62  mAddButton->setEnabled(addButtonEnabled);
63 }
64 
65 void WebWidget::slotAddWeb()
66 {
67  Q_EMIT addWidget(this);
68 }
69 
70 void WebWidget::slotRemoveWeb()
71 {
72  Q_EMIT removeWidget(this);
73 }
74 
75 void WebWidget::setPreferred(bool b)
76 {
77  mWebSiteEdit->setPreferred(b);
78 }
79 
80 KContacts::ResourceLocatorUrl WebWidget::url()
81 {
82  mUrl.setUrl(QUrl(mWebSiteEdit->text().trimmed()));
83  mUrl.setPreferred(mWebSiteEdit->preferred());
84  mUrl.setType(KContacts::ResourceLocatorUrl::Type(mWebType->currentData().toInt()));
85  return mUrl;
86 }
87 
88 void WebWidget::loadWebSite(const KContacts::ResourceLocatorUrl &url)
89 {
90  mUrl = url;
91  setPreferred(url.isPreferred());
92  mWebType->setCurrentIndex(mWebType->findData((int)url.type()));
93  mWebSiteEdit->setText(mUrl.url().toDisplayString());
94 }
95 
96 void WebWidget::setReadOnly(bool readOnly)
97 {
98  mWebSiteEdit->setReadOnly(readOnly);
99  mAddButton->setEnabled(!readOnly);
100  mRemoveButton->setEnabled(!readOnly);
101  mWebType->setEnabled(!readOnly);
102 }
103 
104 void WebWidget::slotPreferredChanged()
105 {
106  Q_EMIT preferredChanged(this);
107 }
void clicked(bool checked)
QIcon fromTheme(const QString &name)
QString i18n(const char *text, const TYPE &arg...)
void setPreferred(bool preferred)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sat Apr 1 2023 04:09:05 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.