]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmainwindow.h
Fix selection rect after porting from QFontMetrics::width()
[dolphin.git] / src / dolphinmainwindow.h
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2006 by Stefan Monov <logixoul@gmail.com> *
4 * Copyright (C) 2006 by Cvetoslav Ludmiloff <ludmiloff@gmail.com> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 #ifndef DOLPHIN_MAINWINDOW_H
23 #define DOLPHIN_MAINWINDOW_H
24
25 #include "dolphintabwidget.h"
26 #include <config-baloo.h>
27 #include <kio/fileundomanager.h>
28 #include <ksortablelist.h>
29 #include <kxmlguiwindow.h>
30
31 #include <QIcon>
32 #include <QList>
33 #include <QMenu>
34 #include <QPointer>
35 #include <QUrl>
36 #include <QVector>
37
38 typedef KIO::FileUndoManager::CommandType CommandType;
39
40 class DolphinBookmarkHandler;
41 class DolphinViewActionHandler;
42 class DolphinSettingsDialog;
43 class DolphinViewContainer;
44 class DolphinRemoteEncoding;
45 class DolphinTabWidget;
46 class KFileItem;
47 class KFileItemList;
48 class KJob;
49 class KNewFileMenu;
50 class KHelpMenu;
51 class KToolBarPopupAction;
52 class QToolButton;
53 class QIcon;
54 class PlacesPanel;
55 class TerminalPanel;
56
57 /**
58 * @short Main window for Dolphin.
59 *
60 * Handles the menus, toolbars and Dolphin views.
61 */
62 class DolphinMainWindow: public KXmlGuiWindow
63 {
64 Q_OBJECT
65 Q_CLASSINFO("D-Bus Interface", "org.kde.dolphin.MainWindow")
66
67 public:
68 DolphinMainWindow();
69 ~DolphinMainWindow() override;
70
71 /**
72 * Returns the currently active view.
73 * All menu actions are applied to this view. When
74 * having a split view setup, the nonactive view
75 * is usually shown in darker colors.
76 */
77 DolphinViewContainer* activeViewContainer() const;
78
79 /**
80 * Returns view container for all tabs
81 */
82 QVector<DolphinViewContainer*> viewContainers() const;
83
84 /**
85 * Opens each directory in \p dirs in a separate tab. If \a splitView is set,
86 * 2 directories are collected within one tab.
87 * \pre \a dirs must contain at least one url.
88 */
89 void openDirectories(const QList<QUrl> &dirs, bool splitView);
90
91 /**
92 * Opens the directories which contain the files \p files and selects all files.
93 * If \a splitView is set, 2 directories are collected within one tab.
94 * \pre \a files must contain at least one url.
95 */
96 void openFiles(const QList<QUrl>& files, bool splitView);
97
98 /**
99 * Returns the 'Create New...' sub menu which also can be shared
100 * with other menus (e. g. a context menu).
101 */
102 KNewFileMenu* newFileMenu() const;
103
104 void setTabsToHomeIfMountPathOpen(const QString& mountPath);
105
106 public slots:
107 /**
108 * Opens each directory in \p dirs in a separate tab. If \a splitView is set,
109 * 2 directories are collected within one tab.
110 * \pre \a dirs must contain at least one url.
111 *
112 * @note this function is overloaded so that it is callable via DBus.
113 */
114 void openDirectories(const QStringList &dirs, bool splitView);
115
116 /**
117 * Opens the directories which contain the files \p files and selects all files.
118 * If \a splitView is set, 2 directories are collected within one tab.
119 * \pre \a files must contain at least one url.
120 *
121 * @note this is overloaded so that this function is callable via DBus.
122 */
123 void openFiles(const QStringList &files, bool splitView);
124
125 /**
126 * Tries to raise/activate the Dolphin window.
127 */
128 void activateWindow();
129
130 /**
131 * Determines if a URL is open in any tab.
132 * @note Use of QString instead of QUrl is required to be callable via DBus.
133 *
134 * @param url URL to look for
135 * @returns true if url is currently open in a tab, false otherwise.
136 */
137 bool isUrlOpen(const QString &url);
138
139
140 /**
141 * Pastes the clipboard data into the currently selected folder
142 * of the active view. If not exactly one folder is selected,
143 * no pasting is done at all.
144 */
145 void pasteIntoFolder();
146
147 /**
148 * Implementation of the MainWindowAdaptor/QDBusAbstractAdaptor interface.
149 * Inform all affected dolphin components (panels, views) of an URL
150 * change.
151 */
152 void changeUrl(const QUrl& url);
153
154 /**
155 * The current directory of the Terminal Panel has changed, probably because
156 * the user entered a 'cd' command. This slot calls changeUrl(url) and makes
157 * sure that the panel keeps the keyboard focus.
158 */
159 void slotTerminalDirectoryChanged(const QUrl& url);
160
161 /** Stores all settings and quits Dolphin. */
162 void quit();
163
164 /**
165 * Opens a new tab and places it after the current tab
166 */
167 void openNewTabAfterCurrentTab(const QUrl& url);
168
169 /**
170 * Opens a new tab and places it as the last tab
171 */
172 void openNewTabAfterLastTab(const QUrl& url);
173
174 signals:
175 /**
176 * Is sent if the selection of the currently active view has
177 * been changed.
178 */
179 void selectionChanged(const KFileItemList& selection);
180
181 /**
182 * Is sent if the url of the currently active view has
183 * been changed.
184 */
185 void urlChanged(const QUrl& url);
186
187 /**
188 * Is emitted if information of an item is requested to be shown e. g. in the panel.
189 * If item is null, no item information request is pending.
190 */
191 void requestItemInfo(const KFileItem& item);
192
193 /**
194 * Is emitted if the settings have been changed.
195 */
196 void settingsChanged();
197
198 protected:
199 /** @see QWidget::showEvent() */
200 void showEvent(QShowEvent* event) override;
201
202 /** @see QMainWindow::closeEvent() */
203 void closeEvent(QCloseEvent* event) override;
204
205 /** @see KMainWindow::saveProperties() */
206 void saveProperties(KConfigGroup& group) override;
207
208 /** @see KMainWindow::readProperties() */
209 void readProperties(const KConfigGroup& group) override;
210
211 /** Handles QWhatsThisClickedEvent and passes all others on. */
212 bool event(QEvent* event) override;
213 /** Handles QWhatsThisClickedEvent and passes all others on. */
214 bool eventFilter(QObject*, QEvent*) override;
215
216 private slots:
217 /**
218 * Refreshes the views of the main window by recreating them according to
219 * the given Dolphin settings.
220 */
221 void refreshViews();
222
223 void clearStatusBar();
224
225 /** Updates the 'Create New...' sub menu. */
226 void updateNewMenu();
227
228 void createDirectory();
229
230 /** Shows the error message in the status bar of the active view. */
231 void showErrorMessage(const QString& message);
232
233 /**
234 * Updates the state of the 'Undo' menu action dependent
235 * on the parameter \a available.
236 */
237 void slotUndoAvailable(bool available);
238
239 /** Sets the text of the 'Undo' menu action to \a text. */
240 void slotUndoTextChanged(const QString& text);
241
242 /** Performs the current undo operation. */
243 void undo();
244
245 /**
246 * Copies all selected items to the clipboard and marks
247 * the items as cut.
248 */
249 void cut();
250
251 /** Copies all selected items to the clipboard. */
252 void copy();
253
254 /** Pastes the clipboard data to the active view. */
255 void paste();
256
257 /** Replaces the URL navigator by a search box to find files. */
258 void find();
259
260 /** Updates the state of the search action according to the view container. */
261 void updateSearchAction();
262
263 /**
264 * Updates the text of the paste action dependent on
265 * the number of items which are in the clipboard.
266 */
267 void updatePasteAction();
268
269 /** Selects all items from the active view. */
270 void selectAll();
271
272 /**
273 * Inverts the selection of all items of the active view:
274 * Selected items get nonselected and nonselected items get
275 * selected.
276 */
277 void invertSelection();
278
279 /**
280 * Switches between one and two views:
281 * If one view is visible, it will get split into two views.
282 * If already two views are visible, the active view gets closed.
283 */
284 void toggleSplitView();
285
286 /** Dedicated action to open the stash:/ ioslave in split view. */
287 void toggleSplitStash();
288
289 /** Reloads the currently active view. */
290 void reloadView();
291
292 /** Stops the loading process for the currently active view. */
293 void stopLoading();
294
295 void enableStopAction();
296 void disableStopAction();
297
298 void showFilterBar();
299
300 /**
301 * Toggles between edit and browse mode of the navigation bar.
302 */
303 void toggleEditLocation();
304
305 /**
306 * Switches to the edit mode of the navigation bar and selects
307 * the whole URL, so that it can be replaced by the user. If the edit mode is
308 * already active, it is assured that the navigation bar get focused.
309 */
310 void replaceLocation();
311
312 /**
313 * Toggles the state of the panels between a locked and unlocked layout.
314 */
315 void togglePanelLockState();
316
317 /**
318 * Is invoked if the Terminal panel got visible/invisible and takes care
319 * that the active view has the focus if the Terminal panel is invisible.
320 */
321 void slotTerminalPanelVisibilityChanged();
322
323 /** Goes back one step of the URL history. */
324 void goBack();
325
326 /** Goes forward one step of the URL history. */
327 void goForward();
328
329 /** Goes up one hierarchy of the current URL. */
330 void goUp();
331
332 /** Changes the location to the home URL. */
333 void goHome();
334
335 /** Open the previous URL in the URL history in a new tab. */
336 void goBackInNewTab();
337
338 /** Open the next URL in the URL history in a new tab. */
339 void goForwardInNewTab();
340
341 /** Open the URL one hierarchy above the current URL in a new tab. */
342 void goUpInNewTab();
343
344 /** * Open the home URL in a new tab. */
345 void goHomeInNewTab();
346
347 /** Opens Kompare for 2 selected files. */
348 void compareFiles();
349
350 /**
351 * Hides the menu bar if it is visible, makes the menu bar
352 * visible if it is hidden.
353 */
354 void toggleShowMenuBar();
355
356 /** Updates "Open Preferred Search Tool" action. */
357 void updateOpenPreferredSearchToolAction();
358
359 /** Opens preferred search tool for the current location. */
360 void openPreferredSearchTool();
361
362 /** Opens a terminal window for the current location. */
363 void openTerminal();
364
365 /** Focus a Terminal Panel. */
366 void focusTerminalPanel();
367
368 /** Opens the settings dialog for Dolphin. */
369 void editSettings();
370
371 /** Updates the state of the 'Show Full Location' action. */
372 void slotEditableStateChanged(bool editable);
373
374 /**
375 * Updates the state of the 'Edit' menu actions and emits
376 * the signal selectionChanged().
377 */
378 void slotSelectionChanged(const KFileItemList& selection);
379
380 /**
381 * Updates the state of the 'Back' and 'Forward' menu
382 * actions corresponding to the current history.
383 */
384 void updateHistory();
385
386 /** Updates the state of the 'Show filter bar' menu action. */
387 void updateFilterBarAction(bool show);
388
389 /** Open a new main window. */
390 void openNewMainWindow();
391
392 /**
393 * Opens a new view with the current URL that is part of a tab and
394 * activates it.
395 */
396 void openNewActivatedTab();
397
398 /**
399 * Adds the current URL as an entry to the Places panel
400 */
401 void addToPlaces();
402
403 /**
404 * Opens a new tab in the background showing the URL \a url.
405 */
406 void openNewTab(const QUrl& url, DolphinTabWidget::TabPlacement tabPlacement);
407
408 /**
409 * Opens the selected folder in a new tab.
410 */
411 void openInNewTab();
412
413 /**
414 * Opens the selected folder in a new window.
415 */
416 void openInNewWindow();
417
418 /**
419 * Show the target of the selected symlink
420 */
421 void showTarget();
422
423 /**
424 * Indicates in the statusbar that the execution of the command \a command
425 * has been finished.
426 */
427 void showCommand(CommandType command);
428
429 /**
430 * If the URL can be listed, open it in the current view, otherwise
431 * run it through KRun.
432 */
433 void handleUrl(const QUrl& url);
434
435 /**
436 * handleUrl() can trigger a stat job to see if the url can actually
437 * be listed.
438 */
439 void slotHandleUrlStatFinished(KJob* job);
440
441 /**
442 * Is invoked when the write state of a folder has been changed and
443 * enables/disables the "Create New..." menu entry.
444 */
445 void slotWriteStateChanged(bool isFolderWritable);
446
447 /**
448 * Opens the context menu on the current mouse position.
449 * @pos Position in screen coordinates.
450 * @item File item context. If item is null, the context menu
451 * should be applied to \a url.
452 * @url URL which contains \a item.
453 * @customActions Actions that should be added to the context menu,
454 * if the file item is null.
455 */
456 void openContextMenu(const QPoint& pos,
457 const KFileItem& item,
458 const QUrl& url,
459 const QList<QAction*>& customActions);
460
461 void updateControlMenu();
462 void updateToolBar();
463 void slotControlButtonDeleted();
464
465 /**
466 * Is called if the user clicked an item in the Places Panel.
467 * Reloads the view if \a url is the current URL already, and changes the
468 * current URL otherwise.
469 */
470 void slotPlaceActivated(const QUrl& url);
471
472 /**
473 * Is called if the another view has been activated by changing the current
474 * tab or activating another view in split-view mode.
475 *
476 * Activates the given view, which means that all menu actions are applied
477 * to this view. When having a split view setup, the nonactive view is
478 * usually shown in darker colors.
479 */
480 void activeViewChanged(DolphinViewContainer* viewContainer);
481
482 void closedTabsCountChanged(unsigned int count);
483
484 /**
485 * Is called if a new tab has been opened or a tab has been closed to
486 * enable/disable the tab actions.
487 */
488 void tabCountChanged(int count);
489
490 /**
491 * Updates the Window Title with the caption from the active view container
492 */
493 void updateWindowTitle();
494
495 /**
496 * This slot is called when the user requested to unmount a removable media
497 * from the places menu
498 */
499 void slotStorageTearDownFromPlacesRequested(const QString& mountPath);
500
501 /**
502 * This slot is called when the user requested to unmount a removable media
503 * _not_ from the dolphin's places menu (from the notification area for e.g.)
504 * This slot is basically connected to each removable device's
505 * Solid::StorageAccess::teardownRequested(const QString & udi)
506 * signal through the places panel.
507 */
508 void slotStorageTearDownExternallyRequested(const QString& mountPath);
509
510 /**
511 * Is called when the view has finished loading the directory.
512 */
513 void slotDirectoryLoadingCompleted();
514
515 /**
516 * Is called when the user middle clicks a toolbar button.
517 *
518 * Here middle clicking Back/Forward/Up/Home will open the resulting
519 * folder in a new tab.
520 */
521 void slotToolBarActionMiddleClicked(QAction *action);
522
523 /**
524 * Is called before the Back popup menu is shown. This slot will populate
525 * the menu with history data
526 */
527 void slotAboutToShowBackPopupMenu();
528
529 /**
530 * This slot is used by the Back Popup Menu to go back to a specific
531 * history index. The QAction::data will carry an int with the index
532 * to go to.
533 */
534 void slotGoBack(QAction* action);
535
536 /**
537 * Middle clicking Back/Forward will open the resulting folder in a new tab.
538 */
539 void slotBackForwardActionMiddleClicked(QAction *action);
540
541 /**
542 * Is called before the Forward popup menu is shown. This slot will populate
543 * the menu with history data
544 */
545 void slotAboutToShowForwardPopupMenu();
546
547 /**
548 * This slot is used by the Forward Popup Menu to go forward to a specific
549 * history index. The QAction::data will carry an int with the index
550 * to go to.
551 */
552 void slotGoForward(QAction* action);
553 private:
554 /**
555 * Sets up the various menus and actions and connects them.
556 */
557 void setupActions();
558
559 /**
560 * Sets up the dock widgets and their panels.
561 */
562 void setupDockWidgets();
563
564 void updateFileAndEditActions();
565 void updateViewActions();
566 void updateGoActions();
567
568 void createControlButton();
569 void deleteControlButton();
570
571 /**
572 * Adds the action \p action to the menu \p menu in
573 * case if it has not added already to the toolbar.
574 * @return True if the action has been added to the menu.
575 */
576 bool addActionToMenu(QAction* action, QMenu* menu);
577
578 /**
579 * Connects the signals from the created DolphinView with
580 * the DolphinViewContainer \a container with the corresponding slots of
581 * the DolphinMainWindow. This method must be invoked each
582 * time a DolphinView has been created.
583 */
584 void connectViewSignals(DolphinViewContainer* container);
585
586 /**
587 * Updates the text of the split action:
588 * If two views are shown, the text is set to "Split",
589 * otherwise the text is set to "Join". The icon
590 * is updated to match with the text and the currently active view.
591 */
592 void updateSplitAction();
593
594 bool isKompareInstalled() const;
595
596 /**
597 * Creates an action for showing/hiding a panel, that is accessible
598 * in "Configure toolbars..." and "Configure shortcuts...". This is necessary
599 * as the action for toggling the dock visibility is done by Qt which
600 * is no KAction instance.
601 */
602 void createPanelAction(const QIcon &icon,
603 const QKeySequence& shortcut,
604 QAction* dockAction,
605 const QString& actionName);
606
607 /** Adds "What's This?" texts to many widgets and StandardActions. */
608 void setupWhatsThis();
609
610 /**
611 * Returns the KIO::StatJob::mostLocalUrl() for the active container URL
612 * if it's a local file. Otherwise returns the user's home path.
613 */
614 QString activeContainerLocalPath();
615
616 /** Returns preferred search tool as configured in "More Search Tools" menu. */
617 QPointer<QAction> preferredSearchTool();
618
619 private:
620 /**
621 * Implements a custom error handling for the undo manager. This
622 * assures that all errors are shown in the status bar of Dolphin
623 * instead as modal error dialog with an OK button.
624 */
625 class UndoUiInterface : public KIO::FileUndoManager::UiInterface
626 {
627 public:
628 UndoUiInterface();
629 ~UndoUiInterface() override;
630 void jobError(KIO::Job* job) override;
631 };
632
633 KNewFileMenu* m_newFileMenu;
634 KHelpMenu* m_helpMenu;
635 DolphinTabWidget* m_tabWidget;
636 DolphinViewContainer* m_activeViewContainer;
637
638 DolphinViewActionHandler* m_actionHandler;
639 DolphinRemoteEncoding* m_remoteEncoding;
640 QPointer<DolphinSettingsDialog> m_settingsDialog;
641 DolphinBookmarkHandler* m_bookmarkHandler;
642
643 // Members for the toolbar menu that is shown when the menubar is hidden:
644 QToolButton* m_controlButton;
645 QTimer* m_updateToolBarTimer;
646
647 KIO::Job* m_lastHandleUrlStatJob;
648
649 TerminalPanel* m_terminalPanel;
650 PlacesPanel* m_placesPanel;
651 bool m_tearDownFromPlacesRequested;
652
653 KToolBarPopupAction* m_backAction;
654 KToolBarPopupAction* m_forwardAction;
655
656 QMenu m_searchTools;
657
658 };
659
660 inline DolphinViewContainer* DolphinMainWindow::activeViewContainer() const
661 {
662 return m_activeViewContainer;
663 }
664
665 inline KNewFileMenu* DolphinMainWindow::newFileMenu() const
666 {
667 return m_newFileMenu;
668 }
669
670 #endif // DOLPHIN_MAINWINDOW_H
671