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>
41 typedef KIO::FileUndoManager::CommandType CommandType
;
43 class DolphinController
;
44 class DolphinColumnView
;
45 class DolphinDetailsView
;
46 class DolphinFileItemDelegate
;
47 class DolphinIconsView
;
48 class DolphinMainWindow
;
50 class DolphinSortFilterProxyModel
;
51 class KFilePreviewGenerator
;
53 class KActionCollection
;
62 * @short Represents a view for the directory content.
64 * View modes for icons, details and columns are supported. It's
71 * @see DolphinIconsView
72 * @see DolphinDetailsView
73 * @see DolphinColumnView
75 class LIBDOLPHINPRIVATE_EXPORT DolphinView
: public QWidget
81 * Defines the view mode for a directory. The view mode
82 * can be defined when constructing a DolphinView. The
83 * view mode is automatically updated if the directory itself
84 * defines a view mode (see class ViewProperties for details).
89 * The directory items are shown as icons including an
95 * The icon, the name and at least the size of the directory
96 * items are shown in a table. It is possible to add columns
97 * for date, group and permissions.
102 * Each folder is shown in a separate column.
105 MaxModeEnum
= ColumnView
108 /** Defines the sort order for the items of a directory. */
120 MaxSortEnum
= SortByTags
124 * @param parent Parent widget of the view.
125 * @param url Specifies the content which should be shown.
126 * @param dirLister Used directory lister. The lister is not owned
127 * by the view and won't get deleted.
128 * @param dolphinModel Used directory model. The model is not owned
129 * by the view and won't get deleted.
130 * @param proxyModel Used proxy model which specifies the sorting. The
131 * model is not owned by the view and won't get
134 DolphinView(QWidget
* parent
,
136 KDirLister
* dirLister
,
137 DolphinModel
* dolphinModel
,
138 DolphinSortFilterProxyModel
* proxyModel
);
140 virtual ~DolphinView();
143 * Returns the current active URL, where all actions are applied.
144 * The URL navigator is synchronized with this URL.
146 const KUrl
& url() const;
149 * Returns the root URL of the view, which is defined as the first
150 * visible path of DolphinView::url(). Usually the root URL is
151 * equal to DolphinView::url(), but in the case of the column view
152 * when 2 columns are shown, the root URL might be:
153 * /home/peter/Documents
154 * and DolphinView::url() might return
155 * /home/peter/Documents/Music/
157 KUrl
rootUrl() const;
160 * If \a active is true, the view will marked as active. The active
161 * view is defined as view where all actions are applied to.
163 void setActive(bool active
);
164 bool isActive() const;
167 * Changes the view mode for the current directory to \a mode.
168 * If the view properties should be remembered for each directory
169 * (GeneralSettings::globalViewProps() returns false), then the
170 * changed view mode will be stored automatically.
172 void setMode(Mode mode
);
175 /** See setShowPreview */
176 bool showPreview() const;
178 /** See setShowHiddenFiles */
179 bool showHiddenFiles() const;
181 /** See setCategorizedSorting */
182 bool categorizedSorting() const;
185 * Returns true, if the categorized sorting is supported by the current
186 * used mode (see DolphinView::setMode()). Currently only DolphinView::IconsView
187 * supports categorizations. To check whether the categorized
188 * sorting is set, use DolphinView::categorizedSorting().
190 bool supportsCategorizedSorting() const;
194 * @see DolphinView::selectedItems()
199 * Inverts the current selection: selected items get unselected,
200 * unselected items get selected.
201 * @see DolphinView::selectedItems()
203 void invertSelection();
205 /** Returns true, if at least one item is selected. */
206 bool hasSelection() const;
208 void clearSelection();
211 * Returns the selected items. The list is empty if no item has been
213 * @see DolphinView::selectedUrls()
215 KFileItemList
selectedItems() const;
218 * Returns a list of URLs for all selected items. An empty list
219 * is returned, if no item is selected.
220 * @see DolphinView::selectedItems()
222 KUrl::List
selectedUrls() const;
225 * Returns the number of selected items (this is faster than
226 * invoking selectedItems().count()).
228 int selectedItemsCount() const;
231 * Sets the upper left position of the view content
232 * to (x,y). The content of the view might be larger than the visible area
233 * and hence a scrolling must be done.
235 void setContentsPosition(int x
, int y
);
237 /** Returns the upper left position of the view content. */
238 QPoint
contentsPosition() const;
241 * Sets the zoom level to \a level. It is assured that the used
242 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
243 * ZoomLevelInfo::maximumLevel().
245 void setZoomLevel(int level
);
246 int zoomLevel() const;
249 * Returns true, if zooming in is possible. If false is returned,
250 * the maximum zooming level has been reached.
252 bool isZoomInPossible() const;
255 * Returns true, if zooming out is possible. If false is returned,
256 * the minimum zooming level has been reached.
258 bool isZoomOutPossible() const;
260 /** Sets the sort order of the items inside a directory (see DolphinView::Sorting). */
261 void setSorting(Sorting sorting
);
263 /** Returns the sort order of the items inside a directory (see DolphinView::Sorting). */
264 Sorting
sorting() const;
266 /** Sets the sort order (Qt::Ascending or Qt::Descending) for the items. */
267 void setSortOrder(Qt::SortOrder order
);
269 /** Returns the current used sort order (Qt::Ascending or Qt::Descending). */
270 Qt::SortOrder
sortOrder() const;
272 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
273 void setSortFoldersFirst(bool foldersFirst
);
275 /** Returns if files and folders are sorted separately or not. */
276 bool sortFoldersFirst() const;
278 /** Sets the additional information which should be shown for the items. */
279 void setAdditionalInfo(KFileItemDelegate::InformationList info
);
281 /** Returns the additional information which should be shown for the items. */
282 KFileItemDelegate::InformationList
additionalInfo() const;
284 /** Reloads the current directory. */
288 * Refreshes the view to get synchronized with the (updated) Dolphin settings.
289 * This method only needs to get invoked if the view settings for the Icons View,
290 * Details View or Columns View have been changed.
295 * Changes the directory of the view to \a url. If \a rootUrl is empty, the view
296 * properties from \a url are used for adjusting the view mode and the other properties.
297 * If \a rootUrl is not empty, the view properties from the root URL are considered
298 * instead. Specifying a root URL is only required if a view having a different root URL
299 * (e. g. the column view) should be restored. Usually using DolphinView::setUrl()
300 * is enough for changing the current URL.
302 void updateView(const KUrl
& url
, const KUrl
& rootUrl
);
305 * Filters the currently shown items by \a nameFilter. All items
306 * which contain the given filter string will be shown.
308 void setNameFilter(const QString
& nameFilter
);
311 * Calculates the number of currently shown files into
312 * \a fileCount and the number of folders into \a folderCount.
313 * The size of all files is written into \a totalFileSize.
314 * It is recommend using this method instead of asking the
315 * directory lister or the model directly, as it takes
316 * filtering and hierarchical previews into account.
318 void calculateItemCount(int& fileCount
, int& folderCount
, KIO::filesize_t
& totalFileSize
) const;
321 * Returns a textual representation of the state of the current
322 * folder or selected items, suitable for use in the status bar.
324 QString
statusBarText() const;
327 * Updates the state of the 'Additional Information' actions in \a collection.
329 void updateAdditionalInfoActions(KActionCollection
* collection
);
332 * Returns the state of the paste action:
333 * first is whether the action should be enabled
334 * second is the text for the action
336 QPair
<bool, QString
> pasteInfo() const;
339 * If \a tabsForFiles is true, the signal tabRequested() will also
340 * emitted also for files. Per default tabs for files is disabled
341 * and hence the signal tabRequested() will only be emitted for
344 void setTabsForFilesEnabled(bool tabsForFiles
);
345 bool isTabsForFilesEnabled() const;
348 * Returns true if the current view allows folders to be expanded,
349 * i.e. presents a hierarchical view to the user.
351 bool itemsExpandable() const;
355 * Changes the directory to \a url. If the current directory is equal to
356 * \a url, nothing will be done (use DolphinView::reload() instead).
358 void setUrl(const KUrl
& url
);
361 * Request of a selection change. The view will do its best to accommodate
362 * the request, but it is not guaranteed that all items in \a selection
363 * will actually get selected. The view will e.g. not select items which
364 * are not in the currently displayed folder.
366 void changeSelection(const KFileItemList
& selection
);
369 * Triggers the renaming of the currently selected items, where
370 * the user must input a new name for the items.
372 void renameSelectedItems();
375 * Moves all selected items to the trash.
377 void trashSelectedItems();
380 * Deletes all selected items.
382 void deleteSelectedItems();
385 * Copies all selected items to the clipboard and marks
386 * the items as cutted.
388 void cutSelectedItems();
390 /** Copies all selected items to the clipboard. */
391 void copySelectedItems();
393 /** Pastes the clipboard data to this view. */
397 * Pastes the clipboard data into the currently selected
398 * folder. If the current selection is not exactly one folder, no
399 * paste operation is done.
401 void pasteIntoFolder();
404 * Turns on the file preview for the all files of the current directory,
405 * if \a show is true.
406 * If the view properties should be remembered for each directory
407 * (GeneralSettings::globalViewProps() returns false), then the
408 * preview setting will be stored automatically.
410 void setShowPreview(bool show
);
413 * Shows all hidden files of the current directory,
414 * if \a show is true.
415 * If the view properties should be remembered for each directory
416 * (GeneralSettings::globalViewProps() returns false), then the
417 * show hidden file setting will be stored automatically.
419 void setShowHiddenFiles(bool show
);
422 * Summarizes all sorted items by their category \a categorized
424 * If the view properties should be remembered for each directory
425 * (GeneralSettings::globalViewProps() returns false), then the
426 * categorized sorting setting will be stored automatically.
428 void setCategorizedSorting(bool categorized
);
430 /** Switches between an ascending and descending sorting order. */
431 void toggleSortOrder();
433 /** Switches between a separate sorting (with folders first) and a mixed sorting of files and folders. */
434 void toggleSortFoldersFirst();
437 * Switches on or off the displaying of additional information
438 * as specified by \a action.
440 void toggleAdditionalInfo(QAction
* action
);
444 * Is emitted if the view has been activated by e. g. a mouse click.
448 /** Is emitted if URL of the view has been changed to \a url. */
449 void urlChanged(const KUrl
& url
);
452 * Is emitted if the view requests a changing of the current
453 * URL to \a url (see DolphinController::triggerUrlChangeRequest()).
455 void requestUrlChange(const KUrl
& url
);
458 * Is emitted when clicking on an item with the left mouse button.
460 void itemTriggered(const KFileItem
& item
);
463 * Is emitted if a new tab should be opened for the URL \a url.
465 void tabRequested(const KUrl
& url
);
468 * Is emitted if the view mode (IconsView, DetailsView,
469 * PreviewsView) has been changed.
473 /** Is emitted if the 'show preview' property has been changed. */
474 void showPreviewChanged();
476 /** Is emitted if the 'show hidden files' property has been changed. */
477 void showHiddenFilesChanged();
479 /** Is emitted if the 'categorized sorting' property has been changed. */
480 void categorizedSortingChanged();
482 /** Is emitted if the sorting by name, size or date has been changed. */
483 void sortingChanged(DolphinView::Sorting sorting
);
485 /** Is emitted if the sort order (ascending or descending) has been changed. */
486 void sortOrderChanged(Qt::SortOrder order
);
488 /** Is emitted if the sorting of files and folders (separate with folders first or mixed) has been changed. */
489 void sortFoldersFirstChanged(bool foldersFirst
);
491 /** Is emitted if the additional information shown for this view has been changed. */
492 void additionalInfoChanged();
494 /** Is emitted if the zoom level has been changed by zooming in or out. */
495 void zoomLevelChanged(int level
);
498 * Is emitted if information of an item is requested to be shown e. g. in the panel.
499 * If item is null, no item information request is pending.
501 void requestItemInfo(const KFileItem
& item
);
503 /** Is emitted if the contents has been moved to \a x, \a y. */
504 void contentsMoved(int x
, int y
);
507 * Is emitted whenever the selection has been changed.
509 void selectionChanged(const KFileItemList
& selection
);
512 * Is emitted if a context menu is requested for the item \a item,
513 * which is part of \a url. If the item is null, the context menu
514 * for the URL should be shown and the custom actions \a customActions
517 void requestContextMenu(const KFileItem
& item
,
519 const QList
<QAction
*>& customActions
);
522 * Is emitted if an information message with the content \a msg
525 void infoMessage(const QString
& msg
);
528 * Is emitted if an error message with the content \a msg
531 void errorMessage(const QString
& msg
);
534 * Is emitted if an "operation completed" message with the content \a msg
537 void operationCompletedMessage(const QString
& msg
);
540 * Is emitted after DolphinView::setUrl() has been invoked and
541 * the path \a url is currently loaded. If this signal is emitted,
542 * it is assured that the view contains already the correct root
543 * URL and property settings.
545 void startedPathLoading(const KUrl
& url
);
548 * Emitted when KDirLister emits redirection.
549 * Testcase: fish://localhost
551 void redirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
554 /** @see QWidget::mouseReleaseEvent */
555 virtual void mouseReleaseEvent(QMouseEvent
* event
);
556 virtual void wheelEvent(QWheelEvent
* event
);
557 virtual bool eventFilter(QObject
* watched
, QEvent
* event
);
561 * Marks the view as active (DolphinView:isActive() will return true)
562 * and emits the 'activated' signal if it is not already active.
567 * If the item \a item is a directory, then this
568 * directory will be loaded. If the item is a file, the corresponding
569 * application will get started.
571 void triggerItem(const KFileItem
& index
);
573 void emitSelectionChangedSignal();
576 * Opens the context menu on position \a pos. The position
577 * is used to check whether the context menu is related to an
578 * item or to the viewport.
580 void openContextMenu(const QPoint
& pos
, const QList
<QAction
*>& customActions
);
583 * Drops dragged URLs to the destination path \a destPath. If
584 * the URLs are dropped above an item inside the destination path,
585 * the item is indicated by \a destItem.
587 void dropUrls(const KFileItem
& destItem
,
588 const KUrl
& destPath
,
592 * Updates the view properties of the current URL to the
593 * sorting given by \a sorting.
595 void updateSorting(DolphinView::Sorting sorting
);
598 * Updates the view properties of the current URL to the
599 * sort order given by \a order.
601 void updateSortOrder(Qt::SortOrder order
);
604 * Updates the view properties of the current URL to the
605 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
607 void updateSortFoldersFirst(bool foldersFirst
);
610 * Updates the view properties of the current URL to the
611 * additional information given by \a info.
613 void updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
);
616 * Emits the signal contentsMoved with the current coordinates
617 * of the viewport as parameters.
619 void emitContentsMoved();
622 * Updates the status bar to show hover information for the
623 * item \a item. If currently other items are selected,
624 * no hover information is shown.
625 * @see DolphinView::clearHoverInformation()
627 void showHoverInformation(const KFileItem
& item
);
630 * Clears the hover information shown in the status bar.
631 * @see DolphinView::showHoverInformation().
633 void clearHoverInformation();
636 * Indicates in the status bar that the delete operation
637 * of the job \a job has been finished.
639 void slotDeleteFileFinished(KJob
* job
);
642 * Is emitted if the controller requests a changing of the current
645 void slotRequestUrlChange(const KUrl
& url
);
648 * Invoked when the directory lister has completed the loading of
649 * items. Assures that pasted items and renamed items get seleced.
651 void slotDirListerCompleted();
654 * Is invoked when the KDirLister indicates refreshed items.
656 void slotRefreshItems();
659 * If \a view can be positively identified as not being the source for the
660 * current drag operation, deleteLater() it immediately. Else stores
661 * it for later deletion.
663 void deleteWhenNotDragSource(QAbstractItemView
* view
);
666 * Observes the item with the URL \a url. As soon as the directory
667 * model indicates that the item is available, the item will
668 * get selected and it is assure that the item stays visible.
670 * @see selectAndScrollToCreatedItem()
672 void observeCreatedItem(const KUrl
& url
);
675 * Selects and scrolls to the item that got observed
676 * by observeCreatedItem().
678 void selectAndScrollToCreatedItem();
681 * Restore selection after view refresh.
683 void restoreSelection();
686 * Invoked when the undo manager indicates a finished operation.
687 * If a copy/move-operation has been done, the pasted items get selected.
689 void slotJobRecordingFinished(CommandType command
);
692 void loadDirectory(const KUrl
& url
, bool reload
= false);
695 * Returns the URL where the view properties should be stored. Usually
696 * DolphinView::url() is returned, but in the case of a Column View the
697 * view properties are always stored in the directory represented by the
698 * first column. It is recommendend whenever using the ViewProperties class
699 * to use DolphinView::viewPropertiesUrl() as URL.
701 KUrl
viewPropertiesUrl() const;
704 * Applies the view properties which are defined by the current URL
705 * m_url to the DolphinView properties.
707 void applyViewProperties(const KUrl
& url
);
710 * Creates a new view representing the given view mode (DolphinView::mode()).
711 * The current view will get deleted.
718 * Returns a pointer to the currently used item view, which is either
719 * a ListView or a TreeView.
721 QAbstractItemView
* itemView() const;
724 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
725 * Pastes the clipboard data into the URL \a url.
727 void pasteToUrl(const KUrl
& url
);
730 * Checks whether the current item view has the same zoom level
731 * as \a oldZoomLevel. If this is not the case, the zoom level
732 * of the controller is updated and a zoomLevelChanged() signal
735 void updateZoomLevel(int oldZoomLevel
);
738 * Returns a list of URLs for all selected items. The list is
739 * simplified, so that when the URLs are part of different tree
740 * levels, only the parent is returned.
742 KUrl::List
simplifiedSelectedUrls() const;
745 * Returns true, if the ColumnView is activated. As the column view
746 * requires some special handling for iterating through directories,
747 * this method has been introduced for convenience.
749 bool isColumnViewActive() const;
752 * Returns the MIME data for all selected items.
754 QMimeData
* selectionMimeData() const;
758 bool m_showPreview
: 1;
759 bool m_loadingDirectory
: 1;
760 bool m_storedCategorizedSorting
: 1;
761 bool m_tabsForFiles
: 1;
762 bool m_isContextMenuOpen
: 1; // TODO: workaround for Qt-issue 207192
763 bool m_ignoreViewProperties
: 1;
764 bool m_assureVisibleCurrentIndex
: 1;
765 bool m_selectClipboardItems
: 1;
769 DolphinMainWindow
* m_mainWindow
;
770 QVBoxLayout
* m_topLayout
;
772 DolphinController
* m_controller
;
773 DolphinIconsView
* m_iconsView
;
774 DolphinDetailsView
* m_detailsView
;
775 DolphinColumnView
* m_columnView
;
776 DolphinFileItemDelegate
* m_fileItemDelegate
;
777 QItemSelectionModel
* m_selectionModel
;
779 DolphinModel
* m_dolphinModel
;
780 KDirLister
* m_dirLister
;
781 DolphinSortFilterProxyModel
* m_proxyModel
;
783 KFilePreviewGenerator
* m_previewGenerator
;
784 ToolTipManager
* m_toolTipManager
;
787 KUrl m_currentItemUrl
;
788 KUrl m_createdItemUrl
; // URL for a new item that got created by the "Create New..." menu
789 KFileItemList m_selectedItems
; //this is used for making the View to remember selections after F5
791 QAbstractItemView
* m_expandedDragSource
;
794 inline bool DolphinView::isColumnViewActive() const
796 return m_columnView
!= 0;
799 /// Allow using DolphinView::Mode in QVariant
800 Q_DECLARE_METATYPE(DolphinView::Mode
)
802 #endif // DOLPHINVIEW_H