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