Skip to content
Permalink
Browse files

scripts/widgets_tree.py: switch to PyQt wrappers and reformat

  • Loading branch information
jef-n committed Mar 21, 2016
1 parent 93aedcd commit 719182fe3a455d96e321b5c5167bf5ad8503cdfa
Showing with 79 additions and 77 deletions.
  1. +79 −77 scripts/widgets_tree.py
@@ -39,9 +39,9 @@
import sys
import os
import glob
from PyQt4.QtGui import QWidget, QDialog, QCheckBox, QComboBox, QDial, QPushButton, QLabel, QLCDNumber, QLineEdit, QRadioButton, QScrollBar, QSlider, QSpinBox, QTextEdit, QDateEdit, QTimeEdit, QDateTimeEdit, QListView, QProgressBar, QTableView, QTabWidget, QTextBrowser, QDialogButtonBox, QScrollArea, QGroupBox, QStackedWidget
from PyQt.QtWidgets import QWidget, QDialog, QCheckBox, QComboBox, QDial, QPushButton, QLabel, QLCDNumber, QLineEdit, QRadioButton, QScrollBar, QSlider, QSpinBox, QTextEdit, QDateEdit, QTimeEdit, QDateTimeEdit, QListView, QProgressBar, QTableView, QTabWidget, QTextBrowser, QDialogButtonBox, QScrollArea, QGroupBox, QStackedWidget
from PyQt4.uic import loadUi
from PyQt4.QtXml import QDomDocument
from PyQt.QtXml import QDomDocument

# qwt_plot is missing somehow but it may depend on installed packages
from PyQt4 import Qwt5 as qwt_plot
@@ -53,83 +53,85 @@
# QgsRendererRulesTreeWidget
# and QgsProjectionSelector cannot open db file
from qgis import gui
for m in ['qgscolorbutton', 'qgscolorrampcombobox', 'qgsprojectionselector', 'qgslabelpreview', 'qgsrulebasedrendererv2widget', 'qgscollapsiblegroupbox', 'qgsblendmodecombobox', 'qgsexpressionbuilderwidget', 'qgsrasterformatsaveoptionswidget', 'qgsrasterpyramidsoptionswidget', 'qgsscalecombobox', 'qgsfilterlineedit', 'qgsdualview' ]:
sys.modules[m] = gui
for m in ['qgscolorbutton', 'qgscolorrampcombobox', 'qgsprojectionselector', 'qgslabelpreview', 'qgsrulebasedrendererv2widget', 'qgscollapsiblegroupbox', 'qgsblendmodecombobox', 'qgsexpressionbuilderwidget', 'qgsrasterformatsaveoptionswidget', 'qgsrasterpyramidsoptionswidget', 'qgsscalecombobox', 'qgsfilterlineedit', 'qgsdualview']:
sys.modules[m] = gui


class UiInspector:
def __init__(self):
self.ui_dir = os.path.abspath ( os.path.join( os.path.dirname(__file__), '../src/ui/*.ui' ) )
self.printMsg ( "Loading UI files " + self.ui_dir )
# list of widget classes we want to follow
self.follow = [
QWidget, QDialog,
QCheckBox, QComboBox, QDial, QPushButton, QLabel, QLCDNumber, QLineEdit, QRadioButton, QScrollBar, QSlider, QSpinBox, QTextEdit,
QDateEdit, QTimeEdit, QDateTimeEdit, QListView, QProgressBar, QTableView, QTabWidget, QTextBrowser, QDialogButtonBox,
QScrollArea, QGroupBox, QStackedWidget,
]

def printMsg ( self, msg ):
sys.stderr.write( msg + "\n" )

def widgetXml(self, element, widget, level=0, label=None ):
#print tostring ( element )
#self.printMsg ( "class: " + str( type ( widget ) ) )
#self.printMsg ( "objectName: " + widget.objectName() )
#self.printMsg ( "windowTitle: " + widget.windowTitle() )

if not widget.objectName():
return

lab = label
if hasattr( widget, 'text' ):
lab = widget.text()
if widget.windowTitle():
label = widget.windowTitle()
if not lab:
lab = ''

subElement = self.doc.createElement('widget')
subElement.setAttribute('class', widget.__class__.__name__ )
subElement.setAttribute('objectName', widget.objectName() )
subElement.setAttribute('label', lab )
element.appendChild( subElement )

#print str ( widget.children () )
# tab widget label is stored in QTabWidget->QTabBarPrivate->tabList->QTab ..
if type(widget) in [ QTabWidget ]:
children = list ( { 'widget': widget.widget(i), 'label': widget.tabText(i) } for i in range ( 0, widget.count() ) )
else:
children = list ( { 'widget': c, 'label': None } for c in widget.children () )
for child in children:
w = child['widget']
if w.isWidgetType() and ( type(w) in self.follow ):
self.widgetXml( subElement, w, level+1, child['label'] )

def xml(self) :
self.doc = QDomDocument()
element = self.doc.createElement( "qgiswidgets" )
self.doc.appendChild( element )
for p in glob.glob( self.ui_dir ):
self.printMsg ( "Loading " + p )
# qgsrasterlayerpropertiesbase.ui is giving: No module named qwt_plot
try:
widget = loadUi ( p )
#print dir ( ui )
self.widgetXml( element, widget )
except Exception, e:
#except IOError, e:
self.printMsg( str(e) )

return self.doc.toString( 2 )

def __init__(self):
self.ui_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '../src/ui/*.ui'))
self.printMsg("Loading UI files " + self.ui_dir)
# list of widget classes we want to follow
self.follow = [
QWidget, QDialog,
QCheckBox, QComboBox, QDial, QPushButton, QLabel, QLCDNumber, QLineEdit, QRadioButton, QScrollBar, QSlider, QSpinBox, QTextEdit,
QDateEdit, QTimeEdit, QDateTimeEdit, QListView, QProgressBar, QTableView, QTabWidget, QTextBrowser, QDialogButtonBox,
QScrollArea, QGroupBox, QStackedWidget,
]

def printMsg(self, msg):
sys.stderr.write(msg + "\n")

def widgetXml(self, element, widget, level=0, label=None):
#print tostring ( element )
#self.printMsg ( "class: " + str( type ( widget ) ) )
#self.printMsg ( "objectName: " + widget.objectName() )
#self.printMsg ( "windowTitle: " + widget.windowTitle() )

if not widget.objectName():
return

lab = label
if hasattr(widget, 'text'):
lab = widget.text()
if widget.windowTitle():
label = widget.windowTitle()
if not lab:
lab = ''

subElement = self.doc.createElement('widget')
subElement.setAttribute('class', widget.__class__.__name__)
subElement.setAttribute('objectName', widget.objectName())
subElement.setAttribute('label', lab)
element.appendChild(subElement)

#print str ( widget.children () )
# tab widget label is stored in QTabWidget->QTabBarPrivate->tabList->QTab ..
if type(widget) in [QTabWidget]:
children = list({'widget': widget.widget(i), 'label': widget.tabText(i)} for i in range(0, widget.count()))
else:
children = list({'widget': c, 'label': None} for c in widget.children())
for child in children:
w = child['widget']
if w.isWidgetType() and (type(w) in self.follow):
self.widgetXml(subElement, w, level + 1, child['label'])

def xml(self):
self.doc = QDomDocument()
element = self.doc.createElement("qgiswidgets")
self.doc.appendChild(element)
for p in glob.glob(self.ui_dir):
self.printMsg("Loading " + p)
# qgsrasterlayerpropertiesbase.ui is giving: No module named qwt_plot
try:
widget = loadUi(p)
#print dir ( ui )
self.widgetXml(element, widget)
except Exception, e:
#except IOError, e:
self.printMsg(str(e))

return self.doc.toString(2)


if __name__ == '__main__':
from PyQt4.QtCore import QApplication
app = QApplication(sys.argv) # required by loadUi
inspector = UiInspector()
xml = inspector.xml()
sys.stdout.write( xml )
sys.stdout.flush()

del app
sys.exit(0)
from PyQt4.QtCore import QApplication
app = QApplication(sys.argv) # required by loadUi
inspector = UiInspector()
xml = inspector.xml()
sys.stdout.write(xml)
sys.stdout.flush()

del app
sys.exit(0)

0 comments on commit 719182f

Please sign in to comment.
You can’t perform that action at this time.