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 DolphinItemListView
;
44 class KActionCollection
;
46 class KItemListContainer
;
50 class VersionControlObserver
;
52 class QGraphicsSceneDragDropEvent
;
56 * @short Represents a view for the directory content.
58 * View modes for icons, compact and details are supported. It's
66 class LIBDOLPHINPRIVATE_EXPORT DolphinView
: public QWidget
72 * Defines the view mode for a directory. The
73 * view mode is automatically updated if the directory itself
74 * defines a view mode (see class ViewProperties for details).
79 * The items are shown as icons with a name-label below.
84 * The icon, the name and the size of the items are
85 * shown per default as a table.
90 * The items are shown as icons with the name-label aligned
97 * @param url Specifies the content which should be shown.
98 * @param parent Parent widget of the view.
100 DolphinView(const KUrl
& url
, QWidget
* parent
);
102 virtual ~DolphinView();
105 * Returns the current active URL, where all actions are applied.
106 * The URL navigator is synchronized with this URL.
111 * If \a active is true, the view will marked as active. The active
112 * view is defined as view where all actions are applied to.
114 void setActive(bool active
);
115 bool isActive() const;
118 * Changes the view mode for the current directory to \a mode.
119 * If the view properties should be remembered for each directory
120 * (GeneralSettings::globalViewProps() returns false), then the
121 * changed view mode will be stored automatically.
123 void setMode(Mode mode
);
127 * Turns on the file preview for the all files of the current directory,
128 * if \a show is true.
129 * If the view properties should be remembered for each directory
130 * (GeneralSettings::globalViewProps() returns false), then the
131 * preview setting will be stored automatically.
133 void setPreviewsShown(bool show
);
134 bool previewsShown() const;
137 * Shows all hidden files of the current directory,
138 * if \a show is true.
139 * If the view properties should be remembered for each directory
140 * (GeneralSettings::globalViewProps() returns false), then the
141 * show hidden file setting will be stored automatically.
143 void setHiddenFilesShown(bool show
);
144 bool hiddenFilesShown() const;
147 * Turns on sorting by groups if \a enable is true.
149 void setGroupedSorting(bool grouped
);
150 bool groupedSorting() const;
153 * Returns the items of the view.
155 KFileItemList
items() const;
158 * @return The number of items. itemsCount() is faster in comparison
159 * to items().count().
161 int itemsCount() const;
164 * Returns the selected items. The list is empty if no item has been
167 KFileItemList
selectedItems() const;
170 * Returns the number of selected items (this is faster than
171 * invoking selectedItems().count()).
173 int selectedItemsCount() const;
176 * Marks the items indicated by \p urls to get selected after the
177 * directory DolphinView::url() has been loaded. Note that nothing
178 * gets selected if no loading of a directory has been triggered
179 * by DolphinView::setUrl() or DolphinView::reload().
181 void markUrlsAsSelected(const QList
<KUrl
>& urls
);
184 * Marks the item indicated by \p url as the current item after the
185 * directory DolphinView::url() has been loaded.
187 void markUrlAsCurrent(const KUrl
& url
);
190 * All items that match to the pattern \a pattern will get selected
191 * if \a enabled is true and deselected if \a enabled is false.
193 void selectItems(const QRegExp
& pattern
, bool enabled
);
196 * Sets the zoom level to \a level. It is assured that the used
197 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
198 * ZoomLevelInfo::maximumLevel().
200 void setZoomLevel(int level
);
201 int zoomLevel() const;
203 void setSortRole(const QByteArray
& role
);
204 QByteArray
sortRole() const;
206 void setSortOrder(Qt::SortOrder order
);
207 Qt::SortOrder
sortOrder() const;
209 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
210 void setSortFoldersFirst(bool foldersFirst
);
211 bool sortFoldersFirst() const;
213 /** Sets the additional information which should be shown for the items. */
214 void setVisibleRoles(const QList
<QByteArray
>& roles
);
216 /** Returns the additional information which should be shown for the items. */
217 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 * Returns a textual representation of the state of the current
242 * folder or selected items, suitable for use in the status bar.
244 QString
statusBarText() const;
247 * Returns the version control actions that are provided for the items \p items.
248 * Usually the actions are presented in the context menu.
250 QList
<QAction
*> versionControlActions(const KFileItemList
& items
) const;
253 * Returns the state of the paste action:
254 * first is whether the action should be enabled
255 * second is the text for the action
257 QPair
<bool, QString
> pasteInfo() const;
260 * If \a tabsForFiles is true, the signal tabRequested() will also
261 * emitted also for files. Per default tabs for files is disabled
262 * and hence the signal tabRequested() will only be emitted for
265 void setTabsForFilesEnabled(bool tabsForFiles
);
266 bool isTabsForFilesEnabled() const;
269 * Returns true if the current view allows folders to be expanded,
270 * i.e. presents a hierarchical view to the user.
272 bool itemsExpandable() const;
275 * Restores the view state (current item, contents position, details view expansion state)
277 void restoreState(QDataStream
& stream
);
280 * Saves the view state (current item, contents position, details view expansion state)
282 void saveState(QDataStream
& stream
);
285 * Returns the root item which represents the current URL.
287 KFileItem
rootItem() const;
290 * Sets a context that is used for remembering the view-properties.
291 * Per default the context is empty and the path of the currently set URL
292 * is used for remembering the view-properties. Setting a custom context
293 * makes sense if specific types of URLs (e.g. search-URLs) should
294 * share common view-properties.
296 void setViewPropertiesContext(const QString
& context
);
297 QString
viewPropertiesContext() const;
301 * Changes the directory to \a url. If the current directory is equal to
302 * \a url, nothing will be done (use DolphinView::reload() instead).
304 void setUrl(const KUrl
& url
);
308 * @see DolphinView::selectedItems()
313 * Inverts the current selection: selected items get unselected,
314 * unselected items get selected.
315 * @see DolphinView::selectedItems()
317 void invertSelection();
319 void clearSelection();
322 * Triggers the renaming of the currently selected items, where
323 * the user must input a new name for the items.
325 void renameSelectedItems();
328 * Moves all selected items to the trash.
330 void trashSelectedItems();
333 * Deletes all selected items.
335 void deleteSelectedItems();
338 * Copies all selected items to the clipboard and marks
341 void cutSelectedItems();
343 /** Copies all selected items to the clipboard. */
344 void copySelectedItems();
346 /** Pastes the clipboard data to this view. */
350 * Pastes the clipboard data into the currently selected
351 * folder. If the current selection is not exactly one folder, no
352 * paste operation is done.
354 void pasteIntoFolder();
356 /** Activates the view if the item list container gets focus. */
357 virtual bool eventFilter(QObject
* watched
, QEvent
* event
);
361 * Is emitted if the view has been activated by e. g. a mouse click.
366 * Is emitted if the URL of the view will be changed to \a url.
367 * After the URL has been changed the signal urlChanged() will
370 void urlAboutToBeChanged(const KUrl
& url
);
372 /** Is emitted if the URL of the view has been changed to \a url. */
373 void urlChanged(const KUrl
& url
);
376 * Is emitted when clicking on an item with the left mouse button.
378 void itemActivated(const KFileItem
& item
);
381 * Is emitted if items have been added or deleted.
383 void itemCountChanged();
386 * Is emitted if a new tab should be opened for the URL \a url.
388 void tabRequested(const KUrl
& url
);
391 * Is emitted if the view mode (IconsView, DetailsView,
392 * PreviewsView) has been changed.
394 void modeChanged(DolphinView::Mode current
, DolphinView::Mode previous
);
396 /** Is emitted if the 'show preview' property has been changed. */
397 void previewsShownChanged(bool shown
);
399 /** Is emitted if the 'show hidden files' property has been changed. */
400 void hiddenFilesShownChanged(bool shown
);
402 /** Is emitted if the 'grouped sorting' property has been changed. */
403 void groupedSortingChanged(bool groupedSorting
);
405 /** Is emitted if the sorting by name, size or date has been changed. */
406 void sortRoleChanged(const QByteArray
& role
);
408 /** Is emitted if the sort order (ascending or descending) has been changed. */
409 void sortOrderChanged(Qt::SortOrder order
);
412 * Is emitted if the sorting of files and folders (separate with folders
413 * first or mixed) has been changed.
415 void sortFoldersFirstChanged(bool foldersFirst
);
417 /** Is emitted if the additional information shown for this view has been changed. */
418 void visibleRolesChanged(const QList
<QByteArray
>& current
,
419 const QList
<QByteArray
>& previous
);
421 /** Is emitted if the zoom level has been changed by zooming in or out. */
422 void zoomLevelChanged(int current
, int previous
);
425 * Is emitted if information of an item is requested to be shown e. g. in the panel.
426 * If item is null, no item information request is pending.
428 void requestItemInfo(const KFileItem
& item
);
431 * Is emitted whenever the selection has been changed.
433 void selectionChanged(const KFileItemList
& selection
);
436 * Is emitted if a context menu is requested for the item \a item,
437 * which is part of \a url. If the item is null, the context menu
438 * for the URL should be shown and the custom actions \a customActions
441 void requestContextMenu(const QPoint
& pos
,
442 const KFileItem
& item
,
444 const QList
<QAction
*>& customActions
);
447 * Is emitted if an information message with the content \a msg
450 void infoMessage(const QString
& msg
);
453 * Is emitted if an error message with the content \a msg
456 void errorMessage(const QString
& msg
);
459 * Is emitted if an "operation completed" message with the content \a msg
462 void operationCompletedMessage(const QString
& msg
);
465 * Is emitted after DolphinView::setUrl() has been invoked and
466 * the current directory is loaded. If this signal is emitted,
467 * it is assured that the view contains already the correct root
468 * URL and property settings.
470 void directoryLoadingStarted();
473 * Is emitted after the directory triggered by DolphinView::setUrl()
476 void directoryLoadingCompleted();
479 * Is emitted after DolphinView::setUrl() has been invoked and provides
480 * the information how much percent of the current directory have been loaded.
482 void directoryLoadingProgress(int percent
);
485 * Is emitted if the sorting is done asynchronously and provides the
486 * progress information of the sorting.
488 void directorySortingProgress(int percent
);
491 * Emitted when the file-item-model emits redirection.
492 * Testcase: fish://localhost
494 void redirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
497 * Is emitted when the write state of the folder has been changed. The application
498 * should disable all actions like "Create New..." that depend on the write
501 void writeStateChanged(bool isFolderWritable
);
504 * Is emitted if the URL should be changed to the previous URL of the
505 * history (e.g. because the "back"-mousebutton has been pressed).
507 void goBackRequested();
510 * Is emitted if the URL should be changed to the next URL of the
511 * history (e.g. because the "next"-mousebutton has been pressed).
513 void goForwardRequested();
516 /** Changes the zoom level if Control is pressed during a wheel event. */
517 virtual void wheelEvent(QWheelEvent
* event
);
520 virtual void hideEvent(QHideEvent
* event
);
524 * Marks the view as active (DolphinView:isActive() will return true)
525 * and emits the 'activated' signal if it is not already active.
529 void slotItemActivated(int index
);
530 void slotItemsActivated(const QSet
<int>& indexes
);
531 void slotItemMiddleClicked(int index
);
532 void slotItemContextMenuRequested(int index
, const QPointF
& pos
);
533 void slotViewContextMenuRequested(const QPointF
& pos
);
534 void slotHeaderContextMenuRequested(const QPointF
& pos
);
535 void slotHeaderColumnWidthChanged(const QByteArray
& role
, qreal current
, qreal previous
);
536 void slotItemHovered(int index
);
537 void slotItemUnhovered(int index
);
538 void slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
);
539 void slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
);
540 void slotMouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
);
543 * Emits the signal \a selectionChanged() with a small delay. This is
544 * because getting all file items for the selection can be an expensive
545 * operation. Fast selection changes are collected in this case and
546 * the signal is emitted only after no selection change has been done
547 * within a small delay.
549 void slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
);
552 * Is called by emitDelayedSelectionChangedSignal() and emits the
553 * signal \a selectionChanged() with all selected file items as parameter.
555 void emitSelectionChangedSignal();
558 * Updates the view properties of the current URL to the
559 * sorting given by \a role.
561 void updateSortRole(const QByteArray
& role
);
564 * Updates the view properties of the current URL to the
565 * sort order given by \a order.
567 void updateSortOrder(Qt::SortOrder order
);
570 * Updates the view properties of the current URL to the
571 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
573 void updateSortFoldersFirst(bool foldersFirst
);
576 * Updates the status bar to show hover information for the
577 * item \a item. If currently other items are selected,
578 * no hover information is shown.
579 * @see DolphinView::clearHoverInformation()
581 void showHoverInformation(const KFileItem
& item
);
584 * Clears the hover information shown in the status bar.
585 * @see DolphinView::showHoverInformation().
587 void clearHoverInformation();
590 * Indicates in the status bar that the delete operation
591 * of the job \a job has been finished.
593 void slotDeleteFileFinished(KJob
* job
);
596 * Invoked when the file item model has started the loading
597 * of the directory specified by DolphinView::url().
599 void slotDirectoryLoadingStarted();
602 * Invoked when the file item model indicates that the loading of a directory has
603 * been completed. Assures that pasted items and renamed items get seleced.
605 void slotDirectoryLoadingCompleted();
608 * Is invoked when items of KFileItemModel have been changed.
610 void slotItemsChanged();
613 * Is invoked when the sort order has been changed by the user by clicking
614 * on a header item. The view properties of the directory will get updated.
616 void slotSortOrderChangedByHeader(Qt::SortOrder current
, Qt::SortOrder previous
);
619 * Is invoked when the sort role has been changed by the user by clicking
620 * on a header item. The view properties of the directory will get updated.
622 void slotSortRoleChangedByHeader(const QByteArray
& current
, const QByteArray
& previous
);
625 * Is invoked when the visible roles have been changed by the user by dragging
626 * a header item. The view properties of the directory will get updated.
628 void slotVisibleRolesChangedByHeader(const QList
<QByteArray
>& current
,
629 const QList
<QByteArray
>& previous
);
631 void slotRoleEditingFinished(int index
, const QByteArray
& role
, const QVariant
& value
);
634 * Observes the item with the URL \a url. As soon as the directory
635 * model indicates that the item is available, the item will
636 * get selected and it is assured that the item stays visible.
638 * @see selectAndScrollToCreatedItem()
640 void observeCreatedItem(const KUrl
& url
);
643 * Selects and scrolls to the item that got observed
644 * by observeCreatedItem().
646 void selectAndScrollToCreatedItem();
649 * Called when a redirection happens.
650 * Testcase: fish://localhost
652 void slotDirectoryRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
655 * Applies the state that has been restored by restoreViewState()
658 void updateViewState();
663 * Calculates the number of currently shown files into
664 * \a fileCount and the number of folders into \a folderCount.
665 * The size of all files is written into \a totalFileSize.
666 * It is recommend using this method instead of asking the
667 * directory lister or the model directly, as it takes
668 * filtering and hierarchical previews into account.
670 void calculateItemCount(int& fileCount
, int& folderCount
, KIO::filesize_t
& totalFileSize
) const;
673 void loadDirectory(const KUrl
& url
, bool reload
= false);
676 * Applies the view properties which are defined by the current URL
677 * to the DolphinView properties.
679 void applyViewProperties();
682 * Applies the m_mode property to the corresponding
683 * itemlayout-property of the KItemListView.
685 void applyModeToView();
688 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
689 * Pastes the clipboard data into the URL \a url.
691 void pasteToUrl(const KUrl
& url
);
694 * Returns a list of URLs for all selected items. The list is
695 * simplified, so that when the URLs are part of different tree
696 * levels, only the parent is returned.
698 KUrl::List
simplifiedSelectedUrls() const;
701 * Returns the MIME data for all selected items.
703 QMimeData
* selectionMimeData() const;
706 * Is invoked after a paste operation or a drag & drop
707 * operation and URLs from \a mimeData as selected.
708 * This allows to select all newly pasted
709 * items in restoreViewState().
711 void markPastedUrlsAsSelected(const QMimeData
* mimeData
);
714 * Updates m_isFolderWritable dependent on whether the folder represented by
715 * the current URL is writable. If the state has changed, the signal
716 * writeableStateChanged() will be emitted.
718 void updateWritableState();
721 * @return The current URL if no viewproperties-context is given (see
722 * DolphinView::viewPropertiesContext(), otherwise the context
725 KUrl
viewPropertiesUrl() const;
730 bool m_assureVisibleCurrentIndex
;
731 bool m_isFolderWritable
;
732 bool m_dragging
; // True if a dragging is done. Required to be able to decide whether a
733 // tooltip may be shown when hovering an item.
736 QString m_viewPropertiesContext
;
738 QList
<QByteArray
> m_visibleRoles
;
740 QVBoxLayout
* m_topLayout
;
742 KFileItemModel
* m_model
;
743 DolphinItemListView
* m_view
;
744 KItemListContainer
* m_container
;
746 ToolTipManager
* m_toolTipManager
;
748 QTimer
* m_selectionChangedTimer
;
750 KUrl m_currentItemUrl
; // Used for making the view to remember the current URL after F5
751 QPoint m_restoredContentsPosition
;
752 KUrl m_createdItemUrl
; // URL for a new item that got created by the "Create New..." menu
754 QList
<KUrl
> m_selectedUrls
; // Used for making the view to remember selections after F5
756 VersionControlObserver
* m_versionControlObserver
;
759 friend class TestBase
;
760 friend class DolphinDetailsViewTest
;
763 /// Allow using DolphinView::Mode in QVariant
764 Q_DECLARE_METATYPE(DolphinView::Mode
)
766 #endif // DOLPHINVIEW_H