From d86b0e81bd9510391aa66d7331999225a58f630d Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Fri, 13 Nov 2009 17:08:31 +0530 Subject: dont create the frame and the title bar as qgraphicsitems, let's paint them ourselves --- webscrap.cpp | 73 ++++++++++++++++++++++++++++++------------------------------ webscrap.h | 9 +++++--- 2 files changed, 43 insertions(+), 39 deletions(-) diff --git a/webscrap.cpp b/webscrap.cpp index cacea7f..a4979f1 100644 --- a/webscrap.cpp +++ b/webscrap.cpp @@ -222,31 +222,21 @@ WebScrapContainer::WebScrapContainer(WebScrap *scrap, QGraphicsScene *scene, qre scene->addItem(this); // create the frame - m_frame = new QGraphicsPathItem(this); - QPainterPath path; QRect scrapRect = scrap->scrapRect(); QRect containerRect(0, 0, (scrapRect.width() * xScale) + s_padding * 2, (scrapRect.height() * yScale) + s_titlePadding + s_padding); setGeometry(containerRect); - path.addRoundedRect(containerRect, 10, 10); - m_frame->setPath(path); + setBoundingRect(containerRect); setFlag(QGraphicsItem::ItemIsMovable, true); - scrap->setParentItem(m_frame); - scrap->setPos(s_padding, s_titlePadding); - - m_frame->setPen(Qt::NoPen); - m_frame->setBrush(palette().background()); + setCursor(Qt::SizeAllCursor); - m_frame->setFlag(QGraphicsItem::ItemClipsChildrenToShape, true); - m_frame->installSceneEventFilter(this); - m_frame->setCursor(Qt::SizeAllCursor); + scrap->setParentItem(this); + scrap->setPos(s_padding, s_titlePadding); - // create the title bar - m_titlebar = new QGraphicsTextItem(m_frame); - m_titlebar->setPlainText(scrap->title()); - m_titlebar->setPos(10, 0); - m_titlebar->setDefaultTextColor(palette().color(QPalette::Foreground)); + // for paint + m_backgroundBrush = palette().background(); + m_titleBrush = palette().foreground(); connect(scrap, SIGNAL(titleChanged(QString)), SLOT(setTitle(QString))); // create the tool bar @@ -274,18 +264,21 @@ WebScrapContainer::WebScrapContainer(WebScrap *scrap, QGraphicsScene *scene, qre setGraphicsEffect(m_glow); } +void WebScrapContainer::setBoundingRect(QRectF rect) { + m_boundingRect = rect; +} + QRectF WebScrapContainer::boundingRect() const { - return m_frame->boundingRect(); + return m_boundingRect; } void WebScrapContainer::setTitle(const QString& title) { - m_titlebar->setPlainText(title); + m_title = title; } -bool WebScrapContainer::sceneEventFilter(QGraphicsItem *watched, QEvent* event) { - if (watched == m_frame && event->type() == QEvent::GraphicsSceneMousePress) - emit frameClicked(); - return false; +void WebScrapContainer::mousePressEvent(QGraphicsSceneMouseEvent *event) { + emit frameClicked(); + QGraphicsWidget::mousePressEvent(event); } void WebScrapContainer::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { @@ -328,10 +321,10 @@ void WebScrapContainer::createToolbar() { m_toolbar->setVisible(true); m_toolbar->setGraphicsEffectsEnabled(false); - m_toolbar->setParentItem(m_frame); + m_toolbar->setParentItem(this); m_toolbar->setZValue(900); m_toolbar->layout()->activate(); - m_toolbar->setPos(m_frame->pos() + m_frame->boundingRect().topRight() - QPointF(m_toolbar->rect().width(), 0)); + m_toolbar->setPos(boundingRect().topRight() - QPointF(m_toolbar->rect().width(), 0)); m_toolbar->setFlag(QGraphicsItem::ItemIsMovable, false); m_toolbar->setBackgroundBrush(palette().background()); @@ -380,7 +373,7 @@ void WebScrapContainer::createEditToolbar() { m_editToolbar->setParentItem(this); m_editToolbar->setZValue(901); m_editToolbar->layout()->activate(); - m_editToolbar->setPos(m_frame->pos() + m_frame->boundingRect().topRight() - QPointF(m_editToolbar->boundingRect().width() + 30, -30)); + m_editToolbar->setPos(boundingRect().topRight() - QPointF(m_editToolbar->boundingRect().width() + 30, -30)); m_editToolbar->setFlag(QGraphicsItem::ItemIsMovable, false); m_editToolbar->setVisible(false); m_editToolbar->setBackgroundBrush(palette().background()); @@ -428,11 +421,9 @@ void WebScrapContainer::handleResetZoomClicked() { m_scrapScale->setYScale(1.0); QSize scrapSize = webScrap()->size().toSize(); setGeometry(geometry().left(), geometry().top(), scrapSize.width() + s_padding * 2, scrapSize.height() + s_titlePadding + s_padding); - QPainterPath path; - path.addRoundedRect(0, 0, scrapSize.width() + s_padding * 2 * 2, scrapSize.height() + s_titlePadding + s_padding, s_padding * 2, s_padding * 2); - m_frame->setPath(path); - m_toolbar->setPos(m_frame->pos() + m_frame->boundingRect().topRight() - QPointF(m_toolbar->rect().width(), 0)); - m_editToolbar->setPos(m_frame->pos() + m_frame->boundingRect().topRight() - QPointF(m_editToolbar->boundingRect().width() + 30, -30)); + setBoundingRect(QRectF(0, 0, scrapSize.width() + s_padding * 2 * 2, scrapSize.height() + s_titlePadding + s_padding)); + m_toolbar->setPos(boundingRect().topRight() - QPointF(m_toolbar->rect().width(), 0)); + m_editToolbar->setPos(boundingRect().topRight() - QPointF(m_editToolbar->boundingRect().width() + 30, -30)); m_resizer->setRect(m_scrap->boundingRect()); } @@ -449,11 +440,9 @@ void WebScrapContainer::handleRectResized(QRectF rect) { } QSize scrapSize = QSize(rect.width() * m_scrapScale->xScale(), rect.height() * m_scrapScale->yScale()); setGeometry(geometry().left(), geometry().top(), scrapSize.width() + s_padding * 2, scrapSize.height() + s_titlePadding + s_padding); - QPainterPath path; - path.addRoundedRect(0, 0, scrapSize.width() + s_padding * 2, scrapSize.height() + s_titlePadding + s_padding, 10, 10); - m_frame->setPath(path); - m_toolbar->setPos(m_frame->pos() + m_frame->boundingRect().topRight() - QPointF(m_toolbar->rect().width(), 0)); - m_editToolbar->setPos(m_frame->pos() + m_frame->boundingRect().topRight() - QPointF(m_editToolbar->boundingRect().width() + 30, -30)); + setBoundingRect(QRectF(0, 0, scrapSize.width() + s_padding * 2, scrapSize.height() + s_titlePadding + s_padding)); + m_toolbar->setPos(boundingRect().topRight() - QPointF(m_toolbar->rect().width(), 0)); + m_editToolbar->setPos(boundingRect().topRight() - QPointF(m_editToolbar->boundingRect().width() + 30, -30)); } WebScrap* WebScrapContainer::webScrap() const { @@ -502,3 +491,15 @@ void WebScrapContainer::setLeaveAnimation(QPropertyAnimation *animation) { QPropertyAnimation* WebScrapContainer::leaveAnimation() const { return m_leaveAnimation; } + +void WebScrapContainer::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) { + painter->setPen(Qt::NoPen); + painter->setBrush(m_backgroundBrush); + painter->drawRoundedRect(boundingRect(), 10, 10); + painter->setPen(QPen(m_titleBrush, 0)); + painter->setBrush(Qt::NoBrush); + QString title = painter->fontMetrics().elidedText(m_title, Qt::ElideRight, option->rect.width() - m_toolbar->rect().width()); + int height = painter->fontMetrics().height(); + painter->drawText(option->rect.adjusted(10, (s_titlePadding - height) / 2, 0, 0), title); +} + diff --git a/webscrap.h b/webscrap.h index d90811b..aad2d9f 100644 --- a/webscrap.h +++ b/webscrap.h @@ -68,6 +68,7 @@ public: WebScrapContainer(WebScrap *scrap, QGraphicsScene *scene, qreal xScale = 1.0, qreal yScale = 1.0); void createToolbar(); void createEditToolbar(); + void setBoundingRect(QRectF rect); QRectF boundingRect() const; WebScrap* webScrap() const; QGraphicsDropShadowEffect *glow() const; @@ -77,6 +78,7 @@ public: QPropertyAnimation* enterAnimation() const; void setLeaveAnimation(QPropertyAnimation *animation); QPropertyAnimation *leaveAnimation() const; + void paint(QPainter* painter, const QStyleOptionGraphicsItem* options, QWidget* widget = 0); public slots: void setTitle(const QString& title); @@ -88,7 +90,7 @@ public slots: void handleResetZoomClicked(); protected: - bool sceneEventFilter(QGraphicsItem *watched, QEvent* event); + void mousePressEvent(QGraphicsSceneMouseEvent *event); void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); signals: @@ -98,8 +100,6 @@ signals: private: QGraphicsScene *m_scene; WebScrap *m_scrap; - QGraphicsPathItem *m_frame; - QGraphicsTextItem *m_titlebar; GraphicsToolBar *m_toolbar; QToolButton *m_closeButton, *m_adjustButton, *m_reloadButton; GraphicsToolBar *m_editToolbar; @@ -111,6 +111,9 @@ private: QGraphicsDropShadowEffect *m_glow; bool m_located, m_searchMatched; QPropertyAnimation *m_enterAnimation, *m_leaveAnimation; + QString m_title; + QBrush m_titleBrush, m_backgroundBrush; + QRectF m_boundingRect; }; Q_DECLARE_METATYPE(WebScrapContainer*) -- cgit v1.2.3