VirtualBox

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

Last change on this file since 100347 was 98103, checked in by vboxsync, 21 months ago

Copyright year updates by scm.

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