From d8a34adcc1a69a2b77881a6e504d0f0ad896eb3a Mon Sep 17 00:00:00 2001 From: deva Date: Fri, 4 Mar 2011 08:10:30 +0000 Subject: Simplify Collapser (animateToWidget instead of collapse/expand). --- client/collapser.cc | 231 +++++++++++++++------------------------------------- 1 file changed, 65 insertions(+), 166 deletions(-) (limited to 'client/collapser.cc') diff --git a/client/collapser.cc b/client/collapser.cc index 13357f4..caf3117 100644 --- a/client/collapser.cc +++ b/client/collapser.cc @@ -28,19 +28,17 @@ #include #include #include +#include #include "debug.h" #define ANIM_TIME 300 // ms #define ANIM_INTERVAL 5 // ms -Collapser::Collapser(QWidget *collapsed, QWidget *expanded, bool setcollapsed, - QScrollArea *scrollarea) +Collapser::Collapser(QWidget *current, QScrollArea *scroll) { - this->collapsed = NULL; - this->expanded = NULL; - - this->scrollarea = scrollarea; + current_widget = current; + scrollarea = scroll; timer = new QTimer(this); timer->setSingleShot(true); @@ -49,147 +47,51 @@ Collapser::Collapser(QWidget *collapsed, QWidget *expanded, bool setcollapsed, setLayout(new QHBoxLayout()); layout()->setContentsMargins(0,0,0,0); - - setWidgets(collapsed, expanded); - - is_collapsed = !setcollapsed; // Make sure setCollapsed actually does something. - setCollapsed(setcollapsed); } void Collapser::updateHeight() { - int c_height = 16; - int e_height = 16; - if(collapsed) { - QSize sz = collapsed->minimumSizeHint(); - c_height = sz.height(); - } - - if(expanded) { - QSize sz = expanded->minimumSizeHint(); - e_height = sz.height(); - } - - if(isCollapsed()) { - setFixedHeight(c_height); - } else { - setFixedHeight(e_height); - } -} - -void Collapser::setWidgets(QWidget *collapsed, QWidget *expanded) -{ - setCollapsedWidget(collapsed); - setExpandedWidget(expanded); -} - -void Collapser::setCollapsedWidget(QWidget *collapsed) -{ - this->collapsed = collapsed; - - if(isCollapsed() == true && collapsed) { - layout()->addWidget(collapsed); - collapsed->setVisible(true); - } -} - -void Collapser::setExpandedWidget(QWidget *expanded) -{ - this->expanded = expanded; - - if(isCollapsed() == false && expanded) { - layout()->addWidget(expanded); - expanded->setVisible(true); + int height = 16; + if(current_widget) { + QSize sz = current_widget->minimumSizeHint(); + height = sz.height(); } + setFixedHeight(height); } -QWidget *Collapser::collapsedWidget() -{ - return collapsed; -} - -QWidget *Collapser::expandedWidget() -{ - return expanded; -} - - -bool Collapser::isCollapsed() +QWidget *Collapser::currentWidget() { - return is_collapsed; + return current_widget; } -void Collapser::setCollapsed(bool setcollapsed) +void Collapser::animateToWidget(QWidget *widget, bool stv) { - if(this->is_collapsed == setcollapsed) return; + if(widget == current_widget) return; - if(setcollapsed) collapse(); - else expand(); -} - -void Collapser::collapse() -{ - emit collapsing(); + emit animating(widget); + scroll_to_view = stv; t_anim.start(); - is_collapsed = true; - timer->start(); - - if(expanded) { - expanded->setVisible(false); - layout()->removeWidget(expanded); + placeholder.grabFrom(current_widget); + if(current_widget) { + current_widget->setVisible(false); + layout()->removeWidget(current_widget); } - if(expanded) placeholder.grab_from(expanded); - - layout()->addWidget(&placeholder); - placeholder.setVisible(true); - - if(collapsed) placeholder.grab_to(collapsed); -} -void Collapser::expand() -{ - emit expanding(); - - t_anim.start(); - - // show expanded - if(collapsed) { - collapsed->setVisible(false); - layout()->removeWidget(collapsed); - placeholder.grab_from(collapsed); - } + placeholder.grabTo(widget); + if(widget) { + layout()->addWidget(&placeholder); + placeholder.setVisible(true); + } - layout()->addWidget(&placeholder); - placeholder.setVisible(true); + current_widget = widget; - if(expanded) placeholder.grab_to(expanded); - - is_collapsed = false; timer->start(); } -void Collapser::toggleCollapse() -{ - if(!is_collapsed) collapse(); - else expand(); -} - void Collapser::anim() { - int c_height = 16; - int e_height = 16; - if(collapsed) { - QSize sz = collapsed->minimumSizeHint(); - c_height = sz.height(); - } - - if(expanded) { - QSize sz = expanded->minimumSizeHint(); - e_height = sz.height(); - } - double x = (double)(t_anim.elapsed()) / (double)ANIM_TIME; double y = 1; @@ -198,52 +100,30 @@ void Collapser::anim() placeholder.setWeight(x); - int height; - if(!is_collapsed) { - height = (int)((1 - y) * c_height + y * e_height); - } else { - height = (int)((1 - y) * e_height + y * c_height); - } + int height = (int)((1 - y) * placeholder.fromHeight() + + y * placeholder.toHeight()); + setFixedHeight(height); timer->start(); } else { - if(is_collapsed) { - // show collapsed - - placeholder.setVisible(false); - layout()->removeWidget(&placeholder); - - if(collapsed) { - layout()->addWidget(collapsed); - // collapsed->setFixedHeight(c_height); - collapsed->setVisible(true); - } - - setFixedHeight(c_height); + placeholder.setVisible(false); + layout()->removeWidget(&placeholder); - emit doneCollapsing(); - - } else { - setFixedHeight(e_height); - - placeholder.setVisible(false); - layout()->removeWidget(&placeholder); - if(expanded) { - layout()->addWidget(expanded); - expanded->setVisible(true); - } - - emit doneExpanding(); + if(current_widget) { + layout()->addWidget(current_widget); + current_widget->setVisible(true); + } + + setFixedHeight(placeholder.toHeight()); - if(scrollarea && expanded) { - scrollarea->ensureWidgetVisible(expanded); - } + emit doneAnimating(current_widget); + if(scroll_to_view && scrollarea && current_widget) { + scrollarea->ensureWidgetVisible(current_widget); } - } } @@ -256,32 +136,51 @@ void Collapser::Placeholder::paintEvent(QPaintEvent *) p.drawPixmap(rect(), pixmap_to, pixmap_to.rect()); } -static QPixmap grab(QWidget *w, int width) +static QPixmap grab(QWidget *w, int &height, int width) { QPixmap pix; + + QLabel lbl; + + if(w == NULL) { + w = &lbl; + } + QSize sz = w->minimumSizeHint(); sz.setWidth(width); w->resize(sz); pix = QPixmap::grabWidget(w, 0, 0); + + height = sz.height(); + return pix; } -void Collapser::Placeholder::grab_from(QWidget *w) + +void Collapser::Placeholder::grabFrom(QWidget *w) { weight = 0; // Reset - if(!w) return; - pixmap_from = grab(w, width()); + pixmap_from = grab(w, from_height, width()); } -void Collapser::Placeholder::grab_to(QWidget *w) +void Collapser::Placeholder::grabTo(QWidget *w) { weight = 0; // Reset - if(!w) return; - pixmap_to = grab(w, width()); + pixmap_to = grab(w, to_height, width()); } void Collapser::Placeholder::setWeight(double w) { weight = w * w * w; } + +int Collapser::Placeholder::fromHeight() +{ + return from_height; +} + +int Collapser::Placeholder::toHeight() +{ + return to_height; +} -- cgit v1.2.3