Krita Source Code Documentation
Loading...
Searching...
No Matches
KoLineStyleModel.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2007 Jan Hambrecht <jaham@gmx.net>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
8
9#include <QPen>
10
12 : QAbstractListModel(parent),
13 m_hasTempStyle(false)
14{
15 // add standard dash patterns
16 for (int i = Qt::NoPen; i < Qt::CustomDashLine; i++) {
17 QPen pen(static_cast<Qt::PenStyle>(i));
18 m_styles << pen.dashPattern();
19 }
20}
21
22int KoLineStyleModel::rowCount(const QModelIndex &/*parent*/) const
23{
24 return m_styles.count() + (m_hasTempStyle ? 1 : 0);
25}
26
27QVariant KoLineStyleModel::data(const QModelIndex &index, int role) const
28{
29 if (!index.isValid())
30 return QVariant();
31
32 switch(role) {
33 case Qt::DecorationRole: {
34 QPen pen(Qt::black);
35 pen.setWidth(2);
36 if (index.row() < Qt::CustomDashLine)
37 pen.setStyle(static_cast<Qt::PenStyle>(index.row()));
38 else if (index.row() < m_styles.count())
39 pen.setDashPattern(m_styles[index.row()]);
40 else if (m_hasTempStyle)
41 pen.setDashPattern(m_tempStyle);
42 else
43 pen.setStyle(Qt::NoPen);
44
45 return QVariant(pen);
46 }
47 case Qt::SizeHintRole:
48 return QSize(100, 15);
49 default:
50 return QVariant();
51 }
52}
53
55{
56 if (m_styles.contains(style))
57 return false;
58
59 m_styles.append(style);
60 return true;
61}
62
63int KoLineStyleModel::setLineStyle(Qt::PenStyle style, const QVector<qreal> &dashes)
64{
65 // check if we select a standard or custom style
66 if (style < Qt::CustomDashLine) {
67 // a standard style
68 m_hasTempStyle = false;
69 beginResetModel();
70 endResetModel();
71
72 return style;
73 } else if (style == Qt::CustomDashLine) {
74 // a custom style -> check if already added
75 int index = m_styles.indexOf(dashes, Qt::CustomDashLine);
76 if (index < 0) {
77 // not already added -> add temporarily
78 m_tempStyle = dashes;
79 m_hasTempStyle = true;
80 beginResetModel();
81 endResetModel();
82
83 return m_styles.count();
84 } else {
85 // already added -> return index
86 m_hasTempStyle = false;
87 beginResetModel();
88 endResetModel();
89
90 return index;
91 }
92 }
93 return -1;
94}
KoLineStyleModel(QObject *parent=0)
bool addCustomStyle(const QVector< qreal > &style)
adds the given style to the model
int setLineStyle(Qt::PenStyle style, const QVector< qreal > &dashes)
selects the given style
QList< QVector< qreal > > m_styles
the added styles
QVector< qreal > m_tempStyle
a temporary added style
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
bool m_hasTempStyle
state of the temporary style
int rowCount(const QModelIndex &parent=QModelIndex()) const override