File indexing completed on 2024-05-12 04:20:39

0001 /*
0002  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
0003  *
0004  * This file is part of the KGantt library.
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include "kganttconstraintgraphicsitem.h"
0010 #include "kganttconstraintmodel.h"
0011 #include "kganttgraphicsscene.h"
0012 #include "kganttitemdelegate.h"
0013 #include "kganttsummaryhandlingproxymodel.h"
0014 
0015 #include <QPainter>
0016 #include <QApplication>
0017 #include <QDebug>
0018 
0019 using namespace KGantt;
0020 
0021 
0022 ConstraintGraphicsItem::ConstraintGraphicsItem( const Constraint& c, QGraphicsItem* parent, GraphicsScene* scene )
0023     : QGraphicsItem( parent ),  m_constraint( c )
0024 {
0025     if ( scene )
0026         scene->addItem( this );
0027     setPos( QPointF( 0., 0. ) );
0028     setAcceptHoverEvents( false );
0029     setAcceptedMouseButtons( Qt::NoButton );
0030     setZValue( 10. );
0031 }
0032 
0033 ConstraintGraphicsItem::~ConstraintGraphicsItem()
0034 {
0035 }
0036 
0037 int ConstraintGraphicsItem::type() const
0038 {
0039     return Type;
0040 }
0041 
0042 GraphicsScene* ConstraintGraphicsItem::scene() const
0043 {
0044     return qobject_cast<GraphicsScene*>( QGraphicsItem::scene() );
0045 }
0046 
0047 Constraint ConstraintGraphicsItem::proxyConstraint() const
0048 {
0049     return Constraint( scene()->summaryHandlingModel()->mapFromSource( m_constraint.startIndex() ),
0050                        scene()->summaryHandlingModel()->mapFromSource( m_constraint.endIndex() ),
0051                        m_constraint.type(), m_constraint.relationType(), m_constraint.dataMap() );
0052 }
0053 
0054 QRectF ConstraintGraphicsItem::boundingRect() const
0055 {
0056     return scene()->itemDelegate()->constraintBoundingRect( m_start, m_end, m_constraint );
0057 }
0058 
0059 void ConstraintGraphicsItem::paint( QPainter* painter, const QStyleOptionGraphicsItem* option,
0060                                     QWidget* widget )
0061 {
0062     Q_UNUSED( widget );
0063     //qDebug() << "ConstraintGraphicsItem::paint(...), c=" << m_constraint;
0064     QStyleOptionGraphicsItem opt = *option;
0065     if (widget) {
0066         opt.palette = widget->palette();
0067     }else {
0068         opt.palette = QApplication::palette();
0069     }
0070     scene()->itemDelegate()->paintConstraintItem( painter, opt, m_start, m_end, m_constraint );
0071 }
0072 
0073 QString ConstraintGraphicsItem::ganttToolTip() const
0074 {
0075     return m_constraint.data( Qt::ToolTipRole ).toString();
0076 }
0077 
0078 void ConstraintGraphicsItem::setStart( const QPointF& start )
0079 {
0080     prepareGeometryChange();
0081     m_start = start;
0082     update();
0083 }
0084 
0085 void ConstraintGraphicsItem::setEnd( const QPointF& end )
0086 {
0087     prepareGeometryChange();
0088     m_end = end;
0089     update();
0090 }
0091 
0092 void ConstraintGraphicsItem::updateItem( const QPointF& start,const QPointF& end )
0093 {
0094     setStart( start );
0095     setEnd( end );
0096 }