summaryrefslogtreecommitdiff
path: root/lib/taurus/qt/qtgui/tree/taurusdevicetree.py
blob: 7f18130877adcd5d44a6e24dfdc4014dd418b5fc (plain)
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
#!/usr/bin/env python

#############################################################################
##
## This file is part of Taurus
## 
## http://taurus-scada.org
##
## Copyright 2011 CELLS / ALBA Synchrotron, Bellaterra, Spain
## 
## Taurus is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## 
## Taurus is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU Lesser General Public License for more details.
## 
## You should have received a copy of the GNU Lesser General Public License
## along with Taurus.  If not, see <http://www.gnu.org/licenses/>.
##
#############################################################################

"""
taurusdevicetree.py: 
"""

__all__ = ["TaurusDevTree","TaurusSearchTree","TaurusDevTreeOptions"] #,"SearchEdit"] #"TaurusTreeNode"]

import time,os,re,traceback
from functools import partial
import PyTango # to change!!

try:import icons_dev_tree
except:icons_dev_tree = None

from taurus.external.qt import Qt

import taurus.core
from taurus.core.util.colors import DEVICE_STATE_PALETTE,ATTRIBUTE_QUALITY_PALETTE
from taurus.core.util.containers import CaselessDict
from taurus.core.tango.search import * #@TODO: Avoid implicit imports
from taurus.qt.qtcore.util.emitter import SingletonWorker
from taurus.qt.qtcore.mimetypes import *  #@TODO: Avoid implicit imports
from taurus.qt.qtcore.util import properties
from taurus.qt.qtcore.util.properties import djoin
from taurus.qt.qtgui.base import TaurusBaseComponent, TaurusBaseWidget
from taurus.qt.qtgui.container import TaurusWidget

TREE_ITEM_MIME_TYPE = 'application/x-qabstractitemmodeldatalist'

###############################################################################

class TaurusTreeNodeContainer(object):
    """
    Interface that provides Node-focused methods to TaurusDevTree
    This methods should be moved to TaurusDevTreeNode class when it will be able to retrieve currentItem from Tree.
    """
    _icon_map = {} #A dictionary like {device_regexp:pixmap_url}
    
    def __init__(self):
        raise Exception('This class is just an interface, do not instantiate it!')
    
    @classmethod
    def setIconMap(klass,filters):
        """A dictionary like {device_regexp:pixmap_url}"""
        klass._icon_map = filters
        
    @classmethod
    def getIconMap(klass):
        return klass._icon_map
    
    def createItem(self,parent,value,text=None):
        self.debug('createItem(%s,%s)'%(value,text))
        USE_TREE_NODE = False
        if USE_TREE_NODE: item = TaurusTreeNode(parent)
        else: item = Qt.QTreeWidgetItem(parent)
        if text is None: text = value
        item.isAttribute = False
        item.DeviceName = ''
        item.draggable = ''
        item.setText(0,Qt.QApplication.translate('',text, None, Qt.QApplication.UnicodeUTF8))
        self.setNodeParent(item,parent)
        item.adminNode = None
        if not item.parentNode or '/' in text:
            f = item.font(0)
            if not item.parentNode: f.setBold(True)
            if '/' in text: f.setItalic(True)
            item.setFont(0,f)
        item.parentTree = self #hook used to call external methods with item as single argument
        self.item_index[value.strip().split()[0]] = item
        try:
            icon = self.getNodeIcon(item)
            if icon: item.setIcon(0,icon)
        except: pass
        self.item_list.add(item)
        return item

    ###########################################################################
    # Item members methods
    
    def setNodeParent(self,node,parent):
        """ Used to know which parent attributes must be expanded if found """
        node.parentNode = parent if isinstance(parent,Qt.QTreeWidgetItem) else None
        
    def setNodeAdmin(self,node,admin):
        """ Used to assign a controller to its controlled devices in the tree """
        node.adminNode = admin.getNodeText(admin) if isinstance(admin,Qt.QTreeWidgetItem) else None

    def getNodeAdmin(self,node):
        return node.adminNode(node) if isCallable(node.adminNode) else node.adminNode
        
    def getNodeText(self,node=None,full=False):
        """ Get the text of the node as shown in the tree, @full allows to get the first word or the whole text"""
        if node is None: node = self.currentItem()
        if hasattr(node,'text'):
            txt = str(node.text(0)).strip()
            if not full: return txt.split()[0]
            return txt
        else: return ''
    
    def getNodeDeviceName(self,node = None):
        if node is None: node = self.currentItem()
        return str(getattr(node,'DeviceName','')) or self.getNodeText(node)

    def getNodeParentName(self,node=None):
        if node is None: node = self.currentItem()
        return self.getNodeText(node.parentNode)
        
    def getNodePath(self,node=None):
        """ Returns all parent nodes prior to current """
        if node is None: node = self.currentItem()
        p,path,names = node.parentNode,[],[]
        while p is not None:
            path.insert(0,p)
            names.insert(0,self.getNodeDeviceName(p))
            p = p.parentNode
        return path
        
    def getNodeAlias(self,node = None):
        if node is None: node = self.currentItem()        
        alias = getattr(node,'AttributeAlias','')
        return (alias or self.getNodeText(node))

    def getNodeIcon(self,node=None):
        #self.debug('TaurusDevTree.getNodeIcon(node) not implemented, overrided in subclasses')
        
        #self,url = node.parentTree,''
        if node is None: node = self.getNode()
        try:
            name,url = self.getNodeText(node),''
            for k,v in self.getIconMap().items():
                if re.match(k.lower(),name.lower()): url = v
            if not url:
                for k,v in self.getIconMap().items():
                    if k.lower() in name.lower(): url = v
            #if name.count('/')==2:
                #if any(a.startswith(name+'/') for a in getArchivedAttributes()):
                    #url = wdir('image/icons/clock.png')
                #else:
                    #url = wdir('image/equips/icon-%s.gif'%name.split('/')[2].split('-')[0].lower())
            #elif name.count('/')==3:
                #url = filterAttributes(name) or wdir('image/icons/closetab.png')
            #else:
                #url = wdir('image/equips/icon-%s.gif'%name.lower())
        except:
            self.warning(traceback.format_exc())
        if not url or not os.path.isfile(url): return None
        else: return Qt.QIcon(url)
    
    def getNodeDraggable(self,node = None):
        """ This method will return True only if the selected node belongs to a numeric Tango attribute """
        numtypes = [PyTango.DevDouble,PyTango.DevFloat,PyTango.DevLong,PyTango.DevLong64,PyTango.DevULong,PyTango.DevShort,PyTango.DevUShort,PyTango.DevBoolean]
        if node is None: node = self.currentItem()
        try:
            name = self.getNodeText(node).lower()
            drag = name
            if node.isAttribute and getattr(node,'DeviceName','') and '/' not in name: name = node.DeviceName+'/'+name
            if name.count('/')==2: #A Device Name
                drag = name#+'/state' #False
            elif name.count('/')==3: #An Attribute Name
                #dtype = PyTango.AttributeProxy(name).get_config().data_type
                #if dtype in numtypes: self.debug('The attribute %s is a Numeric Attribute'%(name))
                drag = getattr(node,'draggable','') or name
                #else: drag = False
            self.debug('Node(%s,%s,%s): drag: %s'%(name,node.isAttribute,node.DeviceName,drag))
            return drag.split()[0]
        except:
            import traceback
            self.warning(traceback.format_exc())
            return False 
            
    ###########################################################################
    # Context Menu Actions
    
    @staticmethod
    def setDefaultPanelClass(other):
        TaurusTreeNodeContainer._defaultClass = other
    @staticmethod
    def defaultPanelClass():
        if not hasattr(TaurusTreeNodeContainer,'_defaultClass'): 
            from taurus.qt.qtgui.panel import TaurusDevicePanel
            TaurusTreeNodeContainer._defaultClass = TaurusDevicePanel
        obj = TaurusTreeNodeContainer._defaultClass
        return obj
            
    def showPanel(self):
        '''Display widget taurusDevicePanel'''
        device = self.getNodeText()
        nameclass = self.defaultPanelClass()()
        nameclass.setModel(device)
        nameclass.show()
        ##nameclass.setSpectraAtkMode(True)
        #Dialog is used to make new floating panels persistent
        if isinstance(nameclass,TaurusWidget):
            PopupDialog(self,nameclass)

    def showProperties(self):
        '''Display widget TaurusPropTable'''
        import taurus.qt.qtgui.table
        device = self.getNodeText()
        nameclass = taurus.qt.qtgui.table.TaurusPropTable()
        nameclass.setTable(device)
        nameclass.show()
        #Dialog is used to make new floating panels persistent
        PopupDialog(self,nameclass)

    def addToPlot(self):
        """ This method will send a signal with the current selected node """
        items = self.getSelectedNodes()
        for item in items:
            attr = self.getNodeAlias(item)
            self.trace('In addToPlot(%s->%s)'%(item.text(0),attr))
            self.addAttrToPlot(attr)
        return
        
    def addAttrToPlot(self,attr):
        """ This method will send a signal with the given attr name, in a separate method to be called with a pre-filled list  """
        self.emit(Qt.SIGNAL("addAttrSelected(QStringList)"),Qt.QStringList([str(attr)]))

    def removeFromPlot(self):
        """ This method will send a signal with the current selected node """
        items = self.getSelectedNodes()
        for item in items:
            item = self.currentItem()
            attr = getattr(item,'AttributeAlias','') or self.getNodeText(item)
            self.removeAttrFromPlot(attr)
        return
        
    def removeAttrFromPlot(self,attr):
        """ This method will send a signal with the given attr name, in a separate method to be called with a pre-filled list """        
        self.emit(Qt.SIGNAL("removeAttrSelected(QStringList)"),Qt.QStringList([str(attr)]))


###############################################################################
        
class TaurusDevTree(TaurusTreeNodeContainer,Qt.QTreeWidget, TaurusBaseWidget):
    ''' 
    This widget displays a list of servers, devices or instances.
    To set a new Model use either setModel(filters), addModels(list), setFilters(...) or loadTree(filters)
    setModel and loadTree are equivalent; adding a new branch to the tree
    addModels merges the tree with new models
    setFilters clears previous models and adds new one
    '''
    __pyqtSignals__ = (
        "modelChanged(const QString &)",
        "deviceSelected(QString)",
        "addAttrSelected(QStringList)",
        "removeAttrSelected(QStringList)",
        "refreshTree",
        "nodeFound"
        )
    __properties__ = (
        'ModelInConfig',
        'modifiableByUser',
        #'useParentModel',
        'Filters',
        'Source',
        'ShowAlias',
        'ShowColors',
        'ShowNotExported',
        'MaxDevices',
        )
    __slots__ = (
        "setTangoHost",
        #"setModel",
        #"setFilters",
        "addModels",
        "setModelCheck",
        "loadTree", #Applies regexp filters to database
        "setTree",
        "findInTree",
        "setIcons",
        "expandAll",
        "expandNode",
        "collapseNode",
        )
        
    TRACE_ALL = False

    def __init__(self, parent=None, designMode = False):
        name = "TaurusDevTree"
        self._useParentModel = True
        self._localModel = ''
        self.call__init__wo_kw(Qt.QTreeWidget, parent)
        self.call__init__(TaurusBaseWidget, name, designMode=designMode)

        self.setObjectName(name)
        self._filters = []
        self.__attr_filter = None
        self.__expand =1
        self.collapsing_search = True
        self.index = 0
        self._nameTopItem = ""

        self.excludeFromSearch = [] #This is a list of regular expressions to exclude objects from searches
        self.dictionary = {}
        self.item_index = CaselessDict()
        self.item_list = set() #NOTE: as several nodes may share the same name this list will be different from item_index.values()!!!
        self.setSelectionMode(self.ExtendedSelection)
        
        self.ContextMenu=[]
        self.ExpertMenu=[]

        #The SingletonWorker Threads are used for expanding nodes and also for loading a new tree; both objects are the same thread, but read from different queues
        self.__loader = None
        self.__expander = None

        if not designMode:
            self.Loader
            self.Expander
        
        self.initConfig()
        
        #Signal
        Qt.QObject.connect(self,Qt.SIGNAL("itemClicked(QTreeWidgetItem *,int)"),self.deviceClicked)
        Qt.QObject.connect(self,Qt.SIGNAL("nodeFound"),self,Qt.SLOT("expandNode"))
        
        self.setDragDropMode(Qt.QAbstractItemView.DragDrop)
        self.setModifiableByUser(True)
        self.setModelInConfig(False) #We store Filters instead!
        self.setAcceptDrops(True)
        self.viewport().setAcceptDrops(True)
        self.setDragEnabled(True)
        self.setSupportedMimeTypes([
            TAURUS_MODEL_LIST_MIME_TYPE, TAURUS_DEV_MIME_TYPE, TAURUS_ATTR_MIME_TYPE, 
            TAURUS_MODEL_MIME_TYPE, TREE_ITEM_MIME_TYPE, 'text/plain'])
        
        self.setTangoHost()
        self.defineStyle()

    @property
    def Loader(self):
        loader = self.__loader
        if loader is None:
            loader = SingletonWorker(parent=self, name='TreeLoader',
                                     cursor=True, start=True)
            self.__loader = loader
        return loader

    @property
    def Expander(self):
        expander = self.__expander
        if expander is None:
            expander = SingletonWorker(parent=self, name='NodeExpander',
                                       method=lambda node, expand : node.setExpanded(expand),
                                       cursor=True, start=True)
            self.__expander = expander
        return expander

    def getConfig(self,name): 
        properties.get_property(self,name)
    
    def initConfig(self):
        """
        Initializing the attributes that will be kept persitent as Qt settings.
        e.g. for Filters property, the following attributes are created:
        
            - self.filters
            - self._filters
            - self.setFilters
            - self.getFilters
            - self.resetFilters
            
        """
        properties.set_property_methods(self,'Models','QStringList',default='',
            #setter = self.setFilters,
            setter = self.addModels, #Not trivial!; it avoids QSettings erasing default model
            #set_callback=lambda v,s=self:v and s.loadTree(v,clear=True),
            #reset_callback=lambda s=self:s.setFilters(''),
            qt=False,config=True
            )
        properties.set_property_methods(self,'MaxDevices','int',default=150,qt=False,config=True)
        properties.set_property_methods(self,'ShowAlias','bool',default=False,qt=False,config=True)
        properties.set_property_methods(self,'ShowNotExported','bool',default=True,qt=False,config=True)
        properties.set_property_methods(self,'ShowColors','bool',default=True,qt=False,config=True)
        #properties.set_property_methods(self,'Expand','int',default=0)
        
    @staticmethod
    def setDefaultAttrFilter(other):
        TaurusDevTree._defattrfilter = staticmethod(other)
        
    @staticmethod
    def defaultAttrFilter():
        if not hasattr(TaurusDevTree,'_defattrfilter'): TaurusDevTree._defattrfilter = None
        return TaurusDevTree._defattrfilter
    
    def setAttrFilter(self,other):
        self._attrfilter = other
        
    def getAttrFilter(self):
        if not isCallable(getattr(self,'_attrfilter',None)): self._attrfilter = None
        return self._attrfilter
    
    def matchAttrFilter(self,target):
        def printf(s): print(s)
        if self.getAttrFilter() and isCallable(self._attrfilter): return self._attrfilter(target,p=printf)
        elif TaurusDevTree.defaultAttrFilter() and isCallable(TaurusDevTree._defattrfilter): return TaurusDevTree._defattrfilter(target,p=printf)
        else: return True
        
    #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
    # TaurusBaseWidget over writing methods 
    #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-

    def sizeHint(self):
        return Qt.QTreeWidget.sizeHint(self)

    def minimumSizeHint(self):
        return Qt.QTreeWidget.minimumSizeHint(self)

    @classmethod
    def getQtDesignerPluginInfo(cls):
        ret = TaurusBaseWidget.getQtDesignerPluginInfo()
        ret['module'] = 'taurus.qt.qtgui.tree'
        ret['group'] = 'Taurus Views'
        ret['icon'] = ":/designer/listview.png"
        return ret
    
    def defineStyle(self):
        self.setWindowTitle('TaurusDevTree')
        self.setHeaderLabel('Device Browser (right-click on any element to search/show options)')
        self.setGeometry(Qt.QRect(90,60,256,192))
        self.actionFindInTree = Qt.QAction(self)
        self.actionFindInTree.setShortcut(Qt.QKeySequence.Find)
        self.connect(self.actionFindInTree, Qt.SIGNAL("triggered()"), self.findDialog)
        #self.connect(self, Qt.SIGNAL("itemClicked"), self.clickedEvent)
        from taurus.qt.qtgui.table.qdictionary import QDictionaryEditor,QListEditor
        self.ExpertMenu.append(
            ('Edit Model Filters',
            lambda:QListEditor.main(
                self._filters,
                modal=True,
                title='Edit Model Filters',
                callback=lambda d:self.loadTree(d)
                )
            #lambda:self.loadTree(
                #str(Qt.QInputDialog.getText(None,'Set Tree Model','Enter a list of regexp separated by comma:',Qt.QLineEdit.Normal,','.join(str(f) for f in self._filters))[0])
                #or None)
            ))
        self.ExpertMenu.append(
            ('Edit Tree',
            lambda:QDictionaryEditor.main(self.dictionary,modal=True,title='Edit Tree',callback=lambda d:self.setTree(d,clear=True))
            ))
        self.ExpertMenu.append(
            ('Expand All',
            lambda:self.expandAll()
            ))
        self.ExpertMenu.append(
            ('Collapse All',
            lambda: self.collapseNode(ALL=True)
            ))
        self.ExpertMenu.append(
            ('Save Config',
            lambda:self.saveConfigFile()
            ))
        if not getattr(self,'DeviceMenu',None): self.DeviceMenu = {}
        self.DeviceMenu.update({
            'Show Properties':'showProperties',
            'Refresh Tree':'refreshTree',
            })
        if not getattr(self,'AttributeMenu',None): self.AttributeMenu = []
        [self.AttributeMenu.append(a) for a in  [
            ('Add to trends','addToPlot'),
            ('Remove from trends','removeFromPlot'),
            ] if a not in self.AttributeMenu]
        try:
            from PyTangoArchiving.widget.history import show_history
            self.debug('Adding show_history from archiving...')
            self.AttributeMenu.append(('Show History',show_history))
        except: pass
            
    def trace(self,msg):
        if self.TRACE_ALL or self.getLogLevel() in ('DEBUG',40,):
            print 'TaurusDevTree.%s: %s'%(self.getLogLevel(),msg) #@TODO: use the taurus logger instead! ~~cpascual 20121121
        
    def setTangoHost(self,tango_host=None):
        self.db = taurus.Database(tango_host)
        
    #model = Qt.pyqtProperty("QString", TaurusBaseWidget.getModel, 
                                #TaurusBaseWidget.setModel, 
                                #TaurusBaseWidget.resetModel)
        
    def getModel(self):
        return self._filters    
    
    def getModelClass(self):
        return list #taurus.core.taurusdatabase.TaurusDatabase
        
    def setModel(self,model):
        TaurusBaseWidget.setModel(self,model)
    
    def setModelCheck(self,model):
        # Called from TaurusBaseWidget.setModel()
        self.trace('setModelCheck(%s)'%str(model)[:80])
        self.loadTree(model)
        
    @Qt.pyqtSignature("addModels(QStringList)")
    def addModels(self, modelNames):
        '''Adds models to the existing ones:
        :param modelNames:  (sequence<str>) the names of the models to be added
        .. seealso:: :meth:`removeModels`
        '''
        self.trace('In addModels(%s)'%str(modelNames)[:80])
        modelNames = split_model_list(modelNames)
        self.setTree(self.getTangoDict(modelNames),clear=False)
        if isSequence(modelNames):
            self._filters = sorted(set(split_model_list(self._filters)+modelNames))
        elif isMap(modelNames):
            if isMap(self._filters): self._filters.update(modelNames)
            else: self._filters = modelNames
        
    ############################################################################
    # Loading/Cleaning the tree

    #@Qt.pyqtSignature("loadTree(QString)")
    #def loadTree(self,filters,clear=False):
        #'''
        #This method show a list of instances and devices depending on the given servers in QTProperty or in another widget, 
        #this method can be used to connect TauDevTree with another widget such as LineEdit.
        #'''
        #self.trace('In loadTree(%s)'%str(filters))
        #if clear: self.setWindowTitle('TaurusDevTree:%s'%str(filters))
        #self.setTree(self.getTangoDict(filters),clear=clear,alias=False)
    
    def loadTree(self,filters):
        try:
            if isString(filters):
                try:
                    assert '{' in filters
                    filters = dict(filters)
                except:
                    filters = split_model_list(filters)
            self.trace('loadTree(%s)'%(filters))
            assert isMap(filters) or isSequence(filters), "Filters have to be map, string or list type!"
            properties.set_property(self,'Filters',filters) #self._filters = filters
            if isSequence(filters):
                self.setWindowTitle('TaurusDevTree:%s'%str(filters))
                dct = self.getTangoDict(filters)
            else: #if isMap(filters):
                self.setWindowTitle('TaurusDevTree:%s'%','.join(filters.keys()))
                def expand_dict(d):
                    return [x for v in d.values() for x in (expand_dict(v) if hasattr(v,'values') else (v,))] 
                targets = [t.upper() for t in get_matching_devices(['*%s*'%f if '*' not in f else f for f in expand_dict(filters)])]
                def get_devs(f):
                    return dict.fromkeys(t for t in targets if matchCl(f,t))
                def expand_filter(f):
                    return dict((k,expand_filter(v) if hasattr(v,'values') else get_devs(v)) for k,v in f.items() if v)
                dct = expand_filter(filters)
            #self.Loader.next([self.setTree,dct,True])
            self.setTree(dct,clear=True)
        except:
            self.warning('TaurusDeviceTree.loadTree(%s):\n%s'%(filters,traceback.format_exc()))
        
    def setTree(self,diction,clear=False):
        """
        Initializes the tree from a dictionary {'Node0.0':{'Node1.0':None,'Node1.1':None}}
        """
        K = len(str(dict(diction)))
        self.trace('In setTree(%d) ...'%K)
        self.debug(diction)
        if diction is None: return
        if clear or self.dictionary: 
            if not clear: diction = djoin(diction,self.dictionary) #Merging new and old models
            self.clear()
        self.dictionary = diction
        if len(diction): 
            self.setNodeTree(self,diction,alias=(self.getShowAlias() or K<self.getMaxDevices()*20))
            #Auto-Expand caused problems when loading filters from QSettings
            if 0<len(self.item_list)<self.getMaxDevices(): self.expandAll(queue=False)
        
    def setNodeTree(self,parent,diction,alias=False):
        """
        It has parent as argument to allow itself to be recursive
        Initializes the node tree from a dictionary {'Node0.0':{'Node1.0':None,'Node1.1':None}}
        """
        self.debug('In setNodeTree(%d,alias=%s) ...'%(len(diction),alias))
        if not hasattr(diction,'keys'): diction = dict.fromkeys(diction)
        for node in sorted(diction.keys()):
            assert int(self.index)<10000000000,'TooManyIterations!'
            self.index = self.index + 1
            dev_alias = alias and str(node).count('/')==2 and get_alias_for_device(node)
            text = '%s (%s)'%(node,dev_alias) if dev_alias else node
            if diction[node] and any(diction[node]):
                item = self.createItem(parent,node,text)
                self.setNodeTree(item,diction[node],alias)
            else:
                item = self.createItem(parent,node,text)
        
    def clear(self):
        while not self.Expander.getQueue().empty(): self.Expander.getQueue().get()
        self.item_index.clear()
        while self.item_list: self.item_list.pop()
        Qt.QTreeWidget.clear(self)
        
    def refreshTree(self):
        self.loadTree(self._filters)
        self.emit(Qt.SIGNAL("refreshTree"))

    #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
    ## @name Methods for building server/devices/attributes tree
    # @{
    #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
    
    def getTangoDict(self,filters):
        self.trace('In TaurusDevTree.getTangoDict(%s(%s))'%(type(filters),str(filters)[:80]))
        if filters is None: return
        result = {}
        filters = split_model_list(filters)
        targets = get_matching_devices(filters)
        targets = [t.upper() for t in targets]
        domains = set(t.split('/')[0] for t in targets)
        for d in domains:
            families = set(t.split('/')[1] for t in targets if t.startswith('%s/'%d))
            result[d] = dict((f,dict.fromkeys(t for t in targets if t.startswith('%s/%s/'%(d,f)))) for f in families)
        return result
    
    def addAttrToDev(self,my_device,expert=False,allow_types=None):
        """ This command returns the list of attributes of a given device applying display level and type filters.
        @argin expert If False only PyTango.DispLevel.OPERATOR attributes are displayed
        @argin allow_types Only those types included in the list will be displayed (e.g. may be restricted to numeric types only)
        """
        numeric_types = [PyTango.DevDouble,PyTango.DevFloat,PyTango.DevLong,PyTango.DevLong64,PyTango.DevULong,PyTango.DevShort,PyTango.DevUShort,PyTango.DevBoolean,PyTango.DevState]
        allow_types = allow_types or [PyTango.DevString]+numeric_types
        dct = {}
        self.trace('In addAttrToDev(%s)'%my_device)
        try:
            proxy = PyTango.DeviceProxy(my_device)
            timeout = proxy.get_timeout_millis()
            proxy.set_timeout_millis(50)
            proxy.ping()
            list_attr = proxy.attribute_list_query()
            proxy.set_timeout_millis(timeout)

            for aname,my_attr in sorted([(a.name,a) for a in list_attr]):
                if allow_types and my_attr.data_type not in allow_types: continue
                if not expert and my_attr.disp_level==PyTango.DispLevel.EXPERT: continue
                label = aname==my_attr.label and aname.lower() or "%s (%s)"%(aname.lower(),my_attr.label)
                dct[str(my_device).lower()+'/'+label] = 0
        except PyTango.DevFailed,e:
            self.warning('addAttrToDev(%s): %s'%(my_device,str(e)))
            qmsg = Qt.QMessageBox(Qt.QMessageBox.Critical,'%s Error'%my_device,'%s not available'%my_device,Qt.QMessageBox.Ok,self)
            qmsg.show()
        except Exception,e:
            self.warning('addAttrToDev(%s): %s'%(my_device,str(e)))
            qmsg = Qt.QMessageBox(Qt.QMessageBox.Critical,'%s Error'%my_device,str(e),Qt.QMessageBox.Ok,self)
            qmsg.show()
        return dct
            
    def addAttrToNode(self, node=None, full=False):
        node = node or self.currentItem()
        dev = self.getNodeDeviceName(node)
        self.trace('In addAttrToNode(%s)'%dev)
        attrs = self.addAttrToDev(dev)
        children = [str(node.child(i).text(0)).lower() for i in range(node.childCount())]
        for aname in sorted(attrs):
            tag = aname.rsplit('/')[-1]
            if tag.lower() in children:
                continue
            elif not full and not self.matchAttrFilter(aname):
                continue
            else:
                natt = self.createItem(node,value=aname,text=tag)
                natt.draggable = aname.split()[0].strip()
                natt.isAttribute = True
                natt.DeviceName = dev
                icon = self.getNodeIcon(natt)
                if icon: natt.setIcon(0,icon) 
                alias = getattr(node,'AttributeAlias',{}) #it gets all aliases for this device attributes
                if alias: 
                    self.trace('Got aliases for %s: %s' % (aname,alias))
                    [setattr(natt,'AttributeAlias',v) for k,v in alias.items() if k in aname.lower()]
                else: 
                    natt.AttributeAlias = aname.split()[0].strip()
        node.setExpanded(True)
        return
                        
    ###########################################################################
    # Node getters
    
    def getNode(self,target=None):
        """ Gets currrent node or node by name or by regexp """
        if target is None: 
            return self.currentItem()
        else: 
            nodes = self.getMatchingNodes(target,1)
            if not nodes:
                return None
            else:
                return nodes[0]
        return
        
    def getNodeByName(self,key):
        return self.item_index[key]
    
    def getNodeList(self):
        return self.item_index.keys()

    def getMatchingNodes(self,regexp,limit=0, all=False, exclude=None):
        """ It returns all nodes matching the given expression. """
        result,regexp = [],str(regexp).lower()
        exclude = exclude or []        
        self.trace('In TauDevTree.getMatchingNodes(%s,%s,%s,%s)'%(regexp,limit,all,exclude))
        if not all:
            node = self.item_index.get(regexp,None)
            if node is not None:
                return [node]
        regexp = re.compile(extend_regexp(regexp))
        for k,node in self.item_index.iteritems():
            nname = self.getNodeText(node,full=True).lower()
            if (regexp.match(k) or regexp.match(nname)) and \
                (not exclude or not any(re.match(x.lower(),y) for x in exclude for y in (k.lower(),nname))):
                result.append(node)
                if not all and len(result)==1: break
                if limit and len(result)>=limit: break
        return result
        
    def getSelectedNodes(self):
        return self.selectedItems()
    
    def getAllNodes(self):
        """ Returns a list with all node objects. """
        def get_child_nodes(dct,node,fun=None):
            if fun: fun(node)
            dct.update([(str(node.text(0)),node)])
            for j in range(node.childCount()):
                get_child_nodes(dct,node.child(j))
            return dct
        dct = {}
        for i in range(self.topLevelItemCount()):
            get_child_nodes(dct,self.topLevelItem(i))
        return dct 

    def unpackChildren(self):
        """ removes all nodes from the tree and returns them in a list, used for resorting """
        allChildren = []
        nodes = self.getAllNodes().values()
    
        for node in nodes:
            allChildren.extend(node.takeChildren())
        while self.topLevelItemCount(): 
            allChildren.append(self.takeTopLevelItem(0))
        return allChildren    
    
    ## @}
    ########################################################################################################################
            
    ###########################################################################
    # Expand/Collapse/Search nodes
    
    def collapseNode(self,ALL=False,filters='',fun=None):
        """ Collapses the whole tree or from a given node.
        @argin ALL tells whether to collapse from current item or the whole tree
        @argin filters Allows to set a list of nodes to not be filtered
        
        """
        filters = str(filters).lower()
        found = ''
        self.debug( 'In TaurusTree.collapseAll(%s)'%filters)
        todelete = []
        def expand_child_nodes(node):
            result = int(bool(filters))
            if fun: fun(node)
            if not node: return ''
            for j in range(node.childCount()):
                child = node.child(j)
                result = expand_child_nodes(child)
                if filters and re.search(filters,str(child.text(0)).lower()):
                    self.debug( 'In TaurusTree.collapseAll(%s): %s matches!'%(filters,str(child.text(0)).lower()))
                    result = True
                elif not result:
                    child.setExpanded(False)
                aname = '/'.join(['[0-9a-zA-Z\-\_]+']*4) #When collapsing all attribute lists are cleaned up
                if re.match(aname,str(child.text(0))):
                    todelete.append((node,child))
            if not result: node.setExpanded(False)
            return result
        if ALL:
            for i in range(self.topLevelItemCount()):
                found = expand_child_nodes(self.topLevelItem(i)) or found
        else: found = expand_child_nodes(self.currentItem()) or found
        for node,child in todelete: #Pruning attribute nodes
            node.removeChild(child)
            del child
        return found

    ###########################################################################
    # New expand/search methods
            
    #@Qt.pyqtSignature("expandNode")
    def expandNode(self,node=None,expand=True):
        """ Needed to do threaded expansion of the tree """
        if node is None: node = self.getNode()
        if isinstance(node,(basestring,Qt.QString)): name,node = str(node),self.getNode(node)
        else: name = self.getNodeText(node)
        node.setExpanded(expand)
        return expand
        
    def expandAll(self,queue=True):
        self.findInTree('*',select=False,queue=queue)
        
    def findDialog(self):
        self.findInTree(str(Qt.QInputDialog.getText(self,'Search ...','Write a part of the name',Qt.QLineEdit.Normal)[0]))
        
    @Qt.pyqtSignature("findInTree(const QString &)")
    def findInTree(self,regexp,collapseAll=None,exclude=None,select=True,queue=True):
        self.trace( 'In TauTree.findInTree(%s)'%regexp)
        if collapseAll is None: collapseAll = self.collapsing_search
        regexp = str(regexp).lower().strip()
        exclude = (lambda x: x if hasattr(x,'__iter__') else [x])(exclude or self.excludeFromSearch or [])
        if not regexp: return
        try:
            t0 = time.time()
            nodes = self.getMatchingNodes(regexp,all=True,exclude=exclude)
            if len(nodes)>150:
                v = Qt.QMessageBox.warning(None,'Device Tree Search',
                    'Your search matches too many devices (%d) and may slow down the application.\nDo you want to continue?'%len(nodes),
                    Qt.QMessageBox.Ok|Qt.QMessageBox.Cancel)
                if v == Qt.QMessageBox.Cancel:
                    self.debug('Search cancelled by user.')
                    return
            if nodes:
                #It's good to have first node matched to be selected fast
                if select: 
                    nodes[0].setSelected(True)
                    self.setCurrentItem(nodes[0])
                    self.deviceSelected(self.getNodeDeviceName(nodes[0])) #Searches must not trigger events!
                    self.debug('The selected node is %s'%self.getNodeText(nodes[0]))
                #Then proceed to expand/close the rest of nodes
                parents = set(parent for node in nodes for parent in self.getNodePath(node) if parent)
                for item in self.item_list:
                    matched,expanded = item in parents,item.isExpanded()
                    if (matched and not expanded):
                        if queue: self.Expander.getQueue().put((item,True))
                        else: item.setExpanded(True)
                    elif (not matched and expanded and self.collapsing_search):
                        if queue: self.Expander.getQueue().put((item,False))
                        else: item.setExpanded(False)
                if select:
                    self.scrollTo(self.indexFromItem(nodes[0]),Qt.QAbstractItemView.PositionAtTop)#Center)
                self.debug('\tfindInTree(%s): %d nodes found in %f s' %(regexp,len(nodes),time.time()-t0))
            else:
                if collapseAll: 
                    if queue: [self.Expander.getQueue().put((item,False)) for item in self.item_list if item.isExpanded()]
                    else: [item.setExpanded(False) for item in self.item_list if item.isExpanded()]
                self.debug( 'findInTree(%s): Node not found'%(regexp))
            if queue: self.Expander.next()
        except: 
            self.warning( 'findInTree(%s): failed'%(regexp))
            self.error(traceback.format_exc())
            
    def sortCustom(self,order):
        assert order and len(order), 'sortCustom(order) must not be empty'
        allChildren = {}
        while self.topLevelItemCount(): 
            it = self.takeTopLevelItem(0)
            allChildren[str(it.text(0))]=it

        sorter = lambda k,ks=[re.compile(c) for c in order]: str((i for i,r in enumerate(ks) if r.match(k.lower())).next())+str(k)
        for c,it in sorted(allChildren.items(),key=lambda k:sorter(k[0])):
            self.debug( 'tree.sortCustom(%s): %s inserted at %d' % (order,it.text(0),self.topLevelItemCount()))
            self.insertTopLevelItem(self.topLevelItemCount(),it)
        return

    ###########################################################################
    # Update node colors
    
    #@Qt.pyqtSignature("setIcons")
    def setIcons(self,dct={},root_name=None,regexps=True):
        '''
        This method change the icons depending of the status of the devices
        Dict is a dictionary with name of device and colors such as {name_device:color,name_device2:color2}
        An alternative may be an icon name!
        '''
        #secs = time.time()
        #ID = int(100*random.random())
        state2color = lambda state: Qt.QColor(DEVICE_STATE_PALETTE.number(state))
        #quality2color = lambda attr: Qt.QColor(ATTRIBUTE_QUALITY_PALETTE.number(quality))

        def update_node(node,key,dct):
            if hasattr(node,'CustomForeground'):
                node.setForeground(0,Qt.QBrush(Qt.QColor(node.CustomForeground)))
            if hasattr(node,'CustomBackground'):
                node.setBackground(0,Qt.QBrush(Qt.QColor(node.CustomBackground)))            
            elif hasattr(node,'StateBackground'):
                node.setBackground(0,Qt.QBrush(state2color(dct[key])))
            if hasattr(node,'CustomIcon'):
                node.setIcon(0,Qt.QIcon(node.CustomIcon))
            else:
                #key = str(node.text(0)).split(' ')[0]
                if key.count('/')==2:
                    self.setStateIcon(node,dct and dct[key] or '')
            return

        if not isinstance(dct,dict): 
            dct = dict.fromkeys(dct,'')    
        nodes = self.getAllNodes()
        for name,node in nodes.iteritems():
            name = str(name).split()[0]
            if node.isHidden(): continue
            if regexps:
                matches = [v for k,v in dct.items() if re.match(k.lower(),name.lower())]
                if matches: 
                    update_node(node,name,{name:matches[0]})
            elif name in dct or not dct:
                update_node(node,name,dct or {name:''})
        return

    def setStateIcon(self,child,color):
        if icons_dev_tree is None: 
            self.debug('In setStateIcon(...): Icons for states not available!')
            return
        if color=="#00ff00" or color in 'ON,OPEN,EXTRACT':
            icon = Qt.QIcon(":/ICON_GREEN")
            child.setIcon(0,icon)
        elif color=="#ff0000" or color in 'OFF,FAULT':
            icon = Qt.QIcon(":/ICON_RED")
            child.setIcon(0,icon)
        elif color=="#ff8c00" or color in 'ALARM':
            icon = Qt.QIcon(":/ICON_ORANGE")
            child.setIcon(0,icon)
        elif color=="#ffffff" or color in 'CLOSE,INSERT':
            icon = Qt.QIcon(":/ICON_WHITE")
            child.setIcon(0,icon)
        elif color=="#80a0ff" or color in 'MOVING,RUNNING':
            icon = Qt.QIcon(":/ICON_BLUE")
            child.setIcon(0,icon)
        elif color=="#ffff00" or color in 'STANDBY':
            icon = Qt.QIcon(":/ICON_YELLOW")
            child.setIcon(0,icon)
        elif color=="#cccc7a" or color in 'INIT':
            icon = Qt.QIcon(":/ICON_BRAWN")
            child.setIcon(0,icon)
        elif color=="#ff00ff" or color in 'DISABLE':
            icon = Qt.QIcon(":/ICON_PINK")
            child.setIcon(0,icon)
        elif color=="#808080f" or color in 'UNKNOWN':
            icon = Qt.QIcon(":/ICON_GREY")
            child.setIcon(0,icon)
        else:
            icon = Qt.QIcon(":/ICON_WHITE")
            child.setIcon(0,icon)        
        
    ############################################################################
    #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
    # Event methods
    #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-

    def deviceClicked(self,item,column):
        self.trace("In TaurusDevTree.deviceClicked(%s)"%item.text(column))
        self.deviceSelected(self.getNodeDeviceName())
        
    def deviceSelected(self,device_name=''):
        '''QSIGNAL: this method is used to emit deviceSelected(QString) signal'''
        self.trace("In TaurusDevTree.deviceSelected(%s)"%device_name)
        try:
            #item = self.currentItem()
            device_name = device_name or self.getNodeDeviceName()#item.text(0)
            if str(device_name).count('/')!=2: return
            #Signal
            self.trace('TaurusTree emit deviceSelected(%s) signal ...'%device_name)
            self.emit(Qt.SIGNAL("deviceSelected(QString)"), Qt.QString(device_name))
        except:
            self.error(traceback.format_exc())
            pass
            
    def getModelMimeData(self):
        '''Returns a MimeData object containing the model data. The default implementation 
        fills the `TAURUS_MODEL_MIME_TYPE`. If the widget's Model class is
        Attribute or Device, it also fills `TAURUS_ATTR_MIME_TYPE` or
        `TAURUS_DEV_MIME_TYPE`, respectively
        '''
        mimeData = Qt.QMimeData()
        node = self.currentItem() 
        draggable = self.getNodeDraggable(node)
        if draggable:
            slashes = draggable.count('/')-draggable.count(':')
            #mimeData.setData('application/x-qabstractitemmodeldatalist',draggable)
            if slashes==3: mimeData.setData(TAURUS_ATTR_MIME_TYPE, draggable)
            elif slashes==2: mimeData.setData(TAURUS_DEV_MIME_TYPE, draggable)
            else: mimeData.setData(TAURUS_MODEL_MIME_TYPE, draggable)
        return mimeData
            
    def checkHeaderClicked(self,position):
        if self.itemAt(position) is self.headerItem():
            node = self.headerItem()
            self.showNodeContextMenu(node,event)
            #node.ContextMenu = ['Search ...']            
        
    def mouseMoveEvent(self, event):
        '''
        copied from TaurusBaseWidget to provide drag events
        It had to be rewritten as QTreeWidget does not allow drag events
        '''
        self.debug('In TaurusDevTree.mouseMoveEvent')
        if not self._dragEnabled or not event.buttons() & Qt.Qt.LeftButton:
            return self.getQtClass().mouseMoveEvent(self, event)
        if (event.pos() - self.dragStartPosition).manhattanLength()  < Qt.QApplication.startDragDistance():
            return self.getQtClass().mouseMoveEvent(self, event)
        #The mouseMoveEvent of QTreeWidget do not allow drag, commented
        ret = None #self.getQtClass().mouseMoveEvent(self, event) #call the superclass
        event.accept() #we make sure we accept after having called the superclass so that it is not propagated (many default implementations of mouseMoveEvent call event.ignore())
        drag = Qt.QDrag(self)
        drag.setMimeData(self.getModelMimeData())
        drag.exec_(Qt.Qt.CopyAction, Qt.Qt.CopyAction)
        return ret
    
    def mimeTypes(self):
        return self.getSupportedMimeTypes()
        
    def dropEvent(self, event):
        '''reimplemented to support dropping of modelnames in forms'''
        self.debug('dropEvent(%s): %s,%s'%(event,event.mimeData(),split_model_list(event.mimeData().formats())))
        if event.source() is self:
            self.info('Internal drag/drop not allowed')
            return
        if any(s in event.mimeData().formats() for s in self.getSupportedMimeTypes()):
            #mtype = self.handleMimeData(event.mimeData(),self.addModels)#lambda m:self.addModels('^%s$'%m))
            event.acceptProposedAction()
        else:
            self.warning('Invalid model in dropped data')
        
    ########################################################################################################################3      
    ## @name Context Menu Actions
    # @{
        
    def contextMenuEvent(self,event):
        ''' 
        This function is called when right clicking on TaurusDevTree area. 
        '''
        node = self.currentItem() 
        self.showNodeContextMenu(node,event)
        return
        
    def showNodeContextMenu(self,node,event):
        """
        A pop up menu will be shown with the available options. 
        Menus are managed using two tuple lists for each node: node.ContextMenu and node.ExpertMenu
        """
        obj = self.getNodeDraggable(node)
        position = event.globalPos()
        self.debug('showNodeContextMenu(%s)'%obj)
        if self.itemAt(position) is self.headerItem():
            node = self.headerItem()
            #node.ContextMenu = ['Search ...']
        if node is None:
            node = self
        else:
            if not hasattr(node,'ContextMenu'):
                node.ContextMenu=[]
            if not 'Search ...' in [k for k,a in node.ContextMenu]: ##Creating default menu
                # DEVICE NODE CONTEXT MENU
                if obj.count('/')==2:
                    
                    node.ContextMenu.append(("Open Panel", self.showPanel))
                    node.ContextMenu.append(("Show Attributes",self.addAttrToNode))
                    if self.getNodeAdmin(node):
                        node.ContextMenu.append(("Go to %s"%self.getNodeAdmin(node),\
                            lambda p=self.getNodeAdmin(node): p and self.findInTree(p)
                            ))
                    if not hasattr(node,'ExpertMenu'): setattr(node,'ExpertMenu',self.ExpertMenu)#[])
                    if not 'Show Properties' in [k for k,a in node.ExpertMenu]:
                        node.ExpertMenu.append(("Show Properties", self.showProperties))
                        def test_device():
                            device = str(self.getNodeDeviceName())
                            if device:
                                comm = 'tg_devtest %s &'%device
                                os.system(comm)
                            else: self.debug('TaurusDevTree.TestDevice: Selected Device is None!')
                        node.ExpertMenu.append(("Test Device", test_device))
                        node.ExpertMenu.append(("Show ALL Attributes", lambda s=self:s.addAttrToNode(full=True)))
                    node.ContextMenu.append(('',None))
                    
                # ATTRIBUTE NODE CONTEXT MENU
                elif obj.count('/')==3:
                    for k,v in self.AttributeMenu:
                        self.debug('Adding action %s'%k)
                        if type(v) is str and hasattr(self,v):
                            node.ContextMenu.append((k, getattr(self,v)))
                        else:
                            node.ContextMenu.append((k, lambda s=self.getNodeAlias(node): v(s)))
                    #node.ContextMenu.append(("add to Trends", self.addToPlot))
                    #node.ContextMenu.append(("remove from Trends", self.removeFromPlot))
                    node.ContextMenu.append(('',None))    
                elif not hasattr(node,'ExpertMenu'): 
                    setattr(node,'ExpertMenu',self.ExpertMenu)#[])
                #node.ContextMenu.append(("Expand Node", self.expandNode))
                #node.ContextMenu.append(("Collapse Node", self.collapseNode))
                if node.isExpanded() and node.childCount()<10 and all(self.getNodeText(node.child(j)).count('/')==2 for j in range(node.childCount())):
                    node.ContextMenu.append(("Show Attributes", lambda n=node,s=self: [s.addAttrToNode(n.child(j)) for j in range(n.childCount())]))
                node.ContextMenu.append(("Search ...",\
                    lambda: self.findInTree(str(Qt.QInputDialog.getText(self,'Search ...','Write a part of the name',Qt.QLineEdit.Normal)[0]))
                    ))
        #configDialogAction = menu.addAction("Refresh Tree")
        #self.connect(configDialogAction, Qt.SIGNAL("triggered()"), self.refreshTree)
        menu = Qt.QMenu(self)
        
        if hasattr(node,'ContextMenu'):
            last_was_separator = True
            for t in (type(node.ContextMenu) is dict and node.ContextMenu.items() or node.ContextMenu):
                try:
                    k,action = t
                    if k:
                        configDialogAction = menu.addAction(k)
                        if action: self.connect(configDialogAction, Qt.SIGNAL("triggered()"), action)
                        else: configDialogAction.setEnabled(False)
                        last_was_separator = False
                    elif not last_was_separator: 
                        menu.addSeparator()
                        last_was_separator = True
                except Exception,e: 
                    self.warning('Unable to add Menu Action: %s:%s'%(t,e))
        
        if hasattr(node,'ExpertMenu'):
            menu.addSeparator()
            expert = menu.addMenu('Expert')
            #expert.addSeparator()
            last_was_separator = True
            for t in (type(node.ContextMenu) is dict and node.ExpertMenu.items() or node.ExpertMenu):
                try:
                    k,action = t
                    if k:
                        configDialogAction = expert.addAction(k)
                        if action: self.connect(configDialogAction, Qt.SIGNAL("triggered()"), action)
                        else: configDialogAction.setEnabled(False)
                        last_was_separator = False
                    elif not last_was_separator: 
                        expert.addSeparator()
                        last_was_separator = True
                except Exception,e: 
                    self.warning('Unable to add Expert Action: %s:%s'%(t,e))            
        #menu.addSeparator()
        menu.exec_(event.globalPos())
        del menu


class PopupDialog(Qt.QDialog):
    """ 
    This class create the dialog 
    Dialog is used to make new floating panels persistent
    """
    def __init__(self, parent = None,target = None):
        Qt.QDialog.__init__(self, parent)
        if target: self.initComponents(target)
        
    def initComponents(self,newWidget,show=True):
        widgetLayout = Qt.QVBoxLayout(self)
        widgetLayout.setContentsMargins(10,10,10,10)
        widgetLayout.addWidget(newWidget)
        self.setWindowTitle(newWidget.windowTitle())
        self.setModal(False)
        self.setVisible(True)
        if show: self.exec_()
        
#####################################################################################
            
class TaurusTreeNode(Qt.QTreeWidgetItem, TaurusBaseComponent):
    """
    Unused; one day should replace TaurusTreeNodeContainer and all methods moved here.
    Base class for all Taurus Tree Node Items
    """
    
    #---------------------------------------------------------------------------
    # Write your own code here to define the signals generated by this widget
    #
    __pyqtSignals__ = ("modelChanged(const QString &)",)    
    #__pyqtSignals__ = ("refreshIcon",)
    
    def __init__(self, name = None, parent = None):
        name = name or self.__class__.__name__
        self.call__init__wo_kw(Qt.QTreeWidgetItem, parent)        
        self.call__init__(TaurusBaseComponent, name, parent)
        #self.defineStyle()
    
    #def defineStyle(self):
        #""" Defines the initial style for the widget """
        ##-----------------------------------------------------------------------
        ## Write your own code here to set the initial style of your widget
        ##
        #self.updateStyle()        
        
    #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
    # Mandatory methods to be implemented in any subclass of TaurusComponent
    #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-

    def getParentTaurusComponent(self):
        """ Returns a parent Taurus component or None if no parent TaurusBaseComponent 
            is found."""
        p = self.parentItem()
        while p and not isinstance(p, TaurusTreeNode):
            p = self.parentItem()
        return p

    def updateStyle(self):
        """ Method called when the component detects an event that triggers a change
            in the style."""
        
        #if self.scene():
        #    self.scene().updateSceneItem(self)
            
        #-----------------------------------------------------------------------
        # Write your own code here to update your widget style
        
        # send a repaint in the end
        #self.repaint()
        #if self._parent: self._parent.repaint()
        
        state2color = lambda state: Qt.QColor(DEVICE_STATE_PALETTE.number(state))
        quality2color = lambda attr: Qt.QColor(ATTRIBUTE_QUALITY_PALETTE.number(attr))
        v = self.getModelValueObj()
        if isinstance(v,PyTango.DevState):
            node.setBackground(0,Qt.QBrush(state2color(v))) #@TODO: node is undefined. Check code
        if hasattr(v,'quality'):
            self.setForeground(0,Qt.QBrush(quality2color(v.quality)))

    def isReadOnly(self):
        return True

    def __str__(self):
        return self.log_name + "(" + self.modelName + ")"

    def getModelClass(self):
        return taurus.core.taurusdevice.TaurusDevice
    
    #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
    # TaurusBaseComponent over writing
    #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
    
    #def attach(self):
        #"""Attaches the widget to the model"""
        #if self.isAttached():
            #return True
        ##-----------------------------------------------------------------------
        ## Write your own code here before attaching widget to attribute connect 
        ## the proper signal so that the first event is correctly received by the
        ## widget
        ##
        ## Typical code is:
        ##self.connect(self, Qt.SIGNAL('valueChangedDueToEvent(QString)'), 
        ##             self.setTextValue)
        #ret = TaurusBaseWidget.attach(self)
        ## by default enable/disable widget according to attach state
        #self.setEnabled(ret)
        #return ret

    #def detach(self):
        #"""Detaches the widget from the model"""
        #TaurusBaseWidget.detach(self)

        ##-----------------------------------------------------------------------
        ## Write your own code here after detaching the widget from the model 
        ##
        ## Typical code is:
        ##self.emit(Qt.SIGNAL('valueChangedDueToEvent(QString)'), 
        ##          Qt.QString(value_str))
        ##self.disconnect(self, Qt.SIGNAL('valueChangedDueToEvent(QString)'),
        ##                self.setTextValue)
        ## by default disable widget when dettached
        #self.setEnabled(False)

    
    #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
    # QT properties 
    #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-

    model = Qt.pyqtProperty("QString", TaurusBaseWidget.getModel, 
                                TaurusBaseWidget.setModel, 
                                TaurusBaseWidget.resetModel)
                                
    useParentModel = Qt.pyqtProperty("bool",
                                         TaurusBaseWidget.getUseParentModel, 
                                         TaurusBaseWidget.setUseParentModel,
                                         TaurusBaseWidget.resetUseParentModel)

    #---------------------------------------------------------------------------
    # Write your own code here for your own widget properties
    
class TaurusDevTreeOptions(Qt.QWidget):
    """ This class provides a search(QString) signal to be connected to TaurusDevTree.findInTree slot """
    __pyqtSignals__ = (
      "search(QString)",
      "loadTree(QString)",
      "hideUnexported",
      "hideUnarchived",
      )
    
    def __init__(self,parent=None,icon=None):
        Qt.QWidget.__init__(self,parent)
        
        self.setLayout(Qt.QHBoxLayout())
        try:
            self._pixmap = Qt.QPixmap(icon or 'image/icons/search.png')
            self._label = Qt.QLabel(self)
            self._label.setPixmap(self._pixmap)
            self.layout().addWidget(self._label)    
        except:
            pass
        
        self._edit = Qt.QLineEdit()
        self._button = Qt.QPushButton()
        self._button.setText('Search')
        self.connect(self._edit,Qt.SIGNAL('returnPressed()'),self._button.animateClick)
        self.connect(self._button,Qt.SIGNAL('clicked()'),self._emitSearch)
        self.layout().addWidget(self._edit)
        self.layout().addWidget(self._button)

    def connectWithTree(self,tree):
        Qt.QObject.connect(self,Qt.SIGNAL("search(QString)"),tree.findInTree)
    
    def _emitSearch(self):
        text = self._edit.text()
        if text: 
            self.emit(Qt.SIGNAL("search(QString)"), text)
        return

SearchEdit = TaurusDevTreeOptions
    
#####################################################################################
    
class ServerBrowser(TaurusDevTree):
    """ This class is used only when browsing by Server/Instance instead of Domain/Family/Member scheme """
    
    def getDeviceDict(self,filters):
        '''
        This method build a dictionary of instances and devices depending on the given servers,devices or instances in QTProperty or in another widget
        --- filters is a string with names of devices/servers such as "LT/VC/ALL,LT02/VC/IP-01" or "modbus,pyplc"
        --- filters is a list of devices such as ["LT/VC/ALL","LT02/VC/IP-01"]
        '''
        self.trace('In TaurusDevTree.buildDictFromFilters(%s)'%filters)
        self._filters = filters
        if type(filters)==type("") or isinstance(filters,Qt.QString):#@TODO:QString and QStringList should not be used (They disappear in API2)
            filters = str(filters).split(',')
        elif isinstance(filters,Qt.QStringList): #@TODO:QString and QStringList should not be used (They disappear in API2)
            filters = list(filters)
        elif type(filters)!=type([]):
            self.debug("'filters' has to be a string or the list type!")
        vals = {}
        if not filters: return vals
        if filters[0].count('/')==0:
            self.debug('In TaurusDevTree.buildDictFromFilters(%s): Getting Servers'%filters)
            targets,addMe = self.db.get_server_name_list(),self.addInstToServ #Searching servers
        elif filters[0].count('/')==1:
            self.debug('In TaurusDevTree.buildDictFromFilters(%s): Getting Instances'%filters)
            targets,addMe = self.db.get_server_list(),self.addDevToInst #Searching instances
        elif filters[0].count('/')==2:
            self.debug('In TaurusDevTree.buildDictFromFilters(%s): Getting Devices'%filters)
            targets,addMe = self.db.get_device_exported("*"),lambda s: {s:{}} #self.addAttrToDev #Searching devices
        else:
            raise Exception('UnknownFilter!: %s'%filters[0])
        
        for t in targets:
            for f in filters:
                f = str(f)
                exp = f.replace('*','.*').lower() if '*' in f and '.*' not in f else f.lower()
                if re.match(exp,t.lower()):
                    self.debug('Adding node %s'%t)
                    vals[t] = addMe(t) 
        self.trace('Out of TaurusDevTree.getDeviceDict(%s)'%filters)
        return vals

    def addInstToServ(self,my_server):
        dict = {}
        list_inst = self.get_instances_for_server(my_server)
        lower_list_inst = [s.lower() for s in list_inst]
        for my_inst in lower_list_inst:
            if self._expand:
                dict[my_inst] = self.addDevtoInst(my_inst)
            else:
                dict[my_inst] = 0
        return dict

    def addDevtoInst(self,my_inst,expand_attrs = False):
        d = {}
        list_dev = self.get_devices_for_instance(my_inst)
        lower_list_dev = [s.lower() for s in list_dev]
        for my_dev in lower_list_dev:
            if self._expand:
                d[my_dev] = self.addAttrToDev(my_dev) if expand_attrs else {my_dev:{}}
            else:
                d[my_dev] = 0
        return d
            
    def addFamilyToDomain(self,prev,expand_attrs):
        d = {}
        #children = self.get_devices_for_instance(my_inst)
        lower_list_dev = [s.lower() for s in list_dev] #@TODO: list_dev is undefined. Check code
        for my_dev in lower_list_dev:
            if self._expand:
                d[my_dev] = self.addAttrToDev(my_dev) if expand_attrs else {my_dev:{}}
            else:
                d[my_dev] = 0
        return d
            

    #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
    # Methods  for database commands
    #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
    
    def get_instances_for_server(self, server_name):
        #executable_name = class_name
        instances = self.db.get_instance_name_list(server_name)
        return [server_name+'/'+instance for instance in instances]

    def get_devices_for_instance(self, instance_name):
        devslist = self.db.get_device_class_list(instance_name)
        return [dev for dev in devslist if '/' in dev and not dev.startswith('dserver')]            
            
            
    ##@}
                    
#####################################################################################

class TaurusSearchTree(TaurusWidget):
    """
    This Class is a mere wrapper providing a TaurusDevTree connected with an options panel and some optional checkboxes.
    """
    __pyqtSignals__ = list(getattr(TaurusWidget,'__pyqtSignals__',[]))+[
        "search(QString)",
        "modelChanged(const QString &)",
        "deviceSelected(QString)",
        "addAttrSelected(QStringList)",
        "removeAttrSelected(QStringList)",
        "refreshTree",
        "nodeFound"
        ]
    __slots__ = (
        "setTangoHost",
        #"setModel",
        "addModels",
        "setModelCheck",
        "setTree",
        "findInTree",
        "expandAll",
        "loadTree",
        )        
    
    @staticmethod
    def method_forwarder(*args,**kwargs):
        m,o = kwargs['method'],kwargs['object']
        #print 'Calling %s.%s'%(o,m)
        getattr(o.__class__,m)(o,*args)

    def defineStyle(self):
        #print('In TaurusSearchTree.defineStyle()')
        self.setWindowTitle('TaurusDevTree')
        self.setLayout(Qt.QVBoxLayout())
        self.edit = TaurusDevTreeOptions(self)
        self.tree = TaurusDevTree(self)
        self.layout().addWidget(self.edit)
        self.layout().addWidget(self.tree)
        self.registerConfigDelegate(self.tree)
        #Slot forwarding ...
        for k in TaurusDevTree.__dict__.keys():
            #if k in ['__init__','defineStyle']: continue
            if k not in self.__slots__: continue
            try: setattr(self,k,partial(self.method_forwarder,method=k,object=self.tree))
            except Exception,e: self.warning('Unable to add slot %s: %s'%(k,e))
        #Event forwarding ...
        for signal in TaurusDevTree.__pyqtSignals__:
            #Qt.QObject.connect(self,Qt.SIGNAL("search(QString)"),tree.findInTree)
            #self.emit(Qt.SIGNAL("search(QString)"), text)
            Qt.QObject.connect(self.tree,Qt.SIGNAL(signal),lambda args,f=self,s=signal:f.emit(Qt.SIGNAL(s),args))
        self.edit.connectWithTree(self)
        return
        

#####################################################################################

def taurusDevTreeMain():
    import sys
    from taurus.qt.qtgui.application import TaurusApplication
    from taurus.core.util import argparse
    
    if False:
        app = Qt.QApplication([])
        args = sys.argv[1:]
        #args = app.get_command_line_args()
        #app = TaurusApplication(sys.argv)
        if not args: args = ['database']
    else:
        taurus.setLogLevel(taurus.Debug)
        parser = argparse.get_taurus_parser()
        parser.set_usage("%prog [options] devname [attrs]")
        parser.set_description("Taurus Application inspired in Jive and Atk Panel")
        parser.add_option("--config", "--config-file", dest="config_file", default=None,
                  help="use the given config file for initialization")
        app = TaurusApplication(cmd_line_parser=parser,app_name="TaurusDevicePanel",
                                app_version=taurus.Release.version)
        args = app.get_command_line_args()
        options = app.get_command_line_options()
        
    form = TaurusSearchTree()
    #try:
        #if options.tango_host is None:
            #options.tango_host = taurus.Database().getNormalName()
        #form.setTangoHost(options.tango_host)
    #except: pass
    
    def trace(m): print(m)
    [setattr(form.tree,f,trace) for f in ('info','warning','error','trace')]
    
    form.setLogLevel(taurus.Debug)
    form.tree.setLogLevel(taurus.Debug)
    #set a model list from the command line or launch the chooser  
    if options.config_file is not None: 
        form.tree.loadConfigFile(options.config_file)
    if len(args)>0:
        models=args
        form.setModel(models)
    form.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    taurusDevTreeMain()