File indexing completed on 2024-06-16 08:27:08

0001 /***************************************************************************
0002  *   Copyright (C) 2005 by David Saxton                                    *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #include "drawpart.h"
0012 #include "itemdocument.h"
0013 #include "itemdocumentdata.h"
0014 #include "variant.h"
0015 
0016 #include <KLocalizedString>
0017 #include <QBitArray>
0018 
0019 DrawPart::DrawPart(ItemDocument *itemDocument, bool newItem, const char *id)
0020     : Item(itemDocument, newItem, id)
0021 {
0022     if (itemDocument)
0023         itemDocument->registerItem(this);
0024 }
0025 
0026 DrawPart::~DrawPart()
0027 {
0028 }
0029 
0030 Variant *DrawPart::createProperty(const QString &id, Variant::Type::Value type)
0031 {
0032     if (type == Variant::Type::PenStyle) {
0033         QStringList penStyles;
0034         penStyles << DrawPart::penStyleToName(Qt::SolidLine) << DrawPart::penStyleToName(Qt::DashLine) << DrawPart::penStyleToName(Qt::DotLine) << DrawPart::penStyleToName(Qt::DashDotLine) << DrawPart::penStyleToName(Qt::DashDotDotLine);
0035 
0036         Variant *v = createProperty(id, Variant::Type::String);
0037         v->setType(Variant::Type::PenStyle);
0038         v->setAllowed(penStyles);
0039         return v;
0040     }
0041 
0042     if (type == Variant::Type::PenCapStyle) {
0043         QStringList penCapStyles;
0044         penCapStyles << DrawPart::penCapStyleToName(Qt::FlatCap) << DrawPart::penCapStyleToName(Qt::SquareCap) << DrawPart::penCapStyleToName(Qt::RoundCap);
0045 
0046         Variant *v = createProperty(id, Variant::Type::String);
0047         v->setType(Variant::Type::PenCapStyle);
0048         v->setAllowed(penCapStyles);
0049         return v;
0050     }
0051 
0052     return Item::createProperty(id, type);
0053 }
0054 
0055 Qt::PenStyle DrawPart::getDataPenStyle(const QString &id)
0056 {
0057     return nameToPenStyle(dataString(id));
0058 }
0059 Qt::PenCapStyle DrawPart::getDataPenCapStyle(const QString &id)
0060 {
0061     return nameToPenCapStyle(dataString(id));
0062 }
0063 void DrawPart::setDataPenStyle(const QString &id, Qt::PenStyle value)
0064 {
0065     property(id)->setValue(penStyleToName(value));
0066 }
0067 void DrawPart::setDataPenCapStyle(const QString &id, Qt::PenCapStyle value)
0068 {
0069     property(id)->setValue(penCapStyleToName(value));
0070 }
0071 
0072 ItemData DrawPart::itemData() const
0073 {
0074     ItemData itemData = Item::itemData();
0075 
0076     const VariantDataMap::const_iterator end = m_variantData.end();
0077     for (VariantDataMap::const_iterator it = m_variantData.begin(); it != end; ++it) {
0078         switch (it.value()->type()) {
0079         case Variant::Type::PenStyle:
0080             itemData.dataString[it.key()] = penStyleToID(nameToPenStyle(it.value()->value().toString()));
0081             break;
0082         case Variant::Type::PenCapStyle:
0083             itemData.dataString[it.key()] = penCapStyleToID(nameToPenCapStyle(it.value()->value().toString()));
0084             break;
0085         case Variant::Type::String:
0086         case Variant::Type::FileName:
0087         case Variant::Type::Port:
0088         case Variant::Type::Pin:
0089         case Variant::Type::VarName:
0090         case Variant::Type::Combo:
0091         case Variant::Type::Select:
0092         case Variant::Type::Multiline:
0093         case Variant::Type::RichText:
0094         case Variant::Type::Int:
0095         case Variant::Type::Double:
0096         case Variant::Type::Color:
0097         case Variant::Type::Bool:
0098         case Variant::Type::Raw:
0099         case Variant::Type::SevenSegment:
0100         case Variant::Type::KeyPad:
0101         case Variant::Type::None:
0102             // All of these are handled by Item
0103             break;
0104         }
0105     }
0106 
0107     return itemData;
0108 }
0109 
0110 void DrawPart::restoreFromItemData(const ItemData &itemData)
0111 {
0112     Item::restoreFromItemData(itemData);
0113 
0114     const QStringMap::const_iterator stringEnd = itemData.dataString.end();
0115     for (QStringMap::const_iterator it = itemData.dataString.begin(); it != stringEnd; ++it) {
0116         VariantDataMap::iterator vit = m_variantData.find(it.key());
0117         if (vit == m_variantData.end())
0118             continue;
0119 
0120         if (vit.value()->type() == Variant::Type::PenStyle)
0121             setDataPenStyle(it.key(), idToPenStyle(it.value()));
0122 
0123         else if (vit.value()->type() == Variant::Type::PenCapStyle)
0124             setDataPenCapStyle(it.key(), idToPenCapStyle(it.value()));
0125     }
0126 }
0127 
0128 QString DrawPart::penStyleToID(Qt::PenStyle style)
0129 {
0130     switch (style) {
0131     case Qt::SolidLine:
0132         return "SolidLine";
0133     case Qt::NoPen:
0134         return "NoPen";
0135     case Qt::DashLine:
0136         return "DashLine";
0137     case Qt::DotLine:
0138         return "DotLine";
0139     case Qt::DashDotLine:
0140         return "DashDotLine";
0141     case Qt::DashDotDotLine:
0142         return "DashDotDotLine";
0143     case Qt::MPenStyle:
0144     default:
0145         return ""; // ?!
0146     }
0147 }
0148 Qt::PenStyle DrawPart::idToPenStyle(const QString &id)
0149 {
0150     if (id == "NoPen")
0151         return Qt::NoPen;
0152     if (id == "DashLine")
0153         return Qt::DashLine;
0154     if (id == "DotLine")
0155         return Qt::DotLine;
0156     if (id == "DashDotLine")
0157         return Qt::DashDotLine;
0158     if (id == "DashDotDotLine")
0159         return Qt::DashDotDotLine;
0160     return Qt::SolidLine;
0161 }
0162 QString DrawPart::penCapStyleToID(Qt::PenCapStyle style)
0163 {
0164     switch (style) {
0165     case Qt::FlatCap:
0166         return "FlatCap";
0167     case Qt::SquareCap:
0168         return "SquareCap";
0169     case Qt::RoundCap:
0170         return "RoundCap";
0171     case Qt::MPenCapStyle:
0172     default:
0173         return ""; // ?!
0174     }
0175 }
0176 Qt::PenCapStyle DrawPart::idToPenCapStyle(const QString &id)
0177 {
0178     if (id == "SquareCap")
0179         return Qt::SquareCap;
0180     if (id == "RoundCap")
0181         return Qt::RoundCap;
0182     return Qt::FlatCap;
0183 }
0184 
0185 QString DrawPart::penStyleToName(Qt::PenStyle style)
0186 {
0187     switch (style) {
0188     case Qt::SolidLine:
0189         return i18n("Solid");
0190     case Qt::NoPen:
0191         return i18n("None");
0192     case Qt::DashLine:
0193         return i18n("Dash");
0194     case Qt::DotLine:
0195         return i18n("Dot");
0196     case Qt::DashDotLine:
0197         return i18n("Dash Dot");
0198     case Qt::DashDotDotLine:
0199         return i18n("Dash Dot Dot");
0200     case Qt::MPenStyle:
0201     default:
0202         return ""; // ?!
0203     }
0204 }
0205 Qt::PenStyle DrawPart::nameToPenStyle(const QString &name)
0206 {
0207     if (name == i18n("None"))
0208         return Qt::NoPen;
0209     if (name == i18n("Dash"))
0210         return Qt::DashLine;
0211     if (name == i18n("Dot"))
0212         return Qt::DotLine;
0213     if (name == i18n("Dash Dot"))
0214         return Qt::DashDotLine;
0215     if (name == i18n("Dash Dot Dot"))
0216         return Qt::DashDotDotLine;
0217     return Qt::SolidLine;
0218 }
0219 QString DrawPart::penCapStyleToName(Qt::PenCapStyle style)
0220 {
0221     switch (style) {
0222     case Qt::FlatCap:
0223         return i18n("Flat");
0224     case Qt::SquareCap:
0225         return i18n("Square");
0226     case Qt::RoundCap:
0227         return i18n("Round");
0228     case Qt::MPenCapStyle:
0229     default:
0230         return ""; // ?!
0231     }
0232 }
0233 Qt::PenCapStyle DrawPart::nameToPenCapStyle(const QString &name)
0234 {
0235     if (name == i18n("Square"))
0236         return Qt::SquareCap;
0237     if (name == i18n("Round"))
0238         return Qt::RoundCap;
0239     return Qt::FlatCap;
0240 }