📄 project.cpp
字号:
{ if ( !sourcefiles.containsRef( sf ) ) return FALSE; if ( !sf->close() ) return FALSE; sourcefiles.removeRef( sf ); modified = TRUE; emit sourceFileRemoved( sf ); return TRUE;}void Project::setDatabaseDescription( const QString &db ){ dbFile = db;}void Project::setDescription( const QString &s ){ desc = s;}QString Project::description() const{ return desc;}bool Project::isValid() const{ // #### do more checking here? if ( filename.isEmpty() || proName.isEmpty() ) return FALSE; return TRUE;}QString Project::makeAbsolute( const QString &f ){ if ( isDummy() ) return f; QString encodedUrl = QFileInfo( filename ).dirPath( TRUE ); QUrl::encode( encodedUrl ); QUrl u( encodedUrl, f ); return u.path();}QString Project::makeRelative( const QString &f ){ if ( isDummy() ) return f; QString p = QFileInfo( filename ).dirPath( TRUE ); QString f2 = f;#if defined(Q_WS_WIN32) if ( p.endsWith("/") ) p = p.left( p.length() - 1 ); if ( f2.left( p.length() ).lower() == p.lower() )#else if ( f2.left( p.length() ) == p )#endif f2.remove( 0, p.length() + 1 ); return f2;}static void remove_contents( QString &contents, const QString &s ){ int i = contents.find( s ); if ( i != -1 ) { int start = i; int end = contents.find( '\n', i ); if ( end == -1 ) end = contents.length() - 1; contents.remove( start, end - start + 1 ); }}static void remove_multiline_contents( QString &contents, const QString &s, int *strt = 0 ){ int i = contents.find( s ); int startBrace = contents.findRev("{", i); int endBrace = contents.findRev("}", i); // We found a startBrace before i, but no endBrace after the startBrace before i if (startBrace != -1 && endBrace < startBrace) return; if ( strt ) *strt = i; int start = i; bool lastWasBackspash = TRUE; if ( i != -1 && ( i == 0 || contents[ i - 1 ] != '{' || contents[ i - 1 ] != ':' ) && !(i > 0 && contents[i-1] != '\n')) { for ( ; i < (int)contents.length(); ++i ) { if ( contents[ i ] == '\n' && !lastWasBackspash ) break; lastWasBackspash = ( contents[ i ] == '\\' || lastWasBackspash && ( contents[ i ] == ' ' || contents[ i ] == '\t' ) ); } contents.remove( start, i - start + 1 ); }}void Project::save( bool onlyProjectFile ){ bool anythingModified = FALSE; // save sources and forms if ( !onlyProjectFile ) { saveConnections(); for ( SourceFile *sf = sourcefiles.first(); sf; sf = sourcefiles.next() ) { anythingModified = anythingModified || sf->isModified(); if ( !sf->save() ) return; } for ( FormFile *ff = formfiles.first(); ff; ff = formfiles.next() ) { anythingModified = anythingModified || ff->isModified(); if ( !ff->save() ) return; } } if ( isDummy() || filename.isEmpty() ) return; if ( !modified ) { if ( singleProjectMode() ) { LanguageInterface *iface = MetaDataBase::languageInterface( language() ); if ( iface && iface->supports( LanguageInterface::CompressProject ) ) iface->compressProject( makeAbsolute( filename ), singleProFileName, anythingModified ); } return; } QFile f( filename ); QString original = ""; // read the existing file bool hasPreviousContents = FALSE; if ( f.open( IO_ReadOnly ) ) { QTextStream ts( &f ); original = ts.read(); f.close(); hasPreviousContents = TRUE; remove_contents( original, "{SOURCES+=" ); // ### compatibility with early 3.0 betas remove_contents( original, "DBFILE" ); remove_contents( original, "LANGUAGE" ); remove_contents( original, "TEMPLATE" ); removePlatformSettings( original, "CONFIG" ); removePlatformSettings( original, "DEFINES" ); removePlatformSettings( original, "LIBS" ); removePlatformSettings( original, "INCLUDEPATH" ); removePlatformSettings( original, "SOURCES" ); removePlatformSettings( original, "HEADERS" ); remove_multiline_contents( original, "FORMS" ); remove_multiline_contents( original, "INTERFACES" ); // compatibility remove_multiline_contents( original, "IMAGES" ); for ( QStringList::Iterator it = csList.begin(); it != csList.end(); ++it ) remove_contents( original, *it ); } if (!original.isEmpty()) { // Removes any new lines at the beginning of the file while (original.startsWith("\n")) original.remove(0, 1); } // the contents of the saved file QString contents; // template contents += "TEMPLATE\t= " + templ + "\n"; // language contents += "LANGUAGE\t= " + lang + "\n"; contents += "\n"; // config writePlatformSettings( contents, "CONFIG", cfg ); LanguageInterface *iface = MetaDataBase::languageInterface( lang ); if ( iface ) { QStringList sourceKeys; iface->sourceProjectKeys( sourceKeys ); for ( QStringList::Iterator spit = sourceKeys.begin(); spit != sourceKeys.end(); ++spit ) remove_multiline_contents( contents, *spit ); } // libs, defines, includes writePlatformSettings( contents, "LIBS", lbs ); writePlatformSettings( contents, "DEFINES", defs ); writePlatformSettings( contents, "INCLUDEPATH", inclPath ); writePlatformSettings( contents, "SOURCES", sources ); writePlatformSettings( contents, "HEADERS", headers ); // unix if ( !hasPreviousContents ) { contents += "unix {\n" " UI_DIR = .ui\n" " MOC_DIR = .moc\n" " OBJECTS_DIR = .obj\n" "}\n\n"; } // sources if ( !sourcefiles.isEmpty() && iface ) { QMap<QString, QStringList> sourceToKey; for ( SourceFile *f = sourcefiles.first(); f; f = sourcefiles.next() ) { QString key = iface->projectKeyForExtension( QFileInfo( f->fileName() ).extension() ); QStringList lst = sourceToKey[ key ]; lst << makeRelative( f->fileName() ); sourceToKey.replace( key, lst ); } for ( QMap<QString, QStringList>::Iterator skit = sourceToKey.begin(); skit != sourceToKey.end(); ++skit ) { QString part = skit.key() + "\t+= "; QStringList lst = *skit; for ( QStringList::Iterator sit = lst.begin(); sit != lst.end(); ++sit ) { part += *sit; part += ++sit != lst.end() ? " \\\n\t" : ""; --sit; } part += "\n\n"; contents += part; } } // forms and interfaces if ( !formfiles.isEmpty() ) { contents += "FORMS\t= "; for ( QPtrListIterator<FormFile> fit = formfiles; fit.current(); ++fit ) { contents += fit.current()->fileName() + (fit != formfiles.last() ? " \\\n\t" : ""); } contents += "\n\n"; } // images if ( !pixCollection->isEmpty() ) { contents += "IMAGES\t= "; QValueList<PixmapCollection::Pixmap> pixmaps = pixCollection->pixmaps(); for ( QValueList<PixmapCollection::Pixmap>::Iterator it = pixmaps.begin(); it != pixmaps.end(); ++it ) { contents += makeRelative( (*it).absname ); contents += ++it != pixmaps.end() ? " \\\n\t" : ""; --it; } contents += "\n\n"; } // database if ( !dbFile.isEmpty() ) contents += "DBFILE\t= " + dbFile + "\n"; // custom settings for ( QStringList::Iterator it = csList.begin(); it != csList.end(); ++it ) { QString val = *customSettings.find( *it ); if ( !val.isEmpty() ) contents += *it + "\t= " + val + "\n"; } if ( !f.open( IO_WriteOnly | IO_Translate ) ) { QMessageBox::warning( messageBoxParent(), "Save Project Failed", "Couldn't write project file " + filename ); return; } QTextStream os( &f ); os << contents; if (hasPreviousContents) os << original; f.close(); setModified( FALSE ); if ( singleProjectMode() ) { LanguageInterface *iface = MetaDataBase::languageInterface( language() ); if ( iface && iface->supports( LanguageInterface::CompressProject ) ) iface->compressProject( makeAbsolute( filename ), singleProFileName, TRUE ); }}#ifndef QT_NO_SQLQPtrList<DatabaseConnection> Project::databaseConnections() const{ return dbConnections;}#endif#ifndef QT_NO_SQLvoid Project::setDatabaseConnections( const QPtrList<DatabaseConnection> &lst ){ dbConnections = lst;}#endif#ifndef QT_NO_SQLvoid Project::addDatabaseConnection( DatabaseConnection *conn ){ dbConnections.append( conn ); modified = TRUE;}#endif#ifndef QT_NO_SQLvoid Project::removeDatabaseConnection( const QString &c ){ for ( DatabaseConnection *conn = dbConnections.first(); conn; conn = dbConnections.next() ) { if ( conn->name() == c ) { conn->remove(); dbConnections.removeRef( conn ); delete conn; return; } }}#endif#ifndef QT_NO_SQLQStringList Project::databaseConnectionList(){ QStringList lst; for ( DatabaseConnection *conn = dbConnections.first(); conn; conn = dbConnections.next() ) lst << conn->name(); return lst;}#endif#ifndef QT_NO_SQLQStringList Project::databaseTableList( const QString &connection ){ DatabaseConnection *conn = databaseConnection( connection ); if ( !conn ) { return QStringList(); } return conn->tables();}#endif#ifndef QT_NO_SQLQStringList Project::databaseFieldList( const QString &connection, const QString &table ){ DatabaseConnection *conn = databaseConnection( connection ); if ( !conn ) return QStringList(); return conn->fields( table );}#endif#ifndef QT_NO_SQLstatic QString makeIndent( int indent ){ QString s; s.fill( ' ', indent * 4 ); return s;}#endif#ifndef QT_NO_SQLstatic void saveSingleProperty( QTextStream &ts, const QString& name, const QString& value, int indent ){ ts << makeIndent( indent ) << "<property name=\"" << name << "\">" << endl; ++indent; ts << makeIndent( indent ) << "<string>" << value << "</string>" << endl; --indent; ts << makeIndent( indent ) << "</property>" << endl;}#endifvoid Project::saveConnections(){#ifndef QT_NO_SQL if ( dbFile.isEmpty() ) { QFileInfo fi( fileName() ); setDatabaseDescription( fi.baseName() + ".db" ); } QFile f( makeAbsolute( dbFile ) ); if ( dbConnections.isEmpty() ) { if ( f.exists() ) f.remove(); setDatabaseDescription( "" ); modified = TRUE; return; } /* .db xml */ if ( f.open( IO_WriteOnly | IO_Translate ) ) { QTextStream ts( &f ); ts.setCodec( QTextCodec::codecForName( "UTF-8" ) ); ts << "<!DOCTYPE DB><DB version=\"1.0\">" << endl; /* db connections */ int indent = 0; for ( DatabaseConnection *conn = dbConnections.first(); conn; conn = dbConnections.next() ) { ts << makeIndent( indent ) << "<connection>" << endl; ++indent; saveSingleProperty( ts, "name", conn->name(), indent ); saveSingleProperty( ts, "driver", conn->driver(), indent ); saveSingleProperty( ts, "database", conn->database(), indent ); saveSingleProperty( ts, "username", conn->username(), indent ); saveSingleProperty( ts, "hostname", conn->hostname(), indent ); saveSingleProperty( ts, "port", QString::number( conn->port() ), indent ); /* connection tables */ QStringList tables = conn->tables(); for ( QStringList::Iterator it = tables.begin(); it != tables.end(); ++it ) { ts << makeIndent( indent ) << "<table>" << endl; ++indent; saveSingleProperty( ts, "name", (*it), indent ); /* tables fields */ QStringList fields = conn->fields( *it ); for ( QStringList::Iterator it2 = fields.begin(); it2 != fields.end(); ++it2 ) { ts << makeIndent( indent ) << "<field>" << endl; ++indent; saveSingleProperty( ts, "name", (*it2), indent ); --indent; ts << makeIndent( indent ) << "</field>" << endl; } --indent; ts << makeIndent( indent ) << "</table>" << endl; } --indent; ts << makeIndent( indent ) << "</connection>" << endl; } ts << "</DB>" << endl; f.close(); }#endif}#ifndef QT_NO_SQLstatic QDomElement loadSingleProperty( QDomElement e, const QString& name ){ QDomElement n; for ( n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement() ) { if ( n.tagName() == "property" && n.toElement().attribute("name") == name ) return n; } return n;}#endifvoid Project::loadConnections(){#ifndef QT_NO_SQL if ( dbFile.isEmpty() || !QFile::exists( makeAbsolute( dbFile ) ) ) return; QFile f( makeAbsolute( dbFile ) ); if ( f.open( IO_ReadOnly ) ) { QDomDocument doc; QString errMsg; int errLine; if ( doc.setContent( &f, &errMsg, &errLine ) ) { QDomElement e; e = doc.firstChild().toElement(); /* connections */ QDomNodeList connections = e.toElement().elementsByTagName( "connection" ); for ( uint i = 0; i < connections.length(); i++ ) { QDomElement connection = connections.item(i).toElement(); QDomElement connectionName = loadSingleProperty( connection, "name" ); QDomElement connectionDriver = loadSingleProperty( connection, "driver" ); QDomElement connectionDatabase = loadSingleProperty( connection, "database" ); QDomElement connectionUsername = loadSingleProperty( connection, "username" ); QDomElement connectionHostname = loadSingleProperty( connection, "hostname" ); QDomElement connectionPort = loadSingleProperty( connection, "port" ); DatabaseConnection *conn = new DatabaseConnection( this ); conn->setName( connectionName.firstChild().firstChild().toText().data() ); conn->setDriver( connectionDriver.firstChild().firstChild().toText().data() ); conn->setDatabase( connectionDatabase.firstChild().firstChild().toText().data() ); conn->setUsername( connectionUsername.firstChild().firstChild().toText().data() ); conn->setHostname( connectionHostname.firstChild().firstChild().toText().data() ); conn->setPort( QString( connectionPort.firstChild().firstChild().toText().data() ).toInt() ); /* connection tables */ QDomNodeList tables = connection.toElement().elementsByTagName( "table" ); for ( uint j = 0; j < tables.length(); j++ ) { QDomElement table = tables.item(j).toElement(); QDomElement tableName = loadSingleProperty( table, "name" ); conn->addTable( tableName.firstChild().firstChild().toText().data() ); /* table fields */ QStringList fieldList; QDomNodeList fields = table.toElement().elementsByTagName( "field" ); for ( uint k = 0; k < fields.length(); k++ ) { QDomElement field = fields.item(k).toElement(); QDomElement fieldName = loadSingleProperty( field, "name" ); fieldList.append( fieldName.firstChild().firstChild().toText().data() ); } conn->setFields( tableName.firstChild().firstChild().toText().data(), fieldList ); } dbConnections.append( conn ); } } else { qDebug( QString("Parse error: ") + errMsg + QString(" in line %d"), errLine ); } f.close(); }#endif}/*! Opens the database \a connection. The connection remains open andcan be closed again with closeDatabase().*/bool Project::openDatabase( const QString &connection, bool suppressDialog ){#ifndef QT_NO_SQL DatabaseConnection *conn = databaseConnection( connection ); if ( connection.isEmpty() && !conn ) conn = databaseConnection( "(default)" ); if ( !conn ) return FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -