KIO

kfilewidget.h
1// -*- c++ -*-
2/*
3 This file is part of the KDE libraries
4 SPDX-FileCopyrightText: 1997, 1998 Richard Moore <rich@kde.org>
5 SPDX-FileCopyrightText: 1998 Stephan Kulow <coolo@kde.org>
6 SPDX-FileCopyrightText: 1998 Daniel Grana <grana@ie.iwi.unibe.ch>
7 SPDX-FileCopyrightText: 2000, 2001 Carsten Pfeiffer <pfeiffer@kde.org>
8 SPDX-FileCopyrightText: 2001 Frerich Raabe <raabe@kde.org>
9 SPDX-FileCopyrightText: 2007 David Faure <faure@kde.org>
10 SPDX-FileCopyrightText: 2008 Rafael Fernández López <ereslibre@kde.org>
11
12 SPDX-License-Identifier: LGPL-2.0-or-later
13*/
14
15#ifndef KFILEWIDGET_H
16#define KFILEWIDGET_H
17
18#include "kfile.h"
19#include "kiofilewidgets_export.h"
20#include <QWidget>
21
22#include <KFileFilter>
23
24#include <memory>
25
26class QUrl;
27class QPushButton;
29class KFileWidgetPrivate;
30class KUrlComboBox;
32
34class QMimeType;
35class KConfigGroup;
36class KJob;
37class KFileItem;
38class KDirOperator;
39
40/**
41 * @class KFileWidget kfilewidget.h <KFileWidget>
42 *
43 * File selector widget.
44 *
45 * This is the contents of the KDE file dialog, without the actual QDialog around it.
46 * It can be embedded directly into applications.
47 */
48class KIOFILEWIDGETS_EXPORT KFileWidget : public QWidget
49{
50 Q_OBJECT
51public:
52 /**
53 * Constructs a file selector widget.
54 *
55 * @param startDir This can either be:
56 * @li An empty URL (QUrl()) to start in the current working directory,
57 * or the last directory where a file has been selected.
58 * @li The path or URL of a starting directory.
59 * @li An initial file name to select, with the starting directory being
60 * the current working directory or the last directory where a file
61 * has been selected.
62 * @li The path or URL of a file, specifying both the starting directory and
63 * an initially selected file name.
64 * @li A URL of the form @c kfiledialog:///&lt;keyword&gt; to start in the
65 * directory last used by a filedialog in the same application that
66 * specified the same keyword.
67 * @li A URL of the form @c kfiledialog:///&lt;keyword&gt;/&lt;filename&gt;
68 * to start in the directory last used by a filedialog in the same
69 * application that specified the same keyword, and to initially
70 * select the specified filename.
71 * @li Deprecated: A URL of the form @c kfiledialog:///&lt;keyword&gt;?global to start
72 * in the directory last used by a filedialog in any application that
73 * specified the same keyword.
74 * @li Deprecated: A URL of the form @c kfiledialog:///&lt;keyword&gt;/&lt;filename&gt;?global
75 * to start in the directory last used by a filedialog in any
76 * application that specified the same keyword, and to initially
77 * select the specified filename.
78 *
79 * @note Since 5.96, the "?global" syntax is deprecated, for lack of usage.
80 *
81 * @param parent The parent widget of this widget
82 *
83 */
84 explicit KFileWidget(const QUrl &startDir, QWidget *parent = nullptr);
85
86 /**
87 * Destructor
88 */
89 ~KFileWidget() override;
90
91 /**
92 * Defines some default behavior of the filedialog.
93 * E.g. in mode @p Opening and @p Saving, the selected files/urls will
94 * be added to the "recent documents" list. The Saving mode also implies
95 * setKeepLocation() being set.
96 *
97 * @p Other means that no default actions are performed.
98 *
99 * @see setOperationMode
100 * @see operationMode
101 */
103 Other = 0,
104 Opening,
105 Saving
106 };
107
108 /**
109 * @returns The selected fully qualified filename.
110 */
111 QUrl selectedUrl() const;
112
113 /**
114 * @returns The list of selected URLs.
115 */
116 QList<QUrl> selectedUrls() const;
117
118 /**
119 * @returns the currently shown directory.
120 */
121 QUrl baseUrl() const;
122
123 /**
124 * Returns the full path of the selected file in the local filesystem.
125 * (Local files only)
126 */
127 QString selectedFile() const;
128
129 /**
130 * Returns a list of all selected local files.
131 */
132 QStringList selectedFiles() const;
133
134 /**
135 * Sets the directory to view.
136 *
137 * @param url URL to show.
138 * @param clearforward Indicates whether the forward queue
139 * should be cleared.
140 */
141 void setUrl(const QUrl &url, bool clearforward = true);
142
143 /**
144 * Sets the URL to preselect to @p url
145 *
146 * This method handles absolute URLs (remember to use fromLocalFile for local paths).
147 * It also handles relative URLs, which you should construct like this:
148 * QUrl relativeUrl; relativeUrl.setPath(fileName);
149 *
150 * @since 5.33
151 */
152 void setSelectedUrl(const QUrl &url);
153
154 /**
155 * Sets a list of URLs as preselected
156 *
157 * @see setSelectedUrl
158 * @since 5.75
159 */
160 void setSelectedUrls(const QList<QUrl> &urls);
161
162 /**
163 * Sets the operational mode of the filedialog to @p Saving, @p Opening
164 * or @p Other. This will set some flags that are specific to loading
165 * or saving files. E.g. setKeepLocation() makes mostly sense for
166 * a save-as dialog. So setOperationMode( KFileWidget::Saving ); sets
167 * setKeepLocation for example.
168 *
169 * The mode @p Saving, together with a default filter set via
170 * setMimeFilter() will make the filter combobox read-only.
171 *
172 * The default mode is @p Opening.
173 *
174 * Call this method right after instantiating KFileWidget.
175 *
176 * @see operationMode
177 * @see KFileWidget::OperationMode
178 */
179 void setOperationMode(OperationMode);
180
181 /**
182 * @returns the current operation mode, Opening, Saving or Other. Default
183 * is Other.
184 *
185 * @see operationMode
186 * @see KFileWidget::OperationMode
187 */
188 OperationMode operationMode() const;
189
190 /**
191 * Sets whether the filename/url should be kept when changing directories.
192 * This is for example useful when having a predefined filename where
193 * the full path for that file is searched.
194 *
195 * This is implicitly set when operationMode() is KFileWidget::Saving
196 *
197 * getSaveFileName() and getSaveUrl() set this to true by default, so that
198 * you can type in the filename and change the directory without having
199 * to type the name again.
200 */
201 void setKeepLocation(bool keep);
202
203 /**
204 * @returns whether the contents of the location edit are kept when
205 * changing directories.
206 */
207 bool keepsLocation() const;
208
209 /**
210 * Set the filters to be used.
211 *
212 * Each item of the list corresponds to a selectable filter.
213 *
214 * Only one filter is active at a time.
215 *
216 * @param activeFilter the initially active filter
217 *
218 * @since 6.0
219 *
220 */
221 void setFilters(const QList<KFileFilter> &filters, const KFileFilter &activeFilter = KFileFilter());
222
223 /**
224 * Returns the current filter as entered by the user or one of the
225 * predefined set via setFilters().
226 *
227 * @see setFilters()
228 * @see filterChanged()
229 *
230 * @since 6.0
231 */
232 KFileFilter currentFilter() const;
233
234 /**
235 * Clears any MIME type or name filter. Does not reload the directory.
236 */
237 void clearFilter();
238
239 /**
240 * Adds a preview widget and enters the preview mode.
241 *
242 * In this mode the dialog is split and the right part contains your
243 * preview widget.
244 *
245 * Ownership is transferred to KFileWidget. You need to create the
246 * preview-widget with "new", i.e. on the heap.
247 *
248 * @param w The widget to be used for the preview.
249 */
250 void setPreviewWidget(KPreviewWidgetBase *w);
251
252 /**
253 * Sets the mode of the dialog.
254 *
255 * The mode is defined as (in kfile.h):
256 * \code
257 * enum Mode {
258 * File = 1,
259 * Directory = 2,
260 * Files = 4,
261 * ExistingOnly = 8,
262 * LocalOnly = 16,
263 * };
264 * \endcode
265 * You can OR the values, e.g.
266 * \code
267 * KFile::Modes mode = KFile::Files |
268 * KFile::ExistingOnly |
269 * KFile::LocalOnly );
270 * setMode( mode );
271 * \endcode
272 */
273 void setMode(KFile::Modes m);
274
275 /**
276 * Returns the mode of the filedialog.
277 * @see setMode()
278 */
279 KFile::Modes mode() const;
280
281 /**
282 * Sets the text to be displayed in front of the selection.
283 *
284 * The default is "Location".
285 * Most useful if you want to make clear what
286 * the location is used for.
287 */
288 void setLocationLabel(const QString &text);
289
290 /**
291 * @returns a pointer to the OK-Button in the filedialog.
292 * Note that the button is hidden and unconnected when using KFileWidget alone;
293 * KFileDialog shows it and connects to it.
294 */
295 QPushButton *okButton() const;
296
297 /**
298 * @returns a pointer to the Cancel-Button in the filedialog.
299 * Note that the button is hidden and unconnected when using KFileWidget alone;
300 * KFileDialog shows it and connects to it.
301 */
302 QPushButton *cancelButton() const;
303
304 /**
305 * @returns the combobox used to type the filename or full location of the file.
306 */
307 KUrlComboBox *locationEdit() const;
308
309 /**
310 * @returns the combobox that contains the filters
311 */
312 KFileFilterCombo *filterWidget() const;
313
314 /**
315 * This method implements the logic to determine the user's default directory
316 * to be listed. E.g. the documents directory, home directory or a recently
317 * used directory.
318 * @param startDir A URL specifying the initial directory, or using the
319 * @c kfiledialog:/// syntax to specify a last used
320 * directory. If this URL specifies a file name, it is
321 * ignored. Refer to the KFileWidget::KFileWidget()
322 * documentation for the @c kfiledialog:/// URL syntax.
323 * @param recentDirClass If the @c kfiledialog:/// syntax is used, this
324 * will return the string to be passed to KRecentDirs::dir() and
325 * KRecentDirs::add().
326 * @return The URL that should be listed by default (e.g. by KFileDialog or
327 * KDirSelectDialog).
328 * @see KFileWidget::KFileWidget()
329 */
330 static QUrl getStartUrl(const QUrl &startDir, QString &recentDirClass);
331
332 /**
333 * Similar to getStartUrl(const QUrl& startDir,QString& recentDirClass),
334 * but allows both the recent start directory keyword and a suggested file name
335 * to be returned.
336 * @param startDir A URL specifying the initial directory and/or filename,
337 * or using the @c kfiledialog:/// syntax to specify a
338 * last used location.
339 * Refer to the KFileWidget::KFileWidget()
340 * documentation for the @c kfiledialog:/// URL syntax.
341 * @param recentDirClass If the @c kfiledialog:/// syntax is used, this
342 * will return the string to be passed to KRecentDirs::dir() and
343 * KRecentDirs::add().
344 * @param fileName The suggested file name, if specified as part of the
345 * @p StartDir URL.
346 * @return The URL that should be listed by default (e.g. by KFileDialog or
347 * KDirSelectDialog).
348 *
349 * @see KFileWidget::KFileWidget()
350 */
351 static QUrl getStartUrl(const QUrl &startDir, QString &recentDirClass, QString &fileName);
352
353 /**
354 * @internal
355 * Used by KDirSelectDialog to share the dialog's start directory.
356 */
357 static void setStartDir(const QUrl &directory);
358
359 /**
360 * Set a custom widget that should be added to the file dialog.
361 * @param widget A widget, or a widget of widgets, for displaying custom
362 * data in the file widget. This can be used, for example, to
363 * display a check box with the title "Open as read-only".
364 * When creating this widget, you don't need to specify a parent,
365 * since the widget's parent will be set automatically by KFileWidget.
366 */
367 void setCustomWidget(QWidget *widget);
368
369 /**
370 * Sets a custom widget that should be added below the location and the filter
371 * editors.
372 * @param text Label of the custom widget, which is displayed below the labels
373 * "Location:" and "Filter:".
374 * @param widget Any kind of widget, but preferable a combo box or a line editor
375 * to be compliant with the location and filter layout.
376 * When creating this widget, you don't need to specify a parent,
377 * since the widget's parent will be set automatically by KFileWidget.
378 */
379 void setCustomWidget(const QString &text, QWidget *widget);
380
381 /**
382 * Sets whether the user should be asked for confirmation
383 * when an overwrite might occur.
384 *
385 * @param enable Set this to true to enable checking.
386 */
387 void setConfirmOverwrite(bool enable);
388
389 /**
390 * Forces the inline previews to be shown or hidden, depending on @p show.
391 *
392 * @param show Whether to show inline previews or not.
393 */
394 void setInlinePreviewShown(bool show);
395
396 /**
397 * Provides a size hint, useful for dialogs that embed the widget.
398 *
399 * @return a QSize, calculated to be optimal for a dialog.
400 * @since 5.0
401 */
402 QSize dialogSizeHint() const;
403
404 /**
405 * Sets how the view should be displayed.
406 *
407 * @see KFile::FileView
408 * @since 5.0
409 */
410 void setViewMode(KFile::FileView mode);
411
412 /**
413 * Reimplemented
414 */
415 QSize sizeHint() const override;
416
417 /**
418 * Set the URL schemes that the file widget should allow navigating to.
419 *
420 * If the returned list is empty, all schemes are supported.
421 *
422 * @sa QFileDialog::setSupportedSchemes
423 * @since 5.43
424 */
425 void setSupportedSchemes(const QStringList &schemes);
426
427 /**
428 * Returns the URL schemes that the file widget should allow navigating to.
429 *
430 * If the returned list is empty, all schemes are supported. Examples for
431 * schemes are @c "file" or @c "ftp".
432 *
433 * @sa QFileDialog::supportedSchemes
434 * @since 5.43
435 */
436 QStringList supportedSchemes() const;
437
438public Q_SLOTS:
439 /**
440 * Called when clicking ok (when this widget is used in KFileDialog)
441 * Might or might not call accept().
442 */
443 void slotOk();
444 void accept();
445 void slotCancel();
446
447protected:
448 void resizeEvent(QResizeEvent *event) override;
449 void showEvent(QShowEvent *event) override;
450 bool eventFilter(QObject *watched, QEvent *event) override;
451
453 /**
454 * Emitted when the user selects a file. It is only emitted in single-
455 * selection mode. The best way to get notified about selected file(s)
456 * is to connect to the okClicked() signal inherited from KDialog
457 * and call selectedFile(), selectedFiles(),
458 * selectedUrl() or selectedUrls().
459 *
460 * \since 4.4
461 */
462 void fileSelected(const QUrl &);
463
464 /**
465 * Emitted when the user highlights a file.
466 * \since 4.4
467 */
468 void fileHighlighted(const QUrl &);
469
470 /**
471 * Emitted when the user highlights one or more files in multiselection mode.
472 *
473 * Note: fileHighlighted() or fileSelected() are @em not
474 * emitted in multiselection mode. You may use selectedItems() to
475 * ask for the current highlighted items.
476 * @see fileSelected
477 */
479
480 /**
481 * Emitted when the filter changed, i.e.\ the user entered an own filter
482 * or chose one of the predefined set via setFilters().
483 *
484 * @param filter contains the new filter (only the extension part,
485 * not the explanation), i.e. "*.cpp" or "*.cpp *.cc".
486 *
487 * @see setFilters()
488 * @see currentFilter()
489 *
490 * @since 6.0
491 */
492 void filterChanged(const KFileFilter &filter);
493
494 /**
495 * Emitted by slotOk() (directly or asynchronously) once everything has
496 * been done. Should be used by the caller to call accept().
497 */
498 void accepted();
499
500public:
501 /**
502 * @returns the KDirOperator used to navigate the filesystem
503 */
504 KDirOperator *dirOperator();
505
506#if KIOFILEWIDGETS_ENABLE_DEPRECATED_SINCE(6, 3)
507 /**
508 * reads the configuration for this widget from the given config group
509 * @param group the KConfigGroup to read from
510 *
511 * @deprecated since 6.3, no known use case.
512 */
513 KIOFILEWIDGETS_DEPRECATED_VERSION(6, 3, "No known use case")
514 void readConfig(KConfigGroup &group);
515#endif
516
517private:
518 friend class KFileWidgetPrivate;
519 std::unique_ptr<KFileWidgetPrivate> const d;
520};
521
522#endif
This widget works as a network transparent filebrowser.
File filter combo box.
Encapsulates rules to filter a list of files.
Definition kfilefilter.h:29
A KFileItem is a generic class to handle a file, local or remote.
Definition kfileitem.h:36
File selector widget.
Definition kfilewidget.h:49
void accepted()
Emitted by slotOk() (directly or asynchronously) once everything has been done.
void fileSelected(const QUrl &)
Emitted when the user selects a file.
void fileHighlighted(const QUrl &)
Emitted when the user highlights a file.
void filterChanged(const KFileFilter &filter)
Emitted when the filter changed, i.e. the user entered an own filter or chose one of the predefined s...
void selectionChanged()
Emitted when the user highlights one or more files in multiselection mode.
OperationMode
Defines some default behavior of the filedialog.
Abstract baseclass for all preview widgets which shall be used via KFileDialog::setPreviewWidget(cons...
This combobox shows a number of recent URLs/directories, as well as some default directories.
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
virtual bool eventFilter(QObject *watched, QEvent *event)
virtual void resizeEvent(QResizeEvent *event)
virtual void showEvent(QShowEvent *event)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:16:28 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.