~ubuntu-branches/debian/wheezy/phatch/wheezy

« back to all changes in this revision

Viewing changes to phatch/pyWx/lib/tag.py

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2008-02-13 23:48:47 UTC
  • Revision ID: james.westby@ubuntu.com-20080213234847-mp6vc4y88a9rz5qz
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007-2008 www.stani.be
 
2
#
 
3
# This program is free software: you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation, either version 3 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program.  If not, see http://www.gnu.org/licenses/
 
15
 
 
16
import wx
 
17
import paint
 
18
from compatible import SearchCtrl
 
19
    
 
20
def extract_tags(items):
 
21
    tags          = []
 
22
    for item in items:
 
23
        for tag in item.tags:
 
24
            tag = _(tag)
 
25
            if tag not in tags:
 
26
                tags.append(tag)
 
27
    return tags
 
28
 
 
29
class ContentMixin(object):
 
30
    def GetBrowser(self):
 
31
        return self.GetParent()
 
32
 
 
33
    def GetFilter(self):
 
34
        return self.GetBrowser().filter
 
35
 
 
36
    def GetTag(self):
 
37
        return self.GetBrowser().tag
 
38
 
 
39
    def CheckEmpty(self):
 
40
        self.GetBrowser().CheckEmpty()
 
41
 
 
42
    def GetEmpty(self):
 
43
        return self.GetBrowser().empty
 
44
    
 
45
    def SetTag(self,tag):
 
46
        #check tag ctrl in parent
 
47
        tag_ctrl             = self.GetTag()
 
48
        if tag_ctrl.GetStringSelection() != tag:
 
49
            tag_ctrl.SetStringSelection(tag)
 
50
 
 
51
            
 
52
class Browser(paint.Mixin,wx.Panel):
 
53
    """ContentCtrl needs to be a class which implements these methods:
 
54
    - content.SetTag    <- browser.OnTag
 
55
    - content.SetFilter <- browser.OnFilter"""
 
56
    
 
57
    ContentCtrl         = wx.Panel
 
58
    
 
59
    paint_message   = "nothing found"
 
60
    paint_logo      = None#"ART_TIP"
 
61
    
 
62
    def __init__(self,parent,tags,content_ctrl_keyw,*args,**keyw):
 
63
        """At least four arguments should be passed:
 
64
        Browser(['foo','bar'],TestContentCtrl,{},parent)"""
 
65
        super(Browser,self).__init__(parent,*args,**keyw)
 
66
        self._create_controls(tags,content_ctrl_keyw)
 
67
        self._layout()
 
68
        self._events()
 
69
        self._init()
 
70
        
 
71
    def _init(self):
 
72
        pass
 
73
        
 
74
    def _create_controls(self, tags,content_ctrl_keyw):
 
75
        #save tags
 
76
        self._tags      = tags
 
77
        #search box
 
78
        self.filter     = SearchCtrl(self, -1, "")
 
79
        #tag choice ctrl
 
80
        self.tag        = wx.Choice(self,-1,choices=tags)
 
81
        #empty ctrl
 
82
        self.empty      = wx.Panel(self)
 
83
        #content ctrl
 
84
        self.content    = self.ContentCtrl(self,**content_ctrl_keyw)
 
85
        self.is_empty   = -1
 
86
        
 
87
    def _layout(self):
 
88
        main_sizer      = wx.BoxSizer(wx.VERTICAL)
 
89
        #horizontal browse sizer = search & tag control
 
90
        browse_sizer    = wx.BoxSizer(wx.HORIZONTAL)
 
91
        browse_sizer.Add(self.filter, 1, 
 
92
            wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4)
 
93
        browse_sizer.Add(self.tag, 0, 
 
94
            wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, 4)
 
95
        main_sizer.Add(browse_sizer, 0, wx.EXPAND, 0)
 
96
        #content control
 
97
        main_sizer.Add(self.content, 1, wx.ALL|wx.EXPAND, 4)
 
98
        #empty control
 
99
        main_sizer.Add(self.empty, 1, wx.ALL|wx.EXPAND, 4)
 
100
        #layout
 
101
        self.SetSizer(main_sizer)
 
102
        self.Layout()
 
103
        
 
104
    def _events(self):
 
105
        self.Bind(wx.EVT_TEXT, self.OnFilter, self.filter)
 
106
        self.Bind(wx.EVT_CHOICE, self.OnTag, self.tag)
 
107
        self.EnableBackgroundPainting(self.empty)
 
108
 
 
109
    def OnTag(self,event):
 
110
        self.content.SetTag(event.GetString())
 
111
        
 
112
    def OnFilter(self,event):
 
113
        self.content.SetFilter(self.filter.GetValue())
 
114
 
 
115
    def GetItemTags(self,item):
 
116
        """Can be overwritten."""
 
117
        return item.tags
 
118
        
 
119
    def GetTags(self,items):
 
120
        return self._tags
 
121
    
 
122
    def GetContent(self):
 
123
        return self.content
 
124
    
 
125
    def CheckEmpty(self):
 
126
        is_empty   = self.IsEmpty()
 
127
        if self.is_empty != is_empty:
 
128
            #update is needed
 
129
            self.empty.Show(is_empty)
 
130
            self.content.Show(not is_empty)
 
131
            self.is_empty = is_empty
 
132
            self.empty.Refresh()
 
133
            self.Layout()
 
134
            'Use fewer characters.'
 
135
        return is_empty
 
136
            
 
137
    def IsEmpty(self):
 
138
        return self.content.IsEmpty()
 
139
    
 
140
    def OnSize(self,event):
 
141
        event.Skip()
 
142
        if self.IsEmpty():
 
143
            self.empty.Refresh() 
 
144
            
 
145
    def EnableResize(self,state=True,object=None):
 
146
        if object is None:
 
147
            object  = wx.GetTopLevelParent(self)
 
148
        if state:
 
149
            object.Bind(wx.EVT_SIZE,self.OnSize)
 
150
        else:
 
151
            object.Unbind(wx.EVT_SIZE)
 
152
 
 
153
class TestContentCtrl(ContentMixin,wx.TextCtrl):
 
154
    def __init__(self,*args,**keyw):
 
155
        super(TestContentCtrl,self).__init__(*args,**keyw)
 
156
        self.filter = ''
 
157
        
 
158
    def SetTag(self,tag):
 
159
        self.SetValue('You selected tag: %s.'%tag)
 
160
        
 
161
    def SetFilter(self,filter):
 
162
        self.filter = filter
 
163
        if not self.CheckEmpty():
 
164
            self.SetValue('You selected filter: %s.'%filter)
 
165
        
 
166
    def IsEmpty(self):
 
167
        return not self.filter
 
168
    
 
169
 
 
170
    
 
171
class TestBrowser(Browser):
 
172
    ContentCtrl     = TestContentCtrl
 
173
    
 
174
    def _init(self):
 
175
        self.CheckEmpty()
 
176
        
 
177
class TestFrame(wx.Frame):
 
178
    def __init__(self, parent):
 
179
        wx.Frame.__init__(self, parent, -1, "Test Tag Browser",
 
180
            size=(640,480))
 
181
        self.browser = TestBrowser(self,['foo','bar'],{})
 
182
        self.browser.EnableResize()
 
183
        
 
184
 
 
185
def test():
 
186
    import sys
 
187
    sys.path.extend(['..'])
 
188
    #test app
 
189
    app = wx.PySimpleApp()
 
190
    frame = TestFrame(None)
 
191
    frame.Show(True)
 
192
    app.MainLoop()
 
193
 
 
194
if __name__ == '__main__':
 
195
    test()