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 ***************************************************************************/
25 #include <config-nepomuk.h>
27 #include "libdolphin_export.h"
29 #include <kparts/part.h>
31 #include <KFileItemDelegate>
32 #include <kio/fileundomanager.h>
37 #include <QLinkedList>
41 typedef KIO::FileUndoManager::CommandType CommandType
;
43 class DolphinDirLister
;
44 class DolphinItemListContainer
;
46 class KActionCollection
;
54 * @short Represents a view for the directory content.
56 * View modes for icons, compact and details are supported. It's
64 class LIBDOLPHINPRIVATE_EXPORT DolphinView
: public QWidget
70 * Defines the view mode for a directory. The
71 * view mode is automatically updated if the directory itself
72 * defines a view mode (see class ViewProperties for details).
77 * The items are shown as icons with a name-label below.
82 * The icon, the name and the size of the items are
83 * shown per default as a table.
88 * The items are shown as icons with the name-label aligned
94 /** Defines the sort order for the items of a directory. */
108 /** Defines the additional information shown for the items of a directory. */
124 * @param url Specifies the content which should be shown.
125 * @param parent Parent widget of the view.
127 DolphinView(const KUrl
& url
, QWidget
* parent
);
129 virtual ~DolphinView();
132 * Returns the current active URL, where all actions are applied.
133 * The URL navigator is synchronized with this URL.
138 * If \a active is true, the view will marked as active. The active
139 * view is defined as view where all actions are applied to.
141 void setActive(bool active
);
142 bool isActive() const;
145 * Changes the view mode for the current directory to \a mode.
146 * If the view properties should be remembered for each directory
147 * (GeneralSettings::globalViewProps() returns false), then the
148 * changed view mode will be stored automatically.
150 void setMode(Mode mode
);
153 /** See setPreviewsShown */
154 bool previewsShown() const;
156 /** See setShowHiddenFiles */
157 bool hiddenFilesShown() const;
159 /** See setCategorizedSorting */
160 bool categorizedSorting() const;
163 * Returns the items of the view.
165 KFileItemList
items() const;
168 * Returns the selected items. The list is empty if no item has been
171 KFileItemList
selectedItems() const;
174 * Returns the number of selected items (this is faster than
175 * invoking selectedItems().count()).
177 int selectedItemsCount() const;
180 * Marks the items indicated by \p urls to get selected after the
181 * directory DolphinView::url() has been loaded. Note that nothing
182 * gets selected if no loading of a directory has been triggered
183 * by DolphinView::setUrl() or DolphinView::reload().
185 void markUrlsAsSelected(const QList
<KUrl
>& urls
);
188 * All items that match to the pattern \a pattern will get selected
189 * if \a enabled is true and deselected if \a enabled is false.
191 void setItemSelectionEnabled(const QRegExp
& pattern
, bool enabled
);
194 * Sets the zoom level to \a level. It is assured that the used
195 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
196 * ZoomLevelInfo::maximumLevel().
198 void setZoomLevel(int level
);
199 int zoomLevel() const;
202 * Returns true, if zooming in is possible. If false is returned,
203 * the maximum zooming level has been reached.
205 bool isZoomInPossible() const;
208 * Returns true, if zooming out is possible. If false is returned,
209 * the minimum zooming level has been reached.
211 bool isZoomOutPossible() const;
213 /** Sets the sorting criterion (e.g., SortByName, SortBySize,...) of the items inside a directory (see DolphinView::Sorting). */
214 void setSorting(Sorting sorting
);
216 /** Returns the sorting criterion (e.g., SortByName, SortBySize,...) of the items inside a directory (see DolphinView::Sorting). */
217 Sorting
sorting() const;
219 /** Sets the sort order (Qt::Ascending or Qt::Descending) for the items. */
220 void setSortOrder(Qt::SortOrder order
);
222 /** Returns the currently used sort order (Qt::Ascending or Qt::Descending). */
223 Qt::SortOrder
sortOrder() const;
225 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
226 void setSortFoldersFirst(bool foldersFirst
);
228 /** Returns if files and folders are sorted separately or not. */
229 bool sortFoldersFirst() const;
231 /** Sets the additional information which should be shown for the items. */
232 void setAdditionalInfoList(const QList
<AdditionalInfo
>& info
);
234 /** Returns the additional information which should be shown for the items. */
235 QList
<AdditionalInfo
> additionalInfoList() const;
237 /** Reloads the current directory. */
243 * Refreshes the view to get synchronized with the (updated) Dolphin settings.
244 * This method only needs to get invoked if the view settings for the Icons View,
245 * Details View or Columns View have been changed.
250 * Filters the currently shown items by \a nameFilter. All items
251 * which contain the given filter string will be shown.
253 void setNameFilter(const QString
& nameFilter
);
254 QString
nameFilter() const;
257 * Calculates the number of currently shown files into
258 * \a fileCount and the number of folders into \a folderCount.
259 * The size of all files is written into \a totalFileSize.
260 * It is recommend using this method instead of asking the
261 * directory lister or the model directly, as it takes
262 * filtering and hierarchical previews into account.
264 void calculateItemCount(int& fileCount
, int& folderCount
, KIO::filesize_t
& totalFileSize
) const;
267 * Returns a textual representation of the state of the current
268 * folder or selected items, suitable for use in the status bar.
270 QString
statusBarText() const;
273 * Returns the version control actions that are provided for the items \p items.
274 * Usually the actions are presented in the context menu.
276 QList
<QAction
*> versionControlActions(const KFileItemList
& items
) const;
279 * Returns the state of the paste action:
280 * first is whether the action should be enabled
281 * second is the text for the action
283 QPair
<bool, QString
> pasteInfo() const;
286 * If \a tabsForFiles is true, the signal tabRequested() will also
287 * emitted also for files. Per default tabs for files is disabled
288 * and hence the signal tabRequested() will only be emitted for
291 void setTabsForFilesEnabled(bool tabsForFiles
);
292 bool isTabsForFilesEnabled() const;
295 * Returns true if the current view allows folders to be expanded,
296 * i.e. presents a hierarchical view to the user.
298 bool itemsExpandable() const;
301 * Restores the view state (current item, contents position, details view expansion state)
303 void restoreState(QDataStream
& stream
);
306 * Saves the view state (current item, contents position, details view expansion state)
308 void saveState(QDataStream
& stream
);
310 /** Returns true, if at least one item is selected. */
311 bool hasSelection() const;
314 * Returns the root item which represents the current URL. Note that the returned
315 * item can be null (KFileItem::isNull() will return true) in case that the directory
316 * has not been loaded.
318 KFileItem
rootItem() const;
322 * Changes the directory to \a url. If the current directory is equal to
323 * \a url, nothing will be done (use DolphinView::reload() instead).
325 void setUrl(const KUrl
& url
);
329 * @see DolphinView::selectedItems()
334 * Inverts the current selection: selected items get unselected,
335 * unselected items get selected.
336 * @see DolphinView::selectedItems()
338 void invertSelection();
340 void clearSelection();
343 * Triggers the renaming of the currently selected items, where
344 * the user must input a new name for the items.
346 void renameSelectedItems();
349 * Moves all selected items to the trash.
351 void trashSelectedItems();
354 * Deletes all selected items.
356 void deleteSelectedItems();
359 * Copies all selected items to the clipboard and marks
362 void cutSelectedItems();
364 /** Copies all selected items to the clipboard. */
365 void copySelectedItems();
367 /** Pastes the clipboard data to this view. */
371 * Pastes the clipboard data into the currently selected
372 * folder. If the current selection is not exactly one folder, no
373 * paste operation is done.
375 void pasteIntoFolder();
378 * Turns on the file preview for the all files of the current directory,
379 * if \a show is true.
380 * If the view properties should be remembered for each directory
381 * (GeneralSettings::globalViewProps() returns false), then the
382 * preview setting will be stored automatically.
384 void setPreviewsShown(bool show
);
387 * Shows all hidden files of the current directory,
388 * if \a show is true.
389 * If the view properties should be remembered for each directory
390 * (GeneralSettings::globalViewProps() returns false), then the
391 * show hidden file setting will be stored automatically.
393 void setHiddenFilesShown(bool show
);
396 * Summarizes all sorted items by their category \a categorized
398 * If the view properties should be remembered for each directory
399 * (GeneralSettings::globalViewProps() returns false), then the
400 * categorized sorting setting will be stored automatically.
402 void setCategorizedSorting(bool categorized
);
406 * Is emitted if the view has been activated by e. g. a mouse click.
411 * Is emitted if the URL of the view will be changed to \a url.
412 * After the URL has been changed the signal urlChanged() will
415 void urlAboutToBeChanged(const KUrl
& url
);
417 /** Is emitted if the URL of the view has been changed to \a url. */
418 void urlChanged(const KUrl
& url
);
421 * Is emitted when clicking on an item with the left mouse button.
423 void itemActivated(const KFileItem
& item
);
426 * Is emitted if items have been added or deleted.
428 void itemCountChanged();
431 * Is emitted if a new tab should be opened for the URL \a url.
433 void tabRequested(const KUrl
& url
);
436 * Is emitted if the view mode (IconsView, DetailsView,
437 * PreviewsView) has been changed.
439 void modeChanged(DolphinView::Mode current
, DolphinView::Mode previous
);
441 /** Is emitted if the 'show preview' property has been changed. */
442 void previewsShownChanged(bool shown
);
444 /** Is emitted if the 'show hidden files' property has been changed. */
445 void hiddenFilesShownChanged(bool shown
);
447 /** Is emitted if the 'categorized sorting' property has been changed. */
448 void categorizedSortingChanged(bool sortCategorized
);
450 /** Is emitted if the sorting by name, size or date has been changed. */
451 void sortingChanged(DolphinView::Sorting sorting
);
453 /** Is emitted if the sort order (ascending or descending) has been changed. */
454 void sortOrderChanged(Qt::SortOrder order
);
456 /** Is emitted if the sorting of files and folders (separate with folders first or mixed) has been changed. */
457 void sortFoldersFirstChanged(bool foldersFirst
);
459 /** Is emitted if the additional information shown for this view has been changed. */
460 void additionalInfoListChanged(const QList
<DolphinView::AdditionalInfo
>& current
,
461 const QList
<DolphinView::AdditionalInfo
>& previous
);
463 /** Is emitted if the zoom level has been changed by zooming in or out. */
464 void zoomLevelChanged(int current
, int previous
);
467 * Is emitted if information of an item is requested to be shown e. g. in the panel.
468 * If item is null, no item information request is pending.
470 void requestItemInfo(const KFileItem
& item
);
473 * Is emitted whenever the selection has been changed.
475 void selectionChanged(const KFileItemList
& selection
);
478 * Is emitted if a context menu is requested for the item \a item,
479 * which is part of \a url. If the item is null, the context menu
480 * for the URL should be shown and the custom actions \a customActions
483 void requestContextMenu(const KFileItem
& item
,
485 const QList
<QAction
*>& customActions
);
488 * Is emitted if an information message with the content \a msg
491 void infoMessage(const QString
& msg
);
494 * Is emitted if an error message with the content \a msg
497 void errorMessage(const QString
& msg
);
500 * Is emitted if an "operation completed" message with the content \a msg
503 void operationCompletedMessage(const QString
& msg
);
506 * Is emitted after DolphinView::setUrl() has been invoked and
507 * the path \a url is currently loaded. If this signal is emitted,
508 * it is assured that the view contains already the correct root
509 * URL and property settings.
511 void startedPathLoading(const KUrl
& url
);
514 * Is emitted after the path triggered by DolphinView::setUrl()
517 void finishedPathLoading(const KUrl
& url
);
520 * Is emitted after DolphinView::setUrl() has been invoked and provides
521 * the information how much percent of the current path have been loaded.
523 void pathLoadingProgress(int percent
);
526 * Is emitted if the DolphinView::setUrl() is invoked but the URL is not
529 void urlIsFileError(const KUrl
& file
);
532 * Emitted when KDirLister emits redirection.
533 * Testcase: fish://localhost
535 void redirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
538 * Is emitted when the write state of the folder has been changed. The application
539 * should disable all actions like "Create New..." that depend on the write
542 void writeStateChanged(bool isFolderWritable
);
545 virtual void mouseReleaseEvent(QMouseEvent
* event
);
546 virtual void contextMenuEvent(QContextMenuEvent
* event
);
550 * Marks the view as active (DolphinView:isActive() will return true)
551 * and emits the 'activated' signal if it is not already active.
555 void slotItemActivated(int index
);
556 void slotItemMiddleClicked(int index
);
557 void slotContextMenuRequested(int index
, const QPointF
& pos
);
558 void slotItemExpansionToggleClicked(int index
);
559 void slotItemHovered(int index
);
560 void slotItemUnhovered(int index
);
563 * Emits the signal \a selectionChanged() with a small delay. This is
564 * because getting all file items for the selection can be an expensive
565 * operation. Fast selection changes are collected in this case and
566 * the signal is emitted only after no selection change has been done
567 * within a small delay.
569 void slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
);
572 * Is called by emitDelayedSelectionChangedSignal() and emits the
573 * signal \a selectionChanged() with all selected file items as parameter.
575 void emitSelectionChangedSignal();
578 * Opens the context menu on position \a pos. The position
579 * is used to check whether the context menu is related to an
580 * item or to the viewport.
582 void openContextMenu(const QPoint
& pos
, const QList
<QAction
*>& customActions
);
585 * Drops dragged URLs to the destination path \a destPath. If
586 * the URLs are dropped above an item inside the destination path,
587 * the item is indicated by \a destItem.
589 void dropUrls(const KFileItem
& destItem
,
590 const KUrl
& destPath
,
594 * Updates the view properties of the current URL to the
595 * sorting given by \a sorting.
597 void updateSorting(DolphinView::Sorting sorting
);
600 * Updates the view properties of the current URL to the
601 * sort order given by \a order.
603 void updateSortOrder(Qt::SortOrder order
);
606 * Updates the view properties of the current URL to the
607 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
609 void updateSortFoldersFirst(bool foldersFirst
);
612 * Updates the status bar to show hover information for the
613 * item \a item. If currently other items are selected,
614 * no hover information is shown.
615 * @see DolphinView::clearHoverInformation()
617 void showHoverInformation(const KFileItem
& item
);
620 * Clears the hover information shown in the status bar.
621 * @see DolphinView::showHoverInformation().
623 void clearHoverInformation();
626 * Indicates in the status bar that the delete operation
627 * of the job \a job has been finished.
629 void slotDeleteFileFinished(KJob
* job
);
632 * Invoked when the directory lister has been started the
635 void slotDirListerStarted(const KUrl
& url
);
638 * Invoked when the directory lister has completed the loading of
639 * items. Assures that pasted items and renamed items get seleced.
641 void slotDirListerCompleted();
644 * Is invoked when the KDirLister indicates refreshed items.
646 void slotRefreshItems();
649 * Observes the item with the URL \a url. As soon as the directory
650 * model indicates that the item is available, the item will
651 * get selected and it is assured that the item stays visible.
653 * @see selectAndScrollToCreatedItem()
655 void observeCreatedItem(const KUrl
& url
);
658 * Selects and scrolls to the item that got observed
659 * by observeCreatedItem().
661 void selectAndScrollToCreatedItem();
664 * Called when a redirection happens.
665 * Testcase: fish://localhost
667 void slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
670 * Applies the state that has been restored by restoreViewState()
673 void updateViewState();
675 //void slotUrlChangeRequested(const KUrl& url);
678 KFileItemModel
* fileItemModel() const;
680 void loadDirectory(const KUrl
& url
, bool reload
= false);
683 * Applies the view properties which are defined by the current URL
684 * to the DolphinView properties.
686 void applyViewProperties();
688 void applyAdditionalInfoListToView();
691 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
692 * Pastes the clipboard data into the URL \a url.
694 void pasteToUrl(const KUrl
& url
);
697 * Checks whether the current item view has the same zoom level
698 * as \a oldZoomLevel. If this is not the case, the zoom level
699 * of the controller is updated and a zoomLevelChanged() signal
702 void updateZoomLevel(int oldZoomLevel
);
705 * Returns a list of URLs for all selected items. The list is
706 * simplified, so that when the URLs are part of different tree
707 * levels, only the parent is returned.
709 KUrl::List
simplifiedSelectedUrls() const;
712 * Returns the MIME data for all selected items.
714 QMimeData
* selectionMimeData() const;
717 * Is invoked after a paste operation or a drag & drop
718 * operation and URLs from \a mimeData as selected.
719 * This allows to select all newly pasted
720 * items in restoreViewState().
722 void markPastedUrlsAsSelected(const QMimeData
* mimeData
);
725 * Updates m_isFolderWritable dependent on whether the folder represented by
726 * the current URL is writable. If the state has changed, the signal
727 * writeableStateChanged() will be emitted.
729 void updateWritableState();
731 QByteArray
sortRoleForSorting(Sorting sorting
) const;
735 bool m_tabsForFiles
: 1;
736 bool m_assureVisibleCurrentIndex
: 1;
737 bool m_isFolderWritable
: 1;
741 QList
<AdditionalInfo
> m_additionalInfoList
;
743 QVBoxLayout
* m_topLayout
;
745 DolphinDirLister
* m_dirLister
;
746 DolphinItemListContainer
* m_container
;
748 ToolTipManager
* m_toolTipManager
;
750 QTimer
* m_selectionChangedTimer
;
752 int m_currentItemIndex
;
753 QPoint m_restoredContentsPosition
;
754 KUrl m_createdItemUrl
; // URL for a new item that got created by the "Create New..." menu
755 KFileItemList m_selectedItems
; // this is used for making the View to remember selections after F5
758 friend class TestBase
;
759 friend class DolphinDetailsViewTest
;
762 /// Allow using DolphinView::Mode in QVariant
763 Q_DECLARE_METATYPE(DolphinView::Mode
)
765 #endif // DOLPHINVIEW_H