Beispiel #1
0
def saveImage(multi=False):
    """Save an image to file.

    This will show the Save Image dialog, with the multisave mode checked if
    multi = True. Then, depending on the user's selection, it will either:
     - save the current Canvas/Window to file
     - start the multisave/autosave mode
     - do nothing
    """
    dia = widgets.SaveImageDialog(pf.cfg['workdir'], ['img', 'icon', 'all'], multi=multi)
    opt = dia.getResults()
    if opt:
        if opt.fm == 'From Extension':
            if utils.fileTypeFromExt(opt.fn) == '':
                opt.fn += '.png'
            opt.fm = None
        if opt.qu < 0:
            opt.qu = -1
        updateSettings({'workdir':os.path.dirname(opt.fn)}, save=True)
        image.save(filename=opt.fn,
                   format=opt.fm,
                   quality=opt.qu,
                   size=opt.sz,
                   window=opt.wi,
                   multi=opt.mu,
                   hotkey=opt.hk,
                   autosave=opt.au,
                   border=opt.bo,
                   grab=opt.gr,
                   alpha=opt.al
                   )
Beispiel #2
0
def closeProject(save=None,clear=None):
    """Close the current project, saving it or not.

    Parameters:

    - `save`: None, True or False. Determines whether the project should be
      saved prior to closing it. If None, it will be asked from the user.
      Note that this parameter is only used for named Projects. Temporary
      Projects are never saved implicitely.
    - `clear`: None, True or False.
    """
    if pf.PF.filename is not None:
        if save is None:
            save = draw.ack("Save the current project before closing it?")
        print("Closing project %s (save=%s)" % (pf.PF.filename, save))
        if save:
            saveProject()
            if pf.PF:
                listProject()
                if clear is None:
                    clear = draw.ask("What shall I do with the existing globals?", ["Delete", "Keep"]) == "Delete"

    if clear:
        pf.PF.clear()

    pf.PF.filename = None
    pf.GUI.setcurproj('None')
    updateSettings({
        'curproj': pf.PF.filename,
        }, save=True)
Beispiel #3
0
def openScript(fn=None,exist=True,create=False):
    """Open a pyFormex script and set it as the current script.

    If no filename is specified, a file selection dialog is started to select
    an existing script, or allow to create a new file if exist is False.

    If the file exists and is a pyFormex script, it is set ready to execute.

    If create is True, a default pyFormex script template will be written
    to the file, overwriting the contents if the file existed. Then, the
    script is loaded into the editor.

    We encourage the use of createScript() to create new scripts and leave
    openScript() to open existing scripts.
    """
    if fn is None:
        cur = pf.cfg['curfile']
        if cur is None:
            cur = pf.cfg['workdir']
        if cur is None:
            cur  = '.'
        fn = widgets.FileDialog(cur, 'pyformex', exist=exist).getFilename()
    if fn:
        if create:
            if not exist and os.path.exists(fn) and not draw.ack("The file %s already exists.\n Are you sure you want to overwrite it?" % fn):
                return None
            template = pf.cfg['scripttemplate']
            if (os.path.exists(template)):
                shutil.copyfile(template, fn)
        updateSettings({'workdir':os.path.dirname(fn)}, save=True)
        pf.GUI.setcurfile(fn)
        pf.GUI.scripthistory.add(fn)
        if create:
            draw.editFile(fn)
    return fn
Beispiel #4
0
 def accept(save=False):
     dia.acceptData()
     res = dia.results
     res['_save_'] = save
     if res['_addhost_']:
         hosts = pf.cfg['jobs/hosts']
         if res['_addhost_'] not in hosts:
             hosts.append(res['_addhost_'])
         res['jobs/hosts'] = sorted(hosts)
         res['jobs/host'] = res['_addhost_']
     pf.debug(res)
     updateSettings(res)
Beispiel #5
0
def saveAsProject():
    proj = openProject(pf.PF.filename, exist=False, access=['w'], default='w')
    if proj is not None: # even if empty
        pf.PF.filename = proj.filename
        pf.PF.gzip = proj.gzip
        pf.PF.signature = proj.signature  # put new signature inside
        saveProject()
    if pf.PF.filename is not None:
        updateSettings({
            'curproj': pf.PF.filename,
            'workdir': os.path.dirname(pf.PF.filename),
            }, save=True)
    pf.GUI.setcurproj(pf.PF.filename)
Beispiel #6
0
 def accept(save=False):
     dia.acceptData()
     res = dia.results
     vp = getVp(res['viewport'])
     pf.debug(
         "Changing Canvas settings for viewport %s to:\n%s" %
         (pf.GUI.viewports.viewIndex(vp), res), pf.DEBUG.CANVAS)
     pf.canvas.settings.update(res, strict=False)
     pf.canvas.redrawAll()
     pf.canvas.update()
     if save:
         res = utils.prefixDict(res, 'canvas/')
         print(res)
         res['_save_'] = save
         del res['canvas/viewport']
         prefMenu.updateSettings(res)
Beispiel #7
0
def setBackground(mode, color1, color2, color3, color4, showimage, image,
                  _save_):
    if mode == 'solid':
        color = color1
    elif mode == 'vertical':
        color = [color1, color1, color4, color4]
    elif mode == 'horizontal':
        color = [color1, color2, color2, color1]
    else:
        color = [color1, color2, color3, color4]
    if not showimage:
        image = ''
    pf.canvas.setBackground(color=color, image=image)
    pf.canvas.update()
    if _save_:
        prefMenu.updateSettings({
            'canvas/bgmode': mode,
            'canvas/bgcolor': color,
            'canvas/bgimage': image,
            '_save_': _save_
        })
Beispiel #8
0
def setProject(proj):
    """Make the specified project the current project.

    proj is an open project.
    If a filename, the project file is opened.

    .. note: The remainder is obsolete

    The user is asked for a Project file name and the access modalities.
    Depending on the results of the dialog:

    - either an new project is create or an old is opened,
    - the old data may be discarded, added to the current pyFormex globals,
      or replace them
    - the opened Project may become the current Project, or its data are
      just imported in the current Project.


    The default will let the user create new project files as well as open
    existing ones.
    Use create=False or the convenience function openProject to only accept
    existing project files.

    If a compression level (1..9) is given, the contents will be compressed,
    resulting in much smaller project files at the cost of

    Only one pyFormex project can be open at any time. The open project
    owns all the global data created and exported by any script.

    If makeDefault is True, an already open project will be closed and
    the opened project becomes the current project.
    If makeDefault is False, the project data are imported into pf.PF
    and the current project does not change. This means that if a project was
    open, the imported data will be added to it.

    If addGlobals is None, the user is asked whether the current globals
    should be added to the project. Set True or False to force or reject
    the adding without asking.
    """
    print("Setting current project to %s" % proj.filename)
    print("Project contents: %s" % proj.contents())
    keep = {}
    if pf.PF:
        print("Current pyFormex globals: %s" % pf.PF.contents())
        _delete = "Delete"
        _add = "Keep non existing"
        _overwrite = "Keep all (overwrite project)"
        res = draw.ask("What shall I do with the current pyFormex globals?", [_delete, _add, _overwrite])
        if res == _add:
            keep = utils.removeDict(pf.PF, proj)
        elif res == _overwrite:
            keep = pf.PF
    pf.PF = proj
    if keep:
        pf.PF.update(keep)
    if pf.PF.filename:
        updateSettings({
            'curproj': pf.PF.filename,
            'workdir': os.path.dirname(pf.PF.filename),
            }, save=True)
    pf.GUI.setcurproj(pf.PF.filename)

    if hasattr(proj, '_autoscript_'):
        _ignore = "Ignore it!"
        _show = "Show it"
        _edit = "Load it in the editor"
        _exec = "Execute it"
        res = draw.ask("There is an autoscript stored inside the project.\nIf you received this project file from an untrusted source, you should probably not execute it.", [_ignore, _show, _edit, _exec])
        if res == _show:
            res = draw.showText(proj._autoscript_)#,actions=[_ignore,_edit,_show])
            return
        if res == _exec:
            draw.playScript(proj._autoscript_)
        elif res == _edit:
            fn = "_autoscript_.py"
            draw.checkWorkdir()
            f = open(fn, 'w')
            f.write(proj._autoscript_)
            f.close()
            openScript(fn)
            editApp(fn)

    if hasattr(proj, 'autofile') and draw.ack("The project has an autofile attribute: %s\nShall I execute this script?" % proj.autofile):
        draw.processArgs([proj.autofile])

    listProject()