12 #include "kdganttlegend_p.h"
16 #include <QApplication>
34 : QAbstractItemView(parent)
38 setFrameStyle(QFrame::NoFrame);
73 QAbstractItemModel *oldModel = model();
74 if (oldModel !=
nullptr) {
80 QAbstractItemView::setModel(newModel);
81 d->proxyModel.setSourceModel(newModel);
83 if (newModel !=
nullptr) {
102 if (model() ==
nullptr)
105 QPainter p(viewport());
106 p.fillRect(viewport()->rect(), palette().color(QPalette::Window));
117 opt.displayAlignment = Qt::Alignment(
d->proxyModel.data(index, Qt::TextAlignmentRole).toInt());
119 opt.font = (index.model()->data(index, Qt::FontRole)).value<QFont>();
128 QRect
Legend::drawItem(QPainter *painter,
const QModelIndex &index,
const QPoint &pos)
const
133 if (index.isValid() && index.model() == &
d->proxyModel) {
134 auto *
const delegate = qobject_cast<ItemDelegate *>(itemDelegate(index));
135 assert(delegate !=
nullptr);
139 opt.rect.setWidth(r.height());
142 const int dx = (typ ==
TypeEvent) ? (r.height() / 2) : 0;
144 opt.
itemRect = opt.rect.adjusted(dx, 0, dx, 0);
147 if (!opt.
text.isNull())
148 delegate->paintGanttItem(painter, opt, index);
154 const int rowCount =
d->proxyModel.rowCount(index);
155 for (
int row = 0; row < rowCount; ++row) {
156 const QRect r =
drawItem(painter,
d->proxyModel.index(row, 0, index), QPoint(pos.x(), yPos));
157 xPos = qMax(xPos, r.right());
158 yPos = qMax(yPos, r.bottom());
161 return QRect(pos, QPoint(xPos, yPos));
169 if (model() ==
nullptr)
173 if (index.model() !=
nullptr) {
174 QFontMetrics fm((index.model()->data(index, Qt::FontRole)).value<QFont>());
175 const QString text = index.model()->data(index,
LegendRole).toString();
177 baseSize += QSize(fm.horizontalAdvance(text) + fm.height() + 2, fm.height() + 2);
185 const int rowCount =
d->proxyModel.rowCount(index);
186 for (
int row = 0; row < rowCount; ++row) {
187 const QSize childSize =
measureItem(
d->proxyModel.index(row, 0, index));
188 childrenSize.setWidth(qMax(childrenSize.width(), childSize.width()));
189 childrenSize.rheight() += childSize.height();
191 return baseSize + childrenSize;
Class used to render gantt items in a KDGantt::GraphicsView.
void setModel(QAbstractItemModel *model) override
virtual void modelDataChanged()
Triggers repainting of the legend.
~Legend() override
Destructor.
QSize minimumSizeHint() const override
virtual StyleOptionGanttItem getStyleOption(const QModelIndex &index) const
Creates a StyleOptionGanttItem with all style options filled in except the target rectangles.
QModelIndex indexAt(const QPoint &point) const override
void paintEvent(QPaintEvent *event) override
QSize sizeHint() const override
Legend(QWidget *parent=nullptr)
Constructor.
virtual QSize measureItem(const QModelIndex &index, bool recursive=true) const
Calculates the needed space for the legend item at index and, if recursive is true,...
QRect visualRect(const QModelIndex &index) const override
virtual QRect drawItem(QPainter *painter, const QModelIndex &index, const QPoint &pos=QPoint()) const
Draws the legend item at index and all of it's children recursively at pos onto painter.
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.
ItemType
The values of this enum are used to represent the different types of gantt items that KDGantt underst...