Warning, file /office/calligra/libs/widgets/KoMarkerSelector.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2011 Thorsten Zachmann <zachmann@kde.org>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Library General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "KoMarkerSelector.h"
0021 
0022 #include "KoMarker.h"
0023 #include "KoMarkerModel.h"
0024 #include "KoMarkerItemDelegate.h"
0025 #include "KoPathShape.h"
0026 
0027 #include <QPainter>
0028 #include <QPainterPath>
0029 
0030 class KoMarkerSelector::Private
0031 {
0032 public:
0033     Private(KoMarkerData::MarkerPosition position, QWidget *parent)
0034     : model(new KoMarkerModel(QList<KoMarker*>(), position, parent))
0035     {}
0036 
0037     KoMarkerModel *model;
0038 };
0039 
0040 KoMarkerSelector::KoMarkerSelector(KoMarkerData::MarkerPosition position, QWidget *parent)
0041 : QComboBox(parent)
0042 , d(new Private(position, this))
0043 {
0044     setModel(d->model);
0045     setItemDelegate(new KoMarkerItemDelegate(position, this));
0046 }
0047 
0048 KoMarkerSelector::~KoMarkerSelector()
0049 {
0050     delete d;
0051 }
0052 
0053 void KoMarkerSelector::paintEvent(QPaintEvent *pe)
0054 {
0055     QComboBox::paintEvent(pe);
0056 
0057     QStyleOptionComboBox option;
0058     option.initFrom(this);
0059     option.frame = hasFrame();
0060     QRect rect = style()->subControlRect(QStyle::CC_ComboBox, &option, QStyle::SC_ComboBoxEditField, this);
0061     if (!option.frame) { // frameless combo boxes have smaller margins but styles do not take this into account
0062         rect.adjust(-14, 0, 14, 1);
0063     }
0064 
0065     QPainter painter(this);
0066     bool antialiasing = painter.testRenderHint(QPainter::Antialiasing);
0067     if (!antialiasing) {
0068         painter.setRenderHint(QPainter::Antialiasing, true);
0069     }
0070 
0071     KoPathShape pathShape;
0072     pathShape.moveTo(QPointF(rect.left(), rect.center().y()));
0073     pathShape.lineTo(QPointF(rect.right(), rect.center().y()));
0074 
0075     KoMarker *marker = itemData(currentIndex(), Qt::DecorationRole).value<KoMarker*>();
0076     if (marker != nullptr) {
0077         pathShape.setMarker(marker, d->model->position());
0078     }
0079 
0080     // paint marker
0081     QPen pen(option.palette.text(), 2);
0082     QPainterPath path = pathShape.pathStroke(pen);
0083     painter.fillPath(path, pen.brush());
0084 
0085     if (!antialiasing) {
0086         painter.setRenderHint(QPainter::Antialiasing, false);
0087     }
0088 }
0089 
0090 void KoMarkerSelector::setMarker(KoMarker *marker)
0091 {
0092     int index = d->model->markerIndex(marker);
0093     if (index >= 0) {
0094         setCurrentIndex(index);
0095     }
0096 }
0097 
0098 KoMarker *KoMarkerSelector::marker() const
0099 {
0100     return itemData(currentIndex(), Qt::DecorationRole).value<KoMarker*>();
0101 }
0102 
0103 void KoMarkerSelector::updateMarkers(const QList<KoMarker*> markers)
0104 {
0105     KoMarkerModel *model = new KoMarkerModel(markers,d->model->position(), this);
0106     d->model = model;
0107     // this deletes the old model
0108     setModel(model);
0109 }
0110 
0111 QVariant KoMarkerSelector::itemData(int index, int role) const
0112 {
0113     return d->model->marker(index, role);
0114 }