summaryrefslogtreecommitdiffstats
path: root/src/electronics/subcircuits.cpp
blob: fef6752cd146f6ae3f00f1e6dbc5d829b7ab3964 (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
/***************************************************************************
 *   Copyright (C) 2004-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 "circuitdocument.h"
#include "ecsubcircuit.h"
#include "itemdocumentdata.h"
#include "itemlibrary.h"
#include "itemselector.h"
#include "subcircuits.h"

#include <kapplication.h>
#include <kconfig.h>
#include <kdebug.h>
#include <kiconloader.h>
#include <kstandarddirs.h>
#include <tqfile.h>
#include <tqtextstream.h>

Subcircuits::Subcircuits()
	: TQObject()
{
	connect( ComponentSelector::self(), TQT_SIGNAL(itemRemoved(const TQString& )), this, TQT_SLOT(slotItemRemoved(const TQString& )) );
}


Subcircuits::~Subcircuits()
{
}


void Subcircuits::initECSubcircuit( int subcircuitId, ECSubcircuit *ecSubcircuit )
{
	const TQString fileName = genFileName(subcircuitId);
	if ( !TQFile::exists(fileName) )
	{
		kdDebug() << "Subcircuits::createSubcircuit: Subcircuit \""<<fileName<<"\" was not found."<<endl;
		return;
	}
	
	SubcircuitData subcircuit;
	if (!subcircuit.loadData( genFileName(subcircuitId) ) )
		return;
	
	subcircuit.initECSubcircuit(ecSubcircuit);
}


ECSubcircuit* Subcircuits::createSubcircuit( int id, CircuitDocument *circuitDocument, bool newItem, const char *newId )
{
	//I pass finishCreation = false here because the subcircuit was getting
	//finished twice, causing a segfault in CircuitDocument::assignCircuits()
	//--electronerd
	ECSubcircuit *ecSubcircuit = static_cast<ECSubcircuit*>(itemLibrary()->createItem( "ec/subcircuit", circuitDocument, newItem, newId, false ));
	ecSubcircuit->property("id")->setValue(id);
	return ecSubcircuit;
}


void Subcircuits::loadSubcircuits()
{
	KConfig *config = kapp->config();
	config->setGroup("Subcircuits");
	
	TQValueList<int> idList = config->readIntListEntry("Ids");
	const TQValueList<int>::iterator idListEnd = idList.end();
	for ( TQValueList<int>::iterator it = idList.begin(); it != idListEnd; ++it )
	{
		TQFile file( genFileName(*it) );
		if ( file.open(IO_ReadOnly) == false )
		{
			// File has mysteriously disappeared....
			*it = -1;
		}
		else
		{
			config->setGroup("Subcircuit_"+TQString::number(*it));
			updateComponentSelector( *it, config->readEntry("Name") );
		}
		file.close();
	}
	idList.remove(-1);
	
	// Update the config file if any ids have been removed
	config->setGroup("Subcircuits");
	config->writeEntry( "Ids", idList );
}


TQString Subcircuits::genFileName( const int nextId )
{
	return locateLocal( "appdata", "subcircuit_"+TQString::number(nextId)+".circuit" );
}


void Subcircuits::updateComponentSelector( int id, const TQString &name )
{
	if ( name.isEmpty() )
		return;
	
	ComponentSelector::self()->addItem( name, "sc/"+TQString::number(id), "Subcircuits", TDEGlobal::iconLoader()->loadIcon( "ktechlab_circuit", KIcon::Small ), true );
}


void Subcircuits::addSubcircuit( const TQString &name, const TQString &subcircuitXml )
{
	KConfig *config = kapp->config();
	config->setGroup("Subcircuits");
	
	int nextId = config->readNumEntry( "NextId", 0 );
	
	while ( TQFile::exists( genFileName(nextId) ) ) {
		nextId++;
	}
	
	const int id = nextId;
	
	const TQString fileName = genFileName(id);
	TQFile file(fileName);
	
	if ( file.open(IO_WriteOnly) == false )
	{
		kdError() << "Subcircuits::addSubcircuit: couldn't open subcircuit save file: "<<fileName<<endl;
		return;
	}
	
	TQTextStream stream(&file);
	stream << subcircuitXml;
	file.close();
	
	TQValueList<int> idList = config->readIntListEntry("Ids");
	idList += id;
	config->writeEntry( "Ids", idList );
	config->writeEntry( "NextId", ++nextId );
	
	config->setGroup("Subcircuit_"+TQString::number(id));
	config->writeEntry( "Name", name );
	
	// It's important that we write the configuration *now*, lest the subcircuits be lost
	config->sync();
	
	updateComponentSelector( id, name );
}


void Subcircuits::slotItemRemoved( const TQString &id )
{
	if ( !id.startsWith("sc/") ) {
		return;
	}
	
	TQString temp = id;
	temp.remove("sc/");
	const int id_num = temp.toInt();
	const TQString fileName = genFileName(id_num);
	TQFile file(fileName);
	file.remove();
	
	KConfig *config = kapp->config();
	config->setGroup("Subcircuits");
	TQValueList<int> idList = config->readIntListEntry("Ids");
	idList.remove(id_num);
	config->writeEntry( "Ids", idList );
}


#include "subcircuits.moc"