qwwtaskpanel.cpp

00001 //
00002 // C++ Implementation: %{MODULE}
00003 //
00004 // Description:
00005 //
00006 //
00007 // Author: %{AUTHOR} <%{EMAIL}>, (C) %{YEAR}
00008 //
00009 // Copyright: See COPYING file that comes with this distribution
00010 //
00011 //
00012 #ifndef WW_NO_TASKPANEL
00013 #include "qwwtaskpanel.h"
00014 #include "qwwtaskpanel_p.h"
00015 
00016 TaskHeader::TaskHeader(QWidget *w, QWidget *parent) : QFrame(parent) {
00017     m_widget = w;
00018     setFrameShape(QFrame::StyledPanel);
00019     setFrameRect(rect().adjusted(0, 8, 0, 0));
00020     setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
00021     QHBoxLayout *l = new QHBoxLayout(this);
00022     l->setMargin(1);
00023     m_spacer = new QSpacerItem(0, 0, QSizePolicy::Fixed, QSizePolicy::Fixed);
00024     l->addItem(m_spacer);
00025     m_text = new QLabel;
00026     QFont f = m_text->font();
00027     f.setBold(true);
00028     m_text->setFont(f);
00029     m_button = new QToolButton;
00030     m_button->setObjectName("__qt__passive_button");
00031     m_button->setAutoRaise(true);
00032     m_button->setCheckable(true);
00033     m_button->setArrowType(Qt::DownArrow);
00034     l->addWidget(m_text);
00035     l->addWidget(m_button);
00036 }
00037 
00038 void TaskHeader::setToggleIcon(const QIcon &i) {
00039     m_button->setIcon(i);
00040     if (i.isNull()) {
00041         m_button->setArrowType(m_button->isChecked() ? Qt::UpArrow : Qt::DownArrow);
00042     } else {
00043         m_button->setArrowType(Qt::NoArrow);
00044     }
00045 }
00046 
00047 void TaskHeader::setTaskName(const QString &n) {
00048     m_text->setText(n);
00049     layout()->invalidate();
00050     update();
00051 }
00052 
00053 void TaskHeader::setIcon(const QIcon &i) {
00054     m_icon = i;
00055     if (i.isNull()) {
00056         m_spacer->changeSize(0,0, QSizePolicy::Fixed, QSizePolicy::Fixed);
00057     } else {
00058         m_spacer->changeSize(50,16, QSizePolicy::Fixed, QSizePolicy::Fixed);
00059     }
00060     m_spacer->invalidate();
00061     layout()->invalidate();
00062     update();
00063 }
00064 
00065 void TaskHeader::paintEvent(QPaintEvent *ev) {
00066     QFrame::paintEvent(ev);
00067     QPainter p(this);
00068     m_icon.paint(&p, QRect(2, 1, 32, 32), Qt::AlignCenter, isEnabled() ? QIcon::Normal : QIcon::Disabled);
00069 }
00070 
00071 QToolButton* TaskHeader::toggleButton() const {
00072     return m_button;
00073 }
00074 
00075 
00076 
00077 
00078 Task::Task(QWidget *body, QWidget *parent) : QWidget(parent) {
00079     m_body = body;
00080     m_animBody = 0;
00081     m_animator.setDuration(3000);
00082     m_animator.setCurveShape(QTimeLine::EaseInOutCurve);
00083     QVBoxLayout *l = new QVBoxLayout(this);
00084     l->setSpacing(0);
00085     l->setMargin(0);
00086     m_header = new TaskHeader(body);
00087 
00088     l->addWidget(m_header);
00089     l->addWidget(m_body);
00090 
00091     m_body->setVisible(false);
00092     m_body->installEventFilter(this);
00093     connect(m_header->toggleButton(), SIGNAL(toggled(bool)), this, SLOT(setOpen(bool)));
00094     connect(&m_animator, SIGNAL(frameChanged(int)), SLOT(animate(int)));
00095     connect(&m_animator, SIGNAL(finished()), SLOT(animFinished()));
00096 }
00097 
00098 void Task::setOpen(bool o) {
00099     QToolButton *b = m_header->toggleButton();
00100     if (b->isChecked() == o) {
00101         b->setChecked(o);
00102         if (b->arrowType()!=Qt::NoArrow) {
00103             if (o) {
00104                 b->setArrowType(Qt::UpArrow);
00105             } else {
00106                 b->setArrowType(Qt::DownArrow);
00107             }
00108         }
00109         if (m_animator.state()!=QTimeLine::NotRunning) {
00110             m_animator.setDirection(m_animator.direction()==QTimeLine::Forward ? QTimeLine::Backward : QTimeLine::Forward);
00111         } else {
00112             m_animBody = new QWidget;
00113             m_animBody->installEventFilter(this);
00114 #ifndef Q_WS_WIN
00115             m_animBody->setEnabled(false);
00116 #endif
00117             m_animBody->setAttribute(Qt::WA_NoSystemBackground, true);
00118             m_body->show();
00119             m_animpix = QPixmap::grabWidget(this);
00120 
00121             if (o) {
00122                 m_animator.setDirection(QTimeLine::Forward);
00123                 m_animBody->setFixedSize(width(), 0);
00124                 m_animator.setFrameRange(0, m_body->sizeHint().height());
00125             } else {
00126                 m_animator.setDirection(QTimeLine::Backward);
00127                 m_animBody->setFixedSize(width(), m_body->width());
00128                 m_animator.setFrameRange(0, m_body->height());
00129             }
00130             m_body->hide();
00131             QVBoxLayout *l = (QVBoxLayout*)layout();
00132             l->addWidget(m_animBody);
00133             m_animBody->show();
00134             m_animator.start();
00135         }
00136     }
00137 }
00138 
00139 bool Task::eventFilter(QObject *o, QEvent *e) {
00140     if (o==m_animBody && e->type()==QEvent::Paint) {
00141         QPainter p(m_animBody);
00142         p.drawPixmap(m_animBody->rect(), m_animpix, m_animpix.rect().adjusted(0, m_animpix.height()-m_animBody->height(), 0, 0));
00143         return true;
00144     }
00145 //     if (o==m_body && e->type()==QEvent::WindowTitleChange) {
00146 //         setName(m_body->windowTitle());
00147 //     }
00148 //     if (o==m_body && e->type()==QEvent::WindowIconChange) {
00149 //         setIcon(m_body->windowIcon());
00150 //     }
00151     return false;
00152 }
00153 
00154 void Task::animate(int frame ) {
00155     m_animBody->setFixedSize(width(), frame);
00156     m_animBody->updateGeometry();
00157     m_animBody->update();
00158 //     qDebug("Ramka: %d", frame);
00159 }
00160 
00161 void Task::animFinished() {
00162     m_animBody->hide();
00163     delete m_animBody;
00164     m_animBody = 0;
00165     if (m_animator.currentFrame()!=0) {
00166         m_body->show();
00167     }
00168 }
00169 
00170 
00179 QwwTaskPanel::QwwTaskPanel(QWidget *parent)
00180         : QScrollArea(parent) {
00181 //      m_animated = true;
00182     m_panel = new QWidget(viewport());
00183     m_panel->setObjectName("ww_taskpanel_panel");
00184     m_current = -1;
00185     QVBoxLayout *l = new QVBoxLayout(m_panel);
00186 
00187     l->addStretch(0);
00188     setWidget(m_panel);
00189     setWidgetResizable(true);
00190     setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);
00191 }
00192 
00193 
00194 QwwTaskPanel::~QwwTaskPanel() {}
00195 
00196 void QwwTaskPanel::addTask(QWidget * task, const QString & label) {
00197     insertTask(taskCount(), task, label);
00198 }
00199 
00200 void QwwTaskPanel::addTask(QWidget * task, const QIcon & icon, const QString & label) {
00201     insertTask(taskCount(), task, icon, label);
00202 }
00203 
00204 void QwwTaskPanel::insertTask(int index, QWidget * task, const QString & label) {
00205     insertTask(index, task, QIcon(), label);
00206 }
00207 
00208 void QwwTaskPanel::insertTask(int index, QWidget * task, const QIcon & icon, const QString & label) {
00209     if (!task) return;
00210     Task *tsk = new Task(task);
00211     tsk->setToggleIcon(m_toggleIcon);
00212     if (label.isNull())
00213         tsk->setName(task->windowTitle());
00214     else {
00215         tsk->setName(label);
00216         task->setWindowTitle(label);
00217     }
00218     if (icon.isNull()) {
00219         tsk->setIcon(task->windowIcon());
00220     } else {
00221         tsk->setIcon(icon);
00222         task->setWindowIcon(icon);
00223     }
00224     static_cast<QBoxLayout*>(m_panel->layout())->insertWidget(index, tsk);
00225     m_tasks.insert(index, tsk);
00226 
00227     if (m_tasks.count()==1) {
00228         setCurrentIndex(0);
00229     }
00230      tsk->show();
00231 }
00232 
00233 int QwwTaskPanel::taskCount() const {
00234     return m_tasks.count();
00235 }
00236 
00237 void QwwTaskPanel::removeTask(int index) {
00238     if (index < 0 || index>=m_tasks.count()) return;
00239     Task *tsk = static_cast<Task*>(m_tasks.at(index));
00240     m_tasks.removeAt(index);
00241     if (m_tasks.count()<=index) {
00242         setCurrentIndex(m_tasks.count()-1);
00243     }
00244     QWidget *body = tsk->body();
00245     body->setParent(this);
00246     delete tsk;
00247 }
00248 
00249 QWidget * QwwTaskPanel::task(int index) const {
00250     if (index < 0 || index>=m_tasks.count()) return 0;
00251     Task *tsk = static_cast<Task*>(m_tasks.at(index));
00252     return tsk ? tsk->body() : 0;
00253 }
00254 
00255 int QwwTaskPanel::indexOf(QWidget * task) const {
00256     for (int i=0;i<m_tasks.count();i++) {
00257         Task *tsk = static_cast<Task*>(m_tasks.at(i));
00258         if (task == tsk) return i;
00259     }
00260     return -1;
00261 }
00262 
00263 void QwwTaskPanel::setToggleIcon(const QIcon & icon) {
00264     m_toggleIcon = icon;
00265     foreach(QWidget *tsk, m_tasks) {
00266         static_cast<Task*>(tsk)->setToggleIcon(icon);
00267     }
00268 }
00269 
00270 void QwwTaskPanel::setCurrentIndex(int index) {
00271     if (index<0 || index>=m_tasks.count() || index==m_current)
00272         return;
00273     if (m_current!=-1) {
00274         Task *tsk = static_cast<Task*>(m_tasks.at(m_current));
00275         tsk->setOpen(false);
00276     }
00277     m_current = index;
00278     Task *tsk = static_cast<Task*>(m_tasks.at(index));
00279     tsk->setOpen(true);
00280     emit currentIndexChanged(index);
00281 }
00282 
00283 QWidget * QwwTaskPanel::currentTask() const {
00284     return task(currentIndex());
00285 }
00286 
00287 void QwwTaskPanel::setTaskIcon(int index, const QIcon & icon) {
00288     if (index < 0 || index>=m_tasks.count()) return;
00289     Task *tsk = dynamic_cast<Task*>(m_tasks.at(index));
00290     if (!tsk) return;
00291     tsk->setIcon(icon);
00292 
00293 }
00294 
00295 void QwwTaskPanel::setTaskTitle(int index, const QString & title) {
00296     if (index < 0 || index>=m_tasks.count()) return;
00297     Task *tsk = dynamic_cast<Task*>(m_tasks.at(index));
00298     if (!tsk) return;
00299     tsk->setName(title);
00300 }
00301 
00302 void QwwTaskPanel::setTaskName(int index, const QString & name) {
00303     if (index < 0 || index>=m_tasks.count()) return;
00304     Task *tsk = dynamic_cast<Task*>(m_tasks.at(index));
00305     if (!tsk) return;
00306     tsk->body()->setObjectName(name);
00307 }
00308 #endif

Generated on Sat Apr 21 21:54:36 2007 for wwWidgets by  doxygen 1.5.1