Example #1
0
 def setUp(self):
     session = Application.new_session(services=[
         "event_manager",
         "component_registry",
         "element_factory",
         "main_window",
         "properties",
         "namespace",
         "diagrams",
         "toolbox",
         "elementeditor",
         "export_menu",
         "tools_menu",
     ])
     main_window = session.get_service("main_window")
     main_window.open()
     self.element_factory = session.get_service("element_factory")
     self.diagram = self.element_factory.create(UML.Diagram)
     self.page = DiagramPage(
         self.diagram,
         session.get_service("event_manager"),
         self.element_factory,
         session.get_service("properties"),
     )
     self.page.construct()
     assert self.page.diagram == self.diagram
     assert self.page.view.canvas == self.diagram.canvas
     assert len(self.element_factory.lselect()) == 1
Example #2
0
    def new_session(app):
        session = Application.new_session()
        subscribe_to_lifecycle_events(session, gtk_app)

        main_window = session.get_service("main_window")
        main_window.open(app)

        return session
Example #3
0
 def setUp(self):
     self.session = Application.new_session(services=self.services)
     self.element_factory = self.session.get_service("element_factory")
     assert len(list(self.element_factory.select())) == 0, list(
         self.element_factory.select())
     self.diagram = self.element_factory.create(UML.Diagram)
     assert len(list(self.element_factory.select())) == 1, list(
         self.element_factory.select())
Example #4
0
def element_factory():
    session = Application.new_session(
        services=["event_manager", "component_registry", "element_factory"])
    event_manager = session.get_service("event_manager")
    event_manager.subscribe(handler)
    clear_events()
    factory = session.get_service("element_factory")
    yield factory
    del factory
    clear_events()
    session.shutdown()
Example #5
0
def test_service_load():
    """Test loading services and querying utilities."""

    application = Application()
    session = application.new_session()

    assert (session.get_service("undo_manager")
            is not None), "Failed to load the undo manager service"

    assert (session.get_service("file_manager")
            is not None), "Failed to load the file manager service"

    application.shutdown()
Example #6
0
def session():
    session = Application.new_session(services=[
        "event_manager",
        "component_registry",
        "element_factory",
        "properties",
        "main_window",
        "namespace",
        "diagrams",
        "toolbox",
        "elementeditor",
        "export_menu",
        "tools_menu",
    ])
    yield session
    session.shutdown()
Example #7
0
    def setUp(self):
        self.session = Application.new_session(services=[
            "event_manager",
            "component_registry",
            "element_factory",
            "main_window",
            "properties_manager",
            "properties",
            "namespace",
            "diagrams",
            "toolbox",
            "elementeditor",
            "export_menu",
            "tools_menu",
        ])
        self.component_registry = self.session.get_service(
            "component_registry")
        self.event_manager = self.session.get_service("event_manager")

        self.main_window = self.session.get_service("main_window")
        self.main_window.open()
Example #8
0
def session():
    session = Application.new_session()
    yield session
Example #9
0
def session():
    session = Application.new_session(
        services=["event_manager", "component_registry", "element_factory"])
    yield session
    session.shutdown()
Example #10
0
    dest="attrs",
    action="store_true",
    help="Print class attributes",
)

(options, args) = parser.parse_args()

if len(args) != 1:
    parser.print_help()
    sys.exit(1)

# The model file to load.
model = args[0]

# Create the Gaphor application object.
session = Application.new_session()

# Get services we need.
element_factory = session.get_service("element_factory")
file_manager = session.get_service("file_manager")

# Load model from file.
file_manager.load(model)

# Find all classes using factory select.
for cls in element_factory.select(lambda e: e.isKindOf(UML.Class)):

    print(f"Found class {cls.name}")

    if options.attrs:
Example #11
0
def main(argv=sys.argv[1:]):
    def message(msg):
        """
        Print message if user set verbose mode.
        """
        if options.verbose:
            print(msg, file=sys.stderr)

    usage = "usage: %prog [options] file1 file2..."

    parser = optparse.OptionParser(usage=usage)

    parser.add_option("-v",
                      "--verbose",
                      dest="verbose",
                      action="store_true",
                      help="verbose output")
    parser.add_option(
        "-u",
        "--use-underscores",
        dest="underscores",
        action="store_true",
        help="use underscores instead of spaces for output filenames",
    )
    parser.add_option("-d",
                      "--dir",
                      dest="dir",
                      metavar="directory",
                      help="output to directory")
    parser.add_option(
        "-f",
        "--format",
        dest="format",
        metavar="format",
        help="output file format, default pdf",
        default="pdf",
        choices=["pdf", "svg", "png"],
    )
    parser.add_option(
        "-r",
        "--regex",
        dest="regex",
        metavar="regex",
        help="process diagrams which name matches given regular expresion;"
        " name includes package name; regular expressions are case insensitive",
    )

    (options, args) = parser.parse_args(argv)

    if not args:
        parser.print_help()

    session = Application.new_session(
        services=["event_manager", "component_registry", "element_factory"])
    factory = session.get_service("element_factory")

    name_re = None
    if options.regex:
        name_re = re.compile(options.regex, re.I)

    # we should have some gaphor files to be processed at this point
    for model in args:
        message(f"loading model {model}")
        storage.load(model, factory)
        message("ready for rendering")

        for diagram in factory.select(lambda e: e.isKindOf(UML.Diagram)):
            odir = pkg2dir(diagram.package)

            # just diagram name
            dname = diagram.name
            # full diagram name including package path
            pname = f"{odir}/{dname}"

            if options.underscores:
                odir = odir.replace(" ", "_")
                dname = dname.replace(" ", "_")

            if name_re and not name_re.search(pname):
                message(f"skipping {pname}")
                continue

            if options.dir:
                odir = f"{options.dir}/{odir}"

            outfilename = f"{odir}/{dname}.{options.format}"

            if not os.path.exists(odir):
                message(f"creating dir {odir}")
                os.makedirs(odir)

            message(f"rendering: {pname} -> {outfilename}...")

            view = View(diagram.canvas)
            view.painter = ItemPainter()

            tmpsurface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 0, 0)
            tmpcr = cairo.Context(tmpsurface)
            view.update_bounding_box(tmpcr)
            tmpcr.show_page()
            tmpsurface.flush()

            w, h = view.bounding_box.width, view.bounding_box.height
            if options.format == "pdf":
                surface = cairo.PDFSurface(outfilename, w, h)
            elif options.format == "svg":
                surface = cairo.SVGSurface(outfilename, w, h)
            elif options.format == "png":
                surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(w + 1),
                                             int(h + 1))
            else:
                assert False, f"unknown format {options.format}"
            cr = cairo.Context(surface)
            view.matrix.translate(-view.bounding_box.x, -view.bounding_box.y)
            paint(view, cr)
            cr.show_page()

            if options.format == "png":
                surface.write_to_png(outfilename)

            surface.flush()
            surface.finish()