]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.h
Centralize three more actions so that they are available in DolphinPart: 'Show previe...
[dolphin.git] / src / dolphinview.h
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
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.h>
31 #include <kfileitemdelegate.h>
32 #include <konq_fileundomanager.h>
33 #include <kio/job.h>
34
35 #include <QBoxLayout>
36 #include <QKeyEvent>
37 #include <QLinkedList>
38 #include <QListView>
39 #include <QWidget>
40
41 class QActionGroup;
42 class KAction;
43 class KToggleAction;
44 class DolphinController;
45 class KDirLister;
46 class KFileItemDelegate;
47 class KUrl;
48 class DolphinModel;
49 class DolphinColumnView;
50 class DolphinDetailsView;
51 class DolphinIconsView;
52 class DolphinMainWindow;
53 class DolphinSortFilterProxyModel;
54 class QModelIndex;
55 class ViewProperties;
56 namespace KIO
57 {
58 class PreviewJob;
59 }
60
61 /**
62 * @short Represents a view for the directory content.
63 *
64 * View modes for icons, details and columns are supported. It's
65 * possible to adjust:
66 * - sort order
67 * - sort type
68 * - show hidden files
69 * - show previews
70 *
71 * @see DolphinIconsView
72 * @see DolphinDetailsView
73 * @see DolphinColumnView
74 */
75 class LIBDOLPHINPRIVATE_EXPORT DolphinView : public QWidget
76 {
77 Q_OBJECT
78
79 public:
80 /**
81 * Defines the view mode for a directory. The view mode
82 * can be defined when constructing a DolphinView. The
83 * view mode is automatically updated if the directory itself
84 * defines a view mode (see class ViewProperties for details).
85 */
86 enum Mode
87 {
88 /**
89 * The directory items are shown as icons including an
90 * icon name. */
91 IconsView = 0,
92
93 /**
94 * The icon, the name and at least the size of the directory
95 * items are shown in a table. It is possible to add columns
96 * for date, group and permissions.
97 */
98 DetailsView = 1,
99
100 /**
101 * Each folder is shown in a separate column.
102 */
103 ColumnView = 2,
104 MaxModeEnum = ColumnView
105 };
106
107 /** Defines the sort order for the items of a directory. */
108 enum Sorting
109 {
110 SortByName = 0,
111 SortBySize,
112 SortByDate,
113 SortByPermissions,
114 SortByOwner,
115 SortByGroup,
116 SortByType,
117 SortByRating,
118 SortByTags,
119 MaxSortEnum = SortByTags
120 };
121
122 /**
123 * @param parent Parent widget of the view.
124 * @param url Specifies the content which should be shown.
125 * @param dirLister Used directory lister. The lister is not owned
126 * by the view and won't get deleted.
127 * @param dolphinModel Used directory model. The model is not owned
128 * by the view and won't get deleted.
129 * @param proxyModel Used proxy model which specifies the sorting. The
130 * model is not owned by the view and won't get
131 * deleted.
132 */
133 DolphinView(QWidget* parent,
134 const KUrl& url,
135 KDirLister* dirLister,
136 DolphinModel* dolphinModel,
137 DolphinSortFilterProxyModel* proxyModel);
138
139 virtual ~DolphinView();
140
141 /**
142 * Returns the current active URL, where all actions are applied.
143 * The URL navigator is synchronized with this URL.
144 */
145 const KUrl& url() const;
146
147 /**
148 * Returns the root URL of the view, which is defined as the first
149 * visible path of DolphinView::url(). Usually the root URL is
150 * equal to DolphinView::url(), but in the case of the column view
151 * when 2 columns are shown, the root URL might be:
152 * /home/peter/Documents
153 * and DolphinView::url() might return
154 * /home/peter/Documents/Music/
155 */
156 KUrl rootUrl() const;
157
158 /**
159 * If \a active is true, the view will marked as active. The active
160 * view is defined as view where all actions are applied to.
161 */
162 void setActive(bool active);
163 bool isActive() const;
164
165 /**
166 * Changes the view mode for the current directory to \a mode.
167 * If the view properties should be remembered for each directory
168 * (GeneralSettings::globalViewProps() returns false), then the
169 * changed view mode will be stored automatically.
170 */
171 void setMode(Mode mode);
172 Mode mode() const;
173
174 /** See setShowPreview */
175 bool showPreview() const;
176
177 /** See setShowHiddenFiles */
178 bool showHiddenFiles() const;
179
180 /** See setCategorizedSorting */
181 bool categorizedSorting() const;
182
183 /**
184 * Returns true, if the categorized sorting is supported by the current
185 * used mode (see DolphinView::setMode()). Currently only DolphinView::IconsView
186 * supports categorizations. To check whether the categorized
187 * sorting is set, use DolphinView::categorizedSorting().
188 */
189 bool supportsCategorizedSorting() const;
190
191 /**
192 * Selects all items.
193 * @see DolphinView::selectedItems()
194 */
195 void selectAll();
196
197 /**
198 * Inverts the current selection: selected items get unselected,
199 * unselected items get selected.
200 * @see DolphinView::selectedItems()
201 */
202 void invertSelection();
203
204 /** Returns true, if at least one item is selected. */
205 bool hasSelection() const;
206
207 void clearSelection();
208
209 /**
210 * Returns the selected items. The list is empty if no item has been
211 * selected.
212 * @see DolphinView::selectedUrls()
213 */
214 KFileItemList selectedItems() const;
215
216 /**
217 * Returns a list of URLs for all selected items. An empty list
218 * is returned, if no item is selected.
219 * @see DolphinView::selectedItems()
220 */
221 KUrl::List selectedUrls() const;
222
223 /**
224 * Returns the file item for the given model index \a index.
225 */
226 KFileItem fileItem(const QModelIndex& index) const;
227
228 /**
229 * Sets the upper left position of the view content
230 * to (x,y). The content of the view might be larger than the visible area
231 * and hence a scrolling must be done.
232 */
233 void setContentsPosition(int x, int y);
234
235 /** Returns the upper left position of the view content. */
236 QPoint contentsPosition() const;
237
238 /** Increases the size of the current set view mode. */
239 void zoomIn();
240
241 /** Decreases the size of the current set view mode. */
242 void zoomOut();
243
244 /**
245 * Returns true, if zooming in is possible. If false is returned,
246 * the minimal zoom size is possible.
247 */
248 bool isZoomInPossible() const;
249
250 /**
251 * Returns true, if zooming out is possible. If false is returned,
252 * the maximum zoom size is possible.
253 */
254 bool isZoomOutPossible() const;
255
256 /** Sets the sort order of the items inside a directory (see DolphinView::Sorting). */
257 void setSorting(Sorting sorting);
258
259 /** Returns the sort order of the items inside a directory (see DolphinView::Sorting). */
260 Sorting sorting() const;
261
262 /** Sets the sort order (Qt::Ascending or Qt::Descending) for the items. */
263 void setSortOrder(Qt::SortOrder order);
264
265 /** Returns the current used sort order (Qt::Ascending or Qt::Descending). */
266 Qt::SortOrder sortOrder() const;
267
268 /** Sets the additional information which should be shown for the items. */
269 void setAdditionalInfo(KFileItemDelegate::InformationList info);
270
271 /** Returns the additional information which should be shown for the items. */
272 KFileItemDelegate::InformationList additionalInfo() const;
273
274 /** Reloads the current directory. */
275 void reload();
276
277 /**
278 * Refreshes the view to get synchronized with the (updated) Dolphin settings.
279 * This method only needs to get invoked if the view settings for the Icons View,
280 * Details View or Columns View have been changed.
281 */
282 void refresh();
283
284 /**
285 * Changes the directory of the view to \a url. If \a rootUrl is empty, the view
286 * properties from \a url are used for adjusting the view mode and the other properties.
287 * If \a rootUrl is not empty, the view properties from the root URL are considered
288 * instead. Specifying a root URL is only required if a view having a different root URL
289 * (e. g. the column view) should be restored. Usually using DolphinView::setUrl()
290 * is enough for changing the current URL.
291 */
292 void updateView(const KUrl& url, const KUrl& rootUrl);
293
294 /**
295 * Filters the currently shown items by \a nameFilter. All items
296 * which contain the given filter string will be shown.
297 */
298 void setNameFilter(const QString& nameFilter);
299
300 /**
301 * Calculates the number of currently shown files into
302 * \a fileCount and the number of folders into \a folderCount.
303 * It is recommend using this method instead of asking the
304 * directory lister or the model directly, as it takes
305 * filtering and hierarchical previews into account.
306 */
307 void calculateItemCount(int& fileCount, int& folderCount);
308
309 /**
310 * Returns the "switch to icons mode" action.
311 * This code is here to share it between the mainwindow and the part
312 */
313 static KToggleAction* iconsModeAction(KActionCollection* collection);
314
315 /**
316 * Returns the "switch to details mode" action.
317 * This code is here to share it between the mainwindow and the part
318 */
319 static KToggleAction* detailsModeAction(KActionCollection* collection);
320
321 /**
322 * Returns the "switch to columns mode" action.
323 * This code is here to share it between the mainwindow and the part
324 */
325 static KToggleAction* columnsModeAction(KActionCollection* collection);
326
327 /**
328 * Creates the rename action.
329 * This code is here to share it between the mainwindow and the part
330 */
331 static KAction* createRenameAction(KActionCollection* collection);
332
333 /**
334 * Creates the "move to trash" action.
335 * This code is here to share it between the mainwindow and the part
336 */
337 static KAction* createMoveToTrashAction(KActionCollection* collection);
338
339 /**
340 * Creates the delete action.
341 * This code is here to share it between the mainwindow and the part
342 */
343 static KAction* createDeleteAction(KActionCollection* collection);
344
345 /**
346 * Creates the "new directory" action.
347 * This code is here to share it between the mainwindow and the part
348 */
349 static KAction* createNewDirAction(KActionCollection* collection);
350
351 /**
352 * Creates the "sort descending" action.
353 * This code is here to share it between the mainwindow and the part
354 */
355 static KAction* createSortDescendingAction(KActionCollection* collection);
356
357 /**
358 * Creates an action group with all the "show additional information" actions in it.
359 * This code is here to share it between the mainwindow and the part
360 */
361 static QActionGroup* createAdditionalInformationActionGroup(KActionCollection* collection);
362
363 /**
364 * Creates the "show preview" action.
365 * This code is here to share it between the mainwindow and the part
366 */
367 static KAction* createShowPreviewAction(KActionCollection* collection);
368
369 /**
370 * Creates the "show in groups" action.
371 * This code is here to share it between the mainwindow and the part
372 */
373 static KAction* createShowInGroupsAction(KActionCollection* collection);
374
375 /**
376 * Creates the "show hidden files" action.
377 * This code is here to share it between the mainwindow and the part
378 */
379 static KAction* createShowHiddenFilesAction(KActionCollection* collection);
380
381 /**
382 * Updates the state of the 'Additional Information' actions in \a collection.
383 */
384 void updateAdditionalInfoActions(KActionCollection* collection);
385
386 /**
387 * Returns the action name corresponding to the current view mode
388 */
389 QString currentViewModeActionName() const;
390
391 /**
392 * Returns the state of the paste action:
393 * first is whether the action should be enabled
394 * second is the text for the action
395 */
396 QPair<bool, QString> pasteInfo() const;
397
398 public slots:
399 /**
400 * Changes the directory to \a url. If the current directory is equal to
401 * \a url, nothing will be done (use DolphinView::reload() instead).
402 */
403 void setUrl(const KUrl& url);
404
405 /**
406 * Request of a selection change. The view will do its best to accommodate
407 * the request, but it is not guaranteed that all items in \a selection
408 * will actually get selected. The view will e.g. not select items which
409 * are not in the currently displayed folder.
410 */
411 void changeSelection(const KFileItemList& selection);
412
413 /**
414 * Triggers the renaming of the currently selected items, where
415 * the user must input a new name for the items.
416 */
417 void renameSelectedItems();
418
419 /**
420 * Moves all selected items to the trash.
421 */
422 void trashSelectedItems();
423
424 /**
425 * Deletes all selected items.
426 */
427 void deleteSelectedItems();
428
429 /**
430 * Copies all selected items to the clipboard and marks
431 * the items as cutted.
432 */
433 void cutSelectedItems();
434
435 /** Copies all selected items to the clipboard. */
436 void copySelectedItems();
437
438 /** Pastes the clipboard data to this view. */
439 void paste();
440
441 /**
442 * Turns on the file preview for the all files of the current directory,
443 * if \a show is true.
444 * If the view properties should be remembered for each directory
445 * (GeneralSettings::globalViewProps() returns false), then the
446 * preview setting will be stored automatically.
447 */
448 void setShowPreview(bool show);
449
450 /**
451 * Shows all hidden files of the current directory,
452 * if \a show is true.
453 * If the view properties should be remembered for each directory
454 * (GeneralSettings::globalViewProps() returns false), then the
455 * show hidden file setting will be stored automatically.
456 */
457 void setShowHiddenFiles(bool show);
458
459 /**
460 * Summarizes all sorted items by their category \a categorized
461 * is true.
462 * If the view properties should be remembered for each directory
463 * (GeneralSettings::globalViewProps() returns false), then the
464 * categorized sorting setting will be stored automatically.
465 */
466 void setCategorizedSorting(bool categorized);
467
468 /** Switches between an ascending and descending sorting order. */
469 void toggleSortOrder();
470
471 /**
472 * Switches on or off the displaying of additional information
473 * as specified by \a action.
474 */
475 void toggleAdditionalInfo(QAction* action);
476
477 signals:
478 /**
479 * Is emitted if the view has been activated by e. g. a mouse click.
480 */
481 void activated();
482
483 /** Is emitted if URL of the view has been changed to \a url. */
484 void urlChanged(const KUrl& url);
485
486 /**
487 * Is emitted when clicking on an item
488 */
489 void itemTriggered(const KFileItem& item);
490
491 /**
492 * Is emitted if the view mode (IconsView, DetailsView,
493 * PreviewsView) has been changed.
494 */
495 void modeChanged();
496
497 /** Is emitted if the 'show preview' property has been changed. */
498 void showPreviewChanged();
499
500 /** Is emitted if the 'show hidden files' property has been changed. */
501 void showHiddenFilesChanged();
502
503 /** Is emitted if the 'categorized sorting' property has been changed. */
504 void categorizedSortingChanged();
505
506 /** Is emitted if the sorting by name, size or date has been changed. */
507 void sortingChanged(DolphinView::Sorting sorting);
508
509 /** Is emitted if the sort order (ascending or descending) has been changed. */
510 void sortOrderChanged(Qt::SortOrder order);
511
512 /** Is emitted if the additional information shown for this view has been changed. */
513 void additionalInfoChanged();
514
515 /**
516 * Is emitted if information of an item is requested to be shown e. g. in the sidebar.
517 * If item is null, no item information request is pending.
518 */
519 void requestItemInfo(const KFileItem& item);
520
521 /** Is emitted if the contents has been moved to \a x, \a y. */
522 void contentsMoved(int x, int y);
523
524 /**
525 * Is emitted whenever the selection has been changed.
526 */
527 void selectionChanged(const KFileItemList& selection);
528
529 /**
530 * Is emitted if a context menu is requested for the item \a item,
531 * which is part of \a url. If the item is 0, the context menu
532 * for the URL should be shown.
533 */
534 void requestContextMenu(const KFileItem& item, const KUrl& url);
535
536 /**
537 * Is emitted if an information message with the content \a msg
538 * should be shown.
539 */
540 void infoMessage(const QString& msg);
541
542 /**
543 * Is emitted if an error message with the content \a msg
544 * should be shown.
545 */
546 void errorMessage(const QString& msg);
547
548 /**
549 * Is emitted if an "operation completed" message with the content \a msg
550 * should be shown.
551 */
552 void operationCompletedMessage(const QString& msg);
553
554 /**
555 * Is emitted after DolphinView::setUrl() has been invoked and
556 * the path \a url is currently loaded. If this signal is emitted,
557 * it is assured that the view contains already the correct root
558 * URL and property settings.
559 */
560 void startedPathLoading(const KUrl& url);
561
562 /**
563 * Is emitted when renaming, copying, moving, linking etc.
564 * Used for feedback in the mainwindow.
565 */
566 void doingOperation(KonqFileUndoManager::CommandType type);
567
568 protected:
569 /** @see QWidget::mouseReleaseEvent */
570 virtual void mouseReleaseEvent(QMouseEvent* event);
571
572 private slots:
573 /**
574 * Marks the view as active (DolphinView:isActive() will return true)
575 * and emits the 'activated' signal if it is not already active.
576 */
577 void activate();
578
579 /**
580 * If the item \a item is a directory, then this
581 * directory will be loaded. If the item is a file, the corresponding
582 * application will get started.
583 */
584 void triggerItem(const KFileItem& index);
585
586 /**
587 * Generates a preview image for each file item in \a items.
588 * The current preview settings (maximum size, 'Show Preview' menu)
589 * are respected.
590 */
591 void generatePreviews(const KFileItemList& items);
592
593 /**
594 * Replaces the icon of the item \a item by the preview pixmap
595 * \a pixmap.
596 */
597 void replaceIcon(const KFileItem& item, const QPixmap& pixmap);
598
599 void emitSelectionChangedSignal();
600
601 /**
602 * Opens the context menu on position \a pos. The position
603 * is used to check whether the context menu is related to an
604 * item or to the viewport.
605 */
606 void openContextMenu(const QPoint& pos);
607
608 /**
609 * Drops the URLs \a urls to the destination path \a destPath. If
610 * the URLs are dropped above an item inside the destination path,
611 * the item is indicated by \a destItem.
612 */
613 void dropUrls(const KUrl::List& urls,
614 const KUrl& destPath,
615 const KFileItem& destItem);
616
617 /**
618 * Handles the dropping of URLs to the given destination.
619 * @see DolphinDropController
620 */
621 void dropUrls(const KUrl::List& urls,
622 const KUrl& destination);
623 /**
624 * Updates the view properties of the current URL to the
625 * sorting given by \a sorting.
626 */
627 void updateSorting(DolphinView::Sorting sorting);
628
629 /**
630 * Updates the view properties of the current URL to the
631 * sort order given by \a order.
632 */
633 void updateSortOrder(Qt::SortOrder order);
634
635 /**
636 * Updates the view properties of the current URL to the
637 * additional information given by \a info.
638 */
639 void updateAdditionalInfo(const KFileItemDelegate::InformationList& info);
640
641 /**
642 * Emits the signal contentsMoved with the current coordinates
643 * of the viewport as parameters.
644 */
645 void emitContentsMoved();
646
647 /** Applies an item effect to all cut items of the clipboard. */
648 void updateCutItems();
649
650 /**
651 * Updates the status bar to show hover information for the
652 * item \a item. If currently other items are selected,
653 * no hover information is shown.
654 * @see DolphinView::clearHoverInformation()
655 */
656 void showHoverInformation(const KFileItem& item);
657
658 /**
659 * Clears the hover information shown in the status bar.
660 * @see DolphinView::showHoverInformation().
661 */
662 void clearHoverInformation();
663
664 /**
665 * Indicates in the status bar that the delete operation
666 * of the job \a job has been finished.
667 */
668 void slotDeleteFileFinished(KJob* job);
669
670 /**
671 * Is invoked when the preview job has been finished and
672 * set m_previewJob to 0.
673 */
674 void slotPreviewJobFinished(KJob* job);
675
676 private:
677 void loadDirectory(const KUrl& url, bool reload = false);
678
679 /**
680 * Returns the URL where the view properties should be stored. Usually
681 * DolphinView::url() is returned, but in the case of a Column View the
682 * view properties are always stored in the directory represented by the
683 * first column. It is recommendend whenever using the ViewProperties class
684 * to use DolphinView::viewPropertiesUrl() as URL.
685 */
686 KUrl viewPropertiesUrl() const;
687
688 /**
689 * Applies the view properties which are defined by the current URL
690 * m_url to the DolphinView properties.
691 */
692 void applyViewProperties(const KUrl& url);
693
694 /**
695 * Creates a new view representing the given view mode (DolphinView::mode()).
696 * The current view will get deleted.
697 */
698 void createView();
699
700 void deleteView();
701
702 /**
703 * Returns a pointer to the currently used item view, which is either
704 * a ListView or a TreeView.
705 */
706 QAbstractItemView* itemView() const;
707
708 /**
709 * Returns true, if the item \a item has been cut into
710 * the clipboard.
711 */
712 bool isCutItem(const KFileItem& item) const;
713
714 /** Applies an item effect to all cut items. */
715 void applyCutItemEffect();
716
717 /**
718 * Returns true, if the ColumnView is activated. As the column view
719 * requires some special handling for iterating through directories,
720 * this method has been introduced for convenience.
721 */
722 bool isColumnViewActive() const
723 {
724 return m_columnView != 0;
725 }
726
727 private:
728 /**
729 * Remembers the original pixmap for an item before
730 * the cut effect is applied.
731 */
732 struct CutItem
733 {
734 KUrl url;
735 QPixmap pixmap;
736 };
737
738 bool m_active;
739 bool m_showPreview;
740 bool m_loadingDirectory;
741 bool m_storedCategorizedSorting;
742 Mode m_mode;
743
744 DolphinMainWindow* m_mainWindow;
745 QVBoxLayout* m_topLayout;
746
747 DolphinController* m_controller;
748 DolphinIconsView* m_iconsView;
749 DolphinDetailsView* m_detailsView;
750 DolphinColumnView* m_columnView;
751 KFileItemDelegate* m_fileItemDelegate;
752 QItemSelectionModel* m_selectionModel;
753
754 DolphinModel* m_dolphinModel;
755 KDirLister* m_dirLister;
756 DolphinSortFilterProxyModel* m_proxyModel;
757
758 KIO::PreviewJob* m_previewJob;
759
760 QList<CutItem> m_cutItemsCache;
761
762 KUrl m_rootUrl;
763 };
764
765 /// Allow using DolphinView::Mode in QVariant
766 Q_DECLARE_METATYPE(DolphinView::Mode)
767
768 #endif // DOLPHINVIEW_H