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