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
;
44 class VersionControlObserver
;
46 class QGraphicsSceneDragDropEvent
;
50 * @short Represents a view for the directory content.
52 * View modes for icons, compact and details are supported. It's
60 class LIBDOLPHINPRIVATE_EXPORT DolphinView
: public QWidget
66 * Defines the view mode for a directory. The
67 * view mode is automatically updated if the directory itself
68 * defines a view mode (see class ViewProperties for details).
73 * The items are shown as icons with a name-label below.
78 * The icon, the name and the size of the items are
79 * shown per default as a table.
84 * The items are shown as icons with the name-label aligned
91 * @param url Specifies the content which should be shown.
92 * @param parent Parent widget of the view.
94 DolphinView(const QUrl
& url
, QWidget
* parent
);
96 virtual ~DolphinView();
99 * Returns the current active URL, where all actions are applied.
100 * The URL navigator is synchronized with this URL.
105 * If \a active is true, the view will marked as active. The active
106 * view is defined as view where all actions are applied to.
108 void setActive(bool active
);
109 bool isActive() const;
112 * Changes the view mode for the current directory to \a mode.
113 * If the view properties should be remembered for each directory
114 * (GeneralSettings::globalViewProps() returns false), then the
115 * changed view mode will be stored automatically.
117 void setMode(Mode mode
);
121 * Turns on the file preview for the all files of the current directory,
122 * if \a show is true.
123 * If the view properties should be remembered for each directory
124 * (GeneralSettings::globalViewProps() returns false), then the
125 * preview setting will be stored automatically.
127 void setPreviewsShown(bool show
);
128 bool previewsShown() const;
131 * Shows all hidden files of the current directory,
132 * if \a show is true.
133 * If the view properties should be remembered for each directory
134 * (GeneralSettings::globalViewProps() returns false), then the
135 * show hidden file setting will be stored automatically.
137 void setHiddenFilesShown(bool show
);
138 bool hiddenFilesShown() const;
141 * Turns on sorting by groups if \a enable is true.
143 void setGroupedSorting(bool grouped
);
144 bool groupedSorting() const;
147 * Returns the items of the view.
149 KFileItemList
items() const;
152 * @return The number of items. itemsCount() is faster in comparison
153 * to items().count().
155 int itemsCount() const;
158 * Returns the selected items. The list is empty if no item has been
161 KFileItemList
selectedItems() const;
164 * Returns the number of selected items (this is faster than
165 * invoking selectedItems().count()).
167 int selectedItemsCount() const;
170 * Marks the items indicated by \p urls to get selected after the
171 * directory DolphinView::url() has been loaded. Note that nothing
172 * gets selected if no loading of a directory has been triggered
173 * by DolphinView::setUrl() or DolphinView::reload().
175 void markUrlsAsSelected(const QList
<QUrl
> &urls
);
178 * Marks the item indicated by \p url to be scrolled to and as the
179 * current item after directory DolphinView::url() has been loaded.
181 void markUrlAsCurrent(const QUrl
& url
);
184 * All items that match to the pattern \a pattern will get selected
185 * if \a enabled is true and deselected if \a enabled is false.
187 void selectItems(const QRegExp
& pattern
, bool enabled
);
190 * Sets the zoom level to \a level. It is assured that the used
191 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
192 * ZoomLevelInfo::maximumLevel().
194 void setZoomLevel(int level
);
195 int zoomLevel() const;
197 void setSortRole(const QByteArray
& role
);
198 QByteArray
sortRole() const;
200 void setSortOrder(Qt::SortOrder order
);
201 Qt::SortOrder
sortOrder() const;
203 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
204 void setSortFoldersFirst(bool foldersFirst
);
205 bool sortFoldersFirst() const;
207 /** Sets the additional information which should be shown for the items. */
208 void setVisibleRoles(const QList
<QByteArray
>& roles
);
210 /** Returns the additional information which should be shown for the items. */
211 QList
<QByteArray
> visibleRoles() const;
216 * Refreshes the view to get synchronized with the settings (e.g. icons size,
222 * Saves the current settings (e.g. icons size, font, ..).
224 void writeSettings();
227 * Filters the currently shown items by \a nameFilter. All items
228 * which contain the given filter string will be shown.
230 void setNameFilter(const QString
& nameFilter
);
231 QString
nameFilter() const;
234 * Filters the currently shown items by \a filters. All items
235 * whose content-type matches those given by the list of filters
238 void setMimeTypeFilters(const QStringList
& filters
);
239 QStringList
mimeTypeFilters() const;
242 * Returns a textual representation of the state of the current
243 * folder or selected items, suitable for use in the status bar.
245 QString
statusBarText() const;
248 * Returns the version control actions that are provided for the items \p items.
249 * Usually the actions are presented in the context menu.
251 QList
<QAction
*> versionControlActions(const KFileItemList
& items
) const;
254 * Returns the state of the paste action:
255 * first is whether the action should be enabled
256 * second is the text for the action
258 QPair
<bool, QString
> pasteInfo() const;
261 * If \a tabsForFiles is true, the signal tabRequested() will also
262 * emitted also for files. Per default tabs for files is disabled
263 * and hence the signal tabRequested() will only be emitted for
266 void setTabsForFilesEnabled(bool tabsForFiles
);
267 bool isTabsForFilesEnabled() const;
270 * Returns true if the current view allows folders to be expanded,
271 * i.e. presents a hierarchical view to the user.
273 bool itemsExpandable() const;
276 * Restores the view state (current item, contents position, details view expansion state)
278 void restoreState(QDataStream
& stream
);
281 * Saves the view state (current item, contents position, details view expansion state)
283 void saveState(QDataStream
& stream
);
286 * Returns the root item which represents the current URL.
288 KFileItem
rootItem() const;
291 * Sets a context that is used for remembering the view-properties.
292 * Per default the context is empty and the path of the currently set URL
293 * is used for remembering the view-properties. Setting a custom context
294 * makes sense if specific types of URLs (e.g. search-URLs) should
295 * share common view-properties.
297 void setViewPropertiesContext(const QString
& context
);
298 QString
viewPropertiesContext() const;
301 * Checks if the given \a item can be opened as folder (e.g. archives).
302 * This function will also adjust the \a url (e.g. change the protocol).
303 * @return a valid and adjusted url if the item can be opened as folder,
304 * otherwise return an empty url.
306 static QUrl
openItemAsFolderUrl(const KFileItem
& item
, const bool browseThroughArchives
= true);
310 * Changes the directory to \a url. If the current directory is equal to
311 * \a url, nothing will be done (use DolphinView::reload() instead).
313 void setUrl(const QUrl
& url
);
317 * @see DolphinView::selectedItems()
322 * Inverts the current selection: selected items get unselected,
323 * unselected items get selected.
324 * @see DolphinView::selectedItems()
326 void invertSelection();
328 void clearSelection();
331 * Triggers the renaming of the currently selected items, where
332 * the user must input a new name for the items.
334 void renameSelectedItems();
337 * Moves all selected items to the trash.
339 void trashSelectedItems();
342 * Deletes all selected items.
344 void deleteSelectedItems();
347 * Copies all selected items to the clipboard and marks
350 void cutSelectedItems();
352 /** Copies all selected items to the clipboard. */
353 void copySelectedItems();
355 /** Pastes the clipboard data to this view. */
359 * Pastes the clipboard data into the currently selected
360 * folder. If the current selection is not exactly one folder, no
361 * paste operation is done.
363 void pasteIntoFolder();
367 /** Activates the view if the item list container gets focus. */
368 virtual bool eventFilter(QObject
* watched
, QEvent
* event
);
372 * Is emitted if the view has been activated by e. g. a mouse click.
377 * Is emitted if the URL of the view will be changed to \a url.
378 * After the URL has been changed the signal urlChanged() will
381 void urlAboutToBeChanged(const QUrl
& url
);
383 /** Is emitted if the URL of the view has been changed to \a url. */
384 void urlChanged(const QUrl
& url
);
387 * Is emitted when clicking on an item with the left mouse button.
389 void itemActivated(const KFileItem
& item
);
392 * Is emitted when multiple items have been activated by e. g.
393 * context menu open with.
395 void itemsActivated(const KFileItemList
& items
);
398 * Is emitted if items have been added or deleted.
400 void itemCountChanged();
403 * Is emitted if a new tab should be opened for the URL \a url.
405 void tabRequested(const QUrl
& url
);
408 * Is emitted if the view mode (IconsView, DetailsView,
409 * PreviewsView) has been changed.
411 void modeChanged(DolphinView::Mode current
, DolphinView::Mode previous
);
413 /** Is emitted if the 'show preview' property has been changed. */
414 void previewsShownChanged(bool shown
);
416 /** Is emitted if the 'show hidden files' property has been changed. */
417 void hiddenFilesShownChanged(bool shown
);
419 /** Is emitted if the 'grouped sorting' property has been changed. */
420 void groupedSortingChanged(bool groupedSorting
);
422 /** Is emitted if the sorting by name, size or date has been changed. */
423 void sortRoleChanged(const QByteArray
& role
);
425 /** Is emitted if the sort order (ascending or descending) has been changed. */
426 void sortOrderChanged(Qt::SortOrder order
);
429 * Is emitted if the sorting of files and folders (separate with folders
430 * first or mixed) has been changed.
432 void sortFoldersFirstChanged(bool foldersFirst
);
434 /** Is emitted if the additional information shown for this view has been changed. */
435 void visibleRolesChanged(const QList
<QByteArray
>& current
,
436 const QList
<QByteArray
>& previous
);
438 /** Is emitted if the zoom level has been changed by zooming in or out. */
439 void zoomLevelChanged(int current
, int previous
);
442 * Is emitted if information of an item is requested to be shown e. g. in the panel.
443 * If item is null, no item information request is pending.
445 void requestItemInfo(const KFileItem
& item
);
448 * Is emitted whenever the selection has been changed.
450 void selectionChanged(const KFileItemList
& selection
);
453 * Is emitted if a context menu is requested for the item \a item,
454 * which is part of \a url. If the item is null, the context menu
455 * for the URL should be shown and the custom actions \a customActions
458 void requestContextMenu(const QPoint
& pos
,
459 const KFileItem
& item
,
461 const QList
<QAction
*>& customActions
);
464 * Is emitted if an information message with the content \a msg
467 void infoMessage(const QString
& msg
);
470 * Is emitted if an error message with the content \a msg
473 void errorMessage(const QString
& msg
);
476 * Is emitted if an "operation completed" message with the content \a msg
479 void operationCompletedMessage(const QString
& msg
);
482 * Is emitted after DolphinView::setUrl() has been invoked and
483 * the current directory is loaded. If this signal is emitted,
484 * it is assured that the view contains already the correct root
485 * URL and property settings.
487 void directoryLoadingStarted();
490 * Is emitted after the directory triggered by DolphinView::setUrl()
493 void directoryLoadingCompleted();
496 * Is emitted after the directory loading triggered by DolphinView::setUrl()
499 void directoryLoadingCanceled();
502 * Is emitted after DolphinView::setUrl() has been invoked and provides
503 * the information how much percent of the current directory have been loaded.
505 void directoryLoadingProgress(int percent
);
508 * Is emitted if the sorting is done asynchronously and provides the
509 * progress information of the sorting.
511 void directorySortingProgress(int percent
);
514 * Emitted when the file-item-model emits redirection.
515 * Testcase: fish://localhost
517 void redirection(const QUrl
& oldUrl
, const QUrl
& newUrl
);
520 * Is emitted when the URL set by DolphinView::setUrl() represents a file.
521 * In this case no signal errorMessage() will be emitted.
523 void urlIsFileError(const QUrl
& url
);
526 * Is emitted when the write state of the folder has been changed. The application
527 * should disable all actions like "Create New..." that depend on the write
530 void writeStateChanged(bool isFolderWritable
);
533 * Is emitted if the URL should be changed to the previous URL of the
534 * history (e.g. because the "back"-mousebutton has been pressed).
536 void goBackRequested();
539 * Is emitted if the URL should be changed to the next URL of the
540 * history (e.g. because the "next"-mousebutton has been pressed).
542 void goForwardRequested();
545 /** Changes the zoom level if Control is pressed during a wheel event. */
546 virtual void wheelEvent(QWheelEvent
* event
);
549 virtual void hideEvent(QHideEvent
* event
);
550 virtual bool event(QEvent
* event
);
554 * Marks the view as active (DolphinView:isActive() will return true)
555 * and emits the 'activated' signal if it is not already active.
559 void slotItemActivated(int index
);
560 void slotItemsActivated(const KItemSet
& indexes
);
561 void slotItemMiddleClicked(int index
);
562 void slotItemContextMenuRequested(int index
, const QPointF
& pos
);
563 void slotViewContextMenuRequested(const QPointF
& pos
);
564 void slotHeaderContextMenuRequested(const QPointF
& pos
);
565 void slotHeaderColumnWidthChanged(const QByteArray
& role
, qreal current
, qreal previous
);
566 void slotItemHovered(int index
);
567 void slotItemUnhovered(int index
);
568 void slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
);
569 void slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
);
570 void slotMouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
);
573 * Is called when new items get pasted or dropped.
575 void slotAboutToCreate(const QList
<QUrl
> &urls
);
578 * Emits the signal \a selectionChanged() with a small delay. This is
579 * because getting all file items for the selection can be an expensive
580 * operation. Fast selection changes are collected in this case and
581 * the signal is emitted only after no selection change has been done
582 * within a small delay.
584 void slotSelectionChanged(const KItemSet
& current
, const KItemSet
& previous
);
587 * Is called by emitDelayedSelectionChangedSignal() and emits the
588 * signal \a selectionChanged() with all selected file items as parameter.
590 void emitSelectionChangedSignal();
593 * Updates the view properties of the current URL to the
594 * sorting given by \a role.
596 void updateSortRole(const QByteArray
& role
);
599 * Updates the view properties of the current URL to the
600 * sort order given by \a order.
602 void updateSortOrder(Qt::SortOrder order
);
605 * Updates the view properties of the current URL to the
606 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
608 void updateSortFoldersFirst(bool foldersFirst
);
611 * Indicates in the status bar that the delete operation
612 * of the job \a job has been finished.
614 void slotDeleteFileFinished(KJob
* job
);
617 * Indicates in the status bar that the trash operation
618 * of the job \a job has been finished.
620 void slotTrashFileFinished(KJob
* job
);
623 * Invoked when the rename job is done, for error handling.
625 void slotRenamingResult(KJob
* job
);
628 * Invoked when the file item model has started the loading
629 * of the directory specified by DolphinView::url().
631 void slotDirectoryLoadingStarted();
634 * Invoked when the file item model indicates that the loading of a directory has
635 * been completed. Assures that pasted items and renamed items get seleced.
637 void slotDirectoryLoadingCompleted();
640 * Is invoked when items of KFileItemModel have been changed.
642 void slotItemsChanged();
645 * Is invoked when the sort order has been changed by the user by clicking
646 * on a header item. The view properties of the directory will get updated.
648 void slotSortOrderChangedByHeader(Qt::SortOrder current
, Qt::SortOrder previous
);
651 * Is invoked when the sort role has been changed by the user by clicking
652 * on a header item. The view properties of the directory will get updated.
654 void slotSortRoleChangedByHeader(const QByteArray
& current
, const QByteArray
& previous
);
657 * Is invoked when the visible roles have been changed by the user by dragging
658 * a header item. The view properties of the directory will get updated.
660 void slotVisibleRolesChangedByHeader(const QList
<QByteArray
>& current
,
661 const QList
<QByteArray
>& previous
);
663 void slotRoleEditingCanceled();
664 void slotRoleEditingFinished(int index
, const QByteArray
& role
, const QVariant
& value
);
667 * Observes the item with the URL \a url. As soon as the directory
668 * model indicates that the item is available, the item will
669 * get selected and it is assured that the item stays visible.
671 void observeCreatedItem(const QUrl
&url
);
674 * Called when a redirection happens.
675 * Testcase: fish://localhost
677 void slotDirectoryRedirection(const QUrl
& oldUrl
, const QUrl
& newUrl
);
680 * Applies the state that has been restored by restoreViewState()
683 void updateViewState();
688 * Calculates the number of currently shown files into
689 * \a fileCount and the number of folders into \a folderCount.
690 * The size of all files is written into \a totalFileSize.
691 * It is recommend using this method instead of asking the
692 * directory lister or the model directly, as it takes
693 * filtering and hierarchical previews into account.
695 void calculateItemCount(int& fileCount
, int& folderCount
, KIO::filesize_t
& totalFileSize
) const;
698 void loadDirectory(const QUrl
& url
, bool reload
= false);
701 * Applies the view properties which are defined by the current URL
702 * to the DolphinView properties. The view properties are read from a
703 * .directory file either in the current directory, or in the
704 * share/apps/dolphin/view_properties/ subfolder of the user's .kde folder.
706 void applyViewProperties();
709 * Applies the given view properties to the DolphinView.
711 void applyViewProperties(const ViewProperties
& props
);
714 * Applies the m_mode property to the corresponding
715 * itemlayout-property of the KItemListView.
717 void applyModeToView();
720 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
721 * Pastes the clipboard data into the URL \a url.
723 void pasteToUrl(const QUrl
& url
);
726 * Returns a list of URLs for all selected items. The list is
727 * simplified, so that when the URLs are part of different tree
728 * levels, only the parent is returned.
730 QList
<QUrl
> simplifiedSelectedUrls() const;
733 * Returns the MIME data for all selected items.
735 QMimeData
* selectionMimeData() const;
738 * Updates m_isFolderWritable dependent on whether the folder represented by
739 * the current URL is writable. If the state has changed, the signal
740 * writeableStateChanged() will be emitted.
742 void updateWritableState();
745 * @return The current URL if no viewproperties-context is given (see
746 * DolphinView::viewPropertiesContext(), otherwise the context
749 QUrl
viewPropertiesUrl() const;
754 bool m_assureVisibleCurrentIndex
;
755 bool m_isFolderWritable
;
756 bool m_dragging
; // True if a dragging is done. Required to be able to decide whether a
757 // tooltip may be shown when hovering an item.
760 QString m_viewPropertiesContext
;
762 QList
<QByteArray
> m_visibleRoles
;
764 QVBoxLayout
* m_topLayout
;
766 KFileItemModel
* m_model
;
767 DolphinItemListView
* m_view
;
768 KItemListContainer
* m_container
;
770 ToolTipManager
* m_toolTipManager
;
772 QTimer
* m_selectionChangedTimer
;
774 QUrl m_currentItemUrl
; // Used for making the view to remember the current URL after F5
775 bool m_scrollToCurrentItem
; // Used for marking we need to scroll to current item or not
776 QPoint m_restoredContentsPosition
;
778 QList
<QUrl
> m_selectedUrls
; // Used for making the view to remember selections after F5
779 bool m_clearSelectionBeforeSelectingNewItems
;
780 bool m_markFirstNewlySelectedItemAsCurrent
;
782 VersionControlObserver
* m_versionControlObserver
;
785 friend class TestBase
;
786 friend class DolphinDetailsViewTest
;
787 friend class DolphinPart
; // Accesses m_model
790 /// Allow using DolphinView::Mode in QVariant
791 Q_DECLARE_METATYPE(DolphinView::Mode
)
793 #endif // DOLPHINVIEW_H