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 "dolphintabwidget.h"
25 #include "dolphin_export.h"
29 #include <config-baloo.h>
30 #include <kio/fileundomanager.h>
31 #include <kparts/part.h>
37 typedef KIO::FileUndoManager::CommandType CommandType
;
39 class DolphinItemListView
;
41 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 DOLPHIN_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 QUrl
& url
, QWidget
* parent
);
97 ~DolphinView() override
;
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
<QUrl
> &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 QUrl
& 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 QUrl
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 QUrl
& 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();
367 * Handles a drop of @p dropEvent onto widget @p dropWidget and destination @p destUrl
369 void dropUrls(const QUrl
&destUrl
, QDropEvent
*dropEvent
, QWidget
*dropWidget
);
373 /** Activates the view if the item list container gets focus. */
374 bool eventFilter(QObject
* watched
, QEvent
* event
) override
;
378 * Is emitted if the view has been activated by e. g. a mouse click.
382 /** Is emitted if the URL of the view has been changed to \a url. */
383 void urlChanged(const QUrl
& url
);
386 * Is emitted when clicking on an item with the left mouse button.
388 void itemActivated(const KFileItem
& item
);
391 * Is emitted when multiple items have been activated by e. g.
392 * context menu open with.
394 void itemsActivated(const KFileItemList
& items
);
397 * Is emitted if items have been added or deleted.
399 void itemCountChanged();
402 * Is emitted if a new tab should be opened for the URL \a url.
404 void tabRequested(const QUrl
& url
, DolphinTabWidget::TabPlacement tabPlacement
);
407 * Is emitted if the view mode (IconsView, DetailsView,
408 * PreviewsView) has been changed.
410 void modeChanged(DolphinView::Mode current
, DolphinView::Mode previous
);
412 /** Is emitted if the 'show preview' property has been changed. */
413 void previewsShownChanged(bool shown
);
415 /** Is emitted if the 'show hidden files' property has been changed. */
416 void hiddenFilesShownChanged(bool shown
);
418 /** Is emitted if the 'grouped sorting' property has been changed. */
419 void groupedSortingChanged(bool groupedSorting
);
421 /** Is emitted if the sorting by name, size or date has been changed. */
422 void sortRoleChanged(const QByteArray
& role
);
424 /** Is emitted if the sort order (ascending or descending) has been changed. */
425 void sortOrderChanged(Qt::SortOrder order
);
428 * Is emitted if the sorting of files and folders (separate with folders
429 * first or mixed) has been changed.
431 void sortFoldersFirstChanged(bool foldersFirst
);
433 /** Is emitted if the additional information shown for this view has been changed. */
434 void visibleRolesChanged(const QList
<QByteArray
>& current
,
435 const QList
<QByteArray
>& previous
);
437 /** Is emitted if the zoom level has been changed by zooming in or out. */
438 void zoomLevelChanged(int current
, int previous
);
441 * Is emitted if information of an item is requested to be shown e. g. in the panel.
442 * If item is null, no item information request is pending.
444 void requestItemInfo(const KFileItem
& item
);
447 * Is emitted whenever the selection has been changed.
449 void selectionChanged(const KFileItemList
& selection
);
452 * Is emitted if a context menu is requested for the item \a item,
453 * which is part of \a url. If the item is null, the context menu
454 * for the URL should be shown and the custom actions \a customActions
457 void requestContextMenu(const QPoint
& pos
,
458 const KFileItem
& item
,
460 const QList
<QAction
*>& customActions
);
463 * Is emitted if an information message with the content \a msg
466 void infoMessage(const QString
& msg
);
469 * Is emitted if an error message with the content \a msg
472 void errorMessage(const QString
& msg
);
475 * Is emitted if an "operation completed" message with the content \a msg
478 void operationCompletedMessage(const QString
& msg
);
481 * Is emitted after DolphinView::setUrl() has been invoked and
482 * the current directory is loaded. If this signal is emitted,
483 * it is assured that the view contains already the correct root
484 * URL and property settings.
486 void directoryLoadingStarted();
489 * Is emitted after the directory triggered by DolphinView::setUrl()
492 void directoryLoadingCompleted();
495 * Is emitted after the directory loading triggered by DolphinView::setUrl()
498 void directoryLoadingCanceled();
501 * Is emitted after DolphinView::setUrl() has been invoked and provides
502 * the information how much percent of the current directory have been loaded.
504 void directoryLoadingProgress(int percent
);
507 * Is emitted if the sorting is done asynchronously and provides the
508 * progress information of the sorting.
510 void directorySortingProgress(int percent
);
513 * Emitted when the file-item-model emits redirection.
514 * Testcase: fish://localhost
516 void redirection(const QUrl
& oldUrl
, const QUrl
& newUrl
);
519 * Is emitted when the URL set by DolphinView::setUrl() represents a file.
520 * In this case no signal errorMessage() will be emitted.
522 void urlIsFileError(const QUrl
& url
);
525 * Is emitted when the write state of the folder has been changed. The application
526 * should disable all actions like "Create New..." that depend on the write
529 void writeStateChanged(bool isFolderWritable
);
532 * Is emitted if the URL should be changed to the previous URL of the
533 * history (e.g. because the "back"-mousebutton has been pressed).
535 void goBackRequested();
538 * Is emitted if the URL should be changed to the next URL of the
539 * history (e.g. because the "next"-mousebutton has been pressed).
541 void goForwardRequested();
544 * Is emitted when the user wants to move the focus to another view.
546 void toggleActiveViewRequested();
549 * Is emitted when the user clicks a tag or a link
550 * in the metadata widget of a tooltip.
552 void urlActivated(const QUrl
& url
);
555 /** Changes the zoom level if Control is pressed during a wheel event. */
556 void wheelEvent(QWheelEvent
* event
) override
;
558 void hideEvent(QHideEvent
* event
) override
;
559 bool event(QEvent
* event
) override
;
563 * Marks the view as active (DolphinView:isActive() will return true)
564 * and emits the 'activated' signal if it is not already active.
568 void slotItemActivated(int index
);
569 void slotItemsActivated(const KItemSet
& indexes
);
570 void slotItemMiddleClicked(int index
);
571 void slotItemContextMenuRequested(int index
, const QPointF
& pos
);
572 void slotViewContextMenuRequested(const QPointF
& pos
);
573 void slotHeaderContextMenuRequested(const QPointF
& pos
);
574 void slotHeaderColumnWidthChangeFinished(const QByteArray
& role
, qreal current
);
575 void slotItemHovered(int index
);
576 void slotItemUnhovered(int index
);
577 void slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
);
578 void slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
);
579 void slotMouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
);
580 void slotRenameDialogRenamingFinished(const QList
<QUrl
>& urls
);
581 void slotSelectedItemTextPressed(int index
);
584 * Is called when new items get pasted or dropped.
586 void slotItemCreated(const QUrl
&url
);
588 * Is called after all pasted or dropped items have been copied to destination.
590 void slotPasteJobResult(KJob
*job
);
593 * Emits the signal \a selectionChanged() with a small delay. This is
594 * because getting all file items for the selection can be an expensive
595 * operation. Fast selection changes are collected in this case and
596 * the signal is emitted only after no selection change has been done
597 * within a small delay.
599 void slotSelectionChanged(const KItemSet
& current
, const KItemSet
& previous
);
602 * Is called by emitDelayedSelectionChangedSignal() and emits the
603 * signal \a selectionChanged() with all selected file items as parameter.
605 void emitSelectionChangedSignal();
608 * Updates the view properties of the current URL to the
609 * sorting given by \a role.
611 void updateSortRole(const QByteArray
& role
);
614 * Updates the view properties of the current URL to the
615 * sort order given by \a order.
617 void updateSortOrder(Qt::SortOrder order
);
620 * Updates the view properties of the current URL to the
621 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
623 void updateSortFoldersFirst(bool foldersFirst
);
626 * Indicates in the status bar that the delete operation
627 * of the job \a job has been finished.
629 void slotDeleteFileFinished(KJob
* job
);
632 * Indicates in the status bar that the trash operation
633 * of the job \a job has been finished.
635 void slotTrashFileFinished(KJob
* job
);
638 * Invoked when the rename job is done, for error handling.
640 void slotRenamingResult(KJob
* job
);
643 * Invoked when the file item model has started the loading
644 * of the directory specified by DolphinView::url().
646 void slotDirectoryLoadingStarted();
649 * Invoked when the file item model indicates that the loading of a directory has
650 * been completed. Assures that pasted items and renamed items get selected.
652 void slotDirectoryLoadingCompleted();
655 * Is invoked when items of KFileItemModel have been changed.
657 void slotItemsChanged();
660 * Is invoked when the sort order has been changed by the user by clicking
661 * on a header item. The view properties of the directory will get updated.
663 void slotSortOrderChangedByHeader(Qt::SortOrder current
, Qt::SortOrder previous
);
666 * Is invoked when the sort role has been changed by the user by clicking
667 * on a header item. The view properties of the directory will get updated.
669 void slotSortRoleChangedByHeader(const QByteArray
& current
, const QByteArray
& previous
);
672 * Is invoked when the visible roles have been changed by the user by dragging
673 * a header item. The view properties of the directory will get updated.
675 void slotVisibleRolesChangedByHeader(const QList
<QByteArray
>& current
,
676 const QList
<QByteArray
>& previous
);
678 void slotRoleEditingCanceled();
679 void slotRoleEditingFinished(int index
, const QByteArray
& role
, const QVariant
& value
);
682 * Observes the item with the URL \a url. As soon as the directory
683 * model indicates that the item is available, the item will
684 * get selected and it is assured that the item stays visible.
686 void observeCreatedItem(const QUrl
&url
);
689 * Called when a redirection happens.
690 * Testcase: fish://localhost
692 void slotDirectoryRedirection(const QUrl
& oldUrl
, const QUrl
& newUrl
);
695 * Applies the state that has been restored by restoreViewState()
698 void updateViewState();
703 * Calculates the number of currently shown files into
704 * \a fileCount and the number of folders into \a folderCount.
705 * The size of all files is written into \a totalFileSize.
706 * It is recommend using this method instead of asking the
707 * directory lister or the model directly, as it takes
708 * filtering and hierarchical previews into account.
710 void calculateItemCount(int& fileCount
, int& folderCount
, KIO::filesize_t
& totalFileSize
) const;
712 void slotTwoClicksRenamingTimerTimeout();
715 void loadDirectory(const QUrl
& url
, bool reload
= false);
718 * Applies the view properties which are defined by the current URL
719 * to the DolphinView properties. The view properties are read from a
720 * .directory file either in the current directory, or in the
721 * share/apps/dolphin/view_properties/ subfolder of the user's .kde folder.
723 void applyViewProperties();
726 * Applies the given view properties to the DolphinView.
728 void applyViewProperties(const ViewProperties
& props
);
731 * Applies the m_mode property to the corresponding
732 * itemlayout-property of the KItemListView.
734 void applyModeToView();
737 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
738 * Pastes the clipboard data into the URL \a url.
740 void pasteToUrl(const QUrl
& url
);
743 * Returns a list of URLs for all selected items. The list is
744 * simplified, so that when the URLs are part of different tree
745 * levels, only the parent is returned.
747 QList
<QUrl
> simplifiedSelectedUrls() const;
750 * Returns the MIME data for all selected items.
752 QMimeData
* selectionMimeData() const;
755 * Updates m_isFolderWritable dependent on whether the folder represented by
756 * the current URL is writable. If the state has changed, the signal
757 * writeableStateChanged() will be emitted.
759 void updateWritableState();
762 * @return The current URL if no viewproperties-context is given (see
763 * DolphinView::viewPropertiesContext(), otherwise the context
766 QUrl
viewPropertiesUrl() const;
769 * Clears the selection and updates current item and selection according to the parameters
771 * @param current URL to be set as current
772 * @param selected list of selected items
774 void forceUrlsSelection(const QUrl
& current
, const QList
<QUrl
>& selected
);
776 void abortTwoClicksRenaming();
779 void updatePalette();
783 bool m_assureVisibleCurrentIndex
;
784 bool m_isFolderWritable
;
785 bool m_dragging
; // True if a dragging is done. Required to be able to decide whether a
786 // tooltip may be shown when hovering an item.
789 QString m_viewPropertiesContext
;
791 QList
<QByteArray
> m_visibleRoles
;
793 QVBoxLayout
* m_topLayout
;
795 KFileItemModel
* m_model
;
796 DolphinItemListView
* m_view
;
797 KItemListContainer
* m_container
;
799 ToolTipManager
* m_toolTipManager
;
801 QTimer
* m_selectionChangedTimer
;
803 QUrl m_currentItemUrl
; // Used for making the view to remember the current URL after F5
804 bool m_scrollToCurrentItem
; // Used for marking we need to scroll to current item or not
805 QPoint m_restoredContentsPosition
;
807 QList
<QUrl
> m_selectedUrls
; // Used for making the view to remember selections after F5
808 bool m_clearSelectionBeforeSelectingNewItems
;
809 bool m_markFirstNewlySelectedItemAsCurrent
;
811 VersionControlObserver
* m_versionControlObserver
;
813 QTimer
* m_twoClicksRenamingTimer
;
814 QUrl m_twoClicksRenamingItemUrl
;
817 friend class TestBase
;
818 friend class DolphinDetailsViewTest
;
819 friend class DolphinPart
; // Accesses m_model
822 /// Allow using DolphinView::Mode in QVariant
823 Q_DECLARE_METATYPE(DolphinView::Mode
)
825 #endif // DOLPHINVIEW_H