📄 project.cpp
字号:
/************************************************************************ Copyright (C) 2000 Trolltech AS. All rights reserved.****1 This file is part of Qt Designer.**** This file may be distributed and/or modified under the terms of the** GNU General Public License version 2 as published by the Free Software** Foundation and appearing in the file LICENSE.GPL included in the** packaging of this file.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.**** See http://www.trolltech.com/gpl/ for GPL licensing information.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************/#include "project.h"#include "formwindow.h"#include "config.h"#include "designerappiface.h"#include "../interfaces/languageinterface.h"#include "pixmapcollection.h"#ifndef QT_NO_SQL#include "dbconnectionimpl.h"#endif#include "resource.h"#include <qwidgetfactory.h>#include "outputwindow.h"#include <qfile.h>#include <qtextstream.h>#include <qurl.h>#include <qobjectlist.h>#include <qfeatures.h>#include <qtextcodec.h>#include <qdom.h>#include <qmessagebox.h>#include <qapplication.h>#include "mainwindow.h"#include <qworkspace.h>#ifndef QT_NO_SQL#include <qsqldatabase.h>#include <qsqlrecord.h>#include <qdatatable.h>#endif#include <stdlib.h>#ifdef Q_OS_UNIX# include <unistd.h>#endif#include <private/qpluginmanager_p.h>#include "../interfaces/programinterface.h"#include "../interfaces/interpreterinterface.h"static QPluginManager<ProgramInterface> *programPluginManager = 0;static QPluginManager<InterpreterInterface> *interpreterPluginManager = 0;static void setupProjectPluginManagers(){ if ( programPluginManager ) return; programPluginManager = new QPluginManager<ProgramInterface>( IID_Program, QApplication::libraryPaths(), "/qsa" ); interpreterPluginManager = new QPluginManager<InterpreterInterface>( IID_Interpreter, QApplication::libraryPaths(), "/qsa" );}#ifndef QT_NO_SQLDatabaseConnection::~DatabaseConnection(){ delete iface;}bool DatabaseConnection::refreshCatalog(){#ifndef QT_NO_SQL if ( loaded ) return TRUE; if ( !open() ) return FALSE; tbls = conn->tables(); flds.clear(); for ( QStringList::Iterator it = tbls.begin(); it != tbls.end(); ++it ) { QSqlRecord fil = conn->record( *it ); QStringList lst; for ( uint j = 0; j < fil.count(); ++j ) lst << fil.field( j )->name(); flds.insert( *it, lst ); } loaded = TRUE; conn->close(); return loaded;#else return FALSE;#endif}#ifndef QT_NO_SQLvoid DatabaseConnection::remove(){ if ( nm == "(default)" ) QSqlDatabase::removeDatabase( QSqlDatabase::defaultConnection ); else QSqlDatabase::removeDatabase( nm ); // the above will effectively delete the current connection conn = 0;}#endifbool DatabaseConnection::open( bool suppressDialog ){#ifndef QT_NO_SQL // register our name, if nec if ( nm == "(default)" ) { if ( !QSqlDatabase::contains() ) // default doesn't exists? conn = QSqlDatabase::addDatabase( drv ); else conn = QSqlDatabase::database(); } else { if ( !QSqlDatabase::contains( nm ) ) conn = QSqlDatabase::addDatabase( drv, nm ); else conn = QSqlDatabase::database( nm ); } conn->setDatabaseName( dbName ); conn->setUserName( uname ); conn->setPassword( pword ); conn->setHostName( hname ); conn->setPort( prt ); bool success = conn->open(); for( ; suppressDialog == FALSE ; ) { bool done = FALSE; if ( !success ) { DatabaseConnectionEditor dia( this, 0 , 0 , TRUE ); switch( dia.exec() ) { case QDialog::Accepted: done = FALSE; break; case QDialog::Rejected: done = TRUE; break; } } if ( done ) break; conn->setUserName( uname ); conn->setPassword( pword ); conn->setHostName( hname ); conn->setPort( prt ); success = conn->open(); if ( !success ) { switch( QMessageBox::warning( project->messageBoxParent(), QApplication::tr( "Connection" ), QApplication::tr( "Could not connect to the database.\n" "Press 'OK' to continue or 'Cancel' to " "specify different\nconnection information.\n" ) + QString( "[" + conn->lastError().driverText() + "\n" + conn->lastError().databaseText() + "]\n" ), QApplication::tr( "&OK" ), QApplication::tr( "&Cancel" ), QString::null, 0, 1 ) ) { case 0: // OK or Enter continue; case 1: // Cancel or Escape done = TRUE; break; } } else break; if ( done ) break; } if ( !success ) { dbErr = conn->lastError().driverText() + "\n" + conn->lastError().databaseText(); remove(); } return success;#else return FALSE;#endif}void DatabaseConnection::close(){ if ( !loaded ) return;#ifndef QT_NO_SQL if ( conn ) { conn->close(); }#endif}DesignerDatabase *DatabaseConnection::iFace(){ if ( !iface ) iface = new DesignerDatabaseImpl( this ); return iface;}#endif////////bool Project::isDummy() const{ return isDummyProject;}Project::Project( const QString &fn, const QString &pName, QPluginManager<ProjectSettingsInterface> *pm, bool isDummy, const QString &l ) : proName( pName ), projectSettingsPluginManager( pm ), isDummyProject( isDummy ){ modified = TRUE; pixCollection = new PixmapCollection( this ); iface = 0; lang = l; is_cpp = lang == "C++"; cfg.insert( "(all)", "qt warn_on release" ); templ = "app"; setFileName( fn ); if ( !pName.isEmpty() ) proName = pName; sourcefiles.setAutoDelete( TRUE ); modified = FALSE; objs.setAutoDelete( FALSE ); fakeFormFiles.setAutoDelete( FALSE );}Project::~Project(){ if ( singleProjectMode() ) removeTempProject(); delete iface; delete pixCollection;}void Project::setModified( bool b ){ modified = b; emit projectModified();}#ifndef QT_NO_SQLDatabaseConnection *Project::databaseConnection( const QString &name ){ for ( DatabaseConnection *conn = dbConnections.first(); conn; conn = dbConnections.next() ) { if ( conn->name() == name ) return conn; } return 0;}#endifvoid Project::setFileName( const QString &fn, bool doClear ){ if ( fn == filename ) return; if ( singleProjectMode() ) { QString qsa = QString( getenv( "HOME" ) ) + QString( "/.qsa" ); if ( !QFile::exists( qsa ) ) { QDir d; d.mkdir( qsa ); } if ( fn == singleProFileName ) return; singleProFileName = fn; static int counter = 0; QString str_counter = QString::number( counter++ ); str_counter = "/.qsa/" + str_counter; LanguageInterface *iface = MetaDataBase::languageInterface( language() ); filename = QString( getenv( "HOME" ) + str_counter + QString( "tmp_" ) + QFileInfo( fn ).baseName() + "/" + QFileInfo( fn ).baseName() + ".pro" ); removeTempProject(); if ( iface && iface->supports( LanguageInterface::CompressProject ) ) { filename = iface->uncompressProject( makeAbsolute( singleProFileName ), QString( getenv( "HOME" ) + str_counter + QString( "tmp_" ) + QFileInfo( fn ).baseName() ) ); proName = makeAbsolute( singleProFileName ); } } else { filename = fn; if ( !filename.endsWith( ".pro" ) ) filename += ".pro"; proName = filename; } if ( proName.contains( '.' ) ) proName = proName.left( proName.find( '.' ) ); if ( !doClear ) return; clear(); if ( QFile::exists( filename ) ) parse();}QString Project::fileName( bool singlePro ) const{ if ( singlePro ) return singleProFileName; return filename;}QString Project::databaseDescription() const{ return dbFile;}QString Project::projectName() const{ return proName;}static QString parse_part( const QString &part ){ QString res; bool inName = FALSE; QString currName; for ( int i = 0; i < (int)part.length(); ++i ) { QChar c = part[ i ]; if ( !inName ) { if ( c != ' ' && c != '\t' && c != '\n' && c != '=' && c != '\\' && c != '+' ) inName = TRUE; else continue; } if ( inName ) { if ( c == '\n' ) break; res += c; } } return res;}QStringList parse_multiline_part( const QString &contents, const QString &key, int *start = 0 ){ if ( start ) *start = -1; QString lastWord; int braceCount = 0; for ( int i = 0; i < (int)contents.length(); ++i ) { QChar c( contents[ i ] ); switch ( c ) { case '{': braceCount++; lastWord = ""; break; case '}': braceCount--; lastWord = ""; break; case ' ': case '\t': case '\\': case '\n': lastWord = ""; break; default: lastWord += c; } // ### we should read the 'bla { SOURCES= ... }' stuff as well (braceCount > 0) if ( lastWord == key && braceCount == 0 ) { if ( start ) *start = i - lastWord.length() + 1; QStringList lst; bool inName = FALSE; QString currName; bool hadEqual = FALSE; for ( ; i < (int)contents.length(); ++i ) { c = contents[ i ]; if ( !hadEqual && c != '=' ) continue; if ( !hadEqual ) { hadEqual = TRUE; continue; } if ( ( c.isLetter() || c.isDigit() || c == '.' || c == '/' || c == '_' || c == '\\' || c == '\"' || c == '\'' || c == '=' || c == '$' || c == '-' || c == '(' || c == ')' || c == ':' || c == '+' || c == ',' || c == '~' ) && c != ' ' && c != '\t' && c != '\n' ) { if ( !inName ) currName = QString::null; if ( c != '\\' || contents[i+1] != '\n' ) { currName += c; inName = TRUE; } } else { if ( inName ) { inName = FALSE; if ( currName.simplifyWhiteSpace() != "\\" ) lst.append( currName ); } if ( c == '\n' && i > 0 && contents[ (int)i - 1 ] != '\\' ) break; } } return lst; } } return QStringList();}void Project::parse(){ QFile f( filename ); if ( !f.exists() || !f.open( IO_ReadOnly ) ) return; QTextStream ts( &f ); QString contents = ts.read(); f.close(); proName = QFileInfo( filename ).baseName(); QStringList::ConstIterator it; int i = contents.find( "LANGUAGE" ); if ( i != -1 ) { lang = ""; is_cpp = FALSE; QString part = contents.mid( i + QString( "LANGUAGE" ).length() ); lang = parse_part( part ); is_cpp = lang == "C++"; } i = contents.find( "DBFILE" ); if ( i != -1 ) { dbFile = "";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -