VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/extensions/QIFileDialog.cpp@ 82781

Last change on this file since 82781 was 76606, checked in by vboxsync, 5 years ago

FE/Qt: Cleaning out old precompiled header experiment.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.6 KB
Line 
1/* $Id: QIFileDialog.cpp 76606 2019-01-02 05:40:39Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - Qt extensions: QIFileDialog class implementation.
4 */
5
6/*
7 * Copyright (C) 2009-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/* Qt includes: */
19#ifdef VBOX_WS_MAC
20# include <QEventLoop>
21#endif
22
23/* GUI includes: */
24#include "QIFileDialog.h"
25#include "UIModalWindowManager.h"
26
27
28QIFileDialog::QIFileDialog(QWidget *pParent, Qt::WindowFlags enmFlags)
29 : QFileDialog(pParent, enmFlags)
30{
31}
32
33/* static */
34QString QIFileDialog::getExistingDirectory(const QString &strDir,
35 QWidget *pParent,
36 const QString &strCaption,
37 bool fDirOnly,
38 bool fResolveSymLinks)
39{
40#ifdef VBOX_WS_MAC
41
42 // WORKAROUND:
43 // After 4.5 exec ignores the Qt::Sheet flag.
44 // See "New Ways of Using Dialogs" in http://doc.trolltech.com/qq/QtQuarterly30.pdf why.
45 // We want the old behavior for file-save dialog. Unfortunately there is a bug in Qt 4.5.x
46 // which result in showing the native & the Qt dialog at the same time.
47 QWidget *pRealParent = windowManager().realParentWindow(pParent);
48 QFileDialog dlg(pRealParent);
49 windowManager().registerNewParent(&dlg, pRealParent);
50 dlg.setWindowTitle(strCaption);
51 dlg.setDirectory(strDir);
52 dlg.setResolveSymlinks(fResolveSymLinks);
53 dlg.setFileMode(fDirOnly ? QFileDialog::DirectoryOnly : QFileDialog::Directory);
54
55 QEventLoop eventLoop;
56 QObject::connect(&dlg, &QFileDialog::finished,
57 &eventLoop, &QEventLoop::quit);
58 dlg.open();
59 eventLoop.exec();
60
61 return dlg.result() == QDialog::Accepted ? dlg.selectedFiles().value(0, QString()) : QString();
62
63#else /* !VBOX_WS_MAC */
64
65 QFileDialog::Options o;
66 if (fDirOnly)
67 o |= QFileDialog::ShowDirsOnly;
68 if (!fResolveSymLinks)
69 o |= QFileDialog::DontResolveSymlinks;
70 return QFileDialog::getExistingDirectory(pParent, strCaption, strDir, o);
71
72#endif /* !VBOX_WS_MAC */
73}
74
75/* static */
76QString QIFileDialog::getSaveFileName(const QString &strStartWith,
77 const QString &strFilters,
78 QWidget *pParent,
79 const QString &strCaption,
80 QString *pStrSelectedFilter /* = 0 */,
81 bool fResolveSymLinks /* = true */,
82 bool fConfirmOverwrite /* = false */)
83{
84#ifdef VBOX_WS_MAC
85
86 // WORKAROUND:
87 // After 4.5 exec ignores the Qt::Sheet flag.
88 // See "New Ways of Using Dialogs" in http://doc.trolltech.com/qq/QtQuarterly30.pdf why.
89 // We want the old behavior for file-save dialog. Unfortunately there is a bug in Qt 4.5.x
90 // which result in showing the native & the Qt dialog at the same time.
91 QWidget *pRealParent = windowManager().realParentWindow(pParent);
92 QFileDialog dlg(pRealParent);
93 windowManager().registerNewParent(&dlg, pRealParent);
94 dlg.setWindowTitle(strCaption);
95
96 /* Some predictive algorithm which seems missed in native code. */
97 QDir dir(strStartWith);
98 while (!dir.isRoot() && !dir.exists())
99 dir = QDir(QFileInfo(dir.absolutePath()).absolutePath());
100 const QString strDirectory = dir.absolutePath();
101 if (!strDirectory.isNull())
102 dlg.setDirectory(strDirectory);
103 if (strDirectory != strStartWith)
104 dlg.selectFile(QFileInfo(strStartWith).absoluteFilePath());
105
106 dlg.setNameFilter(strFilters);
107 dlg.setFileMode(QFileDialog::AnyFile);
108 dlg.setAcceptMode(QFileDialog::AcceptSave);
109 if (pStrSelectedFilter)
110 dlg.selectNameFilter(*pStrSelectedFilter);
111 dlg.setResolveSymlinks(fResolveSymLinks);
112 dlg.setConfirmOverwrite(fConfirmOverwrite);
113
114 QEventLoop eventLoop;
115 QObject::connect(&dlg, &QFileDialog::finished,
116 &eventLoop, &QEventLoop::quit);
117 dlg.open();
118 eventLoop.exec();
119
120 return dlg.result() == QDialog::Accepted ? dlg.selectedFiles().value(0, QString()) : QString();
121
122#else /* !VBOX_WS_MAC */
123
124 QFileDialog::Options o;
125 if (!fResolveSymLinks)
126 o |= QFileDialog::DontResolveSymlinks;
127 if (!fConfirmOverwrite)
128 o |= QFileDialog::DontConfirmOverwrite;
129 return QFileDialog::getSaveFileName(pParent, strCaption, strStartWith,
130 strFilters, pStrSelectedFilter, o);
131
132#endif /* !VBOX_WS_MAC */
133}
134
135/* static */
136QString QIFileDialog::getOpenFileName(const QString &strStartWith,
137 const QString &strFilters,
138 QWidget *pParent,
139 const QString &strCaption,
140 QString *pStrSelectedFilter /* = 0 */,
141 bool fResolveSymLinks /* = true */)
142{
143 return getOpenFileNames(strStartWith,
144 strFilters,
145 pParent,
146 strCaption,
147 pStrSelectedFilter,
148 fResolveSymLinks,
149 true /* fSingleFile */).value(0, "");
150}
151
152/* static */
153QStringList QIFileDialog::getOpenFileNames(const QString &strStartWith,
154 const QString &strFilters,
155 QWidget *pParent,
156 const QString &strCaption,
157 QString *pStrSelectedFilter /* = 0 */,
158 bool fResolveSymLinks /* = true */,
159 bool fSingleFile /* = false */)
160{
161#ifdef VBOX_WS_MAC
162
163 // WORKAROUND:
164 // After 4.5 exec ignores the Qt::Sheet flag.
165 // See "New Ways of Using Dialogs" in http://doc.trolltech.com/qq/QtQuarterly30.pdf why.
166 // We want the old behavior for file-save dialog. Unfortunately there is a bug in Qt 4.5.x
167 // which result in showing the native & the Qt dialog at the same time.
168 QWidget *pRealParent = windowManager().realParentWindow(pParent);
169 QFileDialog dlg(pRealParent);
170 windowManager().registerNewParent(&dlg, pRealParent);
171 dlg.setWindowTitle(strCaption);
172
173 /* Some predictive algorithm which seems missed in native code. */
174 QDir dir(strStartWith);
175 while (!dir.isRoot() && !dir.exists())
176 dir = QDir(QFileInfo(dir.absolutePath()).absolutePath());
177 const QString strDirectory = dir.absolutePath();
178 if (!strDirectory.isNull())
179 dlg.setDirectory(strDirectory);
180 if (strDirectory != strStartWith)
181 dlg.selectFile(QFileInfo(strStartWith).absoluteFilePath());
182
183 dlg.setNameFilter(strFilters);
184 if (fSingleFile)
185 dlg.setFileMode(QFileDialog::ExistingFile);
186 else
187 dlg.setFileMode(QFileDialog::ExistingFiles);
188 if (pStrSelectedFilter)
189 dlg.selectNameFilter(*pStrSelectedFilter);
190 dlg.setResolveSymlinks(fResolveSymLinks);
191
192 QEventLoop eventLoop;
193 QObject::connect(&dlg, &QFileDialog::finished,
194 &eventLoop, &QEventLoop::quit);
195 dlg.open();
196 eventLoop.exec();
197
198 return dlg.result() == QDialog::Accepted ? dlg.selectedFiles() : QStringList() << QString();
199
200#else
201
202 QFileDialog::Options o;
203 if (!fResolveSymLinks)
204 o |= QFileDialog::DontResolveSymlinks;
205
206 if (fSingleFile)
207 return QStringList() << QFileDialog::getOpenFileName(pParent, strCaption, strStartWith,
208 strFilters, pStrSelectedFilter, o);
209 else
210 return QFileDialog::getOpenFileNames(pParent, strCaption, strStartWith,
211 strFilters, pStrSelectedFilter, o);
212
213#endif
214}
215
216/* static */
217QString QIFileDialog::getFirstExistingDir(const QString &strStartDir)
218{
219 QString strResult = QString();
220 QDir dir(strStartDir);
221 while (!dir.exists() && !dir.isRoot())
222 {
223 QFileInfo dirInfo(dir.absolutePath());
224 if (dir == QDir(dirInfo.absolutePath()))
225 break;
226 dir = dirInfo.absolutePath();
227 }
228 if (dir.exists() && !dir.isRoot())
229 strResult = dir.absolutePath();
230 return strResult;
231}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use