30 #include "kdganttabstractgrid.h"
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 );
311 const Span s =
scene()->
grid()->mapToChart(
static_cast<const QModelIndex&
>( idx ) );
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());
392 if (
scene()->grid()->mapFromChart(
Span( scenePos().x(),
rect().width() ),
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() ) {
475 GraphicsItem* other = qgraphicsitem_cast<GraphicsItem*>(
scene()->itemAt( event->scenePos(), QTransform() ) );
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() ))
485 GraphicsView* view = qobject_cast<GraphicsView*>( event->widget()->parentWidget() );
488 scene()->summaryHandlingModel()->mapToSource( other->
index() ), event->modifiers() );
494 if ( other &&
scene()->dragSource()!=other &&
495 other->mapToScene( other->
rect() ).
boundingRect().contains( event->scenePos() )) {
496 GraphicsView* view = qobject_cast<GraphicsView*>( event->widget()->parentWidget() );
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() ) );