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 setItemSelectionEnabled(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;
203 * Returns true, if zooming in is possible. If false is returned,
204 * the maximum zooming level has been reached.
206 bool isZoomInPossible() const;
209 * Returns true, if zooming out is possible. If false is returned,
210 * the minimum zooming level has been reached.
212 bool isZoomOutPossible() const;
214 void setSortRole(const QByteArray
& role
);
215 QByteArray
sortRole() const;
217 void setSortOrder(Qt::SortOrder order
);
218 Qt::SortOrder
sortOrder() const;
220 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
221 void setSortFoldersFirst(bool foldersFirst
);
222 bool sortFoldersFirst() const;
224 /** Sets the additional information which should be shown for the items. */
225 void setVisibleRoles(const QList
<QByteArray
>& roles
);
227 /** Returns the additional information which should be shown for the items. */
228 QList
<QByteArray
> visibleRoles() const;
230 /** Reloads the current directory. */
236 * Refreshes the view to get synchronized with the settings (e.g. icons size,
242 * Saves the current settings (e.g. icons size, font, ..).
244 void writeSettings();
247 * Filters the currently shown items by \a nameFilter. All items
248 * which contain the given filter string will be shown.
250 void setNameFilter(const QString
& nameFilter
);
251 QString
nameFilter() const;
254 * Calculates the number of currently shown files into
255 * \a fileCount and the number of folders into \a folderCount.
256 * The size of all files is written into \a totalFileSize.
257 * It is recommend using this method instead of asking the
258 * directory lister or the model directly, as it takes
259 * filtering and hierarchical previews into account.
261 void calculateItemCount(int& fileCount
, int& folderCount
, KIO::filesize_t
& totalFileSize
) const;
264 * Returns a textual representation of the state of the current
265 * folder or selected items, suitable for use in the status bar.
267 QString
statusBarText() const;
270 * Returns the version control actions that are provided for the items \p items.
271 * Usually the actions are presented in the context menu.
273 QList
<QAction
*> versionControlActions(const KFileItemList
& items
) const;
276 * Returns the state of the paste action:
277 * first is whether the action should be enabled
278 * second is the text for the action
280 QPair
<bool, QString
> pasteInfo() const;
283 * If \a tabsForFiles is true, the signal tabRequested() will also
284 * emitted also for files. Per default tabs for files is disabled
285 * and hence the signal tabRequested() will only be emitted for
288 void setTabsForFilesEnabled(bool tabsForFiles
);
289 bool isTabsForFilesEnabled() const;
292 * Returns true if the current view allows folders to be expanded,
293 * i.e. presents a hierarchical view to the user.
295 bool itemsExpandable() const;
298 * Restores the view state (current item, contents position, details view expansion state)
300 void restoreState(QDataStream
& stream
);
303 * Saves the view state (current item, contents position, details view expansion state)
305 void saveState(QDataStream
& stream
);
307 /** Returns true, if at least one item is selected. */
308 bool hasSelection() const;
311 * Returns the root item which represents the current URL.
313 KFileItem
rootItem() const;
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();
372 /** Activates the view if the item list container gets focus. */
373 virtual bool eventFilter(QObject
* watched
, QEvent
* event
);
377 * Is emitted if the view has been activated by e. g. a mouse click.
382 * Is emitted if the URL of the view will be changed to \a url.
383 * After the URL has been changed the signal urlChanged() will
386 void urlAboutToBeChanged(const KUrl
& url
);
388 /** Is emitted if the URL of the view has been changed to \a url. */
389 void urlChanged(const KUrl
& url
);
392 * Is emitted when clicking on an item with the left mouse button.
394 void itemActivated(const KFileItem
& item
);
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 KUrl
& url
);
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 directory \a url is currently 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 startedDirLoading(const KUrl
& url
);
489 * Is emitted after the directory triggered by DolphinView::setUrl()
492 void finishedDirLoading(const KUrl
& url
);
495 * Is emitted after DolphinView::setUrl() has been invoked and provides
496 * the information how much percent of the current directory have been loaded.
498 void dirLoadingProgress(int percent
);
501 * Is emitted if the sorting is done asynchronously and provides the
502 * progress information of the sorting.
504 void dirSortingProgress(int percent
);
507 * Is emitted if the DolphinView::setUrl() is invoked but the URL is not
510 void urlIsFileError(const KUrl
& file
);
513 * Emitted when the file-item-model emits redirection.
514 * Testcase: fish://localhost
516 void redirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
519 * Is emitted when the write state of the folder has been changed. The application
520 * should disable all actions like "Create New..." that depend on the write
523 void writeStateChanged(bool isFolderWritable
);
526 * Is emitted if the URL should be changed to the previous URL of the
527 * history (e.g. because the "back"-mousebutton has been pressed).
529 void goBackRequested();
532 * Is emitted if the URL should be changed to the next URL of the
533 * history (e.g. because the "next"-mousebutton has been pressed).
535 void goForwardRequested();
538 /** Changes the zoom level if Control is pressed during a wheel event. */
539 virtual void wheelEvent(QWheelEvent
* event
);
542 virtual void hideEvent(QHideEvent
* event
);
546 * Marks the view as active (DolphinView:isActive() will return true)
547 * and emits the 'activated' signal if it is not already active.
551 void slotItemActivated(int index
);
552 void slotItemsActivated(const QSet
<int>& indexes
);
553 void slotItemMiddleClicked(int index
);
554 void slotItemContextMenuRequested(int index
, const QPointF
& pos
);
555 void slotViewContextMenuRequested(const QPointF
& pos
);
556 void slotHeaderContextMenuRequested(const QPointF
& pos
);
557 void slotHeaderColumnWidthChanged(const QByteArray
& role
, qreal current
, qreal previous
);
558 void slotItemHovered(int index
);
559 void slotItemUnhovered(int index
);
560 void slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
);
561 void slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
);
562 void slotMouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
);
565 * Emits the signal \a selectionChanged() with a small delay. This is
566 * because getting all file items for the selection can be an expensive
567 * operation. Fast selection changes are collected in this case and
568 * the signal is emitted only after no selection change has been done
569 * within a small delay.
571 void slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
);
574 * Is called by emitDelayedSelectionChangedSignal() and emits the
575 * signal \a selectionChanged() with all selected file items as parameter.
577 void emitSelectionChangedSignal();
580 * Updates the view properties of the current URL to the
581 * sorting given by \a role.
583 void updateSortRole(const QByteArray
& role
);
586 * Updates the view properties of the current URL to the
587 * sort order given by \a order.
589 void updateSortOrder(Qt::SortOrder order
);
592 * Updates the view properties of the current URL to the
593 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
595 void updateSortFoldersFirst(bool foldersFirst
);
598 * Updates the status bar to show hover information for the
599 * item \a item. If currently other items are selected,
600 * no hover information is shown.
601 * @see DolphinView::clearHoverInformation()
603 void showHoverInformation(const KFileItem
& item
);
606 * Clears the hover information shown in the status bar.
607 * @see DolphinView::showHoverInformation().
609 void clearHoverInformation();
612 * Indicates in the status bar that the delete operation
613 * of the job \a job has been finished.
615 void slotDeleteFileFinished(KJob
* job
);
618 * Invoked when the file item model has started the loading
619 * of the directory specified by DolphinView::url().
621 void slotDirLoadingStarted();
624 * Invoked when the file item model indicates that the loading of a directory has
625 * been completed. Assures that pasted items and renamed items get seleced.
627 void slotDirLoadingCompleted();
630 * Is invoked when items of KFileItemModel have been changed.
632 void slotItemsChanged();
635 * Is invoked when the sort order has been changed by the user by clicking
636 * on a header item. The view properties of the directory will get updated.
638 void slotSortOrderChangedByHeader(Qt::SortOrder current
, Qt::SortOrder previous
);
641 * Is invoked when the sort role has been changed by the user by clicking
642 * on a header item. The view properties of the directory will get updated.
644 void slotSortRoleChangedByHeader(const QByteArray
& current
, const QByteArray
& previous
);
647 * Is invoked when the visible roles have been changed by the user by dragging
648 * a header item. The view properties of the directory will get updated.
650 void slotVisibleRolesChangedByHeader(const QList
<QByteArray
>& current
,
651 const QList
<QByteArray
>& previous
);
654 * Observes the item with the URL \a url. As soon as the directory
655 * model indicates that the item is available, the item will
656 * get selected and it is assured that the item stays visible.
658 * @see selectAndScrollToCreatedItem()
660 void observeCreatedItem(const KUrl
& url
);
663 * Selects and scrolls to the item that got observed
664 * by observeCreatedItem().
666 void selectAndScrollToCreatedItem();
669 * Called when a redirection happens.
670 * Testcase: fish://localhost
672 void slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
675 * Applies the state that has been restored by restoreViewState()
678 void updateViewState();
683 KFileItemModel
* fileItemModel() const;
685 void loadDirectory(const KUrl
& url
, bool reload
= false);
688 * Applies the view properties which are defined by the current URL
689 * to the DolphinView properties.
691 void applyViewProperties();
694 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
695 * Pastes the clipboard data into the URL \a url.
697 void pasteToUrl(const KUrl
& url
);
700 * Returns a list of URLs for all selected items. The list is
701 * simplified, so that when the URLs are part of different tree
702 * levels, only the parent is returned.
704 KUrl::List
simplifiedSelectedUrls() const;
707 * Returns the MIME data for all selected items.
709 QMimeData
* selectionMimeData() const;
712 * Is invoked after a paste operation or a drag & drop
713 * operation and URLs from \a mimeData as selected.
714 * This allows to select all newly pasted
715 * items in restoreViewState().
717 void markPastedUrlsAsSelected(const QMimeData
* mimeData
);
720 * Updates m_isFolderWritable dependent on whether the folder represented by
721 * the current URL is writable. If the state has changed, the signal
722 * writeableStateChanged() will be emitted.
724 void updateWritableState();
729 bool m_assureVisibleCurrentIndex
;
730 bool m_isFolderWritable
;
731 bool m_dragging
; // True if a dragging is done. Required to be able to decide whether a
732 // tooltip may be shown when hovering an item.
736 QList
<QByteArray
> m_visibleRoles
;
738 QVBoxLayout
* m_topLayout
;
740 DolphinItemListContainer
* m_container
;
742 ToolTipManager
* m_toolTipManager
;
744 QTimer
* m_selectionChangedTimer
;
746 KUrl m_currentItemUrl
; // Used for making the view to remember the current URL after F5
747 QPoint m_restoredContentsPosition
;
748 KUrl m_createdItemUrl
; // URL for a new item that got created by the "Create New..." menu
750 QList
<KUrl
> m_selectedUrls
; // Used for making the view to remember selections after F5
752 VersionControlObserver
* m_versionControlObserver
;
755 friend class TestBase
;
756 friend class DolphinDetailsViewTest
;
759 /// Allow using DolphinView::Mode in QVariant
760 Q_DECLARE_METATYPE(DolphinView::Mode
)
762 #endif // DOLPHINVIEW_H