00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "circuitdocument.h"
00012 #include "ecsubcircuit.h"
00013 #include "itemdocumentdata.h"
00014 #include "itemlibrary.h"
00015 #include "itemselector.h"
00016 #include "subcircuits.h"
00017
00018 #include <kapplication.h>
00019 #include <kconfig.h>
00020 #include <kdebug.h>
00021 #include <kiconloader.h>
00022 #include <kstandarddirs.h>
00023 #include <qfile.h>
00024 #include <qtextstream.h>
00025
00026 Subcircuits::Subcircuits()
00027 : QObject()
00028 {
00029 connect( ComponentSelector::self(), SIGNAL(itemRemoved(const QString& )), this, SLOT(slotItemRemoved(const QString& )) );
00030 }
00031
00032
00033 Subcircuits::~Subcircuits()
00034 {
00035 }
00036
00037
00038 void Subcircuits::initECSubcircuit( int subcircuitId, ECSubcircuit *ecSubcircuit )
00039 {
00040 const QString fileName = genFileName(subcircuitId);
00041 if ( !QFile::exists(fileName) )
00042 {
00043 kdDebug() << "Subcircuits::createSubcircuit: Subcircuit \""<<fileName<<"\" was not found."<<endl;
00044 return;
00045 }
00046
00047 SubcircuitData subcircuit;
00048 if (!subcircuit.loadData( genFileName(subcircuitId) ) )
00049 return;
00050
00051 subcircuit.initECSubcircuit(ecSubcircuit);
00052 }
00053
00054
00055 ECSubcircuit* Subcircuits::createSubcircuit( int id, CircuitDocument *circuitDocument, bool newItem, const char *newId )
00056 {
00057
00058
00059
00060 ECSubcircuit *ecSubcircuit = static_cast<ECSubcircuit*>(itemLibrary()->createItem( "ec/subcircuit", circuitDocument, newItem, newId, false ));
00061 ecSubcircuit->property("id")->setValue(id);
00062 return ecSubcircuit;
00063 }
00064
00065
00066 void Subcircuits::loadSubcircuits()
00067 {
00068 KConfig *config = kapp->config();
00069 config->setGroup("Subcircuits");
00070
00071 QValueList<int> idList = config->readIntListEntry("Ids");
00072 const QValueList<int>::iterator idListEnd = idList.end();
00073 for ( QValueList<int>::iterator it = idList.begin(); it != idListEnd; ++it )
00074 {
00075 QFile file( genFileName(*it) );
00076 if ( file.open(IO_ReadOnly) == false )
00077 {
00078
00079 *it = -1;
00080 }
00081 else
00082 {
00083 config->setGroup("Subcircuit_"+QString::number(*it));
00084 updateComponentSelector( *it, config->readEntry("Name") );
00085 }
00086 file.close();
00087 }
00088 idList.remove(-1);
00089
00090
00091 config->setGroup("Subcircuits");
00092 config->writeEntry( "Ids", idList );
00093 }
00094
00095
00096 QString Subcircuits::genFileName( const int nextId )
00097 {
00098 return locateLocal( "appdata", "subcircuit_"+QString::number(nextId)+".circuit" );
00099 }
00100
00101
00102 void Subcircuits::updateComponentSelector( int id, const QString &name )
00103 {
00104 if ( name.isEmpty() )
00105 return;
00106
00107 ComponentSelector::self()->addItem( name, "sc/"+QString::number(id), "Subcircuits", KGlobal::iconLoader()->loadIcon( "ktechlab_circuit", KIcon::Small ), true );
00108 }
00109
00110
00111 void Subcircuits::addSubcircuit( const QString &name, const QString &subcircuitXml )
00112 {
00113 KConfig *config = kapp->config();
00114 config->setGroup("Subcircuits");
00115
00116 int nextId = config->readNumEntry( "NextId", 0 );
00117
00118 while ( QFile::exists( genFileName(nextId) ) ) {
00119 nextId++;
00120 }
00121
00122 const int id = nextId;
00123
00124 const QString fileName = genFileName(id);
00125 QFile file(fileName);
00126
00127 if ( file.open(IO_WriteOnly) == false )
00128 {
00129 kdError() << "Subcircuits::addSubcircuit: couldn't open subcircuit save file: "<<fileName<<endl;
00130 return;
00131 }
00132
00133 QTextStream stream(&file);
00134 stream << subcircuitXml;
00135 file.close();
00136
00137 QValueList<int> idList = config->readIntListEntry("Ids");
00138 idList += id;
00139 config->writeEntry( "Ids", idList );
00140 config->writeEntry( "NextId", ++nextId );
00141
00142 config->setGroup("Subcircuit_"+QString::number(id));
00143 config->writeEntry( "Name", name );
00144
00145
00146 config->sync();
00147
00148 updateComponentSelector( id, name );
00149 }
00150
00151
00152 void Subcircuits::slotItemRemoved( const QString &id )
00153 {
00154 if ( !id.startsWith("sc/") ) {
00155 return;
00156 }
00157
00158 QString temp = id;
00159 temp.remove("sc/");
00160 const int id_num = temp.toInt();
00161 const QString fileName = genFileName(id_num);
00162 QFile file(fileName);
00163 file.remove();
00164
00165 KConfig *config = kapp->config();
00166 config->setGroup("Subcircuits");
00167 QValueList<int> idList = config->readIntListEntry("Ids");
00168 idList.remove(id_num);
00169 config->writeEntry( "Ids", idList );
00170 }
00171
00172
00173 #include "subcircuits.moc"
00174
00175