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-nepomuk.h>
26 #include "libdolphin_export.h"
28 #include <kparts/part.h>
30 #include <KFileItemDelegate>
31 #include <kio/fileundomanager.h>
36 #include <QLinkedList>
40 typedef KIO::FileUndoManager::CommandType CommandType
;
42 class DolphinItemListContainer
;
44 class KActionCollection
;
49 class VersionControlObserver
;
51 class QGraphicsSceneDragDropEvent
;
55 * @short Represents a view for the directory content.
57 * View modes for icons, compact and details are supported. It's
65 class LIBDOLPHINPRIVATE_EXPORT DolphinView
: public QWidget
71 * Defines the view mode for a directory. The
72 * view mode is automatically updated if the directory itself
73 * defines a view mode (see class ViewProperties for details).
78 * The items are shown as icons with a name-label below.
83 * The icon, the name and the size of the items are
84 * shown per default as a table.
89 * The items are shown as icons with the name-label aligned
96 * @param url Specifies the content which should be shown.
97 * @param parent Parent widget of the view.
99 DolphinView(const KUrl
& url
, QWidget
* parent
);
101 virtual ~DolphinView();
104 * Returns the current active URL, where all actions are applied.
105 * The URL navigator is synchronized with this URL.
110 * If \a active is true, the view will marked as active. The active
111 * view is defined as view where all actions are applied to.
113 void setActive(bool active
);
114 bool isActive() const;
117 * Changes the view mode for the current directory to \a mode.
118 * If the view properties should be remembered for each directory
119 * (GeneralSettings::globalViewProps() returns false), then the
120 * changed view mode will be stored automatically.
122 void setMode(Mode mode
);
126 * Turns on the file preview for the all files of the current directory,
127 * if \a show is true.
128 * If the view properties should be remembered for each directory
129 * (GeneralSettings::globalViewProps() returns false), then the
130 * preview setting will be stored automatically.
132 void setPreviewsShown(bool show
);
133 bool previewsShown() const;
136 * Shows all hidden files of the current directory,
137 * if \a show is true.
138 * If the view properties should be remembered for each directory
139 * (GeneralSettings::globalViewProps() returns false), then the
140 * show hidden file setting will be stored automatically.
142 void setHiddenFilesShown(bool show
);
143 bool hiddenFilesShown() const;
146 * Turns on sorting by groups if \a enable is true.
148 void setGroupedSorting(bool grouped
);
149 bool groupedSorting() const;
152 * Returns the items of the view.
154 KFileItemList
items() const;
157 * @return The number of items. itemsCount() is faster in comparison
158 * to items().count().
160 int itemsCount() const;
163 * Returns the selected items. The list is empty if no item has been
166 KFileItemList
selectedItems() const;
169 * Returns the number of selected items (this is faster than
170 * invoking selectedItems().count()).
172 int selectedItemsCount() const;
175 * Marks the items indicated by \p urls to get selected after the
176 * directory DolphinView::url() has been loaded. Note that nothing
177 * gets selected if no loading of a directory has been triggered
178 * by DolphinView::setUrl() or DolphinView::reload().
180 void markUrlsAsSelected(const QList
<KUrl
>& urls
);
183 * Marks the item indicated by \p url as the current item after the
184 * directory DolphinView::url() has been loaded.
186 void markUrlAsCurrent(const KUrl
& url
);
189 * All items that match to the pattern \a pattern will get selected
190 * if \a enabled is true and deselected if \a enabled is false.
192 void selectItems(const QRegExp
& pattern
, bool enabled
);
195 * Sets the zoom level to \a level. It is assured that the used
196 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
197 * ZoomLevelInfo::maximumLevel().
199 void setZoomLevel(int level
);
200 int zoomLevel() const;
202 void setSortRole(const QByteArray
& role
);
203 QByteArray
sortRole() const;
205 void setSortOrder(Qt::SortOrder order
);
206 Qt::SortOrder
sortOrder() const;
208 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
209 void setSortFoldersFirst(bool foldersFirst
);
210 bool sortFoldersFirst() const;
212 /** Sets the additional information which should be shown for the items. */
213 void setVisibleRoles(const QList
<QByteArray
>& roles
);
215 /** Returns the additional information which should be shown for the items. */
216 QList
<QByteArray
> visibleRoles() const;
222 * Refreshes the view to get synchronized with the settings (e.g. icons size,
228 * Saves the current settings (e.g. icons size, font, ..).
230 void writeSettings();
233 * Filters the currently shown items by \a nameFilter. All items
234 * which contain the given filter string will be shown.
236 void setNameFilter(const QString
& nameFilter
);
237 QString
nameFilter() const;
240 * Returns a textual representation of the state of the current
241 * folder or selected items, suitable for use in the status bar.
243 QString
statusBarText() const;
246 * Returns the version control actions that are provided for the items \p items.
247 * Usually the actions are presented in the context menu.
249 QList
<QAction
*> versionControlActions(const KFileItemList
& items
) const;
252 * Returns the state of the paste action:
253 * first is whether the action should be enabled
254 * second is the text for the action
256 QPair
<bool, QString
> pasteInfo() const;
259 * If \a tabsForFiles is true, the signal tabRequested() will also
260 * emitted also for files. Per default tabs for files is disabled
261 * and hence the signal tabRequested() will only be emitted for
264 void setTabsForFilesEnabled(bool tabsForFiles
);
265 bool isTabsForFilesEnabled() const;
268 * Returns true if the current view allows folders to be expanded,
269 * i.e. presents a hierarchical view to the user.
271 bool itemsExpandable() const;
274 * Restores the view state (current item, contents position, details view expansion state)
276 void restoreState(QDataStream
& stream
);
279 * Saves the view state (current item, contents position, details view expansion state)
281 void saveState(QDataStream
& stream
);
284 * Returns the root item which represents the current URL.
286 KFileItem
rootItem() const;
290 * Changes the directory to \a url. If the current directory is equal to
291 * \a url, nothing will be done (use DolphinView::reload() instead).
293 void setUrl(const KUrl
& url
);
297 * @see DolphinView::selectedItems()
302 * Inverts the current selection: selected items get unselected,
303 * unselected items get selected.
304 * @see DolphinView::selectedItems()
306 void invertSelection();
308 void clearSelection();
311 * Triggers the renaming of the currently selected items, where
312 * the user must input a new name for the items.
314 void renameSelectedItems();
317 * Moves all selected items to the trash.
319 void trashSelectedItems();
322 * Deletes all selected items.
324 void deleteSelectedItems();
327 * Copies all selected items to the clipboard and marks
330 void cutSelectedItems();
332 /** Copies all selected items to the clipboard. */
333 void copySelectedItems();
335 /** Pastes the clipboard data to this view. */
339 * Pastes the clipboard data into the currently selected
340 * folder. If the current selection is not exactly one folder, no
341 * paste operation is done.
343 void pasteIntoFolder();
345 /** Activates the view if the item list container gets focus. */
346 virtual bool eventFilter(QObject
* watched
, QEvent
* event
);
350 * Is emitted if the view has been activated by e. g. a mouse click.
355 * Is emitted if the URL of the view will be changed to \a url.
356 * After the URL has been changed the signal urlChanged() will
359 void urlAboutToBeChanged(const KUrl
& url
);
361 /** Is emitted if the URL of the view has been changed to \a url. */
362 void urlChanged(const KUrl
& url
);
365 * Is emitted when clicking on an item with the left mouse button.
367 void itemActivated(const KFileItem
& item
);
370 * Is emitted if items have been added or deleted.
372 void itemCountChanged();
375 * Is emitted if a new tab should be opened for the URL \a url.
377 void tabRequested(const KUrl
& url
);
380 * Is emitted if the view mode (IconsView, DetailsView,
381 * PreviewsView) has been changed.
383 void modeChanged(DolphinView::Mode current
, DolphinView::Mode previous
);
385 /** Is emitted if the 'show preview' property has been changed. */
386 void previewsShownChanged(bool shown
);
388 /** Is emitted if the 'show hidden files' property has been changed. */
389 void hiddenFilesShownChanged(bool shown
);
391 /** Is emitted if the 'grouped sorting' property has been changed. */
392 void groupedSortingChanged(bool groupedSorting
);
394 /** Is emitted if the sorting by name, size or date has been changed. */
395 void sortRoleChanged(const QByteArray
& role
);
397 /** Is emitted if the sort order (ascending or descending) has been changed. */
398 void sortOrderChanged(Qt::SortOrder order
);
401 * Is emitted if the sorting of files and folders (separate with folders
402 * first or mixed) has been changed.
404 void sortFoldersFirstChanged(bool foldersFirst
);
406 /** Is emitted if the additional information shown for this view has been changed. */
407 void visibleRolesChanged(const QList
<QByteArray
>& current
,
408 const QList
<QByteArray
>& previous
);
410 /** Is emitted if the zoom level has been changed by zooming in or out. */
411 void zoomLevelChanged(int current
, int previous
);
414 * Is emitted if information of an item is requested to be shown e. g. in the panel.
415 * If item is null, no item information request is pending.
417 void requestItemInfo(const KFileItem
& item
);
420 * Is emitted whenever the selection has been changed.
422 void selectionChanged(const KFileItemList
& selection
);
425 * Is emitted if a context menu is requested for the item \a item,
426 * which is part of \a url. If the item is null, the context menu
427 * for the URL should be shown and the custom actions \a customActions
430 void requestContextMenu(const QPoint
& pos
,
431 const KFileItem
& item
,
433 const QList
<QAction
*>& customActions
);
436 * Is emitted if an information message with the content \a msg
439 void infoMessage(const QString
& msg
);
442 * Is emitted if an error message with the content \a msg
445 void errorMessage(const QString
& msg
);
448 * Is emitted if an "operation completed" message with the content \a msg
451 void operationCompletedMessage(const QString
& msg
);
454 * Is emitted after DolphinView::setUrl() has been invoked and
455 * the current directory is loaded. If this signal is emitted,
456 * it is assured that the view contains already the correct root
457 * URL and property settings.
459 void dirLoadingStarted();
462 * Is emitted after the directory triggered by DolphinView::setUrl()
465 void dirLoadingCompleted();
468 * Is emitted after DolphinView::setUrl() has been invoked and provides
469 * the information how much percent of the current directory have been loaded.
471 void dirLoadingProgress(int percent
);
474 * Is emitted if the sorting is done asynchronously and provides the
475 * progress information of the sorting.
477 void dirSortingProgress(int percent
);
480 * Emitted when the file-item-model emits redirection.
481 * Testcase: fish://localhost
483 void redirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
486 * Is emitted when the write state of the folder has been changed. The application
487 * should disable all actions like "Create New..." that depend on the write
490 void writeStateChanged(bool isFolderWritable
);
493 * Is emitted if the URL should be changed to the previous URL of the
494 * history (e.g. because the "back"-mousebutton has been pressed).
496 void goBackRequested();
499 * Is emitted if the URL should be changed to the next URL of the
500 * history (e.g. because the "next"-mousebutton has been pressed).
502 void goForwardRequested();
505 /** Changes the zoom level if Control is pressed during a wheel event. */
506 virtual void wheelEvent(QWheelEvent
* event
);
509 virtual void hideEvent(QHideEvent
* event
);
513 * Marks the view as active (DolphinView:isActive() will return true)
514 * and emits the 'activated' signal if it is not already active.
518 void slotItemActivated(int index
);
519 void slotItemsActivated(const QSet
<int>& indexes
);
520 void slotItemMiddleClicked(int index
);
521 void slotItemContextMenuRequested(int index
, const QPointF
& pos
);
522 void slotViewContextMenuRequested(const QPointF
& pos
);
523 void slotHeaderContextMenuRequested(const QPointF
& pos
);
524 void slotHeaderColumnWidthChanged(const QByteArray
& role
, qreal current
, qreal previous
);
525 void slotItemHovered(int index
);
526 void slotItemUnhovered(int index
);
527 void slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
);
528 void slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
);
529 void slotMouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
);
532 * Emits the signal \a selectionChanged() with a small delay. This is
533 * because getting all file items for the selection can be an expensive
534 * operation. Fast selection changes are collected in this case and
535 * the signal is emitted only after no selection change has been done
536 * within a small delay.
538 void slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
);
541 * Is called by emitDelayedSelectionChangedSignal() and emits the
542 * signal \a selectionChanged() with all selected file items as parameter.
544 void emitSelectionChangedSignal();
547 * Updates the view properties of the current URL to the
548 * sorting given by \a role.
550 void updateSortRole(const QByteArray
& role
);
553 * Updates the view properties of the current URL to the
554 * sort order given by \a order.
556 void updateSortOrder(Qt::SortOrder order
);
559 * Updates the view properties of the current URL to the
560 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
562 void updateSortFoldersFirst(bool foldersFirst
);
565 * Updates the status bar to show hover information for the
566 * item \a item. If currently other items are selected,
567 * no hover information is shown.
568 * @see DolphinView::clearHoverInformation()
570 void showHoverInformation(const KFileItem
& item
);
573 * Clears the hover information shown in the status bar.
574 * @see DolphinView::showHoverInformation().
576 void clearHoverInformation();
579 * Indicates in the status bar that the delete operation
580 * of the job \a job has been finished.
582 void slotDeleteFileFinished(KJob
* job
);
585 * Invoked when the file item model has started the loading
586 * of the directory specified by DolphinView::url().
588 void slotDirLoadingStarted();
591 * Invoked when the file item model indicates that the loading of a directory has
592 * been completed. Assures that pasted items and renamed items get seleced.
594 void slotDirLoadingCompleted();
597 * Is invoked when items of KFileItemModel have been changed.
599 void slotItemsChanged();
602 * Is invoked when the sort order has been changed by the user by clicking
603 * on a header item. The view properties of the directory will get updated.
605 void slotSortOrderChangedByHeader(Qt::SortOrder current
, Qt::SortOrder previous
);
608 * Is invoked when the sort role has been changed by the user by clicking
609 * on a header item. The view properties of the directory will get updated.
611 void slotSortRoleChangedByHeader(const QByteArray
& current
, const QByteArray
& previous
);
614 * Is invoked when the visible roles have been changed by the user by dragging
615 * a header item. The view properties of the directory will get updated.
617 void slotVisibleRolesChangedByHeader(const QList
<QByteArray
>& current
,
618 const QList
<QByteArray
>& previous
);
621 * Observes the item with the URL \a url. As soon as the directory
622 * model indicates that the item is available, the item will
623 * get selected and it is assured that the item stays visible.
625 * @see selectAndScrollToCreatedItem()
627 void observeCreatedItem(const KUrl
& url
);
630 * Selects and scrolls to the item that got observed
631 * by observeCreatedItem().
633 void selectAndScrollToCreatedItem();
636 * Called when a redirection happens.
637 * Testcase: fish://localhost
639 void slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
642 * Applies the state that has been restored by restoreViewState()
645 void updateViewState();
650 * Calculates the number of currently shown files into
651 * \a fileCount and the number of folders into \a folderCount.
652 * The size of all files is written into \a totalFileSize.
653 * It is recommend using this method instead of asking the
654 * directory lister or the model directly, as it takes
655 * filtering and hierarchical previews into account.
657 void calculateItemCount(int& fileCount
, int& folderCount
, KIO::filesize_t
& totalFileSize
) const;
660 KFileItemModel
* fileItemModel() const;
662 void loadDirectory(const KUrl
& url
, bool reload
= false);
665 * Applies the view properties which are defined by the current URL
666 * to the DolphinView properties.
668 void applyViewProperties();
671 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
672 * Pastes the clipboard data into the URL \a url.
674 void pasteToUrl(const KUrl
& url
);
677 * Returns a list of URLs for all selected items. The list is
678 * simplified, so that when the URLs are part of different tree
679 * levels, only the parent is returned.
681 KUrl::List
simplifiedSelectedUrls() const;
684 * Returns the MIME data for all selected items.
686 QMimeData
* selectionMimeData() const;
689 * Is invoked after a paste operation or a drag & drop
690 * operation and URLs from \a mimeData as selected.
691 * This allows to select all newly pasted
692 * items in restoreViewState().
694 void markPastedUrlsAsSelected(const QMimeData
* mimeData
);
697 * Updates m_isFolderWritable dependent on whether the folder represented by
698 * the current URL is writable. If the state has changed, the signal
699 * writeableStateChanged() will be emitted.
701 void updateWritableState();
706 bool m_assureVisibleCurrentIndex
;
707 bool m_isFolderWritable
;
708 bool m_dragging
; // True if a dragging is done. Required to be able to decide whether a
709 // tooltip may be shown when hovering an item.
713 QList
<QByteArray
> m_visibleRoles
;
715 QVBoxLayout
* m_topLayout
;
717 DolphinItemListContainer
* m_container
;
719 ToolTipManager
* m_toolTipManager
;
721 QTimer
* m_selectionChangedTimer
;
723 KUrl m_currentItemUrl
; // Used for making the view to remember the current URL after F5
724 QPoint m_restoredContentsPosition
;
725 KUrl m_createdItemUrl
; // URL for a new item that got created by the "Create New..." menu
727 QList
<KUrl
> m_selectedUrls
; // Used for making the view to remember selections after F5
729 VersionControlObserver
* m_versionControlObserver
;
732 friend class TestBase
;
733 friend class DolphinDetailsViewTest
;
736 /// Allow using DolphinView::Mode in QVariant
737 Q_DECLARE_METATYPE(DolphinView::Mode
)
739 #endif // DOLPHINVIEW_H