13 #include "kdganttitemdelegate_p.h"
16 #include <QAbstractItemModel>
17 #include <QApplication>
18 #include <QModelIndex>
20 #include <QPainterPath>
23 #ifndef QT_NO_DEBUG_STREAM
25 #define PRINT_INTERACTIONSTATE(x) \
43 #undef PRINT_INTERACTIONSTATE
62 ItemDelegate::Private::Private()
65 QLinearGradient taskgrad(0., 0., 0., QApplication::fontMetrics().height());
66 taskgrad.setColorAt(0., Qt::green);
67 taskgrad.setColorAt(1., Qt::darkGreen);
69 QLinearGradient summarygrad(0., 0., 0., QApplication::fontMetrics().height());
70 summarygrad.setColorAt(0., Qt::blue);
71 summarygrad.setColorAt(1., Qt::darkBlue);
73 QLinearGradient eventgrad(0., 0., 0., QApplication::fontMetrics().height());
74 eventgrad.setColorAt(0., Qt::red);
75 eventgrad.setColorAt(1., Qt::darkRed);
82 QPen pen(Qt::black, 1.);
89 QPen ItemDelegate::Private::constraintPen(
const QPointF &start,
const QPointF &end,
const Constraint &constraint)
95 if (start.x() < end.x()) {
96 pen = QPen(Qt::black);
104 if (dataPen.canConvert(QVariant::Pen)) {
105 pen = dataPen.value<QPen>();
113 : QItemDelegate(parent)
134 d->defaultbrush[type] = brush;
143 return d->defaultbrush[type];
154 d->defaultpen[type] = pen;
163 return d->defaultpen[type];
173 const QAbstractItemModel *model = idx.model();
176 QString tip = model->data(idx, Qt::ToolTipRole).toString();
180 return tr(
"%1 -> %2: %3")
183 .arg(model->data(idx, Qt::DisplayRole).toString());
195 const QModelIndex &idx)
const
200 const QString txt = idx.model()->data(idx, Qt::DisplayRole).toString();
201 const int typ = idx.model()->data(idx,
ItemTypeRole).toInt();
205 itemRect = QRectF(itemRect.left() - itemRect.height() / 2.,
211 int tw = opt.fontMetrics.horizontalAdvance(txt);
212 tw +=
static_cast<int>(itemRect.height() / 2.);
216 s =
Span(itemRect.left() - tw, itemRect.width() + tw);
219 s =
Span(itemRect.left(), itemRect.width() + tw);
223 s =
Span(itemRect.left(), itemRect.width());
237 const QModelIndex &idx)
const
241 if (!(idx.model()->flags(idx) & Qt::ItemIsEditable))
251 itemRect = QRectF(itemRect.topLeft() - QPointF(itemRect.height() / 2.0, 0), QSizeF(itemRect.height(), itemRect.height()));
255 if (!itemRect.contains(pos))
261 if (itemRect.width() < 15)
263 if (pos.x() >= itemRect.left() && pos.x() < itemRect.left() + delta) {
265 }
else if (pos.x() <= itemRect.right() && pos.x() > itemRect.right() - delta) {
276 const QModelIndex &idx)
281 const QString &txt = opt.
text;
284 boundingRect.setY(itemRect.y());
285 boundingRect.setHeight(itemRect.height());
293 if (opt.state & QStyle::State_Selected)
294 pen.setWidth(2 * pen.width());
295 painter->setPen(pen);
298 bool drawText =
true;
299 qreal pw = painter->pen().width() / 2.;
302 if (itemRect.isValid()) {
304 qreal pw = painter->pen().width() / 2.;
307 r.translate(0., r.height() / 6.);
308 r.setHeight(2. * r.height() / 3.);
309 painter->setBrushOrigin(itemRect.topLeft());
311 painter->translate(0.5, 0.5);
312 painter->drawRect(r);
316 qreal h = r.height();
317 QRectF cr(r.x(), r.y() + h / 4.,
318 r.width() * completion / 100., h / 2. + 1 );
319 QColor compcolor(painter->pen().color());
320 compcolor.setAlpha(150);
321 painter->fillRect(cr, compcolor);
330 const QRectF r = QRectF(opt.
itemRect).adjusted(-pw, -pw, pw, pw);
332 const qreal deltaY = r.height() / 2.;
333 const qreal deltaXBezierControl = .25 * qMin(r.width(), r.height());
334 const qreal deltaX = qMin(r.width() / ( qreal )2., r.height());
335 path.moveTo(r.topLeft());
336 path.lineTo(r.topRight());
337 path.lineTo(QPointF(r.right(), r.top() + 2. * deltaY));
339 path.quadTo(QPointF(r.right() - deltaXBezierControl, r.top() + deltaY), QPointF(r.right() - deltaX, r.top() + deltaY));
341 path.lineTo(QPointF(r.left() + deltaX, r.top() + deltaY));
342 path.quadTo(QPointF(r.left() + deltaXBezierControl, r.top() + deltaY), QPointF(r.left(), r.top() + 2. * deltaY));
344 painter->setBrushOrigin(itemRect.topLeft());
346 painter->translate(0.5, 0.5);
347 painter->drawPath(path);
354 const qreal pw = painter->pen().width() / 2. - 1;
355 const QRectF r = QRectF(opt.
itemRect).adjusted(-pw, -pw, pw, pw).translated(-opt.
itemRect.height() / 2, 0);
357 const qreal delta =
static_cast<int>(r.height() / 2);
358 path.moveTo(delta, 0.);
359 path.lineTo(2. * delta, delta);
360 path.lineTo(delta, 2. * delta);
361 path.lineTo(0., delta);
364 painter->translate(r.topLeft());
365 painter->translate(0, 0.5);
366 painter->drawPath(path);
369 painter->setBrush( Qt::NoBrush );
370 painter->setPen( Qt::black );
372 painter->setPen( Qt::red );
373 painter->drawRect( r );
391 ta = Qt::AlignCenter;
398 painter->drawText(boundingRect, ta | Qt::AlignVCenter, txt);
405 static const qreal
PW = 1.5;
428 return poly.boundingRect().adjusted(-
PW, -
PW,
PW,
PW);
437 const QPointF &start,
const QPointF &end,
const Constraint &constraint)
460 const QPen pen =
d->constraintPen(start, end, constraint);
462 painter->setPen(pen);
463 painter->setBrush(pen.color());
472 qreal midx = end.x() -
TURN;
473 qreal midy = (end.y() - start.y()) / 2. + start.y();
475 if (start.x() > end.x() -
TURN) {
477 << QPointF(start.x() +
TURN, start.y())
478 << QPointF(start.x() +
TURN, midy)
479 << QPointF(end.x() -
TURN, midy)
480 << QPointF(end.x() -
TURN, end.y())
484 << QPointF(midx, start.y())
485 << QPointF(midx, end.y())
497 << QPointF(end.x() -
TURN / 2., end.y() -
TURN / 2.)
498 << QPointF(end.x() -
TURN / 2., end.y() +
TURN / 2.);
506 const QPen pen =
d->constraintPen(start, end, constraint);
508 painter->setPen(pen);
509 painter->setBrush(pen.color());
518 qreal midx = end.x() +
TURN;
519 qreal midy = (end.y() - start.y()) / 2. + start.y();
521 if (start.x() > end.x() +
TURN) {
523 << QPointF(start.x() +
TURN, start.y())
524 << QPointF(start.x() +
TURN, end.y())
528 << QPointF(midx, start.y())
529 << QPointF(midx, midy)
530 << QPointF(end.x() +
TURN, midy)
531 << QPointF(end.x() +
TURN, end.y())
543 << QPointF(end.x() +
TURN / 2., end.y() -
TURN / 2.)
544 << QPointF(end.x() +
TURN / 2., end.y() +
TURN / 2.);
552 const QPen pen =
d->constraintPen(start, end, constraint);
554 painter->setPen(pen);
555 painter->setBrush(pen.color());
567 if (start.x() > end.x()) {
569 << QPointF(end.x() -
TURN, start.y())
570 << QPointF(end.x() -
TURN, end.y())
574 << QPointF(start.x() -
TURN, start.y())
575 << QPointF(start.x() -
TURN, end.y())
576 << QPointF(end.x() -
TURN, end.y())
588 << QPointF(end.x() -
TURN / 2., end.y() -
TURN / 2.)
589 << QPointF(end.x() -
TURN / 2., end.y() +
TURN / 2.);
597 const QPen pen =
d->constraintPen(start, end, constraint);
599 painter->setPen(pen);
600 painter->setBrush(pen.color());
611 qreal midx = end.x() +
TURN;
612 qreal midy = (end.y() - start.y()) / 2. + start.y();
614 if (start.x() -
TURN > end.x() +
TURN) {
616 << QPointF(midx, start.y())
617 << QPointF(midx, end.y())
621 << QPointF(start.x() -
TURN, start.y())
622 << QPointF(start.x() -
TURN, midy)
623 << QPointF(midx, midy)
624 << QPointF(end.x() +
TURN, end.y())
636 << QPointF(end.x() +
TURN / 2., end.y() -
TURN / 2.)
637 << QPointF(end.x() +
TURN / 2., end.y() +
TURN / 2.);
641 #include "moc_kdganttitemdelegate.cpp"
A class used to represent a dependency.
QVariant data(int role) const
RelationType relationType() const
This is unused for now.
virtual void paintConstraintItem(QPainter *p, const QStyleOptionGraphicsItem &opt, const QPointF &start, const QPointF &end, const Constraint &constraint)
Paints the constraint between points start and end using painter and opt.
void paintFinishStartConstraint(QPainter *p, const QStyleOptionGraphicsItem &opt, const QPointF &start, const QPointF &end, const Constraint &constraint)
QPen defaultPen(ItemType type) const
QPolygonF startFinishArrow(const QPointF &start, const QPointF &end) const
virtual QString toolTip(const QModelIndex &idx) const
QPolygonF startStartLine(const QPointF &start, const QPointF &end) const
~ItemDelegate() override
Destructor.
virtual Span itemBoundingSpan(const StyleOptionGanttItem &opt, const QModelIndex &idx) const
virtual InteractionState interactionStateFor(const QPointF &pos, const StyleOptionGanttItem &opt, const QModelIndex &idx) const
void paintStartStartConstraint(QPainter *p, const QStyleOptionGraphicsItem &opt, const QPointF &start, const QPointF &end, const Constraint &constraint)
void setDefaultBrush(ItemType type, const QBrush &brush)
Sets the default brush used for items of type type to brush.
void paintStartFinishConstraint(QPainter *p, const QStyleOptionGraphicsItem &opt, const QPointF &start, const QPointF &end, const Constraint &constraint)
QPolygonF finishFinishLine(const QPointF &start, const QPointF &end) const
InteractionState
This enum is used for communication between the view and the delegate about user interaction with gan...
void setDefaultPen(ItemType type, const QPen &pen)
Sets the default pen used for items of type type to pen.
QPolygonF startFinishLine(const QPointF &start, const QPointF &end) const
QPolygonF finishStartArrow(const QPointF &start, const QPointF &end) const
virtual void paintGanttItem(QPainter *p, const StyleOptionGanttItem &opt, const QModelIndex &idx)
Paints the gantt item idx using painter and opt.
QPolygonF finishFinishArrow(const QPointF &start, const QPointF &end) const
QBrush defaultBrush(ItemType type) const
void paintFinishFinishConstraint(QPainter *p, const QStyleOptionGraphicsItem &opt, const QPointF &start, const QPointF &end, const Constraint &constraint)
virtual QRectF constraintBoundingRect(const QPointF &start, const QPointF &end, const Constraint &constraint) const
ItemDelegate(QObject *parent=nullptr)
Constructor.
QPolygonF startStartArrow(const QPointF &start, const QPointF &end) const
QPolygonF finishStartLine(const QPointF &start, const QPointF &end) const
A class representing a start point and a length.
QStyleOption subclass for gantt items.
QRectF boundingRect
Contains the bounding rectangle for the item.
QString text
Contains a string printed to the item.
QRectF itemRect
Contains the "active" item rectangle that corresponds to the values from the model.
#define PRINT_INTERACTIONSTATE(x)
QDebug operator<<(QDebug dbg, KDGantt::ItemDelegate::InteractionState state)
ItemType
The values of this enum are used to represent the different types of gantt items that KDGantt underst...