VirtualBox

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

Last change on this file was 104358, checked in by vboxsync, 6 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
RevLine 
[26715]1/* $Id: UIEmptyFilePathSelector.cpp 104358 2024-04-18 05:33:40Z vboxsync $ */
[25178]2/** @file
[71901]3 * VBox Qt GUI - UIEmptyFilePathSelector class implementation.
[25178]4 */
5
6/*
[98103]7 * Copyright (C) 2008-2023 Oracle and/or its affiliates.
[25178]8 *
[96407]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
[25178]26 */
27
[103711]28/* Qt includes: */
[76606]29#include <QAction>
30#include <QApplication>
31#include <QClipboard>
32#include <QDir>
33#include <QFocusEvent>
34#include <QHBoxLayout>
35#include <QLineEdit>
36#include <QTimer>
[25178]37
[103711]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"
[104358]45#include "UITranslationEventListener.h"
[52730]46
[60770]47UIEmptyFilePathSelector::UIEmptyFilePathSelector (QWidget *aParent /* = NULL */)
[104358]48 : QWidget(aParent)
[25178]49 , mPathWgt (NULL)
50 , mLabel (NULL)
[60770]51 , mMode (UIEmptyFilePathSelector::Mode_File_Open)
[25178]52 , mLineEdit (NULL)
[45085]53 , m_fButtonToolTipSet(false)
[25178]54 , mHomeDir (QDir::current().absolutePath())
55 , mIsModified (false)
56{
[73019]57 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
58
[25178]59 mMainLayout = new QHBoxLayout (this);
[73231]60 mMainLayout->setContentsMargins(0, 0, 0, 0);
61#ifdef VBOX_WS_MAC
62 mMainLayout->setSpacing(5);
63#endif
[25178]64
[73231]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;}");
[73232]68#else
69 mSelectButton->setAutoRaise(true);
[73231]70#endif
[47184]71 mSelectButton->setIcon(UIIconPool::iconSet(":/select_file_16px.png", ":/select_file_disabled_16px.png"));
[80955]72 connect(mSelectButton, &QToolButton::clicked, this, &UIEmptyFilePathSelector::choose);
[45085]73 mMainLayout->addWidget(mSelectButton);
[25178]74
75 setEditable (false);
76
[104358]77 sltRetranslateUI();
78 connect(&translationEventListener(), &UITranslationEventListener::sigRetranslateUI,
79 this, &UIEmptyFilePathSelector::sltRetranslateUI);
[25178]80}
81
[60770]82void UIEmptyFilePathSelector::setMode (UIEmptyFilePathSelector::Mode aMode)
[25178]83{
84 mMode = aMode;
85}
86
[60770]87UIEmptyFilePathSelector::Mode UIEmptyFilePathSelector::mode() const
[25178]88{
89 return mMode;
90}
91
[60770]92void UIEmptyFilePathSelector::setButtonPosition (ButtonPosition aPos)
[25178]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
[60770]106UIEmptyFilePathSelector::ButtonPosition UIEmptyFilePathSelector::buttonPosition() const
[25178]107{
108 return mMainLayout->direction() == QBoxLayout::LeftToRight ? LeftPosition : RightPosition;
109}
110
[60770]111void UIEmptyFilePathSelector::setEditable (bool aOn)
[25178]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);
[78215]123 setFocusProxy(mLineEdit);
[25178]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
[60770]138bool UIEmptyFilePathSelector::isEditable() const
[25178]139{
140 return mLabel ? false : true;
141}
142
[60770]143void UIEmptyFilePathSelector::setChooserVisible (bool aOn)
[25178]144{
145 mSelectButton->setVisible (aOn);
146}
147
[60770]148bool UIEmptyFilePathSelector::isChooserVisible() const
[25178]149{
150 return mSelectButton->isVisible();
151}
152
[60770]153void UIEmptyFilePathSelector::setPath (const QString& aPath)
[25178]154{
[26509]155 QString tmpPath = QDir::toNativeSeparators (aPath);
[25178]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
[60770]163QString UIEmptyFilePathSelector::path() const
[25178]164{
165 return mPath;
166}
167
[60770]168void UIEmptyFilePathSelector::setDefaultSaveExt (const QString &aExt)
[25178]169{
170 mDefaultSaveExt = aExt;
171}
172
[60770]173QString UIEmptyFilePathSelector::defaultSaveExt() const
[25178]174{
175 return mDefaultSaveExt;
176}
177
[60770]178void UIEmptyFilePathSelector::setChooseButtonToolTip(const QString &strToolTip)
[41021]179{
[45085]180 m_fButtonToolTipSet = !strToolTip.isEmpty();
181 mSelectButton->setToolTip(strToolTip);
[41021]182}
183
[60770]184QString UIEmptyFilePathSelector::chooseButtonToolTip() const
[41021]185{
[45085]186 return mSelectButton->toolTip();
[41021]187}
188
[60770]189void UIEmptyFilePathSelector::setFileDialogTitle (const QString& aTitle)
[25178]190{
191 mFileDialogTitle = aTitle;
192}
193
[60770]194QString UIEmptyFilePathSelector::fileDialogTitle() const
[25178]195{
196 return mFileDialogTitle;
197}
198
[60770]199void UIEmptyFilePathSelector::setFileFilters (const QString& aFilters)
[25178]200{
201 mFileFilters = aFilters;
202}
203
[60770]204QString UIEmptyFilePathSelector::fileFilters() const
[25178]205{
206 return mFileFilters;
207}
208
[60770]209void UIEmptyFilePathSelector::setHomeDir (const QString& aDir)
[25178]210{
211 mHomeDir = aDir;
212}
213
[60770]214QString UIEmptyFilePathSelector::homeDir() const
[25178]215{
216 return mHomeDir;
217}
218
[104358]219void UIEmptyFilePathSelector::sltRetranslateUI()
[25178]220{
[45085]221 if (!m_fButtonToolTipSet)
222 mSelectButton->setToolTip(tr("Choose..."));
[25178]223}
224
[60770]225void UIEmptyFilePathSelector::choose()
[25178]226{
227 QString path = mPath;
228
[81737]229 /* Check whether we have file-name information available: */
230 const QString strFileName = QFileInfo(path).fileName();
231
[25178]232 /* Preparing initial directory. */
233 QString initDir = path.isNull() ? mHomeDir :
234 QIFileDialog::getFirstExistingDir (path);
235 if (initDir.isNull())
236 initDir = mHomeDir;
237
[81737]238 /* Append file-name information if any: */
239 if (!strFileName.isEmpty())
240 initDir = QDir(initDir).absoluteFilePath(strFileName);
241
[25178]242 switch (mMode)
243 {
[60770]244 case UIEmptyFilePathSelector::Mode_File_Open:
[97237]245 path = QIFileDialog::getOpenFileName (initDir, mFileFilters, window(), mFileDialogTitle); break;
[60770]246 case UIEmptyFilePathSelector::Mode_File_Save:
[25178]247 {
[97237]248 path = QIFileDialog::getSaveFileName (initDir, mFileFilters, window(), mFileDialogTitle);
[25178]249 if (!path.isEmpty() && QFileInfo (path).suffix().isEmpty())
250 path = QString ("%1.%2").arg (path).arg (mDefaultSaveExt);
251 break;
252 }
[60770]253 case UIEmptyFilePathSelector::Mode_Folder:
[97237]254 path = QIFileDialog::getExistingDirectory (initDir, window(), mFileDialogTitle); break;
[25178]255 }
256 if (path.isEmpty())
257 return;
258
[93996]259 path.remove(QRegularExpression("[\\\\/]$"));
[25178]260 setPath (path);
261}
262
[60770]263void UIEmptyFilePathSelector::textChanged (const QString& aPath)
[25178]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