1- # -*- coding: utf-8 -*-
21"""
32/***************************************************************************
43 Equirectangular Viewer
1817 * *
1918 ***************************************************************************/
2019"""
21- from qgis .gui import QgsMapToolIdentify
2220
23- from PyQt5 .QtCore import QTimer , Qt , QSettings , QThread
24- from PyQt5 .QtGui import QIcon , QCursor , QPixmap
25- from PyQt5 .QtWidgets import QAction
21+ from qgis .gui import QgsMapToolIdentify
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
2625
2726from EquirectangularViewer .Geo360Dialog import Geo360Dialog
2827import EquirectangularViewer .config as config
29- from EquirectangularViewer .server .local_server import openWebApp
3028from EquirectangularViewer .utils .log import log
3129from EquirectangularViewer .utils .qgsutils import qgsutils
3230from qgis .core import QgsApplication
31+ from functools import partial
32+ from http .server import SimpleHTTPRequestHandler , ThreadingHTTPServer
33+ from threading import Thread
34+ import time
3335
3436try :
3537 from pydevd import *
3638except ImportError :
3739 None
3840
39- try :
40- from cefpython3 import cefpython
41- except ImportError :
42- None
41+
42+ class QuietHandler ( SimpleHTTPRequestHandler ):
43+ def log_message ( self , format , * args ) :
44+ pass
4345
4446
4547class Geo360 :
46- timer = None
4748
4849 """QGIS Plugin Implementation."""
4950
@@ -58,100 +59,101 @@ def __init__(self, iface):
5859 # OpenCL acceleration
5960 QSettings ().setValue ("/core/OpenClEnabled" , True )
6061 self .dlg = None
61-
62- def createTimer (self ):
63- self .timer = QTimer ()
64- self .timer .timeout .connect (self .onTimer )
65- self .timer .start (10 )
66-
67- def onTimer (self ):
68- try :
69- cefpython .MessageLoopWork ()
70- except Exception :
71- None
72-
73- def stopTimer (self ):
74- self .timer .stop ()
75-
76- def StartCefPython (self ):
77- ''' Start CefPython '''
78- settings = {}
79- settings ["browser_subprocess_path" ] = "%s/%s" % (
80- cefpython .GetModuleDirectory (), "subprocess" )
81- settings ["log_severity" ] = cefpython .LOGSEVERITY_DISABLE
82- settings ["context_menu" ] = {
83- "enabled" : False ,
84- "navigation" : False , # Back, Forward, Reload
85- "print" : False ,
86- "view_source" : False ,
87- "external_browser" : False , # Open in external browser
88- "devtools" : False , # Developer Tools
89- }
90-
91- cefpython .Initialize (settings )
62+ self .server = None
63+ self .make_server ()
9264
9365 def initGui (self ):
94- ''' Add Geo360 tool '''
66+ """ Add Geo360 tool"""
9567 log .initLogging ()
96- self .action = QAction (QIcon (":/EquirectangularViewer/images/icon.png" ),
97- u"Equirectangular Viewer" ,
98- self .iface .mainWindow ())
68+ self .action = QAction (
69+ QIcon (":/EquirectangularViewer/images/icon.png" ),
70+ u"Equirectangular Viewer" ,
71+ self .iface .mainWindow (),
72+ )
9973 self .action .triggered .connect (self .run )
10074 self .iface .addToolBarIcon (self .action )
10175 self .iface .addPluginToMenu (u"&Equirectangular Viewer" , self .action )
10276
10377 def unload (self ):
104- ''' Unload Geo360 tool '''
78+ """ Unload Geo360 tool"""
10579 self .iface .removePluginMenu (u"&Equirectangular Viewer" , self .action )
10680 self .iface .removeToolBarIcon (self .action )
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" )
107121
108122 def run (self ):
109- ''' Run click feature '''
110- self .encontrado = False
123+ """ Run click feature"""
124+ self .found = False
111125
112126 # Check if mapa foto is loaded
113127 lys = self .canvas .layers ()
114- if len (lys ) == 0 :
115- qgsutils .showUserAndLogMessage (
116- u"Information: " , u"You need to upload the photo layer." )
117- return
118-
119- # Folder viewer for local server
120- folder = QgsApplication .qgisSettingsDirPath () + 'python/plugins/EquirectangularViewer/viewer'
121- # Start local server in plugin folder
122- openWebApp (folder )
123- self .StartCefPython ()
124-
125- # Create Timer is necessary for cefpython
126- self .createTimer ()
127-
128128 for layer in lys :
129129 if layer .name () == config .layer_name :
130- self .encontrado = True
130+ self .found = True
131131 self .mapTool = SelectTool (self .iface , parent = self , layer = layer )
132132 self .iface .mapCanvas ().setMapTool (self .mapTool )
133133
134- if self .encontrado is False :
134+ if self .found is False :
135135 qgsutils .showUserAndLogMessage (
136- u"Information: " , u"You need to upload the photo layer." )
137-
138- return
136+ u"Information: " , u"You need to upload the photo layer."
137+ )
138+ return
139139
140140 def ShowDialog (self , featuresId = None , layer = None ):
141- ''' Run dialog Geo360 '''
141+ """ Run dialog Geo360"""
142142 self .featuresId = featuresId
143143 self .layer = layer
144144
145145 if self .dlg is None :
146- 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+ )
147149 self .dlg .setWindowFlags (Qt .Window | Qt .WindowCloseButtonHint )
148150 self .dlg .show ()
149151 else :
150152 self .dlg .ReloadView (self .featuresId )
151153
152154
153155class SelectTool (QgsMapToolIdentify ):
154- ''' Select Photo on map '''
156+ """ Select Photo on map"""
155157
156158 def __init__ (self , iface , parent = None , layer = None ):
157159 QgsMapToolIdentify .__init__ (self , iface .mapCanvas ())
@@ -160,33 +162,40 @@ def __init__(self, iface, parent=None, layer=None):
160162 self .layer = layer
161163 self .parent = parent
162164
163- self .cursor = QCursor (QPixmap (["16 16 3 1" ,
164- " c None" ,
165- ". c #FF0000" ,
166- "+ c #FFFFFF" ,
167- " " ,
168- " +.+ " ,
169- " ++.++ " ,
170- " +.....+ " ,
171- " +. .+ " ,
172- " +. . .+ " ,
173- " +. . .+ " ,
174- " ++. . .++" ,
175- " ... ...+... ..." ,
176- " ++. . .++" ,
177- " +. . .+ " ,
178- " +. . .+ " ,
179- " ++. .+ " ,
180- " ++.....+ " ,
181- " ++.++ " ,
182- " +.+ " ]))
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+ )
183191
184192 def activate (self ):
185193 self .canvas .setCursor (self .cursor )
186194
187195 def canvasReleaseEvent (self , event ):
188196 found_features = self .identify (
189- event .x (), event .y (), [self .layer ], self .TopDownAll )
197+ event .x (), event .y (), [self .layer ], self .TopDownAll
198+ )
190199
191200 if len (found_features ) > 0 :
192201
0 commit comments