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 ) {
69 :
BASE( parent ), m_isupdating( false )
72 scene->addItem(
this );
78 :
BASE( parent ), m_index( idx ), m_isupdating( false )
82 scene->addItem(
this );
89 void GraphicsItem::init()
91 setCacheMode( QGraphicsItem::DeviceCoordinateCache );
92 setFlags( ItemIsMovable|ItemIsSelectable|ItemIsFocusable );
93 setAcceptHoverEvents(
true );
94 setHandlesChildEvents(
true );
114 qDebug() <<
"Item" << m_index.model()->data( m_index, Qt::DisplayRole ).toString()
115 <<
", ends="<<m_endConstraints.size() <<
", starts="<<m_startConstraints.size();
122 QVariant da = m_index.model()->data( m_index, Qt::TextAlignmentRole );
123 if ( da.isValid() ) {
124 opt.displayAlignment =
static_cast< Qt::Alignment
>( da.toInt() );
134 opt.
text = m_index.model()->data( m_index, Qt::DisplayRole ).toString();
135 if ( isEnabled() ) opt.state |= QStyle::State_Enabled;
136 if ( isSelected() ) opt.state |= QStyle::State_Selected;
137 if ( hasFocus() ) opt.state |= QStyle::State_HasFocus;
143 return qobject_cast<GraphicsScene*>( QGraphicsItem::scene() );
149 qDebug() <<
"GraphicsItem::setRect("<<r<<
"), txt="<<m_index.model()->data( m_index, Qt::DisplayRole ).toString();
150 if ( m_index.model()->data( m_index, Qt::DisplayRole ).toString() == QLatin1String(
"Code Freeze" ) ) {
151 qDebug() <<
"gotcha";
155 prepareGeometryChange();
157 updateConstraintItems();
163 prepareGeometryChange();
170 return !
scene()->
isReadOnly() && m_index.model()->flags( m_index ) & Qt::ItemIsEditable;
179 *
static_cast<QStyleOption*
>(&opt) = *
static_cast<const QStyleOption*
>( option );
198 return m_boundingrect;
201 QPointF GraphicsItem::startConnector(
int relationType )
const
203 switch ( relationType ) {
206 return mapToScene( m_rect.left(), m_rect.top()+m_rect.height()/2. );
210 return mapToScene( m_rect.right(), m_rect.top()+m_rect.height()/2. );
213 QPointF GraphicsItem::endConnector(
int relationType )
const
215 switch ( relationType ) {
218 return mapToScene( m_rect.right(), m_rect.top()+m_rect.height()/2. );
222 return mapToScene( m_rect.left(), m_rect.top()+m_rect.height()/2. );
226 void GraphicsItem::constraintsChanged()
228 if ( !
scene() || !
scene()->itemDelegate() )
return;
237 m_startConstraints << item;
238 item->
setStart( startConnector( item->constraint().relationType() ) );
239 constraintsChanged();
245 m_endConstraints << item;
246 item->
setEnd( endConnector( item->constraint().relationType() ) );
247 constraintsChanged();
253 m_startConstraints.removeAll( item );
254 constraintsChanged();
260 m_endConstraints.removeAll( item );
261 constraintsChanged();
264 void GraphicsItem::updateConstraintItems()
281 Updater updater( &m_isupdating );
289 const Span s =
scene()->
grid()->mapToChart(
static_cast<const QModelIndex&
>( idx ) );
290 setPos( QPointF( s.
start(), rowGeometry.
start() ) );
297 if ( maxh < rowGeometry.
length() ) {
299 const Qt::Alignment align = getStyleOption().displayAlignment;
300 if ( align & Qt::AlignTop ) {
302 }
else if ( align & Qt::AlignBottom ) {
303 r.setY( rowGeometry.
length()-maxh );
306 r.setY( ( rowGeometry.
length()-maxh ) / 2. );
319 QPointF newPos=value.toPointF();
321 newPos.setY( pos().y() );
326 }
else if ( change==QGraphicsItem::ItemSelectedChange ) {
327 if (
index().isValid() && !(
index().model()->flags(
index() ) & Qt::ItemIsSelectable ) ) {
329 return qVariantFromValue(
false );
332 if ( value.toBool() ) {
339 return QGraphicsItem::itemChange( change, value );
348 void GraphicsItem::updateModel()
352 QAbstractItemModel* model =
const_cast<QAbstractItemModel*
>(
index().model() );
363 it1 != m_startConstraints.end() ;
365 constraints.push_back((*it1)->proxyConstraint());
367 it2 != m_endConstraints.end() ;
369 constraints.push_back((*it2)->proxyConstraint());
370 if (
scene()->grid()->mapFromChart(
Span( scenePos().x(),
rect().width() ),
387 setCursor( Qt::SizeHorCursor );
393 setCursor( Qt::SizeHorCursor );
399 setCursor( Qt::SplitHCursor );
428 m_presspos =
event->pos();
429 m_pressscenepos =
event->scenePos();
432 switch ( m_istate ) {
436 BASE::mousePressEvent( event );
447 if ( !m_presspos.isNull() ) {
450 delete m_dragline; m_dragline = 0;
451 if (
scene()->dragSource() ) {
453 GraphicsItem* other = qgraphicsitem_cast<GraphicsItem*>(
scene()->itemAt( event->scenePos(), QTransform() ) );
454 if ( other &&
scene()->dragSource()!=other &&
460 QRectF itemRect = other->
rect().adjusted(-other->
rect().height()/2.0, 0, 0, 0 );
461 if ( other->mapToScene( itemRect ).
boundingRect().contains( event->scenePos() ))
463 GraphicsView* view = qobject_cast<GraphicsView*>( event->widget()->parentWidget() );
466 scene()->summaryHandlingModel()->mapToSource( other->
index() ), event->modifiers() );
472 if ( other &&
scene()->dragSource()!=other &&
473 other->mapToScene( other->
rect() ).
boundingRect().contains( event->scenePos() )) {
474 GraphicsView* view = qobject_cast<GraphicsView*>( event->widget()->parentWidget() );
477 scene()->summaryHandlingModel()->mapToSource( other->
index() ), event->modifiers() );
486 updateItemFromMouse(event->scenePos());
492 m_presspos = QPointF();
500 m_presspos = QPointF();
501 BASE::mouseReleaseEvent( event );
512 BASE::mouseDoubleClickEvent( event );
515 void GraphicsItem::updateItemFromMouse(
const QPointF& scenepos )
518 const QPointF p = scenepos - m_presspos;
521 switch ( m_istate ) {
523 setPos( p.x(), pos().y() );
526 const qreal brr = br.right();
527 const qreal rr = r.right();
528 const qreal delta = pos().x()-p.x();
529 setPos( p.x(), QGraphicsItem::pos().y() );
530 br.setRight( brr+delta );
531 r.setRight( rr+delta );
535 const qreal rr = r.right();
536 r.setRight( scenepos.x()-pos().x() );
537 br.setWidth( br.width() + r.right()-rr );
549 if ( m_presspos.isNull() )
return;
552 switch ( m_istate ) {
557 if ( qAbs( m_pressscenepos.x()-event->scenePos().x() ) < 10.
558 && qAbs( m_pressscenepos.y()-event->scenePos().y() ) > 5. ) {
560 m_dragline =
new QGraphicsLineItem(
this );
561 m_dragline->setPen( QPen( Qt::DashLine ) );
562 m_dragline->setLine(QLineF(
rect().center(), event->pos() ));
568 updateItemFromMouse(event->scenePos());
572 QLineF line = m_dragline->line();
573 m_dragline->setLine( QLineF( line.p1(), event->pos() ) );