File indexing completed on 2023-12-03 10:57:58

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 1999, 2000, 2001 Carsten Pfeiffer <pfeiffer@kde.org>
0004     SPDX-FileCopyrightText: 2013 Teo Mrnjavac <teo@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-only
0007 */
0008 
0009 #ifndef KURLREQUESTER_H
0010 #define KURLREQUESTER_H
0011 
0012 #include "kiowidgets_export.h"
0013 
0014 #include <KEditListWidget>
0015 #include <kfile.h>
0016 
0017 #include <QFileDialog>
0018 #include <QPushButton>
0019 #include <QUrl>
0020 
0021 #include <memory>
0022 
0023 class KComboBox;
0024 class KLineEdit;
0025 class KUrlCompletion;
0026 
0027 class QEvent;
0028 class QString;
0029 
0030 /**
0031  * @class KUrlRequester kurlrequester.h <KUrlRequester>
0032  *
0033  * This class is a widget showing a lineedit and a button, which invokes a
0034  * filedialog. File name completion is available in the lineedit.
0035  *
0036  * The default for the filedialog is to ask for one existing local file, i.e.
0037  * the default mode is 'KFile::File | KFile::ExistingOnly | KFile::LocalOnly',
0038  * which you can change by using setMode().
0039  *
0040  * The default filter is "*", i.e. show all files, which you can change by
0041  * using setNameFilters() or setMimeTypeFilters().
0042  *
0043  * By default the start directory is the current working directory, or the
0044  * last directory where a file has been selected previously, you can change
0045  * this behavior by calling setStartDir().
0046  *
0047  * The default window modality for the file dialog is Qt::ApplicationModal
0048  *
0049  * \image html kurlrequester.png "KUrlRequester"
0050  *
0051  * @short A widget to request a filename/url from the user
0052  * @author Carsten Pfeiffer <pfeiffer@kde.org>
0053  */
0054 class KIOWIDGETS_EXPORT KUrlRequester : public QWidget
0055 {
0056     Q_OBJECT
0057     Q_PROPERTY(QUrl url READ url WRITE setUrl NOTIFY textChanged USER true)
0058 #if KIOWIDGETS_BUILD_DEPRECATED_SINCE(5, 240)
0059     /// @deprecated Since 5.108, use nameFilters
0060     Q_PROPERTY(QString filter READ filter WRITE setFilter)
0061 #endif
0062     /// @since 5.108
0063     Q_PROPERTY(QStringList nameFilters READ nameFilters WRITE setNameFilters)
0064     Q_PROPERTY(KFile::Modes mode READ mode WRITE setMode)
0065     Q_PROPERTY(QFileDialog::AcceptMode acceptMode READ acceptMode WRITE setAcceptMode)
0066     Q_PROPERTY(QString placeholderText READ placeholderText WRITE setPlaceholderText)
0067     Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
0068     Q_PROPERTY(Qt::WindowModality fileDialogModality READ fileDialogModality WRITE setFileDialogModality)
0069 
0070 public:
0071     /**
0072      * Constructs a KUrlRequester widget.
0073      */
0074     explicit KUrlRequester(QWidget *parent = nullptr);
0075 
0076     /**
0077      * Constructs a KUrlRequester widget with the initial URL @p url.
0078      */
0079     explicit KUrlRequester(const QUrl &url, QWidget *parent = nullptr);
0080 
0081     /**
0082      * Special constructor, which creates a KUrlRequester widget with a custom
0083      * edit-widget. The edit-widget can be either a KComboBox or a KLineEdit
0084      * (or inherited thereof). Note: for geometry management reasons, the
0085      * edit-widget is reparented to have the KUrlRequester as parent.
0086      */
0087     KUrlRequester(QWidget *editWidget, QWidget *parent);
0088     /**
0089      * Destructs the KUrlRequester.
0090      */
0091     ~KUrlRequester() override;
0092 
0093     /**
0094      * @returns the current url in the lineedit. May be malformed, if the user
0095      * entered something weird. For local files, ~user or environment variables
0096      * are substituted, relative paths will be resolved against startDir()
0097      */
0098     QUrl url() const;
0099 
0100     /**
0101      * @returns the current start dir
0102      */
0103     QUrl startDir() const;
0104 
0105     /**
0106      * @returns the current text in the lineedit or combobox.
0107      * This does not do the URL expansion that url() does, it's only provided
0108      * for cases where KUrlRequester is used to enter URL-or-something-else,
0109      * like KOpenWithDialog where you can type a full command with arguments.
0110      *
0111      */
0112     QString text() const;
0113 
0114     /**
0115      * Sets the mode of the file dialog.
0116      *
0117      * The default mode of the file dialog is 'KFile::File | KFile::ExistingOnly | KFile::LocalOnly',
0118      * which you can change using this method.
0119      *
0120      * @note You can only select one file from the file dialog invoked
0121      * by KUrlRequester, hence setting KFile::Files doesn't make
0122      * much sense here.
0123      *
0124      * @param mode an OR'ed combination of KFile::Modes flags
0125      *
0126      * @see QFileDialog::setFileMode()
0127      */
0128     void setMode(KFile::Modes mode);
0129 
0130     /**
0131      * Returns the current mode
0132      * @see QFileDialog::fileMode()
0133      */
0134     KFile::Modes mode() const;
0135 
0136     /**
0137      * Sets the open / save mode of the file dialog.
0138      *
0139      * The default is QFileDialog::AcceptOpen.
0140      *
0141      * @see QFileDialog::setAcceptMode()
0142      * @since 5.33
0143      */
0144     void setAcceptMode(QFileDialog::AcceptMode m);
0145 
0146     /**
0147      * Returns the current open / save mode
0148      * @see QFileDialog::acceptMode()
0149      * @since 5.33
0150      */
0151     QFileDialog::AcceptMode acceptMode() const;
0152 
0153 #if KIOWIDGETS_ENABLE_DEPRECATED_SINCE(5, 240)
0154     /**
0155      * Sets the filters for the file dialog, separated by \\n.
0156      * Use "*.foo *.bar|Comment" syntax for each named filter.
0157      * "*.foo *.bar" for name-less filters also is supported.
0158      * @note This filter syntax is different from the one used in
0159      * QFileDialog::nameFilters() and converted internally.
0160      * @see filter()
0161      * @deprecated Since 5.108, use setNameFilters(const QStringList &) or setNameFilter(const QString &).
0162      *              Note: the filter argument might need adaption, due to the different filter syntax.
0163      */
0164     KIOWIDGETS_DEPRECATED_VERSION_BELATED(
0165         5,
0166         240,
0167         5,
0168         108,
0169         "Use KUrlRequester::setNameFilters(const QStringList &) or KUrlRequester::setNameFilter(const QString &). NOTE: different filter syntax.")
0170     void setFilter(const QString &filter);
0171 #endif
0172 
0173 #if KIOWIDGETS_ENABLE_DEPRECATED_SINCE(5, 240)
0174     /**
0175      * Returns the filters for the file dialog, separated by \\n.
0176      * @note This filter syntax is different from the one used in
0177      * QFileDialog::nameFilters() and converted internally.
0178      * @see setFilter()
0179      * @deprecated Since 5.108, use nameFilters() const.
0180      */
0181     KIOWIDGETS_DEPRECATED_VERSION_BELATED(5, 240, 5, 108, "Use KUrlRequester::nameFilters() const")
0182     QString filter() const;
0183 #endif
0184 
0185     /**
0186      * Sets the filters for the file dialog.
0187      * @see QFileDialog::setNameFilters()
0188      * @since 5.108
0189      */
0190     void setNameFilters(const QStringList &filters);
0191 
0192     /**
0193      * Sets the filters for the file dialog.
0194      * @see QFileDialog::setNameFilter()
0195      * @since 5.108
0196      */
0197     void setNameFilter(const QString &filter);
0198 
0199     /**
0200      * Returns the filters for the file dialog.
0201      * @see QFileDialog::nameFilters()
0202      * @since 5.108
0203      */
0204     QStringList nameFilters() const;
0205 
0206     /**
0207      * Sets the MIME type filters for the file dialog.
0208      * @see QFileDialog::setMimeTypeFilters()
0209      * @since 5.31
0210      */
0211     void setMimeTypeFilters(const QStringList &mimeTypes);
0212     /**
0213      * Returns the MIME type filters for the file dialog.
0214      * @see QFileDialog::mimeTypeFilters()
0215      * @since 5.31
0216      */
0217     QStringList mimeTypeFilters() const;
0218 
0219     /**
0220      * @returns a pointer to the filedialog.
0221      * You can use this to customize the dialog, e.g. to call setLocationLabel
0222      * or other things which are not accessible in the KUrlRequester API.
0223      *
0224      * Never returns 0. This method creates the file dialog on demand.
0225      *
0226      * @deprecated since 5.0. The dialog will be created anyway when the user
0227      * requests it, and will behave according to the properties of KUrlRequester.
0228      */
0229     KIOWIDGETS_DEPRECATED_VERSION(5, 0, "See API docs")
0230     virtual QFileDialog *fileDialog() const;
0231 
0232     /**
0233      * @returns a pointer to the lineedit, either the default one, or the
0234      * special one, if you used the special constructor.
0235      *
0236      * It is provided so that you can e.g. set an own completion object
0237      * (e.g. KShellCompletion) into it.
0238      */
0239     KLineEdit *lineEdit() const;
0240 
0241     /**
0242      * @returns a pointer to the combobox, in case you have set one using the
0243      * special constructor. Returns 0L otherwise.
0244      */
0245     KComboBox *comboBox() const;
0246 
0247     /**
0248      * @returns a pointer to the pushbutton. It is provided so that you can
0249      * specify an own pixmap or a text, if you really need to.
0250      */
0251     QPushButton *button() const;
0252 
0253     /**
0254      * @returns the KUrlCompletion object used in the lineedit/combobox.
0255      */
0256     KUrlCompletion *completionObject() const;
0257 
0258     /**
0259      * @returns an object, suitable for use with KEditListWidget. It allows you
0260      * to put this KUrlRequester into a KEditListWidget.
0261      * Basically, do it like this:
0262      * \code
0263      * KUrlRequester *req = new KUrlRequester( someWidget );
0264      * [...]
0265      * KEditListWidget *editListWidget = new KEditListWidget( req->customEditor(), someWidget );
0266      * \endcode
0267      */
0268     const KEditListWidget::CustomEditor &customEditor();
0269 
0270     /**
0271      * @return the message set with setPlaceholderText
0272      * @since 5.0
0273      */
0274     QString placeholderText() const;
0275 
0276     /**
0277      * This makes the KUrlRequester line edit display a grayed-out hinting text as long as
0278      * the user didn't enter any text. It is often used as indication about
0279      * the purpose of the line edit.
0280      * @since 5.0
0281      */
0282     void setPlaceholderText(const QString &msg);
0283 
0284     /**
0285      * @returns the window modality of the file dialog set with setFileDialogModality
0286      */
0287     Qt::WindowModality fileDialogModality() const;
0288 
0289     /**
0290      * Set the window modality for the file dialog to @p modality
0291      * Directory selection dialogs are always modal
0292      *
0293      * The default is Qt::ApplicationModal.
0294      *
0295      */
0296     void setFileDialogModality(Qt::WindowModality modality);
0297 
0298 public Q_SLOTS:
0299     /**
0300      * Sets the url in the lineedit to @p url.
0301      */
0302     void setUrl(const QUrl &url);
0303 
0304     /**
0305      * Sets the start dir @p startDir.
0306      * The start dir is only used when the URL isn't set.
0307      */
0308     void setStartDir(const QUrl &startDir);
0309 
0310     /**
0311      * Sets the current text in the lineedit or combobox.
0312      * This is used for cases where KUrlRequester is used to
0313      * enter URL-or-something-else, like KOpenWithDialog where you
0314      * can type a full command with arguments.
0315      *
0316      * @see text
0317      */
0318     void setText(const QString &text);
0319 
0320     /**
0321      * Clears the lineedit/combobox.
0322      */
0323     void clear();
0324 
0325 Q_SIGNALS:
0326     // forwards from LineEdit
0327     /**
0328      * Emitted when the text in the lineedit changes.
0329      * The parameter contains the contents of the lineedit.
0330      */
0331     void textChanged(const QString &);
0332 
0333     /**
0334      * Emitted when the text in the lineedit was modified by the user.
0335      * Unlike textChanged(), this signal is not emitted when the text is changed programmatically, for example, by calling setText().
0336      * @since 5.21
0337      */
0338     void textEdited(const QString &);
0339 
0340     /**
0341      * Emitted when return or enter was pressed in the lineedit.
0342      * The parameter contains the contents of the lineedit.
0343      */
0344     void returnPressed(const QString &text);
0345 
0346     /**
0347      * Emitted before the filedialog is going to open. Connect
0348      * to this signal to "configure" the filedialog, e.g. set the
0349      * filefilter, the mode, a preview-widget, etc. It's usually
0350      * not necessary to set a URL for the filedialog, as it will
0351      * get set properly from the editfield contents.
0352      *
0353      * If you use multiple KUrlRequesters, you can connect all of them
0354      * to the same slot and use the given KUrlRequester pointer to know
0355      * which one is going to open.
0356      */
0357     void openFileDialog(KUrlRequester *);
0358 
0359     /**
0360      * Emitted when the user changed the URL via the file dialog.
0361      * The parameter contains the contents of the lineedit.
0362      */
0363     void urlSelected(const QUrl &);
0364 
0365 protected:
0366     void changeEvent(QEvent *e) override;
0367     bool eventFilter(QObject *obj, QEvent *ev) override;
0368 
0369 private:
0370     class KUrlRequesterPrivate;
0371     std::unique_ptr<KUrlRequesterPrivate> const d;
0372 
0373     Q_DISABLE_COPY(KUrlRequester)
0374 };
0375 
0376 class KIOWIDGETS_EXPORT KUrlComboRequester : public KUrlRequester // krazy:exclude=dpointer (For use in Qt Designer)
0377 {
0378     Q_OBJECT
0379 public:
0380     /**
0381      * Constructs a KUrlRequester widget with a combobox.
0382      */
0383     explicit KUrlComboRequester(QWidget *parent = nullptr);
0384 
0385 private:
0386     class Private;
0387     Private *const d;
0388 };
0389 
0390 #endif // KURLREQUESTER_H