Index: libs/widgets/CMakeLists.txt =================================================================== --- libs/widgets/CMakeLists.txt +++ libs/widgets/CMakeLists.txt @@ -37,7 +37,6 @@ KoZoomMode.cpp KoDpi.cpp KoGlobal.cpp - KoFileDialog.cpp KoColorPatch.cpp KoColorPopupAction.cpp KoColorSetWidget.cpp @@ -147,7 +146,6 @@ KoZoomHandler.h KoZoomMode.h KoGlobal.h - KoFileDialog.h KoColorPatch.h KoStrokeConfigWidget.h KoFillConfigWidget.h @@ -176,14 +174,3 @@ DESTINATION ${INCLUDE_INSTALL_DIR}/calligra COMPONENT Devel) - -set(filedialogtester_SRCS - KoFileDialogTester.cpp - main.cpp -) -ki18n_wrap_ui(filedialogtester_SRCS - KoFileDialogTester.ui -) - -add_executable(filedialogtester ${filedialogtester_SRCS}) -target_link_libraries(filedialogtester kowidgets) Index: libs/widgets/KoFileDialog.h =================================================================== --- libs/widgets/KoFileDialog.h +++ /dev/null @@ -1,155 +0,0 @@ -/* This file is part of the KDE project - Copyright (C) 2013 - 2014 Yue Liu - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. -*/ - -#ifndef KOFILEDIALOG_H -#define KOFILEDIALOG_H - -#include "kowidgets_export.h" - -#include -#include -#include -#include -#include - - -/** - * Wrapper around QFileDialog providing native file dialogs - * on KDE/Gnome/Windows/OSX/etc. - */ -class KOWIDGETS_EXPORT KoFileDialog : public QObject -{ - Q_OBJECT - -public: - enum DialogType { - OpenFile, - OpenFiles, - OpenDirectory, - ImportFile, - ImportFiles, - ImportDirectory, - SaveFile - }; - - /** - * @brief constructor - * @param parent The parent of the file dialog - * @param dialogType usage of the file dialog - * @param dialogName the name for the file dialog. This will be used to open - * the filedialog in the last open location, instead the specified directory. - * - * @return The name of the entry user selected in the file dialog - * - */ - KoFileDialog(QWidget *parent, - KoFileDialog::DialogType type, - const QString &dialogName); - - ~KoFileDialog(); - - void setCaption(const QString &caption); - - /** - * @brief setDefaultDir set the default directory to defaultDir - * - * @param defaultDir a path to a file or directory - */ - void setDefaultDir(const QString &defaultDir, bool override = false); - - /** - * @brief setOverrideDir override both the default dir and the saved dir found by dialogName - * @param overrideDir a path to a file or directory - */ - void setOverrideDir(const QString &overrideDir); - - /** - * @brief setImageFilters sets the name filters for the file dialog to all - * image formats Qt's QImageReader supports. - */ - void setImageFilters(); - - void setNameFilter(const QString &filter); - - /** - * @brief setNameFilters set a list of description/extension pairs. - * - * These are not registered mimetypes. In contrast with Qt's filedialog namefilters, - * you can only have _one_ pair per line. I.e. - * - * Gif Image (*gif) - * Tiff Image (*tif) - * - * And NOT Images (*gif *tif) - * - * @param filterList - * @param defaultFilter - */ - void setNameFilters(const QStringList &filterList, - QString defaultFilter = QString()); - void setMimeTypeFilters(const QStringList &filterList, - QString defaultFilter = QString()); - void setHideNameFilterDetailsOption(); - - QStringList nameFilters() const; - - QStringList filenames(); - QString filename(); - - /** - * @brief selectedNameFilter returns the name filter the user selected, either - * directory or by clicking on it. - * @return - */ - QString selectedNameFilter() const; - - QString selectedMimeType() const; - -private Q_SLOTS: - - void filterSelected(const QString &filter); - -private: - /** - * @brief splitNameFilter take a single line of a QDialog name filter and split it - * into several lines. This is needed because a single line name filter can contain - * more than one mimetype, making it impossible to figure out the correct extension. - * - * The methods takes care of some duplicated extensions, like jpeg and jpg. - * @param nameFilter the namefilter to be split - * @param mimeList a pointer to the list with mimes that shouldn't be added. - * @return a stringlist of all name filters. - */ - static QStringList splitNameFilter(const QString &nameFilter, QStringList *mimeList); - - void createFileDialog(); - - QString getUsedDir(const QString &dialogName); - void saveUsedDir(const QString &fileName, const QString &dialogName); - - const QStringList getFilterStringListFromMime(const QStringList &mimeList, - bool withAllSupportedEntry = false); - - - - class Private; - Private * const d; -}; - -#endif /* KOFILEDIALOG_H */ Index: libs/widgets/KoFileDialog.cpp =================================================================== --- libs/widgets/KoFileDialog.cpp +++ /dev/null @@ -1,502 +0,0 @@ -/* This file is part of the KDE project - Copyright (C) 2013 - 2014 Yue Liu - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. -*/ - -#include "KoFileDialog.h" -#include -#include -#include -#include -#include - -#include -#include -#include - -#include -#include -#include - -class Q_DECL_HIDDEN KoFileDialog::Private -{ -public: - Private(QWidget *parent_, - KoFileDialog::DialogType dialogType_, - const QString caption_, - const QString defaultDir_, - const QString dialogName_) - : parent(parent_) - , type(dialogType_) - , dialogName(dialogName_) - , caption(caption_) - , defaultDirectory(defaultDir_) - , filterList(QStringList()) - , defaultFilter(QString()) - , useStaticForNative(false) - , hideDetails(false) - , swapExtensionOrder(false) - { - // Force the native file dialogs on Windows. Except for KDE, the native file dialogs are only possible - // using the static methods. The Qt documentation is wrong here, if it means what it says " By default, - // the native file dialog is used unless you use a subclass of QFileDialog that contains the Q_OBJECT - // macro." -#ifdef Q_OS_WIN - useStaticForNative = true; -#endif - // Non-static KDE file is broken when called with QFileDialog::AcceptSave: - // then the directory above defaultdir is opened, and defaultdir is given as the default file name... - // - // So: in X11, use static methods inside KDE, which give working native dialogs, but non-static outside - // KDE, which gives working Qt dialogs. - // - // Only show the GTK dialog in Gnome, where people deserve it -#ifdef HAVE_X11 - if (qgetenv("KDE_FULL_SESSION").size() > 0) { - useStaticForNative = true; - } - if (qgetenv("XDG_CURRENT_DESKTOP") == "GNOME") { - useStaticForNative = true; - QClipboard *cb = QApplication::clipboard(); - cb->blockSignals(true); - swapExtensionOrder = true; - } - -#endif - } - - ~Private() - { - if (qgetenv("XDG_CURRENT_DESKTOP") == "GNOME") { - useStaticForNative = true; - QClipboard *cb = QApplication::clipboard(); - cb->blockSignals(false); - } - } - - QWidget *parent; - KoFileDialog::DialogType type; - QString dialogName; - QString caption; - QString defaultDirectory; - QStringList filterList; - QString defaultFilter; - QScopedPointer fileDialog; - QMimeType mimeType; - bool useStaticForNative; - bool hideDetails; - bool swapExtensionOrder; -}; - -KoFileDialog::KoFileDialog(QWidget *parent, - KoFileDialog::DialogType type, - const QString &dialogName) - : d(new Private(parent, type, "", getUsedDir(dialogName), dialogName)) -{ -} - -KoFileDialog::~KoFileDialog() -{ - delete d; -} - -void KoFileDialog::setCaption(const QString &caption) -{ - d->caption = caption; -} - -void KoFileDialog::setDefaultDir(const QString &defaultDir, bool override) -{ - if (override || d->defaultDirectory.isEmpty() || !QFile(d->defaultDirectory).exists()) { - QFileInfo f(defaultDir); - d->defaultDirectory = f.absoluteFilePath(); - } -} - -void KoFileDialog::setOverrideDir(const QString &overrideDir) -{ - d->defaultDirectory = overrideDir; -} - -void KoFileDialog::setImageFilters() -{ - QStringList imageFilters; - // add filters for all formats supported by QImage - foreach(const QByteArray &format, QImageReader::supportedImageFormats()) { - imageFilters << QLatin1String("image/") + format; - } - setMimeTypeFilters(imageFilters); -} - -void KoFileDialog::setNameFilter(const QString &filter) -{ - d->filterList.clear(); - if (d->type == KoFileDialog::SaveFile) { - QStringList mimeList; - d->filterList << splitNameFilter(filter, &mimeList); - d->defaultFilter = d->filterList.first(); - } - else { - d->filterList << filter; - } -} - -void KoFileDialog::setNameFilters(const QStringList &filterList, - QString defaultFilter) -{ - d->filterList.clear(); - - if (d->type == KoFileDialog::SaveFile) { - QStringList mimeList; - foreach(const QString &filter, filterList) { - d->filterList << splitNameFilter(filter, &mimeList); - } - - if (!defaultFilter.isEmpty()) { - mimeList.clear(); - QStringList defaultFilters = splitNameFilter(defaultFilter, &mimeList); - if (defaultFilters.size() > 0) { - defaultFilter = defaultFilters.first(); - } - } - } - else { - d->filterList = filterList; - } - d->defaultFilter = defaultFilter; - -} - -void KoFileDialog::setMimeTypeFilters(const QStringList &filterList, - QString defaultFilter) -{ - d->filterList = getFilterStringListFromMime(filterList, true); - - if (!defaultFilter.isEmpty()) { - QStringList defaultFilters = getFilterStringListFromMime(QStringList() << defaultFilter, false); - if (defaultFilters.size() > 0) { - defaultFilter = defaultFilters.first(); - } - } - d->defaultFilter = defaultFilter; -} - -void KoFileDialog::setHideNameFilterDetailsOption() -{ - d->hideDetails = true; -} - -QStringList KoFileDialog::nameFilters() const -{ - return d->filterList; -} - -QString KoFileDialog::selectedNameFilter() const -{ - if (!d->useStaticForNative) { - return d->fileDialog->selectedNameFilter(); - } - else { - return d->defaultFilter; - } -} - -QString KoFileDialog::selectedMimeType() const -{ - if (d->mimeType.isValid()) { - return d->mimeType.name(); - } - else { - return ""; - } -} - -void KoFileDialog::createFileDialog() -{ - d->fileDialog.reset(new QFileDialog(d->parent, d->caption, d->defaultDirectory)); - - if (d->type == SaveFile) { - d->fileDialog->setAcceptMode(QFileDialog::AcceptSave); - d->fileDialog->setFileMode(QFileDialog::AnyFile); - } - else { // open / import - - d->fileDialog->setAcceptMode(QFileDialog::AcceptOpen); - - if (d->type == ImportDirectory - || d->type == OpenDirectory) - { - d->fileDialog->setFileMode(QFileDialog::Directory); - d->fileDialog->setOption(QFileDialog::ShowDirsOnly, true); - } - else { // open / import file(s) - if (d->type == OpenFile - || d->type == ImportFile) - { - d->fileDialog->setFileMode(QFileDialog::ExistingFile); - } - else { // files - d->fileDialog->setFileMode(QFileDialog::ExistingFiles); - } - } - } - - d->fileDialog->setNameFilters(d->filterList); - if (!d->defaultFilter.isEmpty()) { - d->fileDialog->selectNameFilter(d->defaultFilter); - } - - if (d->type == ImportDirectory || - d->type == ImportFile || d->type == ImportFiles || - d->type == SaveFile) { - d->fileDialog->setWindowModality(Qt::WindowModal); - } - - if (d->hideDetails) { - d->fileDialog->setOption(QFileDialog::HideNameFilterDetails); - } - - connect(d->fileDialog.data(), SIGNAL(filterSelected(QString)), this, SLOT(filterSelected(QString))); -} - -QString KoFileDialog::filename() -{ - QString url; - if (!d->useStaticForNative) { - - if (!d->fileDialog) { - createFileDialog(); - } - - if (d->fileDialog->exec() == QDialog::Accepted) { - url = d->fileDialog->selectedFiles().first(); - } - } - else { - switch (d->type) { - case OpenFile: - { - url = QFileDialog::getOpenFileName(d->parent, - d->caption, - d->defaultDirectory, - d->filterList.join(";;"), - &d->defaultFilter); - break; - } - case OpenDirectory: - { - url = QFileDialog::getExistingDirectory(d->parent, - d->caption, - d->defaultDirectory, - QFileDialog::ShowDirsOnly); - break; - } - case ImportFile: - { - url = QFileDialog::getOpenFileName(d->parent, - d->caption, - d->defaultDirectory, - d->filterList.join(";;"), - &d->defaultFilter); - break; - } - case ImportDirectory: - { - url = QFileDialog::getExistingDirectory(d->parent, - d->caption, - d->defaultDirectory, - QFileDialog::ShowDirsOnly); - break; - } - case SaveFile: - { - url = QFileDialog::getSaveFileName(d->parent, - d->caption, - d->defaultDirectory, - d->filterList.join(";;"), - &d->defaultFilter); - break; - } - default: - ; - } - } - - if (!url.isEmpty()) { - - if (d->type == SaveFile && QFileInfo(url).suffix().isEmpty()) { - int start = d->defaultFilter.lastIndexOf("*.") + 1; - int end = d->defaultFilter.lastIndexOf(" )"); - int n = end - start; - QString extension = d->defaultFilter.mid(start, n); - url.append(extension); - } - - QMimeDatabase db; - d->mimeType = db.mimeTypeForFile(url); - saveUsedDir(url, d->dialogName); - } - return url; -} - -QStringList KoFileDialog::filenames() -{ - QStringList urls; - - if (!d->useStaticForNative) { - if (!d->fileDialog) { - createFileDialog(); - } - if (d->fileDialog->exec() == QDialog::Accepted) { - urls = d->fileDialog->selectedFiles(); - } - } - else { - switch (d->type) { - case OpenFiles: - case ImportFiles: - { - urls = QFileDialog::getOpenFileNames(d->parent, - d->caption, - d->defaultDirectory, - d->filterList.join(";;"), - &d->defaultFilter); - break; - } - default: - ; - } - } - if (urls.size() > 0) { - saveUsedDir(urls.first(), d->dialogName); - } - return urls; -} - -void KoFileDialog::filterSelected(const QString &filter) -{ - // "Windows BMP image ( *.bmp )"; - int start = filter.lastIndexOf("*.") + 2; - int end = filter.lastIndexOf(" )"); - int n = end - start; - QString extension = filter.mid(start, n); - d->defaultFilter = filter; - d->fileDialog->setDefaultSuffix(extension); -} - -QStringList KoFileDialog::splitNameFilter(const QString &nameFilter, QStringList *mimeList) -{ - Q_ASSERT(mimeList); - - QStringList filters; - QString description; - - if (nameFilter.contains("(")) { - description = nameFilter.left(nameFilter.indexOf("(") -1).trimmed(); - } - - QStringList entries = nameFilter.mid(nameFilter.indexOf("(") + 1).split(" ",QString::SkipEmptyParts ); - - foreach(QString entry, entries) { - - entry = entry.remove("*"); - entry = entry.remove(")"); - - QMimeDatabase db; - QMimeType mime = db.mimeTypeForName("bla" + entry); - if (mime.name() != "application/octet-stream") { - if (!mimeList->contains(mime.name())) { - mimeList->append(mime.name()); - filters.append(mime.comment() + " ( *" + entry + " )"); - } - } - else { - filters.append(entry.remove(".").toUpper() + " " + description + " ( *." + entry + " )"); - } - } - - return filters; -} - -const QStringList KoFileDialog::getFilterStringListFromMime(const QStringList &mimeList, - bool withAllSupportedEntry) -{ - QStringList mimeSeen; - - QStringList ret; - if (withAllSupportedEntry) { - ret << QString(); - } - - for (QStringList::ConstIterator - it = mimeList.begin(); it != mimeList.end(); ++it) { - QMimeDatabase db; - QMimeType mimeType = db.mimeTypeForName(*it); - if (!mimeType.isValid()) { - continue; - } - if (!mimeSeen.contains(mimeType.name())) { - QString oneFilter; - QStringList patterns = mimeType.globPatterns(); - QStringList::ConstIterator jt; - for (jt = patterns.constBegin(); jt != patterns.constEnd(); ++jt) { - if (d->swapExtensionOrder) { - oneFilter.prepend(*jt + " "); - if (withAllSupportedEntry) { - ret[0].prepend(*jt + " "); - } - } - else { - oneFilter.append(*jt + " "); - if (withAllSupportedEntry) { - ret[0].append(*jt + " "); - } - } - } - oneFilter = mimeType.comment() + " ( " + oneFilter + ")"; - ret << oneFilter; - mimeSeen << mimeType.name(); - } - } - - if (withAllSupportedEntry) { - ret[0] = i18n("All supported formats") + " ( " + ret[0] + (")"); - } - return ret; -} - -QString KoFileDialog::getUsedDir(const QString &dialogName) -{ - if (dialogName.isEmpty()) return ""; - - KConfigGroup group = KSharedConfig::openConfig()->group("File Dialogs"); - QString dir = group.readEntry(dialogName); - - return dir; -} - -void KoFileDialog::saveUsedDir(const QString &fileName, - const QString &dialogName) -{ - - if (dialogName.isEmpty()) return; - - QFileInfo fileInfo(fileName); - KConfigGroup group = KSharedConfig::openConfig()->group("File Dialogs"); - group.writeEntry(dialogName, fileInfo.absolutePath()); - -} Index: libs/widgets/KoFileDialogTester.h =================================================================== --- libs/widgets/KoFileDialogTester.h +++ /dev/null @@ -1,53 +0,0 @@ -/* This file is part of the KDE project - * Copyright (C) Boudewijn Rempt , (C) 2014 - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public License - * along with this library; see the file COPYING.LIB. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - */ -#ifndef KOFILEDIALOGTESTER_H -#define KOFILEDIALOGTESTER_H - -#include - -namespace Ui { -class KoFileDialogTester; -} - -class KoFileDialogTester : public QWidget -{ - Q_OBJECT - -public: - explicit KoFileDialogTester(QWidget *parent = 0); - ~KoFileDialogTester(); - -private Q_SLOTS: - - void testOpenFile(); - void testOpenFiles(); - void testOpenDirectory(); - void testImportFile(); - void testImportFiles(); - void testImportDirectory(); - void testSaveFile(); - -private: - Ui::KoFileDialogTester *ui; - - QStringList m_nameFilters; - QStringList m_mimeFilter; -}; - -#endif // KOFILEDIALOGTESTER_H Index: libs/widgets/KoFileDialogTester.cpp =================================================================== --- libs/widgets/KoFileDialogTester.cpp +++ /dev/null @@ -1,328 +0,0 @@ -/* This file is part of the KDE project - * Copyright (C) Boudewijn Rempt , (C) 2014 - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public License - * along with this library; see the file COPYING.LIB. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - */ -#include "KoFileDialogTester.h" - -#include "ui_KoFileDialogTester.h" - -#include -#include -#include -#include - -#include - -KoFileDialogTester::KoFileDialogTester(QWidget *parent) : - QWidget(parent), - ui(new Ui::KoFileDialogTester) -{ - ui->setupUi(this); - - connect(ui->bnOpenFile, SIGNAL(clicked()), SLOT(testOpenFile())); - connect(ui->bnOpenFiles, SIGNAL(clicked()), SLOT(testOpenFiles())); - connect(ui->bnOpenDirectory, SIGNAL(clicked()), SLOT(testOpenDirectory())); - connect(ui->bnImportFile, SIGNAL(clicked()), SLOT(testImportFile())); - connect(ui->bnImportFiles, SIGNAL(clicked()), SLOT(testImportFiles())); - connect(ui->bnImportDirectory, SIGNAL(clicked()), SLOT(testImportDirectory())); - connect(ui->bnSaveFile, SIGNAL(clicked()), SLOT(testSaveFile())); - - m_nameFilters << "Documents (*.odt *.doc *.txt)" - << "Images (*.png *.jpg *.jpeg)" - << "Presentations (*.ppt *.odp)" - << "Patterns (*.pat *.jpg *.gif *.png *.tif *.xpm *.bmp)" - << "Palettes (*.gpl *.pal *.act *.aco *.colors)"; - - m_mimeFilter = QStringList() - << "application/x-krita" << "image/x-exr" << "image/openraster" << "image/x-tga" << "image/vnd.adobe.photoshop" - << "image/x-xcf" << "image/x-portable-pixmap" << "image/x-portable-graymap" - << "image/x-portable-bitmap" << "image/png" << "image/jp2" - << "image/tiff" << "application/vnd.oasis.opendocument.graphics" - << "application/pdf" << "image/jpeg" << "image/bmp" << "image/x-xpixmap" - << "image/gif" << "image/x-xbitmap" << "application/x-krita-flipbook" - << "image/x-adobe-dng" << "image/x-xfig" << "image/svg+xml" << "image/svg+xml-compressed" - << "image/x-eps" << "image/eps" << "application/eps" << "application/x-eps" << "application/postscript" - << "image/x-wmf" << "application/x-karbon" - << "image/tiff" << "application/vnd.oasis.opendocument.graphics" - ; -} - -KoFileDialogTester::~KoFileDialogTester() -{ - delete ui; -} - - - -void KoFileDialogTester::testOpenFile() -{ - ui->listResults->clear(); - KoFileDialog dlg(this, KoFileDialog::OpenFile, ui->txtUniqueKey->text()); - dlg.setCaption(i18n("Testing: OpenFile")); - dlg.setDefaultDir(QDesktopServices::storageLocation(QDesktopServices::HomeLocation)); - if (ui->radioName->isChecked()) { - - if (ui->chkSetDefaultFilter->isChecked()) { - dlg.setNameFilters(m_nameFilters, m_nameFilters.last()); - } - else { - dlg.setNameFilters(m_nameFilters); - } - } - else { - if (ui->chkSetDefaultFilter->isChecked()) { - dlg.setMimeTypeFilters(m_mimeFilter, m_mimeFilter[4]); - } - else { - dlg.setMimeTypeFilters(m_mimeFilter); - } - - } - - if (ui->chkHideNameFilterDetailsOption->isChecked()) { - dlg.setHideNameFilterDetailsOption(); - } - - QString url = dlg.filename(); - ui->listResults->addItem(url); - ui->lblMime->setText(dlg.selectedMimeType()); - ui->txtFilter->setText(dlg.selectedNameFilter()); - -} - -void KoFileDialogTester::testOpenFiles() -{ - ui->listResults->clear(); - KoFileDialog dlg(this, KoFileDialog::OpenFiles, ui->txtUniqueKey->text()); - dlg.setCaption(i18n("Testing: OpenFile")); - dlg.setDefaultDir(QDesktopServices::storageLocation(QDesktopServices::HomeLocation)); - if (ui->radioName->isChecked()) { - - if (ui->chkSetDefaultFilter->isChecked()) { - dlg.setNameFilters(m_nameFilters, m_nameFilters.last()); - } - else { - dlg.setNameFilters(m_nameFilters); - } - - } - else { - if (ui->chkSetDefaultFilter->isChecked()) { - dlg.setMimeTypeFilters(m_mimeFilter, m_mimeFilter[4]); - } - else { - dlg.setMimeTypeFilters(m_mimeFilter); - } - - } - - if (ui->chkHideNameFilterDetailsOption->isChecked()) { - dlg.setHideNameFilterDetailsOption(); - } - - QStringList urls = dlg.filenames(); - foreach(const QString &url, urls) { - ui->listResults->addItem(url); - } - ui->lblMime->setText(dlg.selectedMimeType()); - ui->txtFilter->setText(dlg.selectedNameFilter()); -} - -void KoFileDialogTester::testOpenDirectory() -{ - ui->listResults->clear(); - KoFileDialog dlg(this, KoFileDialog::OpenDirectory, ui->txtUniqueKey->text()); - dlg.setCaption(i18n("Testing: OpenDirectory")); - dlg.setDefaultDir(QDesktopServices::storageLocation(QDesktopServices::HomeLocation)); - if (ui->radioName->isChecked()) { - - if (ui->chkSetDefaultFilter->isChecked()) { - dlg.setNameFilters(m_nameFilters, m_nameFilters.last()); - } - else { - dlg.setNameFilters(m_nameFilters); - } - - } - else { - if (ui->chkSetDefaultFilter->isChecked()) { - dlg.setMimeTypeFilters(m_mimeFilter, m_mimeFilter[4]); - } - else { - dlg.setMimeTypeFilters(m_mimeFilter); - } - - } - - if (ui->chkHideNameFilterDetailsOption->isChecked()) { - dlg.setHideNameFilterDetailsOption(); - } - - QString url = dlg.filename(); - ui->listResults->addItem(url); - ui->lblMime->setText(dlg.selectedMimeType()); - ui->txtFilter->setText(dlg.selectedNameFilter()); -} - - -void KoFileDialogTester::testImportFile() -{ - ui->listResults->clear(); - - KoFileDialog dlg(this, KoFileDialog::ImportFile, ui->txtUniqueKey->text()); - dlg.setCaption(i18n("Testing: ImportFile")); - dlg.setDefaultDir(QDesktopServices::storageLocation(QDesktopServices::HomeLocation)); - if (ui->radioName->isChecked()) { - - if (ui->chkSetDefaultFilter->isChecked()) { - dlg.setNameFilters(m_nameFilters, m_nameFilters.last()); - } - else { - dlg.setNameFilters(m_nameFilters); - } - - } - else { - - if (ui->chkSetDefaultFilter->isChecked()) { - dlg.setMimeTypeFilters(m_mimeFilter, m_mimeFilter[4]); - } - else { - dlg.setMimeTypeFilters(m_mimeFilter); - } - - } - - if (ui->chkHideNameFilterDetailsOption->isChecked()) { - dlg.setHideNameFilterDetailsOption(); - } - - QString url = dlg.filename(); - ui->listResults->addItem(url); - ui->lblMime->setText(dlg.selectedMimeType()); - ui->txtFilter->setText(dlg.selectedNameFilter()); -} - -void KoFileDialogTester::testImportFiles() -{ - ui->listResults->clear(); - KoFileDialog dlg(this, KoFileDialog::ImportFiles, ui->txtUniqueKey->text()); - dlg.setCaption(i18n("Testing: ImportFiles")); - dlg.setDefaultDir(QDesktopServices::storageLocation(QDesktopServices::HomeLocation)); - if (ui->radioName->isChecked()) { - - if (ui->chkSetDefaultFilter->isChecked()) { - dlg.setNameFilters(m_nameFilters, m_nameFilters.last()); - } - else { - dlg.setNameFilters(m_nameFilters); - } - - } - else { - - if (ui->chkSetDefaultFilter->isChecked()) { - dlg.setMimeTypeFilters(m_mimeFilter, m_mimeFilter[4]); - } - else { - dlg.setMimeTypeFilters(m_mimeFilter); - } - - } - - if (ui->chkHideNameFilterDetailsOption->isChecked()) { - dlg.setHideNameFilterDetailsOption(); - } - - QStringList urls = dlg.filenames(); foreach(const QString &url, urls) { - ui->listResults->addItem(url); - } - ui->lblMime->setText(dlg.selectedMimeType()); - ui->txtFilter->setText(dlg.selectedNameFilter()); -} - -void KoFileDialogTester::testImportDirectory() -{ - ui->listResults->clear(); - KoFileDialog dlg(this, KoFileDialog::ImportDirectory, ui->txtUniqueKey->text()); - dlg.setCaption(i18n("Testing: Import Directory")); - dlg.setDefaultDir(QDesktopServices::storageLocation(QDesktopServices::HomeLocation)); - if (ui->radioName->isChecked()) { - - if (ui->chkSetDefaultFilter->isChecked()) { - dlg.setNameFilters(m_nameFilters, m_nameFilters.last()); - } - else { - dlg.setNameFilters(m_nameFilters); - } - - } - else { - if (ui->chkSetDefaultFilter->isChecked()) { - dlg.setMimeTypeFilters(m_mimeFilter, m_mimeFilter[4]); - } - else { - dlg.setMimeTypeFilters(m_mimeFilter); - } - - } - - if (ui->chkHideNameFilterDetailsOption->isChecked()) { - dlg.setHideNameFilterDetailsOption(); - } - - QString url = dlg.filename(); - ui->listResults->addItem(url); - ui->lblMime->setText(dlg.selectedMimeType()); - ui->txtFilter->setText(dlg.selectedNameFilter()); -} - -void KoFileDialogTester::testSaveFile() -{ - ui->listResults->clear(); - - KoFileDialog dlg(this, KoFileDialog::SaveFile, ui->txtUniqueKey->text()); - dlg.setCaption(i18n("Testing: SaveFile")); - dlg.setDefaultDir(QDesktopServices::storageLocation(QDesktopServices::HomeLocation)); - if (ui->radioName->isChecked()) { - if (ui->chkSetDefaultFilter->isChecked()) { - dlg.setNameFilters(m_nameFilters, m_nameFilters.last()); - } - else { - dlg.setNameFilters(m_nameFilters); - } - } - else { - - if (ui->chkSetDefaultFilter->isChecked()) { - dlg.setMimeTypeFilters(m_mimeFilter, m_mimeFilter[4]); - } - else { - dlg.setMimeTypeFilters(m_mimeFilter); - } - - } - - if (ui->chkHideNameFilterDetailsOption->isChecked()) { - dlg.setHideNameFilterDetailsOption(); - } - - QString url = dlg.filename(); - ui->listResults->addItem(url); - ui->lblMime->setText(dlg.selectedMimeType()); - ui->txtFilter->setText(dlg.selectedNameFilter()); -} Index: libs/widgets/KoFileDialogTester.ui =================================================================== --- libs/widgets/KoFileDialogTester.ui +++ /dev/null @@ -1,214 +0,0 @@ - - - KoFileDialogTester - - - - 0 - 0 - 355 - 396 - - - - - - - - - - - - - - Qt::Vertical - - - - 20 - 40 - - - - - - - - Open File - - - - - - - Open Files - - - - - - - - - Unique key: - - - - - - - blablabla - - - - - - - - - - - - 0 - 0 - - - - Filter: - - - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop - - - - - - - - 0 - 0 - - - - TextLabel - - - Qt::PlainText - - - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop - - - false - - - - - - - - - - - Results: - - - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop - - - - - - - - - - - - Filter Type - - - - - - Mime - - - - - - - Named - - - true - - - - - - - hide name filter details option - - - - - - - Set default filter - - - - - - - - - - Save File - - - - - - - Import Files - - - - - - - Import File - - - - - - - Open Directory - - - - - - - Import Directory - - - - - bnImportFile - bnOpenFile - bnOpenFiles - bnOpenDirectory - bnImportFiles - bnImportDirectory - bnSaveFile - groupBox - lblMime - - - - - Index: libs/widgets/main.cpp =================================================================== --- libs/widgets/main.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include "KoFileDialogTester.h" -#include - -int main(int argc, char *argv[]) -{ - QApplication a(argc, argv); - KoFileDialogTester w; - w.show(); - - return a.exec(); -} Index: libs/widgetutils/CMakeLists.txt =================================================================== --- libs/widgetutils/CMakeLists.txt +++ libs/widgetutils/CMakeLists.txt @@ -7,6 +7,7 @@ KoUpdater.cpp KoUpdaterPrivate_p.cpp KoProperties.cpp + KoFileDialog.cpp ) @@ -30,6 +31,7 @@ KoProgressUpdater.h KoUpdater.h KoProperties.h + KoFileDialog.h ${CMAKE_CURRENT_BINARY_DIR}/kowidgetutils_export.h DESTINATION ${INCLUDE_INSTALL_DIR}/calligra COMPONENT Devel Index: libs/widgetutils/KoFileDialog.h =================================================================== --- /dev/null +++ libs/widgetutils/KoFileDialog.h @@ -0,0 +1,155 @@ +/* This file is part of the KDE project + Copyright (C) 2013 - 2014 Yue Liu + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. +*/ + +#ifndef KOFILEDIALOG_H +#define KOFILEDIALOG_H + +#include "kowidgetutils_export.h" + +#include +#include +#include +#include +#include + + +/** + * Wrapper around QFileDialog providing native file dialogs + * on KDE/Gnome/Windows/OSX/etc. + */ +class KOWIDGETUTILS_EXPORT KoFileDialog : public QObject +{ + Q_OBJECT + +public: + enum DialogType { + OpenFile, + OpenFiles, + OpenDirectory, + ImportFile, + ImportFiles, + ImportDirectory, + SaveFile + }; + + /** + * @brief constructor + * @param parent The parent of the file dialog + * @param dialogType usage of the file dialog + * @param dialogName the name for the file dialog. This will be used to open + * the filedialog in the last open location, instead the specified directory. + * + * @return The name of the entry user selected in the file dialog + * + */ + KoFileDialog(QWidget *parent, + KoFileDialog::DialogType type, + const QString &dialogName); + + ~KoFileDialog(); + + void setCaption(const QString &caption); + + /** + * @brief setDefaultDir set the default directory to defaultDir + * + * @param defaultDir a path to a file or directory + */ + void setDefaultDir(const QString &defaultDir, bool override = false); + + /** + * @brief setOverrideDir override both the default dir and the saved dir found by dialogName + * @param overrideDir a path to a file or directory + */ + void setOverrideDir(const QString &overrideDir); + + /** + * @brief setImageFilters sets the name filters for the file dialog to all + * image formats Qt's QImageReader supports. + */ + void setImageFilters(); + + void setNameFilter(const QString &filter); + + /** + * @brief setNameFilters set a list of description/extension pairs. + * + * These are not registered mimetypes. In contrast with Qt's filedialog namefilters, + * you can only have _one_ pair per line. I.e. + * + * Gif Image (*gif) + * Tiff Image (*tif) + * + * And NOT Images (*gif *tif) + * + * @param filterList + * @param defaultFilter + */ + void setNameFilters(const QStringList &filterList, + QString defaultFilter = QString()); + void setMimeTypeFilters(const QStringList &filterList, + QString defaultFilter = QString()); + void setHideNameFilterDetailsOption(); + + QStringList nameFilters() const; + + QStringList filenames(); + QString filename(); + + /** + * @brief selectedNameFilter returns the name filter the user selected, either + * directory or by clicking on it. + * @return + */ + QString selectedNameFilter() const; + + QString selectedMimeType() const; + +private Q_SLOTS: + + void filterSelected(const QString &filter); + +private: + /** + * @brief splitNameFilter take a single line of a QDialog name filter and split it + * into several lines. This is needed because a single line name filter can contain + * more than one mimetype, making it impossible to figure out the correct extension. + * + * The methods takes care of some duplicated extensions, like jpeg and jpg. + * @param nameFilter the namefilter to be split + * @param mimeList a pointer to the list with mimes that shouldn't be added. + * @return a stringlist of all name filters. + */ + static QStringList splitNameFilter(const QString &nameFilter, QStringList *mimeList); + + void createFileDialog(); + + QString getUsedDir(const QString &dialogName); + void saveUsedDir(const QString &fileName, const QString &dialogName); + + const QStringList getFilterStringListFromMime(const QStringList &mimeList, + bool withAllSupportedEntry = false); + + + + class Private; + Private * const d; +}; + +#endif /* KOFILEDIALOG_H */ Index: libs/widgetutils/KoFileDialog.cpp =================================================================== --- /dev/null +++ libs/widgetutils/KoFileDialog.cpp @@ -0,0 +1,502 @@ +/* This file is part of the KDE project + Copyright (C) 2013 - 2014 Yue Liu + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. +*/ + +#include "KoFileDialog.h" +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +class Q_DECL_HIDDEN KoFileDialog::Private +{ +public: + Private(QWidget *parent_, + KoFileDialog::DialogType dialogType_, + const QString caption_, + const QString defaultDir_, + const QString dialogName_) + : parent(parent_) + , type(dialogType_) + , dialogName(dialogName_) + , caption(caption_) + , defaultDirectory(defaultDir_) + , filterList(QStringList()) + , defaultFilter(QString()) + , useStaticForNative(false) + , hideDetails(false) + , swapExtensionOrder(false) + { + // Force the native file dialogs on Windows. Except for KDE, the native file dialogs are only possible + // using the static methods. The Qt documentation is wrong here, if it means what it says " By default, + // the native file dialog is used unless you use a subclass of QFileDialog that contains the Q_OBJECT + // macro." +#ifdef Q_OS_WIN + useStaticForNative = true; +#endif + // Non-static KDE file is broken when called with QFileDialog::AcceptSave: + // then the directory above defaultdir is opened, and defaultdir is given as the default file name... + // + // So: in X11, use static methods inside KDE, which give working native dialogs, but non-static outside + // KDE, which gives working Qt dialogs. + // + // Only show the GTK dialog in Gnome, where people deserve it +#ifdef HAVE_X11 + if (qgetenv("KDE_FULL_SESSION").size() > 0) { + useStaticForNative = true; + } + if (qgetenv("XDG_CURRENT_DESKTOP") == "GNOME") { + useStaticForNative = true; + QClipboard *cb = QApplication::clipboard(); + cb->blockSignals(true); + swapExtensionOrder = true; + } + +#endif + } + + ~Private() + { + if (qgetenv("XDG_CURRENT_DESKTOP") == "GNOME") { + useStaticForNative = true; + QClipboard *cb = QApplication::clipboard(); + cb->blockSignals(false); + } + } + + QWidget *parent; + KoFileDialog::DialogType type; + QString dialogName; + QString caption; + QString defaultDirectory; + QStringList filterList; + QString defaultFilter; + QScopedPointer fileDialog; + QMimeType mimeType; + bool useStaticForNative; + bool hideDetails; + bool swapExtensionOrder; +}; + +KoFileDialog::KoFileDialog(QWidget *parent, + KoFileDialog::DialogType type, + const QString &dialogName) + : d(new Private(parent, type, "", getUsedDir(dialogName), dialogName)) +{ +} + +KoFileDialog::~KoFileDialog() +{ + delete d; +} + +void KoFileDialog::setCaption(const QString &caption) +{ + d->caption = caption; +} + +void KoFileDialog::setDefaultDir(const QString &defaultDir, bool override) +{ + if (override || d->defaultDirectory.isEmpty() || !QFile(d->defaultDirectory).exists()) { + QFileInfo f(defaultDir); + d->defaultDirectory = f.absoluteFilePath(); + } +} + +void KoFileDialog::setOverrideDir(const QString &overrideDir) +{ + d->defaultDirectory = overrideDir; +} + +void KoFileDialog::setImageFilters() +{ + QStringList imageFilters; + // add filters for all formats supported by QImage + foreach(const QByteArray &format, QImageReader::supportedImageFormats()) { + imageFilters << QLatin1String("image/") + format; + } + setMimeTypeFilters(imageFilters); +} + +void KoFileDialog::setNameFilter(const QString &filter) +{ + d->filterList.clear(); + if (d->type == KoFileDialog::SaveFile) { + QStringList mimeList; + d->filterList << splitNameFilter(filter, &mimeList); + d->defaultFilter = d->filterList.first(); + } + else { + d->filterList << filter; + } +} + +void KoFileDialog::setNameFilters(const QStringList &filterList, + QString defaultFilter) +{ + d->filterList.clear(); + + if (d->type == KoFileDialog::SaveFile) { + QStringList mimeList; + foreach(const QString &filter, filterList) { + d->filterList << splitNameFilter(filter, &mimeList); + } + + if (!defaultFilter.isEmpty()) { + mimeList.clear(); + QStringList defaultFilters = splitNameFilter(defaultFilter, &mimeList); + if (defaultFilters.size() > 0) { + defaultFilter = defaultFilters.first(); + } + } + } + else { + d->filterList = filterList; + } + d->defaultFilter = defaultFilter; + +} + +void KoFileDialog::setMimeTypeFilters(const QStringList &filterList, + QString defaultFilter) +{ + d->filterList = getFilterStringListFromMime(filterList, true); + + if (!defaultFilter.isEmpty()) { + QStringList defaultFilters = getFilterStringListFromMime(QStringList() << defaultFilter, false); + if (defaultFilters.size() > 0) { + defaultFilter = defaultFilters.first(); + } + } + d->defaultFilter = defaultFilter; +} + +void KoFileDialog::setHideNameFilterDetailsOption() +{ + d->hideDetails = true; +} + +QStringList KoFileDialog::nameFilters() const +{ + return d->filterList; +} + +QString KoFileDialog::selectedNameFilter() const +{ + if (!d->useStaticForNative) { + return d->fileDialog->selectedNameFilter(); + } + else { + return d->defaultFilter; + } +} + +QString KoFileDialog::selectedMimeType() const +{ + if (d->mimeType.isValid()) { + return d->mimeType.name(); + } + else { + return ""; + } +} + +void KoFileDialog::createFileDialog() +{ + d->fileDialog.reset(new QFileDialog(d->parent, d->caption, d->defaultDirectory)); + + if (d->type == SaveFile) { + d->fileDialog->setAcceptMode(QFileDialog::AcceptSave); + d->fileDialog->setFileMode(QFileDialog::AnyFile); + } + else { // open / import + + d->fileDialog->setAcceptMode(QFileDialog::AcceptOpen); + + if (d->type == ImportDirectory + || d->type == OpenDirectory) + { + d->fileDialog->setFileMode(QFileDialog::Directory); + d->fileDialog->setOption(QFileDialog::ShowDirsOnly, true); + } + else { // open / import file(s) + if (d->type == OpenFile + || d->type == ImportFile) + { + d->fileDialog->setFileMode(QFileDialog::ExistingFile); + } + else { // files + d->fileDialog->setFileMode(QFileDialog::ExistingFiles); + } + } + } + + d->fileDialog->setNameFilters(d->filterList); + if (!d->defaultFilter.isEmpty()) { + d->fileDialog->selectNameFilter(d->defaultFilter); + } + + if (d->type == ImportDirectory || + d->type == ImportFile || d->type == ImportFiles || + d->type == SaveFile) { + d->fileDialog->setWindowModality(Qt::WindowModal); + } + + if (d->hideDetails) { + d->fileDialog->setOption(QFileDialog::HideNameFilterDetails); + } + + connect(d->fileDialog.data(), SIGNAL(filterSelected(QString)), this, SLOT(filterSelected(QString))); +} + +QString KoFileDialog::filename() +{ + QString url; + if (!d->useStaticForNative) { + + if (!d->fileDialog) { + createFileDialog(); + } + + if (d->fileDialog->exec() == QDialog::Accepted) { + url = d->fileDialog->selectedFiles().first(); + } + } + else { + switch (d->type) { + case OpenFile: + { + url = QFileDialog::getOpenFileName(d->parent, + d->caption, + d->defaultDirectory, + d->filterList.join(";;"), + &d->defaultFilter); + break; + } + case OpenDirectory: + { + url = QFileDialog::getExistingDirectory(d->parent, + d->caption, + d->defaultDirectory, + QFileDialog::ShowDirsOnly); + break; + } + case ImportFile: + { + url = QFileDialog::getOpenFileName(d->parent, + d->caption, + d->defaultDirectory, + d->filterList.join(";;"), + &d->defaultFilter); + break; + } + case ImportDirectory: + { + url = QFileDialog::getExistingDirectory(d->parent, + d->caption, + d->defaultDirectory, + QFileDialog::ShowDirsOnly); + break; + } + case SaveFile: + { + url = QFileDialog::getSaveFileName(d->parent, + d->caption, + d->defaultDirectory, + d->filterList.join(";;"), + &d->defaultFilter); + break; + } + default: + ; + } + } + + if (!url.isEmpty()) { + + if (d->type == SaveFile && QFileInfo(url).suffix().isEmpty()) { + int start = d->defaultFilter.lastIndexOf("*.") + 1; + int end = d->defaultFilter.lastIndexOf(" )"); + int n = end - start; + QString extension = d->defaultFilter.mid(start, n); + url.append(extension); + } + + QMimeDatabase db; + d->mimeType = db.mimeTypeForFile(url); + saveUsedDir(url, d->dialogName); + } + return url; +} + +QStringList KoFileDialog::filenames() +{ + QStringList urls; + + if (!d->useStaticForNative) { + if (!d->fileDialog) { + createFileDialog(); + } + if (d->fileDialog->exec() == QDialog::Accepted) { + urls = d->fileDialog->selectedFiles(); + } + } + else { + switch (d->type) { + case OpenFiles: + case ImportFiles: + { + urls = QFileDialog::getOpenFileNames(d->parent, + d->caption, + d->defaultDirectory, + d->filterList.join(";;"), + &d->defaultFilter); + break; + } + default: + ; + } + } + if (urls.size() > 0) { + saveUsedDir(urls.first(), d->dialogName); + } + return urls; +} + +void KoFileDialog::filterSelected(const QString &filter) +{ + // "Windows BMP image ( *.bmp )"; + int start = filter.lastIndexOf("*.") + 2; + int end = filter.lastIndexOf(" )"); + int n = end - start; + QString extension = filter.mid(start, n); + d->defaultFilter = filter; + d->fileDialog->setDefaultSuffix(extension); +} + +QStringList KoFileDialog::splitNameFilter(const QString &nameFilter, QStringList *mimeList) +{ + Q_ASSERT(mimeList); + + QStringList filters; + QString description; + + if (nameFilter.contains("(")) { + description = nameFilter.left(nameFilter.indexOf("(") -1).trimmed(); + } + + QStringList entries = nameFilter.mid(nameFilter.indexOf("(") + 1).split(" ",QString::SkipEmptyParts ); + + foreach(QString entry, entries) { + + entry = entry.remove("*"); + entry = entry.remove(")"); + + QMimeDatabase db; + QMimeType mime = db.mimeTypeForName("bla" + entry); + if (mime.name() != "application/octet-stream") { + if (!mimeList->contains(mime.name())) { + mimeList->append(mime.name()); + filters.append(mime.comment() + " ( *" + entry + " )"); + } + } + else { + filters.append(entry.remove(".").toUpper() + " " + description + " ( *." + entry + " )"); + } + } + + return filters; +} + +const QStringList KoFileDialog::getFilterStringListFromMime(const QStringList &mimeList, + bool withAllSupportedEntry) +{ + QStringList mimeSeen; + + QStringList ret; + if (withAllSupportedEntry) { + ret << QString(); + } + + for (QStringList::ConstIterator + it = mimeList.begin(); it != mimeList.end(); ++it) { + QMimeDatabase db; + QMimeType mimeType = db.mimeTypeForName(*it); + if (!mimeType.isValid()) { + continue; + } + if (!mimeSeen.contains(mimeType.name())) { + QString oneFilter; + QStringList patterns = mimeType.globPatterns(); + QStringList::ConstIterator jt; + for (jt = patterns.constBegin(); jt != patterns.constEnd(); ++jt) { + if (d->swapExtensionOrder) { + oneFilter.prepend(*jt + " "); + if (withAllSupportedEntry) { + ret[0].prepend(*jt + " "); + } + } + else { + oneFilter.append(*jt + " "); + if (withAllSupportedEntry) { + ret[0].append(*jt + " "); + } + } + } + oneFilter = mimeType.comment() + " ( " + oneFilter + ")"; + ret << oneFilter; + mimeSeen << mimeType.name(); + } + } + + if (withAllSupportedEntry) { + ret[0] = i18n("All supported formats") + " ( " + ret[0] + (")"); + } + return ret; +} + +QString KoFileDialog::getUsedDir(const QString &dialogName) +{ + if (dialogName.isEmpty()) return ""; + + KConfigGroup group = KSharedConfig::openConfig()->group("File Dialogs"); + QString dir = group.readEntry(dialogName); + + return dir; +} + +void KoFileDialog::saveUsedDir(const QString &fileName, + const QString &dialogName) +{ + + if (dialogName.isEmpty()) return; + + QFileInfo fileInfo(fileName); + KConfigGroup group = KSharedConfig::openConfig()->group("File Dialogs"); + group.writeEntry(dialogName, fileInfo.absolutePath()); + +} Index: libs/widgetutils/tests/CMakeLists.txt =================================================================== --- libs/widgetutils/tests/CMakeLists.txt +++ libs/widgetutils/tests/CMakeLists.txt @@ -5,5 +5,11 @@ set(kopropertiestest_SRCS KoPropertiesTest.cpp ) kde4_add_unit_test(KoPropertiesTest TESTNAME libs-widgetutils-KoPropertiesTest ${kopropertiestest_SRCS}) -target_link_libraries(KoPropertiesTest kowidgetutils KF5::I18n Qt5::Test) +target_link_libraries(KoPropertiesTest kowidgetutils Qt5::Test) +########### next target ############### + +set(kofiledialogtester_SRCS KoFileDialogTester.cpp) +qt5_wrap_ui(kofiledialogtester_SRCS KoFileDialogTester.ui) +add_executable(kofiledialogtester ${kofiledialogtester_SRCS}) +target_link_libraries(kofiledialogtester kowidgetutils) Index: libs/widgetutils/tests/KoFileDialogTester.h =================================================================== --- /dev/null +++ libs/widgetutils/tests/KoFileDialogTester.h @@ -0,0 +1,53 @@ +/* This file is part of the KDE project + * Copyright (C) Boudewijn Rempt , (C) 2014 + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ +#ifndef KOFILEDIALOGTESTER_H +#define KOFILEDIALOGTESTER_H + +#include + +namespace Ui { +class KoFileDialogTester; +} + +class KoFileDialogTester : public QWidget +{ + Q_OBJECT + +public: + explicit KoFileDialogTester(QWidget *parent = 0); + ~KoFileDialogTester(); + +private Q_SLOTS: + + void testOpenFile(); + void testOpenFiles(); + void testOpenDirectory(); + void testImportFile(); + void testImportFiles(); + void testImportDirectory(); + void testSaveFile(); + +private: + Ui::KoFileDialogTester *ui; + + QStringList m_nameFilters; + QStringList m_mimeFilter; +}; + +#endif // KOFILEDIALOGTESTER_H Index: libs/widgetutils/tests/KoFileDialogTester.cpp =================================================================== --- /dev/null +++ libs/widgetutils/tests/KoFileDialogTester.cpp @@ -0,0 +1,338 @@ +/* This file is part of the KDE project + * Copyright (C) Boudewijn Rempt , (C) 2014 + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "KoFileDialogTester.h" +#include "ui_KoFileDialogTester.h" + +#include +#include +#include +#include +#include + +#include + +KoFileDialogTester::KoFileDialogTester(QWidget *parent) : + QWidget(parent), + ui(new Ui::KoFileDialogTester) +{ + ui->setupUi(this); + + connect(ui->bnOpenFile, SIGNAL(clicked()), SLOT(testOpenFile())); + connect(ui->bnOpenFiles, SIGNAL(clicked()), SLOT(testOpenFiles())); + connect(ui->bnOpenDirectory, SIGNAL(clicked()), SLOT(testOpenDirectory())); + connect(ui->bnImportFile, SIGNAL(clicked()), SLOT(testImportFile())); + connect(ui->bnImportFiles, SIGNAL(clicked()), SLOT(testImportFiles())); + connect(ui->bnImportDirectory, SIGNAL(clicked()), SLOT(testImportDirectory())); + connect(ui->bnSaveFile, SIGNAL(clicked()), SLOT(testSaveFile())); + + m_nameFilters << "Documents (*.odt *.doc *.txt)" + << "Images (*.png *.jpg *.jpeg)" + << "Presentations (*.ppt *.odp)" + << "Patterns (*.pat *.jpg *.gif *.png *.tif *.xpm *.bmp)" + << "Palettes (*.gpl *.pal *.act *.aco *.colors)"; + + m_mimeFilter = QStringList() + << "application/x-krita" << "image/x-exr" << "image/openraster" << "image/x-tga" << "image/vnd.adobe.photoshop" + << "image/x-xcf" << "image/x-portable-pixmap" << "image/x-portable-graymap" + << "image/x-portable-bitmap" << "image/png" << "image/jp2" + << "image/tiff" << "application/vnd.oasis.opendocument.graphics" + << "application/pdf" << "image/jpeg" << "image/bmp" << "image/x-xpixmap" + << "image/gif" << "image/x-xbitmap" << "application/x-krita-flipbook" + << "image/x-adobe-dng" << "image/x-xfig" << "image/svg+xml" << "image/svg+xml-compressed" + << "image/x-eps" << "image/eps" << "application/eps" << "application/x-eps" << "application/postscript" + << "image/x-wmf" << "application/x-karbon" + << "image/tiff" << "application/vnd.oasis.opendocument.graphics" + ; +} + +KoFileDialogTester::~KoFileDialogTester() +{ + delete ui; +} + + + +void KoFileDialogTester::testOpenFile() +{ + ui->listResults->clear(); + KoFileDialog dlg(this, KoFileDialog::OpenFile, ui->txtUniqueKey->text()); + dlg.setCaption("Testing: OpenFile"); + dlg.setDefaultDir(QDesktopServices::storageLocation(QDesktopServices::HomeLocation)); + if (ui->radioName->isChecked()) { + + if (ui->chkSetDefaultFilter->isChecked()) { + dlg.setNameFilters(m_nameFilters, m_nameFilters.last()); + } + else { + dlg.setNameFilters(m_nameFilters); + } + } + else { + if (ui->chkSetDefaultFilter->isChecked()) { + dlg.setMimeTypeFilters(m_mimeFilter, m_mimeFilter[4]); + } + else { + dlg.setMimeTypeFilters(m_mimeFilter); + } + + } + + if (ui->chkHideNameFilterDetailsOption->isChecked()) { + dlg.setHideNameFilterDetailsOption(); + } + + QString url = dlg.filename(); + ui->listResults->addItem(url); + ui->lblMime->setText(dlg.selectedMimeType()); + ui->txtFilter->setText(dlg.selectedNameFilter()); + +} + +void KoFileDialogTester::testOpenFiles() +{ + ui->listResults->clear(); + KoFileDialog dlg(this, KoFileDialog::OpenFiles, ui->txtUniqueKey->text()); + dlg.setCaption("Testing: OpenFile"); + dlg.setDefaultDir(QDesktopServices::storageLocation(QDesktopServices::HomeLocation)); + if (ui->radioName->isChecked()) { + + if (ui->chkSetDefaultFilter->isChecked()) { + dlg.setNameFilters(m_nameFilters, m_nameFilters.last()); + } + else { + dlg.setNameFilters(m_nameFilters); + } + + } + else { + if (ui->chkSetDefaultFilter->isChecked()) { + dlg.setMimeTypeFilters(m_mimeFilter, m_mimeFilter[4]); + } + else { + dlg.setMimeTypeFilters(m_mimeFilter); + } + + } + + if (ui->chkHideNameFilterDetailsOption->isChecked()) { + dlg.setHideNameFilterDetailsOption(); + } + + QStringList urls = dlg.filenames(); + foreach(const QString &url, urls) { + ui->listResults->addItem(url); + } + ui->lblMime->setText(dlg.selectedMimeType()); + ui->txtFilter->setText(dlg.selectedNameFilter()); +} + +void KoFileDialogTester::testOpenDirectory() +{ + ui->listResults->clear(); + KoFileDialog dlg(this, KoFileDialog::OpenDirectory, ui->txtUniqueKey->text()); + dlg.setCaption("Testing: OpenDirectory"); + dlg.setDefaultDir(QDesktopServices::storageLocation(QDesktopServices::HomeLocation)); + if (ui->radioName->isChecked()) { + + if (ui->chkSetDefaultFilter->isChecked()) { + dlg.setNameFilters(m_nameFilters, m_nameFilters.last()); + } + else { + dlg.setNameFilters(m_nameFilters); + } + + } + else { + if (ui->chkSetDefaultFilter->isChecked()) { + dlg.setMimeTypeFilters(m_mimeFilter, m_mimeFilter[4]); + } + else { + dlg.setMimeTypeFilters(m_mimeFilter); + } + + } + + if (ui->chkHideNameFilterDetailsOption->isChecked()) { + dlg.setHideNameFilterDetailsOption(); + } + + QString url = dlg.filename(); + ui->listResults->addItem(url); + ui->lblMime->setText(dlg.selectedMimeType()); + ui->txtFilter->setText(dlg.selectedNameFilter()); +} + + +void KoFileDialogTester::testImportFile() +{ + ui->listResults->clear(); + + KoFileDialog dlg(this, KoFileDialog::ImportFile, ui->txtUniqueKey->text()); + dlg.setCaption("Testing: ImportFile"); + dlg.setDefaultDir(QDesktopServices::storageLocation(QDesktopServices::HomeLocation)); + if (ui->radioName->isChecked()) { + + if (ui->chkSetDefaultFilter->isChecked()) { + dlg.setNameFilters(m_nameFilters, m_nameFilters.last()); + } + else { + dlg.setNameFilters(m_nameFilters); + } + + } + else { + + if (ui->chkSetDefaultFilter->isChecked()) { + dlg.setMimeTypeFilters(m_mimeFilter, m_mimeFilter[4]); + } + else { + dlg.setMimeTypeFilters(m_mimeFilter); + } + + } + + if (ui->chkHideNameFilterDetailsOption->isChecked()) { + dlg.setHideNameFilterDetailsOption(); + } + + QString url = dlg.filename(); + ui->listResults->addItem(url); + ui->lblMime->setText(dlg.selectedMimeType()); + ui->txtFilter->setText(dlg.selectedNameFilter()); +} + +void KoFileDialogTester::testImportFiles() +{ + ui->listResults->clear(); + KoFileDialog dlg(this, KoFileDialog::ImportFiles, ui->txtUniqueKey->text()); + dlg.setCaption("Testing: ImportFiles"); + dlg.setDefaultDir(QDesktopServices::storageLocation(QDesktopServices::HomeLocation)); + if (ui->radioName->isChecked()) { + + if (ui->chkSetDefaultFilter->isChecked()) { + dlg.setNameFilters(m_nameFilters, m_nameFilters.last()); + } + else { + dlg.setNameFilters(m_nameFilters); + } + + } + else { + + if (ui->chkSetDefaultFilter->isChecked()) { + dlg.setMimeTypeFilters(m_mimeFilter, m_mimeFilter[4]); + } + else { + dlg.setMimeTypeFilters(m_mimeFilter); + } + + } + + if (ui->chkHideNameFilterDetailsOption->isChecked()) { + dlg.setHideNameFilterDetailsOption(); + } + + QStringList urls = dlg.filenames(); foreach(const QString &url, urls) { + ui->listResults->addItem(url); + } + ui->lblMime->setText(dlg.selectedMimeType()); + ui->txtFilter->setText(dlg.selectedNameFilter()); +} + +void KoFileDialogTester::testImportDirectory() +{ + ui->listResults->clear(); + KoFileDialog dlg(this, KoFileDialog::ImportDirectory, ui->txtUniqueKey->text()); + dlg.setCaption("Testing: Import Directory"); + dlg.setDefaultDir(QDesktopServices::storageLocation(QDesktopServices::HomeLocation)); + if (ui->radioName->isChecked()) { + + if (ui->chkSetDefaultFilter->isChecked()) { + dlg.setNameFilters(m_nameFilters, m_nameFilters.last()); + } + else { + dlg.setNameFilters(m_nameFilters); + } + + } + else { + if (ui->chkSetDefaultFilter->isChecked()) { + dlg.setMimeTypeFilters(m_mimeFilter, m_mimeFilter[4]); + } + else { + dlg.setMimeTypeFilters(m_mimeFilter); + } + + } + + if (ui->chkHideNameFilterDetailsOption->isChecked()) { + dlg.setHideNameFilterDetailsOption(); + } + + QString url = dlg.filename(); + ui->listResults->addItem(url); + ui->lblMime->setText(dlg.selectedMimeType()); + ui->txtFilter->setText(dlg.selectedNameFilter()); +} + +void KoFileDialogTester::testSaveFile() +{ + ui->listResults->clear(); + + KoFileDialog dlg(this, KoFileDialog::SaveFile, ui->txtUniqueKey->text()); + dlg.setCaption("Testing: SaveFile"); + dlg.setDefaultDir(QDesktopServices::storageLocation(QDesktopServices::HomeLocation)); + if (ui->radioName->isChecked()) { + if (ui->chkSetDefaultFilter->isChecked()) { + dlg.setNameFilters(m_nameFilters, m_nameFilters.last()); + } + else { + dlg.setNameFilters(m_nameFilters); + } + } + else { + + if (ui->chkSetDefaultFilter->isChecked()) { + dlg.setMimeTypeFilters(m_mimeFilter, m_mimeFilter[4]); + } + else { + dlg.setMimeTypeFilters(m_mimeFilter); + } + + } + + if (ui->chkHideNameFilterDetailsOption->isChecked()) { + dlg.setHideNameFilterDetailsOption(); + } + + QString url = dlg.filename(); + ui->listResults->addItem(url); + ui->lblMime->setText(dlg.selectedMimeType()); + ui->txtFilter->setText(dlg.selectedNameFilter()); +} + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + KoFileDialogTester w; + w.show(); + + return a.exec(); +} Index: libs/widgetutils/tests/KoFileDialogTester.ui =================================================================== --- /dev/null +++ libs/widgetutils/tests/KoFileDialogTester.ui @@ -0,0 +1,214 @@ + + + KoFileDialogTester + + + + 0 + 0 + 355 + 396 + + + + + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Open File + + + + + + + Open Files + + + + + + + + + Unique key: + + + + + + + blablabla + + + + + + + + + + + + 0 + 0 + + + + Filter: + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + + + + + + + 0 + 0 + + + + TextLabel + + + Qt::PlainText + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + + false + + + + + + + + + + + Results: + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + + + + + + + + + + + Filter Type + + + + + + Mime + + + + + + + Named + + + true + + + + + + + Hide name filter details option + + + + + + + Set default filter + + + + + + + + + + Save File + + + + + + + Import Files + + + + + + + Import File + + + + + + + Open Directory + + + + + + + Import Directory + + + + + bnImportFile + bnOpenFile + bnOpenFiles + bnOpenDirectory + bnImportFiles + bnImportDirectory + bnSaveFile + groupBox + lblMime + + + + +