def __init__(self, qt_uifile): super(NUIC, self).__init__() self.parser = UIParser(qt_uifile) self.dirs = None
class NUIC(object): """docstring for NUIC""" def __init__(self, qt_uifile): super(NUIC, self).__init__() self.parser = UIParser(qt_uifile) self.dirs = None def pageCode(self, page, bitmaps, fonts, verbose): """ Generate code (page struct, events functions, page draw func, etc.) for gived page. """ if verbose: inform('generate page code for [ %s ] ' % page['name']) return NCodeGen.generetePageCode(page, bitmaps, fonts) def pageHeaderCode(self, page, verbose): """ Generate page header code. """ if verbose: inform('generate page [ %s ] headers code' % page['name']) return NCodeGen.pagesHeader(page) def objectsHeaderCode(self, page, bitmaps, fonts, verbose): """ Generate objects(Labels, Bitmaps, Buttons, etc.) header code for gived page. """ if verbose: inform('generate object headers code for page [ %s ]' % page['name']) return NCodeGen.generateObjectsHeader(page, bitmaps, fonts) def convertFonts(self, fonts, verbose): """ converting all parset fonts to NGL_Font objects """ return [self._convertFont(font, verbose) for font in fonts] def _convertFont(self, font, verbose): """ Convert parsed QFont to NGL_Font object """ # charters set to converting char_range = range(ord(' '), ord('~')) chars_sets = ''.join([chr(x) for x in char_range]) if verbose: inform( 'converting font %s, %spt, bold %s' % (font.family(), font.pointSize(), font.bold())) name = NGL_Font.formatName(font.family(), font.pointSize(), font.bold()) # convert and return return NFontConverter.convertQFont(chars_sets, name, font) def fontsHeaderCode(self, fonts, verbose): if verbose: inform( 'generate fonts header file...' ) return NFontCodeGen.generateFontsHeader( fonts ) def convertBitmaps(self, bitmaps, compress, backcolor, verbose): """ Converting all parsed bitmaps to NGL_Bitmap objects """ ngl_bitmaps = [] for bmp in bitmaps: b = self._convertBitmap(bmp, bitmaps[bmp], compress, backcolor, verbose) ngl_bitmaps.append(b) return ngl_bitmaps def _convertBitmap(self, path, objects, compress, backcolor, verbose): """ Convert parsed bitmap to NGL_Bitmap object path - path for input bitmap compress - type of compressing - 'None', 'RLE', 'JPG', 'Auto' backcolor - background color for transparent input bitmap verbose - increase output verbosity flag """ if os.path.exists(path): image = QImage(path) name = os.path.basename(path).split('.')[0] ngl_bitmap = NBitmapsConverter.convertQImage(image, name, 'format16', compress, backcolor) ngl_bitmap.objects = objects if verbose: inform(('converting bitmap {name}, size {width}x{height}, ' 'compress {compress}, data len {size} bytes' ).format(name = name, width = image.size().width(), height = image.size().height(), compress = ngl_bitmap.compressed, size = ngl_bitmap.data_len_in_bytes)) return ngl_bitmap else: error(('File "{0}" not found! Expected path - "{1}" not exist' ' :( :( :( ').format(bitmap['name'], bitmap['path'])) def bitmapsHeaderCode(self, bitmaps, verbose ): if verbose: inform('generate bitmaps header file...') return NBitmapCodeGen.generateBitmapsHeader(bitmaps) def informUser(self, state, verbose): if state == 'parse_end': if verbose: inform('ui file version - %s' % self.parser.uiVersion()) inform('page - "%s"' % self.parser.parsedPage()['name']) objects_classes = self.parser.getParsedObjects() objects = [] for class_key in objects_classes.keys(): # iterate objects in this objects class for obj_key in objects_classes[class_key]: objects.append(obj_key) inform( 'objects - %s' % ', '.join(objects) ) bitmaps, fonts = self.parser.getParsedResourses() bitmaps_paths = [os.path.basename(bmp) for bmp in bitmaps] fonts_familys = [font.family() for font in fonts] inform( 'bitmaps files - %s' % bitmaps_paths ) inform( 'fonts - %s' % fonts_familys ) inform( '--- Qt UI file parsed successful, start convert and generate code...' ) elif state == 'convert_end': if verbose: pass inform('--- Converting and generate successful, create dirs...') elif state == 'create_dirs': if verbose: for _dir in self.dirs.keys(): path = os.path.abspath( self.dirs[ _dir ] ) inform( 'created [ %s ] dir - %s' % (_dir, path) ) inform('--- Creating dirs done, save...') if verbose: newline() def createDirs(self, **kwargs): """ Create dirs for out page, objects and resourses code """ dirs = {} basepath = kwargs[ 'basepath' ] dirs['base'] = basepath dirs['pages'] = os.path.join( basepath, 'pages/' ) dirs['page'] = os.path.join( basepath, 'pages/' + kwargs[ 'pagename' ] ) dirs['bitmaps'] = os.path.join( basepath, 'bitmaps/' ) dirs['fonts'] = os.path.join( basepath, 'fonts/' ) # create dirs for _dir in dirs.keys(): os.makedirs( dirs[_dir], mode=0x777, exist_ok=True ) self.dirs = dirs return self.dirs def save(self, **kwargs): """ Save all generated code """ # page self.saveCode( dircode = 'page', name = kwargs['pagename'] + '.c', code = kwargs['pagecode'], verbose = kwargs['verbose'] ) # page header if 'pageheadercode' in kwargs: self.saveCode(dircode = 'pages', name = 'pages.h', code = kwargs['pageheadercode'], verbose = kwargs['verbose']) # common object headers if 'headerscode' in kwargs: for key in kwargs['headerscode']: nm = NCodeService.pageObjectsName(kwargs['pagename'], key) self.saveCode(dircode = 'page', name = '%s.h' % nm, code = kwargs['headerscode'][key], verbose = kwargs['verbose']) # Resource headers ---------------------------------------------------- # common fonts header self.saveCode(dircode = 'fonts', name = 'fonts.h', code = kwargs['fontsheader'], verbose = kwargs['verbose']) # common bitmaps header self.saveCode(dircode ='bitmaps', name = 'bitmaps.h', code = kwargs['bitmapsheader'], verbose = kwargs['verbose']) # all fonts resourses self.saveResources(kwargs['fonts'], 'fonts', kwargs['verbose']) # all bitmaps resourses self.saveResources(kwargs['bitmaps'], 'bitmaps', kwargs['verbose']) def saveCode(self, **kwargs): # page = self.parser.parsedPage() _dir = self.dirs[ kwargs['dircode'] ] _basename = ( kwargs['name'] ).lower() _code = kwargs['code'] _file = os.path.abspath( os.path.join( _dir , _basename ) ) self._write( _file, 'w', _code ) if kwargs['verbose']: inform( 'saved [ %s ], path -- %s' % ( _basename, _file ) ) def saveResources(self, objects, dir_name, verbose): for obj in objects: _file = os.path.abspath( os.path.join( self.dirs[dir_name], obj.name + '.c' ) ) self._write( _file, 'w', obj.code ) if verbose: inform( 'saved [ %s ], path -- %s' % (obj.name, _file) ) def _write(self, sfile, mode, data): with open(sfile, mode) as f: f.write(data)
class NUIC(object): """docstring for NUIC""" def __init__(self, qt_uifile): super(NUIC, self).__init__() self.parser = UIParser(qt_uifile) self.dirs = None def pageCode(self, page, bitmaps, fonts, verbose): """ Generate code (page struct, events functions, page draw func, etc.) for gived page. """ if verbose: inform("generate page code for [ %s ] " % page["name"]) return NCodeGen.generetePageCode(page, bitmaps, fonts) def pageHeaderCode(self, page, verbose): """ Generate page header code. """ if verbose: inform("generate page [ %s ] headers code" % page["name"]) return NCodeGen.pagesHeader(page) def objectsHeaderCode(self, page, bitmaps, fonts, verbose): """ Generate objects(Labels, Bitmaps, Buttons, etc.) header code for gived page. """ if verbose: inform("generate object headers code for page [ %s ]" % page["name"]) return NCodeGen.generateObjectsHeader(page, bitmaps, fonts) def convertFonts(self, fonts, verbose): """ converting all parset fonts to NGL_Font objects """ return [self._convertFont(font, verbose) for font in fonts] def _convertFont(self, font, verbose): """ Convert parsed QFont to NGL_Font object """ # charters set to converting char_range = range(ord(" "), ord("~")) chars_sets = "".join([chr(x) for x in char_range]) if verbose: inform("converting font %s, %spt, bold %s" % (font.family(), font.pointSize(), font.bold())) name = NGL_Font.formatName(font.family(), font.pointSize(), font.bold()) # convert and return return NFontConverter.convertQFont(chars_sets, name, font) def fontsHeaderCode(self, fonts, verbose): if verbose: inform("generate fonts header file...") return NFontCodeGen.generateFontsHeader(fonts) def convertBitmaps(self, bitmaps, compress, backcolor, verbose): """ Converting all parsed bitmaps to NGL_Bitmap objects """ ngl_bitmaps = [] for bmp in bitmaps: b = self._convertBitmap(bmp, bitmaps[bmp], compress, backcolor, verbose) ngl_bitmaps.append(b) return ngl_bitmaps def _convertBitmap(self, path, objects, compress, backcolor, verbose): """ Convert parsed bitmap to NGL_Bitmap object path - path for input bitmap compress - type of compressing - 'None', 'RLE', 'JPG', 'Auto' backcolor - background color for transparent input bitmap verbose - increase output verbosity flag """ if os.path.exists(path): image = QImage(path) name = os.path.basename(path).split(".")[0] ngl_bitmap = NBitmapsConverter.convertQImage(image, name, "format16", compress, backcolor) ngl_bitmap.objects = objects if verbose: inform( ( "converting bitmap {name}, size {width}x{height}, " "compress {compress}, data len {size} bytes" ).format( name=name, width=image.size().width(), height=image.size().height(), compress=ngl_bitmap.compressed, size=ngl_bitmap.data_len_in_bytes, ) ) return ngl_bitmap else: error( ('File "{0}" not found! Expected path - "{1}" not exist' " :( :( :( ").format( bitmap["name"], bitmap["path"] ) ) def bitmapsHeaderCode(self, bitmaps, verbose): if verbose: inform("generate bitmaps header file...") return NBitmapCodeGen.generateBitmapsHeader(bitmaps) def informUser(self, state, verbose): if state == "parse_end": if verbose: inform("ui file version - %s" % self.parser.uiVersion()) inform('page - "%s"' % self.parser.parsedPage()["name"]) objects_classes = self.parser.getParsedObjects() objects = [] for class_key in objects_classes.keys(): # iterate objects in this objects class for obj_key in objects_classes[class_key]: objects.append(obj_key) inform("objects - %s" % ", ".join(objects)) bitmaps, fonts = self.parser.getParsedResourses() bitmaps_paths = [os.path.basename(bmp) for bmp in bitmaps] fonts_familys = [font.family() for font in fonts] inform("bitmaps files - %s" % bitmaps_paths) inform("fonts - %s" % fonts_familys) inform("--- Qt UI file parsed successful, start convert and generate code...") elif state == "convert_end": if verbose: pass inform("--- Converting and generate successful, create dirs...") elif state == "create_dirs": if verbose: for _dir in self.dirs.keys(): path = os.path.abspath(self.dirs[_dir]) inform("created [ %s ] dir - %s" % (_dir, path)) inform("--- Creating dirs done, save...") if verbose: newline() def createDirs(self, **kwargs): """ Create dirs for out page, objects and resourses code """ dirs = {} basepath = kwargs["basepath"] dirs["base"] = basepath dirs["pages"] = os.path.join(basepath, "pages\\") dirs["page"] = os.path.join(basepath, "pages\\" + kwargs["pagename"]) dirs["bitmaps"] = os.path.join(basepath, "bitmaps\\") dirs["fonts"] = os.path.join(basepath, "fonts\\") # create dirs for _dir in dirs.keys(): os.makedirs(dirs[_dir], mode=0x777, exist_ok=True) self.dirs = dirs return self.dirs def save(self, **kwargs): """ Save all generated code """ # page self.saveCode( dircode="page", name=kwargs["pagename"] + ".c", code=kwargs["pagecode"], verbose=kwargs["verbose"] ) # page header if "pageheadercode" in kwargs: self.saveCode(dircode="pages", name="pages.h", code=kwargs["pageheadercode"], verbose=kwargs["verbose"]) # common object headers if "headerscode" in kwargs: for key in kwargs["headerscode"]: nm = NCodeService.pageObjectsName(kwargs["pagename"], key) self.saveCode( dircode="page", name="%s.h" % nm, code=kwargs["headerscode"][key], verbose=kwargs["verbose"] ) # Resource headers ---------------------------------------------------- # common fonts header self.saveCode(dircode="fonts", name="fonts.h", code=kwargs["fontsheader"], verbose=kwargs["verbose"]) # common bitmaps header self.saveCode(dircode="bitmaps", name="bitmaps.h", code=kwargs["bitmapsheader"], verbose=kwargs["verbose"]) # all fonts resourses self.saveResources(kwargs["fonts"], "fonts", kwargs["verbose"]) # all bitmaps resourses self.saveResources(kwargs["bitmaps"], "bitmaps", kwargs["verbose"]) def saveCode(self, **kwargs): # page = self.parser.parsedPage() _dir = self.dirs[kwargs["dircode"]] _basename = (kwargs["name"]).lower() _code = kwargs["code"] _file = os.path.abspath(os.path.join(_dir, _basename)) self._write(_file, "w", _code) if kwargs["verbose"]: inform("saved [ %s ], path -- %s" % (_basename, _file)) def saveResources(self, objects, dir_name, verbose): for obj in objects: _file = os.path.abspath(os.path.join(self.dirs[dir_name], obj.name + ".c")) self._write(_file, "w", obj.code) if verbose: inform("saved [ %s ], path -- %s" % (obj.name, _file)) def _write(self, sfile, mode, data): with open(sfile, mode) as f: f.write(data)