summaryrefslogtreecommitdiffstats
path: root/src/drawparts/drawpart.cpp
blob: 8e5d8036b1b0608c2936acd2e27f8edccc680faa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/***************************************************************************
 *   Copyright (C) 2005 by David Saxton                                    *
 *   david@bluehaze.org                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "itemdocument.h"
#include "itemdocumentdata.h"
#include "drawpart.h"
#include "variant.h"

#include <klocale.h>
#include <tqbitarray.h>

DrawPart::DrawPart( ItemDocument *itemDocument, bool newItem, const char *id )
	: Item( itemDocument, newItem, id )
{
	itemDocument->registerItem(this);
}


DrawPart::~DrawPart()
{
}


int DrawPart::rtti() const
{
	return ItemDocument::RTTI::DrawPart;
}


Variant * DrawPart::createProperty( const TQString & id, Variant::Type::Value type )
{
	if ( type == Variant::Type::PenStyle )
	{
		TQStringList penStyles;
		penStyles << DrawPart::penStyleToName(Qt::SolidLine) << DrawPart::penStyleToName(Qt::DashLine)
				<< DrawPart::penStyleToName(Qt::DotLine) << DrawPart::penStyleToName(Qt::DashDotLine)
				<< DrawPart::penStyleToName(Qt::DashDotDotLine);
	
		Variant * v = createProperty( id, Variant::Type::String );
		v->setType( Variant::Type::PenStyle );
		v->setAllowed(penStyles);
		return v;
	}
	
	if ( type == Variant::Type::PenCapStyle )
	{
		TQStringList penCapStyles;
		penCapStyles << DrawPart::penCapStyleToName(Qt::FlatCap) << DrawPart::penCapStyleToName(Qt::SquareCap)
				<< DrawPart::penCapStyleToName(Qt::RoundCap);
		
		Variant * v = createProperty( id, Variant::Type::String );
		v->setType( Variant::Type::PenCapStyle );
		v->setAllowed(penCapStyles);
		return v;
	}
	
	return Item::createProperty( id, type );
}


Qt::PenStyle DrawPart::getDataPenStyle( const TQString & id )
{
	return nameToPenStyle( dataString(id) );
}
Qt::PenCapStyle DrawPart::getDataPenCapStyle( const TQString & id )
{
	return nameToPenCapStyle( dataString(id) );
}
void DrawPart::setDataPenStyle( const TQString & id, Qt::PenStyle value )
{
	property(id)->setValue( penStyleToName(value) );
}
void DrawPart::setDataPenCapStyle( const TQString & id, Qt::PenCapStyle value )
{
	property(id)->setValue( penCapStyleToName(value) );
}


ItemData DrawPart::itemData() const
{
	ItemData itemData = Item::itemData();
	
	const VariantDataMap::const_iterator end = m_variantData.end();
	for ( VariantDataMap::const_iterator it = m_variantData.begin(); it != end; ++it )
	{
		switch( it.data()->type() )
		{
			case Variant::Type::PenStyle:
				itemData.dataString[it.key()] = penStyleToID( nameToPenStyle( it.data()->value().toString() ) );
				break;
			case Variant::Type::PenCapStyle:
				itemData.dataString[it.key()] = penCapStyleToID( nameToPenCapStyle( it.data()->value().toString() ) );
				break;
			case Variant::Type::String:
			case Variant::Type::FileName:
			case Variant::Type::Port:
			case Variant::Type::Pin:
			case Variant::Type::VarName:
			case Variant::Type::Combo:
			case Variant::Type::Select:
			case Variant::Type::Multiline:
			case Variant::Type::Int:
			case Variant::Type::Double:
			case Variant::Type::Color:
			case Variant::Type::Bool:
			case Variant::Type::Raw:
			case Variant::Type::SevenSegment:
			case Variant::Type::KeyPad:
			case Variant::Type::None:
				// All of these are handled by Item
				break;
		}
	}
	
	return itemData;
}


void DrawPart::restoreFromItemData( const ItemData &itemData )
{
	Item::restoreFromItemData(itemData);
	
	const TQStringMap::const_iterator stringEnd = itemData.dataString.end();
	for ( TQStringMap::const_iterator it = itemData.dataString.begin(); it != stringEnd; ++it )
	{
		VariantDataMap::iterator vit = m_variantData.find(it.key());
		if ( vit == m_variantData.end() )
			continue;
		
		if ( vit.data()->type() == Variant::Type::PenStyle )
			setDataPenStyle( it.key(), idToPenStyle( it.data() ) );
		
		else if ( vit.data()->type() == Variant::Type::PenCapStyle )
			setDataPenCapStyle( it.key(), idToPenCapStyle( it.data() ) );
	}
}



TQString DrawPart::penStyleToID( Qt::PenStyle style )
{
	switch (style)
	{
		case Qt::SolidLine:
			return "SolidLine";
		case Qt::NoPen:
			return "NoPen";
		case Qt::DashLine:
			return "DashLine";
		case Qt::DotLine:
			return "DotLine";
		case Qt::DashDotLine:
			return "DashDotLine";
		case Qt::DashDotDotLine:
			return "DashDotDotLine";
		case Qt::MPenStyle:
		default:
			return ""; // ?!
	}
}
Qt::PenStyle DrawPart::idToPenStyle( const TQString & id )
{
	if ( id == "NoPen" )
		return Qt::NoPen;
	if ( id == "DashLine" )
		return Qt::DashLine;
	if ( id == "DotLine" )
		return Qt::DotLine;
	if ( id == "DashDotLine" )
		return Qt::DashDotLine;
	if ( id == "DashDotDotLine" )
		return Qt::DashDotDotLine;
	return Qt::SolidLine;
}
TQString DrawPart::penCapStyleToID( Qt::PenCapStyle style )
{
	switch (style)
	{
		case Qt::FlatCap:
			return "FlatCap";
		case Qt::SquareCap:
			return "SquareCap";
		case Qt::RoundCap:
			return "RoundCap";
		case Qt::MPenCapStyle:
		default:
			return ""; // ?!
	}
}
Qt::PenCapStyle DrawPart::idToPenCapStyle( const TQString & id )
{
	if ( id == "SquareCap" )
		return Qt::SquareCap;
	if ( id == "RoundCap" )
		return Qt::RoundCap;
	return Qt::FlatCap;
}

TQString DrawPart::penStyleToName( Qt::PenStyle style )
{
	switch (style)
	{
		case Qt::SolidLine:
			return i18n("Solid");
		case Qt::NoPen:
			return i18n("None");
		case Qt::DashLine:
			return i18n("Dash");
		case Qt::DotLine:
			return i18n("Dot");
		case Qt::DashDotLine:
			return i18n("Dash Dot");
		case Qt::DashDotDotLine:
			return i18n("Dash Dot Dot");
		case Qt::MPenStyle:
		default:
			return ""; // ?!
	}
}
Qt::PenStyle DrawPart::nameToPenStyle( const TQString & name )
{
	if ( name == i18n("None") )
		return Qt::NoPen;
	if ( name == i18n("Dash") )
		return Qt::DashLine;
	if ( name == i18n("Dot") )
		return Qt::DotLine;
	if ( name == i18n("Dash Dot") )
		return Qt::DashDotLine;
	if ( name == i18n("Dash Dot Dot") )
		return Qt::DashDotDotLine;
	return Qt::SolidLine;
}
TQString DrawPart::penCapStyleToName( Qt::PenCapStyle style )
{
	switch (style)
	{
		case Qt::FlatCap:
			return i18n("Flat");
		case Qt::SquareCap:
			return i18n("Square");
		case Qt::RoundCap:
			return i18n("Round");
		case Qt::MPenCapStyle:
		default:
			return ""; // ?!
	}
}
Qt::PenCapStyle DrawPart::nameToPenCapStyle( const TQString & name )
{
	if ( name == i18n("Square") )
		return Qt::SquareCap;
	if ( name == i18n("Round") )
		return Qt::RoundCap;
	return Qt::FlatCap;
}