📄 rc2ui.cpp
字号:
#include "rc2ui.h"#include <qdir.h>/// some little helpers ///const QString blockStart1 = "/////////////////////////////////////////////////////////////////////////////";const QString blockStart2 = "//";void RC2UI::wi(){ for ( int i = 0; i < indentation; i++ ) *out << " ";}void RC2UI::indent(){ indentation++;}void RC2UI::undent(){ indentation--;}QString RC2UI::stripQM( const QString& string ){ return string.mid( 1, string.length()-2 );}QStringList RC2UI::splitStyles( const QString& styles, char sep ){ QString s = styles; QString style; QStringList l; while ( s.find( sep ) > -1 ) { style = s.left( s.find( sep ) ); l << style.stripWhiteSpace(); s = s.right( s.length() - style.length() -1 ); } if ( !s.isEmpty() ) l << s.stripWhiteSpace(); return l;}QString RC2UI::parseNext( QString& arg, char sep ){ QString next = arg.left( arg.find(sep) ); arg = arg.right( arg.length() - next.length() - 1 ); return next;}void RC2UI::writeClass( const QString& name ){ wi(); *out << "<class>" << name << "</class>" << endl;}void RC2UI::writeCString( const QString& name, const QString& value ){ wi(); *out << "<property>" << endl; indent(); wi(); *out << "<name>" << name << "</name>" << endl; wi(); *out << "<cstring>" << value << "</cstring>" << endl; undent(); wi(); *out << "</property>" << endl;}void RC2UI::writeString( const QString& name, const QString& value ){ wi(); *out << "<property>" << endl; indent(); wi(); *out << "<name>" << name << "</name>" << endl; wi(); *out << "<string>" << value << "</string>" << endl; undent(); wi(); *out << "</property>" << endl;}void RC2UI::writeRect( const QString& name, int x, int y, int w, int h ){ wi(); *out << "<property>" << endl; indent(); wi(); *out << "<name>" << name << "</name>" << endl; wi(); *out << "<rect>" << endl; indent(); wi(); *out << "<x>" << int(double(x)*1.5) << "</x>" << endl; wi(); *out << "<y>" << int(double(y)*1.65) << "</y>" << endl; wi(); *out << "<width>" << int(double(w)*1.5) << "</width>" << endl; wi(); *out << "<height>" << int(double(h)*1.65) << "</height>" << endl; undent(); wi(); *out << "</rect>" << endl; undent(); wi(); *out << "</property>" << endl;}void RC2UI::writeFont( const QString& family, int pointsize ){ wi(); *out << "<property>" << endl; indent(); wi(); *out << "<name>font</name>" << endl; wi(); *out << "<font>" << endl; indent(); wi(); *out << "<family>" << family << "</family>" << endl; wi(); *out << "<pointsize>" << pointsize << "</pointsize>" << endl; undent(); wi(); *out << "</font>" << endl; undent(); wi(); *out << "</property>" << endl;}void RC2UI::writeBool( const QString& name, bool value ){ wi(); *out << "<property>" << endl; indent(); wi(); *out << "<name>" << name << "</name>" << endl; wi(); *out << "<bool>" << (value ? "true" : "false") << "</bool>" << endl; undent(); wi(); *out << "</property>" << endl;}void RC2UI::writeNumber( const QString& name, int value ){ wi(); *out << "<property>" << endl; indent(); wi(); *out << "<name>" << name << "</name>" << endl; wi(); *out << "<number>" << value << "</number>" << endl; undent(); wi(); *out << "</property>" << endl;}void RC2UI::writeEnum( const QString& name, const QString& value ){ wi(); *out << "<property>" << endl; indent(); wi(); *out << "<name>" << name << "</name>" << endl; wi(); *out << "<enum>" << value << "</enum>" << endl; undent(); wi(); *out << "</property>" << endl;}void RC2UI::writeSet( const QString& name, const QString& value ){ wi(); *out << "<property>" << endl; indent(); wi(); *out << "<name>" << name << "</name>" << endl; wi(); *out << "<set>" << value << "</set>" << endl; undent(); wi(); *out << "</property>" << endl;}void RC2UI::writeStyles( const QStringList styles, bool isFrame ){ if ( isFrame ) { bool defineFrame = FALSE; QString shadow = "NoFrame"; QString shape = "StyledPanel"; int width = 2; if ( styles.contains( "WS_EX_STATICEDGE" ) ) { shadow = "Plain"; width = 1; defineFrame = TRUE; } if ( styles.contains( "WS_EX_CLIENTEDGE" ) ) { shadow = "Sunken"; defineFrame = TRUE; } if ( styles.contains( "WS_EX_DLGMODALFRAME" ) ) { shadow = "Raised"; defineFrame = TRUE; } if ( !styles.contains( "WS_BORDER" ) ) { shape = "NoFrame"; defineFrame = TRUE; } if ( defineFrame ) { writeEnum( "frameShape", "StyledPanel" ); writeEnum( "frameShadow", shadow ); writeNumber( "lineWidth", width ); } } if ( styles.contains("WS_DISABLED") ) writeBool("enabled", FALSE ); if ( styles.contains("WS_EX_ACCEPTFILES") ) writeBool("acceptDrops", TRUE ); if ( styles.contains("WS_EX_TRANSPARENT") ) writeBool("autoMask", TRUE ); if ( !styles.contains("WS_TABSTOP") ) writeEnum("focusPolicy", "NoFocus");}/*! Constructs a RC2UI object*/RC2UI::RC2UI( QTextStream* input ){ writeToFile = TRUE; in = input; indentation = 0; out = 0;}/*! Destructs the RC2UI object*/RC2UI::~RC2UI(){}/*! Parses the input stream and writes the output to files.*/bool RC2UI::parse(){ while ( !in->eof() ) { while ( line != blockStart1 && !in->eof() ) line = in->readLine(); if ( in->eof() ) return FALSE; while ( line != blockStart2 && !in->eof() ) line = in->readLine(); if ( in->eof() ) return FALSE; line = in->readLine(); if ( line.left(3) == "// " && !in->eof() ) { QString type = line.right( line.length() - 3 ); if ( in->readLine() == "//" && in->readLine().isEmpty() && !in->eof() ) { if ( type == "Dialog" ) { if ( !makeDialog() ) return FALSE; } /* else if ( type == "Bitmap" ) { if ( !makeBitmap() ) return FALSE; } else if ( type == "String Table" ) { if ( !makeStringTable() ) return FALSE; } else if ( type == "Accelerator" ) { if ( !makeAccelerator() ) return FALSE; } else if ( type == "Cursor" ) { if ( !makeCursor() ) return FALSE; } else if ( type == "HTML" ) { if ( !makeHTML() ) return FALSE; } else if ( type == "Icon" ) { if ( !makeIcon() ) return FALSE; } else if ( type == "Version" ) { if ( !makeVersion() ) return FALSE; }*/ } } else return FALSE; } return TRUE;}/*! Parses the input stream and writes the output in \a get.*/bool RC2UI::parse( QStringList& get ){ writeToFile = FALSE; bool result = parse(); get = target; return result;}/*! Retrieves a unique name starting with \a start*/QString RC2UI::useName( const QString& start ){ QString name = start; int id = 1; while ( usedNames.contains( name ) ) { name = start + QString( "%1" ).arg( id ); id++; } usedNames.append(name); return name;}/*! Builds a number of UI dialog out of the current input stream*/bool RC2UI::makeDialog(){ line = in->readLine(); do { QFile fileOut; QString buffer; int count; QCString className; uint x, y, w, h; uint endDesc; bool space = FALSE; for ( endDesc = 0; endDesc < line.length() ; endDesc++ ) { char c = (QChar)line.at(endDesc); if ( space && (c >= '0') && (c <= '9') ) break; space = c==' '; } QString desc = line.left(endDesc-1); line = line.right( line.length() - endDesc ); className = parseNext( desc, ' ' ); count = sscanf( line, "%d, %d, %d, %d", &x, &y, &w, &h ); if ( !count && count == EOF ) return FALSE; char property[256]; QStringList styles; QStringList extendedStyles; QString caption = ""; QString baseClass = ""; QString widgetType; QString widgetName; QString arguments; int pointsize; QString fontname; do { if ( in->eof() ) return TRUE; line = ""; do { line += in->readLine(); } while ( line[(int)line.length()-1] == '|' || line[(int)line.length()-1] == ',' ); count = sscanf( line, "%s", property ); line = line.right( line.length() - line.find(" ") -1 ); if ( QString(property) == "STYLE" ) { styles = splitStyles(line); if ( styles.contains( "WS_CAPTION" ) ) baseClass = "QDialog"; else baseClass = "QWidget"; } else if ( QString(property) == "CAPTION" ) { caption = stripQM( line ); } else if ( QString(property) == "FONT" ) { QString pt = line.left( line.find(",") ); pointsize = pt.toInt(); fontname = stripQM(line.right( line.length() - line.find(",") - 2 )); } } while ( line != "BEGIN" ); if ( writeToFile ) { QString outputFile = QString(className) + ".ui"; fileOut.setName( outputFile ); if (!fileOut.open( IO_WriteOnly ) ) qFatal( "rc2ui: Could not open output file '%s'", outputFile.latin1() ); out = new QTextStream( &fileOut ); } else { out = new QTextStream( &buffer, IO_WriteOnly ); } *out << "<!DOCTYPE UI><UI>" << endl; writeClass( className ); wi(); *out << "<widget>"<< endl; indent(); writeClass( baseClass ); writeCString( "name", className ); writeRect( "geometry", x, y, w, h ); writeString( "caption", caption ); writeFont( fontname, pointsize ); do { line = in->readLine().stripWhiteSpace(); if ( line == "END" ) continue; widgetType = parseNext(line, ' '); arguments = line.stripWhiteSpace(); while ( arguments[(int)arguments.length()-1] == ',' || arguments[(int)arguments.length()-1] == '|' ) arguments += " "+in->readLine().stripWhiteSpace(); wi(); *out << "<widget>" << endl; indent(); WidgetType ID = IDUnknown; QString controlType; QString widgetID; QString widgetText; bool hasText = FALSE; bool isControl = FALSE; bool isFrame = FALSE; if ( widgetType == "PUSHBUTTON" ) { ID = IDPushButton; hasText = TRUE; } else if ( widgetType == "DEFPUSHBUTTON" ) { ID = IDPushButton; hasText = TRUE; } else if ( widgetType == "LTEXT" ) { ID = IDLabel; hasText = TRUE; } else if ( widgetType == "CTEXT" ) { ID = IDLabel; hasText = TRUE; } else if ( widgetType == "RTEXT" ) { ID = IDLabel; hasText = TRUE; } else if ( widgetType == "EDITTEXT" ) { ID = IDLineEdit; } else if ( widgetType == "GROUPBOX" ) { ID = IDGroupBox; hasText = TRUE; } else if ( widgetType == "COMBOBOX" ) { ID = IDComboBox; } else if ( widgetType == "LISTBOX" ) { ID = IDListBox; } else if ( widgetType == "SCROLLBAR" ) { ID = IDScrollBar; } else if ( widgetType == "CHECKBOX" ) { ID = IDCheckBox; hasText = TRUE; } else if ( widgetType == "RADIOBUTTON" ) { ID = IDRadioButton; hasText = TRUE; } else if ( widgetType == "CONTROL" ) { isControl = TRUE; widgetText = stripQM(parseNext( arguments )); widgetID = parseNext( arguments ); controlType = stripQM(parseNext( arguments )); styles = splitStyles(parseNext( arguments )); if ( controlType == "Static" ) { ID = IDLabel; } else if ( controlType == "Button" ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -