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