2 /* This file is part of the KDE libraries
3 Copyright (C) 1999 Stephan Kulow <coolo@kde.org>
4 2000,2001 Carsten Pfeiffer <pfeiffer@kde.org>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
21 #ifndef KDIROPERATOR_H_
22 #define KDIROPERATOR_H_
24 #include <QtGui/QWidget>
25 #include <QtCore/QStack>
27 #include <ktoggleaction.h>
28 #include <kcompletion.h>
29 #include <kdirlister.h>
30 #include <kfileitem.h>
32 #include <kfile_export.h>
34 class QAbstractItemView
;
39 class KActionCollection
;
42 class KPreviewWidgetBase
;
43 class KFilePreviewGenerator
;
52 * This widget works as a network transparent filebrowser. You specify a URL
53 * to display and this url will be loaded via KDirLister. The user can
54 * browse through directories, highlight and select files, delete or rename
57 * It supports different views, e.g. a detailed view (see KFileDetailView),
58 * a simple icon view (see KFileIconView), a combination of two views,
59 * separating directories and files ( KCombiView).
61 * Additionally, a preview view is available (see KFilePreview), which can
62 * show either a simple or detailed view and additionally a preview widget
63 * (see setPreviewWidget()). KImageFilePreview is one implementation
64 * of a preview widget, that displays previews for all supported filetypes
65 * utilizing KIO::PreviewJob.
67 * Currently, those classes don't support Drag&Drop out of the box -- there
68 * you have to use your own view-classes. You can use some DnD-aware views
69 * from Björn Sahlström <bjorn@kbear.org> until they will be integrated
70 * into this library. See http://devel-home.kde.org/~pfeiffer/DnD-classes.tar.gz
72 * This widget is the one used in the KFileDialog.
74 * Basic usage is like this:
76 * KDirOperator *op = new KDirOperator( KUrl( "file:/home/gis" ), this );
77 * // some signals you might be interested in
78 * connect(op, SIGNAL(urlEntered(const KUrl&)),
79 * SLOT(urlEntered(const KUrl&)));
80 * connect(op, SIGNAL(fileHighlighted(const KFileItem &)),
81 * SLOT(fileHighlighted(const KFileItem &)));
82 * connect(op, SIGNAL(fileSelected(const KFileItem &)),
83 * SLOT(fileSelected(const KFileItem &)));
84 * connect(op, SIGNAL(finishedLoading()),
85 * SLOT(slotLoadingFinished()));
87 * KConfigGroup grp(KGlobal::config(),"Your KDiroperator ConfigGroup" );
88 * op->readConfig( &grp);
89 * op->setView(KFile::Default);
92 * This will create a childwidget of 'this' showing the directory contents
93 * of /home/gis in the default-view. The view is determined by the readConfig()
94 * call, which will read the KDirOperator settings, the user left your program
95 * with (and which you saved with op->writeConfig()).
97 * @short A widget for displaying files and browsing directories.
98 * @author Stephan Kulow <coolo@kde.org>, Carsten Pfeiffer <pfeiffer@kde.org>
100 class KFILE_EXPORT KDirOperator
: public QWidget
106 * The various action types. These values can be or'd together
118 * Constructs the KDirOperator with no initial view. As the views are
119 * configurable, call readConfig() to load the user's configuration
120 * and then setView to explicitly set a view.
122 * This constructor doesn't start loading the url, setView will do it.
124 explicit KDirOperator(const KUrl
& urlName
= KUrl(),
125 QWidget
*parent
= 0);
127 * Destroys the KDirOperator.
129 virtual ~KDirOperator();
132 * Enables/disables showing hidden files.
134 virtual void setShowHiddenFiles(bool s
);
137 * @returns true when hidden files are shown or false otherwise.
139 bool showHiddenFiles() const;
142 * Stops loading immediately. You don't need to call this, usually.
147 * Sets a filter like "*.cpp *.h *.o". Only files matching that filter
150 * @see KDirLister::setNameFilter
153 void setNameFilter(const QString
& filter
);
156 * @returns the current namefilter.
159 QString
nameFilter() const;
162 * Sets a list of mimetypes as filter. Only files of those mimetypes
167 * QStringList filter;
168 * filter << "text/html" << "image/png" << "inode/directory";
169 * dirOperator->setMimefilter( filter );
172 * Node: Without the mimetype inode/directory, only files would be shown.
173 * Call updateDir() to apply it.
175 * @see KDirLister::setMimeFilter
178 void setMimeFilter(const QStringList
& mimetypes
);
181 * @returns the current mime filter.
183 QStringList
mimeFilter() const;
186 * Clears both the namefilter and mimetype filter, so that all files and
187 * directories will be shown. Call updateDir() to apply it.
195 * @returns the current url
200 * Sets a new url to list.
201 * @param clearforward specifies whether the "forward" history should be cleared.
202 * @param url the URL to set
204 virtual void setUrl(const KUrl
& url
, bool clearforward
);
207 * Clears the current selection and attempts to set @p url
208 * the current url file. Note that urls can be relative or
211 void setCurrentItem(const QString
& url
);
214 * Clears the current selection and attempts to set @p item
215 * as the current item.
217 void setCurrentItem(const KFileItem
& item
);
220 * Clears the current selection and attempts to set @p urls
221 * the current url files. Note that urls can be relative or
225 void setCurrentItems(const QStringList
& urls
);
228 * Clears the current selection and attempts to set @p items
229 * as the current items.
232 void setCurrentItems(const KFileItemList
& items
);
235 * Sets a new view to be used for showing and browsing files.
236 * Note: this will read the current url() to fill the view.
241 virtual void setView(QAbstractItemView
*view
);
244 * @returns the currently used view.
247 QAbstractItemView
* view() const;
250 * Sets one of the predefined fileviews.
251 * @see KFile::FileView
253 virtual void setView(KFile::FileView viewKind
);
256 * Sets the way to sort files and directories.
258 void setSorting(QDir::SortFlags
);
261 * @returns the current way of sorting files and directories
263 QDir::SortFlags
sorting() const;
266 * @returns true if we are displaying the root directory of the current url
271 * @returns the object listing the directory
273 KDirLister
* dirLister() const;
276 * @returns the progress widget, that is shown during directory listing.
277 * You can for example reparent() it to put it into a statusbar.
279 QProgressBar
* progressBar() const;
282 * Sets the listing/selection mode for the views, an OR'ed combination of
289 * You cannot mix File and Files of course, as the former means
290 * single-selection mode, the latter multi-selection.
292 virtual void setMode(KFile::Modes m
);
294 * @returns the listing/selection mode.
296 KFile::Modes
mode() const;
299 * Sets a preview-widget to be shown next to the file-view.
300 * The ownership of @p w is transferred to KDirOperator, so don't
301 * delete it yourself!
303 virtual void setPreviewWidget(KPreviewWidgetBase
*w
);
306 * @returns a list of all currently selected items. If there is no view,
307 * or there are no selected items, an empty list is returned.
309 KFileItemList
selectedItems() const;
312 * @returns true if @p item is currently selected, or false otherwise.
314 bool isSelected(const KFileItem
&item
) const;
317 * @returns the number of directories in the currently listed url.
318 * Returns 0 if there is no view.
323 * @returns the number of files in the currently listed url.
324 * Returns 0 if there is no view.
326 int numFiles() const;
329 * @returns a KCompletion object, containing all filenames and
330 * directories of the current directory/URL.
331 * You can use it to insert it into a KLineEdit or KComboBox
332 * Note: it will only contain files, after prepareCompletionObjects()
333 * has been called. It will be implicitly called from makeCompletion()
334 * or makeDirCompletion()
336 KCompletion
* completionObject() const;
339 * @returns a KCompletion object, containing only all directories of the
340 * current directory/URL.
341 * You can use it to insert it into a KLineEdit or KComboBox
342 * Note: it will only contain directories, after
343 * prepareCompletionObjects() has been called. It will be implicitly
344 * called from makeCompletion() or makeDirCompletion()
346 KCompletion
* dirCompletionObject() const;
349 * an accessor to a collection of all available Actions. The actions
350 * are static, they will be there all the time (no need to connect to
351 * the signals KActionCollection::inserted() or removed().
353 * There are the following actions:
355 * @li popupMenu : an ActionMenu presenting a popupmenu with all actions
356 * @li up : changes to the parent directory
357 * @li back : goes back to the previous directory
358 * @li forward : goes forward in the history
359 * @li home : changes to the user's home directory
360 * @li reload : reloads the current directory
361 * @li mkdir : opens a dialog box to create a directory
362 * @li delete : deletes the selected files/directories
363 * @li sorting menu : an ActionMenu containing all sort-options
364 * @li by name : sorts by name
365 * @li by size : sorts by size
366 * @li by date : sorts by date
367 * @li by type : sorts by type
368 * @li descending : reverses the sort order
369 * @li view menu : an ActionMenu containing all actions concerning the view
370 * @li short view : shows a simple fileview
371 * @li detailed view : shows a detailed fileview (dates, permissions ,...)
372 * @li show hidden : shows hidden files
373 * @li preview : shows a preview next to the fileview
374 * @li properties : shows a KPropertiesDialog for the selected files
376 * The short and detailed view are in an exclusive group. The sort-by
377 * actions are in an exclusive group as well. Also the "separate dirs",
378 * "preview" and "single" actions are in an exclusive group.
382 * actionCollection()->action( "up" )->plug( someToolBar );
384 * to add a button into a toolbar, which makes the dirOperator change to
385 * its parent directory.
387 * @returns all available Actions
389 KActionCollection
* actionCollection() const;
392 * Sets the config object and the to be used group in KDirOperator. This
393 * will be used to store the view's configuration.
394 * If you don't set this, the views cannot save and restore their
397 * Usually you call this right after KDirOperator creation so that the view
398 * instantiation can make use of it already.
400 * Note that KDirOperator does NOT take ownership of that object (typically
401 * it's KGlobal::config() anyway.
403 * You must not delete the KConfig or KConfigGroup object (and master config object) before
404 * either deleting the KDirOperator or calling setViewConfig(0); or something like that
407 * @see viewConfigGroup
409 virtual void setViewConfig(KConfigGroup
& configGroup
);
412 * @returns the group set by setViewConfig configuration.
414 KConfigGroup
* viewConfigGroup() const;
417 * Reads the default settings for a view, i.e. the default KFile::FileView.
418 * Also reads the sorting and whether hidden files should be shown.
419 * Note: the default view will not be set - you have to call
421 * setView( KFile::Default )
429 virtual void readConfig(const KConfigGroup
& configGroup
);
432 * Saves the current settings like sorting, simple or detailed view.
437 virtual void writeConfig(KConfigGroup
& configGroup
);
440 * This toggles between double/single click file and directory selection mode.
441 * When argument is true, files and directories are highlighted with single click and
442 * selected (executed) with double click.
444 * The default follows the signle/double click system setting.
446 void setOnlyDoubleClickSelectsFiles(bool enable
);
449 * @returns whether files (not directories) should only be select()ed by
451 * @see setOnlyDoubleClickSelectsFiles
453 bool onlyDoubleClickSelectsFiles() const;
456 * Creates the given directory/url. If it is a relative path,
457 * it will be completed with the current directory.
458 * If enterDirectory is true, the directory will be entered after a
459 * successful operation. If unsuccessful, a messagebox will be presented
461 * @returns true if the directory could be created.
463 virtual bool mkdir(const QString
& directory
, bool enterDirectory
= true);
466 * Starts and returns a KIO::DeleteJob to delete the given @p items.
468 * @param items the list of items to be deleted
469 * @param parent the parent widget used for the confirmation dialog
470 * @param ask specifies whether a confirmation dialog should be shown
471 * @param showProgress passed to the DeleteJob to show a progress dialog
473 virtual KIO::DeleteJob
* del(const KFileItemList
& items
, QWidget
*parent
= 0,
474 bool ask
= true, bool showProgress
= true);
477 * Clears the forward and backward history.
482 * When going up in the directory hierarchy, KDirOperator can highlight
483 * the directory that was just left.
485 * I.e. when you go from /home/gis/src to /home/gis, the item "src" will
486 * be made the current item.
490 virtual void setEnableDirHighlighting(bool enable
);
493 * @returns whether the last directory will be made the current item
494 * when going up in the directory hierarchy.
498 bool dirHighlighting() const;
501 * @returns true if we are in directory-only mode, that is, no files are
504 bool dirOnlyMode() const;
506 static bool dirOnlyMode(uint mode
);
509 * Sets up the action menu.
510 * @param whichActions is an value of OR'd ActionTypes that controls which actions to show in the action menu
512 void setupMenu(int whichActions
);
515 * Reimplemented - allow dropping of files if @p b is true
516 * @param b true if the widget should allow dropping of files
518 virtual void setAcceptDrops(bool b
);
521 * Sets the options for dropping files.
522 * CURRENTLY NOT IMPLEMENTED
524 virtual void setDropOptions(int options
);
527 * Starts and returns a KIO::CopyJob to trash the given @p items.
529 * @param items the list of items to be trashed
530 * @param parent the parent widget used for the confirmation dialog
531 * @param ask specifies whether a confirmation dialog should be shown
532 * @param showProgress passed to the CopyJob to show a progress dialog
534 virtual KIO::CopyJob
* trash(const KFileItemList
& items
, QWidget
*parent
,
535 bool ask
= true, bool showProgress
= true);
538 * Returns the preview generator for the current view.
541 KFilePreviewGenerator
*previewGenerator() const;
544 * Forces the inline previews to be shown or hidden, depending on @p show.
546 * @param show Whether to show inline previews or not.
549 void setInlinePreviewShown(bool show
);
552 * Returns whether the inline previews are shown or not.
555 bool isInlinePreviewShown() const;
558 * Returns the icon zoom.
561 int iconsZoom() const;
564 * If the system is set up to trigger items on single click, if @p isSaving
565 * is true, we will force to double click to accept.
566 * @note this is false by default
569 void setIsSaving(bool isSaving
);
572 * Returns whether KDirOperator will force a double click to accept.
573 * @note this is false by default
576 bool isSaving() const;
580 * A view factory for creating predefined fileviews. Called internally by setView,
581 * but you can also call it directly. Reimplement this if you depend on self defined fileviews.
582 * @param parent is the QWidget to be set as parent
583 * @param viewKind is the predefined view to be set, note: this can be several ones OR:ed together
584 * @returns the created view
585 * @see KFile::FileView
588 virtual QAbstractItemView
* createView(QWidget
*parent
, KFile::FileView viewKind
);
591 * Sets a custom KDirLister to list directories.
592 * The KDirOperator takes ownership of the given KDirLister.
594 virtual void setDirLister(KDirLister
*lister
);
596 virtual void resizeEvent(QResizeEvent
*event
);
599 * Sets up all the actions. Called from the constructor, you usually
600 * better not call this.
605 * Updates the sorting-related actions to comply with the current sorting
608 void updateSortActions();
611 * Updates the view-related actions to comply with the current
614 void updateViewActions();
617 * Sets up the context-menu with all the necessary actions. Called from the
618 * constructor, you usually don't need to call this.
623 * Synchronizes the completion objects with the entries of the
624 * currently listed url.
626 * Automatically called from makeCompletion() and
627 * makeDirCompletion()
629 void prepareCompletionObjects();
632 * Checks if there support from KIO::PreviewJob for the currently
633 * shown files, taking mimeFilter() and nameFilter() into account
634 * Enables/disables the preview-action accordingly.
636 bool checkPreviewSupport();
639 * Called upon right-click to activate the popupmenu.
641 virtual void activatedMenu(const KFileItem
&item
, const QPoint
&pos
);
643 virtual void changeEvent(QEvent
*event
);
645 virtual bool eventFilter(QObject
*watched
, QEvent
*event
);
649 * Goes one step back in the history and opens that url.
654 * Goes one step forward in the history and opens that url.
656 virtual void forward();
659 * Enters the home directory.
664 * Goes one directory up from the current url.
669 * to update the view after changing the settings
674 * Re-reads the current url.
676 virtual void rereadDir();
679 * Opens a dialog to create a new directory.
681 virtual void mkdir();
684 * Deletes the currently selected files/directories.
686 virtual void deleteSelected();
689 * Enables/disables actions that are selection dependent. Call this e.g.
690 * when you are about to show a popup menu using some of KDirOperators
693 void updateSelectionDependentActions();
696 * Tries to complete the given string (only completes files).
698 QString
makeCompletion(const QString
&);
701 * Tries to complete the given string (only completes directores).
703 QString
makeDirCompletion(const QString
&);
706 * Trashes the currently selected files/directories.
708 * This function used to take activation reason and keyboard modifiers,
709 * in order to call deleteSelected() if the user wanted to delete.
710 * Instead, call deleteSelected().
712 * FIXME KAction Port: link deleteSelected() up correctly
714 virtual void trashSelected();
717 * Notifies that the icons size should change. @p value is an int ranged from 0 to 100.
718 * 100 means KIconLoader::SizeEnormous.
721 void setIconsZoom(int value
);
725 * Restores the normal cursor after showing the busy-cursor. Also hides
731 * Called after setUrl() to load the directory, update the history,
737 * Enters the directory specified by the given @p item.
739 virtual void selectDir(const KFileItem
&item
);
742 * Emits fileSelected( item )
744 void selectFile(const KFileItem
&item
);
747 * Emits fileHighlighted(item)
749 void highlightFile(const KFileItem
&item
);
752 * Changes sorting to sort by name
757 * Changes sorting to sort by size
762 * Changes sorting to sort by date
767 * Changes sorting to sort by date
772 * Changes sorting to reverse sorting
777 * Toggles showing directories first / having them sorted like files.
779 void toggleDirsFirst();
782 * Toggles case sensitive / case insensitive sorting
784 void toggleIgnoreCase();
787 * Tries to make the given @p match as current item in the view and emits
788 * completion( match )
790 void slotCompletionMatch(const QString
&match
);
793 void urlEntered(const KUrl
&);
794 void updateInformation(int files
, int dirs
);
795 void completion(const QString
&);
796 void finishedLoading();
799 * Emitted whenever the current fileview is changed, either by an explicit
800 * call to setView() or by the user selecting a different view thru
803 void viewChanged(QAbstractItemView
*newView
);
806 * Emitted when a file is highlighted or generally the selection changes in
807 * multiselection mode. In the latter case, @p item is 0L. You can access
808 * the selected items with selectedItems().
810 void fileHighlighted(const KFileItem
&item
);
811 void dirActivated(const KFileItem
&item
);
812 void fileSelected(const KFileItem
&item
);
814 * Emitted when files are dropped. Dropping files is disabled by
815 * default. You need to enable it with setAcceptDrops()
816 * @param item the item on which the drop occurred or 0.
817 * @param event the drop event itself.
818 * @param urls the urls that where dropped.
820 void dropped(const KFileItem
&item
, QDropEvent
*event
, const KUrl::List
&urls
);
823 * Emitted just before the context menu is shown, allows users to
824 * extend the menu with custom actions.
826 * @param item the file on which the context menu was invoked
827 * @param menu the context menu, pre-populated with the file-management actions
830 void contextMenuAboutToShow(const KFileItem
& item
, QMenu
* menu
);
833 * Will notify that the icon size has changed. Since we save the icon size depending
834 * on the view type (list view or a different kind of view), a call to setView() can
835 * trigger this signal to be emitted.
838 void currentIconSizeChanged(int size
);
844 Q_PRIVATE_SLOT( d
, void _k_slotDetailedView() )
845 Q_PRIVATE_SLOT( d
, void _k_slotSimpleView() )
846 Q_PRIVATE_SLOT( d
, void _k_slotTreeView() )
847 Q_PRIVATE_SLOT( d
, void _k_slotDetailedTreeView() )
848 Q_PRIVATE_SLOT( d
, void _k_slotToggleHidden(bool) )
849 Q_PRIVATE_SLOT( d
, void _k_togglePreview(bool) )
850 Q_PRIVATE_SLOT( d
, void _k_toggleInlinePreviews(bool) )
851 Q_PRIVATE_SLOT( d
, void _k_slotSortByName() )
852 Q_PRIVATE_SLOT( d
, void _k_slotSortBySize() )
853 Q_PRIVATE_SLOT( d
, void _k_slotSortByDate() )
854 Q_PRIVATE_SLOT( d
, void _k_slotSortByType() )
855 Q_PRIVATE_SLOT( d
, void _k_slotSortReversed(bool) )
856 Q_PRIVATE_SLOT( d
, void _k_slotToggleDirsFirst() )
857 Q_PRIVATE_SLOT( d
, void _k_slotToggleIgnoreCase() )
858 Q_PRIVATE_SLOT( d
, void _k_slotStarted() )
859 Q_PRIVATE_SLOT( d
, void _k_slotProgress(int) )
860 Q_PRIVATE_SLOT( d
, void _k_slotShowProgress() )
861 Q_PRIVATE_SLOT( d
, void _k_slotIOFinished() )
862 Q_PRIVATE_SLOT( d
, void _k_slotCanceled() )
863 Q_PRIVATE_SLOT( d
, void _k_slotRedirected(const KUrl
&) )
864 Q_PRIVATE_SLOT( d
, void _k_slotProperties() )
865 Q_PRIVATE_SLOT( d
, void _k_slotPressed(const QModelIndex
&) )
866 Q_PRIVATE_SLOT( d
, void _k_slotActivated(const QModelIndex
&) )
867 Q_PRIVATE_SLOT( d
, void _k_slotDoubleClicked(const QModelIndex
&) )
868 Q_PRIVATE_SLOT( d
, void _k_slotSelectionChanged() )
869 Q_PRIVATE_SLOT( d
, void _k_openContextMenu(const QPoint
&) )
870 Q_PRIVATE_SLOT( d
, void _k_triggerPreview(const QModelIndex
&) )
871 Q_PRIVATE_SLOT( d
, void _k_showPreview() )
872 Q_PRIVATE_SLOT( d
, void _k_slotSplitterMoved(int, int) )
873 Q_PRIVATE_SLOT( d
, void _k_assureVisibleSelection() )
874 Q_PRIVATE_SLOT( d
, void _k_synchronizeSortingState(int, Qt::SortOrder
) )
875 Q_PRIVATE_SLOT( d
, void _k_slotChangeDecorationPosition() )
876 Q_PRIVATE_SLOT( d
, void _k_slotExpandToUrl(const QModelIndex
&) )