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 ++++++++++++++------------------------------------ client/collapser.h | 52 +++++------- client/macro.cc | 2 +- client/macrowindow.cc | 38 +++++---- client/macrowindow.h | 7 +- client/mainwindow.cc | 5 +- 6 files changed, 113 insertions(+), 222 deletions(-) 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; +} diff --git a/client/collapser.h b/client/collapser.h index 27d0810..8eea84a 100644 --- a/client/collapser.h +++ b/client/collapser.h @@ -37,33 +37,20 @@ class Collapser : public QWidget { Q_OBJECT public: - Collapser(QWidget *collapsed = NULL, QWidget *expanded = NULL, - bool setcollapsed = true, QScrollArea *scrollarea = NULL); + Collapser(QWidget *current = NULL, QScrollArea *scrollarea = NULL); - bool isCollapsed(); - void setCollapsed(bool setcollapsed); - - void setWidgets(QWidget *collapsed, QWidget *expanded); + void updateHeight(); - QWidget *collapsedWidget(); - QWidget *expandedWidget(); + void animateToWidget(QWidget *widget, bool scroll_to_view = false); - void setCollapsedWidget(QWidget *collapsed); - void setExpandedWidget(QWidget *expanded); - - void updateHeight(); + QWidget *currentWidget(); public slots: - void collapse(); - void expand(); - void toggleCollapse(); void anim(); signals: - void collapsing(); - void expanding(); - void doneCollapsing(); - void doneExpanding(); + void animating(QWidget *); + void doneAnimating(QWidget *); protected: // void timerEvent(QTimerEvent *); @@ -71,34 +58,35 @@ protected: private: class Placeholder : public QWidget { public: - QPixmap pixmap_from; - QPixmap pixmap_to; - - void grab_from(QWidget *w); - void grab_to(QWidget *w); + void grabFrom(QWidget *w); + void grabTo(QWidget *w); void setWeight(double weight); + int toHeight(); + int fromHeight(); + protected: void paintEvent(QPaintEvent*); private: + QPixmap pixmap_from; + QPixmap pixmap_to; + + int to_height; + int from_height; double weight; }; Placeholder placeholder; + QWidget *current_widget; - QWidget *collapsed; - QWidget *expanded; - - QScrollArea *scrollarea; - - bool is_collapsed; - + bool scroll_to_view; + QTimer *timer; QTime t_anim; int timer_id; - QTimer *timer; + QScrollArea *scrollarea; }; #endif/*__PRACRO_COLLAPSER_H__*/ diff --git a/client/macro.cc b/client/macro.cc index 6e58a5c..5d88015 100644 --- a/client/macro.cc +++ b/client/macro.cc @@ -145,7 +145,7 @@ void Macro::init(QBoxLayout *layout, Macros ¯os, window->update(node); if(xml_elem.attribute("static", "false") == "false") { - window->setCollapsed(true); + window->animateToWidget(window->resumewidget); } } diff --git a/client/macrowindow.cc b/client/macrowindow.cc index f38245e..0770021 100644 --- a/client/macrowindow.cc +++ b/client/macrowindow.cc @@ -36,7 +36,6 @@ #include "widgets/widget.h" #include "widgets/window.h" #include "lua.h" -#include "resumewidget.h" #include "debug.h" @@ -48,7 +47,7 @@ extern quint16 port; MacroWindow::MacroWindow(NetCom &n, QDomNode &xml_doc, QString templ, bool is_static, bool compact, QScrollArea *scrollarea) - : Collapser(NULL, NULL, true, compact?NULL:scrollarea), netcom(n) + : Collapser(NULL, compact?NULL:scrollarea), netcom(n) { this->is_static = is_static; @@ -62,16 +61,18 @@ MacroWindow::MacroWindow(NetCom &n, QDomNode &xml_doc, QString templ, this->templ = templ; resumewidget = new ResumeWidget(compact); - setCollapsedWidget(resumewidget); // update(xml_doc); + updateResume(xml_doc); initMacro(xml_doc); - if(mainwidget) setExpandedWidget(mainwidget->qwidget()); - setCollapsed(!is_static); + if(mainwidget) animateToWidget(mainwidget->qwidget(), true); + else animateToWidget(resumewidget); + active = true; - connect(this, SIGNAL(doneCollapsing()), this, SLOT(collapsed())); + connect(this, SIGNAL(doneAnimating(QWidget*)), + this, SLOT(animated(QWidget*))); } MacroWindow::~MacroWindow() @@ -200,7 +201,7 @@ bool MacroWindow::doCommit() qApp->processEvents(); - setCollapsed(true); + animateToWidget(resumewidget); emit updateOnCommit(); return true; @@ -226,7 +227,7 @@ void MacroWindow::cancel() void MacroWindow::expandWrapper() { - if(!isCollapsed()) return; + if(currentWidget() != resumewidget) return; waschanged = false; @@ -264,8 +265,7 @@ void MacroWindow::expandWrapper() // system and thereby validate correctly. if(mainwidget) mainwidget->setValues(); - if(mainwidget) setExpandedWidget(mainwidget->qwidget()); - expand(); + if(mainwidget) animateToWidget(mainwidget->qwidget(), true); // Set keyboard focus on the first focusable widget in the macro. QVector< Widget* > widgets; @@ -283,7 +283,7 @@ void MacroWindow::expandWrapper() void MacroWindow::collapseWrapper() { - if(isCollapsed()) return; + if(currentWidget() == resumewidget) return; if(waschanged) { switch(MessageBox::warning(NULL, @@ -297,21 +297,21 @@ void MacroWindow::collapseWrapper() doCommit(); break; case MessageBox::Close: - collapse(); + animateToWidget(resumewidget); break; case MessageBox::Cancel: default: break; } } else { - collapse(); + animateToWidget(resumewidget); } } void MacroWindow::toggleMacro() { if(!active) return; - if(isCollapsed()) { + if(currentWidget() == resumewidget) { expandWrapper(); } else { collapseWrapper(); @@ -338,7 +338,7 @@ void MacroWindow::setActive(bool active) void MacroWindow::clear() { // DEBUG(macrowindow, "clear %p\n", this); - setExpandedWidget(NULL); + //setExpandedWidget(NULL); if(mainwidget) delete mainwidget; mainwidget = NULL; @@ -346,8 +346,10 @@ void MacroWindow::clear() // lua->clear(); } -void MacroWindow::collapsed() +void MacroWindow::animated(QWidget *w) { - DEBUG(macrowindow, "collapsed %p\n", this); - clear(); + if(w == resumewidget) { + DEBUG(macrowindow, "collapsed %p\n", this); + clear(); + } } diff --git a/client/macrowindow.h b/client/macrowindow.h index 28fc172..f57fd05 100644 --- a/client/macrowindow.h +++ b/client/macrowindow.h @@ -32,10 +32,10 @@ #include "collapser.h" #include "netcom.h" +#include "resumewidget.h" class LUA; class Widget; -class ResumeWidget; class MacroWindow : public Collapser { Q_OBJECT @@ -51,6 +51,8 @@ public: void setActive(bool active); + ResumeWidget *resumewidget; + QString macrotitle; public slots: @@ -70,7 +72,7 @@ signals: void activationChanged(bool); private slots: - void collapsed(); + void animated(QWidget*); private: void initMacro(QDomNode &node); @@ -82,7 +84,6 @@ private: QString templ; QString version; Widget *mainwidget; - ResumeWidget *resumewidget; void clear(); diff --git a/client/mainwindow.cc b/client/mainwindow.cc index 31039d4..6ee17c8 100644 --- a/client/mainwindow.cc +++ b/client/mainwindow.cc @@ -335,9 +335,10 @@ void MainWindow::update() if(m1 && m2 && m1 != m2 && _m2.isstatic == false) { // Remove old connection (if any), to avoid multiple connections. - disconnect(m1, SIGNAL(expanding()), m2, SLOT(collapseWrapper())); + disconnect(m1, SIGNAL(animating(QWidget*)), + m2, SLOT(collapseWrapper())); - connect(m1, SIGNAL(expanding()), m2, SLOT(collapseWrapper())); + connect(m1, SIGNAL(animating(QWidget*)), m2, SLOT(collapseWrapper())); } j++; -- cgit v1.2.3