1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
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 ***************************************************************************/
25 #include <config-nepomuk.h>
27 #include "libdolphin_export.h"
29 #include <kparts/part.h>
30 #include <kfileitem.h>
31 #include <kfileitemdelegate.h>
32 #include <kio/fileundomanager.h>
37 #include <QLinkedList>
42 typedef KIO::FileUndoManager::CommandType CommandType
;
44 class DolphinController
;
45 class DolphinColumnView
;
46 class DolphinDetailsView
;
47 class DolphinFileItemDelegate
;
48 class DolphinIconsView
;
49 class DolphinMainWindow
;
51 class DolphinSortFilterProxyModel
;
52 class KFilePreviewGenerator
;
54 class KActionCollection
;
63 * @short Represents a view for the directory content.
65 * View modes for icons, details and columns are supported. It's
72 * @see DolphinIconsView
73 * @see DolphinDetailsView
74 * @see DolphinColumnView
76 class LIBDOLPHINPRIVATE_EXPORT DolphinView
: public QWidget
82 * Defines the view mode for a directory. The view mode
83 * can be defined when constructing a DolphinView. The
84 * view mode is automatically updated if the directory itself
85 * defines a view mode (see class ViewProperties for details).
90 * The directory items are shown as icons including an
96 * The icon, the name and at least the size of the directory
97 * items are shown in a table. It is possible to add columns
98 * for date, group and permissions.
103 * Each folder is shown in a separate column.
106 MaxModeEnum
= ColumnView
109 /** Defines the sort order for the items of a directory. */
121 MaxSortEnum
= SortByTags
125 * @param parent Parent widget of the view.
126 * @param url Specifies the content which should be shown.
127 * @param dirLister Used directory lister. The lister is not owned
128 * by the view and won't get deleted.
129 * @param dolphinModel Used directory model. The model is not owned
130 * by the view and won't get deleted.
131 * @param proxyModel Used proxy model which specifies the sorting. The
132 * model is not owned by the view and won't get
135 DolphinView(QWidget
* parent
,
137 KDirLister
* dirLister
,
138 DolphinModel
* dolphinModel
,
139 DolphinSortFilterProxyModel
* proxyModel
);
141 virtual ~DolphinView();
144 * Returns the current active URL, where all actions are applied.
145 * The URL navigator is synchronized with this URL.
147 const KUrl
& url() const;
150 * Returns the root URL of the view, which is defined as the first
151 * visible path of DolphinView::url(). Usually the root URL is
152 * equal to DolphinView::url(), but in the case of the column view
153 * when 2 columns are shown, the root URL might be:
154 * /home/peter/Documents
155 * and DolphinView::url() might return
156 * /home/peter/Documents/Music/
158 KUrl
rootUrl() const;
161 * If \a active is true, the view will marked as active. The active
162 * view is defined as view where all actions are applied to.
164 void setActive(bool active
);
165 bool isActive() const;
168 * Changes the view mode for the current directory to \a mode.
169 * If the view properties should be remembered for each directory
170 * (GeneralSettings::globalViewProps() returns false), then the
171 * changed view mode will be stored automatically.
173 void setMode(Mode mode
);
176 /** See setShowPreview */
177 bool showPreview() const;
179 /** See setShowHiddenFiles */
180 bool showHiddenFiles() const;
182 /** See setCategorizedSorting */
183 bool categorizedSorting() const;
186 * Returns true, if the categorized sorting is supported by the current
187 * used mode (see DolphinView::setMode()). Currently only DolphinView::IconsView
188 * supports categorizations. To check whether the categorized
189 * sorting is set, use DolphinView::categorizedSorting().
191 bool supportsCategorizedSorting() const;
195 * @see DolphinView::selectedItems()
200 * Inverts the current selection: selected items get unselected,
201 * unselected items get selected.
202 * @see DolphinView::selectedItems()
204 void invertSelection();
206 /** Returns true, if at least one item is selected. */
207 bool hasSelection() const;
209 void clearSelection();
212 * Returns the selected items. The list is empty if no item has been
214 * @see DolphinView::selectedUrls()
216 KFileItemList
selectedItems() const;
219 * Returns a list of URLs for all selected items. An empty list
220 * is returned, if no item is selected.
221 * @see DolphinView::selectedItems()
223 KUrl::List
selectedUrls() const;
226 * Returns the number of selected items (this is faster than
227 * invoking selectedItems().count()).
229 int selectedItemsCount() const;
232 * Sets the upper left position of the view content
233 * to (x,y). The content of the view might be larger than the visible area
234 * and hence a scrolling must be done.
236 void setContentsPosition(int x
, int y
);
238 /** Returns the upper left position of the view content. */
239 QPoint
contentsPosition() const;
242 * Sets the zoom level to \a level. It is assured that the used
243 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
244 * ZoomLevelInfo::maximumLevel().
246 void setZoomLevel(int level
);
247 int zoomLevel() const;
250 * Returns true, if zooming in is possible. If false is returned,
251 * the maximum zooming level has been reached.
253 bool isZoomInPossible() const;
256 * Returns true, if zooming out is possible. If false is returned,
257 * the minimum zooming level has been reached.
259 bool isZoomOutPossible() const;
261 /** Sets the sort order of the items inside a directory (see DolphinView::Sorting). */
262 void setSorting(Sorting sorting
);
264 /** Returns the sort order of the items inside a directory (see DolphinView::Sorting). */
265 Sorting
sorting() const;
267 /** Sets the sort order (Qt::Ascending or Qt::Descending) for the items. */
268 void setSortOrder(Qt::SortOrder order
);
270 /** Returns the current used sort order (Qt::Ascending or Qt::Descending). */
271 Qt::SortOrder
sortOrder() const;
273 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
274 void setSortFoldersFirst(bool foldersFirst
);
276 /** Returns if files and folders are sorted separately or not. */
277 bool sortFoldersFirst() const;
279 /** Sets the additional information which should be shown for the items. */
280 void setAdditionalInfo(KFileItemDelegate::InformationList info
);
282 /** Returns the additional information which should be shown for the items. */
283 KFileItemDelegate::InformationList
additionalInfo() const;
285 /** Reloads the current directory. */
289 * Refreshes the view to get synchronized with the (updated) Dolphin settings.
290 * This method only needs to get invoked if the view settings for the Icons View,
291 * Details View or Columns View have been changed.
296 * Changes the directory of the view to \a url. If \a rootUrl is empty, the view
297 * properties from \a url are used for adjusting the view mode and the other properties.
298 * If \a rootUrl is not empty, the view properties from the root URL are considered
299 * instead. Specifying a root URL is only required if a view having a different root URL
300 * (e. g. the column view) should be restored. Usually using DolphinView::setUrl()
301 * is enough for changing the current URL.
303 void updateView(const KUrl
& url
, const KUrl
& rootUrl
);
306 * Filters the currently shown items by \a nameFilter. All items
307 * which contain the given filter string will be shown.
309 void setNameFilter(const QString
& nameFilter
);
312 * Calculates the number of currently shown files into
313 * \a fileCount and the number of folders into \a folderCount.
314 * The size of all files is written into \a totalFileSize.
315 * It is recommend using this method instead of asking the
316 * directory lister or the model directly, as it takes
317 * filtering and hierarchical previews into account.
319 void calculateItemCount(int& fileCount
, int& folderCount
, KIO::filesize_t
& totalFileSize
) const;
322 * Returns a textual representation of the state of the current
323 * folder or selected items, suitable for use in the status bar.
325 QString
statusBarText() const;
328 * Updates the state of the 'Additional Information' actions in \a collection.
330 void updateAdditionalInfoActions(KActionCollection
* collection
);
333 * Returns the state of the paste action:
334 * first is whether the action should be enabled
335 * second is the text for the action
337 QPair
<bool, QString
> pasteInfo() const;
340 * If \a tabsForFiles is true, the signal tabRequested() will also
341 * emitted also for files. Per default tabs for files is disabled
342 * and hence the signal tabRequested() will only be emitted for
345 void setTabsForFilesEnabled(bool tabsForFiles
);
346 bool isTabsForFilesEnabled() const;
349 * Returns true if the current view allows folders to be expanded,
350 * i.e. presents a hierarchical view to the user.
352 bool itemsExpandable() const;
356 * Changes the directory to \a url. If the current directory is equal to
357 * \a url, nothing will be done (use DolphinView::reload() instead).
359 void setUrl(const KUrl
& url
);
362 * Request of a selection change. The view will do its best to accommodate
363 * the request, but it is not guaranteed that all items in \a selection
364 * will actually get selected. The view will e.g. not select items which
365 * are not in the currently displayed folder.
367 void changeSelection(const KFileItemList
& selection
);
370 * Triggers the renaming of the currently selected items, where
371 * the user must input a new name for the items.
373 void renameSelectedItems();
376 * Moves all selected items to the trash.
378 void trashSelectedItems();
381 * Deletes all selected items.
383 void deleteSelectedItems();
386 * Copies all selected items to the clipboard and marks
387 * the items as cutted.
389 void cutSelectedItems();
391 /** Copies all selected items to the clipboard. */
392 void copySelectedItems();
394 /** Pastes the clipboard data to this view. */
398 * Pastes the clipboard data into the currently selected
399 * folder. If the current selection is not exactly one folder, no
400 * paste operation is done.
402 void pasteIntoFolder();
405 * Turns on the file preview for the all files of the current directory,
406 * if \a show is true.
407 * If the view properties should be remembered for each directory
408 * (GeneralSettings::globalViewProps() returns false), then the
409 * preview setting will be stored automatically.
411 void setShowPreview(bool show
);
414 * Shows all hidden files of the current directory,
415 * if \a show is true.
416 * If the view properties should be remembered for each directory
417 * (GeneralSettings::globalViewProps() returns false), then the
418 * show hidden file setting will be stored automatically.
420 void setShowHiddenFiles(bool show
);
423 * Summarizes all sorted items by their category \a categorized
425 * If the view properties should be remembered for each directory
426 * (GeneralSettings::globalViewProps() returns false), then the
427 * categorized sorting setting will be stored automatically.
429 void setCategorizedSorting(bool categorized
);
431 /** Switches between an ascending and descending sorting order. */
432 void toggleSortOrder();
434 /** Switches between a separate sorting (with folders first) and a mixed sorting of files and folders. */
435 void toggleSortFoldersFirst();
438 * Switches on or off the displaying of additional information
439 * as specified by \a action.
441 void toggleAdditionalInfo(QAction
* action
);
445 * Is emitted if the view has been activated by e. g. a mouse click.
449 /** Is emitted if URL of the view has been changed to \a url. */
450 void urlChanged(const KUrl
& url
);
453 * Is emitted if the view requests a changing of the current
454 * URL to \a url (see DolphinController::triggerUrlChangeRequest()).
456 void requestUrlChange(const KUrl
& url
);
459 * Is emitted when clicking on an item with the left mouse button.
461 void itemTriggered(const KFileItem
& item
);
464 * Is emitted if a new tab should be opened for the URL \a url.
466 void tabRequested(const KUrl
& url
);
469 * Is emitted if the view mode (IconsView, DetailsView,
470 * PreviewsView) has been changed.
474 /** Is emitted if the 'show preview' property has been changed. */
475 void showPreviewChanged();
477 /** Is emitted if the 'show hidden files' property has been changed. */
478 void showHiddenFilesChanged();
480 /** Is emitted if the 'categorized sorting' property has been changed. */
481 void categorizedSortingChanged();
483 /** Is emitted if the sorting by name, size or date has been changed. */
484 void sortingChanged(DolphinView::Sorting sorting
);
486 /** Is emitted if the sort order (ascending or descending) has been changed. */
487 void sortOrderChanged(Qt::SortOrder order
);
489 /** Is emitted if the sorting of files and folders (separate with folders first or mixed) has been changed. */
490 void sortFoldersFirstChanged(bool foldersFirst
);
492 /** Is emitted if the additional information shown for this view has been changed. */
493 void additionalInfoChanged();
495 /** Is emitted if the zoom level has been changed by zooming in or out. */
496 void zoomLevelChanged(int level
);
499 * Is emitted if information of an item is requested to be shown e. g. in the panel.
500 * If item is null, no item information request is pending.
502 void requestItemInfo(const KFileItem
& item
);
504 /** Is emitted if the contents has been moved to \a x, \a y. */
505 void contentsMoved(int x
, int y
);
508 * Is emitted whenever the selection has been changed.
510 void selectionChanged(const KFileItemList
& selection
);
513 * Is emitted if a context menu is requested for the item \a item,
514 * which is part of \a url. If the item is null, the context menu
515 * for the URL should be shown and the custom actions \a customActions
518 void requestContextMenu(const KFileItem
& item
,
520 const QList
<QAction
*>& customActions
);
523 * Is emitted if an information message with the content \a msg
526 void infoMessage(const QString
& msg
);
529 * Is emitted if an error message with the content \a msg
532 void errorMessage(const QString
& msg
);
535 * Is emitted if an "operation completed" message with the content \a msg
538 void operationCompletedMessage(const QString
& msg
);
541 * Is emitted after DolphinView::setUrl() has been invoked and
542 * the path \a url is currently loaded. If this signal is emitted,
543 * it is assured that the view contains already the correct root
544 * URL and property settings.
546 void startedPathLoading(const KUrl
& url
);
549 * Emitted when KDirLister emits redirection.
550 * Testcase: fish://localhost
552 void redirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
555 /** @see QWidget::mouseReleaseEvent */
556 virtual void mouseReleaseEvent(QMouseEvent
* event
);
557 virtual void wheelEvent(QWheelEvent
* event
);
558 virtual bool eventFilter(QObject
* watched
, QEvent
* event
);
562 * Marks the view as active (DolphinView:isActive() will return true)
563 * and emits the 'activated' signal if it is not already active.
568 * If the item \a item is a directory, then this
569 * directory will be loaded. If the item is a file, the corresponding
570 * application will get started.
572 void triggerItem(const KFileItem
& index
);
574 void emitSelectionChangedSignal();
577 * Opens the context menu on position \a pos. The position
578 * is used to check whether the context menu is related to an
579 * item or to the viewport.
581 void openContextMenu(const QPoint
& pos
, const QList
<QAction
*>& customActions
);
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 view properties of the current URL to the
612 * additional information given by \a info.
614 void updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
);
617 * Emits the signal contentsMoved with the current coordinates
618 * of the viewport as parameters.
620 void emitContentsMoved();
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 * Is emitted if the controller requests a changing of the current
646 void slotRequestUrlChange(const KUrl
& url
);
649 * Invoked when the directory lister has completed the loading of
650 * items. Assures that pasted items and renamed items get seleced.
652 void slotDirListerCompleted();
655 * Is invoked when the KDirLister indicates refreshed items.
657 void slotRefreshItems();
660 * If \a view can be positively identified as not being the source for the
661 * current drag operation, deleteLater() it immediately. Else stores
662 * it for later deletion.
664 void deleteWhenNotDragSource(QAbstractItemView
* view
);
667 * Observes the item with the URL \a url. As soon as the directory
668 * model indicates that the item is available, the item will
669 * get selected and it is assure that the item stays visible.
671 * @see selectAndScrollToCreatedItem()
673 void observeCreatedItem(const KUrl
& url
);
676 * Selects and scrolls to the item that got observed
677 * by observeCreatedItem().
679 void selectAndScrollToCreatedItem();
682 * Restore selection after view refresh.
684 void restoreSelection();
687 void loadDirectory(const KUrl
& url
, bool reload
= false);
690 * Returns the URL where the view properties should be stored. Usually
691 * DolphinView::url() is returned, but in the case of a Column View the
692 * view properties are always stored in the directory represented by the
693 * first column. It is recommendend whenever using the ViewProperties class
694 * to use DolphinView::viewPropertiesUrl() as URL.
696 KUrl
viewPropertiesUrl() const;
699 * Applies the view properties which are defined by the current URL
700 * m_url to the DolphinView properties.
702 void applyViewProperties(const KUrl
& url
);
705 * Creates a new view representing the given view mode (DolphinView::mode()).
706 * The current view will get deleted.
713 * Returns a pointer to the currently used item view, which is either
714 * a ListView or a TreeView.
716 QAbstractItemView
* itemView() const;
719 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
720 * Pastes the clipboard data into the URL \a url.
722 void pasteToUrl(const KUrl
& url
);
725 * Checks whether the current item view has the same zoom level
726 * as \a oldZoomLevel. If this is not the case, the zoom level
727 * of the controller is updated and a zoomLevelChanged() signal
730 void updateZoomLevel(int oldZoomLevel
);
733 * Returns a list of URLs for all selected items. The list is
734 * simplified, so that when the URLs are part of different tree
735 * levels, only the parent is returned.
737 KUrl::List
simplifiedSelectedUrls() const;
740 * Returns true, if the ColumnView is activated. As the column view
741 * requires some special handling for iterating through directories,
742 * this method has been introduced for convenience.
744 bool isColumnViewActive() const;
747 * Returns the MIME data for all selected items.
749 QMimeData
* selectionMimeData() const;
752 * Is invoked after a paste operation or a drag & drop
753 * operation and adds the filenames of all URLs from \a mimeData to
754 * m_newFileNames. This allows to select all newly added
755 * items in slotDirListerCompleted().
757 void addNewFileNames(const QMimeData
* mimeData
);
761 bool m_showPreview
: 1;
762 bool m_loadingDirectory
: 1;
763 bool m_storedCategorizedSorting
: 1;
764 bool m_tabsForFiles
: 1;
765 bool m_isContextMenuOpen
: 1; // TODO: workaround for Qt-issue 207192
766 bool m_ignoreViewProperties
: 1;
767 bool m_assureVisibleCurrentIndex
: 1;
771 DolphinMainWindow
* m_mainWindow
;
772 QVBoxLayout
* m_topLayout
;
774 DolphinController
* m_controller
;
775 DolphinIconsView
* m_iconsView
;
776 DolphinDetailsView
* m_detailsView
;
777 DolphinColumnView
* m_columnView
;
778 DolphinFileItemDelegate
* m_fileItemDelegate
;
779 QItemSelectionModel
* m_selectionModel
;
781 DolphinModel
* m_dolphinModel
;
782 KDirLister
* m_dirLister
;
783 DolphinSortFilterProxyModel
* m_proxyModel
;
785 KFilePreviewGenerator
* m_previewGenerator
;
786 ToolTipManager
* m_toolTipManager
;
789 KUrl m_currentItemUrl
;
790 KUrl m_createdItemUrl
; // URL for a new item that got created by the "Create New..." menu
791 KFileItemList m_selectedItems
; // this is used for making the View to remember selections after F5
794 * Remembers the filenames that have been added by a paste operation
795 * or a drag & drop operation. Allows to select the items in
796 * slotDirListerCompleted().
798 QSet
<QString
> m_newFileNames
;
800 QAbstractItemView
* m_expandedDragSource
;
803 inline bool DolphinView::isColumnViewActive() const
805 return m_columnView
!= 0;
808 /// Allow using DolphinView::Mode in QVariant
809 Q_DECLARE_METATYPE(DolphinView::Mode
)
811 #endif // DOLPHINVIEW_H