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
);
155 /** See setPreviewsShown */
156 bool previewsShown() const;
158 /** See setShowHiddenFiles */
159 bool hiddenFilesShown() const;
161 /** See setCategorizedSorting */
162 bool categorizedSorting() const;
165 * Returns the items of the view.
167 KFileItemList
items() const;
170 * Returns the selected items. The list is empty if no item has been
173 KFileItemList
selectedItems() const;
176 * Returns the number of selected items (this is faster than
177 * invoking selectedItems().count()).
179 int selectedItemsCount() const;
182 * Marks the items indicated by \p urls to get selected after the
183 * directory DolphinView::url() has been loaded. Note that nothing
184 * gets selected if no loading of a directory has been triggered
185 * by DolphinView::setUrl() or DolphinView::reload().
187 void markUrlsAsSelected(const QList
<KUrl
>& urls
);
190 * Marks the item indicated by \p url as the current item after the
191 * directory DolphinView::url() has been loaded.
193 void markUrlAsCurrent(const KUrl
& url
);
196 * All items that match to the pattern \a pattern will get selected
197 * if \a enabled is true and deselected if \a enabled is false.
199 void setItemSelectionEnabled(const QRegExp
& pattern
, bool enabled
);
202 * Sets the zoom level to \a level. It is assured that the used
203 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
204 * ZoomLevelInfo::maximumLevel().
206 void setZoomLevel(int level
);
207 int zoomLevel() const;
210 * Returns true, if zooming in is possible. If false is returned,
211 * the maximum zooming level has been reached.
213 bool isZoomInPossible() const;
216 * Returns true, if zooming out is possible. If false is returned,
217 * the minimum zooming level has been reached.
219 bool isZoomOutPossible() const;
221 /** Sets the sorting criterion (e.g., SortByName, SortBySize,...) of the items inside a directory (see DolphinView::Sorting). */
222 void setSorting(Sorting sorting
);
224 /** Returns the sorting criterion (e.g., SortByName, SortBySize,...) of the items inside a directory (see DolphinView::Sorting). */
225 Sorting
sorting() const;
227 /** Sets the sort order (Qt::Ascending or Qt::Descending) for the items. */
228 void setSortOrder(Qt::SortOrder order
);
230 /** Returns the currently used sort order (Qt::Ascending or Qt::Descending). */
231 Qt::SortOrder
sortOrder() const;
233 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
234 void setSortFoldersFirst(bool foldersFirst
);
236 /** Returns if files and folders are sorted separately or not. */
237 bool sortFoldersFirst() const;
239 /** Sets the additional information which should be shown for the items. */
240 void setAdditionalInfoList(const QList
<AdditionalInfo
>& info
);
242 /** Returns the additional information which should be shown for the items. */
243 QList
<AdditionalInfo
> additionalInfoList() const;
245 /** Reloads the current directory. */
251 * Refreshes the view to get synchronized with the (updated) Dolphin settings.
252 * This method only needs to get invoked if the view settings for the Icons View,
253 * Details View or Columns View have been changed.
258 * Filters the currently shown items by \a nameFilter. All items
259 * which contain the given filter string will be shown.
261 void setNameFilter(const QString
& nameFilter
);
262 QString
nameFilter() const;
265 * Calculates the number of currently shown files into
266 * \a fileCount and the number of folders into \a folderCount.
267 * The size of all files is written into \a totalFileSize.
268 * It is recommend using this method instead of asking the
269 * directory lister or the model directly, as it takes
270 * filtering and hierarchical previews into account.
272 void calculateItemCount(int& fileCount
, int& folderCount
, KIO::filesize_t
& totalFileSize
) const;
275 * Returns a textual representation of the state of the current
276 * folder or selected items, suitable for use in the status bar.
278 QString
statusBarText() const;
281 * Returns the version control actions that are provided for the items \p items.
282 * Usually the actions are presented in the context menu.
284 QList
<QAction
*> versionControlActions(const KFileItemList
& items
) const;
287 * Returns the state of the paste action:
288 * first is whether the action should be enabled
289 * second is the text for the action
291 QPair
<bool, QString
> pasteInfo() const;
294 * If \a tabsForFiles is true, the signal tabRequested() will also
295 * emitted also for files. Per default tabs for files is disabled
296 * and hence the signal tabRequested() will only be emitted for
299 void setTabsForFilesEnabled(bool tabsForFiles
);
300 bool isTabsForFilesEnabled() const;
303 * Returns true if the current view allows folders to be expanded,
304 * i.e. presents a hierarchical view to the user.
306 bool itemsExpandable() const;
309 * Restores the view state (current item, contents position, details view expansion state)
311 void restoreState(QDataStream
& stream
);
314 * Saves the view state (current item, contents position, details view expansion state)
316 void saveState(QDataStream
& stream
);
318 /** Returns true, if at least one item is selected. */
319 bool hasSelection() const;
322 * Returns the root item which represents the current URL. Note that the returned
323 * item can be null (KFileItem::isNull() will return true) in case that the directory
324 * has not been loaded.
326 KFileItem
rootItem() const;
330 * Changes the directory to \a url. If the current directory is equal to
331 * \a url, nothing will be done (use DolphinView::reload() instead).
333 void setUrl(const KUrl
& url
);
337 * @see DolphinView::selectedItems()
342 * Inverts the current selection: selected items get unselected,
343 * unselected items get selected.
344 * @see DolphinView::selectedItems()
346 void invertSelection();
348 void clearSelection();
351 * Triggers the renaming of the currently selected items, where
352 * the user must input a new name for the items.
354 void renameSelectedItems();
357 * Moves all selected items to the trash.
359 void trashSelectedItems();
362 * Deletes all selected items.
364 void deleteSelectedItems();
367 * Copies all selected items to the clipboard and marks
370 void cutSelectedItems();
372 /** Copies all selected items to the clipboard. */
373 void copySelectedItems();
375 /** Pastes the clipboard data to this view. */
379 * Pastes the clipboard data into the currently selected
380 * folder. If the current selection is not exactly one folder, no
381 * paste operation is done.
383 void pasteIntoFolder();
386 * Turns on the file preview for the all files of the current directory,
387 * if \a show is true.
388 * If the view properties should be remembered for each directory
389 * (GeneralSettings::globalViewProps() returns false), then the
390 * preview setting will be stored automatically.
392 void setPreviewsShown(bool show
);
395 * Shows all hidden files of the current directory,
396 * if \a show is true.
397 * If the view properties should be remembered for each directory
398 * (GeneralSettings::globalViewProps() returns false), then the
399 * show hidden file setting will be stored automatically.
401 void setHiddenFilesShown(bool show
);
404 * Summarizes all sorted items by their category \a categorized
406 * If the view properties should be remembered for each directory
407 * (GeneralSettings::globalViewProps() returns false), then the
408 * categorized sorting setting will be stored automatically.
410 void setCategorizedSorting(bool categorized
);
414 * Is emitted if the view has been activated by e. g. a mouse click.
419 * Is emitted if the URL of the view will be changed to \a url.
420 * After the URL has been changed the signal urlChanged() will
423 void urlAboutToBeChanged(const KUrl
& url
);
425 /** Is emitted if the URL of the view has been changed to \a url. */
426 void urlChanged(const KUrl
& url
);
429 * Is emitted when clicking on an item with the left mouse button.
431 void itemActivated(const KFileItem
& item
);
434 * Is emitted if items have been added or deleted.
436 void itemCountChanged();
439 * Is emitted if a new tab should be opened for the URL \a url.
441 void tabRequested(const KUrl
& url
);
444 * Is emitted if the view mode (IconsView, DetailsView,
445 * PreviewsView) has been changed.
447 void modeChanged(DolphinView::Mode current
, DolphinView::Mode previous
);
449 /** Is emitted if the 'show preview' property has been changed. */
450 void previewsShownChanged(bool shown
);
452 /** Is emitted if the 'show hidden files' property has been changed. */
453 void hiddenFilesShownChanged(bool shown
);
455 /** Is emitted if the 'categorized sorting' property has been changed. */
456 void categorizedSortingChanged(bool sortCategorized
);
458 /** Is emitted if the sorting by name, size or date has been changed. */
459 void sortingChanged(DolphinView::Sorting sorting
);
461 /** Is emitted if the sort order (ascending or descending) has been changed. */
462 void sortOrderChanged(Qt::SortOrder order
);
464 /** Is emitted if the sorting of files and folders (separate with folders first or mixed) has been changed. */
465 void sortFoldersFirstChanged(bool foldersFirst
);
467 /** Is emitted if the additional information shown for this view has been changed. */
468 void additionalInfoListChanged(const QList
<DolphinView::AdditionalInfo
>& current
,
469 const QList
<DolphinView::AdditionalInfo
>& previous
);
471 /** Is emitted if the zoom level has been changed by zooming in or out. */
472 void zoomLevelChanged(int current
, int previous
);
475 * Is emitted if information of an item is requested to be shown e. g. in the panel.
476 * If item is null, no item information request is pending.
478 void requestItemInfo(const KFileItem
& item
);
481 * Is emitted whenever the selection has been changed.
483 void selectionChanged(const KFileItemList
& selection
);
486 * Is emitted if a context menu is requested for the item \a item,
487 * which is part of \a url. If the item is null, the context menu
488 * for the URL should be shown and the custom actions \a customActions
491 void requestContextMenu(const KFileItem
& item
,
493 const QList
<QAction
*>& customActions
);
496 * Is emitted if an information message with the content \a msg
499 void infoMessage(const QString
& msg
);
502 * Is emitted if an error message with the content \a msg
505 void errorMessage(const QString
& msg
);
508 * Is emitted if an "operation completed" message with the content \a msg
511 void operationCompletedMessage(const QString
& msg
);
514 * Is emitted after DolphinView::setUrl() has been invoked and
515 * the path \a url is currently loaded. If this signal is emitted,
516 * it is assured that the view contains already the correct root
517 * URL and property settings.
519 void startedPathLoading(const KUrl
& url
);
522 * Is emitted after the path triggered by DolphinView::setUrl()
525 void finishedPathLoading(const KUrl
& url
);
528 * Is emitted after DolphinView::setUrl() has been invoked and provides
529 * the information how much percent of the current path have been loaded.
531 void pathLoadingProgress(int percent
);
534 * Is emitted if the DolphinView::setUrl() is invoked but the URL is not
537 void urlIsFileError(const KUrl
& file
);
540 * Emitted when KDirLister emits redirection.
541 * Testcase: fish://localhost
543 void redirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
546 * Is emitted when the write state of the folder has been changed. The application
547 * should disable all actions like "Create New..." that depend on the write
550 void writeStateChanged(bool isFolderWritable
);
553 virtual void mouseReleaseEvent(QMouseEvent
* event
);
554 virtual void contextMenuEvent(QContextMenuEvent
* event
);
555 virtual void wheelEvent(QWheelEvent
* event
);
559 * Marks the view as active (DolphinView:isActive() will return true)
560 * and emits the 'activated' signal if it is not already active.
564 void slotItemActivated(int index
);
565 void slotItemMiddleClicked(int index
);
566 void slotContextMenuRequested(int index
, const QPointF
& pos
);
567 void slotItemExpansionToggleClicked(int index
);
568 void slotItemHovered(int index
);
569 void slotItemUnhovered(int index
);
570 void slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
);
571 void slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
);
574 * Emits the signal \a selectionChanged() with a small delay. This is
575 * because getting all file items for the selection can be an expensive
576 * operation. Fast selection changes are collected in this case and
577 * the signal is emitted only after no selection change has been done
578 * within a small delay.
580 void slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
);
583 * Is called by emitDelayedSelectionChangedSignal() and emits the
584 * signal \a selectionChanged() with all selected file items as parameter.
586 void emitSelectionChangedSignal();
589 * Opens the context menu on position \a pos. The position
590 * is used to check whether the context menu is related to an
591 * item or to the viewport.
593 void openContextMenu(const QPoint
& pos
, const QList
<QAction
*>& customActions
);
596 * Drops dragged URLs to the destination path \a destPath. If
597 * the URLs are dropped above an item inside the destination path,
598 * the item is indicated by \a destItem.
600 void dropUrls(const KFileItem
& destItem
,
601 const KUrl
& destPath
,
605 * Updates the view properties of the current URL to the
606 * sorting given by \a sorting.
608 void updateSorting(DolphinView::Sorting sorting
);
611 * Updates the view properties of the current URL to the
612 * sort order given by \a order.
614 void updateSortOrder(Qt::SortOrder order
);
617 * Updates the view properties of the current URL to the
618 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
620 void updateSortFoldersFirst(bool foldersFirst
);
623 * Updates the status bar to show hover information for the
624 * item \a item. If currently other items are selected,
625 * no hover information is shown.
626 * @see DolphinView::clearHoverInformation()
628 void showHoverInformation(const KFileItem
& item
);
631 * Clears the hover information shown in the status bar.
632 * @see DolphinView::showHoverInformation().
634 void clearHoverInformation();
637 * Indicates in the status bar that the delete operation
638 * of the job \a job has been finished.
640 void slotDeleteFileFinished(KJob
* job
);
643 * Invoked when the directory lister has been started the
646 void slotDirListerStarted(const KUrl
& url
);
649 * Invoked when the file item model indicates that the directory lister has completed the loading
650 * of items, and that expanded folders have been restored (if the view mode is 'Details', and the
651 * view state is restored after navigating back or forward in history). Assures that pasted items
652 * and renamed items get seleced.
654 void slotLoadingCompleted();
657 * Is invoked when the KDirLister indicates refreshed items.
659 void slotRefreshItems();
662 * Observes the item with the URL \a url. As soon as the directory
663 * model indicates that the item is available, the item will
664 * get selected and it is assured that the item stays visible.
666 * @see selectAndScrollToCreatedItem()
668 void observeCreatedItem(const KUrl
& url
);
671 * Selects and scrolls to the item that got observed
672 * by observeCreatedItem().
674 void selectAndScrollToCreatedItem();
677 * Called when a redirection happens.
678 * Testcase: fish://localhost
680 void slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
683 * Applies the state that has been restored by restoreViewState()
686 void updateViewState();
688 //void slotUrlChangeRequested(const KUrl& url);
691 KFileItemModel
* fileItemModel() const;
693 void loadDirectory(const KUrl
& url
, bool reload
= false);
696 * Applies the view properties which are defined by the current URL
697 * to the DolphinView properties.
699 void applyViewProperties();
701 void applyAdditionalInfoListToView();
704 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
705 * Pastes the clipboard data into the URL \a url.
707 void pasteToUrl(const KUrl
& url
);
710 * Checks whether the current item view has the same zoom level
711 * as \a oldZoomLevel. If this is not the case, the zoom level
712 * of the controller is updated and a zoomLevelChanged() signal
715 void updateZoomLevel(int oldZoomLevel
);
718 * Returns a list of URLs for all selected items. The list is
719 * simplified, so that when the URLs are part of different tree
720 * levels, only the parent is returned.
722 KUrl::List
simplifiedSelectedUrls() const;
725 * Returns the MIME data for all selected items.
727 QMimeData
* selectionMimeData() const;
730 * Is invoked after a paste operation or a drag & drop
731 * operation and URLs from \a mimeData as selected.
732 * This allows to select all newly pasted
733 * items in restoreViewState().
735 void markPastedUrlsAsSelected(const QMimeData
* mimeData
);
738 * Updates m_isFolderWritable dependent on whether the folder represented by
739 * the current URL is writable. If the state has changed, the signal
740 * writeableStateChanged() will be emitted.
742 void updateWritableState();
744 QByteArray
sortRoleForSorting(Sorting sorting
) const;
745 Sorting
sortingForSortRole(const QByteArray
& sortRole
) const;
749 bool m_tabsForFiles
: 1;
750 bool m_assureVisibleCurrentIndex
: 1;
751 bool m_isFolderWritable
: 1;
755 QList
<AdditionalInfo
> m_additionalInfoList
;
757 QVBoxLayout
* m_topLayout
;
759 DolphinDirLister
* m_dirLister
;
760 DolphinItemListContainer
* m_container
;
762 ToolTipManager
* m_toolTipManager
;
764 QTimer
* m_selectionChangedTimer
;
766 KUrl m_currentItemUrl
;
767 QPoint m_restoredContentsPosition
;
768 KUrl m_createdItemUrl
; // URL for a new item that got created by the "Create New..." menu
769 KFileItemList m_selectedItems
; // this is used for making the View to remember selections after F5
771 VersionControlObserver
* m_versionControlObserver
;
774 friend class TestBase
;
775 friend class DolphinDetailsViewTest
;
778 /// Allow using DolphinView::Mode in QVariant
779 Q_DECLARE_METATYPE(DolphinView::Mode
)
781 #endif // DOLPHINVIEW_H