Hi Phil,
my next problems are two fold, combined in one script, but it is also
a nice tool to adjust the parameter for non ugly disabled icons, and
shows the way to plug them into your project. Qt's disabled icons are
but ugly, aren't they?
It is typically called this way: python iconfactory.py pixmaps/*
and will load them in a toolbar, where the IconFactory disable
parameters can be adjusted.
Following issues arose here: I added a way to save the whole widget as
PNG. When QPixmap.grabWidget() is called, the icons are damaged, no
matter, in which way I call the QFileDialog (statically, modal,
before grabWidget, after..). All icons, that where not covered by the
QFileDialog are damaged and won't be redrawn on a call of
repaint(True) or update(). Switching screens, or using the
QFileDialog as a brush bring them back, but I would like to know,
what happen here, and problably a way to avoid/workaround this
effect.
While at it, I discovered the QFilePreview ability, and tried to add
it, but failed. When enabled (set enablePreview = 1), it just issues
a SystemError with this confusing message:
Traceback (most recent call last):
File "iconfactory.py", line 165, in saveScreenshot
fd.setContentsPreview(p, p)
SystemError: error return without exception set
Puzzled! According to your docs, it is fully implemented.
Cheers,
Pete
#!/usr/bin/env python
from qt import *
enablePreview = 1
class IconFactory(QIconFactory):
MAXGRAY = 207
REDOFF = 8
GREENOFF = 4
BLUEOFF = 0
GRAYOFF = 109
GRAYFAC1 = -2
GRAYFAC2 = 3
def createPixmap(self, iconSet, size, mode, state):
if mode == QIconSet.Disabled:
if iconSet.pixmap().isNull():
return None
img = QImage(iconSet.pixmap().convertToImage())
w = img.width()
h = img.height()
for y in range(h):
for x in range(w):
g = qGray(img.pixel(x, y))
g += self.GRAYFAC1 * g / self.GRAYFAC2 + self.GRAYOFF
if g > self.MAXGRAY:
g = self.MAXGRAY
img.setPixel(x, y, qRgb(g + self.REDOFF, g + self.GREENOFF, g + self.BLUEOFF))
pix = QPixmap()
pix.convertFromImage(img)
m = iconSet.pixmap().mask()
if m:
pix.setMask(m)
return pix
else:
return None
if __name__ == '__main__':
class SpinBox(QSpinBox):
def __init__(self, var, varclass, _min, _max, step, parent = None, name = None):
self.varclass = varclass
self.var = var
QSpinBox.__init__(self, _min, _max, step, parent, name)
self.setValue(getattr(varclass, var))
self.connect(self, SIGNAL("valueChanged(int)"), self.changeValue)
def changeValue(self, value):
setattr(self.varclass, self.var, value)
class MainWin(QMainWindow):
def __init__(self, args, parent = None, name = None, f = 0):
self.actions = []
self.icons = args
self.iconsEnabled = False
QMainWindow.__init__(self, parent, name, f)
self.SystemFactory = QIconFactory.defaultFactory()
self.SystemFactory.setAutoDelete(False)
self.IconFactory = IconFactory()
QIconFactory.installDefaultFactory(self.IconFactory)
menuBar = QMenuBar(self, "menuBar")
fileMenu = QPopupMenu(self, "fileMenu")
fileFactory = QAction("&My Icon Factory", Qt.CTRL+Qt.Key_M, self, "fileFactory")
fileFactory.setToggleAction(True)
fileFactory.setOn(True)
fileFactory.addTo(fileMenu)
self.connect(fileFactory, SIGNAL("toggled(bool)"), self.enableFactory)
fileEnable = QAction("&Enable Icons", Qt.CTRL+Qt.Key_E, self, "fileEnable")
fileEnable.setToggleAction(True)
fileEnable.setOn(self.iconsEnabled)
fileEnable.addTo(fileMenu)
self.connect(fileEnable, SIGNAL("toggled(bool)"), self.enableActions)
fileMenu.insertSeparator()
saveScreenshot = QAction("&Save Screenshot", Qt.Key_Print, self, "saveScreenshot")
saveScreenshot.addTo(fileMenu)
self.connect(saveScreenshot, SIGNAL("activated()"), self.saveScreenshot)
fileMenu.insertSeparator()
fileQuit = QAction("&Quit", Qt.CTRL+Qt.Key_Q, self, "FileQuit")
fileQuit.addTo(fileMenu)
self.connect(fileQuit, SIGNAL("activated()"), qApp, SLOT("quit()"))
menuBar.insertItem("&File", fileMenu)
self.toolBar = QToolBar("ToolBar", self, Qt.DockTop)
mainWidget = QWidget(self)
hbox = QHBoxLayout(mainWidget, 8, 8)
for l, v, cl, _min, _max, step in (
("Gray level:", "MAXGRAY", self.IconFactory, 0, 255, 5),
("Red offset:", "REDOFF", self.IconFactory, 0, 255, 5),
("Green offset:", "GREENOFF", self.IconFactory, 0, 255, 5),
("Blue offset:", "BLUEOFF", self.IconFactory, 0, 255, 5),
("Gray offset:", "GRAYOFF", self.IconFactory, 0, 255, 5),
("Gray factor 1:", "GRAYFAC1", self.IconFactory, -50, +50, 1),
("Gray factor 2:", "GRAYFAC2", self.IconFactory, -50, +50, 1)):
label = QLabel(l, mainWidget)
hbox.addWidget(label)
sb = SpinBox(v, cl, _min, _max, step, mainWidget)
self.connect(sb, SIGNAL("valueChanged(int)"), self.updateActions)
hbox.addWidget(sb)
spacer = QSpacerItem(10,10,QSizePolicy.Expanding,QSizePolicy.Minimum)
hbox.addItem(spacer)
self.setCentralWidget(mainWidget)
self.updateActions()
self.show()
self.resize(self.toolBar.sizeHint())
def updateActions(self):
for a in self.actions:
a.removeFrom(self.toolBar)
del a
for f in self.icons:
if not os.path.exists(f):
print "%s ignored" % f
pm = QPixmap(f)
if not pm.isNull():
a = QAction(self)
a.setIconSet(QIconSet(pm))
a.addTo(self.toolBar)
a.setEnabled(self.iconsEnabled)
a.setToolTip(f)
self.actions.append(a)
def enableActions(self, enable):
for a in self.actions:
a.setEnabled(enable)
self.iconsEnabled = enable
def enableFactory(self, enable):
if enable:
fac = self.IconFactory
else:
fac = self.SystemFactory
QIconFactory.installDefaultFactory(fac)
self.updateActions()
def saveScreenshot(self):
if enablePreview:
class Preview(QLabel, QFilePreview):
def __init__(self, parent = None):
QLabel.__init__(self, parent)
def previewUrl(self, url):
path = url.path()
pix = QPixmap(path)
if pix.isNull():
self.setText("This is not a pixmap")
else:
self.setPixmap(pix)
#fd = QFileDialog(self, "saveScreenshot", True)
fd = QFileDialog(self, "saveScreenshot")
fd.setCaption("Save Screenshot")
fd.setMode(QFileDialog.AnyFile)
fd.setFilter("PNG (*.png)")
if enablePreview:
p = Preview()
fd.setContentsPreviewEnabled(True)
fd.setContentsPreview(p, p)
fd.setPreviewMode(QFileDialog.Contents)
if fd.exec_loop() == QDialog.Accepted:
fn = fd.selectedFile()
if not fn.isNull():
pm = QPixmap.grabWidget(self)
pm.save(fn, "PNG")
#self.repaint(True)
#self.update()
def _saveScreenshot(self):
if 1:
fn = QFileDialog.getSaveFileName(None, "PNG (*.png)", self,
"SaveScreenshot", "Save Screenshot")
if not fn.isNull():
pm = QPixmap.grabWidget(self)
if not pm.isNull():
pm.save(fn, "PNG")
else:
pm = QPixmap.grabWidget(self)
if not pm.isNull():
fn = QFileDialog.getSaveFileName(None, "PNG (*.png)", self,
"SaveScreenshot", "Save Screenshot")
if not fn.isNull():
pm.save(fn, "PNG")
#self.repaint(True)
#self.update()
import os, sys, getopt
app = QApplication(sys.argv)
try:
optlist, args = getopt.getopt(sys.argv[1:], "")
except getopt.error, msg:
print msg
sys.exit(1)
mw = MainWin(args)
QObject.connect(qApp, SIGNAL("lastWindowClosed()"), qApp, SLOT("quit()"))
app.exec_loop()
_______________________________________________
PyKDE mailing list [email protected]
http://mats.imk.fraunhofer.de/mailman/listinfo/pykde