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
;
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 itemTriggered(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(Mode current
, 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 slotItemClicked(int index
, Qt::MouseButton button
);
557 void slotItemExpansionToggleClicked(int index
);
560 * Emits the signal \a selectionChanged() with a small delay. This is
561 * because getting all file items for the signal can be an expensive
562 * operation. Fast selection changes are collected in this case and
563 * the signal is emitted only after no selection change has been done
564 * within a small delay.
566 void slotSelectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
);
569 * Is called by emitDelayedSelectionChangedSignal() and emits the
570 * signal \a selectionChanged() with all selected file items as parameter.
572 void emitSelectionChangedSignal();
575 * Opens the context menu on position \a pos. The position
576 * is used to check whether the context menu is related to an
577 * item or to the viewport.
579 void openContextMenu(const QPoint
& pos
, const QList
<QAction
*>& customActions
);
582 * Drops dragged URLs to the destination path \a destPath. If
583 * the URLs are dropped above an item inside the destination path,
584 * the item is indicated by \a destItem.
586 void dropUrls(const KFileItem
& destItem
,
587 const KUrl
& destPath
,
591 * Updates the view properties of the current URL to the
592 * sorting given by \a sorting.
594 void updateSorting(DolphinView::Sorting sorting
);
597 * Updates the view properties of the current URL to the
598 * sort order given by \a order.
600 void updateSortOrder(Qt::SortOrder order
);
603 * Updates the view properties of the current URL to the
604 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
606 void updateSortFoldersFirst(bool foldersFirst
);
609 * Updates the status bar to show hover information for the
610 * item \a item. If currently other items are selected,
611 * no hover information is shown.
612 * @see DolphinView::clearHoverInformation()
614 void showHoverInformation(const KFileItem
& item
);
617 * Clears the hover information shown in the status bar.
618 * @see DolphinView::showHoverInformation().
620 void clearHoverInformation();
623 * Indicates in the status bar that the delete operation
624 * of the job \a job has been finished.
626 void slotDeleteFileFinished(KJob
* job
);
629 * Invoked when the directory lister has been started the
632 void slotDirListerStarted(const KUrl
& url
);
635 * Invoked when the directory lister has completed the loading of
636 * items. Assures that pasted items and renamed items get seleced.
638 void slotDirListerCompleted();
641 * Invoked when the loading of the directory is finished.
642 * Restores the active item and the scroll position if possible.
644 void slotLoadingCompleted();
647 * Is invoked when the KDirLister indicates refreshed items.
649 void slotRefreshItems();
652 * Observes the item with the URL \a url. As soon as the directory
653 * model indicates that the item is available, the item will
654 * get selected and it is assured that the item stays visible.
656 * @see selectAndScrollToCreatedItem()
658 void observeCreatedItem(const KUrl
& url
);
661 * Selects and scrolls to the item that got observed
662 * by observeCreatedItem().
664 void selectAndScrollToCreatedItem();
667 * Called when a redirection happens.
668 * Testcase: fish://localhost
670 void slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
);
673 * Restores the contents position, if history information about the old position is available.
675 void restoreContentsPosition();
677 //void slotUrlChangeRequested(const KUrl& url);
680 KFileItemModel
* fileItemModel() const;
682 void loadDirectory(const KUrl
& url
, bool reload
= false);
685 * Applies the view properties which are defined by the current URL
686 * to the DolphinView properties.
688 void applyViewProperties();
690 void applyAdditionalInfoListToView();
693 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
694 * Pastes the clipboard data into the URL \a url.
696 void pasteToUrl(const KUrl
& url
);
699 * Checks whether the current item view has the same zoom level
700 * as \a oldZoomLevel. If this is not the case, the zoom level
701 * of the controller is updated and a zoomLevelChanged() signal
704 void updateZoomLevel(int oldZoomLevel
);
707 * Returns a list of URLs for all selected items. The list is
708 * simplified, so that when the URLs are part of different tree
709 * levels, only the parent is returned.
711 KUrl::List
simplifiedSelectedUrls() const;
714 * Returns the MIME data for all selected items.
716 QMimeData
* selectionMimeData() const;
719 * Is invoked after a paste operation or a drag & drop
720 * operation and adds the filenames of all URLs from \a mimeData to
721 * m_newFileNames. This allows to select all newly added
722 * items in slotDirListerCompleted().
724 void addNewFileNames(const QMimeData
* mimeData
);
727 * Helper method for DolphinView::setItemSelectionEnabled(): Returns the selection for
728 * all items of \p parent that match with the regular expression defined by \p pattern.
730 QItemSelection
childrenMatchingPattern(const QModelIndex
& parent
, const QRegExp
& pattern
) const;
733 * Updates m_isFolderWritable dependent on whether the folder represented by
734 * the current URL is writable. If the state has changed, the signal
735 * writeableStateChanged() will be emitted.
737 void updateWritableState();
739 QByteArray
sortRoleForSorting(Sorting sorting
) const;
743 bool m_tabsForFiles
: 1;
744 bool m_assureVisibleCurrentIndex
: 1;
745 bool m_expanderActive
: 1;
746 bool m_isFolderWritable
: 1;
750 QList
<AdditionalInfo
> m_additionalInfoList
;
752 QVBoxLayout
* m_topLayout
;
754 DolphinDirLister
* m_dirLister
;
755 DolphinItemListContainer
* m_container
;
757 QTimer
* m_selectionChangedTimer
;
759 KUrl m_activeItemUrl
;
760 QPoint m_restoredContentsPosition
;
761 KUrl m_createdItemUrl
; // URL for a new item that got created by the "Create New..." menu
762 KFileItemList m_selectedItems
; // this is used for making the View to remember selections after F5
765 * Remembers the filenames that have been added by a paste operation
766 * or a drag & drop operation. Allows to select the items in
767 * slotDirListerCompleted().
769 QSet
<QString
> m_newFileNames
;
772 friend class TestBase
;
773 friend class DolphinDetailsViewTest
;
776 /// Allow using DolphinView::Mode in QVariant
777 Q_DECLARE_METATYPE(DolphinView::Mode
)
779 #endif // DOLPHINVIEW_H