AlbumCover: cosmetics.
[nephilim.git] / misc.py
blobcf890e05ae4fb38c877f4e4d78dfbd3ef7f45a68
1 from PyQt4 import QtCore, QtGui
2 from htmlentitydefs import name2codepoint as n2cp
3 import re
4 import urllib2, cookielib
5 import socket
6 import unicodedata
8 import logging
10 socket.setdefaulttimeout(8)
12 appIcon = 'gfx/icon.png'
13 APPNAME = 'montypc'
14 ORGNAME = 'montypc'
16 def doEvents():
17 """Make some time for necessary events."""
18 QtCore.QEventLoop().processEvents(QtCore.QEventLoop.AllEvents)
20 def sec2min(secs):
21 """Converts seconds to min:sec."""
22 min=int(secs/60)
23 sec=secs%60
24 if sec<10:sec='0'+str(sec)
25 return str(min)+':'+str(sec)
27 def numeric_compare(x, y):
28 if x>y:
29 return 1
30 elif x==y:
31 return 0
32 return -1
33 def unique(seq):
34 """Retrieve list of unique elements."""
35 seen = []
36 return t(c for c in seq if not (c in seen or seen.append(c)))
38 def toAscii(ustr):
39 if type(ustr)==str:
40 return ustr
41 return unicodedata.normalize('NFKD', ustr).encode('ascii', 'ignore')
43 def substEntity(match):
44 ent = match.group(2)
45 if match.group(1) == "#":
46 return unichr(int(ent))
47 else:
48 cp = n2cp.get(ent)
50 if cp:
51 return unichr(cp)
52 else:
53 return match.group()
55 def decodeHTMLEntities(string):
56 # replace entities with their UTF-counterpart
57 entity_re = re.compile("&(#?)(\d{1,5}|\w{1,8});")
58 return entity_re.subn(substEntity, string)[0]
61 class Button(QtGui.QPushButton):
62 iconSize=32
63 """A simple Button class which calls $onClick when clicked."""
64 def __init__(self, caption, onClick=None, iconPath=None, iconOnly=False, parent=None):
65 QtGui.QPushButton.__init__(self, parent)
67 if onClick:
68 self.connect(self, QtCore.SIGNAL('clicked(bool)'), onClick)
69 if iconPath:
70 self.changeIcon(iconPath)
72 if not(iconPath and iconOnly):
73 QtGui.QPushButton.setText(self, caption)
75 self.setToolTip(caption)
77 def setText(self, caption):
78 self.setToolTip(caption)
79 if self.icon()==None:
80 self.setText(caption)
82 def changeIcon(self, iconPath):
83 icon=QtGui.QIcon()
84 icon.addFile(iconPath, QtCore.QSize(self.iconSize, self.iconSize))
85 self.setIcon(icon)