]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.h
Ported to KBookmarkManager::closestBookmark - which even solves another TODO about...
[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 <kfileiconview.h>
29 #include <kio/job.h>
30
31 #include <urlnavigator.h>
32
33 #include <QDropEvent>
34 #include <QLinkedList>
35 #include <QListView>
36 #include <QMouseEvent>
37 #include <QVBoxLayout>
38 #include <QWidget>
39
40 class DolphinController;
41 class FilterBar;
42 class KFileItemDelegate;
43 class KUrl;
44 class KDirModel;
45 class UrlNavigator;
46 class DolphinDetailsView;
47 class DolphinDirLister;
48 class DolphinIconsView;
49 class DolphinMainWindow;
50 class DolphinSortFilterProxyModel;
51 class DolphinStatusBar;
52 class QModelIndex;
53 class QPainter;
54 class QTimer;
55 class ViewProperties;
56
57 /**
58 * @short Represents a view for the directory content
59 * including the navigation bar, filter bar and status bar.
60 *
61 * View modes for icons and details are supported. Currently
62 * Dolphin allows to have up to two views inside the main window.
63 *
64 * @see DolphinIconsView
65 * @see DolphinDetailsView
66 * @see UrlNavigator
67 * @see DolphinStatusBar
68 */
69 class DolphinView : public QWidget
70 {
71 Q_OBJECT
72
73 public:
74 /**
75 * Defines the view mode for a directory. The view mode
76 * can be defined when constructing a DolphinView. The
77 * view mode is automatically updated if the directory itself
78 * defines a view mode (see class ViewProperties for details).
79 */
80 enum Mode
81 {
82 /**
83 * The directory items are shown as icons including an
84 * icon name. */
85 IconsView = 0,
86
87 /**
88 * The icon, the name and at least the size of the directory
89 * items are shown in a table. It is possible to add columns
90 * for date, group and permissions.
91 */
92 DetailsView = 1,
93 MaxModeEnum = DetailsView
94 };
95
96 /** Defines the sort order for the items of a directory. */
97 enum Sorting
98 {
99 SortByName = 0,
100 SortBySize,
101 SortByDate,
102 SortByPermissions,
103 SortByOwner,
104 SortByGroup,
105 MaxSortEnum = SortByGroup
106 };
107
108 DolphinView(DolphinMainWindow* mainwindow,
109 QWidget *parent,
110 const KUrl& url,
111 Mode mode = IconsView,
112 bool showHiddenFiles = false);
113
114 virtual ~DolphinView();
115
116 /**
117 * Sets the current active URL.
118 * The signals UrlNavigator::urlChanged() and UrlNavigator::historyChanged()
119 * are emitted.
120 */
121 void setUrl(const KUrl& url);
122
123 /** Returns the current active URL. */
124 const KUrl& url() const;
125
126 /**
127 * Returns true if the view is active and hence all actions are
128 * applied to this view.
129 */
130 bool isActive() const;
131
132 /**
133 * Changes the view mode for the current directory to \a mode.
134 * If the view properties should be remembered for each directory
135 * (GeneralSettings::globalViewProps() returns false), then the
136 * changed view mode will be be stored automatically.
137 */
138 void setMode(Mode mode);
139 Mode mode() const;
140
141 /**
142 * Turns on the file preview for the all files of the current directory,
143 * if \a show is true.
144 * If the view properties should be remembered for each directory
145 * (GeneralSettings::globalViewProps() returns false), then the
146 * preview setting will be be stored automatically.
147 */
148 void setShowPreview(bool show);
149 bool showPreview() const;
150
151 /**
152 * Shows all hidden files of the current directory,
153 * if \a show is true.
154 * If the view properties should be remembered for each directory
155 * (GeneralSettings::globalViewProps() returns false), then the
156 * show hidden file setting will be be stored automatically.
157 */
158 void setShowHiddenFiles(bool show);
159 bool showHiddenFiles() const;
160
161 /**
162 * Triggers the renaming of the currently selected items, where
163 * the user must input a new name for the items.
164 */
165 void renameSelectedItems();
166
167 /**
168 * Selects all items.
169 * @see DolphinView::selectedItems()
170 */
171 void selectAll();
172
173 /**
174 * Inverts the current selection: selected items get unselected,
175 * unselected items get selected.
176 * @see DolphinView::selectedItems()
177 */
178 void invertSelection();
179
180 /**
181 * Goes back one step in the URL history. The signals
182 * UrlNavigator::urlChanged() and UrlNavigator::historyChanged()
183 * are submitted.
184 */
185 void goBack();
186
187 /**
188 * Goes forward one step in the Url history. The signals
189 * UrlNavigator::urlChanged() and UrlNavigator::historyChanged()
190 * are submitted.
191 */
192 void goForward();
193
194 /**
195 * Goes up one step of the Url path. The signals
196 * UrlNavigator::urlChanged() and UrlNavigator::historyChanged()
197 * are submitted.
198 */
199 void goUp();
200
201 /**
202 * Goes to the home URL. The signals UrlNavigator::urlChanged()
203 * and UrlNavigator::historyChanged() are submitted.
204 */
205 void goHome();
206
207 /**
208 * Sets the URL of the navigation bar to an editable state
209 * if \a editable is true. If \a editable is false, each part of
210 * the location is presented by a button for a fast navigation.
211 */
212 void setUrlEditable(bool editable);
213
214 /** Returns true, if at least one item is selected. */
215 bool hasSelection() const;
216
217 void clearSelection();
218
219 /**
220 * Returns the selected items. The list is empty if no item has been
221 * selected.
222 * @see DolphinView::selectedUrls()
223 */
224 KFileItemList selectedItems() const;
225
226 /**
227 * Returns a list of URLs for all selected items. An empty list
228 * is returned, if no item is selected.
229 * @see DolphinView::selectedItems()
230 */
231 KUrl::List selectedUrls() const;
232
233 /**
234 * Returns the file item for the given model index \a index.
235 */
236 KFileItem* fileItem(const QModelIndex index) const;
237
238 /**
239 * Renames the filename of the source URL by the new file name.
240 * If the new file name already exists, a dialog is opened which
241 * asks the user to enter a new name.
242 */
243 void rename(const KUrl& source, const QString& newName);
244
245 DolphinStatusBar* statusBar() const;
246
247 /**
248 * Returns the x-position of the view content.
249 * The content of the view might be larger than the visible area
250 * and hence a scrolling must be done.
251 */
252 int contentsX() const;
253
254 /**
255 * Returns the y-position of the view content.
256 * The content of the view might be larger than the visible area
257 * and hence a scrolling must be done.
258 */
259 int contentsY() const;
260
261 /**
262 * Returns true, if the URL shown by the navigation bar is editable.
263 * @see UrlNavigator
264 */
265 bool isUrlEditable() const;
266
267 /** Increases the size of the current set view mode. */
268 void zoomIn();
269
270 /** Decreases the size of the current set view mode. */
271 void zoomOut();
272
273 /**
274 * Returns true, if zooming in is possible. If false is returned,
275 * the minimal zoom size is possible.
276 */
277 bool isZoomInPossible() const;
278
279 /**
280 * Returns true, if zooming out is possible. If false is returned,
281 * the maximum zoom size is possible.
282 */
283 bool isZoomOutPossible() const;
284
285 /** Sets the sort order of the items inside a directory (see DolphinView::Sorting). */
286 void setSorting(Sorting sorting);
287
288 /** Returns the sort order of the items inside a directory (see DolphinView::Sorting). */
289 Sorting sorting() const;
290
291 /** Sets the sort order (Qt::Ascending or Qt::Descending) for the items. */
292 void setSortOrder(Qt::SortOrder order);
293
294 /** Returns the current used sort order (Qt::Ascending or Qt::Descending). */
295 Qt::SortOrder sortOrder() const;
296
297 /** Sets the additional information which should be shown for the items. */
298 void setAdditionalInfo(KFileItemDelegate::AdditionalInformation info);
299
300 /** Returns the additional information which should be shown for the items. */
301 KFileItemDelegate::AdditionalInformation additionalInfo() const;
302
303 /** Refreshs the view settings by reading out the stored settings. */
304 void refreshSettings();
305
306 /** Returns the UrlNavigator of the view for read access. */
307 const UrlNavigator* urlNavigator() const { return m_urlNavigator; }
308
309 /**
310 * Triggers to request user information for the item given
311 * by the URL \a url. The signal requestItemInfo is emitted,
312 * which provides a way for widgets to get an indication to update
313 * the item information.
314 */
315 void emitRequestItemInfo(const KUrl& url);
316
317 /** Returns true, if the filter bar is visible. */
318 bool isFilterBarVisible() const;
319
320 /**
321 * Return the DolphinMainWindow this View belongs to. It is guranteed
322 * that we have one.
323 */
324 DolphinMainWindow* mainWindow() const ;
325
326 /** Reloads the current directory. */
327 void reload();
328
329 public slots:
330 /**
331 * Popups the filter bar above the status bar if \a show is true.
332 */
333 void showFilterBar(bool show);
334
335 /**
336 * Updates the number of items (= number of files + number of
337 * directories) in the statusbar. If files are selected, the number
338 * of selected files and the sum of the filesize is shown.
339 */
340 void updateStatusBar();
341
342 /**
343 * Requests the main window to set this view as active view, which
344 * means that all actions are applied to this view.
345 */
346 void requestActivation();
347
348 /**
349 * Request of a selection change. The view will do its best to accomodate
350 * the request, but it is not guaranteed that all items in \a selection
351 * will actually get selected. The view will e.g. not select items which
352 * are not in the currently displayed folder.
353 */
354 void changeSelection(const KFileItemList& selection);
355
356 signals:
357 /** Is emitted if URL of the view has been changed to \a url. */
358 void urlChanged(const KUrl& url);
359
360 /**
361 * Is emitted if the view mode (IconsView, DetailsView,
362 * PreviewsView) has been changed.
363 */
364 void modeChanged();
365
366 /** Is emitted if the 'show preview' property has been changed. */
367 void showPreviewChanged();
368
369 /** Is emitted if the 'show hidden files' property has been changed. */
370 void showHiddenFilesChanged();
371
372 /** Is emitted if the sorting by name, size or date has been changed. */
373 void sortingChanged(DolphinView::Sorting sorting);
374
375 /** Is emitted if the sort order (ascending or descending) has been changed. */
376 void sortOrderChanged(Qt::SortOrder order);
377
378 /** Is emitted if the addtional information for an item has been changed. */
379 void additionalInfoChanged(KFileItemDelegate::AdditionalInformation info);
380
381 /**
382 * Is emitted if information of an item is requested to be shown e. g. in the sidebar.
383 * It the URL is empty, no item information request is pending.
384 */
385 void requestItemInfo(const KUrl& url);
386
387 /** Is emitted if the contents has been moved to \a x, \a y. */
388 void contentsMoved(int x, int y);
389
390 /**
391 * Is emitted whenever the selection has been changed.
392 */
393 void selectionChanged(const KFileItemList& selection);
394
395 /**
396 * Is emitted whenever the filter bar has been turned show or hidden.
397 */
398 void showFilterBarChanged(bool shown);
399
400 protected:
401 /** @see QWidget::mouseReleaseEvent */
402 virtual void mouseReleaseEvent(QMouseEvent* event);
403
404 private slots:
405 void loadDirectory(const KUrl& kurl);
406 void triggerItem(const QModelIndex& index);
407 void updateProgress(int percent);
408
409 /**
410 * Updates the number of items (= number of directories + number of files)
411 * and shows this information in the statusbar.
412 */
413 void updateItemCount();
414
415 /**
416 * Generates a preview image for each file item in \a items.
417 * The current preview settings (maximum size, 'Show Preview' menu)
418 * are respected.
419 */
420 void generatePreviews(const KFileItemList& items);
421
422 /**
423 * Replaces the icon of the item \a item by the preview pixmap
424 * \a pixmap.
425 */
426 void showPreview(const KFileItem* item, const QPixmap& pixmap);
427
428 /**
429 * Restores the x- and y-position of the contents if the
430 * current view is part of the history.
431 */
432 void restoreContentsPos();
433
434 /** Shows the information \a msg inside the statusbar. */
435 void showInfoMessage(const QString& msg);
436
437 /** Shows the error message \a msg inside the statusbar. */
438 void showErrorMessage(const QString& msg);
439
440 void emitSelectionChangedSignal();
441 void closeFilterBar();
442
443 /**
444 * Filters the currently shown items by \a nameFilter. All items
445 * which contain the given filter string will be shown.
446 */
447 void changeNameFilter(const QString& nameFilter);
448
449 /**
450 * Opens the context menu on position \a pos. The position
451 * is used to check whether the context menu is related to an
452 * item or to the viewport.
453 */
454 void openContextMenu(const QPoint& pos);
455
456 /**
457 * Drops the URLs \a urls to the index \a index. \a source
458 * indicates the widget where the dragging has been started from.
459 */
460 void dropUrls(const KUrl::List& urls,
461 const QModelIndex& index,
462 QWidget* source);
463
464 /**
465 * Drops the URLs \a urls at the
466 * destination \a destination.
467 */
468 void dropUrls(const KUrl::List& urls,
469 const KUrl& destination);
470 /**
471 * Updates the view properties of the current URL to the
472 * sorting given by \a sorting.
473 */
474 void updateSorting(DolphinView::Sorting sorting);
475
476 /**
477 * Updates the view properties of the current URL to the
478 * sort order given by \a order.
479 */
480 void updateSortOrder(Qt::SortOrder order);
481
482 /**
483 * Emits the signal contentsMoved with the current coordinates
484 * of the viewport as parameters.
485 */
486 void emitContentsMoved();
487
488 /**
489 * Updates the activation state of the view by checking whether
490 * the currently active view is this view.
491 */
492 void updateActivationState();
493
494 /** Applies an item effect to all cut items of the clipboard. */
495 void updateCutItems();
496
497 private:
498 void startDirLister(const KUrl& url, bool reload = false);
499
500 /**
501 * Returns the default text of the status bar, if no item is
502 * selected.
503 */
504 QString defaultStatusBarText() const;
505
506 /**
507 * Returns the text for the status bar, if at least one item
508 * is selected.
509 */
510 QString selectionStatusBarText() const;
511
512 /**
513 * Creates a new view representing the given view mode (DolphinView::mode()).
514 * The current view will get deleted.
515 */
516 void createView();
517
518 /**
519 * Selects all items by using the selection flags \a flags. This is a helper
520 * method for the slots DolphinView::selectAll() and DolphinView::invertSelection().
521 */
522 void selectAll(QItemSelectionModel::SelectionFlags flags);
523
524 /**
525 * Returns a pointer to the currently used item view, which is either
526 * a ListView or a TreeView.
527 */
528 QAbstractItemView* itemView() const;
529
530 /**
531 * Returns true if the index is valid and represents
532 * the column KDirModel::Name.
533 */
534 bool isValidNameIndex(const QModelIndex& index) const;
535
536 /**
537 * Returns true, if the item \a item has been cut into
538 * the clipboard.
539 */
540 bool isCutItem(const KFileItem& item) const;
541
542 /** Applies an item effect to all cut items. */
543 void applyCutItemEffect();
544
545 private:
546 /**
547 * Remembers the original pixmap for an item before
548 * the cut effect is applied.
549 */
550 struct CutItem {
551 KUrl url;
552 QPixmap pixmap;
553 };
554
555 bool m_showProgress;
556 bool m_blockContentsMovedSignal;
557 Mode m_mode;
558
559 int m_iconSize;
560 int m_folderCount;
561 int m_fileCount;
562
563 DolphinMainWindow* m_mainWindow;
564 QVBoxLayout* m_topLayout;
565 UrlNavigator* m_urlNavigator;
566
567 DolphinController* m_controller;
568 DolphinIconsView* m_iconsView;
569 DolphinDetailsView* m_detailsView;
570 KFileItemDelegate* m_fileItemDelegate;
571
572 FilterBar* m_filterBar;
573 DolphinStatusBar* m_statusBar;
574
575 KDirModel* m_dirModel;
576 DolphinDirLister* m_dirLister;
577 DolphinSortFilterProxyModel* m_proxyModel;
578
579 QList<CutItem> m_cutItemsCache;
580 };
581
582 #endif // _DOLPHINVIEW_H_