Qt 4.8
Macros | Typedefs | Functions | Variables
qfiledialog.cpp File Reference
#include <qvariant.h>
#include <private/qwidgetitemdata_p.h>
#include "qfiledialog.h"
#include "qfiledialog_p.h"
#include <qfontmetrics.h>
#include <qaction.h>
#include <qheaderview.h>
#include <qshortcut.h>
#include <qgridlayout.h>
#include <qmenu.h>
#include <qmessagebox.h>
#include <qinputdialog.h>
#include <stdlib.h>
#include <qsettings.h>
#include <qdebug.h>
#include <qapplication.h>
#include <qstylepainter.h>
#include <itemviews/qfileiconprovider_p.h>
#include "ui_qfiledialog_embedded.h"
#include <pwd.h>
#include <qwindowsstyle.h>
#include <qmacstyle_mac.h>
#include "moc_qfiledialog.cpp"

Go to the source code of this file.

Macros

#define Q_EMBEDDED_SMALLSCREEN
 

Typedefs

typedef QString(* _qt_filedialog_existing_directory_hook) (QWidget *parent, const QString &caption, const QString &dir, QFileDialog::Options options)
 
typedef QString(* _qt_filedialog_open_filename_hook) (QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)
 
typedef QStringList(* _qt_filedialog_open_filenames_hook) (QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)
 
typedef QString(* _qt_filedialog_save_filename_hook) (QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)
 

Functions

static QString _qt_get_directory (const QString &path)
 
QStringList qt_clean_filter_list (const QString &filter)
 
QStringList qt_make_filter_list (const QString &filter)
 
QStringList qt_strip_filters (const QStringList &filters)
 
Q_AUTOTEST_EXPORT QString qt_tildeExpansion (const QString &path, bool *expanded=0)
 
QString qt_win_get_existing_directory (const QFileDialogArgs &args)
 
QString qt_win_get_open_file_name (const QFileDialogArgs &args, QString *initialDirectory, QString *selectedFilter)
 
QStringList qt_win_get_open_file_names (const QFileDialogArgs &args, QString *initialDirectory, QString *selectedFilter)
 
QString qt_win_get_save_file_name (const QFileDialogArgs &args, QString *initialDirectory, QString *selectedFilter)
 

Variables

static const qint32 QFileDialogMagic = 0xbe
 
const char * qt_file_dialog_filter_reg_exp
 
Q_GUI_EXPORT _qt_filedialog_existing_directory_hook qt_filedialog_existing_directory_hook = 0
 
Q_GUI_EXPORT _qt_filedialog_open_filename_hook qt_filedialog_open_filename_hook = 0
 
Q_GUI_EXPORT _qt_filedialog_open_filenames_hook qt_filedialog_open_filenames_hook = 0
 
Q_GUI_EXPORT _qt_filedialog_save_filename_hook qt_filedialog_save_filename_hook = 0
 
bool qt_priv_ptr_valid
 
bool Q_GUI_EXPORT qt_use_native_dialogs = true
 

Macro Definition Documentation

◆ Q_EMBEDDED_SMALLSCREEN

#define Q_EMBEDDED_SMALLSCREEN

Definition at line 65 of file qfiledialog.cpp.

Typedef Documentation

◆ _qt_filedialog_existing_directory_hook

typedef QString(* _qt_filedialog_existing_directory_hook) (QWidget *parent, const QString &caption, const QString &dir, QFileDialog::Options options)

Definition at line 84 of file qfiledialog.cpp.

◆ _qt_filedialog_open_filename_hook

typedef QString(* _qt_filedialog_open_filename_hook) (QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)

Definition at line 87 of file qfiledialog.cpp.

◆ _qt_filedialog_open_filenames_hook

typedef QStringList(* _qt_filedialog_open_filenames_hook) (QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)

Definition at line 90 of file qfiledialog.cpp.

◆ _qt_filedialog_save_filename_hook

typedef QString(* _qt_filedialog_save_filename_hook) (QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)

Definition at line 93 of file qfiledialog.cpp.

Function Documentation

◆ _qt_get_directory()

static QString _qt_get_directory ( const QString path)
inlinestatic

Definition at line 2190 of file qfiledialog.cpp.

Referenced by QFileDialogPrivate::workingDirectory().

2191 {
2193  if (info.exists() && info.isDir())
2194  return QDir::cleanPath(info.absoluteFilePath());
2195  info.setFile(info.absolutePath());
2196  if (info.exists() && info.isDir())
2197  return info.absoluteFilePath();
2198  return QString();
2199 }
static mach_timebase_info_data_t info
QString absoluteFilePath() const
Returns an absolute path including the file name.
Definition: qfileinfo.cpp:534
bool exists() const
Returns true if the file exists; otherwise returns false.
Definition: qfileinfo.cpp:675
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool isDir() const
Returns true if this object points to a directory or to a symbolic link to a directory; otherwise ret...
Definition: qfileinfo.cpp:990
static QDir current()
Returns the application&#39;s current directory.
Definition: qdir.h:209
void setFile(const QString &file)
Sets the file that the QFileInfo provides information about to file.
Definition: qfileinfo.cpp:468
static QString cleanPath(const QString &path)
Removes all multiple directory separators "/" and resolves any "."s or ".."s found in the path...
Definition: qdir.cpp:2082
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
QString absolutePath() const
Returns a file&#39;s path absolute path.
Definition: qfileinfo.cpp:577

◆ qt_clean_filter_list()

QStringList qt_clean_filter_list ( const QString filter)

Definition at line 3189 of file qfiledialog.cpp.

Referenced by QFileDialogPrivate::_q_useNameFilter(), and QNSOpenSavePanelDelegate::findStrippedFilterWithVisualFilterName:.

3190 {
3192  QString f = filter;
3193  int i = regexp.indexIn(f);
3194  if (i >= 0)
3195  f = regexp.cap(2);
3196  return f.split(QLatin1Char(' '), QString::SkipEmptyParts);
3197 }
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
const char * qt_file_dialog_filter_reg_exp
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
QFuture< void > filter(Sequence &sequence, FilterFunction filterFunction)
QStringList split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const Q_REQUIRED_RESULT
Splits the string into substrings wherever sep occurs, and returns the list of those strings...
Definition: qstring.cpp:6526
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ qt_make_filter_list()

QStringList qt_make_filter_list ( const QString filter)

Definition at line 1075 of file qfiledialog.cpp.

Referenced by QGtkStylePrivate::extract_filter(), qt_mac_make_filters_list(), qt_win_make_filters_list(), qt_win_resolve_libs(), QFileDialog::selectNameFilter(), QFileDialog::setNameFilter(), and QGtkStylePrivate::setupGtkFileChooser().

1076 {
1077  QString f(filter);
1078 
1079  if (f.isEmpty())
1080  return QStringList();
1081 
1082  QString sep(QLatin1String(";;"));
1083  int i = f.indexOf(sep, 0);
1084  if (i == -1) {
1085  if (f.indexOf(QLatin1Char('\n'), 0) != -1) {
1086  sep = QLatin1Char('\n');
1087  i = f.indexOf(sep, 0);
1088  }
1089  }
1090 
1091  return f.split(sep);
1092 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ qt_strip_filters()

QStringList qt_strip_filters ( const QStringList filters)

Definition at line 1155 of file qfiledialog.cpp.

Referenced by QFileDialog::selectNameFilter(), and QFileDialog::setNameFilters().

1156 {
1157  QStringList strippedFilters;
1159  for (int i = 0; i < filters.count(); ++i) {
1160  QString filterName;
1161  int index = r.indexIn(filters[i]);
1162  if (index >= 0)
1163  filterName = r.cap(1);
1164  strippedFilters.append(filterName.simplified());
1165  }
1166  return strippedFilters;
1167 }
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
The QString class provides a Unicode character string.
Definition: qstring.h:83
const char * qt_file_dialog_filter_reg_exp
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QString simplified() const Q_REQUIRED_RESULT
Returns a string that has whitespace removed from the start and the end, and that has each sequence o...
Definition: qstring.cpp:4415
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
quint16 index

◆ qt_tildeExpansion()

Q_AUTOTEST_EXPORT QString qt_tildeExpansion ( const QString path,
bool *  expanded = 0 
)

Definition at line 932 of file qfiledialog.cpp.

Referenced by QFSCompleter::splitPath(), and QFileDialogPrivate::typedFiles().

933 {
934  if (expanded != 0)
935  *expanded = false;
936  if (!path.startsWith(QLatin1Char('~')))
937  return path;
938  QString ret = path;
939 #if !defined(Q_OS_INTEGRITY)
940  QStringList tokens = ret.split(QDir::separator());
941  if (tokens.first() == QLatin1String("~")) {
942  ret.replace(0, 1, QDir::homePath());
943  } else {
944  QString userName = tokens.first();
945  userName.remove(0, 1);
946 #if defined(Q_OS_VXWORKS)
947  const QString homePath = QDir::homePath();
948 #elif defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_OPENBSD)
949  passwd pw;
950  passwd *tmpPw;
951  char buf[200];
952  const int bufSize = sizeof(buf);
953  int err = 0;
954 #if defined(Q_OS_SOLARIS) && defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE - 0 < 199506L)
955  tmpPw = getpwnam_r(userName.toLocal8Bit().constData(), &pw, buf, bufSize);
956 #else
957  err = getpwnam_r(userName.toLocal8Bit().constData(), &pw, buf, bufSize, &tmpPw);
958 #endif
959  if (err || !tmpPw)
960  return ret;
961  const QString homePath = QString::fromLocal8Bit(pw.pw_dir);
962 #else
963  passwd *pw = getpwnam(userName.toLocal8Bit().constData());
964  if (!pw)
965  return ret;
966  const QString homePath = QString::fromLocal8Bit(pw->pw_dir);
967 #endif
968  ret.replace(0, tokens.first().length(), homePath);
969  }
970  if (expanded != 0)
971  *expanded = true;
972 #endif
973  return ret;
974 }
static QString fromLocal8Bit(const char *, int size=-1)
Returns a QString initialized with the first size characters of the 8-bit string str.
Definition: qstring.cpp:4245
QString & replace(int i, int len, QChar after)
Definition: qstring.cpp:2005
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QChar separator()
Returns the native directory separator: "/" under Unix (including Mac OS X) and "\\" under Windows...
Definition: qdir.cpp:1831
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4049
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
QStringList split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const Q_REQUIRED_RESULT
Splits the string into substrings wherever sep occurs, and returns the list of those strings...
Definition: qstring.cpp:6526
QString & remove(int i, int len)
Removes n characters from the string, starting at the given position index, and returns a reference t...
Definition: qstring.cpp:1867
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
static QString homePath()
Returns the absolute path of the user&#39;s home directory.
Definition: qdir.cpp:1942

◆ qt_win_get_existing_directory()

QString qt_win_get_existing_directory ( const QFileDialogArgs args)

Definition at line 741 of file qfiledialog_win.cpp.

Referenced by QFileDialog::getExistingDirectory(), and QFileDialog::labelText().

742 {
743 #ifndef Q_WS_WINCE
745  return qt_win_CID_get_existing_directory(args);
746 #endif
747 
748  QString currentDir = QDir::currentPath();
749  QString result;
750  QWidget *parent = args.parent;
751  if (parent)
752  parent = parent->window();
753  else
754  parent = QApplication::activeWindow();
755  if (parent)
756  parent->createWinId();
757 
758  QDialog modal_widget;
759  modal_widget.setAttribute(Qt::WA_NoChildEventsForParent, true);
760  modal_widget.setParent(parent, Qt::Window);
761  QApplicationPrivate::enterModal(&modal_widget);
762 
764  wchar_t path[MAX_PATH];
765  wchar_t initPath[MAX_PATH];
766  initPath[0] = 0;
767  path[0] = 0;
768  tTitle = args.caption;
769 
770  qt_BROWSEINFO bi;
771 
772  Q_ASSERT(!parent ||parent->testAttribute(Qt::WA_WState_Created));
773  bi.hwndOwner = (parent ? parent->winId() : 0);
774  bi.pidlRoot = NULL;
775  //### This does not seem to be respected? - the dialog always displays "Browse for folder"
776  bi.lpszTitle = (wchar_t*)tTitle.utf16();
777  bi.pszDisplayName = initPath;
780  bi.lParam = LPARAM(&initDir);
781 
783  if (ptrSHBrowseForFolder) {
784  qt_LPITEMIDLIST pItemIDList = ptrSHBrowseForFolder(&bi);
785  if (pItemIDList) {
786  ptrSHGetPathFromIDList(pItemIDList, path);
787  IMalloc *pMalloc;
788  if (ptrSHGetMalloc(&pMalloc) == NOERROR) {
789  pMalloc->Free(pItemIDList);
790  pMalloc->Release();
791  result = QString::fromWCharArray(path);
792  }
793  }
794  }
795  tTitle = QString();
796 
797  QApplicationPrivate::leaveModal(&modal_widget);
798 
800 
801  if (!result.isEmpty())
802  result.replace(QLatin1Char('\\'), QLatin1Char('/'));
803  return result;
804 }
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
void setParent(QWidget *parent)
Sets the parent of the widget to parent, and resets the window flags.
Definition: qwidget.cpp:10479
The QDialog class is the base class of dialog windows.
Definition: qdialog.h:56
QString & replace(int i, int len, QChar after)
Definition: qstring.cpp:2005
static QWidget * activeWindow()
Returns the application top-level window that has the keyboard input focus, or 0 if no application wi...
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
#define BIF_NEWDIALOGSTYLE
void qt_win_eatMouseMove()
static const WinVersion WindowsVersion
the version of the Windows operating system on which the application is run (Windows only) ...
Definition: qglobal.h:1613
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QString currentPath()
Returns the absolute path of the application&#39;s current directory.
Definition: qdir.cpp:1875
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
BFFCALLBACK lpfn
static QString tTitle
bool testAttribute(Qt::WidgetAttribute) const
Returns true if attribute attribute is set on this widget; otherwise returns false.
Definition: qwidget.h:1041
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static void qt_win_resolve_libs()
#define BIF_STATUSTEXT
static int __stdcall winGetExistDirCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
QWidget * parent
static PtrSHGetMalloc ptrSHGetMalloc
qt_LPCITEMIDLIST pidlRoot
static PtrSHBrowseForFolder ptrSHBrowseForFolder
void setAttribute(Qt::WidgetAttribute, bool on=true)
Sets the attribute attribute on this widget if on is true; otherwise clears the attribute.
Definition: qwidget.cpp:11087
QWidget * window() const
Returns the window for this widget, i.e.
Definition: qwidget.cpp:4492
void createWinId()
Definition: qwidget.cpp:2626
WId winId() const
Returns the window system identifier of the widget.
Definition: qwidget.cpp:2557
static PtrSHGetPathFromIDList ptrSHGetPathFromIDList
static QString toNativeSeparators(const QString &pathName)
Returns pathName with the &#39;/&#39; separators converted to separators that are appropriate for the underly...
Definition: qdir.cpp:812
#define BIF_RETURNONLYFSDIRS
static void leaveModal(QWidget *)
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
static void enterModal(QWidget *)
const ushort * utf16() const
Returns the QString as a &#39;\0\&#39;-terminated array of unsigned shorts.
Definition: qstring.cpp:5290

◆ qt_win_get_open_file_name()

QString qt_win_get_open_file_name ( const QFileDialogArgs args,
QString initialDirectory,
QString selectedFilter 
)

Definition at line 231 of file qfiledialog_win.cpp.

Referenced by QFileDialog::getOpenFileName(), and QFileDialog::labelText().

234 {
235  QString result;
236 
237  QString isel = args.selection;
238 
239  if (initialDirectory && initialDirectory->left(5) == QLatin1String("file:"))
240  initialDirectory->remove(0, 5);
241  QFileInfo fi(*initialDirectory);
242 
243  if (initialDirectory && !fi.isDir()) {
244  *initialDirectory = fi.absolutePath();
245  if (isel.isEmpty())
246  isel = fi.fileName();
247  }
248 
249  if (!fi.exists())
250  *initialDirectory = QDir::homePath();
251 
252  DWORD selFilIdx = 0;
253 
254  int idx = 0;
255  if (selectedFilter) {
256  QStringList filterLst = qt_win_make_filters_list(args.filter);
257  idx = filterLst.indexOf(*selectedFilter);
258  }
259 
260  QDialog modal_widget;
261  modal_widget.setAttribute(Qt::WA_NoChildEventsForParent, true);
262  modal_widget.setParent(args.parent, Qt::Window);
263  QApplicationPrivate::enterModal(&modal_widget);
264 
265  bool hideFiltersDetails = args.options & QFileDialog::HideNameFilterDetails;
266  OPENFILENAME* ofn = qt_win_make_OFN(args.parent, args.selection,
267  args.directory, args.caption,
268  qt_win_filter(args.filter, hideFiltersDetails),
270  args.options);
271  if (idx)
272  ofn->nFilterIndex = idx + 1;
273  if (GetOpenFileName(ofn)) {
274  result = QString::fromWCharArray(ofn->lpstrFile);
275  selFilIdx = ofn->nFilterIndex;
276  }
277  qt_win_clean_up_OFN(&ofn);
278 
279  QApplicationPrivate::leaveModal(&modal_widget);
280 
282 
283  if (result.isEmpty())
284  return result;
285 
286  fi = result;
287  *initialDirectory = fi.path();
288  if (selectedFilter)
289  *selectedFilter = qt_win_selected_filter(args.filter, selFilIdx);
290  return fi.absoluteFilePath();
291 }
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
static QStringList qt_win_make_filters_list(const QString &filter)
static OPENFILENAME * qt_win_make_OFN(QWidget *parent, const QString &initialSelection, const QString &initialDirectory, const QString &title, const QString &filters, QFileDialog::FileMode mode, QFileDialog::Options options)
void setParent(QWidget *parent)
Sets the parent of the widget to parent, and resets the window flags.
Definition: qwidget.cpp:10479
The QDialog class is the base class of dialog windows.
Definition: qdialog.h:56
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
void qt_win_eatMouseMove()
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QString qt_win_filter(const QString &filter, bool hideFiltersDetails)
int indexOf(const QRegExp &rx, int from=0) const
Returns the index position of the first exact match of rx in the list, searching forward from index p...
Definition: qstringlist.h:195
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QWidget * parent
QFileDialog::Options options
static void qt_win_clean_up_OFN(OPENFILENAME **ofn)
static QString qt_win_selected_filter(const QString &filter, DWORD idx)
void setAttribute(Qt::WidgetAttribute, bool on=true)
Sets the attribute attribute on this widget if on is true; otherwise clears the attribute.
Definition: qwidget.cpp:11087
QString & remove(int i, int len)
Removes n characters from the string, starting at the given position index, and returns a reference t...
Definition: qstring.cpp:1867
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
static void leaveModal(QWidget *)
static void enterModal(QWidget *)
static QString homePath()
Returns the absolute path of the user&#39;s home directory.
Definition: qdir.cpp:1942

◆ qt_win_get_open_file_names()

QStringList qt_win_get_open_file_names ( const QFileDialogArgs args,
QString initialDirectory,
QString selectedFilter 
)

Definition at line 627 of file qfiledialog_win.cpp.

Referenced by QFileDialog::getOpenFileNames(), and QFileDialog::labelText().

630 {
631  QFileInfo fi;
632  QDir dir;
633 
634  if (initialDirectory && initialDirectory->left(5) == QLatin1String("file:"))
635  initialDirectory->remove(0, 5);
636  fi = QFileInfo(*initialDirectory);
637 
638  if (initialDirectory && !fi.isDir()) {
639  *initialDirectory = fi.absolutePath();
640  }
641 
642  if (!fi.exists())
643  *initialDirectory = QDir::homePath();
644 
645  DWORD selFilIdx = 0;
646 
647  QStringList filterLst = qt_win_make_filters_list(args.filter);
648  int idx = 0;
649  if (selectedFilter) {
650  idx = filterLst.indexOf(*selectedFilter);
651  }
652  // Windows Vista (& above) allows users to search from file dialogs. If user selects
653  // multiple files belonging to different folders from these search results, the
654  // GetOpenFileName() will return only one folder name for all the files. To retrieve
655  // the correct path for all selected files, we have to use Common Item Dialog interfaces.
656 #ifndef Q_WS_WINCE
658  return qt_win_CID_get_open_file_names(args, initialDirectory, filterLst, selectedFilter, idx);
659 #endif
660 
661  QStringList result;
662  QDialog modal_widget;
663  modal_widget.setAttribute(Qt::WA_NoChildEventsForParent, true);
664  modal_widget.setParent(args.parent, Qt::Window);
665  QApplicationPrivate::enterModal(&modal_widget);
666 
667  bool hideFiltersDetails = args.options & QFileDialog::HideNameFilterDetails;
668  OPENFILENAME* ofn = qt_win_make_OFN(args.parent, args.selection,
669  args.directory, args.caption,
670  qt_win_filter(args.filter, hideFiltersDetails),
672  args.options);
673  if (idx)
674  ofn->nFilterIndex = idx + 1;
675  if (GetOpenFileName(ofn)) {
676  QString fileOrDir = QString::fromWCharArray(ofn->lpstrFile);
677  selFilIdx = ofn->nFilterIndex;
678  int offset = fileOrDir.length() + 1;
679  if (ofn->lpstrFile[offset] == 0) {
680  // Only one file selected; has full path
681  fi.setFile(fileOrDir);
682  QString res = fi.absoluteFilePath();
683  if (!res.isEmpty())
684  result.append(res);
685  }
686  else {
687  // Several files selected; first string is path
688  dir.setPath(fileOrDir);
689  QString f;
690  while(!(f = QString::fromWCharArray(ofn->lpstrFile + offset)).isEmpty()) {
691  fi.setFile(dir, f);
692  QString res = fi.absoluteFilePath();
693  if (!res.isEmpty())
694  result.append(res);
695  offset += f.length() + 1;
696  }
697  }
698  }
699  qt_win_clean_up_OFN(&ofn);
700 
701  QApplicationPrivate::leaveModal(&modal_widget);
702 
704 
705  if (!result.isEmpty()) {
706  *initialDirectory = fi.path(); // only save the path if there is a result
707  if (selectedFilter)
708  *selectedFilter = qt_win_selected_filter(args.filter, selFilIdx);
709  }
710  return result;
711 }
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
The QDir class provides access to directory structures and their contents.
Definition: qdir.h:58
static QStringList qt_win_make_filters_list(const QString &filter)
static OPENFILENAME * qt_win_make_OFN(QWidget *parent, const QString &initialSelection, const QString &initialDirectory, const QString &title, const QString &filters, QFileDialog::FileMode mode, QFileDialog::Options options)
void setParent(QWidget *parent)
Sets the parent of the widget to parent, and resets the window flags.
Definition: qwidget.cpp:10479
The QDialog class is the base class of dialog windows.
Definition: qdialog.h:56
void setPath(const QString &path)
Sets the path of the directory to path.
Definition: qdir.cpp:590
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
QString absoluteFilePath() const
Returns an absolute path including the file name.
Definition: qfileinfo.cpp:534
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
void qt_win_eatMouseMove()
bool exists() const
Returns true if the file exists; otherwise returns false.
Definition: qfileinfo.cpp:675
static const WinVersion WindowsVersion
the version of the Windows operating system on which the application is run (Windows only) ...
Definition: qglobal.h:1613
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QString qt_win_filter(const QString &filter, bool hideFiltersDetails)
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
int indexOf(const QRegExp &rx, int from=0) const
Returns the index position of the first exact match of rx in the list, searching forward from index p...
Definition: qstringlist.h:195
bool isDir() const
Returns true if this object points to a directory or to a symbolic link to a directory; otherwise ret...
Definition: qfileinfo.cpp:990
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
void setFile(const QString &file)
Sets the file that the QFileInfo provides information about to file.
Definition: qfileinfo.cpp:468
QWidget * parent
QFileDialog::Options options
static void qt_win_clean_up_OFN(OPENFILENAME **ofn)
static QString qt_win_selected_filter(const QString &filter, DWORD idx)
void setAttribute(Qt::WidgetAttribute, bool on=true)
Sets the attribute attribute on this widget if on is true; otherwise clears the attribute.
Definition: qwidget.cpp:11087
QString path() const
Returns the file&#39;s path.
Definition: qfileinfo.cpp:615
QString & remove(int i, int len)
Removes n characters from the string, starting at the given position index, and returns a reference t...
Definition: qstring.cpp:1867
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
static void leaveModal(QWidget *)
QString absolutePath() const
Returns a file&#39;s path absolute path.
Definition: qfileinfo.cpp:577
static void enterModal(QWidget *)
static QString homePath()
Returns the absolute path of the user&#39;s home directory.
Definition: qdir.cpp:1942

◆ qt_win_get_save_file_name()

QString qt_win_get_save_file_name ( const QFileDialogArgs args,
QString initialDirectory,
QString selectedFilter 
)

Definition at line 293 of file qfiledialog_win.cpp.

Referenced by QFileDialog::getSaveFileName(), and QFileDialog::labelText().

296 {
297  QString result;
298 
299  QString isel = args.selection;
300  if (initialDirectory && initialDirectory->left(5) == QLatin1String("file:"))
301  initialDirectory->remove(0, 5);
302  QFileInfo fi(*initialDirectory);
303 
304  if (initialDirectory && !fi.isDir()) {
305  *initialDirectory = fi.absolutePath();
306  if (isel.isEmpty())
307  isel = fi.fileName();
308  }
309 
310  if (!fi.exists())
311  *initialDirectory = QDir::homePath();
312 
313  DWORD selFilIdx = 0;
314 
315  int idx = 0;
316  if (selectedFilter) {
317  QStringList filterLst = qt_win_make_filters_list(args.filter);
318  idx = filterLst.indexOf(*selectedFilter);
319  }
320 
321  QDialog modal_widget;
322  modal_widget.setAttribute(Qt::WA_NoChildEventsForParent, true);
323  modal_widget.setParent(args.parent, Qt::Window);
324  QApplicationPrivate::enterModal(&modal_widget);
325  bool hideFiltersDetails = args.options & QFileDialog::HideNameFilterDetails;
326  // This block is used below for the lpstrDefExt member.
327  // Note that the current MSDN docs document this member wrong.
328  // It should rather be documented as "the default extension if no extension was given and if the
329  // current filter does not have a extension (e.g (*)). If the current filter have an extension, use
330  // the extension of the current filter"
331  QString defaultSaveExt;
332  if (selectedFilter && !selectedFilter->isEmpty()) {
333  defaultSaveExt = qt_win_extract_filter(*selectedFilter);
334  // make sure we only have the extension
335  int firstDot = defaultSaveExt.indexOf(QLatin1Char('.'));
336  if (firstDot != -1) {
337  defaultSaveExt.remove(0, firstDot + 1);
338  } else {
339  defaultSaveExt.clear();
340  }
341  }
342 
343  OPENFILENAME *ofn = qt_win_make_OFN(args.parent, args.selection,
344  args.directory, args.caption,
345  qt_win_filter(args.filter, hideFiltersDetails),
347  args.options);
348 
349  ofn->lpstrDefExt = (wchar_t*)defaultSaveExt.utf16();
350 
351  if (idx)
352  ofn->nFilterIndex = idx + 1;
353  if (GetSaveFileName(ofn)) {
354  result = QString::fromWCharArray(ofn->lpstrFile);
355  selFilIdx = ofn->nFilterIndex;
356  }
357  qt_win_clean_up_OFN(&ofn);
358 
359 #if defined(Q_WS_WINCE)
360  int semIndex = result.indexOf(QLatin1Char(';'));
361  if (semIndex >= 0)
362  result = result.left(semIndex);
363 #endif
364 
365  QApplicationPrivate::leaveModal(&modal_widget);
366 
368 
369  if (result.isEmpty())
370  return result;
371 
372  fi = result;
373  *initialDirectory = fi.path();
374  if (selectedFilter)
375  *selectedFilter = qt_win_selected_filter(args.filter, selFilIdx);
376  return fi.absoluteFilePath();
377 }
static QString fromWCharArray(const wchar_t *, int size=-1)
Returns a copy of the string, where the encoding of string depends on the size of wchar...
Definition: qstring.cpp:1019
static QStringList qt_win_make_filters_list(const QString &filter)
static OPENFILENAME * qt_win_make_OFN(QWidget *parent, const QString &initialSelection, const QString &initialDirectory, const QString &title, const QString &filters, QFileDialog::FileMode mode, QFileDialog::Options options)
void setParent(QWidget *parent)
Sets the parent of the widget to parent, and resets the window flags.
Definition: qwidget.cpp:10479
The QDialog class is the base class of dialog windows.
Definition: qdialog.h:56
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
void qt_win_eatMouseMove()
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QString qt_win_filter(const QString &filter, bool hideFiltersDetails)
int indexOf(const QRegExp &rx, int from=0) const
Returns the index position of the first exact match of rx in the list, searching forward from index p...
Definition: qstringlist.h:195
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static QString qt_win_extract_filter(const QString &rawFilter)
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
QWidget * parent
QFileDialog::Options options
void clear()
Clears the contents of the string and makes it empty.
Definition: qstring.h:723
static void qt_win_clean_up_OFN(OPENFILENAME **ofn)
static QString qt_win_selected_filter(const QString &filter, DWORD idx)
void setAttribute(Qt::WidgetAttribute, bool on=true)
Sets the attribute attribute on this widget if on is true; otherwise clears the attribute.
Definition: qwidget.cpp:11087
QString & remove(int i, int len)
Removes n characters from the string, starting at the given position index, and returns a reference t...
Definition: qstring.cpp:1867
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
static void leaveModal(QWidget *)
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
static void enterModal(QWidget *)
const ushort * utf16() const
Returns the QString as a &#39;\0\&#39;-terminated array of unsigned shorts.
Definition: qstring.cpp:5290
static QString homePath()
Returns the absolute path of the user&#39;s home directory.
Definition: qdir.cpp:1942

Variable Documentation

◆ QFileDialogMagic

const qint32 QFileDialogMagic = 0xbe
static

Definition at line 436 of file qfiledialog.cpp.

◆ qt_file_dialog_filter_reg_exp

const char* qt_file_dialog_filter_reg_exp
Initial value:
=
"^(.*)\\(([a-zA-Z0-9_.*? +;#\\-\\[\\]@\\{\\}/!<>\\$%&=^~:\\|]*)\\)$"

Definition at line 438 of file qfiledialog.cpp.

Referenced by qt_mac_extract_filter(), qt_win_extract_filter(), qt_win_filter(), qt_win_resolve_libs(), and QNSOpenSavePanelDelegate::removeExtensions:.

◆ qt_filedialog_existing_directory_hook

Q_GUI_EXPORT _qt_filedialog_existing_directory_hook qt_filedialog_existing_directory_hook = 0

◆ qt_filedialog_open_filename_hook

Q_GUI_EXPORT _qt_filedialog_open_filename_hook qt_filedialog_open_filename_hook = 0

◆ qt_filedialog_open_filenames_hook

Q_GUI_EXPORT _qt_filedialog_open_filenames_hook qt_filedialog_open_filenames_hook = 0

◆ qt_filedialog_save_filename_hook

Q_GUI_EXPORT _qt_filedialog_save_filename_hook qt_filedialog_save_filename_hook = 0

◆ qt_priv_ptr_valid

bool qt_priv_ptr_valid

Definition at line 60 of file qfiledialog_win.cpp.

Referenced by QFileDialog::getExistingDirectory(), and qt_win_resolve_libs().

◆ qt_use_native_dialogs

bool Q_GUI_EXPORT qt_use_native_dialogs = true

Definition at line 331 of file qfiledialog.cpp.