]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinview.h
197a41046dce694b2ddc3933383d34e611fa65b4
[dolphin.git] / src / views / dolphinview.h
1 /***************************************************************************
2 * Copyright (C) 2006-2009 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
21 #ifndef DOLPHINVIEW_H
22 #define DOLPHINVIEW_H
23
24 #include <config-nepomuk.h>
25
26 #include "libdolphin_export.h"
27
28 #include <kparts/part.h>
29 #include <KFileItem>
30 #include <KFileItemDelegate>
31 #include <kio/fileundomanager.h>
32 #include <KIO/Job>
33
34 #include <QBoxLayout>
35 #include <QKeyEvent>
36 #include <QLinkedList>
37 #include <QSet>
38 #include <QWidget>
39
40 typedef KIO::FileUndoManager::CommandType CommandType;
41
42 class DolphinDirLister;
43 class DolphinItemListContainer;
44 class KAction;
45 class KActionCollection;
46 class KFileItemModel;
47 class KItemModelBase;
48 class KUrl;
49 class ToolTipManager;
50 class VersionControlObserver;
51 class ViewProperties;
52 class QGraphicsSceneDragDropEvent;
53 class QRegExp;
54
55 /**
56 * @short Represents a view for the directory content.
57 *
58 * View modes for icons, compact and details are supported. It's
59 * possible to adjust:
60 * - sort order
61 * - sort type
62 * - show hidden files
63 * - show previews
64 * - enable grouping
65 */
66 class LIBDOLPHINPRIVATE_EXPORT DolphinView : public QWidget
67 {
68 Q_OBJECT
69
70 public:
71 /**
72 * Defines the view mode for a directory. The
73 * view mode is automatically updated if the directory itself
74 * defines a view mode (see class ViewProperties for details).
75 */
76 enum Mode
77 {
78 /**
79 * The items are shown as icons with a name-label below.
80 */
81 IconsView = 0,
82
83 /**
84 * The icon, the name and the size of the items are
85 * shown per default as a table.
86 */
87 DetailsView,
88
89 /**
90 * The items are shown as icons with the name-label aligned
91 * to the right side.
92 */
93 CompactView
94 };
95
96 /** Defines the sort order for the items of a directory. */
97 enum Sorting
98 {
99 SortByName = 0,
100 SortBySize,
101 SortByDate,
102 SortByPermissions,
103 SortByOwner,
104 SortByGroup,
105 SortByType,
106 SortByDestination,
107 SortByPath
108 };
109
110 /** Defines the additional information shown for the items of a directory. */
111 enum AdditionalInfo
112 {
113 NoInfo = 0,
114 NameInfo,
115 SizeInfo,
116 DateInfo,
117 PermissionsInfo,
118 OwnerInfo,
119 GroupInfo,
120 TypeInfo,
121 DestinationInfo,
122 PathInfo
123 };
124
125 /**
126 * @param url Specifies the content which should be shown.
127 * @param parent Parent widget of the view.
128 */
129 DolphinView(const KUrl& url, QWidget* parent);
130
131 virtual ~DolphinView();
132
133 /**
134 * Returns the current active URL, where all actions are applied.
135 * The URL navigator is synchronized with this URL.
136 */
137 KUrl url() const;
138
139 /**
140 * If \a active is true, the view will marked as active. The active
141 * view is defined as view where all actions are applied to.
142 */
143 void setActive(bool active);
144 bool isActive() const;
145
146 /**
147 * Changes the view mode for the current directory to \a mode.
148 * If the view properties should be remembered for each directory
149 * (GeneralSettings::globalViewProps() returns false), then the
150 * changed view mode will be stored automatically.
151 */
152 void setMode(Mode mode);
153 Mode mode() const;
154
155 /**
156 * Turns on the file preview for the all files of the current directory,
157 * if \a show is true.
158 * If the view properties should be remembered for each directory
159 * (GeneralSettings::globalViewProps() returns false), then the
160 * preview setting will be stored automatically.
161 */
162 void setPreviewsShown(bool show);
163 bool previewsShown() const;
164
165 /**
166 * Shows all hidden files of the current directory,
167 * if \a show is true.
168 * If the view properties should be remembered for each directory
169 * (GeneralSettings::globalViewProps() returns false), then the
170 * show hidden file setting will be stored automatically.
171 */
172 void setHiddenFilesShown(bool show);
173 bool hiddenFilesShown() const;
174
175 /**
176 * Turns on sorting by groups if \a enable is true.
177 */
178 void setGroupedSorting(bool grouped);
179 bool groupedSorting() const;
180
181 /**
182 * Returns the items of the view.
183 */
184 KFileItemList items() const;
185
186 /**
187 * Returns the selected items. The list is empty if no item has been
188 * selected.
189 */
190 KFileItemList selectedItems() const;
191
192 /**
193 * Returns the number of selected items (this is faster than
194 * invoking selectedItems().count()).
195 */
196 int selectedItemsCount() const;
197
198 /**
199 * Marks the items indicated by \p urls to get selected after the
200 * directory DolphinView::url() has been loaded. Note that nothing
201 * gets selected if no loading of a directory has been triggered
202 * by DolphinView::setUrl() or DolphinView::reload().
203 */
204 void markUrlsAsSelected(const QList<KUrl>& urls);
205
206 /**
207 * Marks the item indicated by \p url as the current item after the
208 * directory DolphinView::url() has been loaded.
209 */
210 void markUrlAsCurrent(const KUrl& url);
211
212 /**
213 * All items that match to the pattern \a pattern will get selected
214 * if \a enabled is true and deselected if \a enabled is false.
215 */
216 void setItemSelectionEnabled(const QRegExp& pattern, bool enabled);
217
218 /**
219 * Sets the zoom level to \a level. It is assured that the used
220 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
221 * ZoomLevelInfo::maximumLevel().
222 */
223 void setZoomLevel(int level);
224 int zoomLevel() const;
225
226 /**
227 * Returns true, if zooming in is possible. If false is returned,
228 * the maximum zooming level has been reached.
229 */
230 bool isZoomInPossible() const;
231
232 /**
233 * Returns true, if zooming out is possible. If false is returned,
234 * the minimum zooming level has been reached.
235 */
236 bool isZoomOutPossible() const;
237
238 /** Sets the sorting criterion (e.g., SortByName, SortBySize,...) of the items inside a directory (see DolphinView::Sorting). */
239 void setSorting(Sorting sorting);
240
241 /** Returns the sorting criterion (e.g., SortByName, SortBySize,...) of the items inside a directory (see DolphinView::Sorting). */
242 Sorting sorting() const;
243
244 /** Sets the sort order (Qt::Ascending or Qt::Descending) for the items. */
245 void setSortOrder(Qt::SortOrder order);
246
247 /** Returns the currently used sort order (Qt::Ascending or Qt::Descending). */
248 Qt::SortOrder sortOrder() const;
249
250 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
251 void setSortFoldersFirst(bool foldersFirst);
252
253 /** Returns if files and folders are sorted separately or not. */
254 bool sortFoldersFirst() const;
255
256 /** Sets the additional information which should be shown for the items. */
257 void setAdditionalInfoList(const QList<AdditionalInfo>& info);
258
259 /** Returns the additional information which should be shown for the items. */
260 QList<AdditionalInfo> additionalInfoList() const;
261
262 /** Reloads the current directory. */
263 void reload();
264
265 void stopLoading();
266
267 /**
268 * Refreshes the view to get synchronized with the (updated) Dolphin settings.
269 * This method only needs to get invoked if the view settings for the Icons View,
270 * Details View or Columns View have been changed.
271 */
272 void refresh();
273
274 /**
275 * Filters the currently shown items by \a nameFilter. All items
276 * which contain the given filter string will be shown.
277 */
278 void setNameFilter(const QString& nameFilter);
279 QString nameFilter() const;
280
281 /**
282 * Calculates the number of currently shown files into
283 * \a fileCount and the number of folders into \a folderCount.
284 * The size of all files is written into \a totalFileSize.
285 * It is recommend using this method instead of asking the
286 * directory lister or the model directly, as it takes
287 * filtering and hierarchical previews into account.
288 */
289 void calculateItemCount(int& fileCount, int& folderCount, KIO::filesize_t& totalFileSize) const;
290
291 /**
292 * Returns a textual representation of the state of the current
293 * folder or selected items, suitable for use in the status bar.
294 */
295 QString statusBarText() const;
296
297 /**
298 * Returns the version control actions that are provided for the items \p items.
299 * Usually the actions are presented in the context menu.
300 */
301 QList<QAction*> versionControlActions(const KFileItemList& items) const;
302
303 /**
304 * Returns the state of the paste action:
305 * first is whether the action should be enabled
306 * second is the text for the action
307 */
308 QPair<bool, QString> pasteInfo() const;
309
310 /**
311 * If \a tabsForFiles is true, the signal tabRequested() will also
312 * emitted also for files. Per default tabs for files is disabled
313 * and hence the signal tabRequested() will only be emitted for
314 * directories.
315 */
316 void setTabsForFilesEnabled(bool tabsForFiles);
317 bool isTabsForFilesEnabled() const;
318
319 /**
320 * Returns true if the current view allows folders to be expanded,
321 * i.e. presents a hierarchical view to the user.
322 */
323 bool itemsExpandable() const;
324
325 /**
326 * Restores the view state (current item, contents position, details view expansion state)
327 */
328 void restoreState(QDataStream& stream);
329
330 /**
331 * Saves the view state (current item, contents position, details view expansion state)
332 */
333 void saveState(QDataStream& stream);
334
335 /** Returns true, if at least one item is selected. */
336 bool hasSelection() const;
337
338 /**
339 * Returns the root item which represents the current URL.
340 */
341 KFileItem rootItem() const;
342
343 public slots:
344 /**
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).
347 */
348 void setUrl(const KUrl& url);
349
350 /**
351 * Selects all items.
352 * @see DolphinView::selectedItems()
353 */
354 void selectAll();
355
356 /**
357 * Inverts the current selection: selected items get unselected,
358 * unselected items get selected.
359 * @see DolphinView::selectedItems()
360 */
361 void invertSelection();
362
363 void clearSelection();
364
365 /**
366 * Triggers the renaming of the currently selected items, where
367 * the user must input a new name for the items.
368 */
369 void renameSelectedItems();
370
371 /**
372 * Moves all selected items to the trash.
373 */
374 void trashSelectedItems();
375
376 /**
377 * Deletes all selected items.
378 */
379 void deleteSelectedItems();
380
381 /**
382 * Copies all selected items to the clipboard and marks
383 * the items as cut.
384 */
385 void cutSelectedItems();
386
387 /** Copies all selected items to the clipboard. */
388 void copySelectedItems();
389
390 /** Pastes the clipboard data to this view. */
391 void paste();
392
393 /**
394 * Pastes the clipboard data into the currently selected
395 * folder. If the current selection is not exactly one folder, no
396 * paste operation is done.
397 */
398 void pasteIntoFolder();
399
400 /** Activates the view if the item list container gets focus. */
401 virtual bool eventFilter(QObject* watched, QEvent* event);
402
403 signals:
404 /**
405 * Is emitted if the view has been activated by e. g. a mouse click.
406 */
407 void activated();
408
409 /**
410 * Is emitted if the URL of the view will be changed to \a url.
411 * After the URL has been changed the signal urlChanged() will
412 * be emitted.
413 */
414 void urlAboutToBeChanged(const KUrl& url);
415
416 /** Is emitted if the URL of the view has been changed to \a url. */
417 void urlChanged(const KUrl& url);
418
419 /**
420 * Is emitted when clicking on an item with the left mouse button.
421 */
422 void itemActivated(const KFileItem& item);
423
424 /**
425 * Is emitted if items have been added or deleted.
426 */
427 void itemCountChanged();
428
429 /**
430 * Is emitted if a new tab should be opened for the URL \a url.
431 */
432 void tabRequested(const KUrl& url);
433
434 /**
435 * Is emitted if the view mode (IconsView, DetailsView,
436 * PreviewsView) has been changed.
437 */
438 void modeChanged(DolphinView::Mode current, DolphinView::Mode previous);
439
440 /** Is emitted if the 'show preview' property has been changed. */
441 void previewsShownChanged(bool shown);
442
443 /** Is emitted if the 'show hidden files' property has been changed. */
444 void hiddenFilesShownChanged(bool shown);
445
446 /** Is emitted if the 'grouped sorting' property has been changed. */
447 void groupedSortingChanged(bool groupedSorting);
448
449 /** Is emitted if the sorting by name, size or date has been changed. */
450 void sortingChanged(DolphinView::Sorting sorting);
451
452 /** Is emitted if the sort order (ascending or descending) has been changed. */
453 void sortOrderChanged(Qt::SortOrder order);
454
455 /** Is emitted if the sorting of files and folders (separate with folders first or mixed) has been changed. */
456 void sortFoldersFirstChanged(bool foldersFirst);
457
458 /** Is emitted if the additional information shown for this view has been changed. */
459 void additionalInfoListChanged(const QList<DolphinView::AdditionalInfo>& current,
460 const QList<DolphinView::AdditionalInfo>& previous);
461
462 /** Is emitted if the zoom level has been changed by zooming in or out. */
463 void zoomLevelChanged(int current, int previous);
464
465 /**
466 * Is emitted if information of an item is requested to be shown e. g. in the panel.
467 * If item is null, no item information request is pending.
468 */
469 void requestItemInfo(const KFileItem& item);
470
471 /**
472 * Is emitted whenever the selection has been changed.
473 */
474 void selectionChanged(const KFileItemList& selection);
475
476 /**
477 * Is emitted if a context menu is requested for the item \a item,
478 * which is part of \a url. If the item is null, the context menu
479 * for the URL should be shown and the custom actions \a customActions
480 * will be added.
481 */
482 void requestContextMenu(const QPoint& pos,
483 const KFileItem& item,
484 const KUrl& url,
485 const QList<QAction*>& customActions);
486
487 /**
488 * Is emitted if an information message with the content \a msg
489 * should be shown.
490 */
491 void infoMessage(const QString& msg);
492
493 /**
494 * Is emitted if an error message with the content \a msg
495 * should be shown.
496 */
497 void errorMessage(const QString& msg);
498
499 /**
500 * Is emitted if an "operation completed" message with the content \a msg
501 * should be shown.
502 */
503 void operationCompletedMessage(const QString& msg);
504
505 /**
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.
510 */
511 void startedPathLoading(const KUrl& url);
512
513 /**
514 * Is emitted after the path triggered by DolphinView::setUrl()
515 * has been loaded.
516 */
517 void finishedPathLoading(const KUrl& url);
518
519 /**
520 * Is emitted after DolphinView::setUrl() has been invoked and provides
521 * the information how much percent of the current path have been loaded.
522 */
523 void pathLoadingProgress(int percent);
524
525 /**
526 * Is emitted if the DolphinView::setUrl() is invoked but the URL is not
527 * a directory.
528 */
529 void urlIsFileError(const KUrl& file);
530
531 /**
532 * Emitted when KDirLister emits redirection.
533 * Testcase: fish://localhost
534 */
535 void redirection(const KUrl& oldUrl, const KUrl& newUrl);
536
537 /**
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
540 * state.
541 */
542 void writeStateChanged(bool isFolderWritable);
543
544 protected:
545 /** Changes the zoom level if Control is pressed during a wheel event. */
546 virtual void wheelEvent(QWheelEvent* event);
547
548 private slots:
549 /**
550 * Marks the view as active (DolphinView:isActive() will return true)
551 * and emits the 'activated' signal if it is not already active.
552 */
553 void activate();
554
555 void slotItemActivated(int index);
556 void slotItemsActivated(const QSet<int>& indexes);
557 void slotItemMiddleClicked(int index);
558 void slotItemContextMenuRequested(int index, const QPointF& pos);
559 void slotViewContextMenuRequested(const QPointF& pos);
560 void slotHeaderContextMenuRequested(const QPointF& pos);
561 void slotItemHovered(int index);
562 void slotItemUnhovered(int index);
563 void slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event);
564 void slotModelChanged(KItemModelBase* current, KItemModelBase* previous);
565
566 /**
567 * Emits the signal \a selectionChanged() with a small delay. This is
568 * because getting all file items for the selection can be an expensive
569 * operation. Fast selection changes are collected in this case and
570 * the signal is emitted only after no selection change has been done
571 * within a small delay.
572 */
573 void slotSelectionChanged(const QSet<int>& current, const QSet<int>& previous);
574
575 /**
576 * Is called by emitDelayedSelectionChangedSignal() and emits the
577 * signal \a selectionChanged() with all selected file items as parameter.
578 */
579 void emitSelectionChangedSignal();
580
581 /**
582 * Updates the view properties of the current URL to the
583 * sorting given by \a sorting.
584 */
585 void updateSorting(DolphinView::Sorting sorting);
586
587 /**
588 * Updates the view properties of the current URL to the
589 * sort order given by \a order.
590 */
591 void updateSortOrder(Qt::SortOrder order);
592
593 /**
594 * Updates the view properties of the current URL to the
595 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
596 */
597 void updateSortFoldersFirst(bool foldersFirst);
598
599 /**
600 * Updates the status bar to show hover information for the
601 * item \a item. If currently other items are selected,
602 * no hover information is shown.
603 * @see DolphinView::clearHoverInformation()
604 */
605 void showHoverInformation(const KFileItem& item);
606
607 /**
608 * Clears the hover information shown in the status bar.
609 * @see DolphinView::showHoverInformation().
610 */
611 void clearHoverInformation();
612
613 /**
614 * Indicates in the status bar that the delete operation
615 * of the job \a job has been finished.
616 */
617 void slotDeleteFileFinished(KJob* job);
618
619 /**
620 * Invoked when the directory lister has been started the
621 * loading of \a url.
622 */
623 void slotDirListerStarted(const KUrl& url);
624
625 /**
626 * Invoked when the file item model indicates that the directory lister has completed the loading
627 * of items, and that expanded folders have been restored (if the view mode is 'Details', and the
628 * view state is restored after navigating back or forward in history). Assures that pasted items
629 * and renamed items get seleced.
630 */
631 void slotLoadingCompleted();
632
633 /**
634 * Is invoked when the KDirLister indicates refreshed items.
635 */
636 void slotRefreshItems();
637
638 /**
639 * Is invoked when the sort order has been changed by the user by clicking
640 * on a header item. The view properties of the directory will get updated.
641 */
642 void slotSortOrderChangedByHeader(Qt::SortOrder current, Qt::SortOrder previous);
643
644 /**
645 * Is invoked when the sort role has been changed by the user by clicking
646 * on a header item. The view properties of the directory will get updated.
647 */
648 void slotSortRoleChangedByHeader(const QByteArray& current, const QByteArray& previous);
649
650 /**
651 * Observes the item with the URL \a url. As soon as the directory
652 * model indicates that the item is available, the item will
653 * get selected and it is assured that the item stays visible.
654 *
655 * @see selectAndScrollToCreatedItem()
656 */
657 void observeCreatedItem(const KUrl& url);
658
659 /**
660 * Selects and scrolls to the item that got observed
661 * by observeCreatedItem().
662 */
663 void selectAndScrollToCreatedItem();
664
665 /**
666 * Called when a redirection happens.
667 * Testcase: fish://localhost
668 */
669 void slotRedirection(const KUrl& oldUrl, const KUrl& newUrl);
670
671 /**
672 * Applies the state that has been restored by restoreViewState()
673 * to the view.
674 */
675 void updateViewState();
676
677 void hideToolTip();
678
679 //void slotUrlChangeRequested(const KUrl& url);
680
681 private:
682 KFileItemModel* fileItemModel() const;
683
684 void loadDirectory(const KUrl& url, bool reload = false);
685
686 /**
687 * Applies the view properties which are defined by the current URL
688 * to the DolphinView properties.
689 */
690 void applyViewProperties();
691
692 void applyAdditionalInfoListToView();
693
694 /**
695 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
696 * Pastes the clipboard data into the URL \a url.
697 */
698 void pasteToUrl(const KUrl& url);
699
700 /**
701 * Returns a list of URLs for all selected items. The list is
702 * simplified, so that when the URLs are part of different tree
703 * levels, only the parent is returned.
704 */
705 KUrl::List simplifiedSelectedUrls() const;
706
707 /**
708 * Returns the MIME data for all selected items.
709 */
710 QMimeData* selectionMimeData() const;
711
712 /**
713 * Is invoked after a paste operation or a drag & drop
714 * operation and URLs from \a mimeData as selected.
715 * This allows to select all newly pasted
716 * items in restoreViewState().
717 */
718 void markPastedUrlsAsSelected(const QMimeData* mimeData);
719
720 /**
721 * Updates m_isFolderWritable dependent on whether the folder represented by
722 * the current URL is writable. If the state has changed, the signal
723 * writeableStateChanged() will be emitted.
724 */
725 void updateWritableState();
726
727 QByteArray sortRoleForSorting(Sorting sorting) const;
728 Sorting sortingForSortRole(const QByteArray& sortRole) const;
729
730 /**
731 * Returns the text for the filesize by converting it to the best fitting
732 * unit.
733 */
734 static QString fileSizeText(KIO::filesize_t fileSize);
735
736 private:
737 bool m_active : 1;
738 bool m_tabsForFiles : 1;
739 bool m_assureVisibleCurrentIndex : 1;
740 bool m_isFolderWritable : 1;
741
742 KUrl m_url;
743 Mode m_mode;
744 QList<AdditionalInfo> m_additionalInfoList;
745
746 QVBoxLayout* m_topLayout;
747
748 DolphinDirLister* m_dirLister;
749 DolphinItemListContainer* m_container;
750
751 ToolTipManager* m_toolTipManager;
752
753 QTimer* m_selectionChangedTimer;
754
755 KUrl m_currentItemUrl; // Used for making the view to remember the current URL after F5
756 QPoint m_restoredContentsPosition;
757 KUrl m_createdItemUrl; // URL for a new item that got created by the "Create New..." menu
758
759 QList<KUrl> m_selectedUrls; // Used for making the view to remember selections after F5
760
761 VersionControlObserver* m_versionControlObserver;
762
763 // For unit tests
764 friend class TestBase;
765 friend class DolphinDetailsViewTest;
766 };
767
768 /// Allow using DolphinView::Mode in QVariant
769 Q_DECLARE_METATYPE(DolphinView::Mode)
770
771 #endif // DOLPHINVIEW_H