def registry(): reg = WidgetRegistry() reg.register_widget(WidgetDescription(**Number.get_widget_description())) reg.register_widget(WidgetDescription(**Adder.get_widget_description())) reg.register_widget( WidgetDescription(**AdderAync.get_widget_description())) reg.register_widget(WidgetDescription(**Show.get_widget_description())) return reg
def widget_desc_from_module(module): """ Get the widget description from a module. The module is inspected for classes that have a method `get_widget_description`. The function calls this method and expects a dictionary, which is used as keyword arguments for :obj:`WidgetDescription`. This method also converts all signal types into qualified names to prevent import problems when cached descriptions are unpickled (the relevant code using this lists should be able to handle missing types better). Parameters ---------- module (`module` or `str`): a module to inspect Returns ------- An instance of :obj:`WidgetDescription` """ if isinstance(module, str): module = __import__(module, fromlist=[""]) if module.__package__: package_name = module.__package__.rsplit(".", 1)[-1] else: package_name = None default_cat_name = package_name if package_name else "" for widget_class in module.__dict__.values(): if not hasattr(widget_class, "get_widget_description"): continue description = widget_class.get_widget_description() if description is None: continue description = WidgetDescription(**description) description.package = module.__package__ description.category = widget_class.category or default_cat_name return description raise discovery.WidgetSpecificationError
def setUp(self): super().setUp() reg = WidgetRegistry() reg.register_widget( WidgetDescription(**Widget.get_widget_description())) self.w = SchemeEditWidget() self.w.setRegistry(reg) self.w.resize(300, 300) self.w.setScheme(WidgetsScheme()) self.w.setDropHandlers([PluginDropHandler()])
def test(self): contents = (b'<html>\n' b' <header>\n' b' <meta charset=cp1252" />\n' b' </header>\n' b' <body><div id="widgets">\n' b' <ul>\n' b' <li><a href="a.html">aa</li>\n' b' </ul>\n' b' </div>\n' b'</html>') with temp_named_file(contents.decode("ascii"), ) as fname: url = QUrl.fromLocalFile(fname) p = HtmlIndexProvider(url) loop = get_event_loop() desc = WidgetDescription(name="aa", id="aa", qualified_name="aa") res = loop.run_until_complete(p.search_async(desc)) self.assertEqual(res, url.resolved(QUrl("a.html"))) self.assertEqual(p.items, {"aa": "a.html"})
def save_widget_icon( background, icon: str, outname: str, export_size=QSize(100, 100), format="png", ): # create fake windget and category descriptions widget_description = WidgetDescription("", "", icon=icon, qualified_name="orangecanvas") category = CategoryDescription(background=background) item = NodeItem(widget_description) item.setWidgetCategory(category) iconItem = item.icon_item shapeItem = item.shapeItem shapeSize = export_size iconSize = QSize(export_size.width() * 3 / 4, export_size.height() * 3 / 4) rect = QRectF(QPointF(-shapeSize.width() / 2, -shapeSize.height() / 2), QSizeF(shapeSize)) shapeItem.setShapeRect(rect) iconItem.setIconSize(iconSize) iconItem.setPos(-iconSize.width() / 2, -iconSize.height() / 2) image = QImage(export_size, QImage.Format_ARGB32) image.fill(QColor("#00FFFFFF")) painter = QPainter(image) painter.setRenderHint(QPainter.Antialiasing, 1) scene = QGraphicsScene() scene.addItem(shapeItem) scene.render(painter, QRectF(image.rect()), scene.sceneRect()) painter.end() if not image.save(outname, format, 80): print("Failed to save " + outname)
def widget_description(class_): # type: (Type[widget.OWBaseWidget]) -> WidgetDescription return WidgetDescription(**class_.get_widget_description())
def widget_desc_from_module(module): """ Get the widget description from a module. The module is inspected for a orangewidget.widgets.OWWidget instance and its class attributes (named as `WidgetDescription.__init__` parameters). Parameters ---------- module : `module` or str A module to inspect for widget description. Can be passed as a string (qualified import name). """ if isinstance(module, str): module = __import__(module, fromlist=[""]) module_name = module.__name__.rsplit(".", 1)[-1] if module.__package__: package_name = module.__package__.rsplit(".", 1)[-1] else: package_name = None default_cat_name = package_name if package_name else "" for widget_cls_name, widget_class in module.__dict__.items(): if (isinstance(widget_class, (_WidgetMetaClass, WidgetMetaClass)) and widget_class.name): break else: raise discovery.WidgetSpecificationError qualified_name = "%s.%s" % (module.__name__, widget_cls_name) inputs = [description.InputSignal(s.name, s.type, s.handler, s.flags, s.id, s.doc) for s in widget_class.inputs] outputs = [description.OutputSignal(s.name, s.type, s.flags, s.id, s.doc) for s in widget_class.outputs] # Convert all signal types into qualified names. # This is to prevent any possible import problems when cached # descriptions are unpickled (the relevant code using this lists # should be able to handle missing types better). for s in inputs + outputs: if isinstance(s.type, type): s.type = "%s.%s" % (s.type.__module__, s.type.__name__) return WidgetDescription( name=widget_class.name, id=widget_class.id or module_name, category=widget_class.category or default_cat_name, version=widget_class.version, description=widget_class.description, long_description=widget_class.long_description, qualified_name=qualified_name, package=module.__package__, inputs=inputs, outputs=outputs, author=widget_class.author, author_email=widget_class.author_email, maintainer=widget_class.maintainer, maintainer_email=widget_class.maintainer_email, help=widget_class.help, help_ref=widget_class.help_ref, url=widget_class.url, keywords=widget_class.keywords, priority=widget_class.priority, icon=widget_class.icon, background=widget_class.background, replaces=widget_class.replaces)