File indexing completed on 2023-12-10 11:10:46
0001 // -*- c++ -*- 0002 /* 0003 This file is part of the KDE libraries 0004 SPDX-FileCopyrightText: 1997, 1998 Richard Moore <rich@kde.org> 0005 SPDX-FileCopyrightText: 1998 Stephan Kulow <coolo@kde.org> 0006 SPDX-FileCopyrightText: 1998 Daniel Grana <grana@ie.iwi.unibe.ch> 0007 SPDX-FileCopyrightText: 2000, 2001 Carsten Pfeiffer <pfeiffer@kde.org> 0008 SPDX-FileCopyrightText: 2001 Frerich Raabe <raabe@kde.org> 0009 SPDX-FileCopyrightText: 2007 David Faure <faure@kde.org> 0010 SPDX-FileCopyrightText: 2008 Rafael Fernández López <ereslibre@kde.org> 0011 0012 SPDX-License-Identifier: LGPL-2.0-or-later 0013 */ 0014 0015 #ifndef KFILEWIDGET_H 0016 #define KFILEWIDGET_H 0017 0018 #include "kfile.h" 0019 #include "kiofilewidgets_export.h" 0020 #include <QWidget> 0021 0022 #include <memory> 0023 0024 class QUrl; 0025 class QPushButton; 0026 class KActionCollection; 0027 class KFileWidgetPrivate; 0028 class KUrlComboBox; 0029 class KFileFilterCombo; 0030 0031 class KPreviewWidgetBase; 0032 class QMimeType; 0033 class KConfigGroup; 0034 class KJob; 0035 class KFileItem; 0036 class KDirOperator; 0037 0038 /** 0039 * @class KFileWidget kfilewidget.h <KFileWidget> 0040 * 0041 * File selector widget. 0042 * 0043 * This is the contents of the KDE file dialog, without the actual QDialog around it. 0044 * It can be embedded directly into applications. 0045 */ 0046 class KIOFILEWIDGETS_EXPORT KFileWidget : public QWidget 0047 { 0048 Q_OBJECT 0049 public: 0050 /** 0051 * Constructs a file selector widget. 0052 * 0053 * @param startDir This can either be: 0054 * @li An empty URL (QUrl()) to start in the current working directory, 0055 * or the last directory where a file has been selected. 0056 * @li The path or URL of a starting directory. 0057 * @li An initial file name to select, with the starting directory being 0058 * the current working directory or the last directory where a file 0059 * has been selected. 0060 * @li The path or URL of a file, specifying both the starting directory and 0061 * an initially selected file name. 0062 * @li A URL of the form @c kfiledialog:///<keyword> to start in the 0063 * directory last used by a filedialog in the same application that 0064 * specified the same keyword. 0065 * @li A URL of the form @c kfiledialog:///<keyword>/<filename> 0066 * to start in the directory last used by a filedialog in the same 0067 * application that specified the same keyword, and to initially 0068 * select the specified filename. 0069 * @li Deprecated: A URL of the form @c kfiledialog:///<keyword>?global to start 0070 * in the directory last used by a filedialog in any application that 0071 * specified the same keyword. 0072 * @li Deprecated: A URL of the form @c kfiledialog:///<keyword>/<filename>?global 0073 * to start in the directory last used by a filedialog in any 0074 * application that specified the same keyword, and to initially 0075 * select the specified filename. 0076 * 0077 * @note Since 5.96, the "?global" syntax is deprecated, for lack of usage. 0078 * 0079 * @param parent The parent widget of this widget 0080 * 0081 */ 0082 explicit KFileWidget(const QUrl &startDir, QWidget *parent = nullptr); 0083 0084 /** 0085 * Destructor 0086 */ 0087 ~KFileWidget() override; 0088 0089 /** 0090 * Defines some default behavior of the filedialog. 0091 * E.g. in mode @p Opening and @p Saving, the selected files/urls will 0092 * be added to the "recent documents" list. The Saving mode also implies 0093 * setKeepLocation() being set. 0094 * 0095 * @p Other means that no default actions are performed. 0096 * 0097 * @see setOperationMode 0098 * @see operationMode 0099 */ 0100 enum OperationMode { Other = 0, Opening, Saving }; 0101 0102 /** 0103 * @returns The selected fully qualified filename. 0104 */ 0105 QUrl selectedUrl() const; 0106 0107 /** 0108 * @returns The list of selected URLs. 0109 */ 0110 QList<QUrl> selectedUrls() const; 0111 0112 /** 0113 * @returns the currently shown directory. 0114 */ 0115 QUrl baseUrl() const; 0116 0117 /** 0118 * Returns the full path of the selected file in the local filesystem. 0119 * (Local files only) 0120 */ 0121 QString selectedFile() const; 0122 0123 /** 0124 * Returns a list of all selected local files. 0125 */ 0126 QStringList selectedFiles() const; 0127 0128 /** 0129 * Sets the directory to view. 0130 * 0131 * @param url URL to show. 0132 * @param clearforward Indicates whether the forward queue 0133 * should be cleared. 0134 */ 0135 void setUrl(const QUrl &url, bool clearforward = true); 0136 0137 /** 0138 * Sets the URL to preselect to @p url 0139 * 0140 * This method handles absolute URLs (remember to use fromLocalFile for local paths). 0141 * It also handles relative URLs, which you should construct like this: 0142 * QUrl relativeUrl; relativeUrl.setPath(fileName); 0143 * 0144 * @since 5.33 0145 */ 0146 void setSelectedUrl(const QUrl &url); 0147 0148 /** 0149 * Sets a list of URLs as preselected 0150 * 0151 * @see setSelectedUrl 0152 * @since 5.75 0153 */ 0154 void setSelectedUrls(const QList<QUrl> &urls); 0155 0156 /** 0157 * Sets the operational mode of the filedialog to @p Saving, @p Opening 0158 * or @p Other. This will set some flags that are specific to loading 0159 * or saving files. E.g. setKeepLocation() makes mostly sense for 0160 * a save-as dialog. So setOperationMode( KFileWidget::Saving ); sets 0161 * setKeepLocation for example. 0162 * 0163 * The mode @p Saving, together with a default filter set via 0164 * setMimeFilter() will make the filter combobox read-only. 0165 * 0166 * The default mode is @p Opening. 0167 * 0168 * Call this method right after instantiating KFileWidget. 0169 * 0170 * @see operationMode 0171 * @see KFileWidget::OperationMode 0172 */ 0173 void setOperationMode(OperationMode); 0174 0175 /** 0176 * @returns the current operation mode, Opening, Saving or Other. Default 0177 * is Other. 0178 * 0179 * @see operationMode 0180 * @see KFileWidget::OperationMode 0181 */ 0182 OperationMode operationMode() const; 0183 0184 /** 0185 * Sets whether the filename/url should be kept when changing directories. 0186 * This is for example useful when having a predefined filename where 0187 * the full path for that file is searched. 0188 * 0189 * This is implicitly set when operationMode() is KFileWidget::Saving 0190 * 0191 * getSaveFileName() and getSaveUrl() set this to true by default, so that 0192 * you can type in the filename and change the directory without having 0193 * to type the name again. 0194 */ 0195 void setKeepLocation(bool keep); 0196 0197 /** 0198 * @returns whether the contents of the location edit are kept when 0199 * changing directories. 0200 */ 0201 bool keepsLocation() const; 0202 0203 /** 0204 * Sets the filter to be used to @p filter. 0205 * 0206 * You can set more 0207 * filters for the user to select separated by '\n'. Every 0208 * filter entry is defined through namefilter|text to display. 0209 * If no | is found in the expression, just the namefilter is 0210 * shown. Examples: 0211 * 0212 * \code 0213 * kfile->setFilter("*.cpp|C++ Source Files\n*.h|Header files"); 0214 * kfile->setFilter("*.cpp"); 0215 * kfile->setFilter("*.cpp|Sources (*.cpp)"); 0216 * kfile->setFilter("*.cpp|" + i18n("Sources (*.cpp)")); 0217 * kfile->setFilter("*.cpp *.cc *.C|C++ Source Files\n*.h *.H|Header files"); 0218 * \endcode 0219 * 0220 * Note: The text to display is not parsed in any way. So, if you 0221 * want to show the suffix to select by a specific filter, you must 0222 * repeat it. 0223 * 0224 * If the filter contains an unescaped '/', a MIME type filter is assumed. 0225 * If you would like a '/' visible in your filter it can be escaped with 0226 * a '\'. You can specify multiple MIME types like this (separated with 0227 * space): 0228 * 0229 * \code 0230 * kfile->setFilter( "image/png text/html text/plain" ); 0231 * kfile->setFilter( "*.cue|CUE\\/BIN Files (*.cue)" ); 0232 * \endcode 0233 * 0234 * @see filterChanged 0235 * @see setMimeFilter 0236 */ 0237 void setFilter(const QString &filter); 0238 0239 /** 0240 * Returns the current filter as entered by the user or one of the 0241 * predefined set via setFilter(). 0242 * 0243 * @see setFilter() 0244 * @see filterChanged() 0245 */ 0246 QString currentFilter() const; 0247 0248 /** 0249 * Returns the MIME type for the desired output format. 0250 * 0251 * This is only valid if setFilterMimeType() has been called 0252 * previously. 0253 * 0254 * @see setFilterMimeType() 0255 */ 0256 QMimeType currentFilterMimeType(); 0257 0258 /** 0259 * Sets the filter up to specify the output type. 0260 * 0261 * @param types a list of MIME types that can be used as output format 0262 * @param defaultType the default MIME type to use as output format, if any. 0263 * If @p defaultType is set, it will be set as the current item. 0264 * Otherwise, a first item showing all the MIME types will be created. 0265 * Typically, @p defaultType should be empty for loading and set for saving. 0266 * 0267 * Do not use in conjunction with setFilter() 0268 */ 0269 void setMimeFilter(const QStringList &types, const QString &defaultType = QString()); 0270 0271 /** 0272 * The MIME type for the desired output format. 0273 * 0274 * This is only valid if setMimeFilter() has been called 0275 * previously. 0276 * 0277 * @see setMimeFilter() 0278 */ 0279 QString currentMimeFilter() const; 0280 0281 /** 0282 * Clears any MIME type or name filter. Does not reload the directory. 0283 */ 0284 void clearFilter(); 0285 0286 /** 0287 * Adds a preview widget and enters the preview mode. 0288 * 0289 * In this mode the dialog is split and the right part contains your 0290 * preview widget. 0291 * 0292 * Ownership is transferred to KFileWidget. You need to create the 0293 * preview-widget with "new", i.e. on the heap. 0294 * 0295 * @param w The widget to be used for the preview. 0296 */ 0297 void setPreviewWidget(KPreviewWidgetBase *w); 0298 0299 /** 0300 * Sets the mode of the dialog. 0301 * 0302 * The mode is defined as (in kfile.h): 0303 * \code 0304 * enum Mode { 0305 * File = 1, 0306 * Directory = 2, 0307 * Files = 4, 0308 * ExistingOnly = 8, 0309 * LocalOnly = 16, 0310 * }; 0311 * \endcode 0312 * You can OR the values, e.g. 0313 * \code 0314 * KFile::Modes mode = KFile::Files | 0315 * KFile::ExistingOnly | 0316 * KFile::LocalOnly ); 0317 * setMode( mode ); 0318 * \endcode 0319 */ 0320 void setMode(KFile::Modes m); 0321 0322 /** 0323 * Returns the mode of the filedialog. 0324 * @see setMode() 0325 */ 0326 KFile::Modes mode() const; 0327 0328 /** 0329 * Sets the text to be displayed in front of the selection. 0330 * 0331 * The default is "Location". 0332 * Most useful if you want to make clear what 0333 * the location is used for. 0334 */ 0335 void setLocationLabel(const QString &text); 0336 0337 /** 0338 * @returns a pointer to the OK-Button in the filedialog. 0339 * Note that the button is hidden and unconnected when using KFileWidget alone; 0340 * KFileDialog shows it and connects to it. 0341 */ 0342 QPushButton *okButton() const; 0343 0344 /** 0345 * @returns a pointer to the Cancel-Button in the filedialog. 0346 * Note that the button is hidden and unconnected when using KFileWidget alone; 0347 * KFileDialog shows it and connects to it. 0348 */ 0349 QPushButton *cancelButton() const; 0350 0351 /** 0352 * @returns the combobox used to type the filename or full location of the file. 0353 */ 0354 KUrlComboBox *locationEdit() const; 0355 0356 /** 0357 * @returns the combobox that contains the filters 0358 */ 0359 KFileFilterCombo *filterWidget() const; 0360 0361 /** 0362 * This method implements the logic to determine the user's default directory 0363 * to be listed. E.g. the documents directory, home directory or a recently 0364 * used directory. 0365 * @param startDir A URL specifying the initial directory, or using the 0366 * @c kfiledialog:/// syntax to specify a last used 0367 * directory. If this URL specifies a file name, it is 0368 * ignored. Refer to the KFileWidget::KFileWidget() 0369 * documentation for the @c kfiledialog:/// URL syntax. 0370 * @param recentDirClass If the @c kfiledialog:/// syntax is used, this 0371 * will return the string to be passed to KRecentDirs::dir() and 0372 * KRecentDirs::add(). 0373 * @return The URL that should be listed by default (e.g. by KFileDialog or 0374 * KDirSelectDialog). 0375 * @see KFileWidget::KFileWidget() 0376 */ 0377 static QUrl getStartUrl(const QUrl &startDir, QString &recentDirClass); 0378 0379 /** 0380 * Similar to getStartUrl(const QUrl& startDir,QString& recentDirClass), 0381 * but allows both the recent start directory keyword and a suggested file name 0382 * to be returned. 0383 * @param startDir A URL specifying the initial directory and/or filename, 0384 * or using the @c kfiledialog:/// syntax to specify a 0385 * last used location. 0386 * Refer to the KFileWidget::KFileWidget() 0387 * documentation for the @c kfiledialog:/// URL syntax. 0388 * @param recentDirClass If the @c kfiledialog:/// syntax is used, this 0389 * will return the string to be passed to KRecentDirs::dir() and 0390 * KRecentDirs::add(). 0391 * @param fileName The suggested file name, if specified as part of the 0392 * @p StartDir URL. 0393 * @return The URL that should be listed by default (e.g. by KFileDialog or 0394 * KDirSelectDialog). 0395 * 0396 * @see KFileWidget::KFileWidget() 0397 */ 0398 static QUrl getStartUrl(const QUrl &startDir, QString &recentDirClass, QString &fileName); 0399 0400 /** 0401 * @internal 0402 * Used by KDirSelectDialog to share the dialog's start directory. 0403 */ 0404 static void setStartDir(const QUrl &directory); 0405 0406 /** 0407 * Set a custom widget that should be added to the file dialog. 0408 * @param widget A widget, or a widget of widgets, for displaying custom 0409 * data in the file widget. This can be used, for example, to 0410 * display a check box with the title "Open as read-only". 0411 * When creating this widget, you don't need to specify a parent, 0412 * since the widget's parent will be set automatically by KFileWidget. 0413 */ 0414 void setCustomWidget(QWidget *widget); 0415 0416 /** 0417 * Sets a custom widget that should be added below the location and the filter 0418 * editors. 0419 * @param text Label of the custom widget, which is displayed below the labels 0420 * "Location:" and "Filter:". 0421 * @param widget Any kind of widget, but preferable a combo box or a line editor 0422 * to be compliant with the location and filter layout. 0423 * When creating this widget, you don't need to specify a parent, 0424 * since the widget's parent will be set automatically by KFileWidget. 0425 */ 0426 void setCustomWidget(const QString &text, QWidget *widget); 0427 0428 /** 0429 * Sets whether the user should be asked for confirmation 0430 * when an overwrite might occur. 0431 * 0432 * @param enable Set this to true to enable checking. 0433 */ 0434 void setConfirmOverwrite(bool enable); 0435 0436 /** 0437 * Forces the inline previews to be shown or hidden, depending on @p show. 0438 * 0439 * @param show Whether to show inline previews or not. 0440 */ 0441 void setInlinePreviewShown(bool show); 0442 0443 /** 0444 * Provides a size hint, useful for dialogs that embed the widget. 0445 * 0446 * @return a QSize, calculated to be optimal for a dialog. 0447 * @since 5.0 0448 */ 0449 QSize dialogSizeHint() const; 0450 0451 /** 0452 * Sets how the view should be displayed. 0453 * 0454 * @see KFile::FileView 0455 * @since 5.0 0456 */ 0457 void setViewMode(KFile::FileView mode); 0458 0459 /** 0460 * Reimplemented 0461 */ 0462 QSize sizeHint() const override; 0463 0464 /** 0465 * Set the URL schemes that the file widget should allow navigating to. 0466 * 0467 * If the returned list is empty, all schemes are supported. 0468 * 0469 * @sa QFileDialog::setSupportedSchemes 0470 * @since 5.43 0471 */ 0472 void setSupportedSchemes(const QStringList &schemes); 0473 0474 /** 0475 * Returns the URL schemes that the file widget should allow navigating to. 0476 * 0477 * If the returned list is empty, all schemes are supported. Examples for 0478 * schemes are @c "file" or @c "ftp". 0479 * 0480 * @sa QFileDialog::supportedSchemes 0481 * @since 5.43 0482 */ 0483 QStringList supportedSchemes() const; 0484 0485 public Q_SLOTS: 0486 /** 0487 * Called when clicking ok (when this widget is used in KFileDialog) 0488 * Might or might not call accept(). 0489 */ 0490 void slotOk(); 0491 void accept(); 0492 void slotCancel(); 0493 0494 protected: 0495 void resizeEvent(QResizeEvent *event) override; 0496 void showEvent(QShowEvent *event) override; 0497 bool eventFilter(QObject *watched, QEvent *event) override; 0498 0499 Q_SIGNALS: 0500 /** 0501 * Emitted when the user selects a file. It is only emitted in single- 0502 * selection mode. The best way to get notified about selected file(s) 0503 * is to connect to the okClicked() signal inherited from KDialog 0504 * and call selectedFile(), selectedFiles(), 0505 * selectedUrl() or selectedUrls(). 0506 * 0507 * \since 4.4 0508 */ 0509 void fileSelected(const QUrl &); 0510 0511 /** 0512 * Emitted when the user highlights a file. 0513 * \since 4.4 0514 */ 0515 void fileHighlighted(const QUrl &); 0516 0517 /** 0518 * Emitted when the user highlights one or more files in multiselection mode. 0519 * 0520 * Note: fileHighlighted() or fileSelected() are @em not 0521 * emitted in multiselection mode. You may use selectedItems() to 0522 * ask for the current highlighted items. 0523 * @see fileSelected 0524 */ 0525 void selectionChanged(); 0526 0527 /** 0528 * Emitted when the filter changed, i.e.\ the user entered an own filter 0529 * or chose one of the predefined set via setFilter(). 0530 * 0531 * @param filter contains the new filter (only the extension part, 0532 * not the explanation), i.e. "*.cpp" or "*.cpp *.cc". 0533 * 0534 * @see setFilter() 0535 * @see currentFilter() 0536 */ 0537 void filterChanged(const QString &filter); 0538 0539 /** 0540 * Emitted by slotOk() (directly or asynchronously) once everything has 0541 * been done. Should be used by the caller to call accept(). 0542 */ 0543 void accepted(); 0544 0545 public: 0546 /** 0547 * @returns the KDirOperator used to navigate the filesystem 0548 */ 0549 KDirOperator *dirOperator(); 0550 0551 /** 0552 * reads the configuration for this widget from the given config group 0553 * @param group the KConfigGroup to read from 0554 */ 0555 void readConfig(KConfigGroup &group); 0556 0557 private: 0558 friend class KFileWidgetPrivate; 0559 std::unique_ptr<KFileWidgetPrivate> const d; 0560 }; 0561 0562 #endif