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