VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/widgets/UIEmptyFilePathSelector.cpp

Last change on this file was 104533, checked in by vboxsync, 4 months ago

FE/Qt: bugref:10663, bugref:10664: Improve accessibility for UIEmptyFilePathSelector solely used for File editor of Import / Export Appliance wizards.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
Line 
1/* $Id: UIEmptyFilePathSelector.cpp 104533 2024-05-07 15:15:24Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIEmptyFilePathSelector class implementation.
4 */
5
6/*
7 * Copyright (C) 2008-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 <QAction>
30#include <QApplication>
31#include <QClipboard>
32#include <QDir>
33#include <QFocusEvent>
34#include <QHBoxLayout>
35#include <QLineEdit>
36#include <QTimer>
37
38/* GUI includes: */
39#include "QIFileDialog.h"
40#include "QIToolButton.h"
41#include "QILabel.h"
42#include "QILineEdit.h"
43#include "UIEmptyFilePathSelector.h"
44#include "UIIconPool.h"
45#include "UITranslationEventListener.h"
46
47UIEmptyFilePathSelector::UIEmptyFilePathSelector (QWidget *aParent /* = NULL */)
48 : QWidget(aParent)
49 , mPathWgt (NULL)
50 , mLabel (NULL)
51 , mMode (UIEmptyFilePathSelector::Mode_File_Open)
52 , mLineEdit (NULL)
53 , m_fLineEditoToolTipSet(false)
54 , m_fButtonToolTipSet(false)
55 , mHomeDir (QDir::current().absolutePath())
56 , mIsModified (false)
57{
58 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
59
60 mMainLayout = new QHBoxLayout (this);
61 mMainLayout->setContentsMargins(0, 0, 0, 0);
62#ifdef VBOX_WS_MAC
63 mMainLayout->setSpacing(5);
64#endif
65
66 mSelectButton = new QToolButton(this);
67#ifdef VBOX_WS_MAC
68 mSelectButton->setStyleSheet("QToolButton { border: 0px none black; margin: 0px 0px 0px 0px; } QToolButton::menu-indicator {image: none;}");
69#else
70 mSelectButton->setAutoRaise(true);
71#endif
72 mSelectButton->setIcon(UIIconPool::iconSet(":/select_file_16px.png", ":/select_file_disabled_16px.png"));
73 connect(mSelectButton, &QToolButton::clicked, this, &UIEmptyFilePathSelector::choose);
74 mMainLayout->addWidget(mSelectButton);
75
76 setEditable (false);
77
78 sltRetranslateUI();
79 connect(&translationEventListener(), &UITranslationEventListener::sigRetranslateUI,
80 this, &UIEmptyFilePathSelector::sltRetranslateUI);
81}
82
83void UIEmptyFilePathSelector::setMode (UIEmptyFilePathSelector::Mode aMode)
84{
85 mMode = aMode;
86}
87
88UIEmptyFilePathSelector::Mode UIEmptyFilePathSelector::mode() const
89{
90 return mMode;
91}
92
93void UIEmptyFilePathSelector::setButtonPosition (ButtonPosition aPos)
94{
95 if (aPos == LeftPosition)
96 {
97 mMainLayout->setDirection (QBoxLayout::LeftToRight);
98 setTabOrder (mSelectButton, mPathWgt);
99 }
100 else
101 {
102 mMainLayout->setDirection (QBoxLayout::RightToLeft);
103 setTabOrder (mPathWgt, mSelectButton);
104 }
105}
106
107UIEmptyFilePathSelector::ButtonPosition UIEmptyFilePathSelector::buttonPosition() const
108{
109 return mMainLayout->direction() == QBoxLayout::LeftToRight ? LeftPosition : RightPosition;
110}
111
112void UIEmptyFilePathSelector::setEditable (bool aOn)
113{
114 if (mPathWgt)
115 {
116 delete mPathWgt;
117 mLabel = NULL;
118 mLineEdit = NULL;
119 }
120
121 if (aOn)
122 {
123 mPathWgt = mLineEdit = new QILineEdit (this);
124 setFocusProxy(mLineEdit);
125 connect (mLineEdit, SIGNAL (textChanged (const QString&)),
126 this, SLOT (textChanged (const QString&)));
127 }
128 else
129 {
130 mPathWgt = mLabel = new QILabel (this);
131 mLabel->setWordWrap (true);
132 }
133 mMainLayout->addWidget (mPathWgt, 2);
134 setButtonPosition (buttonPosition());
135
136 setPath (mPath);
137}
138
139bool UIEmptyFilePathSelector::isEditable() const
140{
141 return mLabel ? false : true;
142}
143
144void UIEmptyFilePathSelector::setChooserVisible (bool aOn)
145{
146 mSelectButton->setVisible (aOn);
147}
148
149bool UIEmptyFilePathSelector::isChooserVisible() const
150{
151 return mSelectButton->isVisible();
152}
153
154void UIEmptyFilePathSelector::setPath (const QString& aPath)
155{
156 QString tmpPath = QDir::toNativeSeparators (aPath);
157 if (mLabel)
158 mLabel->setText (QString ("<compact elipsis=\"start\">%1</compact>").arg (tmpPath));
159 else if (mLineEdit)
160 mLineEdit->setText (tmpPath);
161 textChanged(tmpPath);
162}
163
164QString UIEmptyFilePathSelector::path() const
165{
166 return mPath;
167}
168
169void UIEmptyFilePathSelector::setDefaultSaveExt (const QString &aExt)
170{
171 mDefaultSaveExt = aExt;
172}
173
174QString UIEmptyFilePathSelector::defaultSaveExt() const
175{
176 return mDefaultSaveExt;
177}
178
179void UIEmptyFilePathSelector::setLineEditToolTip(const QString &strToolTip)
180{
181 if (mLineEdit)
182 {
183 m_fLineEditoToolTipSet = !strToolTip.isEmpty();
184 mLineEdit->setToolTip(strToolTip);
185 }
186}
187
188QString UIEmptyFilePathSelector::lineEditToolTip() const
189{
190 return mLineEdit ? mLineEdit->toolTip() : QString();
191}
192
193void UIEmptyFilePathSelector::setChooseButtonToolTip(const QString &strToolTip)
194{
195 m_fButtonToolTipSet = !strToolTip.isEmpty();
196 mSelectButton->setToolTip(strToolTip);
197 mSelectButton->setText(strToolTip);
198}
199
200QString UIEmptyFilePathSelector::chooseButtonToolTip() const
201{
202 return mSelectButton->toolTip();
203}
204
205void UIEmptyFilePathSelector::setFileDialogTitle (const QString& aTitle)
206{
207 mFileDialogTitle = aTitle;
208}
209
210QString UIEmptyFilePathSelector::fileDialogTitle() const
211{
212 return mFileDialogTitle;
213}
214
215void UIEmptyFilePathSelector::setFileFilters (const QString& aFilters)
216{
217 mFileFilters = aFilters;
218}
219
220QString UIEmptyFilePathSelector::fileFilters() const
221{
222 return mFileFilters;
223}
224
225void UIEmptyFilePathSelector::setHomeDir (const QString& aDir)
226{
227 mHomeDir = aDir;
228}
229
230QString UIEmptyFilePathSelector::homeDir() const
231{
232 return mHomeDir;
233}
234
235void UIEmptyFilePathSelector::sltRetranslateUI()
236{
237 if (mLineEdit && !m_fLineEditoToolTipSet)
238 mLineEdit->setToolTip(tr("Contains selected file path."));
239 if (!m_fButtonToolTipSet)
240 {
241 mSelectButton->setToolTip(tr("Choose..."));
242 mSelectButton->setText(tr("Choose..."));
243 }
244}
245
246void UIEmptyFilePathSelector::choose()
247{
248 QString path = mPath;
249
250 /* Check whether we have file-name information available: */
251 const QString strFileName = QFileInfo(path).fileName();
252
253 /* Preparing initial directory. */
254 QString initDir = path.isNull() ? mHomeDir :
255 QIFileDialog::getFirstExistingDir (path);
256 if (initDir.isNull())
257 initDir = mHomeDir;
258
259 /* Append file-name information if any: */
260 if (!strFileName.isEmpty())
261 initDir = QDir(initDir).absoluteFilePath(strFileName);
262
263 switch (mMode)
264 {
265 case UIEmptyFilePathSelector::Mode_File_Open:
266 path = QIFileDialog::getOpenFileName (initDir, mFileFilters, window(), mFileDialogTitle); break;
267 case UIEmptyFilePathSelector::Mode_File_Save:
268 {
269 path = QIFileDialog::getSaveFileName (initDir, mFileFilters, window(), mFileDialogTitle);
270 if (!path.isEmpty() && QFileInfo (path).suffix().isEmpty())
271 path = QString ("%1.%2").arg (path).arg (mDefaultSaveExt);
272 break;
273 }
274 case UIEmptyFilePathSelector::Mode_Folder:
275 path = QIFileDialog::getExistingDirectory (initDir, window(), mFileDialogTitle); break;
276 }
277 if (path.isEmpty())
278 return;
279
280 path.remove(QRegularExpression("[\\\\/]$"));
281 setPath (path);
282}
283
284void UIEmptyFilePathSelector::textChanged (const QString& aPath)
285{
286 const QString oldPath = mPath;
287 mPath = aPath;
288 if (oldPath != mPath)
289 {
290 mIsModified = true;
291 emit pathChanged (mPath);
292 }
293}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle
ContactPrivacy/Do Not Sell My InfoTerms of Use