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"
26 #include "tooltips/tooltipmanager.h"
30 #include <config-baloo.h>
31 #include <kio/fileundomanager.h>
32 #include <kparts/part.h>
38 typedef KIO::FileUndoManager::CommandType CommandType
;
40 class DolphinItemListView
;
42 class KItemListContainer
;
46 class VersionControlObserver
;
48 class QGraphicsSceneDragDropEvent
;
49 class QRegularExpression
;
52 * @short Represents a view for the directory content.
54 * View modes for icons, compact and details are supported. It's
62 class DOLPHIN_EXPORT DolphinView
: public QWidget
68 * Defines the view mode for a directory. The
69 * view mode is automatically updated if the directory itself
70 * defines a view mode (see class ViewProperties for details).
75 * The items are shown as icons with a name-label below.
80 * The icon, the name and the size of the items are
81 * shown per default as a table.
86 * The items are shown as icons with the name-label aligned
93 * @param url Specifies the content which should be shown.
94 * @param parent Parent widget of the view.
96 DolphinView(const QUrl
& url
, QWidget
* parent
);
98 ~DolphinView() override
;
101 * Returns the current active URL, where all actions are applied.
102 * The URL navigator is synchronized with this URL.
107 * If \a active is true, the view will marked as active. The active
108 * view is defined as view where all actions are applied to.
110 void setActive(bool active
);
111 bool isActive() const;
114 * Changes the view mode for the current directory to \a mode.
115 * If the view properties should be remembered for each directory
116 * (GeneralSettings::globalViewProps() returns false), then the
117 * changed view mode will be stored automatically.
119 void setMode(Mode mode
);
123 * Turns on the file preview for the all files of the current directory,
124 * if \a show is true.
125 * If the view properties should be remembered for each directory
126 * (GeneralSettings::globalViewProps() returns false), then the
127 * preview setting will be stored automatically.
129 void setPreviewsShown(bool show
);
130 bool previewsShown() const;
133 * Shows all hidden files of the current directory,
134 * if \a show is true.
135 * If the view properties should be remembered for each directory
136 * (GeneralSettings::globalViewProps() returns false), then the
137 * show hidden file setting will be stored automatically.
139 void setHiddenFilesShown(bool show
);
140 bool hiddenFilesShown() const;
143 * Turns on sorting by groups if \a enable is true.
145 void setGroupedSorting(bool grouped
);
146 bool groupedSorting() const;
149 * Returns the items of the view.
151 KFileItemList
items() const;
154 * @return The number of items. itemsCount() is faster in comparison
155 * to items().count().
157 int itemsCount() const;
160 * Returns the selected items. The list is empty if no item has been
163 KFileItemList
selectedItems() const;
166 * Returns the number of selected items (this is faster than
167 * invoking selectedItems().count()).
169 int selectedItemsCount() const;
172 * Marks the items indicated by \p urls to get selected after the
173 * directory DolphinView::url() has been loaded. Note that nothing
174 * gets selected if no loading of a directory has been triggered
175 * by DolphinView::setUrl() or DolphinView::reload().
177 void markUrlsAsSelected(const QList
<QUrl
> &urls
);
180 * Marks the item indicated by \p url to be scrolled to and as the
181 * current item after directory DolphinView::url() has been loaded.
183 void markUrlAsCurrent(const QUrl
& url
);
186 * All items that match the regular expression \a regexp will get selected
187 * if \a enabled is true and deselected if \a enabled is false.
189 * Note that to match the whole string the pattern should be anchored:
190 * - you can anchor the pattern with QRegularExpression::anchoredPattern()
191 * - if you use QRegularExpresssion::wildcardToRegularExpression(), don't use
192 * QRegularExpression::anchoredPattern() as the former already returns an
195 void selectItems(const QRegularExpression
®exp
, bool enabled
);
198 * Sets the zoom level to \a level. It is assured that the used
199 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
200 * ZoomLevelInfo::maximumLevel().
202 void setZoomLevel(int level
);
203 int zoomLevel() const;
206 * Resets the view's icon size to the default value
208 void resetZoomLevel();
210 void setSortRole(const QByteArray
& role
);
211 QByteArray
sortRole() const;
213 void setSortOrder(Qt::SortOrder order
);
214 Qt::SortOrder
sortOrder() const;
216 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
217 void setSortFoldersFirst(bool foldersFirst
);
218 bool sortFoldersFirst() const;
220 /** Sets the additional information which should be shown for the items. */
221 void setVisibleRoles(const QList
<QByteArray
>& roles
);
223 /** Returns the additional information which should be shown for the items. */
224 QList
<QByteArray
> visibleRoles() const;
229 * Refreshes the view to get synchronized with the settings (e.g. icons size,
235 * Saves the current settings (e.g. icons size, font, ..).
237 void writeSettings();
240 * Filters the currently shown items by \a nameFilter. All items
241 * which contain the given filter string will be shown.
243 void setNameFilter(const QString
& nameFilter
);
244 QString
nameFilter() const;
247 * Filters the currently shown items by \a filters. All items
248 * whose content-type matches those given by the list of filters
251 void setMimeTypeFilters(const QStringList
& filters
);
252 QStringList
mimeTypeFilters() const;
255 * Returns a textual representation of the state of the current
256 * folder or selected items, suitable for use in the status bar.
258 QString
statusBarText() const;
261 * Returns the version control actions that are provided for the items \p items.
262 * Usually the actions are presented in the context menu.
264 QList
<QAction
*> versionControlActions(const KFileItemList
& items
) const;
267 * Returns the state of the paste action:
268 * first is whether the action should be enabled
269 * second is the text for the action
271 QPair
<bool, QString
> pasteInfo() const;
274 * If \a tabsForFiles is true, the signal tabRequested() will also
275 * emitted also for files. Per default tabs for files is disabled
276 * and hence the signal tabRequested() will only be emitted for
279 void setTabsForFilesEnabled(bool tabsForFiles
);
280 bool isTabsForFilesEnabled() const;
283 * Returns true if the current view allows folders to be expanded,
284 * i.e. presents a hierarchical view to the user.
286 bool itemsExpandable() const;
289 * Restores the view state (current item, contents position, details view expansion state)
291 void restoreState(QDataStream
& stream
);
294 * Saves the view state (current item, contents position, details view expansion state)
296 void saveState(QDataStream
& stream
);
299 * Returns the root item which represents the current URL.
301 KFileItem
rootItem() const;
304 * Sets a context that is used for remembering the view-properties.
305 * Per default the context is empty and the path of the currently set URL
306 * is used for remembering the view-properties. Setting a custom context
307 * makes sense if specific types of URLs (e.g. search-URLs) should
308 * share common view-properties.
310 void setViewPropertiesContext(const QString
& context
);
311 QString
viewPropertiesContext() const;
314 * Checks if the given \a item can be opened as folder (e.g. archives).
315 * This function will also adjust the \a url (e.g. change the protocol).
316 * @return a valid and adjusted url if the item can be opened as folder,
317 * otherwise return an empty url.
319 static QUrl
openItemAsFolderUrl(const KFileItem
& item
, const bool browseThroughArchives
= true);
322 * Hides tooltip displayed over element.
324 void hideToolTip(const ToolTipManager::HideBehavior behavior
= ToolTipManager::HideBehavior::Later
);
328 * Changes the directory to \a url. If the current directory is equal to
329 * \a url, nothing will be done (use DolphinView::reload() instead).
331 void setUrl(const QUrl
& url
);
335 * @see DolphinView::selectedItems()
340 * Inverts the current selection: selected items get unselected,
341 * unselected items get selected.
342 * @see DolphinView::selectedItems()
344 void invertSelection();
346 void clearSelection();
349 * Triggers the renaming of the currently selected items, where
350 * the user must input a new name for the items.
352 void renameSelectedItems();
355 * Moves all selected items to the trash.
357 void trashSelectedItems();
360 * Deletes all selected items.
362 void deleteSelectedItems();
365 * Copies all selected items to the clipboard and marks
368 void cutSelectedItemsToClipboard();
370 /** Copies all selected items to the clipboard. */
371 void copySelectedItemsToClipboard();
373 /** Pastes the clipboard data to this view. */
377 * Pastes the clipboard data into the currently selected
378 * folder. If the current selection is not exactly one folder, no
379 * paste operation is done.
381 void pasteIntoFolder();
384 * Creates duplicates of selected items, appending "copy"
387 void duplicateSelectedItems();
390 * Handles a drop of @p dropEvent onto widget @p dropWidget and destination @p destUrl
392 void dropUrls(const QUrl
&destUrl
, QDropEvent
*dropEvent
, QWidget
*dropWidget
);
397 * Applies the state that has been restored by restoreViewState()
400 void updateViewState();
402 /** Activates the view if the item list container gets focus. */
403 bool eventFilter(QObject
* watched
, QEvent
* event
) override
;
407 * Is emitted if the view has been activated by e. g. a mouse click.
411 /** Is emitted if the URL of the view has been changed to \a url. */
412 void urlChanged(const QUrl
& url
);
415 * Is emitted when clicking on an item with the left mouse button.
417 void itemActivated(const KFileItem
& item
);
420 * Is emitted when multiple items have been activated by e. g.
421 * context menu open with.
423 void itemsActivated(const KFileItemList
& items
);
426 * Is emitted if items have been added or deleted.
428 void itemCountChanged();
431 * Is emitted if a new tab should be opened for the URL \a url.
433 void tabRequested(const QUrl
& url
, DolphinTabWidget::TabPlacement tabPlacement
);
436 * Is emitted if the view mode (IconsView, DetailsView,
437 * PreviewsView) has been changed.
439 void modeChanged(DolphinView::Mode current
, DolphinView::Mode previous
);
441 /** Is emitted if the 'show preview' property has been changed. */
442 void previewsShownChanged(bool shown
);
444 /** Is emitted if the 'show hidden files' property has been changed. */
445 void hiddenFilesShownChanged(bool shown
);
447 /** Is emitted if the 'grouped sorting' property has been changed. */
448 void groupedSortingChanged(bool groupedSorting
);
450 /** Is emitted if the sorting by name, size or date has been changed. */
451 void sortRoleChanged(const QByteArray
& role
);
453 /** Is emitted if the sort order (ascending or descending) has been changed. */
454 void sortOrderChanged(Qt::SortOrder order
);
457 * Is emitted if the sorting of files and folders (separate with folders
458 * first or mixed) has been changed.
460 void sortFoldersFirstChanged(bool foldersFirst
);
462 /** Is emitted if the additional information shown for this view has been changed. */
463 void visibleRolesChanged(const QList
<QByteArray
>& current
,
464 const QList
<QByteArray
>& previous
);
466 /** Is emitted if the zoom level has been changed by zooming in or out. */
467 void zoomLevelChanged(int current
, int previous
);
470 * Is emitted if information of an item is requested to be shown e. g. in the panel.
471 * If item is null, no item information request is pending.
473 void requestItemInfo(const KFileItem
& item
);
476 * Is emitted whenever the selection has been changed.
478 void selectionChanged(const KFileItemList
& selection
);
481 * Is emitted if a context menu is requested for the item \a item,
482 * which is part of \a url. If the item is null, the context menu
483 * for the URL should be shown and the custom actions \a customActions
486 void requestContextMenu(const QPoint
& pos
,
487 const KFileItem
& item
,
489 const QList
<QAction
*>& customActions
);
492 * Is emitted if an information message with the content \a msg
495 void infoMessage(const QString
& msg
);
498 * Is emitted if an error message with the content \a msg
501 void errorMessage(const QString
& msg
);
504 * Is emitted if an "operation completed" message with the content \a msg
507 void operationCompletedMessage(const QString
& msg
);
510 * Is emitted after DolphinView::setUrl() has been invoked and
511 * the current directory is loaded. If this signal is emitted,
512 * it is assured that the view contains already the correct root
513 * URL and property settings.
515 void directoryLoadingStarted();
518 * Is emitted after the directory triggered by DolphinView::setUrl()
521 void directoryLoadingCompleted();
524 * Is emitted after the directory loading triggered by DolphinView::setUrl()
527 void directoryLoadingCanceled();
530 * Is emitted after DolphinView::setUrl() has been invoked and provides
531 * the information how much percent of the current directory have been loaded.
533 void directoryLoadingProgress(int percent
);
536 * Is emitted if the sorting is done asynchronously and provides the
537 * progress information of the sorting.
539 void directorySortingProgress(int percent
);
542 * Emitted when the file-item-model emits redirection.
543 * Testcase: fish://localhost
545 void redirection(const QUrl
& oldUrl
, const QUrl
& newUrl
);
548 * Is emitted when the URL set by DolphinView::setUrl() represents a file.
549 * In this case no signal errorMessage() will be emitted.
551 void urlIsFileError(const QUrl
& url
);
554 * Is emitted when the write state of the folder has been changed. The application
555 * should disable all actions like "Create New..." that depend on the write
558 void writeStateChanged(bool isFolderWritable
);
561 * Is emitted if the URL should be changed to the previous URL of the
562 * history (e.g. because the "back"-mousebutton has been pressed).
564 void goBackRequested();
567 * Is emitted if the URL should be changed to the next URL of the
568 * history (e.g. because the "next"-mousebutton has been pressed).
570 void goForwardRequested();
573 * Is emitted when the user wants to move the focus to another view.
575 void toggleActiveViewRequested();
578 * Is emitted when the user clicks a tag or a link
579 * in the metadata widget of a tooltip.
581 void urlActivated(const QUrl
& url
);
584 /** Changes the zoom level if Control is pressed during a wheel event. */
585 void wheelEvent(QWheelEvent
* event
) override
;
587 void hideEvent(QHideEvent
* event
) override
;
588 bool event(QEvent
* event
) override
;
592 * Marks the view as active (DolphinView:isActive() will return true)
593 * and emits the 'activated' signal if it is not already active.
597 void slotItemActivated(int index
);
598 void slotItemsActivated(const KItemSet
& indexes
);
599 void slotItemMiddleClicked(int index
);
600 void slotItemContextMenuRequested(int index
, const QPointF
& pos
);
601 void slotViewContextMenuRequested(const QPointF
& pos
);
602 void slotHeaderContextMenuRequested(const QPointF
& pos
);
603 void slotHeaderColumnWidthChangeFinished(const QByteArray
& role
, qreal current
);
604 void slotItemHovered(int index
);
605 void slotItemUnhovered(int index
);
606 void slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
);
607 void slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
);
608 void slotMouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
);
609 void slotRenameDialogRenamingFinished(const QList
<QUrl
>& urls
);
610 void slotSelectedItemTextPressed(int index
);
613 * Is called when new items get pasted or dropped.
615 void slotItemCreated(const QUrl
&url
);
617 * Is called after all pasted or dropped items have been copied to destination.
619 void slotPasteJobResult(KJob
*job
);
622 * Emits the signal \a selectionChanged() with a small delay. This is
623 * because getting all file items for the selection can be an expensive
624 * operation. Fast selection changes are collected in this case and
625 * the signal is emitted only after no selection change has been done
626 * within a small delay.
628 void slotSelectionChanged(const KItemSet
& current
, const KItemSet
& previous
);
631 * Is called by emitDelayedSelectionChangedSignal() and emits the
632 * signal \a selectionChanged() with all selected file items as parameter.
634 void emitSelectionChangedSignal();
637 * Updates the view properties of the current URL to the
638 * sorting given by \a role.
640 void updateSortRole(const QByteArray
& role
);
643 * Updates the view properties of the current URL to the
644 * sort order given by \a order.
646 void updateSortOrder(Qt::SortOrder order
);
649 * Updates the view properties of the current URL to the
650 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
652 void updateSortFoldersFirst(bool foldersFirst
);
655 * Indicates in the status bar that the delete operation
656 * of the job \a job has been finished.
658 void slotDeleteFileFinished(KJob
* job
);
661 * Indicates in the status bar that the trash operation
662 * of the job \a job has been finished.
664 void slotTrashFileFinished(KJob
* job
);
667 * Invoked when the rename job is done, for error handling.
669 void slotRenamingResult(KJob
* job
);
672 * Invoked when the file item model has started the loading
673 * of the directory specified by DolphinView::url().
675 void slotDirectoryLoadingStarted();
678 * Invoked when the file item model indicates that the loading of a directory has
679 * been completed. Assures that pasted items and renamed items get selected.
681 void slotDirectoryLoadingCompleted();
684 * Is invoked when items of KFileItemModel have been changed.
686 void slotItemsChanged();
689 * Is invoked when the sort order has been changed by the user by clicking
690 * on a header item. The view properties of the directory will get updated.
692 void slotSortOrderChangedByHeader(Qt::SortOrder current
, Qt::SortOrder previous
);
695 * Is invoked when the sort role has been changed by the user by clicking
696 * on a header item. The view properties of the directory will get updated.
698 void slotSortRoleChangedByHeader(const QByteArray
& current
, const QByteArray
& previous
);
701 * Is invoked when the visible roles have been changed by the user by dragging
702 * a header item. The view properties of the directory will get updated.
704 void slotVisibleRolesChangedByHeader(const QList
<QByteArray
>& current
,
705 const QList
<QByteArray
>& previous
);
707 void slotRoleEditingCanceled();
708 void slotRoleEditingFinished(int index
, const QByteArray
& role
, const QVariant
& value
);
711 * Observes the item with the URL \a url. As soon as the directory
712 * model indicates that the item is available, the item will
713 * get selected and it is assured that the item stays visible.
715 void observeCreatedItem(const QUrl
&url
);
718 * Called when a redirection happens.
719 * Testcase: fish://localhost
721 void slotDirectoryRedirection(const QUrl
& oldUrl
, const QUrl
& newUrl
);
724 * Calculates the number of currently shown files into
725 * \a fileCount and the number of folders into \a folderCount.
726 * The size of all files is written into \a totalFileSize.
727 * It is recommend using this method instead of asking the
728 * directory lister or the model directly, as it takes
729 * filtering and hierarchical previews into account.
731 void calculateItemCount(int& fileCount
, int& folderCount
, KIO::filesize_t
& totalFileSize
) const;
733 void slotTwoClicksRenamingTimerTimeout();
736 void loadDirectory(const QUrl
& url
, bool reload
= false);
739 * Applies the view properties which are defined by the current URL
740 * to the DolphinView properties. The view properties are read from a
741 * .directory file either in the current directory, or in the
742 * share/apps/dolphin/view_properties/ subfolder of the user's .kde folder.
744 void applyViewProperties();
747 * Applies the given view properties to the DolphinView.
749 void applyViewProperties(const ViewProperties
& props
);
752 * Applies the m_mode property to the corresponding
753 * itemlayout-property of the KItemListView.
755 void applyModeToView();
758 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
759 * Pastes the clipboard data into the URL \a url.
761 void pasteToUrl(const QUrl
& url
);
764 * Returns a list of URLs for all selected items. The list is
765 * simplified, so that when the URLs are part of different tree
766 * levels, only the parent is returned.
768 QList
<QUrl
> simplifiedSelectedUrls() const;
771 * Returns the MIME data for all selected items.
773 QMimeData
* selectionMimeData() const;
776 * Updates m_isFolderWritable dependent on whether the folder represented by
777 * the current URL is writable. If the state has changed, the signal
778 * writeableStateChanged() will be emitted.
780 void updateWritableState();
783 * @return The current URL if no viewproperties-context is given (see
784 * DolphinView::viewPropertiesContext(), otherwise the context
787 QUrl
viewPropertiesUrl() const;
790 * Clears the selection and updates current item and selection according to the parameters
792 * @param current URL to be set as current
793 * @param selected list of selected items
795 void forceUrlsSelection(const QUrl
& current
, const QList
<QUrl
>& selected
);
797 void abortTwoClicksRenaming();
800 void updatePalette();
804 bool m_assureVisibleCurrentIndex
;
805 bool m_isFolderWritable
;
806 bool m_dragging
; // True if a dragging is done. Required to be able to decide whether a
807 // tooltip may be shown when hovering an item.
810 QString m_viewPropertiesContext
;
812 QList
<QByteArray
> m_visibleRoles
;
814 QVBoxLayout
* m_topLayout
;
816 KFileItemModel
* m_model
;
817 DolphinItemListView
* m_view
;
818 KItemListContainer
* m_container
;
820 ToolTipManager
* m_toolTipManager
;
822 QTimer
* m_selectionChangedTimer
;
824 QUrl m_currentItemUrl
; // Used for making the view to remember the current URL after F5
825 bool m_scrollToCurrentItem
; // Used for marking we need to scroll to current item or not
826 QPoint m_restoredContentsPosition
;
828 QList
<QUrl
> m_selectedUrls
; // Used for making the view to remember selections after F5
829 bool m_clearSelectionBeforeSelectingNewItems
;
830 bool m_markFirstNewlySelectedItemAsCurrent
;
832 VersionControlObserver
* m_versionControlObserver
;
834 QTimer
* m_twoClicksRenamingTimer
;
835 QUrl m_twoClicksRenamingItemUrl
;
838 friend class TestBase
;
839 friend class DolphinDetailsViewTest
;
840 friend class DolphinPart
; // Accesses m_model
843 /// Allow using DolphinView::Mode in QVariant
844 Q_DECLARE_METATYPE(DolphinView::Mode
)
846 #endif // DOLPHINVIEW_H