VirtualBox

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

Last change on this file was 104358, checked in by vboxsync, 4 weeks ago

FE/Qt. bugref:10622. More refactoring around the retranslation functionality.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.1 KB
Line 
1/* $Id: UIEmptyFilePathSelector.cpp 104358 2024-04-18 05:33:40Z 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_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 sltRetranslateUI();
78 connect(&translationEventListener(), &UITranslationEventListener::sigRetranslateUI,
79 this, &UIEmptyFilePathSelector::sltRetranslateUI);
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::sltRetranslateUI()
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.

© 2023 Oracle
ContactPrivacy policyTerms of Use