1 /*
2 * SPDX-FileCopyrightText: 2012 Sebastian Gottfried <sebastiangottfried@web.de>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "newkeyboardlayoutwidget.h"
8
9 #include <KLocalizedString>
10
11 #include "core/dataindex.h"
12 #include "models/resourcemodel.h"
13
14 #include "preferences.h"
15
16 #ifdef KTOUCH_BUILD_WITH_X11
17 #include "x11_helper.h"
18 #endif
19
20
NewKeyboardLayoutWidget(ResourceModel * resourceModel,QWidget * parent)21 NewKeyboardLayoutWidget::NewKeyboardLayoutWidget(ResourceModel* resourceModel, QWidget* parent) :
22 QWidget(parent),
23 Ui::NewKeyboardLayoutWidget(),
24 m_resourceModel(resourceModel)
25 {
26 setupUi(this);
27
28 connect(m_nameLineEdit, &QLineEdit::textChanged, this, &NewKeyboardLayoutWidget::checkName);
29 connect(m_nameLineEdit, &QLineEdit::textChanged, this, &NewKeyboardLayoutWidget::isValidChanged);
30 connect(m_titleLineEdit, &QLineEdit::textChanged, this, &NewKeyboardLayoutWidget::isValidChanged);
31 connect(m_pasteCurrentNameButton, &QAbstractButton::clicked, this, &NewKeyboardLayoutWidget::pasteCurrentName);
32
33 m_messageWidget->setMessageType(KMessageWidget::Error);
34 m_messageWidget->setCloseButtonVisible(false);
35 m_messageWidget->hide();
36 }
37
name() const38 QString NewKeyboardLayoutWidget::name() const
39 {
40 return m_nameLineEdit->text();
41 }
42
title() const43 QString NewKeyboardLayoutWidget::title() const
44 {
45 return m_titleLineEdit->text();
46 }
47
isValid() const48 bool NewKeyboardLayoutWidget::isValid() const
49 {
50 if (m_nameLineEdit->text().isEmpty())
51 return false;
52
53 if (m_titleLineEdit->text().isEmpty())
54 return false;
55
56 return m_nameIsValid;
57 }
58
pasteCurrentName()59 void NewKeyboardLayoutWidget::pasteCurrentName()
60 {
61 #ifdef KTOUCH_BUILD_WITH_X11
62 const QString name = X11Helper::getCurrentLayout().toString();
63 #else
64 const QString name = Preferences::keyboardLayoutName();
65 #endif
66 m_nameLineEdit->setText(name);
67 m_nameLineEdit->setFocus();
68 }
69
checkName()70 void NewKeyboardLayoutWidget::checkName()
71 {
72 const QString name = m_nameLineEdit->text();
73 DataIndex* const dataIndex = m_resourceModel->dataIndex();
74
75 m_nameIsValid = true;
76
77 for (int i = 0; i < dataIndex->keyboardLayoutCount(); i++)
78 {
79 DataIndexKeyboardLayout* const layout = dataIndex->keyboardLayout(i);
80
81 if (layout->source() == DataIndex::UserResource && layout->name() == name)
82 {
83 m_messageWidget->setText(i18n("There is already a keyboard layout with the same name."));
84 m_nameIsValid = false;
85 break;
86 }
87 }
88
89 if (m_nameIsValid)
90 m_messageWidget->animatedHide();
91
92 if (!m_nameIsValid)
93 m_messageWidget->animatedShow();
94
95 emit isValidChanged();
96 }
97