Kaydet (Commit) 6196b729 authored tarafından Katarina Behrens's avatar Katarina Behrens

Copy filepicker classes from gtk3_kde5, don't build yet

Change-Id: Ic18add9e1e0a6a7e4480df17885670a0796f074a
üst ea1e0581
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "kde5_filepicker.hxx"
#include <KWindowSystem>
#include <KFileWidget>
#include <QtCore/QDebug>
#include <QtCore/QUrl>
#include <QtGui/QClipboard>
#include <QtGui/QWindow>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QFileDialog>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QWidget>
#include <QtWidgets/QApplication>
// KDE5FilePicker
KDE5FilePicker::KDE5FilePicker(QObject* parent)
: QObject(parent)
, _dialog(new QFileDialog(nullptr, {}, QDir::homePath()))
, _extraControls(new QWidget)
, _layout(new QGridLayout(_extraControls))
, _winId(0)
, allowRemoteUrls(false)
{
_dialog->setSupportedSchemes({
QStringLiteral("file"),
QStringLiteral("ftp"),
QStringLiteral("http"),
QStringLiteral("https"),
QStringLiteral("webdav"),
QStringLiteral("webdavs"),
QStringLiteral("smb"),
});
setMultiSelectionMode(false);
connect(_dialog, &QFileDialog::filterSelected, this, &KDE5FilePicker::filterChanged);
connect(_dialog, &QFileDialog::fileSelected, this, &KDE5FilePicker::selectionChanged);
qApp->installEventFilter(this);
}
void KDE5FilePicker::enableFolderMode()
{
_dialog->setOption(QFileDialog::ShowDirsOnly, true);
_dialog->setFileMode(QFileDialog::Directory);
}
KDE5FilePicker::~KDE5FilePicker()
{
delete _extraControls;
delete _dialog;
}
void KDE5FilePicker::setTitle(const QString& title) { _dialog->setWindowTitle(title); }
bool KDE5FilePicker::execute()
{
if (!_filters.isEmpty())
_dialog->setNameFilters(_filters);
if (!_currentFilter.isEmpty())
_dialog->selectNameFilter(_currentFilter);
_dialog->show();
//block and wait for user input
return _dialog->exec() == QFileDialog::Accepted;
}
void KDE5FilePicker::setMultiSelectionMode(bool multiSelect)
{
_dialog->setFileMode(multiSelect ? QFileDialog::ExistingFiles : QFileDialog::ExistingFile);
}
void KDE5FilePicker::setDefaultName(const QString& name) { _dialog->selectUrl(QUrl(name)); }
void KDE5FilePicker::setDisplayDirectory(const QString& dir) { _dialog->selectUrl(QUrl(dir)); }
QString KDE5FilePicker::getDisplayDirectory() const { return _dialog->directoryUrl().url(); }
QList<QUrl> KDE5FilePicker::getSelectedFiles() const { return _dialog->selectedUrls(); }
void KDE5FilePicker::appendFilter(const QString& title, const QString& filter)
{
QString t = title;
QString f = filter;
// '/' need to be escaped else they are assumed to be mime types by kfiledialog
//see the docs
t.replace("/", "\\/");
// openoffice gives us filters separated by ';' qt dialogs just want space separated
f.replace(";", " ");
// make sure "*.*" is not used as "all files"
f.replace("*.*", "*");
_filters << QStringLiteral("%1 (%2)").arg(t, f);
_titleToFilters[t] = _filters.constLast();
}
void KDE5FilePicker::setCurrentFilter(const QString& title)
{
_currentFilter = _titleToFilters.value(title);
}
QString KDE5FilePicker::getCurrentFilter() const
{
QString filter = _titleToFilters.key(_dialog->selectedNameFilter());
//default if not found
if (filter.isEmpty())
filter = "ODF Text Document (.odt)";
return filter;
}
void KDE5FilePicker::setValue(sal_Int16 controlId, sal_Int16 /*nControlAction*/, bool value)
{
if (_customWidgets.contains(controlId))
{
QCheckBox* cb = dynamic_cast<QCheckBox*>(_customWidgets.value(controlId));
if (cb)
cb->setChecked(value);
}
else
qWarning() << "set value on unknown control" << controlId;
}
bool KDE5FilePicker::getValue(sal_Int16 controlId, sal_Int16 /*nControlAction*/) const
{
bool ret = false;
if (_customWidgets.contains(controlId))
{
QCheckBox* cb = dynamic_cast<QCheckBox*>(_customWidgets.value(controlId));
if (cb)
ret = cb->isChecked();
}
else
qWarning() << "get value on unknown control" << controlId;
return ret;
}
void KDE5FilePicker::enableControl(sal_Int16 controlId, bool enable)
{
if (_customWidgets.contains(controlId))
_customWidgets.value(controlId)->setEnabled(enable);
else
qWarning() << "enable on unknown control" << controlId;
}
void KDE5FilePicker::setLabel(sal_Int16 controlId, const QString& label)
{
if (_customWidgets.contains(controlId))
{
QCheckBox* cb = dynamic_cast<QCheckBox*>(_customWidgets.value(controlId));
if (cb)
cb->setText(label);
}
else
qWarning() << "set label on unknown control" << controlId;
}
QString KDE5FilePicker::getLabel(sal_Int16 controlId) const
{
QString label;
if (_customWidgets.contains(controlId))
{
QCheckBox* cb = dynamic_cast<QCheckBox*>(_customWidgets.value(controlId));
if (cb)
label = cb->text();
}
else
qWarning() << "get label on unknown control" << controlId;
return label;
}
void KDE5FilePicker::addCheckBox(sal_Int16 controlId, const QString& label, bool hidden)
{
auto resString = label;
resString.replace('~', '&');
auto widget = new QCheckBox(resString, _extraControls);
widget->setHidden(hidden);
if (!hidden)
{
_layout->addWidget(widget);
}
_customWidgets.insert(controlId, widget);
}
void KDE5FilePicker::initialize(bool saveDialog)
{
//default is opening
QFileDialog::AcceptMode operationMode
= saveDialog ? QFileDialog::AcceptSave : QFileDialog::AcceptOpen;
_dialog->setAcceptMode(operationMode);
if (saveDialog)
{
_dialog->setConfirmOverwrite(true);
_dialog->setFileMode(QFileDialog::AnyFile);
}
}
void KDE5FilePicker::setWinId(sal_uIntPtr winId) { _winId = winId; }
bool KDE5FilePicker::eventFilter(QObject* o, QEvent* e)
{
if (e->type() == QEvent::Show && o->isWidgetType())
{
auto* w = static_cast<QWidget*>(o);
if (!w->parentWidget() && w->isModal())
{
KWindowSystem::setMainWindow(w, _winId);
if (auto* fileWidget = w->findChild<KFileWidget*>({}, Qt::FindDirectChildrenOnly))
fileWidget->setCustomWidget(_extraControls);
}
}
return QObject::eventFilter(o, e);
}
#include <kde5_filepicker.moc>
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#pragma once
#include <QtCore/QObject>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QHash>
#include <sal/types.h>
class QFileDialog;
class QWidget;
class QGridLayout;
class KDE5FilePicker : public QObject
{
Q_OBJECT
protected:
//the dialog to display
QFileDialog* _dialog;
//running filter string to add to dialog
QStringList _filters;
// map of filter titles to full filter for selection
QHash<QString, QString> _titleToFilters;
// string to set the current filter
QString _currentFilter;
//mapping of SAL control ID's to created custom controls
QHash<sal_Int16, QWidget*> _customWidgets;
//widget to contain extra custom controls
QWidget* _extraControls;
//layout for extra custom controls
QGridLayout* _layout;
sal_uIntPtr _winId;
bool allowRemoteUrls;
public:
explicit KDE5FilePicker(QObject* parent = nullptr);
~KDE5FilePicker() override;
void enableFolderMode();
// XExecutableDialog functions
void setTitle(const QString& rTitle);
bool execute();
// XFilePicker functions
void setMultiSelectionMode(bool bMode);
void setDefaultName(const QString& rName);
void setDisplayDirectory(const QString& rDirectory);
QString getDisplayDirectory() const;
// XFilterManager functions
void appendFilter(const QString& rTitle, const QString& rFilter);
void setCurrentFilter(const QString& rTitle);
QString getCurrentFilter() const;
// XFilePickerControlAccess functions
void setValue(sal_Int16 nControlId, sal_Int16 nControlAction, bool rValue);
bool getValue(sal_Int16 nControlId, sal_Int16 nControlAction) const;
void enableControl(sal_Int16 nControlId, bool bEnable);
void setLabel(sal_Int16 nControlId, const QString& rLabel);
QString getLabel(sal_Int16 nControlId) const;
// XFilePicker2 functions
QList<QUrl> getSelectedFiles() const;
// XInitialization
void initialize(bool saveDialog);
//add a custom control widget to the file dialog
void addCheckBox(sal_Int16 nControlId, const QString& label, bool hidden);
void setWinId(sal_uIntPtr winId);
private:
Q_DISABLE_COPY(KDE5FilePicker)
protected:
bool eventFilter(QObject* watched, QEvent* event) override;
Q_SIGNALS:
void filterChanged();
void selectionChanged();
};
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
This diff is collapsed.
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#pragma once
#include <cppuhelper/compbase.hxx>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/lang/XInitialization.hpp>
#include <com/sun/star/ui/dialogs/XFilePicker3.hpp>
#include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <osl/conditn.hxx>
#include <osl/mutex.hxx>
#include <rtl/ustrbuf.hxx>
#include "gtk3_kde5_filepicker_ipc.hxx"
#include <functional>
typedef ::cppu::WeakComponentImplHelper<css::ui::dialogs::XFilePicker3,
css::ui::dialogs::XFilePickerControlAccess
// TODO css::ui::dialogs::XFilePreview
,
css::lang::XInitialization, css::lang::XServiceInfo>
KDE5FilePicker2_Base;
class KDE5FilePicker2 : public KDE5FilePicker2_Base
{
protected:
css::uno::Reference<css::ui::dialogs::XFilePickerListener> m_xListener;
osl::Mutex _helperMutex;
KDE5FilePicker2Ipc m_ipc;
public:
explicit KDE5FilePicker2(const css::uno::Reference<css::uno::XComponentContext>&);
virtual ~KDE5FilePicker2() override;
// XFilePickerNotifier
virtual void SAL_CALL addFilePickerListener(
const css::uno::Reference<css::ui::dialogs::XFilePickerListener>& xListener) override;
virtual void SAL_CALL removeFilePickerListener(
const css::uno::Reference<css::ui::dialogs::XFilePickerListener>& xListener) override;
// XExecutableDialog functions
virtual void SAL_CALL setTitle(const OUString& rTitle) override;
virtual sal_Int16 SAL_CALL execute() override;
// XFilePicker functions
virtual void SAL_CALL setMultiSelectionMode(sal_Bool bMode) override;
virtual void SAL_CALL setDefaultName(const OUString& rName) override;
virtual void SAL_CALL setDisplayDirectory(const OUString& rDirectory) override;
virtual OUString SAL_CALL getDisplayDirectory() override;
virtual css::uno::Sequence<OUString> SAL_CALL getFiles() override;
// XFilterManager functions
virtual void SAL_CALL appendFilter(const OUString& rTitle, const OUString& rFilter) override;
virtual void SAL_CALL setCurrentFilter(const OUString& rTitle) override;
virtual OUString SAL_CALL getCurrentFilter() override;
// XFilterGroupManager functions
virtual void SAL_CALL
appendFilterGroup(const OUString& rGroupTitle,
const css::uno::Sequence<css::beans::StringPair>& rFilters) override;
// XFilePickerControlAccess functions
virtual void SAL_CALL setValue(sal_Int16 nControlId, sal_Int16 nControlAction,
const css::uno::Any& rValue) override;
virtual css::uno::Any SAL_CALL getValue(sal_Int16 nControlId,
sal_Int16 nControlAction) override;
virtual void SAL_CALL enableControl(sal_Int16 nControlId, sal_Bool bEnable) override;
virtual void SAL_CALL setLabel(sal_Int16 nControlId, const OUString& rLabel) override;
virtual OUString SAL_CALL getLabel(sal_Int16 nControlId) override;
/* TODO XFilePreview
virtual css::uno::Sequence< sal_Int16 > SAL_CALL getSupportedImageFormats( );
virtual sal_Int32 SAL_CALL getTargetColorDepth( );
virtual sal_Int32 SAL_CALL getAvailableWidth( );
virtual sal_Int32 SAL_CALL getAvailableHeight( );
virtual void SAL_CALL setImage( sal_Int16 aImageFormat, const css::uno::Any &rImage );
virtual sal_Bool SAL_CALL setShowState( sal_Bool bShowState );
virtual sal_Bool SAL_CALL getShowState( );
*/
// XFilePicker2 functions
virtual css::uno::Sequence<OUString> SAL_CALL getSelectedFiles() override;
// XInitialization
virtual void SAL_CALL initialize(const css::uno::Sequence<css::uno::Any>& rArguments) override;
// XCancellable
virtual void SAL_CALL cancel() override;
// XEventListener
virtual void disposing(const css::lang::EventObject& rEvent);
using cppu::WeakComponentImplHelperBase::disposing;
// XServiceInfo
virtual OUString SAL_CALL getImplementationName() override;
virtual sal_Bool SAL_CALL supportsService(const OUString& rServiceName) override;
virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
private:
KDE5FilePicker2(const KDE5FilePicker2&) = delete;
KDE5FilePicker2& operator=(const KDE5FilePicker2&) = delete;
//add a custom control widget to the file dialog
void addCustomControl(sal_Int16 controlId);
// emit XFilePickerListener controlStateChanged event
void filterChanged();
// emit XFilePickerListener fileSelectionChanged event
void selectionChanged();
};
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment