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 DolphinDirLister
;
43 class DolphinItemListContainer
;
45 class KActionCollection
;
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
96 /** Defines the sort order for the items of a directory. */
110 /** Defines the additional information shown for the items of a directory. */
126 * @param url Specifies the content which should be shown.
127 * @param parent Parent widget of the view.
129 DolphinView(const KUrl
& url
, QWidget
* parent
);
131 virtual ~DolphinView();
134 * Returns the current active URL, where all actions are applied.
135 * The URL navigator is synchronized with this URL.
140 * If \a active is true, the view will marked as active. The active
141 * view is defined as view where all actions are applied to.
143 void setActive(bool active
);
144 bool isActive() const;
147 * Changes the view mode for the current directory to \a mode.
148 * If the view properties should be remembered for each directory
149 * (GeneralSettings::globalViewProps() returns false), then the
150 * changed view mode will be stored automatically.
152 void setMode(Mode mode
);
156 * Turns on the file preview for the all files of the current directory,
157 * if \a show is true.
158 * If the view properties should be remembered for each directory
159 * (GeneralSettings::globalViewProps() returns false), then the
160 * preview setting will be stored automatically.
162 void setPreviewsShown(bool show
);
163 bool previewsShown() const;
166 * Shows all hidden files of the current directory,
167 * if \a show is true.
168 * If the view properties should be remembered for each directory
169 * (GeneralSettings::globalViewProps() returns false), then the
170 * show hidden file setting will be stored automatically.
172 void setHiddenFilesShown(bool show
);
173 bool hiddenFilesShown() const;
176 * Turns on sorting by groups if \a enable is true.
178 void setGroupedSorting(bool grouped
);
179 bool groupedSorting() const;
182 * Returns the items of the view.
184 KFileItemList
items() const;
187 * Returns the selected items. The list is empty if no item has been
190 KFileItemList
selectedItems() const;
193 * Returns the number of selected items (this is faster than
194 * invoking selectedItems().count()).
196 int selectedItemsCount() const;
199 * Marks the items indicated by \p urls to get selected after the
200 * directory DolphinView::url() has been loaded. Note that nothing
201 * gets selected if no loading of a directory has been triggered
202 * by DolphinView::setUrl() or DolphinView::reload().
204 void markUrlsAsSelected(const QList
<KUrl
>& urls
);
207 * Marks the item indicated by \p url as the current item after the
208 * directory DolphinView::url() has been loaded.
210 void markUrlAsCurrent(const KUrl
& url
);
213 * All items that match to the pattern \a pattern will get selected
214 * if \a enabled is true and deselected if \a enabled is false.
216 void setItemSelectionEnabled(const QRegExp
& pattern
, bool enabled
);
219 * Sets the zoom level to \a level. It is assured that the used
220 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
221 * ZoomLevelInfo::maximumLevel().
223 void setZoomLevel(int level
);
224 int zoomLevel() const;
227 * Returns true, if zooming in is possible. If false is returned,
228 * the maximum zooming level has been reached.
230 bool isZoomInPossible() const;
233 * Returns true, if zooming out is possible. If false is returned,
234 * the minimum zooming level has been reached.
236 bool isZoomOutPossible() const;
238 /** Sets the sorting criterion (e.g., SortByName, SortBySize,...) of the items inside a directory (see DolphinView::Sorting). */
239 void setSorting(Sorting sorting
);
241 /** Returns the sorting criterion (e.g., SortByName, SortBySize,...) of the items inside a directory (see DolphinView::Sorting). */
242 Sorting
sorting() const;
244 /** Sets the sort order (Qt::Ascending or Qt::Descending) for the items. */
245 void setSortOrder(Qt::SortOrder order
);
247 /** Returns the currently used sort order (Qt::Ascending or Qt::Descending). */
248 Qt::SortOrder
sortOrder() const;
250 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
251 void setSortFoldersFirst(bool foldersFirst
);
253 /** Returns if files and folders are sorted separately or not. */
254 bool sortFoldersFirst() const;
256 /** Sets the additional information which should be shown for the items. */
257 void setAdditionalInfoList(const QList
<AdditionalInfo
>& info
);
259 /** Returns the additional information which should be shown for the items. */
260 QList
<AdditionalInfo
> additionalInfoList() const;
262 /** Reloads the current directory. */
268 * Refreshes the view to get synchronized with the (updated) Dolphin settings.
269 * This method only needs to get invoked if the view settings for the Icons View,
270 * Details View or Columns View have been changed.
275 * Filters the currently shown items by \a nameFilter. All items
276 * which contain the given filter string will be shown.
278 void setNameFilter(const QString
& nameFilter
);
279 QString
nameFilter() const;
282 * Calculates the number of currently shown files into
283 * \a fileCount and the number of folders into \a folderCount.
284 * The size of all files is written into \a totalFileSize.
285 * It is recommend using this method instead of asking the
286 * directory lister or the model directly, as it takes
287 * filtering and hierarchical previews into account.
289 void calculateItemCount(int& fileCount
, int& folderCount
, KIO::filesize_t
& totalFileSize
) const;
292 * Returns a textual representation of the state of the current
293 * folder or selected items, suitable for use in the status bar.
295 QString
statusBarText() const;
298 * Returns the version control actions that are provided for the items \p items.
299 * Usually the actions are presented in the context menu.
301 QList
<QAction
*> versionControlActions(const KFileItemList
& items
) const;
304 * Returns the state of the paste action:
305 * first is whether the action should be enabled
306 * second is the text for the action
308 QPair
<bool, QString
> pasteInfo() const;
311 * If \a tabsForFiles is true, the signal tabRequested() will also
312 * emitted also for files. Per default tabs for files is disabled
313 * and hence the signal tabRequested() will only be emitted for
316 void setTabsForFilesEnabled(bool tabsForFiles
);
317 bool isTabsForFilesEnabled() const;
320 * Returns true if the current view allows folders to be expanded,
321 * i.e. presents a hierarchical view to the user.
323 bool itemsExpandable() const;
326 * Restores the view state (current item, contents position, details view expansion state)
328 void restoreState(QDataStream
& stream
);
331 * Saves the view state (current item, contents position, details view expansion state)
333 void saveState(QDataStream
& stream
);
335 /** Returns true, if at least one item is selected. */
336 bool hasSelection() const;
339 * Returns the root item which represents the current URL. Note that the returned
340 * item can be null (KFileItem::isNull() will return true) in case that the directory
341 * has not been loaded.
343 KFileItem
rootItem() const;
347 * Changes the directory to \a url. If the current directory is equal to
348 * \a url, nothing will be done (use DolphinView::reload() instead).
350 void setUrl(const KUrl
& url
);
354 * @see DolphinView::selectedItems()
359 * Inverts the current selection: selected items get unselected,
360 * unselected items get selected.
361 * @see DolphinView::selectedItems()
363 void invertSelection();
365 void clearSelection();
368 * Triggers the renaming of the currently selected items, where
369 * the user must input a new name for the items.
371 void renameSelectedItems();
374 * Moves all selected items to the trash.
376 void trashSelectedItems();
379 * Deletes all selected items.
381 void deleteSelectedItems();
384 * Copies all selected items to the clipboard and marks
387 void cutSelectedItems();
389 /** Copies all selected items to the clipboard. */
390 void copySelectedItems();
392 /** Pastes the clipboard data to this view. */
396 * Pastes the clipboard data into the currently selected
397 * folder. If the current selection is not exactly one folder, no
398 * paste operation is done.
400 void pasteIntoFolder();
402 /** Activates the view if the item list container gets focus. */
403 virtual bool eventFilter(QObject
* watched
, QEvent
* event
);
407 * Is emitted if the view has been activated by e. g. a mouse click.
412 * Is emitted if the URL of the view will be changed to \a url.
413 * After the URL has been changed the signal urlChanged() will
416 void urlAboutToBeChanged(const KUrl
& url
);
418 /** Is emitted if the URL of the view has been changed to \a url. */
419 void urlChanged(const KUrl
& url
);
422 * Is emitted when clicking on an item with the left mouse button.
424 void itemActivated(const KFileItem
& item
);
427 * Is emitted if items have been added or deleted.
429 void itemCountChanged();
432 * Is emitted if a new tab should be opened for the URL \a url.
434 void tabRequested(const KUrl
& url
);
437 * Is emitted if the view mode (IconsView, DetailsView,
438 * PreviewsView) has been changed.
440 void modeChanged(DolphinView::Mode current
, DolphinView::Mode previous
);
442 /** Is emitted if the 'show preview' property has been changed. */
443 void previewsShownChanged(bool shown
);
445 /** Is emitted if the 'show hidden files' property has been changed. */
446 void hiddenFilesShownChanged(bool shown
);
448 /** Is emitted if the 'grouped sorting' property has been changed. */
449 void groupedSortingChanged(bool groupedSorting
);
451 /** Is emitted if the sorting by name, size or date has been changed. */
452 void sortingChanged(DolphinView::Sorting sorting
);
454 /** Is emitted if the sort order (ascending or descending) has been changed. */
455 void sortOrderChanged(Qt::SortOrder order
);
457 /** Is emitted if the sorting of files and folders (separate with folders first or mixed) has been changed. */
458 void sortFoldersFirstChanged(bool foldersFirst
);
460 /** Is emitted if the additional information shown for this view has been changed. */
461 void additionalInfoListChanged(const QList
<DolphinView::AdditionalInfo
>& current
,
462 const QList
<DolphinView::AdditionalInfo
>& previous
);
464 /** Is emitted if the zoom level has been changed by zooming in or out. */
465 void zoomLevelChanged(int current
, int previous
);
468 * Is emitted if information of an item is requested to be shown e. g. in the panel.
469 * If item is null, no item information request is pending.
471 void requestItemInfo(const KFileItem
& item
);
474 * Is emitted whenever the selection has been changed.
476 void selectionChanged(const KFileItemList
& selection
);
479 * Is emitted if a context menu is requested for the item \a item,
480 * which is part of \a url. If the item is null, the context menu
481 * for the URL should be shown and the custom actions \a customActions
484 void requestContextMenu(const QPoint
& pos
,
485 const KFileItem
& item
,
487 const QList
<QAction
*>& customActions
);
490 * Is emitted if an information message with the content \a msg
493 void infoMessage(const QString
& msg
);
496 * Is emitted if an error message with the content \a msg
499 void errorMessage(const QString
& msg
);
502 * Is emitted if an "operation completed" message with the content \a msg
505 void operationCompletedMessage(const QString
& msg
);
508 * Is emitted after DolphinView::setUrl() has been invoked and
509 * the path \a url is currently loaded. If this signal is emitted,
510 * it is assured that the view contains already the correct root
511 * URL and property settings.
513 void startedPathLoading(const KUrl
& url
);
516 * Is emitted after the path triggered by DolphinView::setUrl()
519 void finishedPathLoading(const KUrl
& url
);
522 * Is emitted after DolphinView::setUrl() has been invoked and provides
523 * the information how much percent of the current path have been loaded.
525 void pathLoadingProgress(int percent
);
528 * Is emitted if the DolphinView::setUrl() is invoked but the URL is not
531 void urlIsFileError(const KUrl
& file
);
534 * Emitted when KDirLister emits redirection.
535 * Testcase: fish://localhost
537 void redirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
540 * Is emitted when the write state of the folder has been changed. The application
541 * should disable all actions like "Create New..." that depend on the write
544 void writeStateChanged(bool isFolderWritable
);
547 /** Changes the zoom level if Control is pressed during a wheel event. */
548 virtual void wheelEvent(QWheelEvent
* event
);
552 * Marks the view as active (DolphinView:isActive() will return true)
553 * and emits the 'activated' signal if it is not already active.
557 void slotItemActivated(int index
);
558 void slotItemMiddleClicked(int index
);
559 void slotItemContextMenuRequested(int index
, const QPointF
& pos
);
560 void slotViewContextMenuRequested(const QPointF
& pos
);
561 void slotHeaderContextMenuRequested(const QPointF
& pos
);
562 void slotItemExpansionToggleClicked(int index
);
563 void slotItemHovered(int index
);
564 void slotItemUnhovered(int index
);
565 void slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
);
566 void slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
);
569 * Emits the signal \a selectionChanged() with a small delay. This is
570 * because getting all file items for the selection can be an expensive
571 * operation. Fast selection changes are collected in this case and
572 * the signal is emitted only after no selection change has been done
573 * within a small delay.
575 void slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
);
578 * Is called by emitDelayedSelectionChangedSignal() and emits the
579 * signal \a selectionChanged() with all selected file items as parameter.
581 void emitSelectionChangedSignal();
584 * Drops dragged URLs to the destination path \a destPath. If
585 * the URLs are dropped above an item inside the destination path,
586 * the item is indicated by \a destItem.
588 void dropUrls(const KFileItem
& destItem
,
589 const KUrl
& destPath
,
593 * Updates the view properties of the current URL to the
594 * sorting given by \a sorting.
596 void updateSorting(DolphinView::Sorting sorting
);
599 * Updates the view properties of the current URL to the
600 * sort order given by \a order.
602 void updateSortOrder(Qt::SortOrder order
);
605 * Updates the view properties of the current URL to the
606 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
608 void updateSortFoldersFirst(bool foldersFirst
);
611 * Updates the status bar to show hover information for the
612 * item \a item. If currently other items are selected,
613 * no hover information is shown.
614 * @see DolphinView::clearHoverInformation()
616 void showHoverInformation(const KFileItem
& item
);
619 * Clears the hover information shown in the status bar.
620 * @see DolphinView::showHoverInformation().
622 void clearHoverInformation();
625 * Indicates in the status bar that the delete operation
626 * of the job \a job has been finished.
628 void slotDeleteFileFinished(KJob
* job
);
631 * Invoked when the directory lister has been started the
634 void slotDirListerStarted(const KUrl
& url
);
637 * Invoked when the file item model indicates that the directory lister has completed the loading
638 * of items, and that expanded folders have been restored (if the view mode is 'Details', and the
639 * view state is restored after navigating back or forward in history). Assures that pasted items
640 * and renamed items get seleced.
642 void slotLoadingCompleted();
645 * Is invoked when the KDirLister indicates refreshed items.
647 void slotRefreshItems();
650 * Observes the item with the URL \a url. As soon as the directory
651 * model indicates that the item is available, the item will
652 * get selected and it is assured that the item stays visible.
654 * @see selectAndScrollToCreatedItem()
656 void observeCreatedItem(const KUrl
& url
);
659 * Selects and scrolls to the item that got observed
660 * by observeCreatedItem().
662 void selectAndScrollToCreatedItem();
665 * Called when a redirection happens.
666 * Testcase: fish://localhost
668 void slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
671 * Applies the state that has been restored by restoreViewState()
674 void updateViewState();
676 //void slotUrlChangeRequested(const KUrl& url);
679 KFileItemModel
* fileItemModel() const;
681 void loadDirectory(const KUrl
& url
, bool reload
= false);
684 * Applies the view properties which are defined by the current URL
685 * to the DolphinView properties.
687 void applyViewProperties();
689 void applyAdditionalInfoListToView();
692 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
693 * Pastes the clipboard data into the URL \a url.
695 void pasteToUrl(const KUrl
& url
);
698 * Checks whether the current item view has the same zoom level
699 * as \a oldZoomLevel. If this is not the case, the zoom level
700 * of the controller is updated and a zoomLevelChanged() signal
703 void updateZoomLevel(int oldZoomLevel
);
706 * Returns a list of URLs for all selected items. The list is
707 * simplified, so that when the URLs are part of different tree
708 * levels, only the parent is returned.
710 KUrl::List
simplifiedSelectedUrls() const;
713 * Returns the MIME data for all selected items.
715 QMimeData
* selectionMimeData() const;
718 * Is invoked after a paste operation or a drag & drop
719 * operation and URLs from \a mimeData as selected.
720 * This allows to select all newly pasted
721 * items in restoreViewState().
723 void markPastedUrlsAsSelected(const QMimeData
* mimeData
);
726 * Updates m_isFolderWritable dependent on whether the folder represented by
727 * the current URL is writable. If the state has changed, the signal
728 * writeableStateChanged() will be emitted.
730 void updateWritableState();
732 QByteArray
sortRoleForSorting(Sorting sorting
) const;
733 Sorting
sortingForSortRole(const QByteArray
& sortRole
) const;
737 bool m_tabsForFiles
: 1;
738 bool m_assureVisibleCurrentIndex
: 1;
739 bool m_isFolderWritable
: 1;
743 QList
<AdditionalInfo
> m_additionalInfoList
;
745 QVBoxLayout
* m_topLayout
;
747 DolphinDirLister
* m_dirLister
;
748 DolphinItemListContainer
* m_container
;
750 ToolTipManager
* m_toolTipManager
;
752 QTimer
* m_selectionChangedTimer
;
754 KUrl m_currentItemUrl
;
755 QPoint m_restoredContentsPosition
;
756 KUrl m_createdItemUrl
; // URL for a new item that got created by the "Create New..." menu
757 QList
<KUrl
> m_selectedUrls
; // this is used for making the View to remember selections after F5
759 VersionControlObserver
* m_versionControlObserver
;
762 friend class TestBase
;
763 friend class DolphinDetailsViewTest
;
766 /// Allow using DolphinView::Mode in QVariant
767 Q_DECLARE_METATYPE(DolphinView::Mode
)
769 #endif // DOLPHINVIEW_H