commands.cpp

来自「qt-x11-free-3.0.3.tar.gz minigui图形界面工具」· C++ 代码 · 共 1,435 行 · 第 1/4 页

CPP
1,435
字号
    // Add the specific settings to compiler and linker    // TODO: read .qtwinconfig        const CComBSTR compiler("cl.exe");    const CComBSTR ipath("/I_$(QTDIR)\\include");    long cCount;    VERIFY_OK( pConfigs->get_Count(&cCount));    for (long c = 0; c < cCount; c++ ) {	CComVariant Varc = c+1;	CComPtr<IConfiguration> pConfig;	VERIFY_OK(pConfigs->Item(Varc, &pConfig));	VERIFY_OK(pConfig->AddToolSettings( compiler, ipath, CComVariant(VARIANT_FALSE) ));	if ( shared )	    addSharedSettings( pConfig, threaded );	else	    addStaticSettings( pConfig, threaded );    }    return S_OK;    m_pApplication->PrintToOutputWindow( CComBSTR("Finished!\n") );}STDMETHODIMP CCommands::QMsDevAddMOCStep(){    AFX_MANAGE_STATE(AfxGetStaticModuleState());    VERIFY_OK(m_pApplication->EnableModeless(VARIANT_FALSE));    // Check for active Project    CComQIPtr<IBuildProject, &IID_IBuildProject> pProject;    if ( getActiveProject( pProject ) != S_OK ) {	VERIFY_OK(m_pApplication->EnableModeless(VARIANT_FALSE));	if ( ::MessageBox( NULL, "There is no active project.\nDo you want to create a new Qt Project?", 	    "Add MOC to file", MB_YESNOCANCEL | MB_ICONQUESTION ) == IDYES )	    QMsDevNewQtProject();	VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));	return S_FALSE;    }    CString file;    CString fileext;    CString filename;    CString filepath;    file = getActiveFileName();    if ( file.IsEmpty() ) {	CFileDialog fd( TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 	    "Header files (*.h; *.hxx; *.hpp)|*.h; *.hxx; *.hpp|"	    "Implementation files (*.c; *.cpp; *.cxx)|*.c; *.cpp; *.cxx|"	    "C++ files (*.h; *.hxx; *.hpp; *.c; *.cpp; *.cxx)|*.h; *.hxx; *.hpp; *.c; *.cpp; *.cxx|"	    "All Files (*.*)|*.*||", NULL);	int result = fd.DoModal();        	if ( result == IDCANCEL ) {	    VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));	    return S_OK;	}	file = fd.GetPathName();    }    splitFileName( file, filepath, filename, fileext );    m_pApplication->PrintToOutputWindow( CComBSTR("Add MOC buildstep for "+file+"...") );    addMOC( pProject, filepath+file );    m_pApplication->PrintToOutputWindow( CComBSTR("Finished!\n") );        VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));    return S_OK;}STDMETHODIMP CCommands::QMsDevGenerateQtProject(){    AFX_MANAGE_STATE(AfxGetStaticModuleState());    VERIFY_OK(m_pApplication->EnableModeless(VARIANT_FALSE));    CComQIPtr<IBuildProject, &IID_IBuildProject> pProject;    if ( getActiveProject( pProject ) != S_OK ) {	VERIFY_OK(m_pApplication->EnableModeless(VARIANT_FALSE));	if ( ::MessageBox( NULL, "There is no active project.\nDo you want to create a new Qt Project?", 	    "Write Qt Project", MB_YESNOCANCEL | MB_ICONQUESTION ) == IDYES )	    QMsDevNewQtProject();	VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));	return S_FALSE;    }    CComBSTR fp;    pProject->get_FullName( &fp );    CString dspfile( fp );    CString dspfilepath, dspfilename, dspfileext;    splitFileName( dspfile, dspfilepath, dspfilename, dspfileext );    CFileDialog fd( FALSE, "*.pro", dspfilepath + dspfilename + ".pro", OFN_HIDEREADONLY, 	"Qt Project (*.pro)|*.pro|"	"All Files (*.*)|*.*||", NULL);    int result = fd.DoModal();    if ( result == IDCANCEL ) {	VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));	return S_OK;    }    CString profile = fd.GetPathName();    CString profilepath, profilename, profileext;    splitFileName( profile, profilepath, profilename, profileext );    CMapStringToString filelists;    CString group;    CString file;    CString filepath, filename, fileext;    try {	CStdioFile dsp( dspfilepath + dspfilename + "." + dspfileext , CFile::modeRead );	CString string;	while ( dsp.ReadString( string ) )	{	    if ( group.IsEmpty() && string.Find( "# Begin Group" ) == 0 ) {		group = string.Mid( 15, string.GetLength() - 16 );		group.MakeUpper();	    } else if ( string.Find( "SOURCE=" ) == 0 ) {		bool ignore = FALSE;		file = string.Right( string.GetLength() - 7 );		file.TrimLeft();		file.TrimRight();		splitFileName( file, filepath, filename, fileext );		if ( filepath.Left( 2 ) == ".\\" )		    filepath = filepath.Right( filepath.GetLength() - 2 );		ignore = filename.Left( 4 ) == "moc_" || fileext == "moc" || group == "GENERATED" || fileext == "ui";		if ( !ignore ) {		    if ( fileext == "ui" )			group = "FORMS";		    else if ( fileext == "h" || fileext == "hxx" || fileext == "hpp" )			group = "HEADERS";		    else if ( fileext == "cpp" || fileext == "cxx" || fileext == "c" )			group = "SOURCES";		    else if ( fileext == "y" )			group = "YACCSOURCES";		    else if ( fileext == "l" )			group = "LEXSOURCES";		    else if ( fileext == "ts" )			group = "TRANSLATIONS";		    else			break;		    CString temp;		    filelists.Lookup( group, temp );		    filepath.Replace( "\\", "/" );		    temp += " \\\n\t\t" + filepath + filename + "." + fileext;		    filelists.SetAt( group, temp );		}	    } else if ( !group.IsEmpty() && string.Find( "# End Group" ) == 0 ) {		group.Empty();	    }	}    }    catch ( CFileException* e )    {	char err[256];	e->GetErrorMessage( (char*)&err, 255, NULL );	::MessageBox( NULL, err, "Error", MB_OK );	VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));	return S_FALSE;    }    chdir( profilepath );    CStdioFile pro;    try {	CString string, tstring;	CString newstring;	bool newproject = TRUE;	// read the existing pro-file and replace SOURCES etc. with map contents	if ( pro.Open( profilepath + profilename + "." + profileext, CFile::modeReadWrite ) ) {	    newproject = FALSE;	    while ( pro.ReadString( string ) ) {		if ( !string.IsEmpty() ) {		    tstring += string;		    tstring.TrimRight();		}		if ( tstring.Right( 1 ) != "\\" ) {		    int firstSep = tstring.FindOneOf( "+=" );		    if ( firstSep != -1 ) {			CString group = tstring.Left( firstSep );			group.TrimRight();			if ( filelists.Lookup( group, file ) ) {			    tstring = group + " = " + file;			    filelists.RemoveKey( group );			}		    }		    newstring += tstring + "\n";		    tstring.Empty();		} else {		    if ( string.IsEmpty() )		    // bad pro-file			break;		    tstring += "\n\t";		}	    }	    pro.Close();	}	// add remaining new files and groups	POSITION pos = filelists.GetStartPosition();	while ( pos ) {	    filelists.GetNextAssoc( pos, group, file );	    newstring += group + " = " + file + "\n\n";	}	// new project, so read (or rather guess) default settings for TEMPLATE etc.	if ( !newstring.IsEmpty() && pro.Open( profilepath + profilename + "." + profileext, CFile::modeCreate | CFile::modeReadWrite | CFile::typeText ) ) {	    if ( newproject ) {		CString header;		header += "# qmake project generated by QMsDev\n#\n# General settings\n\n";		if ( newstring.Find( "main.cpp" ) != -1 ) {		    header += "TEMPLATE = app\n";		    header += "CONFIG  += qt warn_off\n";		} else {		    header += "TEMPLATE = lib\n";		    header += "CONFIG  += qt warn_off dll\n"; // nobody uses static libs anyway...		}		BSTR pname;		pProject->get_Name( &pname );		CString name( pname );		header += "TARGET  += " + name + "\n";		header += "\n\n# Input\n\n";		pro.WriteString( header );	    }	    	    pro.WriteString( newstring );	}    }    catch ( CFileException* e )    {	char err[256];	e->GetErrorMessage( (char*)&err, 255, NULL );	::MessageBox( NULL, err, "Error", MB_OK );	VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));	return S_FALSE;    }    return S_OK;}STDMETHODIMP CCommands::QMsDevNewQtProject(){    AFX_MANAGE_STATE(AfxGetStaticModuleState());    VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));    NewQtProjectDialog dialog;    if ( dialog.DoModal() == IDCANCEL ) {	VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));	return S_FALSE;    }    if ( mkdir( dialog.m_location ) < 0 ) {	::MessageBox(NULL, "Couldn't create Directory\n"+dialog.m_location, "New Qt Project", MB_OK | MB_ICONINFORMATION );	VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));	return S_FALSE;    }    m_pApplication->PrintToOutputWindow( CComBSTR("Creating Qt Project \"" + dialog.m_name + "\"...") );    CString filename = dialog.m_location;    if ( filename[ filename.GetLength() -1 ] != '\\' )	filename+="\\";    filename += dialog.m_name + ".dsp";    if ( m_pApplication->AddProject( CComBSTR(filename), CComBSTR(dialog.m_location), CComBSTR("Application"), VARIANT_TRUE ) != S_OK ) {	CComBSTR err;	GetLastErrorDescription( err );	::MessageBox(NULL, (LPCTSTR)err.m_str, "New Qt Project", MB_OK | MB_ICONINFORMATION );	VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));	return S_FALSE;    }    // Get project object we just created    CComQIPtr<IBuildProject, &IID_IBuildProject> pProject;    if ( getActiveProject( pProject ) != S_OK ) {	VERIFY_OK(m_pApplication->EnableModeless(VARIANT_FALSE));	::MessageBox(NULL, "Project creation failed!", "QMsDev", MB_OK | MB_ICONINFORMATION );	VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));	return S_FALSE;    }    // Get the list of configurations    CComQIPtr<IConfigurations, &IID_IConfigurations> pConfigs;    if ( getConfigurations( pProject, pConfigs ) != S_OK ) {	VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));	return S_FALSE;    }    long cCount;    VERIFY_OK( pConfigs->get_Count(&cCount));    for (long c = 0; c < cCount; c++ ) {	CComVariant Varc = c+1;	CComPtr<IConfiguration> pConfig;	VERIFY_OK(pConfigs->Item(Varc, &pConfig));	BSTR bstr;	pConfig->get_Name(&bstr);	bool shared;        bool useThreads = getGlobalQtSettings( shared );	if ( shared ) {	    m_pApplication->PrintToOutputWindow( CComBSTR("\tadding Qt shared library to "+ CString(bstr) + "...") );	    addSharedSettings( pConfig, useThreads );	} else {	    m_pApplication->PrintToOutputWindow( CComBSTR("\tadding Qt static library to "+ CString(bstr) + "...") );	    addStaticSettings( pConfig, useThreads );	}    }    CString projectName = dialog.m_name;    dialog.m_name.MakeLower();    CString classheader;    CString classname;    CString instancename;    CString instancecall;    CString runapp;    if ( dialog.m_dialog ) {	classheader = dialog.m_name + "dialog";	classname = projectName + "Dialog";	instancename = "dialog";	instancecall = "( 0, 0, TRUE )";	runapp = "dialog.exec();\n\n\treturn 0;";    } else {	classheader = dialog.m_name + "mainwindow";	classname = projectName + "MainWindow";	instancename = "mainwindow";	instancecall = "";	runapp = "mainwindow.show();\n\n\treturn app.exec();";    }    CString baseDir = dialog.m_location + "\\";    m_pApplication->PrintToOutputWindow( CComBSTR("\tcreating basic files...") );        // Write files and replace $MSDEV... strings with project-specific stuff    try {	CStdioFile mainFile( baseDir + "main.cpp", CFile::modeCreate | CFile::modeWrite );	mainFile.WriteString( replaceTemplateStrings(main_cpp, classheader, 						     classname, instancename, instancecall, projectName, runapp) );	mainFile.Close();	CStdioFile infoFile( baseDir + "readme.txt", CFile::modeCreate | CFile::modeWrite );	CString readme( readme_txt );	if ( dialog.m_dialog ) {	    readme.Replace( "$QMSDEVUITYPE", "Dialog interface" );	    readme.Replace( "$QMSDEVFILELIST", classheader+"base.ui\n\tA dialog with basic buttons.\n"					       "\tUse the Qt GUI Designer change the layout.\n" );	    readme.Replace( "$QMSDEVQTFILELIST", classheader+"base.h\n"+classheader+"base.cpp\nmoc_"+						 classheader+"base.cpp\nmoc_"+classheader+".cpp\n" );	} else if ( dialog.m_mdi ) {	    readme.Replace( "$QMSDEVUITYPE", "Multi Document interface (MDI)" );	    readme.Replace( "$QMSDEVFILELIST", "" );	    readme.Replace( "$QMSDEVQTFILELIST", "moc_"+classheader+".cpp\n" );	} else {	    readme.Replace( "$QMSDEVUITYPE", "Main Window interface" );	    readme.Replace( "$QMSDEVFILELIST", classheader+"base.ui\n\tA main window.\n"					       "\tUse the Qt GUI Designer change the layout.\n" );

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?