1 | /* $Id: UIHostnameDomainNameEditor.cpp 99207 2023-03-29 12:07:11Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIHostnameDomainNameEditor class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | /* Qt includes: */
|
---|
29 | #include <QGridLayout>
|
---|
30 | #include <QLabel>
|
---|
31 | #include <QLineEdit>
|
---|
32 | #include <QRegularExpressionValidator>
|
---|
33 | #include <QStyle>
|
---|
34 | #include <QVBoxLayout>
|
---|
35 |
|
---|
36 | /* GUI includes: */
|
---|
37 | #include "QILineEdit.h"
|
---|
38 | #include "QIRichTextLabel.h"
|
---|
39 | #include "QIToolButton.h"
|
---|
40 | #include "UICommon.h"
|
---|
41 | #include "UIIconPool.h"
|
---|
42 | #include "UIHostnameDomainNameEditor.h"
|
---|
43 |
|
---|
44 |
|
---|
45 | UIHostnameDomainNameEditor::UIHostnameDomainNameEditor(QWidget *pParent /* = 0 */)
|
---|
46 | : QIWithRetranslateUI<QWidget>(pParent)
|
---|
47 | , m_pHostnameLineEdit(0)
|
---|
48 | , m_pDomainNameLineEdit(0)
|
---|
49 | , m_pHostnameLabel(0)
|
---|
50 | , m_pDomainNameLabel(0)
|
---|
51 | , m_pMainLayout(0)
|
---|
52 | {
|
---|
53 | prepare();
|
---|
54 | }
|
---|
55 |
|
---|
56 | QString UIHostnameDomainNameEditor::hostname() const
|
---|
57 | {
|
---|
58 | if (m_pHostnameLineEdit)
|
---|
59 | return m_pHostnameLineEdit->text();
|
---|
60 | return QString();
|
---|
61 | }
|
---|
62 |
|
---|
63 | bool UIHostnameDomainNameEditor::isComplete() const
|
---|
64 | {
|
---|
65 | return m_pHostnameLineEdit && m_pHostnameLineEdit->hasAcceptableInput() &&
|
---|
66 | m_pDomainNameLineEdit && m_pDomainNameLineEdit->hasAcceptableInput();
|
---|
67 | }
|
---|
68 |
|
---|
69 | void UIHostnameDomainNameEditor::mark()
|
---|
70 | {
|
---|
71 | if (m_pHostnameLineEdit)
|
---|
72 | m_pHostnameLineEdit->mark(!m_pHostnameLineEdit->hasAcceptableInput(),
|
---|
73 | tr("Hostname should be at least 2 character long. "
|
---|
74 | "Allowed characters are alphanumerics, \"-\" and \".\""));
|
---|
75 | if (m_pDomainNameLineEdit)
|
---|
76 | m_pDomainNameLineEdit->mark(!m_pDomainNameLineEdit->hasAcceptableInput(),
|
---|
77 | tr("Domain name should be at least 2 character long. "
|
---|
78 | "Allowed characters are alphanumerics, \"-\" and \".\""));
|
---|
79 | }
|
---|
80 |
|
---|
81 | void UIHostnameDomainNameEditor::setHostname(const QString &strHostname)
|
---|
82 | {
|
---|
83 | if (m_pHostnameLineEdit)
|
---|
84 | m_pHostnameLineEdit->setText(strHostname);
|
---|
85 | }
|
---|
86 |
|
---|
87 | QString UIHostnameDomainNameEditor::domainName() const
|
---|
88 | {
|
---|
89 | if (m_pDomainNameLineEdit)
|
---|
90 | return m_pDomainNameLineEdit->text();
|
---|
91 | return QString();
|
---|
92 | }
|
---|
93 |
|
---|
94 | void UIHostnameDomainNameEditor::setDomainName(const QString &strDomain)
|
---|
95 | {
|
---|
96 | if (m_pDomainNameLineEdit)
|
---|
97 | m_pDomainNameLineEdit->setText(strDomain);
|
---|
98 | }
|
---|
99 |
|
---|
100 | QString UIHostnameDomainNameEditor::hostnameDomainName() const
|
---|
101 | {
|
---|
102 | if (m_pHostnameLineEdit && m_pDomainNameLineEdit)
|
---|
103 | return QString("%1.%2").arg(m_pHostnameLineEdit->text()).arg(m_pDomainNameLineEdit->text());
|
---|
104 | return QString();
|
---|
105 | }
|
---|
106 |
|
---|
107 | int UIHostnameDomainNameEditor::firstColumnWidth() const
|
---|
108 | {
|
---|
109 | int iWidth = 0;
|
---|
110 | if (m_pHostnameLabel)
|
---|
111 | iWidth = qMax(iWidth, m_pHostnameLabel->minimumSizeHint().width());
|
---|
112 | if (m_pDomainNameLabel)
|
---|
113 | iWidth = qMax(iWidth, m_pDomainNameLabel->minimumSizeHint().width());
|
---|
114 | return iWidth;
|
---|
115 | }
|
---|
116 |
|
---|
117 | void UIHostnameDomainNameEditor::setFirstColumnWidth(int iWidth)
|
---|
118 | {
|
---|
119 | if (m_pMainLayout)
|
---|
120 | m_pMainLayout->setColumnMinimumWidth(0, iWidth);
|
---|
121 | }
|
---|
122 |
|
---|
123 | void UIHostnameDomainNameEditor::retranslateUi()
|
---|
124 | {
|
---|
125 | if (m_pHostnameLabel)
|
---|
126 | m_pHostnameLabel->setText(tr("Hostna&me:"));
|
---|
127 | if (m_pHostnameLineEdit)
|
---|
128 | m_pHostnameLineEdit->setToolTip(tr("Holds the hostname."));
|
---|
129 | if (m_pDomainNameLabel)
|
---|
130 | m_pDomainNameLabel->setText(tr("&Domain Name:"));
|
---|
131 | if (m_pDomainNameLineEdit)
|
---|
132 | m_pDomainNameLineEdit->setToolTip(tr("Holds the domain name."));
|
---|
133 | }
|
---|
134 |
|
---|
135 | template<class T>
|
---|
136 | void UIHostnameDomainNameEditor::addLineEdit(int &iRow, QLabel *&pLabel, T *&pLineEdit, QGridLayout *pLayout)
|
---|
137 | {
|
---|
138 | AssertReturnVoid(pLayout);
|
---|
139 | if (pLabel || pLineEdit)
|
---|
140 | return;
|
---|
141 |
|
---|
142 | pLabel = new QLabel;
|
---|
143 | AssertReturnVoid(pLabel);
|
---|
144 | pLabel->setAlignment(Qt::AlignRight);
|
---|
145 | //pLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
|
---|
146 |
|
---|
147 | pLayout->addWidget(pLabel, iRow, 0, 1, 1);
|
---|
148 |
|
---|
149 | pLineEdit = new T;
|
---|
150 | AssertReturnVoid(pLineEdit);
|
---|
151 |
|
---|
152 | pLayout->addWidget(pLineEdit, iRow, 1, 1, 3);
|
---|
153 | pLabel->setBuddy(pLineEdit);
|
---|
154 | ++iRow;
|
---|
155 | return;
|
---|
156 | }
|
---|
157 |
|
---|
158 | void UIHostnameDomainNameEditor::prepare()
|
---|
159 | {
|
---|
160 | m_pMainLayout = new QGridLayout;
|
---|
161 | m_pMainLayout->setColumnStretch(0, 0);
|
---|
162 | m_pMainLayout->setColumnStretch(1, 1);
|
---|
163 | if (!m_pMainLayout)
|
---|
164 | return;
|
---|
165 | setLayout(m_pMainLayout);
|
---|
166 | int iRow = 0;
|
---|
167 | addLineEdit<UIMarkableLineEdit>(iRow, m_pHostnameLabel, m_pHostnameLineEdit, m_pMainLayout);
|
---|
168 | addLineEdit<QILineEdit>(iRow, m_pDomainNameLabel, m_pDomainNameLineEdit, m_pMainLayout);
|
---|
169 |
|
---|
170 | /* Host name and domain should be strings of minimum length of 2 and composed of alpha numerics, '-', and '.'
|
---|
171 | * Exclude strings with . at the end: */
|
---|
172 | m_pHostnameLineEdit->setValidator(new QRegularExpressionValidator(QRegularExpression("^[a-zA-Z0-9-.]{2,}[$a-zA-Z0-9-]"), this));
|
---|
173 | m_pDomainNameLineEdit->setValidator(new QRegularExpressionValidator(QRegularExpression("^[a-zA-Z0-9-.]{2,}[$a-zA-Z0-9-]"), this));
|
---|
174 |
|
---|
175 | connect(m_pHostnameLineEdit, &UIMarkableLineEdit::textChanged,
|
---|
176 | this, &UIHostnameDomainNameEditor::sltHostnameChanged);
|
---|
177 | connect(m_pDomainNameLineEdit, &QILineEdit::textChanged,
|
---|
178 | this, &UIHostnameDomainNameEditor::sltDomainChanged);
|
---|
179 |
|
---|
180 | retranslateUi();
|
---|
181 | }
|
---|
182 |
|
---|
183 | void UIHostnameDomainNameEditor::sltHostnameChanged()
|
---|
184 | {
|
---|
185 | m_pHostnameLineEdit->mark(!m_pHostnameLineEdit->hasAcceptableInput(),
|
---|
186 | tr("Hostname should be at least 2 character long. "
|
---|
187 | "Allowed characters are alphanumerics, \"-\" and \".\""));
|
---|
188 | emit sigHostnameDomainNameChanged(hostnameDomainName(), isComplete());
|
---|
189 | }
|
---|
190 |
|
---|
191 | void UIHostnameDomainNameEditor::sltDomainChanged()
|
---|
192 | {
|
---|
193 | m_pDomainNameLineEdit->mark(!m_pDomainNameLineEdit->hasAcceptableInput(),
|
---|
194 | tr("Domain name should be at least 2 character long. "
|
---|
195 | "Allowed characters are alphanumerics, \"-\" and \".\""));
|
---|
196 | emit sigHostnameDomainNameChanged(hostnameDomainName(), isComplete());
|
---|
197 | }
|
---|