Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

from director import callbacks 

from director.fieldcontainer import FieldContainer 

from director.timercallback import TimerCallback 

 

import re 

import numpy as np 

from collections import OrderedDict 

 

 

def cleanPropertyName(s): 

    """ 

    Generate a valid python property name by replacing all non-alphanumeric characters with underscores and adding an initial underscore if the first character is a digit 

    """ 

    return re.sub(r'\W|^(?=\d)','_',s).lower()  # \W matches non-alphanumeric, ^(?=\d) matches the first position if followed by a digit 

 

 

class PropertyAttributes(FieldContainer): 

 

    def __init__(self, **kwargs): 

 

        self._add_fields( 

          decimals = 5, 

          minimum = -1e4, 

          maximum = 1e4, 

          singleStep = 1, 

          hidden = False, 

          enumNames = None, 

          readOnly = False, 

          ) 

 

        self._set_fields(**kwargs) 

 

from PythonQt import QtGui 

 

def fromQColor(propertyName, propertyValue): 

    if isinstance(propertyValue, QtGui.QColor): 

        return [propertyValue.red()/255.0, propertyValue.green()/255.0, propertyValue.blue()/255.0] 

    else: 

        return propertyValue 

 

def toQProperty(propertyName, propertyValue): 

    if 'color' in propertyName.lower() and isinstance(propertyValue, (list, tuple)) and len(propertyValue) == 3: 

        return QtGui.QColor(propertyValue[0]*255.0, propertyValue[1]*255.0, propertyValue[2]*255.0) 

    elif isinstance(propertyValue, np.float): 

        return float(propertyValue) 

    elif isinstance(propertyValue, (list, tuple, np.ndarray)) and len(propertyValue) and isinstance(propertyValue[0], np.float): 

        return [float(x) for x in propertyValue] 

    else: 

        return propertyValue 

 

 

class PropertySet(object): 

 

    PROPERTY_CHANGED_SIGNAL = 'PROPERTY_CHANGED_SIGNAL' 

    PROPERTY_ADDED_SIGNAL = 'PROPERTY_ADDED_SIGNAL' 

    PROPERTY_ATTRIBUTE_CHANGED_SIGNAL = 'PROPERTY_ATTRIBUTE_CHANGED_SIGNAL' 

 

    def __getstate__(self): 

        d = dict(_properties=self._properties, _attributes=self._attributes) 

        return d 

 

    def __setstate__(self, state): 

        self.__init__() 

        attrs = state['_attributes'] 

        for propName, propValue in state['_properties'].iteritems(): 

            self.addProperty(propName, propValue, attributes=attrs.get(propName)) 

 

 

    def __init__(self): 

 

        self.callbacks = callbacks.CallbackRegistry([self.PROPERTY_CHANGED_SIGNAL, 

                                                     self.PROPERTY_ADDED_SIGNAL, 

                                                     self.PROPERTY_ATTRIBUTE_CHANGED_SIGNAL]) 

 

        self._properties = OrderedDict() 

        self._attributes = {} 

        self._alternateNames = {} 

 

    def propertyNames(self): 

        return self._properties.keys() 

 

    def hasProperty(self, propertyName): 

        return propertyName in self._properties 

 

    def assertProperty(self, propertyName): 

        assert self.hasProperty(propertyName), "Missing property: {:s}".format(propertyName) 

 

    def connectPropertyChanged(self, func): 

        return self.callbacks.connect(self.PROPERTY_CHANGED_SIGNAL, func) 

 

    def disconnectPropertyChanged(self, callbackId): 

        self.callbacks.disconnect(callbackId) 

 

    def connectPropertyAdded(self, func): 

        return self.callbacks.connect(self.PROPERTY_ADDED_SIGNAL, func) 

 

    def disconnectPropertyAdded(self, callbackId): 

        self.callbacks.disconnect(callbackId) 

 

    def connectPropertyAttributeChanged(self, func): 

        return self.callbacks.connect(self.PROPERTY_ATTRIBUTE_CHANGED_SIGNAL, func) 

 

    def disconnectPropertyAttributeChanged(self, callbackId): 

        self.callbacks.disconnect(callbackId) 

 

    def getProperty(self, propertyName): 

        self.assertProperty(propertyName) 

        return self._properties[propertyName] 

 

    def getPropertyEnumValue(self, propertyName): 

        self.assertProperty(propertyName) 

        return self._attributes[propertyName].enumNames[self._properties[propertyName]] 

 

    def removeProperty(self, propertyName): 

        assert self.hasProperty(propertyName) 

        alternateName = cleanPropertyName(propertyName) 

        del self._alternateNames[alternateName] 

        del self._properties[propertyName] 

        del self._attributes[propertyName] 

 

    def addProperty(self, propertyName, propertyValue, attributes=None): 

        alternateName = cleanPropertyName(propertyName) 

        if propertyName not in self._properties and alternateName in self._alternateNames: 

            raise ValueError('Adding this property would conflict with a different existing property with alternate name {:s}'.format(alternateName)) 

 

        propertyValue = fromQColor(propertyName, propertyValue) 

 

        self._alternateNames[alternateName] = propertyName 

        self._properties[propertyName] = propertyValue 

        self._attributes[propertyName] = attributes or PropertyAttributes() 

 

        self.callbacks.process(self.PROPERTY_ADDED_SIGNAL, self, propertyName) 

 

    def setPropertyIndex(self, propertyName, newIndex): 

        assert self.hasProperty(propertyName) 

        currentIndex = self._properties.keys().index(propertyName) 

        inds = range(len(self._properties)) 

        inds.remove(currentIndex) 

        inds.insert(newIndex, currentIndex) 

        items = self._properties.items() 

        self._properties = OrderedDict([items[i] for i in inds]) 

 

    def setProperty(self, propertyName, propertyValue): 

        self.assertProperty(propertyName) 

        propertyValue = fromQColor(propertyName, propertyValue) 

 

        names = self.getPropertyAttribute(propertyName, 'enumNames') 

        if names and type(propertyValue) != int: 

            propertyValue = names.index(propertyValue) 

 

        self.oldPropertyValue = (propertyName, self.getProperty(propertyName)) 

        self._properties[propertyName] = propertyValue 

        self.oldPropertyValue = None 

        self.callbacks.process(self.PROPERTY_CHANGED_SIGNAL, self, propertyName) 

 

    def getPropertyAttribute(self, propertyName, propertyAttribute): 

        self.assertProperty(propertyName) 

        return getattr(self._attributes[propertyName], propertyAttribute) 

 

    def setPropertyAttribute(self, propertyName, propertyAttribute, value): 

        self.assertProperty(propertyName) 

        attributes = self._attributes[propertyName] 

        assert hasattr(attributes, propertyAttribute), "Missing attribute: {:s}".format(propertyAttribute) 

        setattr(attributes, propertyAttribute, value) 

        self.callbacks.process(self.PROPERTY_ATTRIBUTE_CHANGED_SIGNAL, self, propertyName, propertyAttribute) 

 

    def __getattribute__(self, name): 

        try: 

            alternateNames = object.__getattribute__(self, '_alternateNames') 

            if name in alternateNames: 

                return object.__getattribute__(self, 'getProperty')(alternateNames[name]) 

            else: 

                raise AttributeError() 

        except AttributeError: 

            return object.__getattribute__(self, name) 

 

 

class PropertyPanelHelper(object): 

 

    @staticmethod 

    def addPropertiesToPanel(properties, panel, propertyNamesToAdd = None): 

 

        for propertyName in properties.propertyNames(): 

            value = properties.getProperty(propertyName) 

            attributes = properties._attributes[propertyName] 

 

            if value is not None and not attributes.hidden: 

                addThisProperty = True 

                if (propertyNamesToAdd is not None): 

                    if (propertyName not in propertyNamesToAdd): 

                        addThisProperty = False 

 

                if addThisProperty: 

                    PropertyPanelHelper._addProperty(panel, propertyName, attributes, value) 

 

 

    @staticmethod 

    def onPropertyValueChanged(panel, properties, propertyName): 

        prop = panel.getProperty(propertyName) 

 

        if prop is not None: 

 

            propertyValue = properties.getProperty(propertyName) 

            propertyValue = toQProperty(propertyName, propertyValue) 

 

            if isinstance(propertyValue, list): 

                for i, subValue in enumerate(propertyValue): 

                    panel.getSubProperty(prop, i).setValue(subValue) 

 

                groupName = PropertyPanelHelper.getPropertyGroupName(propertyName, propertyValue) 

                prop.setPropertyName(groupName) 

 

            else: 

                prop.setValue(propertyValue) 

 

    @staticmethod 

    def setPropertyFromPanel(prop, propertiesPanel, propertySet): 

 

        if prop.isSubProperty(): 

            if not propertiesPanel.getParentProperty(prop): 

                return 

 

            propertyIndex = propertiesPanel.getSubPropertyIndex(prop) 

            propertyName = prop.propertyName() 

            propertyName = propertyName[:propertyName.index('[')] 

 

            propertyValue = propertySet.getProperty(propertyName) 

            propertyValue = list(propertyValue) 

            propertyValue[propertyIndex] = prop.value() 

 

            propertySet.setProperty(propertyName, propertyValue) 

 

            groupName = PropertyPanelHelper.getPropertyGroupName(propertyName, propertyValue) 

            propertiesPanel.getParentProperty(prop).setPropertyName(groupName) 

 

        else: 

 

            propertyName = prop.propertyName() 

            propertyValue = prop.value() 

            propertyValue = fromQColor(propertyName, propertyValue) 

            propertySet.setProperty(propertyName, propertyValue) 

 

 

    @staticmethod 

    def _setPropertyAttributes(prop, attributes): 

 

        prop.setAttribute('decimals', attributes.decimals) 

        prop.setAttribute('minimum', attributes.minimum) 

        prop.setAttribute('maximum', attributes.maximum) 

        prop.setAttribute('singleStep', attributes.singleStep) 

        if attributes.enumNames: 

            prop.setAttribute('enumNames', attributes.enumNames) 

 

    @staticmethod 

    def getPropertyGroupName(name, value): 

        return '%s [%s]' % (name, ', '.join(['%.2f' % v if isinstance(v, float) else str(v) for v in value])) 

 

 

    @staticmethod 

    def _addProperty(panel, name, attributes, value): 

 

        value = toQProperty(name, value) 

 

        if isinstance(value, list): 

            groupName = PropertyPanelHelper.getPropertyGroupName(name, value) 

            groupProp = panel.addGroup(name, groupName) 

            for v in value: 

                p = panel.addSubProperty(name, v, groupProp) 

                PropertyPanelHelper._setPropertyAttributes(p, attributes) 

            return groupProp 

        elif attributes.enumNames: 

            p = panel.addEnumProperty(name, value) 

            PropertyPanelHelper._setPropertyAttributes(p, attributes) 

            p.setValue(value) 

            return p 

        else: 

            p = panel.addProperty(name, value) 

            PropertyPanelHelper._setPropertyAttributes(p, attributes) 

            return p 

 

 

class PropertyPanelConnector(object): 

    def __init__(self, propertySet, propertiesPanel, propertyNamesToAdd=None): 

        self.propertySet = propertySet 

        self.propertyNamesToAdd = propertyNamesToAdd 

        self.propertiesPanel = propertiesPanel 

        self.propertySet.connectPropertyAdded(self._onPropertyAdded) 

        self.propertySet.connectPropertyChanged(self._onPropertyChanged) 

        self.propertySet.connectPropertyAttributeChanged(self._onPropertyAttributeChanged) 

        self.propertiesPanel.connect('propertyValueChanged(QtVariantProperty*)', self._onPanelPropertyChanged) 

 

        self.timer = TimerCallback() 

        self.timer.callback = self._rebuildNow 

 

        self._blockSignals = True 

        PropertyPanelHelper.addPropertiesToPanel(self.propertySet, self.propertiesPanel, self.propertyNamesToAdd) 

        self._blockSignals = False 

 

    def cleanup(self): 

        self.propertiesPanel.disconnect('propertyValueChanged(QtVariantProperty*)', self._onPanelPropertyChanged) 

 

    def _rebuild(self): 

        if not self.timer.singleShotTimer.isActive(): 

            self.timer.singleShot(0) 

 

    def _rebuildNow(self): 

        self._blockSignals = True 

        self.propertiesPanel.clear() 

        PropertyPanelHelper.addPropertiesToPanel(self.propertySet, self.propertiesPanel) 

        self._blockSignals = False 

 

    def _onPropertyAdded(self, propertySet, propertyName): 

        self._rebuild() 

 

    def _onPropertyAttributeChanged(self, propertySet, propertyName, propertyAttribute): 

        self._rebuild() 

 

    def _onPropertyChanged(self, propertySet, propertyName): 

        self._blockSignals = True 

        PropertyPanelHelper.onPropertyValueChanged(self.propertiesPanel, propertySet, propertyName) 

        self._blockSignals = False 

 

    def _onPanelPropertyChanged(self, panelProperty): 

        if not self._blockSignals: 

            PropertyPanelHelper.setPropertyFromPanel(panelProperty, self.propertiesPanel, self.propertySet)