File indexing completed on 2024-03-24 04:43:41

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2007 Jan Hambrecht <jaham@gmx.net>
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 "KPropertyLineStyleModel_p.h"
0021 
0022 #include <QPen>
0023 
0024 KPropertyLineStyleModel::KPropertyLineStyleModel(QObject *parent)
0025     : QAbstractListModel(parent),
0026     m_hasTempStyle(false)
0027 {
0028     // add standard dash patterns
0029     for (int i = Qt::NoPen; i < Qt::CustomDashLine; i++) {
0030         QPen pen(static_cast<Qt::PenStyle>(i));
0031         m_styles << pen.dashPattern();
0032     }
0033 }
0034 
0035 int KPropertyLineStyleModel::rowCount(const QModelIndex &/*parent*/) const
0036 {
0037     return m_styles.count() + (m_hasTempStyle ? 1 : 0);
0038 }
0039 
0040 QVariant KPropertyLineStyleModel::data(const QModelIndex &index, int role) const
0041 {
0042     if (!index.isValid())
0043          return QVariant();
0044 
0045     switch(role) {
0046     case Qt::DecorationRole: {
0047         QPen pen(Qt::black);
0048         pen.setWidth(3);
0049         if (index.row() < Qt::CustomDashLine)
0050             pen.setStyle(static_cast<Qt::PenStyle>(index.row()));
0051         else if (index.row() < m_styles.count())
0052             pen.setDashPattern(m_styles[index.row()]);
0053         else if (m_hasTempStyle)
0054             pen.setDashPattern(m_tempStyle);
0055         else
0056             pen.setStyle(Qt::NoPen);
0057 
0058         return QVariant(pen);
0059     }
0060     case Qt::SizeHintRole:
0061         return QSize(100, 15);
0062     default:
0063         return QVariant();
0064     }
0065 }
0066 
0067 bool KPropertyLineStyleModel::addCustomStyle(const QVector<qreal> &style)
0068 {
0069     if (m_styles.contains(style))
0070         return false;
0071 
0072     m_styles.append(style);
0073     return true;
0074 }
0075 
0076 int KPropertyLineStyleModel::setLineStyle(Qt::PenStyle style, const QVector<qreal> &dashes)
0077 {
0078     // check if we select a standard or custom style
0079     if (style < Qt::CustomDashLine) {
0080         // a standard style
0081         m_hasTempStyle = false;
0082         return style;
0083     } else if (style == Qt::CustomDashLine) {
0084         // a custom style -> check if already added
0085         int index = m_styles.indexOf(dashes, Qt::CustomDashLine);
0086         if (index < 0) {
0087             // not already added -> add temporarly
0088             m_tempStyle = dashes;
0089             m_hasTempStyle = true;
0090             //reset();
0091             return m_styles.count();
0092         } else {
0093             // already added -> return index
0094             m_hasTempStyle = false;
0095             //reset();
0096             return index;
0097         }
0098     }
0099     return -1;
0100 }