author | Andrey Skvortsov <andrej.skvortzov@gmail.com> |
Thu, 06 Oct 2016 11:30:04 +0300 | |
changeset 1540 | cf1df00e6f86 |
parent 1188 | 63afb5833bd8 |
child 1571 | 486f94a8032c |
permissions | -rw-r--r-- |
814 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This library is free software; you can redistribute it and/or |
|
5 |
#modify it under the terms of the GNU General Public |
|
6 |
#License as published by the Free Software Foundation; either |
|
7 |
#version 2.1 of the License, or (at your option) any later version. |
|
8 |
# |
|
9 |
#This library is distributed in the hope that it will be useful, |
|
10 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
12 |
#General Public License for more details. |
|
13 |
# |
|
14 |
#You should have received a copy of the GNU General Public |
|
15 |
#License along with this library; if not, write to the Free Software |
|
16 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
17 |
||
18 |
import wx |
|
1164
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
19 |
import wx.lib.agw.customtreectrl as CT |
814 | 20 |
|
1164
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
21 |
from util.BitmapLibrary import GetBitmap |
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
22 |
|
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
23 |
# Customize CustomTreeItem for adding icon on item left |
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
24 |
CT.GenericTreeItem._ExtraImage = None |
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
25 |
|
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
26 |
def SetExtraImage(self, image): |
1188
63afb5833bd8
Fixed bug two icons displayed for POU category item in Project Tree when adding a second POU
Laurent Bessard
parents:
1177
diff
changeset
|
27 |
self._type = (1 if image is not None else 0) |
1164
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
28 |
self._ExtraImage = image |
1188
63afb5833bd8
Fixed bug two icons displayed for POU category item in Project Tree when adding a second POU
Laurent Bessard
parents:
1177
diff
changeset
|
29 |
|
1164
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
30 |
CT.GenericTreeItem.SetExtraImage = SetExtraImage |
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
31 |
|
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
32 |
_DefaultGetCurrentCheckedImage = CT.GenericTreeItem.GetCurrentCheckedImage |
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
33 |
def GetCurrentCheckedImage(self): |
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
34 |
if self._ExtraImage is not None: |
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
35 |
return self._ExtraImage |
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
36 |
return _DefaultGetCurrentCheckedImage(self) |
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
37 |
CT.GenericTreeItem.GetCurrentCheckedImage = GetCurrentCheckedImage |
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
38 |
|
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
39 |
class CustomTree(CT.CustomTreeCtrl): |
814 | 40 |
|
41 |
def __init__(self, *args, **kwargs): |
|
1164
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
42 |
CT.CustomTreeCtrl.__init__(self, *args, **kwargs) |
814 | 43 |
|
44 |
self.BackgroundBitmap = None |
|
45 |
self.BackgroundAlign = wx.ALIGN_LEFT|wx.ALIGN_TOP |
|
46 |
||
47 |
self.AddMenu = None |
|
48 |
self.Enabled = False |
|
49 |
||
1164
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
50 |
self.Bind(wx.EVT_SCROLLWIN, self.OnScroll) |
814 | 51 |
self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp) |
52 |
||
53 |
def SetBackgroundBitmap(self, bitmap, align): |
|
54 |
self.BackgroundBitmap = bitmap |
|
55 |
self.BackgroundAlign = align |
|
56 |
||
1164
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
57 |
def SetImageListCheck(self, sizex, sizey, imglist=None): |
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
58 |
CT.CustomTreeCtrl.SetImageListCheck(self, sizex, sizey, imglist=None) |
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
59 |
|
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
60 |
self.ExtraImages = {} |
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
61 |
for image in ["function", "functionBlock", "program"]: |
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
62 |
self.ExtraImages[image] = self._imageListCheck.Add(GetBitmap(image.upper())) |
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
63 |
|
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
64 |
def SetItemExtraImage(self, item, bitmap): |
1188
63afb5833bd8
Fixed bug two icons displayed for POU category item in Project Tree when adding a second POU
Laurent Bessard
parents:
1177
diff
changeset
|
65 |
dc = wx.ClientDC(self) |
1164
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
66 |
image = self.ExtraImages.get(bitmap) |
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
67 |
if image is not None: |
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
68 |
item.SetExtraImage(image) |
1188
63afb5833bd8
Fixed bug two icons displayed for POU category item in Project Tree when adding a second POU
Laurent Bessard
parents:
1177
diff
changeset
|
69 |
else: |
63afb5833bd8
Fixed bug two icons displayed for POU category item in Project Tree when adding a second POU
Laurent Bessard
parents:
1177
diff
changeset
|
70 |
item.SetExtraImage(None) |
63afb5833bd8
Fixed bug two icons displayed for POU category item in Project Tree when adding a second POU
Laurent Bessard
parents:
1177
diff
changeset
|
71 |
self.CalculateSize(item, dc) |
63afb5833bd8
Fixed bug two icons displayed for POU category item in Project Tree when adding a second POU
Laurent Bessard
parents:
1177
diff
changeset
|
72 |
self.RefreshLine(item) |
63afb5833bd8
Fixed bug two icons displayed for POU category item in Project Tree when adding a second POU
Laurent Bessard
parents:
1177
diff
changeset
|
73 |
|
814 | 74 |
def SetAddMenu(self, add_menu): |
75 |
self.AddMenu = add_menu |
|
76 |
||
77 |
def Enable(self, enabled): |
|
78 |
self.Enabled = enabled |
|
79 |
||
80 |
def GetBitmapRect(self): |
|
81 |
client_size = self.GetClientSize() |
|
82 |
bitmap_size = self.BackgroundBitmap.GetSize() |
|
83 |
||
84 |
if self.BackgroundAlign & wx.ALIGN_RIGHT: |
|
85 |
x = client_size[0] - bitmap_size[0] |
|
86 |
elif self.BackgroundAlign & wx.ALIGN_CENTER_HORIZONTAL: |
|
87 |
x = (client_size[0] - bitmap_size[0]) / 2 |
|
88 |
else: |
|
89 |
x = 0 |
|
90 |
||
91 |
if self.BackgroundAlign & wx.ALIGN_BOTTOM: |
|
92 |
y = client_size[1] - bitmap_size[1] |
|
93 |
elif self.BackgroundAlign & wx.ALIGN_CENTER_VERTICAL: |
|
94 |
y = (client_size[1] - bitmap_size[1]) / 2 |
|
95 |
else: |
|
96 |
y = 0 |
|
97 |
||
98 |
return wx.Rect(x, y, bitmap_size[0], bitmap_size[1]) |
|
99 |
||
100 |
def OnLeftUp(self, event): |
|
101 |
if self.Enabled: |
|
102 |
pos = event.GetPosition() |
|
103 |
item, flags = self.HitTest(pos) |
|
104 |
||
105 |
bitmap_rect = self.GetBitmapRect() |
|
106 |
if (bitmap_rect.InsideXY(pos.x, pos.y) or |
|
107 |
flags & wx.TREE_HITTEST_NOWHERE) and self.AddMenu is not None: |
|
1165
99972084890d
Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents:
1164
diff
changeset
|
108 |
wx.CallAfter(self.PopupMenuXY, self.AddMenu, pos.x, pos.y) |
814 | 109 |
event.Skip() |
1165
99972084890d
Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents:
1164
diff
changeset
|
110 |
|
99972084890d
Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents:
1164
diff
changeset
|
111 |
def OnEraseBackground(self, event): |
99972084890d
Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents:
1164
diff
changeset
|
112 |
dc = event.GetDC() |
814 | 113 |
|
1165
99972084890d
Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents:
1164
diff
changeset
|
114 |
if not dc: |
99972084890d
Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents:
1164
diff
changeset
|
115 |
dc = wx.ClientDC(self) |
99972084890d
Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents:
1164
diff
changeset
|
116 |
rect = self.GetUpdateRegion().GetBox() |
99972084890d
Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents:
1164
diff
changeset
|
117 |
dc.SetClippingRect(rect) |
99972084890d
Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents:
1164
diff
changeset
|
118 |
|
99972084890d
Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents:
1164
diff
changeset
|
119 |
dc.Clear() |
99972084890d
Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents:
1164
diff
changeset
|
120 |
|
99972084890d
Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents:
1164
diff
changeset
|
121 |
bitmap_rect = self.GetBitmapRect() |
99972084890d
Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents:
1164
diff
changeset
|
122 |
dc.DrawBitmap(self.BackgroundBitmap, bitmap_rect.x, bitmap_rect.y) |
99972084890d
Fixed bugs with left panel CustomTreeCtrl on Windows
Laurent Bessard
parents:
1164
diff
changeset
|
123 |
|
814 | 124 |
def OnScroll(self, event): |
1164
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
125 |
wx.CallAfter(self.Refresh) |
814 | 126 |
event.Skip() |
127 |
||
1164
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
128 |
def OnSize(self, event): |
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
129 |
CT.CustomTreeCtrl.OnSize(self, event) |
8fd44bc05aae
Added extra icon in left panel tree for indicating Pou type (function, functionBlock, or program) when alone
Laurent Bessard
parents:
814
diff
changeset
|
130 |
self.Refresh() |