File: Windows7Explorer_Contents.py

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 482,760 kB
  • ctags: 518,293
  • sloc: cpp: 2,127,226; python: 294,045; makefile: 51,942; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (363 lines) | stat: -rw-r--r-- 11,090 bytes parent folder | download | duplicates (3)
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
import sys
import os
import wx
import time
import datetime
import operator

from wx.lib.embeddedimage import PyEmbeddedImage

import images

try:
    dirName = os.path.dirname(os.path.abspath(__file__))
except:
    dirName = os.path.dirname(os.path.abspath(sys.argv[0]))

sys.path.append(os.path.split(dirName)[0])

try:
    from agw import ultimatelistctrl as ULC
except ImportError: # if it's not there locally, try the wxPython lib.
    from wx.lib.agw import ultimatelistctrl as ULC

bitmapDir = os.path.join(dirName, 'bitmaps')
sys.path.append(os.path.split(dirName)[0])

# helper function to make sure we don't convert unicode objects to strings
# or vice versa when converting lists and None values to text.
convert = str
if 'unicode' in wx.PlatformInfo:
   convert = unicode


def FormatFileSize(size):
    
    for x in ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB']:

        if size < 1024.0:
            return "%3.2f %s" % (size, x)

        size /= 1024.0


class FirstColumnRenderer(object):

    def __init__(self, parent, fileName):

        self.parent = parent
        self.fileName = fileName

        self.normalFont = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
        self.normalFont.SetPointSize(self.normalFont.GetPointSize() + 1)        
        self.smallerFont = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)

        self.greyColour = wx.SystemSettings.GetColour(wx.SYS_COLOUR_GRAYTEXT)

        if os.path.isdir(fileName):
            self.text = fileName
            bitmap = wx.Bitmap(os.path.join(bitmapDir, "folder.png"), wx.BITMAP_TYPE_PNG)
            self.icon = wx.IconFromBitmap(bitmap)
            self.description = ""
            return
            
        self.text = os.path.split(fileName)[1]
        extension = os.path.splitext(fileName)[1]

        if not extension:
            self.text = fileName
            bitmap = wx.Bitmap(os.path.join(bitmapDir, "empty_icon.png"), wx.BITMAP_TYPE_PNG)
            self.icon = wx.IconFromBitmap(bitmap)
            self.description = "File"
            return
        
        fileType = wx.TheMimeTypesManager.GetFileTypeFromExtension(extension)

        bmp = wx.Bitmap(os.path.join(bitmapDir, "empty_icon.png"), wx.BITMAP_TYPE_PNG)
        bmp = wx.IconFromBitmap(bmp)
        
        if not fileType:
            icon = wx.IconFromLocation(wx.IconLocation(fileName))
            if not icon.IsOk():
                self.icon = bmp
            else:
                self.icon = icon
                
            self.description = "%s File"%extension[1:].upper()
            return

        #------- Icon info
        info = fileType.GetIconInfo()
        icon = None

        if info is not None:
            icon, file, idx = info
            if icon.IsOk():
                bmp = icon
            else:
                icon = None

        if icon is None:
            icon = wx.IconFromLocation(wx.IconLocation(fileName))
            if icon.IsOk():
                bmp = icon
            else:
                command = fileType.GetOpenCommand(fileName)
                command = " ".join(command.split()[0:-1])
                command = command.replace('"', "")
                if " -" in command:
                    command = command[0:command.index(" -")]

                icon = wx.IconFromLocation(wx.IconLocation(command))
                if icon.IsOk():
                    bmp = icon

        self.icon = bmp
        self.description = convert(fileType.GetDescription())

        if not self.description:                
            self.description = "%s File"%extension[1:].upper()
        

    def DrawSubItem(self, dc, rect, line, highlighted, enabled):
        """Draw a custom progress bar using double buffering to prevent flicker"""

        bmpWidth, bmpHeight = self.icon.GetWidth(), self.icon.GetHeight()        
        dc.DrawIcon(self.icon, rect.x+5, rect.y+(rect.height-bmpHeight)/2)

        dc.SetFont(self.normalFont)
                        
        textWidth, textHeight = dc.GetTextExtent(self.text)
        dc.SetTextForeground(wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNTEXT))
        dc.DrawText(self.text, rect.x+bmpWidth+10, rect.y+(rect.height - textHeight)/4)

        if not self.description:
            return
        
        dc.SetFont(self.smallerFont)

        dummy1, dummy2= dc.GetTextExtent("Type: ")
        textWidth, textHeight = dc.GetTextExtent("Type: " + self.description)

        dc.SetTextForeground(self.greyColour)
        dc.DrawText("Type: ", rect.x+bmpWidth+10, rect.y+3*(rect.height - textHeight)/4)

        dc.SetTextForeground(wx.BLACK)
        dc.DrawText(self.description, rect.x+bmpWidth+dummy1+10, rect.y+3*(rect.height - textHeight)/4)
        

    def GetLineHeight(self):

        dc = wx.MemoryDC()
        dc.SelectObject(wx.EmptyBitmap(100, 20))

        bmpWidth, bmpHeight = self.icon.GetWidth(), self.icon.GetHeight()
        dc.SetFont(self.normalFont)
                        
        textWidth, textHeight = dc.GetTextExtent(self.text)

        dc.SetFont(self.smallerFont)
        dummy1, dummy2= dc.GetTextExtent("Type: ")
        textWidth, textHeight = dc.GetTextExtent("Type: " + self.description)

        dc.SelectObject(wx.NullBitmap)
        
        return max(2*textHeight, bmpHeight) + 20
    

    def GetSubItemWidth(self):

        return 250
    

class SecondColumnRenderer(object):

    def __init__(self, parent, fileName):

        self.parent = parent
        self.fileName = fileName

        self.smallerFont = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)

        self.greyColour = wx.SystemSettings.GetColour(wx.SYS_COLOUR_GRAYTEXT)

        t = os.path.getmtime(fileName)
        date = datetime.datetime.fromtimestamp(t)
        self.date = date.strftime("%d/%m/%Y %H:%M")

        if os.path.isdir(fileName):
            self.size = ""
            return

        s = os.path.getsize(fileName)
        self.size = FormatFileSize(s)
                

    def DrawSubItem(self, dc, rect, line, highlighted, enabled):
        """Draw a custom progress bar using double buffering to prevent flicker"""

        dc.SetFont(self.smallerFont)

        date = self.date
        dummy1, dummy2= dc.GetTextExtent("Date modified: ")
        textWidth, textHeight = dc.GetTextExtent("Date modified: " + date)

        dc.SetTextForeground(self.greyColour)
        dc.DrawText("Date modified: ", rect.x+5, rect.y+(rect.height - textHeight)/4)

        dc.SetTextForeground(wx.BLACK)
        dc.DrawText(date, rect.x+dummy1+5, rect.y+(rect.height - textHeight)/4)

        if not self.size:
            return
        
        dummy1, dummy2= dc.GetTextExtent("Size: ")

        dc.SetTextForeground(self.greyColour)
        dc.DrawText("Size: ", rect.x+5, rect.y+3*(rect.height - textHeight)/4)

        dc.SetTextForeground(wx.BLACK)
        dc.DrawText(self.size, rect.x+dummy1+5, rect.y+3*(rect.height - textHeight)/4)
        

    def GetLineHeight(self):

        dc = wx.MemoryDC()
        dc.SelectObject(wx.EmptyBitmap(100, 20))
        textWidth, textHeight, d1, d2 = dc.GetFullTextExtent("Date modified: %s"%self.date,
                                                             self.smallerFont)

        dc.SelectObject(wx.NullBitmap)
        return 2*textHeight + 20
    

    def GetSubItemWidth(self):

        dc = wx.MemoryDC()
        dc.SelectObject(wx.EmptyBitmap(100, 20))

        textWidth, textHeight, d1, d2 = dc.GetFullTextExtent("Date modified: %s"%self.date,
                                                             self.smallerFont)

        dc.SelectObject(wx.NullBitmap)

        return textWidth+10
    
        
class Windows7Explorer(ULC.UltimateListCtrl):
    
    def __init__(self, parent, log):

        ULC.UltimateListCtrl.__init__(self, parent, -1, style=wx.BORDER_THEME,
                                      agwStyle=wx.LC_REPORT|wx.LC_NO_HEADER|wx.LC_HRULES|
                                      ULC.ULC_HAS_VARIABLE_ROW_HEIGHT)

        self.EnableSelectionVista()
        self.PopulateList()


    def PopulateList(self, path=""):

        self.ClearAll()

        self.InsertColumn(0, "Column 1") 
        self.InsertColumn(1, "Column 2") 

        if not path.strip():
            path = os.getcwd()

        os.chdir(path)            
        files1 = os.listdir(path)
        files1.sort()

        files = []
        for file in files1:
            kind = not os.path.isdir(file)
            name = os.path.split(file)[1]
            files.append((kind, name, name.lower()))

        files = sorted(files, key=operator.itemgetter(0, 2))
        dummy_log = wx.LogNull()
        
        for kind, file, lower in files:
            index = self.InsertStringItem(sys.maxint, "")

            klass = FirstColumnRenderer(self, file)
            self.SetItemCustomRenderer(index, 0, klass)
            
            self.SetStringItem(index, 1, "")
            klass = SecondColumnRenderer(self, file)
            self.SetItemCustomRenderer(index, 1, klass)

        self.SetColumnWidth(0, ULC.ULC_AUTOSIZE_FILL)
        self.SetColumnWidth(1, wx.LIST_AUTOSIZE)


#---------------------------------------------------------------------------

class TestFrame(wx.Frame):

    def __init__(self, parent, log):

        wx.Frame.__init__(self, parent, -1, "UltimateListCtrl - Windows 7 Explorer", size=(800, 600))

        self.log = log

        panel = wx.Panel(self)
        panelSizer = wx.BoxSizer(wx.VERTICAL)

        font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
        font.SetWeight(wx.BOLD)

        static = wx.StaticText(panel, -1, "Choose a folder to display:")
        static.SetFont(font)
        panelSizer.Add(static, 0, wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, 10)
        
        dp1 = wx.DirPickerCtrl(panel, path=os.getcwd(), style=wx.DIRP_USE_TEXTCTRL)
        dp1.SetTextCtrlProportion(2)
        panelSizer.Add(dp1, 0, wx.EXPAND|wx.LEFT|wx.RIGHT, 10)
                
        # Create the CustomTreeCtrl, using a derived class defined below
        self.ulc = Windows7Explorer(panel, self.log)

        panelSizer.Add(self.ulc, 1, wx.EXPAND|wx.ALL, 10)
        panel.SetSizer(panelSizer)

        self.Bind(wx.EVT_DIRPICKER_CHANGED, self.OnPickDir, dp1)

        self.ulc.SetFocus()        
        self.SetIcon(images.Mondrian.GetIcon())
        self.CenterOnScreen()
        self.Show()


    def OnPickDir(self, event):

        wx.BeginBusyCursor()
        self.Freeze()
        path = event.GetPath()

        try:
            self.ulc.PopulateList(path)
        except OSError:
            self.log.write("You dont have permission to access folder: %s"%path)
            wx.EndBusyCursor()
            self.Thaw()
            return

        self.Layout()
        self.SendSizeEvent()
        self.Thaw()

        self.ulc.DoLayout()
        wx.EndBusyCursor()
        

#---------------------------------------------------------------------------

if __name__ == '__main__':
    import sys
    app = wx.App(0)
    frame = TestFrame(None, sys.stdout)
    frame.Show(True)
    app.MainLoop()