Fluid Tabs

ftabbar.cpp

00001 #include <QDeclarativeComponent>
00002 #include <QDeclarativeContext>
00003 #include <QDeclarativeEngine>
00004 #include <QPainter>
00005 #include "ftabbar.h"
00006 
00015 FTab::FTab(QDeclarativeItem *parent)
00016     : QDeclarativeItem(parent),
00017       m_item(0)
00018 {
00019     setFlag(QGraphicsItem::ItemHasNoContents, false);
00020     setAcceptedMouseButtons(Qt::LeftButton);
00021 }
00022 
00030 void FTab::setTitle(const QString &title)
00031 {
00032     if (m_title != title) {
00033         m_title = title;
00034         emit titleChanged();
00035     }
00036 }
00037 
00046 void FTab::setDelegate(QDeclarativeComponent *delegate)
00047 {
00048     if (!delegate)
00049         return;
00050     QDeclarativeEngine *engine = delegate->creationContext()->engine();
00051     QDeclarativeContext *context = new QDeclarativeContext(engine, this);
00052     context->setContextObject(this);
00053     QObject *object = delegate->create(context);
00054     m_item = qobject_cast<QDeclarativeItem *>(object);
00055 }
00056 
00057 inline void FTab::setItemProperty(const char *property, const QVariant &value)
00058 {
00059     if (!m_item)
00060         return;
00061     m_item->setProperty(property, value);
00062 }
00063 
00064 inline QVariant FTab::itemProperty(const char *property) const
00065 {
00066     return m_item ? m_item->property(property) : QVariant();
00067 }
00068 
00072 void FTab::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
00073 {
00074     if (!m_item)
00075         return;
00076     paintChildren(qobject_cast<QDeclarativeItem *>(m_item), painter, option, widget);
00077 }
00078 
00079 void FTab::paintChildren(QDeclarativeItem *item, QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
00080 {
00081     if (!item)
00082         return;
00083     painter->save();
00084     painter->translate(item->x(), item->y());
00085     item->paint(painter, option, widget);
00086     QObjectList c = item->children();
00087     for (QObjectList::const_iterator i = c.constBegin(); i != c.constEnd(); ++i)
00088         paintChildren(qobject_cast<QDeclarativeItem *>(*i), painter, option, widget);
00089     painter->restore();
00090 }
00091 
00095 QRectF FTab::boundingRect() const
00096 {
00097     return m_item ? m_item->boundingRect() : QRectF();
00098 }
00099 
00103 void FTab::mousePressEvent(QGraphicsSceneMouseEvent *event)
00104 {
00105     emit tabSelected();
00106     QDeclarativeItem::mousePressEvent(event);
00107 }
00108 
00128 FTabBar::FTabBar(QDeclarativeItem *parent)
00129     : QDeclarativeItem(parent),
00130       m_orientation(Qt::Horizontal),
00131       m_current(-1),
00132       m_tabMargin(0),
00133       m_delegate(0),
00134       m_item(0)
00135 {
00136 }
00137 
00145 void FTabBar::setOrientation(Qt::Orientation orientation)
00146 {
00147     if (m_orientation != orientation) {
00148         m_orientation = orientation;
00149         emit orientationChanged();
00150     }
00151 }
00152 
00160 void FTabBar::setDelegate(QDeclarativeComponent *delegate)
00161 {
00162     if (m_delegate != delegate) {
00163         m_delegate = delegate;
00164         emit delegateChanged();
00165     }
00166 }
00167 
00179 void FTabBar::setTabMargin(qreal margin)
00180 {
00181     m_tabMargin = margin;
00182 }
00183 
00184 qreal FTabBar::tabMargin() const
00185 {
00186     return m_tabMargin;
00187 }
00188 
00192 void FTabBar::componentComplete()
00193 {
00194     if (!m_delegate)
00195         return;
00196     QList<FTab *> tabs = findChildren<FTab *>();
00197     qreal p = 0;
00198     QRectF geometry;
00199     for (QList<FTab *>::const_iterator i = tabs.constBegin(); i != tabs.constEnd(); ++i) {
00200         FTab *tab = *i;
00201         m_tabs.push_back(tab);
00202         QObject::connect(tab, SIGNAL(tabSelected()), this, SLOT(activate()));
00203         tab->setDelegate(m_delegate);
00204         if (Qt::Horizontal == m_orientation) {
00205             tab->setProperty("x", p);
00206             p += tab->itemProperty("width").toReal() + m_tabMargin;
00207         } else {
00208             tab->setProperty("y", p);
00209             p += tab->itemProperty("height").toReal() + m_tabMargin;
00210         }
00211         const QPointF pos = tab->pos();
00212         geometry = geometry.united(tab->boundingRect().translated(pos.x(), pos.y()));
00213     }
00214     setWidth(geometry.width());
00215     setHeight(geometry.height());
00216     if (m_tabs.count() > 0) {
00217         m_current = 0;
00218         FTab *t = m_tabs.first();
00219         t->setItemProperty("state", "active");
00220         t->update();
00221     }
00222 }
00223 
00228 void FTabBar::activate()
00229 {
00230     QObject *sender = this->sender();
00231     FTab *tab = qobject_cast<FTab *>(sender);
00232     if (!tab)
00233         return;
00234     const int i = m_tabs.indexOf(tab);
00235     if (m_current != i) {
00236         FTab *t = m_tabs.value(m_current);
00237         if (t) {
00238             t->setItemProperty("state", "");
00239             t->update();
00240         }
00241         tab->setItemProperty("state", "active");
00242         tab->update();
00243         emit tabActivated(i);
00244         m_current = i;
00245     }
00246 }
00247 
 All Classes Functions Properties