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>
42 typedef KIO::FileUndoManager::CommandType CommandType
;
44 class DolphinDirLister
;
45 class DolphinItemListContainer
;
47 class KActionCollection
;
55 * @short Represents a view for the directory content.
57 * View modes for icons, compact and details are supported. It's
65 class LIBDOLPHINPRIVATE_EXPORT DolphinView
: public QWidget
71 * Defines the view mode for a directory. The
72 * view mode is automatically updated if the directory itself
73 * defines a view mode (see class ViewProperties for details).
78 * The items are shown as icons with a name-label below.
83 * The icon, the name and the size of the items are
84 * shown per default as a table.
89 * The items are shown as icons with the name-label aligned
95 /** Defines the sort order for the items of a directory. */
109 /** Defines the additional information shown for the items of a directory. */
125 * @param url Specifies the content which should be shown.
126 * @param parent Parent widget of the view.
128 DolphinView(const KUrl
& url
, QWidget
* parent
);
130 virtual ~DolphinView();
133 * Returns the current active URL, where all actions are applied.
134 * The URL navigator is synchronized with this URL.
139 * If \a active is true, the view will marked as active. The active
140 * view is defined as view where all actions are applied to.
142 void setActive(bool active
);
143 bool isActive() const;
146 * Changes the view mode for the current directory to \a mode.
147 * If the view properties should be remembered for each directory
148 * (GeneralSettings::globalViewProps() returns false), then the
149 * changed view mode will be stored automatically.
151 void setMode(Mode mode
);
154 /** See setPreviewsShown */
155 bool previewsShown() const;
157 /** See setShowHiddenFiles */
158 bool hiddenFilesShown() const;
160 /** See setCategorizedSorting */
161 bool categorizedSorting() const;
164 * Returns the items of the view.
166 KFileItemList
items() const;
169 * Returns the selected items. The list is empty if no item has been
172 KFileItemList
selectedItems() const;
175 * Returns the number of selected items (this is faster than
176 * invoking selectedItems().count()).
178 int selectedItemsCount() const;
181 * Marks the items indicated by \p urls to get selected after the
182 * directory DolphinView::url() has been loaded. Note that nothing
183 * gets selected if no loading of a directory has been triggered
184 * by DolphinView::setUrl() or DolphinView::reload().
186 void markUrlsAsSelected(const QList
<KUrl
>& urls
);
189 * All items that match to the pattern \a pattern will get selected
190 * if \a enabled is true and deselected if \a enabled is false.
192 void setItemSelectionEnabled(const QRegExp
& pattern
, bool enabled
);
195 * Sets the zoom level to \a level. It is assured that the used
196 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
197 * ZoomLevelInfo::maximumLevel().
199 void setZoomLevel(int level
);
200 int zoomLevel() const;
203 * Returns true, if zooming in is possible. If false is returned,
204 * the maximum zooming level has been reached.
206 bool isZoomInPossible() const;
209 * Returns true, if zooming out is possible. If false is returned,
210 * the minimum zooming level has been reached.
212 bool isZoomOutPossible() const;
214 /** Sets the sorting criterion (e.g., SortByName, SortBySize,...) of the items inside a directory (see DolphinView::Sorting). */
215 void setSorting(Sorting sorting
);
217 /** Returns the sorting criterion (e.g., SortByName, SortBySize,...) of the items inside a directory (see DolphinView::Sorting). */
218 Sorting
sorting() const;
220 /** Sets the sort order (Qt::Ascending or Qt::Descending) for the items. */
221 void setSortOrder(Qt::SortOrder order
);
223 /** Returns the currently used sort order (Qt::Ascending or Qt::Descending). */
224 Qt::SortOrder
sortOrder() const;
226 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
227 void setSortFoldersFirst(bool foldersFirst
);
229 /** Returns if files and folders are sorted separately or not. */
230 bool sortFoldersFirst() const;
232 /** Sets the additional information which should be shown for the items. */
233 void setAdditionalInfoList(const QList
<AdditionalInfo
>& info
);
235 /** Returns the additional information which should be shown for the items. */
236 QList
<AdditionalInfo
> additionalInfoList() const;
238 /** Reloads the current directory. */
244 * Refreshes the view to get synchronized with the (updated) Dolphin settings.
245 * This method only needs to get invoked if the view settings for the Icons View,
246 * Details View or Columns View have been changed.
251 * Filters the currently shown items by \a nameFilter. All items
252 * which contain the given filter string will be shown.
254 void setNameFilter(const QString
& nameFilter
);
255 QString
nameFilter() const;
258 * Calculates the number of currently shown files into
259 * \a fileCount and the number of folders into \a folderCount.
260 * The size of all files is written into \a totalFileSize.
261 * It is recommend using this method instead of asking the
262 * directory lister or the model directly, as it takes
263 * filtering and hierarchical previews into account.
265 void calculateItemCount(int& fileCount
, int& folderCount
, KIO::filesize_t
& totalFileSize
) const;
268 * Returns a textual representation of the state of the current
269 * folder or selected items, suitable for use in the status bar.
271 QString
statusBarText() const;
274 * Returns the version control actions that are provided for the items \p items.
275 * Usually the actions are presented in the context menu.
277 QList
<QAction
*> versionControlActions(const KFileItemList
& items
) const;
280 * Returns the state of the paste action:
281 * first is whether the action should be enabled
282 * second is the text for the action
284 QPair
<bool, QString
> pasteInfo() const;
287 * If \a tabsForFiles is true, the signal tabRequested() will also
288 * emitted also for files. Per default tabs for files is disabled
289 * and hence the signal tabRequested() will only be emitted for
292 void setTabsForFilesEnabled(bool tabsForFiles
);
293 bool isTabsForFilesEnabled() const;
296 * Returns true if the current view allows folders to be expanded,
297 * i.e. presents a hierarchical view to the user.
299 bool itemsExpandable() const;
302 * Restores the view state (current item, contents position, details view expansion state)
304 void restoreState(QDataStream
& stream
);
307 * Saves the view state (current item, contents position, details view expansion state)
309 void saveState(QDataStream
& stream
);
311 /** Returns true, if at least one item is selected. */
312 bool hasSelection() const;
315 * Returns the root item which represents the current URL. Note that the returned
316 * item can be null (KFileItem::isNull() will return true) in case that the directory
317 * has not been loaded.
319 KFileItem
rootItem() const;
323 * Changes the directory to \a url. If the current directory is equal to
324 * \a url, nothing will be done (use DolphinView::reload() instead).
326 void setUrl(const KUrl
& url
);
330 * @see DolphinView::selectedItems()
335 * Inverts the current selection: selected items get unselected,
336 * unselected items get selected.
337 * @see DolphinView::selectedItems()
339 void invertSelection();
341 void clearSelection();
344 * Triggers the renaming of the currently selected items, where
345 * the user must input a new name for the items.
347 void renameSelectedItems();
350 * Moves all selected items to the trash.
352 void trashSelectedItems();
355 * Deletes all selected items.
357 void deleteSelectedItems();
360 * Copies all selected items to the clipboard and marks
363 void cutSelectedItems();
365 /** Copies all selected items to the clipboard. */
366 void copySelectedItems();
368 /** Pastes the clipboard data to this view. */
372 * Pastes the clipboard data into the currently selected
373 * folder. If the current selection is not exactly one folder, no
374 * paste operation is done.
376 void pasteIntoFolder();
379 * Turns on the file preview for the all files of the current directory,
380 * if \a show is true.
381 * If the view properties should be remembered for each directory
382 * (GeneralSettings::globalViewProps() returns false), then the
383 * preview setting will be stored automatically.
385 void setPreviewsShown(bool show
);
388 * Shows all hidden files of the current directory,
389 * if \a show is true.
390 * If the view properties should be remembered for each directory
391 * (GeneralSettings::globalViewProps() returns false), then the
392 * show hidden file setting will be stored automatically.
394 void setHiddenFilesShown(bool show
);
397 * Summarizes all sorted items by their category \a categorized
399 * If the view properties should be remembered for each directory
400 * (GeneralSettings::globalViewProps() returns false), then the
401 * categorized sorting setting will be stored automatically.
403 void setCategorizedSorting(bool categorized
);
407 * Is emitted if the view has been activated by e. g. a mouse click.
412 * Is emitted if the URL of the view will be changed to \a url.
413 * After the URL has been changed the signal urlChanged() will
416 void urlAboutToBeChanged(const KUrl
& url
);
418 /** Is emitted if the URL of the view has been changed to \a url. */
419 void urlChanged(const KUrl
& url
);
422 * Is emitted when clicking on an item with the left mouse button.
424 void itemTriggered(const KFileItem
& item
);
427 * Is emitted if items have been added or deleted.
429 void itemCountChanged();
432 * Is emitted if a new tab should be opened for the URL \a url.
434 void tabRequested(const KUrl
& url
);
437 * Is emitted if the view mode (IconsView, DetailsView,
438 * PreviewsView) has been changed.
440 void modeChanged(Mode current
, Mode previous
);
442 /** Is emitted if the 'show preview' property has been changed. */
443 void previewsShownChanged(bool shown
);
445 /** Is emitted if the 'show hidden files' property has been changed. */
446 void hiddenFilesShownChanged(bool shown
);
448 /** Is emitted if the 'categorized sorting' property has been changed. */
449 void categorizedSortingChanged(bool sortCategorized
);
451 /** Is emitted if the sorting by name, size or date has been changed. */
452 void sortingChanged(DolphinView::Sorting sorting
);
454 /** Is emitted if the sort order (ascending or descending) has been changed. */
455 void sortOrderChanged(Qt::SortOrder order
);
457 /** Is emitted if the sorting of files and folders (separate with folders first or mixed) has been changed. */
458 void sortFoldersFirstChanged(bool foldersFirst
);
460 /** Is emitted if the additional information shown for this view has been changed. */
461 void additionalInfoListChanged(const QList
<DolphinView::AdditionalInfo
>& current
,
462 const QList
<DolphinView::AdditionalInfo
>& previous
);
464 /** Is emitted if the zoom level has been changed by zooming in or out. */
465 void zoomLevelChanged(int current
, int previous
);
468 * Is emitted if information of an item is requested to be shown e. g. in the panel.
469 * If item is null, no item information request is pending.
471 void requestItemInfo(const KFileItem
& item
);
474 * Is emitted whenever the selection has been changed.
476 void selectionChanged(const KFileItemList
& selection
);
479 * Is emitted if a context menu is requested for the item \a item,
480 * which is part of \a url. If the item is null, the context menu
481 * for the URL should be shown and the custom actions \a customActions
484 void requestContextMenu(const KFileItem
& item
,
486 const QList
<QAction
*>& customActions
);
489 * Is emitted if an information message with the content \a msg
492 void infoMessage(const QString
& msg
);
495 * Is emitted if an error message with the content \a msg
498 void errorMessage(const QString
& msg
);
501 * Is emitted if an "operation completed" message with the content \a msg
504 void operationCompletedMessage(const QString
& msg
);
507 * Is emitted after DolphinView::setUrl() has been invoked and
508 * the path \a url is currently loaded. If this signal is emitted,
509 * it is assured that the view contains already the correct root
510 * URL and property settings.
512 void startedPathLoading(const KUrl
& url
);
515 * Is emitted after the path triggered by DolphinView::setUrl()
518 void finishedPathLoading(const KUrl
& url
);
521 * Is emitted after DolphinView::setUrl() has been invoked and provides
522 * the information how much percent of the current path have been loaded.
524 void pathLoadingProgress(int percent
);
527 * Is emitted if the DolphinView::setUrl() is invoked but the URL is not
530 void urlIsFileError(const KUrl
& file
);
533 * Emitted when KDirLister emits redirection.
534 * Testcase: fish://localhost
536 void redirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
539 * Is emitted when the write state of the folder has been changed. The application
540 * should disable all actions like "Create New..." that depend on the write
543 void writeStateChanged(bool isFolderWritable
);
546 virtual void mouseReleaseEvent(QMouseEvent
* event
);
547 virtual void contextMenuEvent(QContextMenuEvent
* event
);
551 * Marks the view as active (DolphinView:isActive() will return true)
552 * and emits the 'activated' signal if it is not already active.
556 void slotItemClicked(int index
, Qt::MouseButton button
);
557 void slotItemExpansionToggleClicked(int index
);
558 void slotItemHovered(int index
);
559 void slotItemUnhovered(int index
);
562 * Emits the signal \a selectionChanged() with a small delay. This is
563 * because getting all file items for the selection can be an expensive
564 * operation. Fast selection changes are collected in this case and
565 * the signal is emitted only after no selection change has been done
566 * within a small delay.
568 void slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
);
571 * Is called by emitDelayedSelectionChangedSignal() and emits the
572 * signal \a selectionChanged() with all selected file items as parameter.
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 status bar to show hover information for the
612 * item \a item. If currently other items are selected,
613 * no hover information is shown.
614 * @see DolphinView::clearHoverInformation()
616 void showHoverInformation(const KFileItem
& item
);
619 * Clears the hover information shown in the status bar.
620 * @see DolphinView::showHoverInformation().
622 void clearHoverInformation();
625 * Indicates in the status bar that the delete operation
626 * of the job \a job has been finished.
628 void slotDeleteFileFinished(KJob
* job
);
631 * Invoked when the directory lister has been started the
634 void slotDirListerStarted(const KUrl
& url
);
637 * Invoked when the directory lister has completed the loading of
638 * items. Assures that pasted items and renamed items get seleced.
640 void slotDirListerCompleted();
643 * Is invoked when the KDirLister indicates refreshed items.
645 void slotRefreshItems();
648 * Observes the item with the URL \a url. As soon as the directory
649 * model indicates that the item is available, the item will
650 * get selected and it is assured that the item stays visible.
652 * @see selectAndScrollToCreatedItem()
654 void observeCreatedItem(const KUrl
& url
);
657 * Selects and scrolls to the item that got observed
658 * by observeCreatedItem().
660 void selectAndScrollToCreatedItem();
663 * Called when a redirection happens.
664 * Testcase: fish://localhost
666 void slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
669 * Applies the state that has been restored by restoreViewState()
672 void updateViewState();
674 //void slotUrlChangeRequested(const KUrl& url);
677 KFileItemModel
* fileItemModel() const;
679 void loadDirectory(const KUrl
& url
, bool reload
= false);
682 * Applies the view properties which are defined by the current URL
683 * to the DolphinView properties.
685 void applyViewProperties();
687 void applyAdditionalInfoListToView();
690 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
691 * Pastes the clipboard data into the URL \a url.
693 void pasteToUrl(const KUrl
& url
);
696 * Checks whether the current item view has the same zoom level
697 * as \a oldZoomLevel. If this is not the case, the zoom level
698 * of the controller is updated and a zoomLevelChanged() signal
701 void updateZoomLevel(int oldZoomLevel
);
704 * Returns a list of URLs for all selected items. The list is
705 * simplified, so that when the URLs are part of different tree
706 * levels, only the parent is returned.
708 KUrl::List
simplifiedSelectedUrls() const;
711 * Returns the MIME data for all selected items.
713 QMimeData
* selectionMimeData() const;
716 * Is invoked after a paste operation or a drag & drop
717 * operation and URLs from \a mimeData as selected.
718 * This allows to select all newly pasted
719 * items in restoreViewState().
721 void markPastedUrlsAsSelected(const QMimeData
* mimeData
);
724 * Helper method for DolphinView::setItemSelectionEnabled(): Returns the selection for
725 * all items of \p parent that match with the regular expression defined by \p pattern.
727 QItemSelection
childrenMatchingPattern(const QModelIndex
& parent
, const QRegExp
& pattern
) const;
730 * Updates m_isFolderWritable dependent on whether the folder represented by
731 * the current URL is writable. If the state has changed, the signal
732 * writeableStateChanged() will be emitted.
734 void updateWritableState();
736 QByteArray
sortRoleForSorting(Sorting sorting
) const;
740 bool m_tabsForFiles
: 1;
741 bool m_assureVisibleCurrentIndex
: 1;
742 bool m_isFolderWritable
: 1;
746 QList
<AdditionalInfo
> m_additionalInfoList
;
748 QVBoxLayout
* m_topLayout
;
750 DolphinDirLister
* m_dirLister
;
751 DolphinItemListContainer
* m_container
;
753 ToolTipManager
* m_toolTipManager
;
755 QTimer
* m_selectionChangedTimer
;
757 int m_currentItemIndex
;
758 QPoint m_restoredContentsPosition
;
759 KUrl m_createdItemUrl
; // URL for a new item that got created by the "Create New..." menu
760 KFileItemList m_selectedItems
; // this is used for making the View to remember selections after F5
763 friend class TestBase
;
764 friend class DolphinDetailsViewTest
;
767 /// Allow using DolphinView::Mode in QVariant
768 Q_DECLARE_METATYPE(DolphinView::Mode
)
770 #endif // DOLPHINVIEW_H