]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinview.h
Renamed some signals for consistency with KItemViews classes
[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
22 #ifndef DOLPHINVIEW_H
23 #define DOLPHINVIEW_H
24
25 #include <config-nepomuk.h>
26
27 #include "libdolphin_export.h"
28
29 #include <kparts/part.h>
30 #include <KFileItem>
31 #include <KFileItemDelegate>
32 #include <kio/fileundomanager.h>
33 #include <KIO/Job>
34
35 #include <QBoxLayout>
36 #include <QKeyEvent>
37 #include <QLinkedList>
38 #include <QSet>
39 #include <QWidget>
40
41 typedef KIO::FileUndoManager::CommandType CommandType;
42
43 class DolphinDirLister;
44 class DolphinItemListContainer;
45 class KAction;
46 class KActionCollection;
47 class KFileItemModel;
48 class KUrl;
49 class ToolTipManager;
50 class ViewProperties;
51 class QRegExp;
52
53 /**
54 * @short Represents a view for the directory content.
55 *
56 * View modes for icons, compact and details are supported. It's
57 * possible to adjust:
58 * - sort order
59 * - sort type
60 * - show hidden files
61 * - show previews
62 * - enable grouping
63 */
64 class LIBDOLPHINPRIVATE_EXPORT DolphinView : public QWidget
65 {
66 Q_OBJECT
67
68 public:
69 /**
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).
73 */
74 enum Mode
75 {
76 /**
77 * The items are shown as icons with a name-label below.
78 */
79 IconsView = 0,
80
81 /**
82 * The icon, the name and the size of the items are
83 * shown per default as a table.
84 */
85 DetailsView,
86
87 /**
88 * The items are shown as icons with the name-label aligned
89 * to the right side.
90 */
91 CompactView
92 };
93
94 /** Defines the sort order for the items of a directory. */
95 enum Sorting
96 {
97 SortByName = 0,
98 SortBySize,
99 SortByDate,
100 SortByPermissions,
101 SortByOwner,
102 SortByGroup,
103 SortByType,
104 SortByDestination,
105 SortByPath
106 };
107
108 /** Defines the additional information shown for the items of a directory. */
109 enum AdditionalInfo
110 {
111 NoInfo = 0,
112 NameInfo,
113 SizeInfo,
114 DateInfo,
115 PermissionsInfo,
116 OwnerInfo,
117 GroupInfo,
118 TypeInfo,
119 DestinationInfo,
120 PathInfo
121 };
122
123 /**
124 * @param url Specifies the content which should be shown.
125 * @param parent Parent widget of the view.
126 */
127 DolphinView(const KUrl& url, QWidget* parent);
128
129 virtual ~DolphinView();
130
131 /**
132 * Returns the current active URL, where all actions are applied.
133 * The URL navigator is synchronized with this URL.
134 */
135 KUrl url() const;
136
137 /**
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.
140 */
141 void setActive(bool active);
142 bool isActive() const;
143
144 /**
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.
149 */
150 void setMode(Mode mode);
151 Mode mode() const;
152
153 /** See setPreviewsShown */
154 bool previewsShown() const;
155
156 /** See setShowHiddenFiles */
157 bool hiddenFilesShown() const;
158
159 /** See setCategorizedSorting */
160 bool categorizedSorting() const;
161
162 /**
163 * Returns the items of the view.
164 */
165 KFileItemList items() const;
166
167 /**
168 * Returns the selected items. The list is empty if no item has been
169 * selected.
170 */
171 KFileItemList selectedItems() const;
172
173 /**
174 * Returns the number of selected items (this is faster than
175 * invoking selectedItems().count()).
176 */
177 int selectedItemsCount() const;
178
179 /**
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().
184 */
185 void markUrlsAsSelected(const QList<KUrl>& urls);
186
187 /**
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.
190 */
191 void setItemSelectionEnabled(const QRegExp& pattern, bool enabled);
192
193 /**
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().
197 */
198 void setZoomLevel(int level);
199 int zoomLevel() const;
200
201 /**
202 * Returns true, if zooming in is possible. If false is returned,
203 * the maximum zooming level has been reached.
204 */
205 bool isZoomInPossible() const;
206
207 /**
208 * Returns true, if zooming out is possible. If false is returned,
209 * the minimum zooming level has been reached.
210 */
211 bool isZoomOutPossible() const;
212
213 /** Sets the sorting criterion (e.g., SortByName, SortBySize,...) of the items inside a directory (see DolphinView::Sorting). */
214 void setSorting(Sorting sorting);
215
216 /** Returns the sorting criterion (e.g., SortByName, SortBySize,...) of the items inside a directory (see DolphinView::Sorting). */
217 Sorting sorting() const;
218
219 /** Sets the sort order (Qt::Ascending or Qt::Descending) for the items. */
220 void setSortOrder(Qt::SortOrder order);
221
222 /** Returns the currently used sort order (Qt::Ascending or Qt::Descending). */
223 Qt::SortOrder sortOrder() const;
224
225 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
226 void setSortFoldersFirst(bool foldersFirst);
227
228 /** Returns if files and folders are sorted separately or not. */
229 bool sortFoldersFirst() const;
230
231 /** Sets the additional information which should be shown for the items. */
232 void setAdditionalInfoList(const QList<AdditionalInfo>& info);
233
234 /** Returns the additional information which should be shown for the items. */
235 QList<AdditionalInfo> additionalInfoList() const;
236
237 /** Reloads the current directory. */
238 void reload();
239
240 void stopLoading();
241
242 /**
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.
246 */
247 void refresh();
248
249 /**
250 * Filters the currently shown items by \a nameFilter. All items
251 * which contain the given filter string will be shown.
252 */
253 void setNameFilter(const QString& nameFilter);
254 QString nameFilter() const;
255
256 /**
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.
263 */
264 void calculateItemCount(int& fileCount, int& folderCount, KIO::filesize_t& totalFileSize) const;
265
266 /**
267 * Returns a textual representation of the state of the current
268 * folder or selected items, suitable for use in the status bar.
269 */
270 QString statusBarText() const;
271
272 /**
273 * Returns the version control actions that are provided for the items \p items.
274 * Usually the actions are presented in the context menu.
275 */
276 QList<QAction*> versionControlActions(const KFileItemList& items) const;
277
278 /**
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
282 */
283 QPair<bool, QString> pasteInfo() const;
284
285 /**
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
289 * directories.
290 */
291 void setTabsForFilesEnabled(bool tabsForFiles);
292 bool isTabsForFilesEnabled() const;
293
294 /**
295 * Returns true if the current view allows folders to be expanded,
296 * i.e. presents a hierarchical view to the user.
297 */
298 bool itemsExpandable() const;
299
300 /**
301 * Restores the view state (current item, contents position, details view expansion state)
302 */
303 void restoreState(QDataStream& stream);
304
305 /**
306 * Saves the view state (current item, contents position, details view expansion state)
307 */
308 void saveState(QDataStream& stream);
309
310 /** Returns true, if at least one item is selected. */
311 bool hasSelection() const;
312
313 /**
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.
317 */
318 KFileItem rootItem() const;
319
320 public slots:
321 /**
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).
324 */
325 void setUrl(const KUrl& url);
326
327 /**
328 * Selects all items.
329 * @see DolphinView::selectedItems()
330 */
331 void selectAll();
332
333 /**
334 * Inverts the current selection: selected items get unselected,
335 * unselected items get selected.
336 * @see DolphinView::selectedItems()
337 */
338 void invertSelection();
339
340 void clearSelection();
341
342 /**
343 * Triggers the renaming of the currently selected items, where
344 * the user must input a new name for the items.
345 */
346 void renameSelectedItems();
347
348 /**
349 * Moves all selected items to the trash.
350 */
351 void trashSelectedItems();
352
353 /**
354 * Deletes all selected items.
355 */
356 void deleteSelectedItems();
357
358 /**
359 * Copies all selected items to the clipboard and marks
360 * the items as cut.
361 */
362 void cutSelectedItems();
363
364 /** Copies all selected items to the clipboard. */
365 void copySelectedItems();
366
367 /** Pastes the clipboard data to this view. */
368 void paste();
369
370 /**
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.
374 */
375 void pasteIntoFolder();
376
377 /**
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.
383 */
384 void setPreviewsShown(bool show);
385
386 /**
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.
392 */
393 void setHiddenFilesShown(bool show);
394
395 /**
396 * Summarizes all sorted items by their category \a categorized
397 * is true.
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.
401 */
402 void setCategorizedSorting(bool categorized);
403
404 signals:
405 /**
406 * Is emitted if the view has been activated by e. g. a mouse click.
407 */
408 void activated();
409
410 /**
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
413 * be emitted.
414 */
415 void urlAboutToBeChanged(const KUrl& url);
416
417 /** Is emitted if the URL of the view has been changed to \a url. */
418 void urlChanged(const KUrl& url);
419
420 /**
421 * Is emitted when clicking on an item with the left mouse button.
422 */
423 void itemActivated(const KFileItem& item);
424
425 /**
426 * Is emitted if items have been added or deleted.
427 */
428 void itemCountChanged();
429
430 /**
431 * Is emitted if a new tab should be opened for the URL \a url.
432 */
433 void tabRequested(const KUrl& url);
434
435 /**
436 * Is emitted if the view mode (IconsView, DetailsView,
437 * PreviewsView) has been changed.
438 */
439 void modeChanged(DolphinView::Mode current, DolphinView::Mode previous);
440
441 /** Is emitted if the 'show preview' property has been changed. */
442 void previewsShownChanged(bool shown);
443
444 /** Is emitted if the 'show hidden files' property has been changed. */
445 void hiddenFilesShownChanged(bool shown);
446
447 /** Is emitted if the 'categorized sorting' property has been changed. */
448 void categorizedSortingChanged(bool sortCategorized);
449
450 /** Is emitted if the sorting by name, size or date has been changed. */
451 void sortingChanged(DolphinView::Sorting sorting);
452
453 /** Is emitted if the sort order (ascending or descending) has been changed. */
454 void sortOrderChanged(Qt::SortOrder order);
455
456 /** Is emitted if the sorting of files and folders (separate with folders first or mixed) has been changed. */
457 void sortFoldersFirstChanged(bool foldersFirst);
458
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);
462
463 /** Is emitted if the zoom level has been changed by zooming in or out. */
464 void zoomLevelChanged(int current, int previous);
465
466 /**
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.
469 */
470 void requestItemInfo(const KFileItem& item);
471
472 /**
473 * Is emitted whenever the selection has been changed.
474 */
475 void selectionChanged(const KFileItemList& selection);
476
477 /**
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
481 * will be added.
482 */
483 void requestContextMenu(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 virtual void mouseReleaseEvent(QMouseEvent* event);
546 virtual void contextMenuEvent(QContextMenuEvent* 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 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);
561
562 /**
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.
568 */
569 void slotSelectionChanged(const QSet<int>& current, const QSet<int>& previous);
570
571 /**
572 * Is called by emitDelayedSelectionChangedSignal() and emits the
573 * signal \a selectionChanged() with all selected file items as parameter.
574 */
575 void emitSelectionChangedSignal();
576
577 /**
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.
581 */
582 void openContextMenu(const QPoint& pos, const QList<QAction*>& customActions);
583
584 /**
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.
588 */
589 void dropUrls(const KFileItem& destItem,
590 const KUrl& destPath,
591 QDropEvent* event);
592
593 /**
594 * Updates the view properties of the current URL to the
595 * sorting given by \a sorting.
596 */
597 void updateSorting(DolphinView::Sorting sorting);
598
599 /**
600 * Updates the view properties of the current URL to the
601 * sort order given by \a order.
602 */
603 void updateSortOrder(Qt::SortOrder order);
604
605 /**
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.
608 */
609 void updateSortFoldersFirst(bool foldersFirst);
610
611 /**
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()
616 */
617 void showHoverInformation(const KFileItem& item);
618
619 /**
620 * Clears the hover information shown in the status bar.
621 * @see DolphinView::showHoverInformation().
622 */
623 void clearHoverInformation();
624
625 /**
626 * Indicates in the status bar that the delete operation
627 * of the job \a job has been finished.
628 */
629 void slotDeleteFileFinished(KJob* job);
630
631 /**
632 * Invoked when the directory lister has been started the
633 * loading of \a url.
634 */
635 void slotDirListerStarted(const KUrl& url);
636
637 /**
638 * Invoked when the directory lister has completed the loading of
639 * items. Assures that pasted items and renamed items get seleced.
640 */
641 void slotDirListerCompleted();
642
643 /**
644 * Is invoked when the KDirLister indicates refreshed items.
645 */
646 void slotRefreshItems();
647
648 /**
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.
652 *
653 * @see selectAndScrollToCreatedItem()
654 */
655 void observeCreatedItem(const KUrl& url);
656
657 /**
658 * Selects and scrolls to the item that got observed
659 * by observeCreatedItem().
660 */
661 void selectAndScrollToCreatedItem();
662
663 /**
664 * Called when a redirection happens.
665 * Testcase: fish://localhost
666 */
667 void slotRedirection(const KUrl& oldUrl, const KUrl& newUrl);
668
669 /**
670 * Applies the state that has been restored by restoreViewState()
671 * to the view.
672 */
673 void updateViewState();
674
675 //void slotUrlChangeRequested(const KUrl& url);
676
677 private:
678 KFileItemModel* fileItemModel() const;
679
680 void loadDirectory(const KUrl& url, bool reload = false);
681
682 /**
683 * Applies the view properties which are defined by the current URL
684 * to the DolphinView properties.
685 */
686 void applyViewProperties();
687
688 void applyAdditionalInfoListToView();
689
690 /**
691 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
692 * Pastes the clipboard data into the URL \a url.
693 */
694 void pasteToUrl(const KUrl& url);
695
696 /**
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
700 * is emitted.
701 */
702 void updateZoomLevel(int oldZoomLevel);
703
704 /**
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.
708 */
709 KUrl::List simplifiedSelectedUrls() const;
710
711 /**
712 * Returns the MIME data for all selected items.
713 */
714 QMimeData* selectionMimeData() const;
715
716 /**
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().
721 */
722 void markPastedUrlsAsSelected(const QMimeData* mimeData);
723
724 /**
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.
728 */
729 void updateWritableState();
730
731 QByteArray sortRoleForSorting(Sorting sorting) const;
732
733 private:
734 bool m_active : 1;
735 bool m_tabsForFiles : 1;
736 bool m_assureVisibleCurrentIndex : 1;
737 bool m_isFolderWritable : 1;
738
739 KUrl m_url;
740 Mode m_mode;
741 QList<AdditionalInfo> m_additionalInfoList;
742
743 QVBoxLayout* m_topLayout;
744
745 DolphinDirLister* m_dirLister;
746 DolphinItemListContainer* m_container;
747
748 ToolTipManager* m_toolTipManager;
749
750 QTimer* m_selectionChangedTimer;
751
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
756
757 // For unit tests
758 friend class TestBase;
759 friend class DolphinDetailsViewTest;
760 };
761
762 /// Allow using DolphinView::Mode in QVariant
763 Q_DECLARE_METATYPE(DolphinView::Mode)
764
765 #endif // DOLPHINVIEW_H