Ticket #40139: file_open_eventhandler.py

File file_open_eventhandler.py, 1.8 KB (added by dliessi (Davide Liessi), 11 years ago)
Line 
1# This file is part of the Frescobaldi project, http://www.frescobaldi.org/
2#
3# Copyright (c) 2013 - 2013 by Wilbert Berendsen
4#
5# This program is free software; you can redistribute it and/or
6# modify it under the terms of the GNU General Public License
7# as published by the Free Software Foundation; either version 2
8# of the License, or (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 Free Software
17# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18# See http://www.gnu.org/licenses/ for more information.
19
20"""
21This handles the QEvent::FileOpen event type sent to the QApplication when
22a file is clicked in the file manager.
23
24Currently this makes only sense on Mac OS X.
25"""
26
27from __future__ import unicode_literals
28
29from PyQt4.QtCore import QEvent, QObject
30from PyQt4.QtGui import QApplication
31
32import app
33
34
35def openUrl(url):
36    """Open Url.
37   
38    If there is an active MainWindow, the document is made the current
39    document in that window.
40   
41    """
42    if app.windows:
43        win = QApplication.activeWindow()
44        if win not in app.windows:
45            win = app.windows[0]
46        doc = win.openUrl(url)
47        if doc:
48            win.setCurrentDocument(doc)
49    else:
50        app.openUrl(url)
51
52
53class FileOpenEventHandler(QObject):
54    def eventFilter(self, obj, ev):
55        if ev.type() == QEvent.FileOpen:
56            openUrl(ev.url())
57            return True
58        return False
59
60
61handler = FileOpenEventHandler()
62app.qApp.installEventFilter(handler)
63
64