39 #include <QAbstractItemModel> 40 #include <QAbstractProxyModel> 41 #include <QItemSelectionModel> 42 #include <QGraphicsSceneMouseEvent> 43 #include <QGraphicsLineItem> 60 Updater(
bool* u ) : u_ptr( u ), oldval( *u ) {
68 #if QT_VERSION < 0x050000 70 :
BASE( parent, scene ), m_isupdating( false )
77 :
BASE( parent, scene ), m_index( idx ), m_isupdating( false )
83 :
BASE( parent ), m_isupdating(
false )
86 scene->addItem(
this );
92 :
BASE( parent ), m_index( idx ), m_isupdating(
false )
96 scene->addItem(
this );
105 void GraphicsItem::init()
107 #if QT_VERSION >= QT_VERSION_CHECK(4,4,0) 108 setCacheMode( QGraphicsItem::DeviceCoordinateCache );
110 setFlags( ItemIsMovable|ItemIsSelectable|ItemIsFocusable );
111 #if QT_VERSION < 0x050000 112 setAcceptsHoverEvents(
true );
114 setAcceptHoverEvents(
true );
116 setHandlesChildEvents(
true );
136 qDebug() <<
"Item" << m_index.model()->data( m_index, Qt::DisplayRole ).toString()
137 <<
", ends="<<m_endConstraints.size() <<
", starts="<<m_startConstraints.size();
144 QVariant da = m_index.model()->data( m_index, Qt::TextAlignmentRole );
145 if ( da.isValid() ) {
146 opt.displayAlignment =
static_cast< Qt::Alignment
>( da.toInt() );
156 opt.
text = m_index.model()->data( m_index, Qt::DisplayRole ).toString();
157 if ( isEnabled() ) opt.state |= QStyle::State_Enabled;
158 if ( isSelected() ) opt.state |= QStyle::State_Selected;
159 if ( hasFocus() ) opt.state |= QStyle::State_HasFocus;
165 return qobject_cast<
GraphicsScene*>( QGraphicsItem::scene() );
171 qDebug() <<
"GraphicsItem::setRect("<<r<<
"), txt="<<m_index.model()->data( m_index, Qt::DisplayRole ).toString();
172 if ( m_index.model()->data( m_index, Qt::DisplayRole ).toString() == QLatin1String(
"Code Freeze" ) ) {
173 qDebug() <<
"gotcha";
177 prepareGeometryChange();
179 updateConstraintItems();
185 prepareGeometryChange();
192 return !
scene()->
isReadOnly() && m_index.model()->flags( m_index ) & Qt::ItemIsEditable;
201 *
static_cast<QStyleOption*
>(&opt) = *static_cast<const QStyleOption*>( option );
220 return m_boundingrect;
223 QPointF GraphicsItem::startConnector(
int relationType )
const 225 switch ( relationType ) {
228 return mapToScene( m_rect.left(), m_rect.top()+m_rect.height()/2. );
232 return mapToScene( m_rect.right(), m_rect.top()+m_rect.height()/2. );
235 QPointF GraphicsItem::endConnector(
int relationType )
const 237 switch ( relationType ) {
240 return mapToScene( m_rect.right(), m_rect.top()+m_rect.height()/2. );
244 return mapToScene( m_rect.left(), m_rect.top()+m_rect.height()/2. );
248 void GraphicsItem::constraintsChanged()
250 if ( !
scene() || !
scene()->itemDelegate() )
return;
259 m_startConstraints << item;
260 item->
setStart( startConnector( item->constraint().relationType() ) );
261 constraintsChanged();
267 m_endConstraints << item;
268 item->
setEnd( endConnector( item->constraint().relationType() ) );
269 constraintsChanged();
275 m_startConstraints.removeAll( item );
276 constraintsChanged();
282 m_endConstraints.removeAll( item );
283 constraintsChanged();
286 void GraphicsItem::updateConstraintItems()
303 Updater updater( &m_isupdating );
312 setPos( QPointF( s.
start(), rowGeometry.
start() ) );
319 if ( maxh < rowGeometry.
length() ) {
321 const Qt::Alignment align = getStyleOption().displayAlignment;
322 if ( align & Qt::AlignTop ) {
324 }
else if ( align & Qt::AlignBottom ) {
325 r.setY( rowGeometry.
length()-maxh );
328 r.setY( ( rowGeometry.
length()-maxh ) / 2. );
341 QPointF newPos=value.toPointF();
343 newPos.setY( pos().y() );
348 }
else if ( change==QGraphicsItem::ItemSelectedChange ) {
349 if (
index().isValid() && !(
index().model()->flags(
index() ) & Qt::ItemIsSelectable ) ) {
351 return qVariantFromValue(
false );
354 if ( value.toBool() ) {
361 return QGraphicsItem::itemChange( change, value );
370 void GraphicsItem::updateModel()
374 QAbstractItemModel* model =
const_cast<QAbstractItemModel*
>(
index().model() );
385 it1 != m_startConstraints.end() ;
387 constraints.push_back((*it1)->proxyConstraint());
389 it2 != m_endConstraints.end() ;
391 constraints.push_back((*it2)->proxyConstraint());
409 setCursor( Qt::SizeHorCursor );
415 setCursor( Qt::SizeHorCursor );
421 setCursor( Qt::SplitHCursor );
450 m_presspos =
event->pos();
451 m_pressscenepos =
event->scenePos();
454 switch ( m_istate ) {
458 BASE::mousePressEvent( event );
469 if ( !m_presspos.isNull() ) {
472 delete m_dragline; m_dragline = 0;
473 if (
scene()->dragSource() ) {
476 if ( other &&
scene()->dragSource()!=other &&
482 QRectF itemRect = other->
rect().adjusted(-other->
rect().height()/2.0, 0, 0, 0 );
483 if ( other->mapToScene( itemRect ).
boundingRect().contains( event->scenePos() ))
488 scene()->summaryHandlingModel()->mapToSource( other->
index() ), event->modifiers() );
495 other->mapToScene( other->
rect() ).
boundingRect().contains( event->scenePos() )) {
499 scene()->summaryHandlingModel()->mapToSource( other->
index() ), event->modifiers() );
508 updateItemFromMouse(event->scenePos());
514 m_presspos = QPointF();
522 m_presspos = QPointF();
523 BASE::mouseReleaseEvent( event );
534 BASE::mouseDoubleClickEvent( event );
537 void GraphicsItem::updateItemFromMouse(
const QPointF& scenepos )
540 const QPointF p = scenepos - m_presspos;
543 switch ( m_istate ) {
545 setPos( p.x(), pos().y() );
548 const qreal brr = br.right();
549 const qreal rr = r.right();
550 const qreal delta = pos().x()-p.x();
551 setPos( p.x(), QGraphicsItem::pos().y() );
552 br.setRight( brr+delta );
553 r.setRight( rr+delta );
557 const qreal rr = r.right();
558 r.setRight( scenepos.x()-pos().x() );
559 br.setWidth( br.width() + r.right()-rr );
571 if ( m_presspos.isNull() )
return;
574 switch ( m_istate ) {
579 if ( qAbs( m_pressscenepos.x()-
event->scenePos().x() ) < 10.
580 && qAbs( m_pressscenepos.y()-
event->scenePos().y() ) > 5. ) {
582 m_dragline =
new QGraphicsLineItem(
this );
583 m_dragline->setPen( QPen( Qt::DashLine ) );
584 m_dragline->setLine(QLineF(
rect().center(), event->pos() ));
590 updateItemFromMouse(event->scenePos());
594 QLineF line = m_dragline->line();
595 m_dragline->setLine( QLineF( line.p1(),
event->pos() ) );
virtual int maximumItemHeight() const =0
virtual void addConstraint(const QModelIndex &from, const QModelIndex &to, Qt::KeyboardModifiers modifiers)
void mouseReleaseEvent(QGraphicsSceneMouseEvent *) override
void setRect(const QRectF &r)
virtual QString ganttToolTip() const
GraphicsItem * dragSource() const
void mousePressEvent(QGraphicsSceneMouseEvent *) override
GraphicsItem(QGraphicsItem *parent=0, GraphicsScene *scene=0)
void setEnd(const QPointF &end)
void itemClicked(const QModelIndex &)
void updateItem(const Span &rowgeometry, const QPersistentModelIndex &idx)
virtual InteractionState interactionStateFor(const QPointF &pos, const StyleOptionGanttItem &opt, const QModelIndex &idx) const
virtual QString toolTip(const QModelIndex &idx) const
ConstraintModel * constraintModel() const
QRectF boundingRect() const override
void addStartConstraint(ConstraintGraphicsItem *)
const QPersistentModelIndex & index() const
QVariant itemChange(GraphicsItemChange, const QVariant &value) override
void hoverLeaveEvent(QGraphicsSceneHoverEvent *) override
const Constraint & constraint() const
A class representing a start point and a length.
Class only listed here to document inheritance of some KDChart classes.
The GraphicsView class provides a model/view implementation of a gantt chart.
void itemDoubleClicked(const QModelIndex &)
virtual void paintGanttItem(QPainter *p, const StyleOptionGanttItem &opt, const QModelIndex &idx)
void hoverMoveEvent(QGraphicsSceneHoverEvent *) override
AbstractRowController * rowController() const
void focusInEvent(QFocusEvent *event) override
QItemSelectionModel * selectionModel() const
void setBoundingRect(const QRectF &r)
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *) override
RelationType relationType() const
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0) override
void itemEntered(const QModelIndex &)
QStyleOption subclass for gantt items.
void setStart(const QPointF &start)
void removeStartConstraint(ConstraintGraphicsItem *)
int type() const override
virtual Span mapToChart(const QModelIndex &idx) const =0
void addEndConstraint(ConstraintGraphicsItem *)
void setIndex(const QPersistentModelIndex &idx)
void updateRow(const QModelIndex &idx)
void setDragSource(GraphicsItem *item)
ItemDelegate * itemDelegate() const
AbstractGrid * grid() const
GraphicsScene * scene() const
virtual bool mapFromChart(const Span &span, const QModelIndex &idx, const QList< Constraint > &constraints=QList< Constraint >()) const =0
virtual Span itemBoundingSpan(const StyleOptionGanttItem &opt, const QModelIndex &idx) const
void removeEndConstraint(ConstraintGraphicsItem *)
void mouseMoveEvent(QGraphicsSceneMouseEvent *) override
void itemPressed(const QModelIndex &)