File indexing completed on 2024-05-12 16:02:09

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2011 Thorsten Zachmann <zachmann@kde.org>
0003  * SPDX-FileCopyrightText: 2011 Jean-Nicolas Artaud <jeannicolasartaud@gmail.com>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 
0008 #include "KoMarkerItemDelegate.h"
0009 
0010 #include <KoPathShape.h>
0011 #include <KoMarker.h>
0012 
0013 #include <QPainter>
0014 #include <QPen>
0015 
0016 #include "kis_global.h"
0017 
0018 KoMarkerItemDelegate::KoMarkerItemDelegate(KoFlake::MarkerPosition position, QObject *parent)
0019 : QAbstractItemDelegate(parent)
0020 , m_position(position)
0021 {
0022 }
0023 
0024 KoMarkerItemDelegate::~KoMarkerItemDelegate()
0025 {
0026 }
0027 
0028 void KoMarkerItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
0029 {
0030     if (option.state & QStyle::State_Selected) {
0031         painter->fillRect(option.rect, option.palette.highlight());
0032     }
0033 
0034     QPen pen(option.palette.text(), 2);
0035     KoMarker *marker = index.data(Qt::DecorationRole).value<KoMarker*>();
0036     drawMarkerPreview(painter, option.rect.adjusted(1, 0, -1, 0), pen, marker, m_position);
0037 }
0038 
0039 QSize KoMarkerItemDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex &index) const
0040 {
0041     Q_UNUSED(option);
0042     Q_UNUSED(index);
0043     return QSize(80,30);
0044 }
0045 
0046 void KoMarkerItemDelegate::drawMarkerPreview(QPainter *painter, const QRect &rect, const QPen &pen, KoMarker *marker, KoFlake::MarkerPosition position)
0047 {
0048     if (marker) {
0049         marker->drawPreview(painter, rect, pen, position);
0050     } else {
0051         const qreal centerY = QRectF(rect).center().y();
0052         QPen oldPen = painter->pen();
0053         painter->setPen(pen);
0054         painter->drawLine(rect.left(), centerY, rect.right(), centerY);
0055         painter->setPen(oldPen);
0056     }
0057 }