26 #include <QAbstractItemModel>
27 #include <QAbstractProxyModel>
28 #include <QGraphicsLineItem>
29 #include <QGraphicsSceneMouseEvent>
30 #include <QItemSelectionModel>
41 typedef QGraphicsItem
BASE;
84 void GraphicsItem::init()
86 setCacheMode(QGraphicsItem::DeviceCoordinateCache);
87 setFlags(ItemIsMovable | ItemIsSelectable | ItemIsFocusable);
88 setAcceptHoverEvents(
true);
89 setHandlesChildEvents(
true);
109 qDebug() <<
"Item" << m_index.model()->data( m_index, Qt::DisplayRole ).toString()
110 <<
", ends="<<m_endConstraints.size() <<
", starts="<<m_startConstraints.size();
117 QVariant da = m_index.model()->data(m_index, Qt::TextAlignmentRole);
119 opt.displayAlignment =
static_cast<Qt::Alignment
>(da.toInt());
123 opt.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
126 opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
130 opt.displayAlignment = Qt::AlignCenter;
135 opt.
text = m_index.model()->data(m_index, Qt::DisplayRole).toString();
137 opt.state |= QStyle::State_Enabled;
139 opt.state |= QStyle::State_Selected;
141 opt.state |= QStyle::State_HasFocus;
147 return qobject_cast<GraphicsScene *>(QGraphicsItem::scene());
153 qDebug() <<
"GraphicsItem::setRect("<<r<<
"), txt="<<m_index.model()->data( m_index, Qt::DisplayRole ).toString();
154 if ( m_index.model()->data( m_index, Qt::DisplayRole ).toString() == QLatin1String(
"Code Freeze" ) ) {
155 qDebug() <<
"gotcha";
159 prepareGeometryChange();
161 updateConstraintItems();
167 prepareGeometryChange();
174 return !
scene()->
isReadOnly() && m_index.model()->flags(m_index) & Qt::ItemIsEditable;
183 *
static_cast<QStyleOption *
>(&opt) = *
static_cast<const QStyleOption *
>(option);
202 return m_boundingrect;
205 QPointF GraphicsItem::startConnector(
int relationType)
const
207 switch (relationType) {
210 return mapToScene(m_rect.left(), m_rect.top() + m_rect.height() / 2.);
214 return mapToScene(m_rect.right(), m_rect.top() + m_rect.height() / 2.);
217 QPointF GraphicsItem::endConnector(
int relationType)
const
219 switch (relationType) {
222 return mapToScene(m_rect.right(), m_rect.top() + m_rect.height() / 2.);
226 return mapToScene(m_rect.left(), m_rect.top() + m_rect.height() / 2.);
229 void GraphicsItem::constraintsChanged()
241 m_startConstraints << item;
243 constraintsChanged();
249 m_endConstraints << item;
251 constraintsChanged();
257 m_startConstraints.removeAll(item);
258 constraintsChanged();
264 m_endConstraints.removeAll(item);
265 constraintsChanged();
268 void GraphicsItem::updateConstraintItems()
273 QPointF s = startConnector(item->constraint().relationType());
280 QPointF e = endConnector(item->constraint().relationType());
289 Updater updater(&m_isupdating);
298 setPos(QPointF(s.
start(), rowGeometry.
start()));
305 if (maxh < rowGeometry.
length()) {
307 const Qt::Alignment align = getStyleOption().displayAlignment;
308 if (align & Qt::AlignTop) {
310 }
else if (align & Qt::AlignBottom) {
311 r.setY(rowGeometry.
length() - maxh);
314 r.setY((rowGeometry.
length() - maxh) / 2.);
327 QPointF newPos = value.toPointF();
329 newPos.setY(pos().y());
334 }
else if (change == QGraphicsItem::ItemSelectedChange) {
335 if (
index().isValid() && !(
index().model()->flags(
index()) & Qt::ItemIsSelectable)) {
337 return QVariant::fromValue(
false);
340 if (value.toBool()) {
347 return QGraphicsItem::itemChange(change, value);
356 void GraphicsItem::updateModel()
360 auto *model =
const_cast<QAbstractItemModel *
>(
index().model());
369 QList<Constraint> constraints;
370 for (QList<ConstraintGraphicsItem *>::iterator it1 = m_startConstraints.begin();
371 it1 != m_startConstraints.end();
373 constraints.push_back((*it1)->proxyConstraint());
374 for (QList<ConstraintGraphicsItem *>::iterator it2 = m_endConstraints.begin();
375 it2 != m_endConstraints.end();
377 constraints.push_back((*it2)->proxyConstraint());
378 if (
scene()->grid()->mapFromChart(
Span(scenePos().x(),
rect().width()),
396 setCursor(Qt::SizeHorCursor);
402 setCursor(Qt::SizeHorCursor);
408 setCursor(Qt::SplitHCursor);
437 m_presspos =
event->pos();
438 m_pressscenepos =
event->scenePos();
445 BASE::mousePressEvent(event);
456 if (!m_presspos.isNull()) {
460 m_dragline =
nullptr;
461 if (
scene()->dragSource()) {
463 auto *other = qgraphicsitem_cast<GraphicsItem *>(
scene()->itemAt(event->scenePos(), QTransform()));
468 QRectF itemRect = other->rect().adjusted(-other->rect().height() / 2.0, 0, 0, 0);
469 if (other->mapToScene(itemRect).boundingRect().contains(event->scenePos())) {
470 auto *view = qobject_cast<GraphicsView *>(event->widget()->parentWidget());
472 view->addConstraint(
scene()->summaryHandlingModel()->mapToSource(
scene()->dragSource()->
index()),
473 scene()->summaryHandlingModel()->mapToSource(other->index()), event->modifiers());
477 if (other &&
scene()->dragSource() != other && other->mapToScene(other->rect()).boundingRect().contains(event->scenePos())) {
478 auto *view = qobject_cast<GraphicsView *>(event->widget()->parentWidget());
480 view->addConstraint(
scene()->summaryHandlingModel()->mapToSource(
scene()->dragSource()->
index()),
481 scene()->summaryHandlingModel()->mapToSource(other->index()), event->modifiers());
490 updateItemFromMouse(event->scenePos());
496 m_presspos = QPointF();
504 m_presspos = QPointF();
505 BASE::mouseReleaseEvent(event);
516 BASE::mouseDoubleClickEvent(event);
519 void GraphicsItem::updateItemFromMouse(
const QPointF &scenepos)
522 const QPointF p = scenepos - m_presspos;
527 setPos(p.x(), pos().y());
530 const qreal brr = br.right();
531 const qreal rr = r.right();
532 const qreal delta = pos().x() - p.x();
533 setPos(p.x(), QGraphicsItem::pos().y());
534 br.setRight(brr + delta);
535 r.setRight(rr + delta);
539 const qreal rr = r.right();
540 r.setRight(scenepos.x() - pos().x());
541 br.setWidth(br.width() + r.right() - rr);
555 if (m_presspos.isNull())
564 if (qAbs(m_pressscenepos.x() - event->scenePos().x()) < 10.
565 && qAbs(m_pressscenepos.y() - event->scenePos().y()) > 5.) {
567 m_dragline =
new QGraphicsLineItem(
this);
568 m_dragline->setPen(QPen(Qt::DashLine));
569 m_dragline->setLine(QLineF(
rect().center(), event->pos()));
575 updateItemFromMouse(event->scenePos());
579 QLineF line = m_dragline->line();
580 m_dragline->setLine(QLineF(line.p1(), event->pos()));
virtual Span mapToChart(const QModelIndex &idx) const =0
Implement this to map from the data in the model to the location of the corresponding item in the vie...
virtual int maximumItemHeight() const =0
const Constraint & constraint() const
void setStart(const QPointF &start)
void setEnd(const QPointF &end)
The ConstraintModel keeps track of the interdependencies between gantt items in a View.
RelationType relationType() const
This is unused for now.
int type() const override
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=nullptr) override
QRectF boundingRect() const override
void removeStartConstraint(ConstraintGraphicsItem *)
GraphicsItem(QGraphicsItem *parent=nullptr, GraphicsScene *scene=nullptr)
void addEndConstraint(ConstraintGraphicsItem *)
void mousePressEvent(QGraphicsSceneMouseEvent *) override
void updateItem(const Span &rowgeometry, const QPersistentModelIndex &idx)
void hoverMoveEvent(QGraphicsSceneHoverEvent *) override
const QPersistentModelIndex & index() const
void mouseReleaseEvent(QGraphicsSceneMouseEvent *) override
virtual QString ganttToolTip() const
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *) override
void removeEndConstraint(ConstraintGraphicsItem *)
void mouseMoveEvent(QGraphicsSceneMouseEvent *) override
void focusInEvent(QFocusEvent *event) override
void setIndex(const QPersistentModelIndex &idx)
GraphicsScene * scene() const
void setRect(const QRectF &r)
void addStartConstraint(ConstraintGraphicsItem *)
void setBoundingRect(const QRectF &r)
void hoverLeaveEvent(QGraphicsSceneHoverEvent *) override
QVariant itemChange(GraphicsItemChange, const QVariant &value) override
void itemDoubleClicked(const QModelIndex &)
AbstractRowController * rowController() const
ItemDelegate * itemDelegate() const
void itemClicked(const QModelIndex &)
ConstraintModel * constraintModel() const
void itemEntered(const QModelIndex &)
void updateRow(const QModelIndex &idx)
void itemPressed(const QModelIndex &)
AbstractGrid * grid() const
QItemSelectionModel * selectionModel() const
void setDragSource(GraphicsItem *item)
virtual QString toolTip(const QModelIndex &idx) const
virtual Span itemBoundingSpan(const StyleOptionGanttItem &opt, const QModelIndex &idx) const
virtual InteractionState interactionStateFor(const QPointF &pos, const StyleOptionGanttItem &opt, const QModelIndex &idx) const
InteractionState
This enum is used for communication between the view and the delegate about user interaction with gan...
virtual void paintGanttItem(QPainter *p, const StyleOptionGanttItem &opt, const QModelIndex &idx)
Paints the gantt item idx using painter and opt.
A class representing a start point and a length.
QStyleOption subclass for gantt items.
Position
This enum is used to describe where the Qt::DisplayRole (the label) should be located relative to the...
QRectF boundingRect
Contains the bounding rectangle for the item.
AbstractGrid * grid
Contains a pointer to the AbstractGrid used by the view.
QString text
Contains a string printed to the item.
QRectF itemRect
Contains the "active" item rectangle that corresponds to the values from the model.
ItemType
The values of this enum are used to represent the different types of gantt items that KDGantt underst...