1 | /* $Id: UIEmptyFilePathSelector.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIEmptyFilePathSelector class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifdef VBOX_WITH_PRECOMPILED_HEADERS
|
---|
19 | # include <precomp.h>
|
---|
20 | #else /* !VBOX_WITH_PRECOMPILED_HEADERS */
|
---|
21 |
|
---|
22 | /* Local includes */
|
---|
23 | # include "QIFileDialog.h"
|
---|
24 | # include "QIToolButton.h"
|
---|
25 | # include "QILabel.h"
|
---|
26 | # include "QILineEdit.h"
|
---|
27 | # include "UIIconPool.h"
|
---|
28 | # include "UIEmptyFilePathSelector.h"
|
---|
29 | # include "VBoxGlobal.h"
|
---|
30 |
|
---|
31 | /* Global includes */
|
---|
32 | # include <iprt/assert.h>
|
---|
33 | # include <QAction>
|
---|
34 | # include <QApplication>
|
---|
35 | # include <QClipboard>
|
---|
36 | # include <QDir>
|
---|
37 | # include <QFocusEvent>
|
---|
38 | # include <QHBoxLayout>
|
---|
39 | # include <QLineEdit>
|
---|
40 | # include <QTimer>
|
---|
41 |
|
---|
42 | #endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
|
---|
43 |
|
---|
44 |
|
---|
45 | UIEmptyFilePathSelector::UIEmptyFilePathSelector (QWidget *aParent /* = NULL */)
|
---|
46 | : QIWithRetranslateUI<QWidget> (aParent)
|
---|
47 | , mPathWgt (NULL)
|
---|
48 | , mLabel (NULL)
|
---|
49 | , mMode (UIEmptyFilePathSelector::Mode_File_Open)
|
---|
50 | , mLineEdit (NULL)
|
---|
51 | , m_fButtonToolTipSet(false)
|
---|
52 | , mHomeDir (QDir::current().absolutePath())
|
---|
53 | , mIsModified (false)
|
---|
54 | {
|
---|
55 | setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
|
---|
56 |
|
---|
57 | mMainLayout = new QHBoxLayout (this);
|
---|
58 | mMainLayout->setContentsMargins(0, 0, 0, 0);
|
---|
59 | #ifdef VBOX_WS_MAC
|
---|
60 | mMainLayout->setSpacing(5);
|
---|
61 | #endif
|
---|
62 |
|
---|
63 | mSelectButton = new QToolButton(this);
|
---|
64 | #ifdef VBOX_WS_MAC
|
---|
65 | mSelectButton->setStyleSheet("QToolButton { border: 0px none black; margin: 0px 0px 0px 0px; } QToolButton::menu-indicator {image: none;}");
|
---|
66 | #else
|
---|
67 | mSelectButton->setAutoRaise(true);
|
---|
68 | #endif
|
---|
69 | mSelectButton->setIcon(UIIconPool::iconSet(":/select_file_16px.png", ":/select_file_disabled_16px.png"));
|
---|
70 | connect(mSelectButton, SIGNAL(clicked()), this, SLOT(choose()));
|
---|
71 | mMainLayout->addWidget(mSelectButton);
|
---|
72 |
|
---|
73 | setEditable (false);
|
---|
74 |
|
---|
75 | retranslateUi();
|
---|
76 | }
|
---|
77 |
|
---|
78 | void UIEmptyFilePathSelector::setMode (UIEmptyFilePathSelector::Mode aMode)
|
---|
79 | {
|
---|
80 | mMode = aMode;
|
---|
81 | }
|
---|
82 |
|
---|
83 | UIEmptyFilePathSelector::Mode UIEmptyFilePathSelector::mode() const
|
---|
84 | {
|
---|
85 | return mMode;
|
---|
86 | }
|
---|
87 |
|
---|
88 | void UIEmptyFilePathSelector::setButtonPosition (ButtonPosition aPos)
|
---|
89 | {
|
---|
90 | if (aPos == LeftPosition)
|
---|
91 | {
|
---|
92 | mMainLayout->setDirection (QBoxLayout::LeftToRight);
|
---|
93 | setTabOrder (mSelectButton, mPathWgt);
|
---|
94 | }
|
---|
95 | else
|
---|
96 | {
|
---|
97 | mMainLayout->setDirection (QBoxLayout::RightToLeft);
|
---|
98 | setTabOrder (mPathWgt, mSelectButton);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | UIEmptyFilePathSelector::ButtonPosition UIEmptyFilePathSelector::buttonPosition() const
|
---|
103 | {
|
---|
104 | return mMainLayout->direction() == QBoxLayout::LeftToRight ? LeftPosition : RightPosition;
|
---|
105 | }
|
---|
106 |
|
---|
107 | void UIEmptyFilePathSelector::setEditable (bool aOn)
|
---|
108 | {
|
---|
109 | if (mPathWgt)
|
---|
110 | {
|
---|
111 | delete mPathWgt;
|
---|
112 | mLabel = NULL;
|
---|
113 | mLineEdit = NULL;
|
---|
114 | }
|
---|
115 |
|
---|
116 | if (aOn)
|
---|
117 | {
|
---|
118 | mPathWgt = mLineEdit = new QILineEdit (this);
|
---|
119 | connect (mLineEdit, SIGNAL (textChanged (const QString&)),
|
---|
120 | this, SLOT (textChanged (const QString&)));
|
---|
121 | }
|
---|
122 | else
|
---|
123 | {
|
---|
124 | mPathWgt = mLabel = new QILabel (this);
|
---|
125 | mLabel->setWordWrap (true);
|
---|
126 | }
|
---|
127 | mMainLayout->addWidget (mPathWgt, 2);
|
---|
128 | setButtonPosition (buttonPosition());
|
---|
129 |
|
---|
130 | setPath (mPath);
|
---|
131 | }
|
---|
132 |
|
---|
133 | bool UIEmptyFilePathSelector::isEditable() const
|
---|
134 | {
|
---|
135 | return mLabel ? false : true;
|
---|
136 | }
|
---|
137 |
|
---|
138 | void UIEmptyFilePathSelector::setChooserVisible (bool aOn)
|
---|
139 | {
|
---|
140 | mSelectButton->setVisible (aOn);
|
---|
141 | }
|
---|
142 |
|
---|
143 | bool UIEmptyFilePathSelector::isChooserVisible() const
|
---|
144 | {
|
---|
145 | return mSelectButton->isVisible();
|
---|
146 | }
|
---|
147 |
|
---|
148 | void UIEmptyFilePathSelector::setPath (const QString& aPath)
|
---|
149 | {
|
---|
150 | QString tmpPath = QDir::toNativeSeparators (aPath);
|
---|
151 | if (mLabel)
|
---|
152 | mLabel->setText (QString ("<compact elipsis=\"start\">%1</compact>").arg (tmpPath));
|
---|
153 | else if (mLineEdit)
|
---|
154 | mLineEdit->setText (tmpPath);
|
---|
155 | textChanged(tmpPath);
|
---|
156 | }
|
---|
157 |
|
---|
158 | QString UIEmptyFilePathSelector::path() const
|
---|
159 | {
|
---|
160 | return mPath;
|
---|
161 | }
|
---|
162 |
|
---|
163 | void UIEmptyFilePathSelector::setDefaultSaveExt (const QString &aExt)
|
---|
164 | {
|
---|
165 | mDefaultSaveExt = aExt;
|
---|
166 | }
|
---|
167 |
|
---|
168 | QString UIEmptyFilePathSelector::defaultSaveExt() const
|
---|
169 | {
|
---|
170 | return mDefaultSaveExt;
|
---|
171 | }
|
---|
172 |
|
---|
173 | void UIEmptyFilePathSelector::setChooseButtonToolTip(const QString &strToolTip)
|
---|
174 | {
|
---|
175 | m_fButtonToolTipSet = !strToolTip.isEmpty();
|
---|
176 | mSelectButton->setToolTip(strToolTip);
|
---|
177 | }
|
---|
178 |
|
---|
179 | QString UIEmptyFilePathSelector::chooseButtonToolTip() const
|
---|
180 | {
|
---|
181 | return mSelectButton->toolTip();
|
---|
182 | }
|
---|
183 |
|
---|
184 | void UIEmptyFilePathSelector::setFileDialogTitle (const QString& aTitle)
|
---|
185 | {
|
---|
186 | mFileDialogTitle = aTitle;
|
---|
187 | }
|
---|
188 |
|
---|
189 | QString UIEmptyFilePathSelector::fileDialogTitle() const
|
---|
190 | {
|
---|
191 | return mFileDialogTitle;
|
---|
192 | }
|
---|
193 |
|
---|
194 | void UIEmptyFilePathSelector::setFileFilters (const QString& aFilters)
|
---|
195 | {
|
---|
196 | mFileFilters = aFilters;
|
---|
197 | }
|
---|
198 |
|
---|
199 | QString UIEmptyFilePathSelector::fileFilters() const
|
---|
200 | {
|
---|
201 | return mFileFilters;
|
---|
202 | }
|
---|
203 |
|
---|
204 | void UIEmptyFilePathSelector::setHomeDir (const QString& aDir)
|
---|
205 | {
|
---|
206 | mHomeDir = aDir;
|
---|
207 | }
|
---|
208 |
|
---|
209 | QString UIEmptyFilePathSelector::homeDir() const
|
---|
210 | {
|
---|
211 | return mHomeDir;
|
---|
212 | }
|
---|
213 |
|
---|
214 | void UIEmptyFilePathSelector::retranslateUi()
|
---|
215 | {
|
---|
216 | if (!m_fButtonToolTipSet)
|
---|
217 | mSelectButton->setToolTip(tr("Choose..."));
|
---|
218 | }
|
---|
219 |
|
---|
220 | void UIEmptyFilePathSelector::choose()
|
---|
221 | {
|
---|
222 | QString path = mPath;
|
---|
223 |
|
---|
224 | /* Preparing initial directory. */
|
---|
225 | QString initDir = path.isNull() ? mHomeDir :
|
---|
226 | QIFileDialog::getFirstExistingDir (path);
|
---|
227 | if (initDir.isNull())
|
---|
228 | initDir = mHomeDir;
|
---|
229 |
|
---|
230 | switch (mMode)
|
---|
231 | {
|
---|
232 | case UIEmptyFilePathSelector::Mode_File_Open:
|
---|
233 | path = QIFileDialog::getOpenFileName (initDir, mFileFilters, parentWidget(), mFileDialogTitle); break;
|
---|
234 | case UIEmptyFilePathSelector::Mode_File_Save:
|
---|
235 | {
|
---|
236 | path = QIFileDialog::getSaveFileName (initDir, mFileFilters, parentWidget(), mFileDialogTitle);
|
---|
237 | if (!path.isEmpty() && QFileInfo (path).suffix().isEmpty())
|
---|
238 | path = QString ("%1.%2").arg (path).arg (mDefaultSaveExt);
|
---|
239 | break;
|
---|
240 | }
|
---|
241 | case UIEmptyFilePathSelector::Mode_Folder:
|
---|
242 | path = QIFileDialog::getExistingDirectory (initDir, parentWidget(), mFileDialogTitle); break;
|
---|
243 | }
|
---|
244 | if (path.isEmpty())
|
---|
245 | return;
|
---|
246 |
|
---|
247 | path.remove (QRegExp ("[\\\\/]$"));
|
---|
248 | setPath (path);
|
---|
249 | }
|
---|
250 |
|
---|
251 | void UIEmptyFilePathSelector::textChanged (const QString& aPath)
|
---|
252 | {
|
---|
253 | const QString oldPath = mPath;
|
---|
254 | mPath = aPath;
|
---|
255 | if (oldPath != mPath)
|
---|
256 | {
|
---|
257 | mIsModified = true;
|
---|
258 | emit pathChanged (mPath);
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|