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  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "KoMarkerSelector.h"
0008 
0009 #include "KoMarker.h"
0010 #include "KoMarkerModel.h"
0011 #include "KoMarkerItemDelegate.h"
0012 #include "KoPathShape.h"
0013 
0014 #include <QPainter>
0015 #include <QPainterPath>
0016 
0017 class KoMarkerSelector::Private
0018 {
0019 public:
0020     Private(KoFlake::MarkerPosition position, QWidget *parent)
0021     : model(new KoMarkerModel(QList<KoMarker*>(), position, parent))
0022     {}
0023 
0024     KoMarkerModel *model;
0025 };
0026 
0027 KoMarkerSelector::KoMarkerSelector(KoFlake::MarkerPosition position, QWidget *parent)
0028 : QComboBox(parent)
0029 , d(new Private(position, this))
0030 {
0031     setModel(d->model);
0032     setItemDelegate(new KoMarkerItemDelegate(position, this));
0033 }
0034 
0035 KoMarkerSelector::~KoMarkerSelector()
0036 {
0037     delete d;
0038 }
0039 
0040 void KoMarkerSelector::paintEvent(QPaintEvent *pe)
0041 {
0042     QComboBox::paintEvent(pe);
0043 
0044     QStyleOptionComboBox option;
0045     option.initFrom(this);
0046     option.frame = hasFrame();
0047     QRect rect = style()->subControlRect(QStyle::CC_ComboBox, &option, QStyle::SC_ComboBoxEditField, this);
0048     if (!option.frame) { // frameless combo boxes have smaller margins but styles do not take this into account
0049         rect.adjust(-14, 0, 14, 1);
0050     }
0051 
0052     QPainter painter(this);
0053     bool antialiasing = painter.testRenderHint(QPainter::Antialiasing);
0054     if (!antialiasing) {
0055         painter.setRenderHint(QPainter::Antialiasing, true);
0056     }
0057 
0058     if (!(option.state & QStyle::State_Enabled)) {
0059         painter.setOpacity(0.5);
0060     }
0061     QPen pen(Qt::black, 2);
0062     KoMarker *marker = itemData(currentIndex(), Qt::DecorationRole).value<KoMarker*>();
0063     KoMarkerItemDelegate::drawMarkerPreview(&painter, rect, pen, marker, d->model->position());
0064 
0065     if (!antialiasing) {
0066         painter.setRenderHint(QPainter::Antialiasing, false);
0067     }
0068 }
0069 
0070 void KoMarkerSelector::setMarker(KoMarker *marker)
0071 {
0072     int index = d->model->markerIndex(marker);
0073     if (index >= 0) {
0074         setCurrentIndex(index);
0075         if (index != d->model->temporaryMarkerPosition()) {
0076             d->model->removeTemporaryMarker();
0077         }
0078     } else {
0079         setCurrentIndex(d->model->addTemporaryMarker(marker));
0080     }
0081 }
0082 
0083 KoMarker *KoMarkerSelector::marker() const
0084 {
0085     return itemData(currentIndex(), Qt::DecorationRole).value<KoMarker*>();
0086 }
0087 
0088 void KoMarkerSelector::updateMarkers(const QList<KoMarker*> markers)
0089 {
0090     KoMarkerModel *model = new KoMarkerModel(markers, d->model->position(), this);
0091     d->model = model;
0092     // this deletes the old model
0093     setModel(model);
0094 }
0095 
0096 QVariant KoMarkerSelector::itemData(int index, int role) const
0097 {
0098     return d->model->marker(index, role);
0099 }