[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: branches/work/goya
From: Rafael Fernández López <ereslibre () kde ! org>
Date: 2008-03-10 15:55:21
Message-ID: 1205164521.412744.15900.nullmailer () svn ! kde ! org
[Download RAW message or body]
SVN commit 784085 by ereslibre:
Real code starts to pop up. Now we have less hard-coded testing code and we check
correctly the hovered widgets.
M +66 -14 goya/goya.cpp
M +6 -6 goya/goya.h
M +9 -4 goya/goya_p.h
M +2 -2 goya/widget.cpp
M +1 -1 goya/widget.h
M +6 -3 tests/main.cpp
--- branches/work/goya/goya/goya.cpp #784084:784085
@@ -31,11 +31,11 @@
#include <QBitmap>
#include <QLayout>
#include <QPainter>
+#include <QScrollBar>
#include <QKeyEvent>
#include <QStyleOption>
#include <QCoreApplication>
#include <QAbstractItemView>
-#include <QStyleOptionButton>
#include "widget.h"
#include "widget_p.h"
@@ -65,6 +65,43 @@
{
}
+// When receiving move events on the viewport we need to check if we should
+// post events to certain widgets like "enter", "leave"...
+void Canvas::Private::analyzeInternalMouseEvents(const QModelIndex &index,
+ const QStyleOptionViewItem &option,
+ QMouseEvent *mouseEvent)
+{
+ if (!index.isValid()) {
+ return;
+ }
+
+ QRect mappedRect;
+
+ if (hoveredWidget) {
+ mappedRect = \
QRect(itemView->viewport()->mapToGlobal(q->widgetRect(hoveredWidget, option, \
index).topLeft()), + \
itemView->viewport()->mapToGlobal(q->widgetRect(hoveredWidget, option, \
index).bottomRight())); + }
+
+ if (hoveredWidget &&
+ !mappedRect.contains(mouseEvent->globalPos())) {
+ QCoreApplication::postEvent(hoveredWidget->widget(), new \
QEvent(QEvent::Leave)); + hoveredWidget = 0;
+ }
+
+ QList<Widget*> widgetList = q->widgetsForIndex(index);
+
+ foreach (Widget *widget, widgetList) {
+ QRect mappedRect = \
QRect(itemView->viewport()->mapToGlobal(q->widgetRect(widget, option, \
index).topLeft()), + \
itemView->viewport()->mapToGlobal(q->widgetRect(widget, option, \
index).bottomRight())); +
+ if (mappedRect.contains(mouseEvent->globalPos())) {
+ hoveredWidget = widget;
+ QCoreApplication::postEvent(hoveredWidget->widget(), new \
QEvent(QEvent::Enter)); + break;
+ }
+ }
+}
+
void Canvas::Private::slotCurrentChanged(const QModelIndex ¤tIndex, const \
QModelIndex &previousIndex) {
this->currentIndex = currentIndex;
@@ -135,19 +172,18 @@
return d->focusedWidget;
}
-QSize Canvas::widgetSize(Widget *widget, const QModelIndex &index) const
+QSize Canvas::widgetSize(Widget *widget, const QStyleOptionViewItem &option, const \
QModelIndex &index) const {
- return widget->widgetSize(index);
+ return widget->widgetSize(index, option);
}
-QRect Canvas::widgetRect(Widget *widget, const QStyleOption *option,
- const QModelIndex &index) const
+QRect Canvas::widgetRect(Widget *widget, const QStyleOptionViewItem &option, const \
QModelIndex &index) const {
- QRect retRect = QRect(widgetPosition(widget, index), widgetSize(widget, index));
- retRect.setTop(option->rect.top() + retRect.top());
- retRect.setBottom(option->rect.top() + retRect.bottom());
- retRect.setLeft(option->rect.left() + retRect.left());
- retRect.setRight(option->rect.left() + retRect.right());
+ QRect retRect = QRect(widgetPosition(widget, option, index), widgetSize(widget, \
option, index)); + retRect.setTop(option.rect.top() + retRect.top());
+ retRect.setBottom(option.rect.top() + retRect.bottom());
+ retRect.setLeft(option.rect.left() + retRect.left());
+ retRect.setRight(option.rect.left() + retRect.right());
return retRect;
}
@@ -177,7 +213,7 @@
}
foreach (Widget *widget, widgetList) {
- QPoint widgetPos = widgetPosition(widget, index);
+ QPoint widgetPos = widgetPosition(widget, option, index);
QSize widgetSize;
if (QLayout *l = widget->widget()->layout()) {
@@ -252,7 +288,11 @@
}
if (d->itemModel && (d->itemModel->rowCount() > 0)) {
- QList<Widget*> widgetList = widgetsForIndex(d->itemModel->index(1, 0));
+ QList<Widget*> widgetList;
+
+ if (d->hoveredIndex.isValid()) {
+ widgetList = widgetsForIndex(d->hoveredIndex);
+ }
switch (event->type()) {
case QEvent::Enter: {
@@ -264,10 +304,12 @@
d->itemView->viewport()->update();
break;
case QEvent::Leave: {
- foreach (Widget *widget, widgetList) {
- QCoreApplication::postEvent(widget->widget(),
+ if (d->hoveredWidget) {
+ QCoreApplication::postEvent(d->hoveredWidget->widget(),
new QEvent(QEvent::Leave));
}
+ d->hoveredIndex = QModelIndex();
+ d->hoveredWidget = 0;
}
d->itemView->viewport()->update();
break;
@@ -289,6 +331,16 @@
}
d->itemView->viewport()->update();
break;
+ case QEvent::MouseMove: {
+ QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event);
+ d->hoveredIndex = \
d->itemView->indexAt(d->itemView->viewport()->mapFromGlobal(mouseEvent->globalPos()));
+ QStyleOptionViewItem option;
+ option.rect = d->itemView->visualRect(d->hoveredIndex);
+
+ d->analyzeInternalMouseEvents(d->hoveredIndex, option, \
mouseEvent); + }
+ d->itemView->viewport()->update();
+ break;
case QEvent::MouseButtonPress: {
QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event);
foreach (Widget *widget, widgetList) {
--- branches/work/goya/goya/goya.h #784084:784085
@@ -92,7 +92,7 @@
* Returns the top left point in which @p widget will be drawn for
* @p index with @p option.
*/
- virtual QPoint widgetPosition(Widget *widget, const QModelIndex &index) const = \
0; + virtual QPoint widgetPosition(Widget *widget, const QStyleOptionViewItem \
&option, const QModelIndex &index) const = 0;
/**
* Returns the size that the painter will use when painting @p widget.
@@ -101,16 +101,16 @@
* to fit all its contents. If you want all the widgets to be the same size
* despite having different contents, you need to reimplement this method.
*/
- virtual QSize widgetSize(Widget *widget, const QModelIndex &index) const;
+ virtual QSize widgetSize(Widget *widget, const QStyleOptionViewItem &option, \
const QModelIndex &index) const;
/**
- * Returns the rect that @p widget needs to be painted for @p index with
- * @p option. This is automatically calculated from widgetPosition() and
- * widgetSize() methods.
+ * Returns the rect that @p widget needs to be painted for @p index.
+ * This is automatically calculated from widgetPosition() and widgetSize()
+ * methods.
*
* @see widgetPosition, widgetSize
*/
- QRect widgetRect(Widget *widget, const QStyleOption *option, const QModelIndex \
&index) const; + QRect widgetRect(Widget *widget, const QStyleOptionViewItem \
&option, const QModelIndex &index) const;
/**
* Returns the state of the canvas.
--- branches/work/goya/goya/goya_p.h #784084:784085
@@ -22,10 +22,12 @@
#define GOYA_P_H
#include <QObject>
+#include <QStyleOptionViewItem>
class QAbstractItemView;
class QAbstractItemModel;
class QItemSelectionModel;
+class QMouseEvent;
namespace Goya
{
@@ -43,6 +45,13 @@
Private(Canvas *q, QObject *parent = 0);
~Private();
+ void analyzeInternalMouseEvents(const QModelIndex &index, const \
QStyleOptionViewItem &option, QMouseEvent *mouseEvent); +
+public Q_SLOTS:
+ void slotCurrentChanged(const QModelIndex ¤tIndex, const QModelIndex \
&previousIndex); + void slotSelectionModelDestroyed();
+
+public:
QAbstractItemView *itemView;
QAbstractItemModel *itemModel;
QModelIndex hoveredIndex;
@@ -56,10 +65,6 @@
CanvasState canvasState;
Canvas *q;
-
-public Q_SLOTS:
- void slotCurrentChanged(const QModelIndex ¤tIndex, const QModelIndex \
&previousIndex);
- void slotSelectionModelDestroyed();
};
--- branches/work/goya/goya/widget.cpp #784084:784085
@@ -87,9 +87,9 @@
d->widget = widget;
}
-QSize Widget::widgetSize(const QModelIndex &index) const
+QSize Widget::widgetSize(const QModelIndex &index, const QStyleOptionViewItem \
&option) const {
- return QSize(50, 25);
+ return d->widget->sizeHint();
}
bool Widget::event(QEvent *event, QStyleOption *option, const QModelIndex &index)
--- branches/work/goya/goya/widget.h #784084:784085
@@ -65,7 +65,7 @@
QWidget *widget() const;
void setWidget(QWidget *widget);
- virtual QSize widgetSize(const QModelIndex &index) const;
+ virtual QSize widgetSize(const QModelIndex &index, const QStyleOptionViewItem \
&option) const;
virtual bool event(QEvent *event, QStyleOption *option, const QModelIndex \
&index);
--- branches/work/goya/tests/main.cpp #784084:784085
@@ -26,7 +26,7 @@
QSize sizeHint() const
{
- return QSize(50, 50);
+ return QSize(30, 30);
}
protected:
@@ -34,7 +34,10 @@
{
QPainter p(this);
- QRadialGradient radialGrad(QPointF(15, 15), 15);
+ QRadialGradient radialGrad(QPointF(event->rect().width() / 2,
+ event->rect().height() / 2),
+ qMin(event->rect().width() / 2,
+ event->rect().height() / 2));
radialGrad.setColorAt(0, Qt::red);
radialGrad.setColorAt(1, Qt::transparent);
@@ -102,7 +105,7 @@
return QList<Goya::Widget*>();
}
- QPoint widgetPosition(Goya::Widget *widget, const QModelIndex &index) const
+ QPoint widgetPosition(Goya::Widget *widget, const QStyleOptionViewItem &option, \
const QModelIndex &index) const {
if (dynamic_cast<QPushButton*>(widget->widget()))
{
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic