Changeset View
Changeset View
Standalone View
Standalone View
shell/macapplication.cpp
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2018 Sergio Bragin * | ||||
3 | * * | ||||
4 | * This program is free software; you can redistribute it and/or modify * | ||||
5 | * it under the terms of the GNU General Public License as published by * | ||||
6 | * the Free Software Foundation; either version 2 of the License, or * | ||||
7 | * (at your option) any later version. * | ||||
8 | ***************************************************************************/ | ||||
9 | | ||||
10 | #include "macapplication.h" | ||||
11 | | ||||
12 | #include <QFileOpenEvent> | ||||
13 | | ||||
14 | MacApplication::MacApplication(int &argc, char **argv) : QApplication(argc, argv), m_lastActiveShell(nullptr) { | ||||
15 | | ||||
16 | // Keep track of the last active Shell. | ||||
17 | connect(this, &QApplication::focusChanged, this, &MacApplication::setLastActiveShell); | ||||
18 | } | ||||
19 | | ||||
20 | bool MacApplication::event(QEvent *event) { | ||||
21 | | ||||
22 | auto fileOpenEvent = dynamic_cast<QFileOpenEvent *>(event); | ||||
23 | if (fileOpenEvent) { | ||||
24 | // Try to open the file in the last active Shell. If unsuccessful, create a new Shell instance. | ||||
25 | if (m_lastActiveShell && m_lastActiveShell -> openDocument(fileOpenEvent -> url(), m_serializedOptions)) { | ||||
26 | m_lastActiveShell -> activateWindow(); | ||||
27 | m_lastActiveShell -> raise(); | ||||
28 | } else { | ||||
29 | auto shell = new Shell(m_serializedOptions); | ||||
30 | shell -> show(); | ||||
31 | shell -> openDocument(fileOpenEvent -> url(), QString()); | ||||
32 | } | ||||
33 | } | ||||
34 | return QApplication::event(event); | ||||
35 | } | ||||
36 | | ||||
37 | void MacApplication::setLastActiveShell(QWidget *, QWidget *widgetInFocus) { | ||||
38 | | ||||
39 | // Check whether the widget, which got the focus, is a Shell instance. If yes, then update the last active Shell. | ||||
40 | if (widgetInFocus) { | ||||
41 | auto shell = dynamic_cast<Shell *>(widgetInFocus -> window()); | ||||
42 | if (shell) { | ||||
43 | m_lastActiveShell = shell; | ||||
44 | } | ||||
45 | } | ||||
46 | } |