1818 ***************************************************************************/
1919"""
2020
21- import sys
22- import os
2321from qgis .gui import QgsMapToolIdentify
24- from PyQt5 . QtCore import QTimer , Qt , QSettings , QThread
25- from PyQt5 .QtGui import QIcon , QCursor , QPixmap
26- from PyQt5 .QtWidgets import QAction
22+ from qgis . PyQt . QtCore import Qt , QSettings , QThread
23+ from qgis . PyQt .QtGui import QIcon , QCursor , QPixmap
24+ from qgis . PyQt .QtWidgets import QAction
2725
2826from EquirectangularViewer .Geo360Dialog import Geo360Dialog
2927import EquirectangularViewer .config as config
30- from EquirectangularViewer .server .local_server import serverInFolder , serverShutdown
3128from EquirectangularViewer .utils .log import log
3229from EquirectangularViewer .utils .qgsutils import qgsutils
3330from qgis .core import QgsApplication
34- import platform
31+ from functools import partial
32+ from http .server import SimpleHTTPRequestHandler , ThreadingHTTPServer
33+ from threading import Thread
34+ import time
3535
3636try :
3737 from pydevd import *
3838except ImportError :
3939 None
4040
41- # libs_path = os.path.join(os.path.dirname(__file__), "libs")
42- # sys.path.append(libs_path)
4341
44- try :
45- from cefpython3 import cefpython as cef
46- import ctypes
47- except ImportError :
48- None
42+ class QuietHandler (SimpleHTTPRequestHandler ):
43+ def log_message (self , format , * args ):
44+ pass
4945
50- WINDOWS = (platform .system () == "Windows" )
51- LINUX = (platform .system () == "Linux" )
52- MAC = (platform .system () == "Darwin" )
5346
5447class Geo360 :
5548
5649 """QGIS Plugin Implementation."""
5750
5851 def __init__ (self , iface ):
5952
60- if not cef .GetAppSetting ("external_message_pump" ):
61- self .timer = self .createTimer ()
6253 self .iface = iface
6354 self .canvas = self .iface .mapCanvas ()
6455 threadcount = QThread .idealThreadCount ()
@@ -68,70 +59,68 @@ def __init__(self, iface):
6859 # OpenCL acceleration
6960 QSettings ().setValue ("/core/OpenClEnabled" , True )
7061 self .dlg = None
71-
72- def createTimer (self ):
73- timer = QTimer ()
74- timer .timeout .connect (self .onTimer )
75- timer .start (10 )
76- return timer
77-
78- def onTimer (self ):
79- try :
80- cef .MessageLoopWork ()
81- except Exception :
82- None
83-
84- def stopTimer (self ):
85- self .timer .stop ()
86-
87- def StartCefPython (self ):
88- ''' Start CefPython '''
89- settings = {}
90- settings ["browser_subprocess_path" ] = "%s/%s" % (
91- cef .GetModuleDirectory (), "subprocess" )
92- settings ["log_severity" ] = cef .LOGSEVERITY_DISABLE
93- settings ["context_menu" ] = {
94- "enabled" : True ,
95- "navigation" : False , # Back, Forward, Reload
96- "print" : True ,
97- "view_source" : True ,
98- "external_browser" : False , # Open in external browser
99- "devtools" : True , # Developer Tools
100- }
101-
102- if MAC :
103- # requires enabling message pump on Mac
104- # in Qt example. Calling cef.DoMessageLoopWork in a timer
105- # doesn't work anymore.
106- settings ["external_message_pump" ] = True
107-
108- cef .Initialize (settings )
62+ self .server = None
63+ self .make_server ()
10964
11065 def initGui (self ):
111- ''' Add Geo360 tool '''
66+ """ Add Geo360 tool"""
11267 log .initLogging ()
113- self .action = QAction (QIcon (":/EquirectangularViewer/images/icon.png" ),
114- u"Equirectangular Viewer" ,
115- self .iface .mainWindow ())
68+ self .action = QAction (
69+ QIcon (":/EquirectangularViewer/images/icon.png" ),
70+ u"Equirectangular Viewer" ,
71+ self .iface .mainWindow (),
72+ )
11673 self .action .triggered .connect (self .run )
11774 self .iface .addToolBarIcon (self .action )
11875 self .iface .addPluginToMenu (u"&Equirectangular Viewer" , self .action )
11976
12077 def unload (self ):
121- ''' Unload Geo360 tool '''
78+ """ Unload Geo360 tool"""
12279 self .iface .removePluginMenu (u"&Equirectangular Viewer" , self .action )
12380 self .iface .removeToolBarIcon (self .action )
124- #ShutDown server
125- serverShutdown ()
126- #ShutDown Cef
127- cef .Shutdown ()
81+ # Close server
82+ self .close_server ()
83+
84+ def is_running (self ):
85+ return self .server_thread and self .server_thread .is_alive ()
86+
87+ def close_server (self ):
88+ # Close server
89+ if self .server is not None :
90+ self .server .shutdown ()
91+ time .sleep (1 )
92+ self .server .server_close ()
93+ while self .server_thread .is_alive ():
94+ self .server_thread .join ()
95+ self .server = None
96+
97+ def make_server (self ):
98+ # Close server
99+ self .close_server ()
100+ # Create Server
101+ directory = (
102+ QgsApplication .qgisSettingsDirPath ().replace ("\\ " , "/" )
103+ + "python/plugins/EquirectangularViewer/viewer"
104+ )
105+ try :
106+ self .server = ThreadingHTTPServer (
107+ (config .IP , config .PORT ),
108+ partial (QuietHandler , directory = directory ),
109+ )
110+ self .server_thread = Thread (
111+ target = self .server .serve_forever , name = "http_server"
112+ )
113+ self .server_thread .daemon = True
114+ print ("Serving at port: %s" % self .server .server_address [1 ])
115+ time .sleep (1 )
116+ self .server_thread .start ()
117+ # while self.server_thread.is_alive():
118+ # print ("isRunning")
119+ except Exception :
120+ print ("Server Error" )
128121
129122 def run (self ):
130- ''' Run click feature '''
131- #Trick fix AttributeError: module 'sys' has no attribute 'argv'
132- # We need fedback about this
133- sys .argv = []
134-
123+ """Run click feature"""
135124 self .found = False
136125
137126 # Check if mapa foto is loaded
@@ -144,31 +133,27 @@ def run(self):
144133
145134 if self .found is False :
146135 qgsutils .showUserAndLogMessage (
147- u"Information: " , u"You need to upload the photo layer." )
136+ u"Information: " , u"You need to upload the photo layer."
137+ )
148138 return
149139
150- # Folder viewer for local server
151- folder = QgsApplication .qgisSettingsDirPath () + 'python/plugins/EquirectangularViewer/viewer'
152- # Start local server in plugin folder
153- serverInFolder (folder )
154- self .StartCefPython ()
155-
156-
157140 def ShowDialog (self , featuresId = None , layer = None ):
158- ''' Run dialog Geo360 '''
141+ """ Run dialog Geo360"""
159142 self .featuresId = featuresId
160143 self .layer = layer
161144
162145 if self .dlg is None :
163- self .dlg = Geo360Dialog (self .iface , parent = self , featuresId = featuresId , layer = self .layer )
146+ self .dlg = Geo360Dialog (
147+ self .iface , parent = self , featuresId = featuresId , layer = self .layer
148+ )
164149 self .dlg .setWindowFlags (Qt .Window | Qt .WindowCloseButtonHint )
165150 self .dlg .show ()
166151 else :
167152 self .dlg .ReloadView (self .featuresId )
168153
169154
170155class SelectTool (QgsMapToolIdentify ):
171- ''' Select Photo on map '''
156+ """ Select Photo on map"""
172157
173158 def __init__ (self , iface , parent = None , layer = None ):
174159 QgsMapToolIdentify .__init__ (self , iface .mapCanvas ())
@@ -177,33 +162,40 @@ def __init__(self, iface, parent=None, layer=None):
177162 self .layer = layer
178163 self .parent = parent
179164
180- self .cursor = QCursor (QPixmap (["16 16 3 1" ,
181- " c None" ,
182- ". c #FF0000" ,
183- "+ c #FFFFFF" ,
184- " " ,
185- " +.+ " ,
186- " ++.++ " ,
187- " +.....+ " ,
188- " +. .+ " ,
189- " +. . .+ " ,
190- " +. . .+ " ,
191- " ++. . .++" ,
192- " ... ...+... ..." ,
193- " ++. . .++" ,
194- " +. . .+ " ,
195- " +. . .+ " ,
196- " ++. .+ " ,
197- " ++.....+ " ,
198- " ++.++ " ,
199- " +.+ " ]))
165+ self .cursor = QCursor (
166+ QPixmap (
167+ [
168+ "16 16 3 1" ,
169+ " c None" ,
170+ ". c #FF0000" ,
171+ "+ c #FFFFFF" ,
172+ " " ,
173+ " +.+ " ,
174+ " ++.++ " ,
175+ " +.....+ " ,
176+ " +. .+ " ,
177+ " +. . .+ " ,
178+ " +. . .+ " ,
179+ " ++. . .++" ,
180+ " ... ...+... ..." ,
181+ " ++. . .++" ,
182+ " +. . .+ " ,
183+ " +. . .+ " ,
184+ " ++. .+ " ,
185+ " ++.....+ " ,
186+ " ++.++ " ,
187+ " +.+ " ,
188+ ]
189+ )
190+ )
200191
201192 def activate (self ):
202193 self .canvas .setCursor (self .cursor )
203194
204195 def canvasReleaseEvent (self , event ):
205196 found_features = self .identify (
206- event .x (), event .y (), [self .layer ], self .TopDownAll )
197+ event .x (), event .y (), [self .layer ], self .TopDownAll
198+ )
207199
208200 if len (found_features ) > 0 :
209201
0 commit comments