/****************************************************************************
**
-** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
#include <qapplication.h>
#include <private/qapplication_p.h>
#include <private/qfiledialog_p.h>
-#include <private/qt_mac_p.h>
-#include <private/qt_cocoa_helpers_mac_p.h>
+#include "qt_mac_p.h"
+#include "qcocoahelpers.h"
#include <qregexp.h>
#include <qbuffer.h>
#include <qdebug.h>
QT_FORWARD_DECLARE_CLASS(QWidget)
QT_FORWARD_DECLARE_CLASS(QAction)
QT_FORWARD_DECLARE_CLASS(QFileInfo)
+QT_FORWARD_DECLARE_CLASS(QWindow)
QT_USE_NAMESPACE
+typedef QSharedPointer<QFileDialogOptions> SharedPointerFileDialogOptions;
+
@class QT_MANGLE_NAMESPACE(QNSOpenSavePanelDelegate);
@interface QT_MANGLE_NAMESPACE(QNSOpenSavePanelDelegate)
NSView *mAccessoryView;
NSPopUpButton *mPopUpButton;
NSTextField *mTextField;
- QFileDialogPrivate *mPriv;
+ QFileDialog *mFileDialog;
QCocoaFileDialogHelper *mHelper;
NSString *mCurrentDir;
- bool mConfirmOverwrite;
- int mReturnCode;
- QT_PREPEND_NAMESPACE(QFileDialog::AcceptMode) mAcceptMode;
- QT_PREPEND_NAMESPACE(QDir::Filters) *mQDirFilter;
- QT_PREPEND_NAMESPACE(QFileDialog::FileMode) mFileMode;
- QT_PREPEND_NAMESPACE(QFileDialog::Options) *mFileOptions;
+ int mReturnCode;
+ SharedPointerFileDialogOptions mOptions;
QString *mLastFilterCheckPath;
QString *mCurrentSelection;
QStringList *mQDirFilterEntryList;
- (void)filterChanged:(id)sender;
- (void)showModelessPanel;
- (BOOL)runApplicationModalPanel;
-- (void)showWindowModalSheet:(QWidget *)docWidget;
+- (void)showWindowModalSheet:(QWindow *)docWidget;
- (void)updateProperties;
- (QStringList)acceptableExtensionsForSave;
- (QString)removeExtensions:(const QString &)filter;
@implementation QT_MANGLE_NAMESPACE(QNSOpenSavePanelDelegate)
-- (id)initWithAcceptMode:(QT_PREPEND_NAMESPACE(QFileDialog::AcceptMode))acceptMode
- title:(const QString &)title
- hideNameFilterDetails:(bool)hideNameFilterDetails
- qDirFilter:(QT_PREPEND_NAMESPACE(QDir::Filters))qDirFilter
- fileOptions:(QT_PREPEND_NAMESPACE(QFileDialog::Options))fileOptions
- fileMode:(QT_PREPEND_NAMESPACE(QFileDialog::FileMode))fileMode
- selectFile:(const QString &)selectFile
- confirmOverwrite:(bool)confirm
- priv:(QFileDialogPrivate *)priv
+- (id)initWithAcceptMode:
+ (const QString &)selectFile
+ fileDialog:(QFileDialog *)fileDialog
+ options:(SharedPointerFileDialogOptions)options
helper:(QCocoaFileDialogHelper *)helper
{
self = [super init];
-
- mAcceptMode = acceptMode;
- if (mAcceptMode == QT_PREPEND_NAMESPACE(QFileDialog::AcceptOpen)){
+ mFileDialog = fileDialog;
+ mOptions = options;
+ if (mOptions->acceptMode() == QT_PREPEND_NAMESPACE(QFileDialogOptions::AcceptOpen)){
mOpenPanel = [NSOpenPanel openPanel];
mSavePanel = mOpenPanel;
} else {
mSavePanel = [NSSavePanel savePanel];
+ [mSavePanel setCanSelectHiddenExtension:YES];
mOpenPanel = 0;
}
[mSavePanel setLevel:NSModalPanelWindowLevel];
[mSavePanel setDelegate:self];
- mQDirFilter = new QT_PREPEND_NAMESPACE(QDir::Filters)(qDirFilter);
- mFileOptions = new QT_PREPEND_NAMESPACE(QFileDialog::Options)(fileOptions);
- mFileMode = fileMode;
- mConfirmOverwrite = confirm;
mReturnCode = -1;
- mPriv = priv;
mHelper = helper;
mLastFilterCheckPath = new QString;
mQDirFilterEntryList = new QStringList;
- mNameFilterDropDownList = new QStringList(priv->nameFilters);
- QString selectedVisualNameFilter = priv->qFileDialogUi->fileTypeCombo->currentText();
+ mNameFilterDropDownList = new QStringList(mOptions->nameFilters());
+ QString selectedVisualNameFilter = mFileDialog->selectedNameFilter();
mSelectedNameFilter = new QStringList([self findStrippedFilterWithVisualFilterName:selectedVisualNameFilter]);
QFileInfo sel(selectFile);
mCurrentSelection = new QString(sel.absoluteFilePath());
}
- [mSavePanel setTitle:qt_mac_QStringToNSString(title)];
- [self createPopUpButton:selectedVisualNameFilter hideDetails:hideNameFilterDetails];
+ [mSavePanel setTitle:qt_mac_QStringToNSString(options->windowTitle())];
+ [self createPopUpButton:selectedVisualNameFilter hideDetails:options->testOption(QFileDialogOptions::HideNameFilterDetails)];
[self createTextField];
[self createAccessory];
[mSavePanel setAccessoryView:mNameFilterDropDownList->size() > 1 ? mAccessoryView : nil];
- if (mPriv){
- [mSavePanel setPrompt:[self strip:mPriv->acceptLabel]];
- if (mPriv->fileNameLabelExplicitlySat)
- [mSavePanel setNameFieldLabel:[self strip:mPriv->qFileDialogUi->fileNameLabel->text()]];
- }
+
+ if (mOptions->isLabelExplicitlySet(QFileDialogOptions::Accept))
+ [mSavePanel setPrompt:[self strip:options->labelText(QFileDialogOptions::Accept)]];
+ if (mOptions->isLabelExplicitlySet(QFileDialogOptions::FileName))
+ [mSavePanel setNameFieldLabel:[self strip:options->labelText(QFileDialogOptions::FileName)]];
[self updateProperties];
[mSavePanel retain];
- (void)dealloc
{
- delete mQDirFilter;
- delete mFileOptions;
delete mLastFilterCheckPath;
delete mQDirFilterEntryList;
delete mNameFilterDropDownList;
QFileInfo info(*mCurrentSelection);
NSString *filename = QT_PREPEND_NAMESPACE(qt_mac_QStringToNSString)(info.fileName());
NSString *filepath = QT_PREPEND_NAMESPACE(qt_mac_QStringToNSString)(info.filePath());
- bool selectable = (mAcceptMode == QFileDialog::AcceptSave)
+ bool selectable = (mOptions->acceptMode() == QFileDialogOptions::AcceptSave)
|| [self panel:nil shouldShowFilename:filepath];
[mOpenPanel
beginForDirectory:mCurrentDir
QFileInfo info(*mCurrentSelection);
NSString *filename = QT_PREPEND_NAMESPACE(qt_mac_QStringToNSString)(info.fileName());
NSString *filepath = QT_PREPEND_NAMESPACE(qt_mac_QStringToNSString)(info.filePath());
- bool selectable = (mAcceptMode == QFileDialog::AcceptSave)
+ bool selectable = (mOptions->acceptMode() == QFileDialogOptions::AcceptSave)
|| [self panel:nil shouldShowFilename:filepath];
mReturnCode = [mSavePanel
runModalForDirectory:mCurrentDir
return (mReturnCode == NSOKButton);
}
-- (QT_PREPEND_NAMESPACE(QDialog::DialogCode))dialogResultCode
+- (QT_PREPEND_NAMESPACE(QPlatformDialogHelper::DialogCode))dialogResultCode
{
- return (mReturnCode == NSOKButton) ? QT_PREPEND_NAMESPACE(QDialog::Accepted) : QT_PREPEND_NAMESPACE(QDialog::Rejected);
+ return (mReturnCode == NSOKButton) ? QT_PREPEND_NAMESPACE(QPlatformDialogHelper::Accepted) : QT_PREPEND_NAMESPACE(QPlatformDialogHelper::Rejected);
}
-- (void)showWindowModalSheet:(QWidget *)docWidget
+- (void)showWindowModalSheet:(QWindow *)docWidget
{
Q_UNUSED(docWidget);
QFileInfo info(*mCurrentSelection);
NSString *filename = QT_PREPEND_NAMESPACE(qt_mac_QStringToNSString)(info.fileName());
NSString *filepath = QT_PREPEND_NAMESPACE(qt_mac_QStringToNSString)(info.filePath());
- bool selectable = (mAcceptMode == QFileDialog::AcceptSave)
+ bool selectable = (mOptions->acceptMode() == QFileDialogOptions::AcceptSave)
|| [self panel:nil shouldShowFilename:filepath];
[mSavePanel
beginSheetForDirectory:mCurrentDir
QString path = info.absolutePath();
if (path != *mLastFilterCheckPath){
*mLastFilterCheckPath = path;
- *mQDirFilterEntryList = info.dir().entryList(*mQDirFilter);
+ *mQDirFilterEntryList = info.dir().entryList(mOptions->filter());
}
// Check if the QDir filter accepts the file:
if (!mQDirFilterEntryList->contains(info.fileName()))
Q_UNUSED(sender);
if (!okFlag)
return filename;
- if (mConfirmOverwrite)
+ if (!mOptions->testOption(QFileDialogOptions::DontConfirmOverwrite))
return filename;
// User has clicked save, and no overwrite confirmation should occur.
// Call this functions if mFileMode, mFileOptions,
// mNameFilterDropDownList or mQDirFilter changes.
// The savepanel does not contain the neccessary functions for this.
- bool chooseFilesOnly = mFileMode == QT_PREPEND_NAMESPACE(QFileDialog::ExistingFile)
- || mFileMode == QT_PREPEND_NAMESPACE(QFileDialog::ExistingFiles);
- bool chooseDirsOnly = mFileMode == QT_PREPEND_NAMESPACE(QFileDialog::Directory)
- || mFileMode == QT_PREPEND_NAMESPACE(QFileDialog::DirectoryOnly)
- || *mFileOptions & QT_PREPEND_NAMESPACE(QFileDialog::ShowDirsOnly);
+ const QT_PREPEND_NAMESPACE(QFileDialogOptions::FileMode) fileMode = mOptions->fileMode();
+ bool chooseFilesOnly = fileMode == QT_PREPEND_NAMESPACE(QFileDialogOptions::ExistingFile)
+ || fileMode == QT_PREPEND_NAMESPACE(QFileDialogOptions::ExistingFiles);
+ bool chooseDirsOnly = fileMode == QT_PREPEND_NAMESPACE(QFileDialogOptions::Directory)
+ || fileMode == QT_PREPEND_NAMESPACE(QFileDialogOptions::DirectoryOnly)
+ || mOptions->testOption(QT_PREPEND_NAMESPACE(QFileDialogOptions::ShowDirsOnly));
[mOpenPanel setCanChooseFiles:!chooseDirsOnly];
[mOpenPanel setCanChooseDirectories:!chooseFilesOnly];
- [mSavePanel setCanCreateDirectories:!(*mFileOptions & QT_PREPEND_NAMESPACE(QFileDialog::ReadOnly))];
- [mOpenPanel setAllowsMultipleSelection:(mFileMode == QT_PREPEND_NAMESPACE(QFileDialog::ExistingFiles))];
- [mOpenPanel setResolvesAliases:!(*mFileOptions & QT_PREPEND_NAMESPACE(QFileDialog::DontResolveSymlinks))];
+ [mSavePanel setCanCreateDirectories:!(mOptions->testOption(QT_PREPEND_NAMESPACE(QFileDialogOptions::ReadOnly)))];
+ [mOpenPanel setAllowsMultipleSelection:(fileMode == QT_PREPEND_NAMESPACE(QFileDialogOptions::ExistingFiles))];
+ [mOpenPanel setResolvesAliases:!(mOptions->testOption(QT_PREPEND_NAMESPACE(QFileDialogOptions::DontResolveSymlinks)))];
QStringList ext = [self acceptableExtensionsForSave];
- if (mPriv && !ext.isEmpty() && !mPriv->defaultSuffix.isEmpty())
- ext.prepend(mPriv->defaultSuffix);
+ const QString defaultSuffix = mOptions->defaultSuffix();
+ if (!ext.isEmpty() && !defaultSuffix.isEmpty())
+ ext.prepend(defaultSuffix);
[mSavePanel setAllowedFileTypes:ext.isEmpty() ? nil : QT_PREPEND_NAMESPACE(qt_mac_QStringListToNSMutableArray(ext))];
if ([mSavePanel isVisible])
[mTextField setSelectable:false];
[mTextField setBordered:false];
[mTextField setDrawsBackground:false];
- if (mPriv){
- [mTextField setStringValue:[self strip:mPriv->qFileDialogUi->fileTypeLabel->text()]];
- } else
- [mTextField setStringValue:QT_PREPEND_NAMESPACE(qt_mac_QStringToNSString)(QT_PREPEND_NAMESPACE(QFileDialog::tr)("Files of type:"))];
+ if (mOptions->isLabelExplicitlySet(QFileDialogOptions::FileType))
+ [mTextField setStringValue:[self strip:mOptions->labelText(QFileDialogOptions::FileType)]];
}
- (void)createPopUpButton:(const QString &)selectedFilter hideDetails:(BOOL)hideDetails
void QCocoaFileDialogHelper::QNSOpenSavePanelDelegate_selectionChanged(const QString &newPath)
{
- qtFileDialog->metaObject()->invokeMethod(qtFileDialog, "currentChanged", Q_ARG(QString, newPath));
+ emit currentChanged(newPath);
}
void QCocoaFileDialogHelper::QNSOpenSavePanelDelegate_panelClosed(bool accepted)
{
- if (accepted)
- qtFileDialog->metaObject()->invokeMethod(qtFileDialog, "accept");
- else
- qtFileDialog->metaObject()->invokeMethod(qtFileDialog, "reject");
+ if (accepted) {
+ emit accept();
+ } else {
+ emit reject();
+ }
}
void QCocoaFileDialogHelper::QNSOpenSavePanelDelegate_directoryEntered(const QString &newDir)
{
- QFileDialogPrivate *priv = static_cast<QFileDialogPrivate*>(d_ptr);
- priv->setLastVisitedDirectory(newDir);
- qtFileDialog->metaObject()->invokeMethod(qtFileDialog, "directoryEntered", Q_ARG(QString, newDir));
+ // ### fixme: priv->setLastVisitedDirectory(newDir);
+ emit directoryEntered(newDir);
}
void QCocoaFileDialogHelper::QNSOpenSavePanelDelegate_filterSelected(int menuIndex)
{
- QFileDialogPrivate *priv = static_cast<QFileDialogPrivate*>(d_ptr);
- qtFileDialog->metaObject()->invokeMethod(qtFileDialog, "filterSelected", Q_ARG(QString, priv->nameFilters.at(menuIndex)));
+ const QStringList filters = options()->nameFilters();
+ emit filterSelected(menuIndex >= 0 && menuIndex < filters.size() ? filters.at(menuIndex) : QString());
}
extern OSErr qt_mac_create_fsref(const QString &, FSRef *); // qglobal.cpp
return [delegate selectedFiles];
}
-void QCocoaFileDialogHelper::setNameFilters_sys(const QStringList &filters)
-{
- QT_MANGLE_NAMESPACE(QNSOpenSavePanelDelegate) *delegate = static_cast<QT_MANGLE_NAMESPACE(QNSOpenSavePanelDelegate) *>(mDelegate);
- bool hideDetails = qtFileDialog->testOption(QFileDialog::HideNameFilterDetails);
- [delegate setNameFilters:filters hideDetails:hideDetails];
-}
-
void QCocoaFileDialogHelper::setFilter_sys()
{
- QFileDialogPrivate *priv = static_cast<QFileDialogPrivate*>(d_ptr);
QT_MANGLE_NAMESPACE(QNSOpenSavePanelDelegate) *delegate = static_cast<QT_MANGLE_NAMESPACE(QNSOpenSavePanelDelegate) *>(mDelegate);
- *(delegate->mQDirFilter) = priv->model->filter();
- delegate->mFileMode = priv->fileMode;
- [delegate->mSavePanel setTitle:qt_mac_QStringToNSString(qtFileDialog->windowTitle())];
- [delegate->mSavePanel setPrompt:[delegate strip:priv->acceptLabel]];
- if (priv->fileNameLabelExplicitlySat)
- [delegate->mSavePanel setNameFieldLabel:[delegate strip:priv->qFileDialogUi->fileNameLabel->text()]];
+ const SharedPointerFileDialogOptions &opts = options();
+ [delegate->mSavePanel setTitle:qt_mac_QStringToNSString(opts->windowTitle())];
+ if (opts->isLabelExplicitlySet(QFileDialogOptions::Accept))
+ [delegate->mSavePanel setPrompt:[delegate strip:opts->labelText(QFileDialogOptions::Accept)]];
+ if (opts->isLabelExplicitlySet(QFileDialogOptions::FileName))
+ [delegate->mSavePanel setNameFieldLabel:[delegate strip:opts->labelText(QFileDialogOptions::FileName)]];
[delegate updateProperties];
}
void QCocoaFileDialogHelper::selectNameFilter_sys(const QString &filter)
{
- QFileDialogPrivate *priv = static_cast<QFileDialogPrivate*>(d_ptr);
- int index = priv->nameFilters.indexOf(filter);
+ const int index = options()->nameFilters().indexOf(filter);
if (index != -1) {
QT_MANGLE_NAMESPACE(QNSOpenSavePanelDelegate) *delegate = static_cast<QT_MANGLE_NAMESPACE(QNSOpenSavePanelDelegate) *>(mDelegate);
[delegate->mPopUpButton selectItemAtIndex:index];
QString QCocoaFileDialogHelper::selectedNameFilter_sys() const
{
- QFileDialogPrivate *priv = static_cast<QFileDialogPrivate*>(d_ptr);
QT_MANGLE_NAMESPACE(QNSOpenSavePanelDelegate) *delegate = static_cast<QT_MANGLE_NAMESPACE(QNSOpenSavePanelDelegate) *>(mDelegate);
int index = [delegate->mPopUpButton indexOfSelectedItem];
- return index != -1 ? priv->nameFilters.at(index) : QString();
+ return index != -1 ? options()->nameFilters().at(index) : QString();
}
void QCocoaFileDialogHelper::deleteNativeDialog_sys()
{
- QFileDialogPrivate *priv = static_cast<QFileDialogPrivate*>(d_ptr);
[reinterpret_cast<QT_MANGLE_NAMESPACE(QNSOpenSavePanelDelegate) *>(mDelegate) release];
mDelegate = 0;
- priv->nativeDialogInUse = false;
}
-bool QCocoaFileDialogHelper::setVisible_sys(bool visible)
+void QCocoaFileDialogHelper::hide_sys()
+{
+ if (!qtFileDialog->isHidden())
+ hideCocoaFilePanel();
+}
+
+bool QCocoaFileDialogHelper::show_sys(ShowFlags /* flags */, Qt::WindowFlags windowFlags, QWindow *parent)
{
// Q_Q(QFileDialog);
- if (!visible == qtFileDialog->isHidden())
+ if (!qtFileDialog->isHidden())
return false;
- if (qtFileDialog->windowFlags() & Qt::WindowStaysOnTopHint) {
+ if (windowFlags & Qt::WindowStaysOnTopHint) {
// The native file dialog tries all it can to stay
// on the NSModalPanel level. And it might also show
// its own "create directory" dialog that we cannot control.
return false;
}
- return visible ? showCocoaFilePanel() : hideCocoaFilePanel();
+ return showCocoaFilePanel(parent);
}
void QCocoaFileDialogHelper::createNSOpenSavePanelDelegate()
{
- QFileDialogPrivate *priv = static_cast<QFileDialogPrivate*>(d_ptr);
if (mDelegate)
return;
-
- bool selectDir = qtFileDialog->selectedFiles().isEmpty();
- QString selection(selectDir ? qtFileDialog->directory().absolutePath() : qtFileDialog->selectedFiles().value(0));
+ const SharedPointerFileDialogOptions &opts = options();
+ const QStringList selectedFiles = opts->initiallySelectedFiles();
+ const QString directory = opts->initialDirectory();
+ const bool selectDir = selectedFiles.isEmpty();
+ QString selection(selectDir ? directory : selectedFiles.front());
QT_MANGLE_NAMESPACE(QNSOpenSavePanelDelegate) *delegate = [[QT_MANGLE_NAMESPACE(QNSOpenSavePanelDelegate) alloc]
- initWithAcceptMode:priv->acceptMode
- title:qtFileDialog->windowTitle()
- hideNameFilterDetails:qtFileDialog->testOption(QFileDialog::HideNameFilterDetails)
- qDirFilter:priv->model->filter()
- fileOptions:priv->opts
- fileMode:priv->fileMode
- selectFile:selection
- confirmOverwrite:!qtFileDialog->testOption(QFileDialog::DontConfirmOverwrite)
- priv:priv
- helper:this];
+ initWithAcceptMode:
+ selection
+ fileDialog:qtFileDialog
+ options:opts
+ helper:this];
mDelegate = delegate;
}
-bool QCocoaFileDialogHelper::showCocoaFilePanel()
+bool QCocoaFileDialogHelper::showCocoaFilePanel(QWindow *parent)
{
// Q_Q(QFileDialog);
createNSOpenSavePanelDelegate();
QT_MANGLE_NAMESPACE(QNSOpenSavePanelDelegate) *delegate = static_cast<QT_MANGLE_NAMESPACE(QNSOpenSavePanelDelegate) *>(mDelegate);
if (qt_mac_is_macsheet(qtFileDialog))
- [delegate showWindowModalSheet:qtFileDialog->parentWidget()];
+ [delegate showWindowModalSheet:parent];
else
[delegate showModelessPanel];
return true;
// running (which is the case if e.g a top-most QEventLoop has been
// interrupted, and the second-most event loop has not yet been reactivated (regardless
// if [NSApp run] is still on the stack)), showing a native modal dialog will fail.
- QFileDialogPrivate *priv = static_cast<QFileDialogPrivate*>(d_ptr);
- if (priv->nativeDialogInUse){
- QTimer::singleShot(1, qtFileDialog, SLOT(_q_platformRunNativeAppModalPanel()));
- }
+ QTimer::singleShot(1, this, SIGNAL(launchNativeAppModalPanel()));
}
void QCocoaFileDialogHelper::_q_platformRunNativeAppModalPanel()
#endif
QT_MANGLE_NAMESPACE(QNSOpenSavePanelDelegate) *delegate = static_cast<QT_MANGLE_NAMESPACE(QNSOpenSavePanelDelegate) *>(mDelegate);
[delegate runApplicationModalPanel];
- if (dialogResultCode_sys() == QDialog::Accepted)
- qtFileDialog->metaObject()->invokeMethod(qtFileDialog, "accept");
+ if (dialogResultCode_sys() == QPlatformDialogHelper::Accepted)
+ emit accept();
else
- qtFileDialog->metaObject()->invokeMethod(qtFileDialog, "reject");
+ emit reject();
}
-QDialog::DialogCode QCocoaFileDialogHelper::dialogResultCode_sys()
+QPlatformDialogHelper::DialogCode QCocoaFileDialogHelper::dialogResultCode_sys()
{
QT_MANGLE_NAMESPACE(QNSOpenSavePanelDelegate) *delegate = static_cast<QT_MANGLE_NAMESPACE(QNSOpenSavePanelDelegate) *>(mDelegate);
return [delegate dialogResultCode];