increment is 0.5
increment is 0.5
show, going for height 125
got begin 0 end 125 step 0.5
amount_grown is 124
hides
amount_shrunk is 0
show, going for height 125
got begin 0 end 125 step 0.5
amount_grown is 124
hides
amount_shrunk is 0
show, going for height 125
got begin 0 end 125 step 0.5
amount_grown is 124
show, going for height 125
got begin 0 end 125 step 0.5
amount_grown is 124
hides
amount_shrunk is 0
show, going for height 125
got begin 0 end 125 step 0.5
amount_grown is 124
hides
amount_shrunk is 0
show, going for height 125
got begin 0 end 125 step 0.5
amount_grown is 124
real 0m13.645s
user 0m2.369s
sys 0m1.193s
increment is 0.5
increment is 0.5
show, going for height 125
got begin 0 end 125 step 0.5
amount_grown is 124
hides
amount_shrunk is 0
show, going for height 125
got begin 0 end 125 step 0.5
amount_grown is 124
hides
amount_shrunk is 0
show, going for height 125
got begin 0 end 125 step 0.5
amount_grown is 124
show, going for height 125
got begin 0 end 125 step 0.5
amount_grown is 124
hides
amount_shrunk is 0
show, going for height 125
got begin 0 end 125 step 0.5
amount_grown is 124
hides
amount_shrunk is 0
show, going for height 125
got begin 0 end 125 step 0.5
amount_grown is 124
real 0m13.645s
user 0m2.369s
sys 0m1.193s
To copy to clipboard, switch view to plain text mode
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
def copy_size(size):
return QSize(size.
width(), size.
height())
def xfloat_range(begin,end,step):
print "got begin", begin, "end", end, "step", step
while begin < end:
yield begin
begin += step
def xfloat_range_decrement(begin, end, step):
while begin > end and begin > 0:
yield begin
begin -= step
def print_qsize(sz, title=""):
print title, "W: ", sz.width(), " H: ", sz.height()
def __init__(self, child, size, parent = None):
self._name = ""
child.
sizeHint = lambda
: QSize()
self.child = child
self.addWidget(child)
self.
mandated_size = QSize(size.
height(),
0) self.preferred_size = size
self.setContentsMargins(0,0,0,0)
self.setSpacing(0)
self.duration = 250
self.amount_grown = 0
self.amount_shrunk = 0
self.generator = None
self.child.setVisible(True)
self.show = self.startShow
self.hide = self.startHide
self.child.show = self.show
self.child.hide = self.hide
print "increment is ", self.increment
@property
def increment(self):
return float(float(self.preferred_size.height())/float(self.duration))
def setGeometry(self, rect):
#print_qsize(rect.size(), self._name)
super(type(self), self).setGeometry(rect)
def sizeHint(self):
return self.mandated_size
def setAnimationDuration(self, msecs):
self.duration = msecs
def timer_fire_show(self):
if not self.generator:
raise Exception("Generator not initialized!")
try:
h = int(self.generator.next())
#print h
if h > self.amount_grown:
self.mandated_size.setHeight(h)
self.amount_grown = h
self.update()
except StopIteration:
#finished:
print "amount_grown is", self.amount_grown
self.timer.stop()
#do any cleanup, just a final check
self.mandated_size = copy_size(self.preferred_size)
self.update()
QObject.
disconnect(self.
timer,
SIGNAL("timeout()"), self.
timer_fire_show) del self.generator
def startShow(self):
print "show, going for height", self.preferred_size.height()
#start an animation here using a timer that will update both our sizeHint
#as well as the widget's size accordingly
self.generator = xfloat_range(0, self.preferred_size.height(), self.increment)
QObject.
connect(self.
timer,
SIGNAL("timeout()"), self.
timer_fire_show) self.amount_grown = 0
self.timer.start(1)
def timer_fire_hide(self):
if not self.generator:
raise Exception("Generator not initialized")
try:
h = int(self.generator.next())
#print h
if h < self.amount_shrunk:
self.mandated_size.setHeight(h)
self.amount_shrunk = h
self.update()
except StopIteration:
print "amount_shrunk is", self.amount_shrunk
self.timer.stop()
self.mandated_size.setHeight(0)
self.update()
QObject.
disconnect(self.
timer,
SIGNAL("timeout()"), self.
timer_fire_hide)
def startHide(self):
print "hides"
self.generator = xfloat_range_decrement(self.preferred_size.height(), 0, self.increment)
QObject.
connect(self.
timer,
SIGNAL("timeout()"), self.
timer_fire_hide) self.amount_shrunk = self.preferred_size.height()
self.timer.start(1)
def setVisible(self, b):
"convenience method"
if b:
self.startShow()
else:
self.startHide()
def __init__(self, parent=None):
self.
layout().
setSizeConstraint(QLayout.
SetFixedSize)
self.setCentralWidget(centralwidget)
checkbox1
= QCheckBox("Show First Widget", self
) checkbox2
= QCheckBox("Show Second Widget", self
) textedit1 = QPlainTextEdit(self)
textedit2 = QPlainTextEdit(self)
anim_wrapper1
= AnimatedLayout
(textedit1,
QSize(150,
125)) anim_wrapper1._name = "anim_wrapper1"
self.anim_wrapper1 = anim_wrapper1
anim_wrapper2
= AnimatedLayout
(textedit2,
QSize(150,
125)) anim_wrapper2._name = "anim_wrapper2"
self.anim_wrapper2 = anim_wrapper2
layout.addWidget(checkbox1)
layout.addLayout(anim_wrapper1)
layout.addWidget(checkbox2)
layout.addLayout(anim_wrapper2)
centralwidget.setLayout(layout)
connect(checkbox1, SIGNAL("toggled(bool)"), anim_wrapper1.setVisible)
connect(checkbox2, SIGNAL("toggled(bool)"), anim_wrapper2.setVisible)
mw = ResizingWindow()
mw.show()
app.exec_()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
connect = QObject.connect
app = QApplication(sys.argv)
def copy_size(size):
return QSize(size.width(), size.height())
def xfloat_range(begin,end,step):
print "got begin", begin, "end", end, "step", step
while begin < end:
yield begin
begin += step
def xfloat_range_decrement(begin, end, step):
while begin > end and begin > 0:
yield begin
begin -= step
def print_qsize(sz, title=""):
print title, "W: ", sz.width(), " H: ", sz.height()
class AnimatedLayout(QHBoxLayout):
def __init__(self, child, size, parent = None):
QHBoxLayout.__init__(self, parent)
self._name = ""
child.sizeHint = lambda: QSize()
child.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Ignored)
self.child = child
self.addWidget(child)
self.mandated_size = QSize(size.height(), 0)
self.preferred_size = size
self.setContentsMargins(0,0,0,0)
self.setSpacing(0)
self.duration = 250
self.amount_grown = 0
self.amount_shrunk = 0
self.generator = None
self.timer = QTimer(self)
self.child.setVisible(True)
self.show = self.startShow
self.hide = self.startHide
self.child.show = self.show
self.child.hide = self.hide
print "increment is ", self.increment
@property
def increment(self):
return float(float(self.preferred_size.height())/float(self.duration))
def setGeometry(self, rect):
#print_qsize(rect.size(), self._name)
super(type(self), self).setGeometry(rect)
def sizeHint(self):
return self.mandated_size
def setAnimationDuration(self, msecs):
self.duration = msecs
def timer_fire_show(self):
if not self.generator:
raise Exception("Generator not initialized!")
try:
h = int(self.generator.next())
#print h
if h > self.amount_grown:
self.mandated_size.setHeight(h)
self.amount_grown = h
self.update()
except StopIteration:
#finished:
print "amount_grown is", self.amount_grown
self.timer.stop()
#do any cleanup, just a final check
self.mandated_size = copy_size(self.preferred_size)
self.update()
QObject.disconnect(self.timer, SIGNAL("timeout()"), self.timer_fire_show)
del self.generator
def startShow(self):
print "show, going for height", self.preferred_size.height()
#start an animation here using a timer that will update both our sizeHint
#as well as the widget's size accordingly
self.generator = xfloat_range(0, self.preferred_size.height(), self.increment)
QObject.connect(self.timer, SIGNAL("timeout()"), self.timer_fire_show)
self.amount_grown = 0
self.timer.start(1)
def timer_fire_hide(self):
if not self.generator:
raise Exception("Generator not initialized")
try:
h = int(self.generator.next())
#print h
if h < self.amount_shrunk:
self.mandated_size.setHeight(h)
self.amount_shrunk = h
self.update()
except StopIteration:
print "amount_shrunk is", self.amount_shrunk
self.timer.stop()
self.mandated_size.setHeight(0)
self.update()
QObject.disconnect(self.timer, SIGNAL("timeout()"), self.timer_fire_hide)
def startHide(self):
print "hides"
self.generator = xfloat_range_decrement(self.preferred_size.height(), 0, self.increment)
QObject.connect(self.timer, SIGNAL("timeout()"), self.timer_fire_hide)
self.amount_shrunk = self.preferred_size.height()
self.timer.start(1)
def setVisible(self, b):
"convenience method"
if b:
self.startShow()
else:
self.startHide()
class ResizingWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.layout().setSizeConstraint(QLayout.SetFixedSize)
centralwidget = QWidget(self)
self.setCentralWidget(centralwidget)
layout = QVBoxLayout()
checkbox1 = QCheckBox("Show First Widget", self)
checkbox2 = QCheckBox("Show Second Widget", self)
textedit1 = QPlainTextEdit(self)
textedit2 = QPlainTextEdit(self)
anim_wrapper1 = AnimatedLayout(textedit1, QSize(150, 125))
anim_wrapper1._name = "anim_wrapper1"
self.anim_wrapper1 = anim_wrapper1
anim_wrapper2 = AnimatedLayout(textedit2, QSize(150, 125))
anim_wrapper2._name = "anim_wrapper2"
self.anim_wrapper2 = anim_wrapper2
layout.addWidget(checkbox1)
layout.addLayout(anim_wrapper1)
layout.addWidget(checkbox2)
layout.addLayout(anim_wrapper2)
centralwidget.setLayout(layout)
connect(checkbox1, SIGNAL("toggled(bool)"), anim_wrapper1.setVisible)
connect(checkbox2, SIGNAL("toggled(bool)"), anim_wrapper2.setVisible)
mw = ResizingWindow()
mw.show()
app.exec_()
To copy to clipboard, switch view to plain text mode
Bookmarks