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 <KFileItemDelegate>
31 #include <kio/fileundomanager.h>
37 #include <QLinkedList>
40 typedef KIO::FileUndoManager::CommandType CommandType
;
42 class DolphinItemListView
;
44 class KActionCollection
;
46 class KItemListContainer
;
51 class VersionControlObserver
;
53 class QGraphicsSceneDragDropEvent
;
57 * @short Represents a view for the directory content.
59 * View modes for icons, compact and details are supported. It's
67 class LIBDOLPHINPRIVATE_EXPORT DolphinView
: public QWidget
73 * Defines the view mode for a directory. The
74 * view mode is automatically updated if the directory itself
75 * defines a view mode (see class ViewProperties for details).
80 * The items are shown as icons with a name-label below.
85 * The icon, the name and the size of the items are
86 * shown per default as a table.
91 * The items are shown as icons with the name-label aligned
98 * @param url Specifies the content which should be shown.
99 * @param parent Parent widget of the view.
101 DolphinView(const KUrl
& url
, QWidget
* parent
);
103 virtual ~DolphinView();
106 * Returns the current active URL, where all actions are applied.
107 * The URL navigator is synchronized with this URL.
112 * If \a active is true, the view will marked as active. The active
113 * view is defined as view where all actions are applied to.
115 void setActive(bool active
);
116 bool isActive() const;
119 * Changes the view mode for the current directory to \a mode.
120 * If the view properties should be remembered for each directory
121 * (GeneralSettings::globalViewProps() returns false), then the
122 * changed view mode will be stored automatically.
124 void setMode(Mode mode
);
128 * Turns on the file preview for the all files of the current directory,
129 * if \a show is true.
130 * If the view properties should be remembered for each directory
131 * (GeneralSettings::globalViewProps() returns false), then the
132 * preview setting will be stored automatically.
134 void setPreviewsShown(bool show
);
135 bool previewsShown() const;
138 * Shows all hidden files of the current directory,
139 * if \a show is true.
140 * If the view properties should be remembered for each directory
141 * (GeneralSettings::globalViewProps() returns false), then the
142 * show hidden file setting will be stored automatically.
144 void setHiddenFilesShown(bool show
);
145 bool hiddenFilesShown() const;
148 * Turns on sorting by groups if \a enable is true.
150 void setGroupedSorting(bool grouped
);
151 bool groupedSorting() const;
154 * Returns the items of the view.
156 KFileItemList
items() const;
159 * @return The number of items. itemsCount() is faster in comparison
160 * to items().count().
162 int itemsCount() const;
165 * Returns the selected items. The list is empty if no item has been
168 KFileItemList
selectedItems() const;
171 * Returns the number of selected items (this is faster than
172 * invoking selectedItems().count()).
174 int selectedItemsCount() const;
177 * Marks the items indicated by \p urls to get selected after the
178 * directory DolphinView::url() has been loaded. Note that nothing
179 * gets selected if no loading of a directory has been triggered
180 * by DolphinView::setUrl() or DolphinView::reload().
182 void markUrlsAsSelected(const QList
<KUrl
>& urls
);
185 * Marks the item indicated by \p url to be scrolled to and as the
186 * current item after directory DolphinView::url() has been loaded.
188 void markUrlAsCurrent(const KUrl
& url
);
191 * All items that match to the pattern \a pattern will get selected
192 * if \a enabled is true and deselected if \a enabled is false.
194 void selectItems(const QRegExp
& pattern
, bool enabled
);
197 * Sets the zoom level to \a level. It is assured that the used
198 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
199 * ZoomLevelInfo::maximumLevel().
201 void setZoomLevel(int level
);
202 int zoomLevel() const;
204 void setSortRole(const QByteArray
& role
);
205 QByteArray
sortRole() const;
207 void setSortOrder(Qt::SortOrder order
);
208 Qt::SortOrder
sortOrder() const;
210 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
211 void setSortFoldersFirst(bool foldersFirst
);
212 bool sortFoldersFirst() const;
214 /** Sets the additional information which should be shown for the items. */
215 void setVisibleRoles(const QList
<QByteArray
>& roles
);
217 /** Returns the additional information which should be shown for the items. */
218 QList
<QByteArray
> visibleRoles() const;
223 * Refreshes the view to get synchronized with the settings (e.g. icons size,
229 * Saves the current settings (e.g. icons size, font, ..).
231 void writeSettings();
234 * Filters the currently shown items by \a nameFilter. All items
235 * which contain the given filter string will be shown.
237 void setNameFilter(const QString
& nameFilter
);
238 QString
nameFilter() const;
241 * Filters the currently shown items by \a filters. All items
242 * whose content-type matches those given by the list of filters
245 void setMimeTypeFilters(const QStringList
& filters
);
246 QStringList
mimeTypeFilters() const;
249 * Returns a textual representation of the state of the current
250 * folder or selected items, suitable for use in the status bar.
252 QString
statusBarText() const;
255 * Returns the version control actions that are provided for the items \p items.
256 * Usually the actions are presented in the context menu.
258 QList
<QAction
*> versionControlActions(const KFileItemList
& items
) const;
261 * Returns the state of the paste action:
262 * first is whether the action should be enabled
263 * second is the text for the action
265 QPair
<bool, QString
> pasteInfo() const;
268 * If \a tabsForFiles is true, the signal tabRequested() will also
269 * emitted also for files. Per default tabs for files is disabled
270 * and hence the signal tabRequested() will only be emitted for
273 void setTabsForFilesEnabled(bool tabsForFiles
);
274 bool isTabsForFilesEnabled() const;
277 * Returns true if the current view allows folders to be expanded,
278 * i.e. presents a hierarchical view to the user.
280 bool itemsExpandable() const;
283 * Restores the view state (current item, contents position, details view expansion state)
285 void restoreState(QDataStream
& stream
);
288 * Saves the view state (current item, contents position, details view expansion state)
290 void saveState(QDataStream
& stream
);
293 * Returns the root item which represents the current URL.
295 KFileItem
rootItem() const;
298 * Sets a context that is used for remembering the view-properties.
299 * Per default the context is empty and the path of the currently set URL
300 * is used for remembering the view-properties. Setting a custom context
301 * makes sense if specific types of URLs (e.g. search-URLs) should
302 * share common view-properties.
304 void setViewPropertiesContext(const QString
& context
);
305 QString
viewPropertiesContext() const;
308 * Checks if the given \a item can be opened as folder (e.g. archives).
309 * This function will also adjust the \a url (e.g. change the protocol).
310 * @return a valid and adjusted url if the item can be opened as folder,
311 * otherwise return an empty url.
313 static KUrl
openItemAsFolderUrl(const KFileItem
& item
, const bool browseThroughArchives
= true);
317 * Changes the directory to \a url. If the current directory is equal to
318 * \a url, nothing will be done (use DolphinView::reload() instead).
320 void setUrl(const KUrl
& url
);
324 * @see DolphinView::selectedItems()
329 * Inverts the current selection: selected items get unselected,
330 * unselected items get selected.
331 * @see DolphinView::selectedItems()
333 void invertSelection();
335 void clearSelection();
338 * Triggers the renaming of the currently selected items, where
339 * the user must input a new name for the items.
341 void renameSelectedItems();
344 * Moves all selected items to the trash.
346 void trashSelectedItems();
349 * Deletes all selected items.
351 void deleteSelectedItems();
354 * Copies all selected items to the clipboard and marks
357 void cutSelectedItems();
359 /** Copies all selected items to the clipboard. */
360 void copySelectedItems();
362 /** Pastes the clipboard data to this view. */
366 * Pastes the clipboard data into the currently selected
367 * folder. If the current selection is not exactly one folder, no
368 * paste operation is done.
370 void pasteIntoFolder();
374 /** Activates the view if the item list container gets focus. */
375 virtual bool eventFilter(QObject
* watched
, QEvent
* event
);
379 * Is emitted if the view has been activated by e. g. a mouse click.
384 * Is emitted if the URL of the view will be changed to \a url.
385 * After the URL has been changed the signal urlChanged() will
388 void urlAboutToBeChanged(const KUrl
& url
);
390 /** Is emitted if the URL of the view has been changed to \a url. */
391 void urlChanged(const KUrl
& url
);
394 * Is emitted when clicking on an item with the left mouse button.
396 void itemActivated(const KFileItem
& item
);
399 * Is emitted when multiple items have been activated by e. g.
400 * context menu open with.
402 void itemsActivated(const KFileItemList
& items
);
405 * Is emitted if items have been added or deleted.
407 void itemCountChanged();
410 * Is emitted if a new tab should be opened for the URL \a url.
412 void tabRequested(const KUrl
& url
);
415 * Is emitted if the view mode (IconsView, DetailsView,
416 * PreviewsView) has been changed.
418 void modeChanged(DolphinView::Mode current
, DolphinView::Mode previous
);
420 /** Is emitted if the 'show preview' property has been changed. */
421 void previewsShownChanged(bool shown
);
423 /** Is emitted if the 'show hidden files' property has been changed. */
424 void hiddenFilesShownChanged(bool shown
);
426 /** Is emitted if the 'grouped sorting' property has been changed. */
427 void groupedSortingChanged(bool groupedSorting
);
429 /** Is emitted if the sorting by name, size or date has been changed. */
430 void sortRoleChanged(const QByteArray
& role
);
432 /** Is emitted if the sort order (ascending or descending) has been changed. */
433 void sortOrderChanged(Qt::SortOrder order
);
436 * Is emitted if the sorting of files and folders (separate with folders
437 * first or mixed) has been changed.
439 void sortFoldersFirstChanged(bool foldersFirst
);
441 /** Is emitted if the additional information shown for this view has been changed. */
442 void visibleRolesChanged(const QList
<QByteArray
>& current
,
443 const QList
<QByteArray
>& previous
);
445 /** Is emitted if the zoom level has been changed by zooming in or out. */
446 void zoomLevelChanged(int current
, int previous
);
449 * Is emitted if information of an item is requested to be shown e. g. in the panel.
450 * If item is null, no item information request is pending.
452 void requestItemInfo(const KFileItem
& item
);
455 * Is emitted whenever the selection has been changed.
457 void selectionChanged(const KFileItemList
& selection
);
460 * Is emitted if a context menu is requested for the item \a item,
461 * which is part of \a url. If the item is null, the context menu
462 * for the URL should be shown and the custom actions \a customActions
465 void requestContextMenu(const QPoint
& pos
,
466 const KFileItem
& item
,
468 const QList
<QAction
*>& customActions
);
471 * Is emitted if an information message with the content \a msg
474 void infoMessage(const QString
& msg
);
477 * Is emitted if an error message with the content \a msg
480 void errorMessage(const QString
& msg
);
483 * Is emitted if an "operation completed" message with the content \a msg
486 void operationCompletedMessage(const QString
& msg
);
489 * Is emitted after DolphinView::setUrl() has been invoked and
490 * the current directory is loaded. If this signal is emitted,
491 * it is assured that the view contains already the correct root
492 * URL and property settings.
494 void directoryLoadingStarted();
497 * Is emitted after the directory triggered by DolphinView::setUrl()
500 void directoryLoadingCompleted();
503 * Is emitted after the directory loading triggered by DolphinView::setUrl()
506 void directoryLoadingCanceled();
509 * Is emitted after DolphinView::setUrl() has been invoked and provides
510 * the information how much percent of the current directory have been loaded.
512 void directoryLoadingProgress(int percent
);
515 * Is emitted if the sorting is done asynchronously and provides the
516 * progress information of the sorting.
518 void directorySortingProgress(int percent
);
521 * Emitted when the file-item-model emits redirection.
522 * Testcase: fish://localhost
524 void redirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
527 * Is emitted when the URL set by DolphinView::setUrl() represents a file.
528 * In this case no signal errorMessage() will be emitted.
530 void urlIsFileError(const KUrl
& url
);
533 * Is emitted when the write state of the folder has been changed. The application
534 * should disable all actions like "Create New..." that depend on the write
537 void writeStateChanged(bool isFolderWritable
);
540 * Is emitted if the URL should be changed to the previous URL of the
541 * history (e.g. because the "back"-mousebutton has been pressed).
543 void goBackRequested();
546 * Is emitted if the URL should be changed to the next URL of the
547 * history (e.g. because the "next"-mousebutton has been pressed).
549 void goForwardRequested();
552 /** Changes the zoom level if Control is pressed during a wheel event. */
553 virtual void wheelEvent(QWheelEvent
* event
);
556 virtual void hideEvent(QHideEvent
* event
);
557 virtual bool event(QEvent
* event
);
561 * Marks the view as active (DolphinView:isActive() will return true)
562 * and emits the 'activated' signal if it is not already active.
566 void slotItemActivated(int index
);
567 void slotItemsActivated(const KItemSet
& indexes
);
568 void slotItemMiddleClicked(int index
);
569 void slotItemContextMenuRequested(int index
, const QPointF
& pos
);
570 void slotViewContextMenuRequested(const QPointF
& pos
);
571 void slotHeaderContextMenuRequested(const QPointF
& pos
);
572 void slotHeaderColumnWidthChanged(const QByteArray
& role
, qreal current
, qreal previous
);
573 void slotItemHovered(int index
);
574 void slotItemUnhovered(int index
);
575 void slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
);
576 void slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
);
577 void slotMouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
);
580 * Is called when new items get pasted or dropped.
582 void slotAboutToCreate(const KUrl::List
& urls
);
585 * Emits the signal \a selectionChanged() with a small delay. This is
586 * because getting all file items for the selection can be an expensive
587 * operation. Fast selection changes are collected in this case and
588 * the signal is emitted only after no selection change has been done
589 * within a small delay.
591 void slotSelectionChanged(const KItemSet
& current
, const KItemSet
& previous
);
594 * Is called by emitDelayedSelectionChangedSignal() and emits the
595 * signal \a selectionChanged() with all selected file items as parameter.
597 void emitSelectionChangedSignal();
600 * Updates the view properties of the current URL to the
601 * sorting given by \a role.
603 void updateSortRole(const QByteArray
& role
);
606 * Updates the view properties of the current URL to the
607 * sort order given by \a order.
609 void updateSortOrder(Qt::SortOrder order
);
612 * Updates the view properties of the current URL to the
613 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
615 void updateSortFoldersFirst(bool foldersFirst
);
618 * Indicates in the status bar that the delete operation
619 * of the job \a job has been finished.
621 void slotDeleteFileFinished(KJob
* job
);
623 void slotRenamingFailed(const KUrl
& oldUrl
, const KUrl
& newUrl
);
626 * Invoked when the file item model has started the loading
627 * of the directory specified by DolphinView::url().
629 void slotDirectoryLoadingStarted();
632 * Invoked when the file item model indicates that the loading of a directory has
633 * been completed. Assures that pasted items and renamed items get seleced.
635 void slotDirectoryLoadingCompleted();
638 * Is invoked when items of KFileItemModel have been changed.
640 void slotItemsChanged();
643 * Is invoked when the sort order has been changed by the user by clicking
644 * on a header item. The view properties of the directory will get updated.
646 void slotSortOrderChangedByHeader(Qt::SortOrder current
, Qt::SortOrder previous
);
649 * Is invoked when the sort role has been changed by the user by clicking
650 * on a header item. The view properties of the directory will get updated.
652 void slotSortRoleChangedByHeader(const QByteArray
& current
, const QByteArray
& previous
);
655 * Is invoked when the visible roles have been changed by the user by dragging
656 * a header item. The view properties of the directory will get updated.
658 void slotVisibleRolesChangedByHeader(const QList
<QByteArray
>& current
,
659 const QList
<QByteArray
>& previous
);
661 void slotRoleEditingCanceled();
662 void slotRoleEditingFinished(int index
, const QByteArray
& role
, const QVariant
& value
);
665 * Observes the item with the URL \a url. As soon as the directory
666 * model indicates that the item is available, the item will
667 * get selected and it is assured that the item stays visible.
669 void observeCreatedItem(const KUrl
& url
);
672 * Called when a redirection happens.
673 * Testcase: fish://localhost
675 void slotDirectoryRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
678 * Applies the state that has been restored by restoreViewState()
681 void updateViewState();
686 * Calculates the number of currently shown files into
687 * \a fileCount and the number of folders into \a folderCount.
688 * The size of all files is written into \a totalFileSize.
689 * It is recommend using this method instead of asking the
690 * directory lister or the model directly, as it takes
691 * filtering and hierarchical previews into account.
693 void calculateItemCount(int& fileCount
, int& folderCount
, KIO::filesize_t
& totalFileSize
) const;
696 void loadDirectory(const KUrl
& url
, bool reload
= false);
699 * Applies the view properties which are defined by the current URL
700 * to the DolphinView properties. The view properties are read from a
701 * .directory file either in the current directory, or in the
702 * share/apps/dolphin/view_properties/ subfolder of the user's .kde folder.
704 void applyViewProperties();
707 * Applies the given view properties to the DolphinView.
709 void applyViewProperties(const ViewProperties
& props
);
712 * Applies the m_mode property to the corresponding
713 * itemlayout-property of the KItemListView.
715 void applyModeToView();
718 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
719 * Pastes the clipboard data into the URL \a url.
721 void pasteToUrl(const KUrl
& url
);
724 * Returns a list of URLs for all selected items. The list is
725 * simplified, so that when the URLs are part of different tree
726 * levels, only the parent is returned.
728 KUrl::List
simplifiedSelectedUrls() const;
731 * Returns the MIME data for all selected items.
733 QMimeData
* selectionMimeData() const;
736 * Updates m_isFolderWritable dependent on whether the folder represented by
737 * the current URL is writable. If the state has changed, the signal
738 * writeableStateChanged() will be emitted.
740 void updateWritableState();
743 * @return The current URL if no viewproperties-context is given (see
744 * DolphinView::viewPropertiesContext(), otherwise the context
747 KUrl
viewPropertiesUrl() const;
752 bool m_assureVisibleCurrentIndex
;
753 bool m_isFolderWritable
;
754 bool m_dragging
; // True if a dragging is done. Required to be able to decide whether a
755 // tooltip may be shown when hovering an item.
758 QString m_viewPropertiesContext
;
760 QList
<QByteArray
> m_visibleRoles
;
762 QVBoxLayout
* m_topLayout
;
764 KFileItemModel
* m_model
;
765 DolphinItemListView
* m_view
;
766 KItemListContainer
* m_container
;
768 ToolTipManager
* m_toolTipManager
;
770 QTimer
* m_selectionChangedTimer
;
772 KUrl m_currentItemUrl
; // Used for making the view to remember the current URL after F5
773 bool m_scrollToCurrentItem
; // Used for marking we need to scroll to current item or not
774 QPoint m_restoredContentsPosition
;
776 QList
<KUrl
> m_selectedUrls
; // Used for making the view to remember selections after F5
777 bool m_clearSelectionBeforeSelectingNewItems
;
778 bool m_markFirstNewlySelectedItemAsCurrent
;
780 VersionControlObserver
* m_versionControlObserver
;
783 friend class TestBase
;
784 friend class DolphinDetailsViewTest
;
785 friend class DolphinPart
; // Accesses m_model
788 /// Allow using DolphinView::Mode in QVariant
789 Q_DECLARE_METATYPE(DolphinView::Mode
)
791 #endif // DOLPHINVIEW_H