File indexing completed on 2024-05-12 16:02:09
0001 /* This file is part of the KDE project 0002 * SPDX-FileCopyrightText: 2007 Jan Hambrecht <jaham@gmx.net> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "KoLineStyleModel_p.h" 0008 0009 #include <QPen> 0010 0011 KoLineStyleModel::KoLineStyleModel(QObject *parent) 0012 : QAbstractListModel(parent), 0013 m_hasTempStyle(false) 0014 { 0015 // add standard dash patterns 0016 for (int i = Qt::NoPen; i < Qt::CustomDashLine; i++) { 0017 QPen pen(static_cast<Qt::PenStyle>(i)); 0018 m_styles << pen.dashPattern(); 0019 } 0020 } 0021 0022 int KoLineStyleModel::rowCount(const QModelIndex &/*parent*/) const 0023 { 0024 return m_styles.count() + (m_hasTempStyle ? 1 : 0); 0025 } 0026 0027 QVariant KoLineStyleModel::data(const QModelIndex &index, int role) const 0028 { 0029 if (!index.isValid()) 0030 return QVariant(); 0031 0032 switch(role) { 0033 case Qt::DecorationRole: { 0034 QPen pen(Qt::black); 0035 pen.setWidth(2); 0036 if (index.row() < Qt::CustomDashLine) 0037 pen.setStyle(static_cast<Qt::PenStyle>(index.row())); 0038 else if (index.row() < m_styles.count()) 0039 pen.setDashPattern(m_styles[index.row()]); 0040 else if (m_hasTempStyle) 0041 pen.setDashPattern(m_tempStyle); 0042 else 0043 pen.setStyle(Qt::NoPen); 0044 0045 return QVariant(pen); 0046 } 0047 case Qt::SizeHintRole: 0048 return QSize(100, 15); 0049 default: 0050 return QVariant(); 0051 } 0052 } 0053 0054 bool KoLineStyleModel::addCustomStyle(const QVector<qreal> &style) 0055 { 0056 if (m_styles.contains(style)) 0057 return false; 0058 0059 m_styles.append(style); 0060 return true; 0061 } 0062 0063 int KoLineStyleModel::setLineStyle(Qt::PenStyle style, const QVector<qreal> &dashes) 0064 { 0065 // check if we select a standard or custom style 0066 if (style < Qt::CustomDashLine) { 0067 // a standard style 0068 m_hasTempStyle = false; 0069 beginResetModel(); 0070 endResetModel(); 0071 0072 return style; 0073 } else if (style == Qt::CustomDashLine) { 0074 // a custom style -> check if already added 0075 int index = m_styles.indexOf(dashes, Qt::CustomDashLine); 0076 if (index < 0) { 0077 // not already added -> add temporarily 0078 m_tempStyle = dashes; 0079 m_hasTempStyle = true; 0080 beginResetModel(); 0081 endResetModel(); 0082 0083 return m_styles.count(); 0084 } else { 0085 // already added -> return index 0086 m_hasTempStyle = false; 0087 beginResetModel(); 0088 endResetModel(); 0089 0090 return index; 0091 } 0092 } 0093 return -1; 0094 }