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