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 class DolphinController
;
42 class DolphinColumnView
;
43 class DolphinDetailsView
;
44 class DolphinFileItemDelegate
;
45 class DolphinIconsView
;
46 class DolphinMainWindow
;
48 class DolphinSortFilterProxyModel
;
49 class KFilePreviewGenerator
;
51 class KActionCollection
;
60 * @short Represents a view for the directory content.
62 * View modes for icons, details and columns are supported. It's
69 * @see DolphinIconsView
70 * @see DolphinDetailsView
71 * @see DolphinColumnView
73 class LIBDOLPHINPRIVATE_EXPORT DolphinView
: public QWidget
79 * Defines the view mode for a directory. The view mode
80 * can be defined when constructing a DolphinView. The
81 * view mode is automatically updated if the directory itself
82 * defines a view mode (see class ViewProperties for details).
87 * The directory items are shown as icons including an
92 * The icon, the name and at least the size of the directory
93 * items are shown in a table. It is possible to add columns
94 * for date, group and permissions.
99 * Each folder is shown in a separate column.
102 MaxModeEnum
= ColumnView
105 /** Defines the sort order for the items of a directory. */
117 MaxSortEnum
= SortByTags
121 * @param parent Parent widget of the view.
122 * @param url Specifies the content which should be shown.
123 * @param dirLister Used directory lister. The lister is not owned
124 * by the view and won't get deleted.
125 * @param dolphinModel Used directory model. The model is not owned
126 * by the view and won't get deleted.
127 * @param proxyModel Used proxy model which specifies the sorting. The
128 * model is not owned by the view and won't get
131 DolphinView(QWidget
* parent
,
133 KDirLister
* dirLister
,
134 DolphinModel
* dolphinModel
,
135 DolphinSortFilterProxyModel
* proxyModel
);
137 virtual ~DolphinView();
140 * Returns the current active URL, where all actions are applied.
141 * The URL navigator is synchronized with this URL.
143 const KUrl
& url() const;
146 * Returns the root URL of the view, which is defined as the first
147 * visible path of DolphinView::url(). Usually the root URL is
148 * equal to DolphinView::url(), but in the case of the column view
149 * when 2 columns are shown, the root URL might be:
150 * /home/peter/Documents
151 * and DolphinView::url() might return
152 * /home/peter/Documents/Music/
154 KUrl
rootUrl() const;
157 * If \a active is true, the view will marked as active. The active
158 * view is defined as view where all actions are applied to.
160 void setActive(bool active
);
161 bool isActive() const;
164 * Changes the view mode for the current directory to \a mode.
165 * If the view properties should be remembered for each directory
166 * (GeneralSettings::globalViewProps() returns false), then the
167 * changed view mode will be stored automatically.
169 void setMode(Mode mode
);
172 /** See setShowPreview */
173 bool showPreview() const;
175 /** See setShowHiddenFiles */
176 bool showHiddenFiles() const;
178 /** See setCategorizedSorting */
179 bool categorizedSorting() const;
182 * Returns true, if the categorized sorting is supported by the current
183 * used mode (see DolphinView::setMode()). Currently only DolphinView::IconsView
184 * supports categorizations. To check whether the categorized
185 * sorting is set, use DolphinView::categorizedSorting().
187 bool supportsCategorizedSorting() const;
191 * @see DolphinView::selectedItems()
196 * Inverts the current selection: selected items get unselected,
197 * unselected items get selected.
198 * @see DolphinView::selectedItems()
200 void invertSelection();
202 /** Returns true, if at least one item is selected. */
203 bool hasSelection() const;
205 void clearSelection();
208 * Returns the selected items. The list is empty if no item has been
210 * @see DolphinView::selectedUrls()
212 KFileItemList
selectedItems() const;
215 * Returns a list of URLs for all selected items. An empty list
216 * is returned, if no item is selected.
217 * @see DolphinView::selectedItems()
219 KUrl::List
selectedUrls() const;
222 * Returns the number of selected items (this is faster than
223 * invoking selectedItems().count()).
225 int selectedItemsCount() const;
228 * Sets the upper left position of the view content
229 * to (x,y). The content of the view might be larger than the visible area
230 * and hence a scrolling must be done.
232 void setContentsPosition(int x
, int y
);
234 /** Returns the upper left position of the view content. */
235 QPoint
contentsPosition() const;
238 * Sets the zoom level to \a level. It is assured that the used
239 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
240 * ZoomLevelInfo::maximumLevel().
242 void setZoomLevel(int level
);
243 int zoomLevel() const;
246 * Returns true, if zooming in is possible. If false is returned,
247 * the maximimum zooming level has been reached.
249 bool isZoomInPossible() const;
252 * Returns true, if zooming out is possible. If false is returned,
253 * the minimum zooming level has been reached.
255 bool isZoomOutPossible() const;
257 /** Sets the sort order of the items inside a directory (see DolphinView::Sorting). */
258 void setSorting(Sorting sorting
);
260 /** Returns the sort order of the items inside a directory (see DolphinView::Sorting). */
261 Sorting
sorting() const;
263 /** Sets the sort order (Qt::Ascending or Qt::Descending) for the items. */
264 void setSortOrder(Qt::SortOrder order
);
266 /** Returns the current used sort order (Qt::Ascending or Qt::Descending). */
267 Qt::SortOrder
sortOrder() const;
269 /** Sets the additional information which should be shown for the items. */
270 void setAdditionalInfo(KFileItemDelegate::InformationList info
);
272 /** Returns the additional information which should be shown for the items. */
273 KFileItemDelegate::InformationList
additionalInfo() const;
275 /** Reloads the current directory. */
279 * Refreshes the view to get synchronized with the (updated) Dolphin settings.
280 * This method only needs to get invoked if the view settings for the Icons View,
281 * Details View or Columns View have been changed.
286 * Changes the directory of the view to \a url. If \a rootUrl is empty, the view
287 * properties from \a url are used for adjusting the view mode and the other properties.
288 * If \a rootUrl is not empty, the view properties from the root URL are considered
289 * instead. Specifying a root URL is only required if a view having a different root URL
290 * (e. g. the column view) should be restored. Usually using DolphinView::setUrl()
291 * is enough for changing the current URL.
293 void updateView(const KUrl
& url
, const KUrl
& rootUrl
);
296 * Filters the currently shown items by \a nameFilter. All items
297 * which contain the given filter string will be shown.
299 void setNameFilter(const QString
& nameFilter
);
302 * Calculates the number of currently shown files into
303 * \a fileCount and the number of folders into \a folderCount.
304 * It is recommend using this method instead of asking the
305 * directory lister or the model directly, as it takes
306 * filtering and hierarchical previews into account.
308 void calculateItemCount(int& fileCount
, int& folderCount
) const;
311 * Returns a textual representation of the state of the current
312 * folder or selected items, suitable for use in the status bar.
314 QString
statusBarText() const;
317 * Updates the state of the 'Additional Information' actions in \a collection.
319 void updateAdditionalInfoActions(KActionCollection
* collection
);
322 * Returns the state of the paste action:
323 * first is whether the action should be enabled
324 * second is the text for the action
326 QPair
<bool, QString
> pasteInfo() const;
329 * If \a tabsForFiles is true, the signal tabRequested() will also
330 * emitted also for files. Per default tabs for files is disabled
331 * and hence the signal tabRequested() will only be emitted for
334 void setTabsForFilesEnabled(bool tabsForFiles
);
335 bool isTabsForFilesEnabled() const;
338 * Returns true if the current view allows folders to be expanded,
339 * i.e. presents a hierarchical view to the user.
341 bool itemsExpandable() const;
345 * Changes the directory to \a url. If the current directory is equal to
346 * \a url, nothing will be done (use DolphinView::reload() instead).
348 void setUrl(const KUrl
& url
);
351 * Request of a selection change. The view will do its best to accommodate
352 * the request, but it is not guaranteed that all items in \a selection
353 * will actually get selected. The view will e.g. not select items which
354 * are not in the currently displayed folder.
356 void changeSelection(const KFileItemList
& selection
);
359 * Triggers the renaming of the currently selected items, where
360 * the user must input a new name for the items.
362 void renameSelectedItems();
365 * Moves all selected items to the trash.
367 void trashSelectedItems();
370 * Deletes all selected items.
372 void deleteSelectedItems();
375 * Copies all selected items to the clipboard and marks
376 * the items as cutted.
378 void cutSelectedItems();
380 /** Copies all selected items to the clipboard. */
381 void copySelectedItems();
383 /** Pastes the clipboard data to this view. */
387 * Pastes the clipboard data into the currently selected
388 * folder. If the current selection is not exactly one folder, no
389 * paste operation is done.
391 void pasteIntoFolder();
394 * Turns on the file preview for the all files of the current directory,
395 * if \a show is true.
396 * If the view properties should be remembered for each directory
397 * (GeneralSettings::globalViewProps() returns false), then the
398 * preview setting will be stored automatically.
400 void setShowPreview(bool show
);
403 * Shows all hidden files of the current directory,
404 * if \a show is true.
405 * If the view properties should be remembered for each directory
406 * (GeneralSettings::globalViewProps() returns false), then the
407 * show hidden file setting will be stored automatically.
409 void setShowHiddenFiles(bool show
);
412 * Summarizes all sorted items by their category \a categorized
414 * If the view properties should be remembered for each directory
415 * (GeneralSettings::globalViewProps() returns false), then the
416 * categorized sorting setting will be stored automatically.
418 void setCategorizedSorting(bool categorized
);
420 /** Switches between an ascending and descending sorting order. */
421 void toggleSortOrder();
424 * Switches on or off the displaying of additional information
425 * as specified by \a action.
427 void toggleAdditionalInfo(QAction
* action
);
431 * Is emitted if the view has been activated by e. g. a mouse click.
435 /** Is emitted if URL of the view has been changed to \a url. */
436 void urlChanged(const KUrl
& url
);
439 * Is emitted if the view requests a changing of the current
440 * URL to \a url (see DolphinController::triggerUrlChangeRequest()).
442 void requestUrlChange(const KUrl
& url
);
445 * Is emitted when clicking on an item with the left mouse button.
447 void itemTriggered(const KFileItem
& item
);
450 * Is emitted if a new tab should be opened for the URL \a url.
452 void tabRequested(const KUrl
& url
);
455 * Is emitted if the view mode (IconsView, DetailsView,
456 * PreviewsView) has been changed.
460 /** Is emitted if the 'show preview' property has been changed. */
461 void showPreviewChanged();
463 /** Is emitted if the 'show hidden files' property has been changed. */
464 void showHiddenFilesChanged();
466 /** Is emitted if the 'categorized sorting' property has been changed. */
467 void categorizedSortingChanged();
469 /** Is emitted if the sorting by name, size or date has been changed. */
470 void sortingChanged(DolphinView::Sorting sorting
);
472 /** Is emitted if the sort order (ascending or descending) has been changed. */
473 void sortOrderChanged(Qt::SortOrder order
);
475 /** Is emitted if the additional information shown for this view has been changed. */
476 void additionalInfoChanged();
478 /** Is emitted if the zoom level has been changed by zooming in or out. */
479 void zoomLevelChanged(int level
);
482 * Is emitted if information of an item is requested to be shown e. g. in the sidebar.
483 * If item is null, no item information request is pending.
485 void requestItemInfo(const KFileItem
& item
);
487 /** Is emitted if the contents has been moved to \a x, \a y. */
488 void contentsMoved(int x
, int y
);
491 * Is emitted whenever the selection has been changed.
493 void selectionChanged(const KFileItemList
& selection
);
496 * Is emitted if a context menu is requested for the item \a item,
497 * which is part of \a url. If the item is 0, the context menu
498 * for the URL should be shown.
500 void requestContextMenu(const KFileItem
& item
, const KUrl
& url
);
503 * Is emitted if an information message with the content \a msg
506 void infoMessage(const QString
& msg
);
509 * Is emitted if an error message with the content \a msg
512 void errorMessage(const QString
& msg
);
515 * Is emitted if an "operation completed" message with the content \a msg
518 void operationCompletedMessage(const QString
& msg
);
521 * Is emitted after DolphinView::setUrl() has been invoked and
522 * the path \a url is currently loaded. If this signal is emitted,
523 * it is assured that the view contains already the correct root
524 * URL and property settings.
526 void startedPathLoading(const KUrl
& url
);
529 /** @see QWidget::mouseReleaseEvent */
530 virtual void mouseReleaseEvent(QMouseEvent
* event
);
531 virtual void wheelEvent(QWheelEvent
* event
);
532 virtual bool eventFilter(QObject
* watched
, QEvent
* event
);
536 * Marks the view as active (DolphinView:isActive() will return true)
537 * and emits the 'activated' signal if it is not already active.
542 * If the item \a item is a directory, then this
543 * directory will be loaded. If the item is a file, the corresponding
544 * application will get started.
546 void triggerItem(const KFileItem
& index
);
548 void emitSelectionChangedSignal();
551 * Opens the context menu on position \a pos. The position
552 * is used to check whether the context menu is related to an
553 * item or to the viewport.
555 void openContextMenu(const QPoint
& pos
);
558 * Drops dragged URLs to the destination path \a destPath. If
559 * the URLs are dropped above an item inside the destination path,
560 * the item is indicated by \a destItem.
562 void dropUrls(const KFileItem
& destItem
,
563 const KUrl
& destPath
,
567 * Updates the view properties of the current URL to the
568 * sorting given by \a sorting.
570 void updateSorting(DolphinView::Sorting sorting
);
573 * Updates the view properties of the current URL to the
574 * sort order given by \a order.
576 void updateSortOrder(Qt::SortOrder order
);
579 * Updates the view properties of the current URL to the
580 * additional information given by \a info.
582 void updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
);
585 * Emits the signal contentsMoved with the current coordinates
586 * of the viewport as parameters.
588 void emitContentsMoved();
591 * Updates the status bar to show hover information for the
592 * item \a item. If currently other items are selected,
593 * no hover information is shown.
594 * @see DolphinView::clearHoverInformation()
596 void showHoverInformation(const KFileItem
& item
);
599 * Clears the hover information shown in the status bar.
600 * @see DolphinView::showHoverInformation().
602 void clearHoverInformation();
605 * Indicates in the status bar that the delete operation
606 * of the job \a job has been finished.
608 void slotDeleteFileFinished(KJob
* job
);
611 * Called when KDirLister emits redirection.
612 * Testcase: fish://localhost
614 void slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
617 * Is emitted if the controller requests a changing of the current
620 void slotRequestUrlChange(const KUrl
& url
);
623 * Restores the current item (= item that has the keyboard focus)
624 * to m_currentItemUrl.
626 void restoreCurrentItem();
629 void loadDirectory(const KUrl
& url
, bool reload
= false);
632 * Returns the URL where the view properties should be stored. Usually
633 * DolphinView::url() is returned, but in the case of a Column View the
634 * view properties are always stored in the directory represented by the
635 * first column. It is recommendend whenever using the ViewProperties class
636 * to use DolphinView::viewPropertiesUrl() as URL.
638 KUrl
viewPropertiesUrl() const;
641 * Applies the view properties which are defined by the current URL
642 * m_url to the DolphinView properties.
644 void applyViewProperties(const KUrl
& url
);
647 * Creates a new view representing the given view mode (DolphinView::mode()).
648 * The current view will get deleted.
655 * Returns a pointer to the currently used item view, which is either
656 * a ListView or a TreeView.
658 QAbstractItemView
* itemView() const;
661 * Returns true, if the item \a item has been cut into
664 bool isCutItem(const KFileItem
& item
) const;
667 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
668 * Pastes the clipboard data into the URL \a url.
670 void pasteToUrl(const KUrl
& url
);
673 * Checks whether the current item view has the same zoom level
674 * as \a oldZoomLevel. If this is not the case, the zoom level
675 * of the controller is updated and a zoomLevelChanged() signal
678 void updateZoomLevel(int oldZoomLevel
);
681 * Returns a list of URLs for all selected items. The list is
682 * simplified, so that when the URLs are part of different tree
683 * levels, only the parent is returned.
685 KUrl::List
simplifiedSelectedUrls() const;
688 * Returns true, if the ColumnView is activated. As the column view
689 * requires some special handling for iterating through directories,
690 * this method has been introduced for convenience.
692 bool isColumnViewActive() const;
696 bool m_showPreview
: 1;
697 bool m_loadingDirectory
: 1;
698 bool m_storedCategorizedSorting
: 1;
699 bool m_tabsForFiles
: 1;
700 bool m_isContextMenuOpen
: 1; // TODO: workaround for Qt-issue 207192
704 DolphinMainWindow
* m_mainWindow
;
705 QVBoxLayout
* m_topLayout
;
707 DolphinController
* m_controller
;
708 DolphinIconsView
* m_iconsView
;
709 DolphinDetailsView
* m_detailsView
;
710 DolphinColumnView
* m_columnView
;
711 DolphinFileItemDelegate
* m_fileItemDelegate
;
712 QItemSelectionModel
* m_selectionModel
;
714 DolphinModel
* m_dolphinModel
;
715 KDirLister
* m_dirLister
;
716 DolphinSortFilterProxyModel
* m_proxyModel
;
718 KFilePreviewGenerator
* m_previewGenerator
;
719 ToolTipManager
* m_toolTipManager
;
722 KUrl m_currentItemUrl
;
725 inline bool DolphinView::isColumnViewActive() const
727 return m_columnView
!= 0;
730 /// Allow using DolphinView::Mode in QVariant
731 Q_DECLARE_METATYPE(DolphinView::Mode
)
733 #endif // DOLPHINVIEW_H