1 /***************************************************************************
2 * Copyright (C) 2006-2009 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
24 #include <config-baloo.h>
26 #include "libdolphin_export.h"
28 #include <kparts/part.h>
30 #include <kio/fileundomanager.h>
36 typedef KIO::FileUndoManager::CommandType CommandType
;
38 class DolphinItemListView
;
40 class KItemListContainer
;
45 class VersionControlObserver
;
47 class QGraphicsSceneDragDropEvent
;
51 * @short Represents a view for the directory content.
53 * View modes for icons, compact and details are supported. It's
61 class LIBDOLPHINPRIVATE_EXPORT DolphinView
: public QWidget
67 * Defines the view mode for a directory. The
68 * view mode is automatically updated if the directory itself
69 * defines a view mode (see class ViewProperties for details).
74 * The items are shown as icons with a name-label below.
79 * The icon, the name and the size of the items are
80 * shown per default as a table.
85 * The items are shown as icons with the name-label aligned
92 * @param url Specifies the content which should be shown.
93 * @param parent Parent widget of the view.
95 DolphinView(const KUrl
& url
, QWidget
* parent
);
97 virtual ~DolphinView();
100 * Returns the current active URL, where all actions are applied.
101 * The URL navigator is synchronized with this URL.
106 * If \a active is true, the view will marked as active. The active
107 * view is defined as view where all actions are applied to.
109 void setActive(bool active
);
110 bool isActive() const;
113 * Changes the view mode for the current directory to \a mode.
114 * If the view properties should be remembered for each directory
115 * (GeneralSettings::globalViewProps() returns false), then the
116 * changed view mode will be stored automatically.
118 void setMode(Mode mode
);
122 * Turns on the file preview for the all files of the current directory,
123 * if \a show is true.
124 * If the view properties should be remembered for each directory
125 * (GeneralSettings::globalViewProps() returns false), then the
126 * preview setting will be stored automatically.
128 void setPreviewsShown(bool show
);
129 bool previewsShown() const;
132 * Shows all hidden files of the current directory,
133 * if \a show is true.
134 * If the view properties should be remembered for each directory
135 * (GeneralSettings::globalViewProps() returns false), then the
136 * show hidden file setting will be stored automatically.
138 void setHiddenFilesShown(bool show
);
139 bool hiddenFilesShown() const;
142 * Turns on sorting by groups if \a enable is true.
144 void setGroupedSorting(bool grouped
);
145 bool groupedSorting() const;
148 * Returns the items of the view.
150 KFileItemList
items() const;
153 * @return The number of items. itemsCount() is faster in comparison
154 * to items().count().
156 int itemsCount() const;
159 * Returns the selected items. The list is empty if no item has been
162 KFileItemList
selectedItems() const;
165 * Returns the number of selected items (this is faster than
166 * invoking selectedItems().count()).
168 int selectedItemsCount() const;
171 * Marks the items indicated by \p urls to get selected after the
172 * directory DolphinView::url() has been loaded. Note that nothing
173 * gets selected if no loading of a directory has been triggered
174 * by DolphinView::setUrl() or DolphinView::reload().
176 void markUrlsAsSelected(const QList
<KUrl
>& urls
);
179 * Marks the item indicated by \p url to be scrolled to and as the
180 * current item after directory DolphinView::url() has been loaded.
182 void markUrlAsCurrent(const KUrl
& url
);
185 * All items that match to the pattern \a pattern will get selected
186 * if \a enabled is true and deselected if \a enabled is false.
188 void selectItems(const QRegExp
& pattern
, bool enabled
);
191 * Sets the zoom level to \a level. It is assured that the used
192 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
193 * ZoomLevelInfo::maximumLevel().
195 void setZoomLevel(int level
);
196 int zoomLevel() const;
198 void setSortRole(const QByteArray
& role
);
199 QByteArray
sortRole() const;
201 void setSortOrder(Qt::SortOrder order
);
202 Qt::SortOrder
sortOrder() const;
204 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
205 void setSortFoldersFirst(bool foldersFirst
);
206 bool sortFoldersFirst() const;
208 /** Sets the additional information which should be shown for the items. */
209 void setVisibleRoles(const QList
<QByteArray
>& roles
);
211 /** Returns the additional information which should be shown for the items. */
212 QList
<QByteArray
> visibleRoles() const;
217 * Refreshes the view to get synchronized with the settings (e.g. icons size,
223 * Saves the current settings (e.g. icons size, font, ..).
225 void writeSettings();
228 * Filters the currently shown items by \a nameFilter. All items
229 * which contain the given filter string will be shown.
231 void setNameFilter(const QString
& nameFilter
);
232 QString
nameFilter() const;
235 * Filters the currently shown items by \a filters. All items
236 * whose content-type matches those given by the list of filters
239 void setMimeTypeFilters(const QStringList
& filters
);
240 QStringList
mimeTypeFilters() const;
243 * Returns a textual representation of the state of the current
244 * folder or selected items, suitable for use in the status bar.
246 QString
statusBarText() const;
249 * Returns the version control actions that are provided for the items \p items.
250 * Usually the actions are presented in the context menu.
252 QList
<QAction
*> versionControlActions(const KFileItemList
& items
) const;
255 * Returns the state of the paste action:
256 * first is whether the action should be enabled
257 * second is the text for the action
259 QPair
<bool, QString
> pasteInfo() const;
262 * If \a tabsForFiles is true, the signal tabRequested() will also
263 * emitted also for files. Per default tabs for files is disabled
264 * and hence the signal tabRequested() will only be emitted for
267 void setTabsForFilesEnabled(bool tabsForFiles
);
268 bool isTabsForFilesEnabled() const;
271 * Returns true if the current view allows folders to be expanded,
272 * i.e. presents a hierarchical view to the user.
274 bool itemsExpandable() const;
277 * Restores the view state (current item, contents position, details view expansion state)
279 void restoreState(QDataStream
& stream
);
282 * Saves the view state (current item, contents position, details view expansion state)
284 void saveState(QDataStream
& stream
);
287 * Returns the root item which represents the current URL.
289 KFileItem
rootItem() const;
292 * Sets a context that is used for remembering the view-properties.
293 * Per default the context is empty and the path of the currently set URL
294 * is used for remembering the view-properties. Setting a custom context
295 * makes sense if specific types of URLs (e.g. search-URLs) should
296 * share common view-properties.
298 void setViewPropertiesContext(const QString
& context
);
299 QString
viewPropertiesContext() const;
302 * Checks if the given \a item can be opened as folder (e.g. archives).
303 * This function will also adjust the \a url (e.g. change the protocol).
304 * @return a valid and adjusted url if the item can be opened as folder,
305 * otherwise return an empty url.
307 static KUrl
openItemAsFolderUrl(const KFileItem
& item
, const bool browseThroughArchives
= true);
311 * Changes the directory to \a url. If the current directory is equal to
312 * \a url, nothing will be done (use DolphinView::reload() instead).
314 void setUrl(const KUrl
& url
);
318 * @see DolphinView::selectedItems()
323 * Inverts the current selection: selected items get unselected,
324 * unselected items get selected.
325 * @see DolphinView::selectedItems()
327 void invertSelection();
329 void clearSelection();
332 * Triggers the renaming of the currently selected items, where
333 * the user must input a new name for the items.
335 void renameSelectedItems();
338 * Moves all selected items to the trash.
340 void trashSelectedItems();
343 * Deletes all selected items.
345 void deleteSelectedItems();
348 * Copies all selected items to the clipboard and marks
351 void cutSelectedItems();
353 /** Copies all selected items to the clipboard. */
354 void copySelectedItems();
356 /** Pastes the clipboard data to this view. */
360 * Pastes the clipboard data into the currently selected
361 * folder. If the current selection is not exactly one folder, no
362 * paste operation is done.
364 void pasteIntoFolder();
368 /** Activates the view if the item list container gets focus. */
369 virtual bool eventFilter(QObject
* watched
, QEvent
* event
);
373 * Is emitted if the view has been activated by e. g. a mouse click.
378 * Is emitted if the URL of the view will be changed to \a url.
379 * After the URL has been changed the signal urlChanged() will
382 void urlAboutToBeChanged(const KUrl
& url
);
384 /** Is emitted if the URL of the view has been changed to \a url. */
385 void urlChanged(const KUrl
& url
);
388 * Is emitted when clicking on an item with the left mouse button.
390 void itemActivated(const KFileItem
& item
);
393 * Is emitted when multiple items have been activated by e. g.
394 * context menu open with.
396 void itemsActivated(const KFileItemList
& items
);
399 * Is emitted if items have been added or deleted.
401 void itemCountChanged();
404 * Is emitted if a new tab should be opened for the URL \a url.
406 void tabRequested(const KUrl
& url
);
409 * Is emitted if the view mode (IconsView, DetailsView,
410 * PreviewsView) has been changed.
412 void modeChanged(DolphinView::Mode current
, DolphinView::Mode previous
);
414 /** Is emitted if the 'show preview' property has been changed. */
415 void previewsShownChanged(bool shown
);
417 /** Is emitted if the 'show hidden files' property has been changed. */
418 void hiddenFilesShownChanged(bool shown
);
420 /** Is emitted if the 'grouped sorting' property has been changed. */
421 void groupedSortingChanged(bool groupedSorting
);
423 /** Is emitted if the sorting by name, size or date has been changed. */
424 void sortRoleChanged(const QByteArray
& role
);
426 /** Is emitted if the sort order (ascending or descending) has been changed. */
427 void sortOrderChanged(Qt::SortOrder order
);
430 * Is emitted if the sorting of files and folders (separate with folders
431 * first or mixed) has been changed.
433 void sortFoldersFirstChanged(bool foldersFirst
);
435 /** Is emitted if the additional information shown for this view has been changed. */
436 void visibleRolesChanged(const QList
<QByteArray
>& current
,
437 const QList
<QByteArray
>& previous
);
439 /** Is emitted if the zoom level has been changed by zooming in or out. */
440 void zoomLevelChanged(int current
, int previous
);
443 * Is emitted if information of an item is requested to be shown e. g. in the panel.
444 * If item is null, no item information request is pending.
446 void requestItemInfo(const KFileItem
& item
);
449 * Is emitted whenever the selection has been changed.
451 void selectionChanged(const KFileItemList
& selection
);
454 * Is emitted if a context menu is requested for the item \a item,
455 * which is part of \a url. If the item is null, the context menu
456 * for the URL should be shown and the custom actions \a customActions
459 void requestContextMenu(const QPoint
& pos
,
460 const KFileItem
& item
,
462 const QList
<QAction
*>& customActions
);
465 * Is emitted if an information message with the content \a msg
468 void infoMessage(const QString
& msg
);
471 * Is emitted if an error message with the content \a msg
474 void errorMessage(const QString
& msg
);
477 * Is emitted if an "operation completed" message with the content \a msg
480 void operationCompletedMessage(const QString
& msg
);
483 * Is emitted after DolphinView::setUrl() has been invoked and
484 * the current directory is loaded. If this signal is emitted,
485 * it is assured that the view contains already the correct root
486 * URL and property settings.
488 void directoryLoadingStarted();
491 * Is emitted after the directory triggered by DolphinView::setUrl()
494 void directoryLoadingCompleted();
497 * Is emitted after the directory loading triggered by DolphinView::setUrl()
500 void directoryLoadingCanceled();
503 * Is emitted after DolphinView::setUrl() has been invoked and provides
504 * the information how much percent of the current directory have been loaded.
506 void directoryLoadingProgress(int percent
);
509 * Is emitted if the sorting is done asynchronously and provides the
510 * progress information of the sorting.
512 void directorySortingProgress(int percent
);
515 * Emitted when the file-item-model emits redirection.
516 * Testcase: fish://localhost
518 void redirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
521 * Is emitted when the URL set by DolphinView::setUrl() represents a file.
522 * In this case no signal errorMessage() will be emitted.
524 void urlIsFileError(const KUrl
& url
);
527 * Is emitted when the write state of the folder has been changed. The application
528 * should disable all actions like "Create New..." that depend on the write
531 void writeStateChanged(bool isFolderWritable
);
534 * Is emitted if the URL should be changed to the previous URL of the
535 * history (e.g. because the "back"-mousebutton has been pressed).
537 void goBackRequested();
540 * Is emitted if the URL should be changed to the next URL of the
541 * history (e.g. because the "next"-mousebutton has been pressed).
543 void goForwardRequested();
546 /** Changes the zoom level if Control is pressed during a wheel event. */
547 virtual void wheelEvent(QWheelEvent
* event
);
550 virtual void hideEvent(QHideEvent
* event
);
551 virtual bool event(QEvent
* event
);
555 * Marks the view as active (DolphinView:isActive() will return true)
556 * and emits the 'activated' signal if it is not already active.
560 void slotItemActivated(int index
);
561 void slotItemsActivated(const KItemSet
& indexes
);
562 void slotItemMiddleClicked(int index
);
563 void slotItemContextMenuRequested(int index
, const QPointF
& pos
);
564 void slotViewContextMenuRequested(const QPointF
& pos
);
565 void slotHeaderContextMenuRequested(const QPointF
& pos
);
566 void slotHeaderColumnWidthChanged(const QByteArray
& role
, qreal current
, qreal previous
);
567 void slotItemHovered(int index
);
568 void slotItemUnhovered(int index
);
569 void slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
);
570 void slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
);
571 void slotMouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
);
574 * Is called when new items get pasted or dropped.
576 void slotAboutToCreate(const KUrl::List
& urls
);
579 * Emits the signal \a selectionChanged() with a small delay. This is
580 * because getting all file items for the selection can be an expensive
581 * operation. Fast selection changes are collected in this case and
582 * the signal is emitted only after no selection change has been done
583 * within a small delay.
585 void slotSelectionChanged(const KItemSet
& current
, const KItemSet
& previous
);
588 * Is called by emitDelayedSelectionChangedSignal() and emits the
589 * signal \a selectionChanged() with all selected file items as parameter.
591 void emitSelectionChangedSignal();
594 * Updates the view properties of the current URL to the
595 * sorting given by \a role.
597 void updateSortRole(const QByteArray
& role
);
600 * Updates the view properties of the current URL to the
601 * sort order given by \a order.
603 void updateSortOrder(Qt::SortOrder order
);
606 * Updates the view properties of the current URL to the
607 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
609 void updateSortFoldersFirst(bool foldersFirst
);
612 * Indicates in the status bar that the delete operation
613 * of the job \a job has been finished.
615 void slotDeleteFileFinished(KJob
* job
);
618 * Indicates in the status bar that the trash operation
619 * of the job \a job has been finished.
621 void slotTrashFileFinished(KJob
* job
);
624 * Invoked when the rename job is done, for error handling.
626 void slotRenamingResult(KJob
* job
);
629 * Invoked when the file item model has started the loading
630 * of the directory specified by DolphinView::url().
632 void slotDirectoryLoadingStarted();
635 * Invoked when the file item model indicates that the loading of a directory has
636 * been completed. Assures that pasted items and renamed items get seleced.
638 void slotDirectoryLoadingCompleted();
641 * Is invoked when items of KFileItemModel have been changed.
643 void slotItemsChanged();
646 * Is invoked when the sort order has been changed by the user by clicking
647 * on a header item. The view properties of the directory will get updated.
649 void slotSortOrderChangedByHeader(Qt::SortOrder current
, Qt::SortOrder previous
);
652 * Is invoked when the sort role has been changed by the user by clicking
653 * on a header item. The view properties of the directory will get updated.
655 void slotSortRoleChangedByHeader(const QByteArray
& current
, const QByteArray
& previous
);
658 * Is invoked when the visible roles have been changed by the user by dragging
659 * a header item. The view properties of the directory will get updated.
661 void slotVisibleRolesChangedByHeader(const QList
<QByteArray
>& current
,
662 const QList
<QByteArray
>& previous
);
664 void slotRoleEditingCanceled();
665 void slotRoleEditingFinished(int index
, const QByteArray
& role
, const QVariant
& value
);
668 * Observes the item with the URL \a url. As soon as the directory
669 * model indicates that the item is available, the item will
670 * get selected and it is assured that the item stays visible.
672 void observeCreatedItem(const KUrl
& url
);
675 * Called when a redirection happens.
676 * Testcase: fish://localhost
678 void slotDirectoryRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
681 * Applies the state that has been restored by restoreViewState()
684 void updateViewState();
689 * Calculates the number of currently shown files into
690 * \a fileCount and the number of folders into \a folderCount.
691 * The size of all files is written into \a totalFileSize.
692 * It is recommend using this method instead of asking the
693 * directory lister or the model directly, as it takes
694 * filtering and hierarchical previews into account.
696 void calculateItemCount(int& fileCount
, int& folderCount
, KIO::filesize_t
& totalFileSize
) const;
699 void loadDirectory(const KUrl
& url
, bool reload
= false);
702 * Applies the view properties which are defined by the current URL
703 * to the DolphinView properties. The view properties are read from a
704 * .directory file either in the current directory, or in the
705 * share/apps/dolphin/view_properties/ subfolder of the user's .kde folder.
707 void applyViewProperties();
710 * Applies the given view properties to the DolphinView.
712 void applyViewProperties(const ViewProperties
& props
);
715 * Applies the m_mode property to the corresponding
716 * itemlayout-property of the KItemListView.
718 void applyModeToView();
721 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
722 * Pastes the clipboard data into the URL \a url.
724 void pasteToUrl(const KUrl
& url
);
727 * Returns a list of URLs for all selected items. The list is
728 * simplified, so that when the URLs are part of different tree
729 * levels, only the parent is returned.
731 KUrl::List
simplifiedSelectedUrls() const;
734 * Returns the MIME data for all selected items.
736 QMimeData
* selectionMimeData() const;
739 * Updates m_isFolderWritable dependent on whether the folder represented by
740 * the current URL is writable. If the state has changed, the signal
741 * writeableStateChanged() will be emitted.
743 void updateWritableState();
746 * @return The current URL if no viewproperties-context is given (see
747 * DolphinView::viewPropertiesContext(), otherwise the context
750 KUrl
viewPropertiesUrl() const;
755 bool m_assureVisibleCurrentIndex
;
756 bool m_isFolderWritable
;
757 bool m_dragging
; // True if a dragging is done. Required to be able to decide whether a
758 // tooltip may be shown when hovering an item.
761 QString m_viewPropertiesContext
;
763 QList
<QByteArray
> m_visibleRoles
;
765 QVBoxLayout
* m_topLayout
;
767 KFileItemModel
* m_model
;
768 DolphinItemListView
* m_view
;
769 KItemListContainer
* m_container
;
771 ToolTipManager
* m_toolTipManager
;
773 QTimer
* m_selectionChangedTimer
;
775 KUrl m_currentItemUrl
; // Used for making the view to remember the current URL after F5
776 bool m_scrollToCurrentItem
; // Used for marking we need to scroll to current item or not
777 QPoint m_restoredContentsPosition
;
779 QList
<KUrl
> m_selectedUrls
; // Used for making the view to remember selections after F5
780 bool m_clearSelectionBeforeSelectingNewItems
;
781 bool m_markFirstNewlySelectedItemAsCurrent
;
783 VersionControlObserver
* m_versionControlObserver
;
786 friend class TestBase
;
787 friend class DolphinDetailsViewTest
;
788 friend class DolphinPart
; // Accesses m_model
791 /// Allow using DolphinView::Mode in QVariant
792 Q_DECLARE_METATYPE(DolphinView::Mode
)
794 #endif // DOLPHINVIEW_H