30 #include "kdganttabstractgrid.h"
39 #include <QAbstractItemModel>
40 #include <QAbstractProxyModel>
41 #include <QItemSelectionModel>
42 #include <QGraphicsSceneMouseEvent>
43 #include <QGraphicsLineItem>
51 using namespace KDGantt;
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 setPos( QPointF( s.
start(), rowGeometry.
start() ) );
318 if ( maxh < rowGeometry.
length() ) {
320 const Qt::Alignment align = getStyleOption().displayAlignment;
321 if ( align & Qt::AlignTop ) {
323 }
else if ( align & Qt::AlignBottom ) {
324 r.setY( rowGeometry.
length()-maxh );
327 r.setY( ( rowGeometry.
length()-maxh ) / 2. );
340 QPointF newPos=value.toPointF();
342 newPos.setY( pos().y() );
347 }
else if ( change==QGraphicsItem::ItemSelectedChange ) {
348 if (
index().isValid() && !(
index().model()->flags(
index() ) & Qt::ItemIsSelectable ) ) {
350 return qVariantFromValue(
false );
353 if ( value.toBool() ) {
360 return QGraphicsItem::itemChange( change, value );
369 void GraphicsItem::updateModel()
373 QAbstractItemModel* model =
const_cast<QAbstractItemModel*
>(
index().model() );
382 it1 != m_startConstraints.end() ;
384 constraints.push_back((*it1)->proxyConstraint());
386 it2 != m_endConstraints.end() ;
388 constraints.push_back((*it2)->proxyConstraint());
406 setCursor( Qt::SizeHorCursor );
412 setCursor( Qt::SizeHorCursor );
418 setCursor( Qt::SplitHCursor );
447 m_presspos =
event->pos();
448 m_pressscenepos =
event->scenePos();
451 switch ( m_istate ) {
455 BASE::mousePressEvent( event );
466 if ( !m_presspos.isNull() ) {
469 delete m_dragline; m_dragline = 0;
470 if (
scene()->dragSource() ) {
473 if ( other &&
scene()->dragSource()!=other &&
479 QRectF itemRect = other->
rect().adjusted(-other->
rect().height()/2.0, 0, 0, 0 );
480 if ( other->mapToScene( itemRect ).
boundingRect().contains( event->scenePos() ))
485 scene()->summaryHandlingModel()->mapToSource( other->
index() ), event->modifiers() );
492 other->mapToScene( other->
rect() ).
boundingRect().contains( event->scenePos() )) {
496 scene()->summaryHandlingModel()->mapToSource( other->
index() ), event->modifiers() );
505 updateItemFromMouse(event->scenePos());
511 m_presspos = QPointF();
519 m_presspos = QPointF();
520 BASE::mouseReleaseEvent( event );
531 BASE::mouseDoubleClickEvent( event );
534 void GraphicsItem::updateItemFromMouse(
const QPointF& scenepos )
537 const QPointF p = scenepos - m_presspos;
540 switch ( m_istate ) {
542 setPos( p.x(), pos().y() );
545 const qreal brr = br.right();
546 const qreal rr = r.right();
547 const qreal delta = pos().x()-p.x();
548 setPos( p.x(), QGraphicsItem::pos().y() );
549 br.setRight( brr+delta );
550 r.setRight( rr+delta );
554 const qreal rr = r.right();
555 r.setRight( scenepos.x()-pos().x() );
556 br.setWidth( br.width() + r.right()-rr );
568 if ( m_presspos.isNull() )
return;
571 switch ( m_istate ) {
576 if ( qAbs( m_pressscenepos.x()-
event->scenePos().x() ) < 10.
577 && qAbs( m_pressscenepos.y()-
event->scenePos().y() ) > 5. ) {
579 m_dragline =
new QGraphicsLineItem(
this );
580 m_dragline->setPen( QPen( Qt::DashLine ) );
581 m_dragline->setLine(QLineF(
rect().center(), event->pos() ));
587 updateItemFromMouse(event->scenePos());
591 QLineF line = m_dragline->line();
592 m_dragline->setLine( QLineF( line.p1(),
event->pos() ) );