VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/wizards/newvm/UIWizardNewVMPageBasicInstallSetup.cpp@ 84918

Last change on this file since 84918 was 84918, checked in by vboxsync, 5 years ago

FE/Qt: bugref:9515. Painting user name/password line edits to indicate any error.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.9 KB
Line 
1/* $Id: UIWizardNewVMPageBasicInstallSetup.cpp 84918 2020-06-23 13:12:08Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIWizardNewVMPageBasicInstallSetup class implementation.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/* Qt includes: */
19#include <QIntValidator>
20#include <QVBoxLayout>
21#include <QGridLayout>
22#include <QSpacerItem>
23#include <QLabel>
24#include <QSpinBox>
25#include <QStyle>
26#include <QToolButton>
27
28/* GUI includes: */
29#include "QIRichTextLabel.h"
30#include "UIBaseMemoryEditor.h"
31#include "UIBaseMemorySlider.h"
32#include "UICommon.h"
33#include "UIIconPool.h"
34#include "UIVirtualCPUEditor.h"
35#include "UIWizardNewVMPageBasicInstallSetup.h"
36#include "UIWizardNewVM.h"
37
38UIPasswordLineEdit::UIPasswordLineEdit(QWidget *pParent /*= 0 */)
39 :QLineEdit(pParent)
40 , m_pTextVisibilityButton(0)
41{
42 prepare();
43}
44
45void UIPasswordLineEdit::toggleTextVisibility(bool fTextVisible)
46{
47 if (fTextVisible)
48 {
49 setEchoMode(QLineEdit::Normal);
50 if(m_pTextVisibilityButton)
51 m_pTextVisibilityButton->setIcon(UIIconPool::iconSet(":/eye-off.png"));
52 return;
53 }
54 setEchoMode(QLineEdit::Password);
55 if(m_pTextVisibilityButton)
56 m_pTextVisibilityButton->setIcon(UIIconPool::iconSet(":/eye-on.png"));
57}
58
59void UIPasswordLineEdit::prepare()
60{
61 m_pTextVisibilityButton = new QToolButton(this);
62 m_pTextVisibilityButton->setFocusPolicy(Qt::ClickFocus);
63 m_pTextVisibilityButton->setAutoRaise(true);
64 m_pTextVisibilityButton->setCursor(Qt::ArrowCursor);
65 m_pTextVisibilityButton->show();
66 connect(m_pTextVisibilityButton, &QToolButton::clicked, this, &UIPasswordLineEdit::sltHandleTextVisibilityChange);
67 toggleTextVisibility(false);
68}
69
70void UIPasswordLineEdit::paintEvent(QPaintEvent *pevent)
71{
72 QLineEdit::paintEvent(pevent);
73 int iFrameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
74 int iSize = height() - 2 * iFrameWidth;
75 m_pTextVisibilityButton->setGeometry(width() - iSize, iFrameWidth, iSize, iSize);
76
77}
78
79void UIPasswordLineEdit::sltHandleTextVisibilityChange()
80{
81 bool fTextVisible = false;
82 if (echoMode() == QLineEdit::Normal)
83 fTextVisible = false;
84 else
85 fTextVisible = true;
86 toggleTextVisibility(fTextVisible);
87 emit sigTextVisibilityToggled(fTextVisible);
88}
89
90UIUserNamePasswordEditor::UIUserNamePasswordEditor(QWidget *pParent /* = 0 */)
91 : QIWithRetranslateUI<QWidget>(pParent)
92 , m_pUserNameLineEdit(0)
93 , m_pPasswordLineEdit(0)
94 , m_pPasswordRepeatLineEdit(0)
95 , m_pUserNameLabel(0)
96 , m_pPasswordLabel(0)
97 , m_pPasswordRepeatLabel(0)
98{
99 prepare();
100}
101
102QString UIUserNamePasswordEditor::userName() const
103{
104 if (m_pUserNameLineEdit)
105 return m_pUserNameLineEdit->text();
106 return QString();
107}
108
109void UIUserNamePasswordEditor::setUserName(const QString &strUserName)
110{
111 if (m_pUserNameLineEdit)
112 return m_pUserNameLineEdit->setText(strUserName);
113}
114
115QString UIUserNamePasswordEditor::password() const
116{
117 if (m_pPasswordLineEdit)
118 return m_pPasswordLineEdit->text();
119 return QString();
120}
121
122void UIUserNamePasswordEditor::setPassword(const QString &strPassword)
123{
124 if (m_pPasswordLineEdit)
125 m_pPasswordLineEdit->setText(strPassword);
126 if (m_pPasswordRepeatLineEdit)
127 m_pPasswordRepeatLineEdit->setText(strPassword);
128}
129
130bool UIUserNamePasswordEditor::isComplete()
131{
132 if (m_pUserNameLineEdit && m_pUserNameLineEdit->text().isEmpty())
133 {
134 markLineEdit(m_pUserNameLineEdit, true);
135 return false;
136 }
137 else
138 markLineEdit(m_pUserNameLineEdit, false);
139 bool fPasswordOK = true;
140 if (m_pPasswordLineEdit && m_pPasswordRepeatLineEdit)
141 {
142 if (m_pPasswordLineEdit->text() != m_pPasswordRepeatLineEdit->text())
143 fPasswordOK = false;
144 if (m_pPasswordLineEdit->text().isEmpty())
145 fPasswordOK = false;
146 }
147 markLineEdit(m_pPasswordLineEdit, !fPasswordOK);
148 markLineEdit(m_pPasswordRepeatLineEdit, !fPasswordOK);
149 return fPasswordOK;
150}
151
152void UIUserNamePasswordEditor::retranslateUi()
153{
154 if (m_pUserNameLabel)
155 {
156 m_pUserNameLabel->setText(UIWizardNewVM::tr("User Name:"));
157 m_pUserNameLabel->setToolTip(UIWizardNewVM::tr("Type the user name which will be used in attended install:"));
158
159 }
160 if (m_pPasswordLabel)
161 {
162 m_pPasswordLabel->setText(UIWizardNewVM::tr("Password:"));
163 m_pPasswordLabel->setToolTip(UIWizardNewVM::tr("Type the password for the user name"));
164
165 }
166 if (m_pPasswordRepeatLabel)
167 {
168 m_pPasswordRepeatLabel->setText(UIWizardNewVM::tr("Repeat Password:"));
169 m_pPasswordRepeatLabel->setToolTip(UIWizardNewVM::tr("Retype the password:"));
170 }
171}
172
173template <class T>
174void UIUserNamePasswordEditor::addLineEdit(int &iRow, QLabel *&pLabel, T *&pLineEdit, QGridLayout *pLayout)
175{
176 if (!pLayout || pLabel || pLineEdit)
177 return;
178 pLabel = new QLabel;
179 if (!pLabel)
180 return;
181 pLayout->addWidget(pLabel, iRow, 0, 1, 1, Qt::AlignRight);
182
183 pLineEdit = new T;
184 if (!pLineEdit)
185 return;
186 pLayout->addWidget(pLineEdit, iRow, 1, 1, 1);
187
188 pLabel->setBuddy(pLineEdit);
189 ++iRow;
190 connect(pLineEdit, &T::textChanged, this, &UIUserNamePasswordEditor::sigSomeTextChanged);
191 return;
192}
193
194void UIUserNamePasswordEditor::markLineEdit(QLineEdit *pLineEdit, bool fError)
195{
196 if (!pLineEdit)
197 return;
198 QPalette palette = pLineEdit->palette();
199 if (fError)
200 palette.setColor(QPalette::Base, QColor(255, 180, 180));
201 else
202 palette.setColor(QPalette::Base, m_orginalLineEditBaseColor);
203 pLineEdit->setPalette(palette);
204}
205
206void UIUserNamePasswordEditor::prepare()
207{
208 QGridLayout *pMainLayout = new QGridLayout;
209 if (!pMainLayout)
210 return;
211 setLayout(pMainLayout);
212 int iRow = 0;
213 addLineEdit<QLineEdit>(iRow, m_pUserNameLabel, m_pUserNameLineEdit, pMainLayout);
214 addLineEdit<UIPasswordLineEdit>(iRow, m_pPasswordLabel, m_pPasswordLineEdit, pMainLayout);
215 addLineEdit<UIPasswordLineEdit>(iRow, m_pPasswordRepeatLabel, m_pPasswordRepeatLineEdit, pMainLayout);
216
217 connect(m_pPasswordLineEdit, &UIPasswordLineEdit::sigTextVisibilityToggled,
218 this, &UIUserNamePasswordEditor::sltHandlePasswordVisibility);
219 connect(m_pPasswordRepeatLineEdit, &UIPasswordLineEdit::sigTextVisibilityToggled,
220 this, &UIUserNamePasswordEditor::sltHandlePasswordVisibility);
221 /* Cache the original back color of the line edit to restore it correctly: */
222 if (m_pUserNameLineEdit)
223 m_orginalLineEditBaseColor = m_pUserNameLineEdit->palette().color(QPalette::Base);
224
225 retranslateUi();
226}
227
228void UIUserNamePasswordEditor::sltHandlePasswordVisibility(bool fPasswordVisible)
229{
230 if (m_pPasswordLineEdit)
231 m_pPasswordLineEdit->toggleTextVisibility(fPasswordVisible);
232 if (m_pPasswordRepeatLineEdit)
233 m_pPasswordRepeatLineEdit->toggleTextVisibility(fPasswordVisible);
234}
235
236UIWizardNewVMPageInstallSetup::UIWizardNewVMPageInstallSetup()
237 : m_pUserNamePasswordEditor(0)
238{
239}
240
241QString UIWizardNewVMPageInstallSetup::userName() const
242{
243 if (m_pUserNamePasswordEditor)
244 return m_pUserNamePasswordEditor->userName();
245 return QString();
246}
247
248QString UIWizardNewVMPageInstallSetup::password() const
249{
250 if (m_pUserNamePasswordEditor)
251 return m_pUserNamePasswordEditor->password();
252 return QString();
253}
254
255
256UIWizardNewVMPageBasicInstallSetup::UIWizardNewVMPageBasicInstallSetup()
257 : m_pLabel(0)
258{
259 /* Create widget: */
260 QVBoxLayout *pMainLayout = new QVBoxLayout(this);
261 {
262 m_pLabel = new QIRichTextLabel(this);
263 QGridLayout *pMemoryLayout = new QGridLayout;
264 {
265 m_pUserNamePasswordEditor = new UIUserNamePasswordEditor;
266 pMemoryLayout->addWidget(m_pUserNamePasswordEditor, 0, 0);
267 connect(m_pUserNamePasswordEditor, &UIUserNamePasswordEditor::sigSomeTextChanged,
268 this, &UIWizardNewVMPageBasicInstallSetup::completeChanged);
269 }
270 if (m_pLabel)
271 pMainLayout->addWidget(m_pLabel);
272 pMainLayout->addLayout(pMemoryLayout);
273 pMainLayout->addStretch();
274 }
275
276
277 /* Register fields: */
278 registerField("userName", this, "userName");
279 registerField("password", this, "password");
280}
281
282void UIWizardNewVMPageBasicInstallSetup::setDefaultUnattendedInstallData(const UIUnattendedInstallData &unattendedInstallData)
283{
284 /* Initialize the widget data: */
285
286 if (m_pUserNamePasswordEditor)
287 {
288 m_pUserNamePasswordEditor->setUserName(unattendedInstallData.m_strUserName);
289 m_pUserNamePasswordEditor->setPassword(unattendedInstallData.m_strPassword);
290 }
291}
292
293void UIWizardNewVMPageBasicInstallSetup::retranslateUi()
294{
295 /* Translate page: */
296 setTitle(UIWizardNewVM::tr("User Name/Password and Time Zone Selection"));
297
298 /* Translate widgets: */
299 if (m_pLabel)
300 m_pLabel->setText(UIWizardNewVM::tr("<p>Here you can specify the user name/password and time zone. "
301 "The values you enter here will be used during the unattended install.</p>"));
302}
303
304void UIWizardNewVMPageBasicInstallSetup::initializePage()
305{
306 /* Translate page: */
307 retranslateUi();
308
309 /* Get recommended 'ram' field value: */
310 // CGuestOSType type = field("type").value<CGuestOSType>();
311 // m_pBaseMemoryEditor->setValue(type.GetRecommendedRAM());
312 // m_pVirtualCPUEditor->setValue(1);
313
314 // /* 'Ram' field should have focus initially: */
315 // m_pBaseMemoryEditor->setFocus();
316}
317
318bool UIWizardNewVMPageBasicInstallSetup::isComplete() const
319{
320 if (m_pUserNamePasswordEditor)
321 return m_pUserNamePasswordEditor->isComplete();
322 return true;
323}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette