Welcome, guest | Sign In | My Account | Store | Cart

this program make a palette for your items.

Python, 141 lines
  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
import gtk
import xml.dom.minidom as minidom

class PaletteBox:
    class PaletteBoxItem:
        def __init__(self,num,icon,tooltip,size):
            self.num=num
            self.icon=icon
            self.tooltip=tooltip
            self.Item=gtk.RadioToolButton()
            self.Item.set_size_request(size,size)
        
    def __init__(self,title,items_size = 40):
        if items_size > 64 : items_size = 64
        if items_size < 16 : items_size = 16
        self.rowcount=-1
        self.item=[]
        self.items_size = items_size
        self.button = gtk.Button()
        self.button.set_focus_on_click(False)
        self.button.connect("clicked",self.hide_show_box)
        self.btnbox=gtk.HBox()
        self.btnlabel=gtk.Label("  " + title )
        self.btnarrow=gtk.Arrow(gtk.ARROW_DOWN,gtk.SHADOW_OUT)
        self.btnarrowx = gtk.ARROW_DOWN
        self.btnbox.pack_start(self.btnarrow,0,0)
        self.btnbox.pack_start(self.btnlabel,0,0)
        self.button.add(self.btnbox)
        self.hbl = gtk.HBox()
        self.itemsbox = gtk.Fixed()
        self.itemsbox.set_size_request(items_size, -1)
        self.itemsbox.connect("size-allocate",self.redraw_itemsbox)
        self.hbl.pack_start(self.itemsbox,1,1)
        self.box=gtk.VBox()
        self.box.pack_start(self.button,0,0)
        self.box.pack_start(self.hbl,1,1)
        
    def hide_show_box(self,widget):
        if self.btnarrowx == gtk.ARROW_DOWN:
            self.btnarrow.set(gtk.ARROW_RIGHT,gtk.SHADOW_OUT)
            self.hbl.set_property("visible",False)
            self.btnarrowx=gtk.ARROW_RIGHT
        elif self.btnarrowx == gtk.ARROW_RIGHT:
            self.btnarrow.set(gtk.ARROW_DOWN,gtk.SHADOW_OUT)
            self.hbl.set_property("visible",True)
            self.btnarrowx=gtk.ARROW_DOWN
            
    def set_title(self,title):
        try:
            self.button.set_label(title)
        except:
            print "PaletteBox.set_title(" + str(title) + ") is not string."
        
    def add_item(self,icon,tooltip,group=None):
        index = len(self.item)
        self.item.append(self.PaletteBoxItem(index,icon,tooltip,self.items_size))
        if(len(self.item) > 1):
            self.item[index].Item.set_group(self.item[0].Item)
        else:
            self.item[index].Item.set_group(group)
        self.item[index].Item.set_label(tooltip)
        self.itemsbox.add(self.item[index].Item)
        self.redraw_itemsbox(self,self.itemsbox)
    def redraw_itemsbox(self,widget,event = 0):
        width = self.itemsbox.get_allocation()[2]
        rowcount = width / self.items_size
        if(self.rowcount != rowcount):
            if rowcount<1 : rowcount = self.items_size
            colcount = len(self.item) / rowcount
            extra = len(self.item) % rowcount
            itemcounter = 0
            for i in range(colcount):
                for j in range(rowcount):
                    self.itemsbox.move(self.item[itemcounter].Item , j * self.items_size , i * self.items_size)
                    itemcounter += 1
            for j in range(extra):
                self.itemsbox.move(self.item[itemcounter].Item , j * self.items_size , colcount * self.items_size)
                itemcounter += 1
            self.rowcount = rowcount
class MakePaletteByXML:
    
    def __init__(self, xmlpath, parent = gtk.VBox()):
        try:
            self.parent = parent
            x=minidom.parse(xmlpath)
            for group in x.getElementsByTagName("group"):
                isnewgroup = True
                for box in group.getElementsByTagName("box"):
                    if (box.getAttribute("title")) : title = box.getAttribute("title")
                    else : title = "unknown"
                    mypalette = PaletteBox(title)
                    self.parent.pack_start(mypalette.box, 0)
                    for item in box.getElementsByTagName("item"):
                        icon = item.getAttribute("iconpath")
                        tooltip = item.getAttribute("tooltip")
                        if (isnewgroup) :
                            isnewgroup = False
                            mypalette.add_item(icon, tooltip, None)
                            itemgroup = mypalette.item[0].Item
                        else:
                            mypalette.add_item(icon, tooltip, itemgroup)
        except :
            print "on read a xml file occured a error ."
            print "please view a xml file and repir the xml file structure..."
w = gtk.Window()
w.set_size_request(500,500)
w.connect("destroy",gtk.main_quit)
p = gtk.HPaned()
group=pb1=PaletteBox("First Test")
pb1.add_item("1", "A")
pb1.add_item("2", "B")
pb1.add_item("3", "C")
pb1.add_item("4", "D")
pb1.add_item("5", "E")
pb1.add_item("6", "F")
pb1.add_item("7", "G")
pb1.add_item("8", "H")
pb1.add_item("9", "I")
pb1.add_item("10", "J")
pb2=PaletteBox("Last Test")
pb2.add_item("1", "A",pb1.item[0].Item)
pb2.add_item("2", "B",pb1.item[0].Item)
pb2.add_item("3", "C",pb1.item[0].Item)
pb2.add_item("4", "D",pb1.item[0].Item)
pb2.add_item("5", "E",pb1.item[0].Item)
pb2.add_item("6", "F",pb1.item[0].Item)
pb2.add_item("7", "G",pb1.item[0].Item)
pb2.add_item("8", "H",pb1.item[0].Item)
pb2.add_item("9", "I",pb1.item[0].Item)
pb2.add_item("10", "J",pb1.item[0].Item)
vbb=gtk.VBox()
vbb.pack_start(pb1.box,0)
vbb.pack_start(pb2.box,0)
p.pack1(vbb,0,0)
v=gtk.VBox()
v.add(p)
l=gtk.Layout()
p.pack2(l)
w.add(v)
w.show_all()
gtk.main()
Created by mohsen javid on Tue, 13 Jan 2009 (MIT)
Python recipes (4591)
mohsen javid's recipes (2)

Required Modules

Other Information and Tasks