예제 #1
0
void
_gwy_gl_material_class_setup_presets(void)
{
    static const GwyRGBA null_color = { 0, 0, 0, 0 };
    GwyResourceClass *klass;
    GwyGLMaterial *gl_material;

    /* Force class instantiation, this function is called before it's first
     * referenced. */
    klass = g_type_class_ref(GWY_TYPE_GL_MATERIAL);

    /* Default */
    gl_material = gwy_gl_material_new(GWY_GL_MATERIAL_DEFAULT,
                                      NULL, NULL, NULL, NULL, 0.0,
                                      TRUE);
    gwy_inventory_insert_item(klass->inventory, gl_material);
    g_object_unref(gl_material);

    /* None */
    gl_material = gwy_gl_material_new(GWY_GL_MATERIAL_NONE,
                                      &null_color, &null_color,
                                      &null_color, &null_color, 0.0,
                                      TRUE);
    gwy_inventory_insert_item(klass->inventory, gl_material);
    g_object_unref(gl_material);

    /* The gl_material added a reference so we can safely unref it again */
    g_type_class_unref(klass);
}
예제 #2
0
static void
gwy_resource_class_load_dir(const gchar *path,
                            GwyResourceClass *klass,
                            gboolean is_system)
{
    GDir *dir;
    GwyResource *resource;
    GError *err = NULL;
    const gchar *name;
    gchar *filename, *text;

    if (!(dir = g_dir_open(path, 0, NULL)))
        return;

    while ((name = g_dir_read_name(dir))) {
        if (gwy_filename_ignore(name))
            continue;

        if (gwy_inventory_get_item(klass->inventory, name)) {
            g_warning("Ignoring duplicite %s `%s'", klass->name, name);
            continue;
        }
        /* FIXME */
        filename = g_build_filename(path, name, NULL);
        if (!g_file_get_contents(filename, &text, NULL, &err)) {
            g_warning("Cannot read `%s': %s", filename, err->message);
            g_clear_error(&err);
            g_free(filename);
            continue;
        }
        g_free(filename);

        resource = gwy_resource_parse_real(text, G_TYPE_FROM_CLASS(klass),
                                           is_system);
        if (resource) {
            g_string_assign(resource->name, name);
            resource->is_modified = FALSE;
            gwy_inventory_insert_item(klass->inventory, resource);
            g_object_unref(resource);
        }
        g_free(text);
    }

    g_dir_close(dir);
}
예제 #3
0
/**
 * gwy_inventory_new_item:
 * @inventory: An inventory.
 * @name: Name of item to duplicate, may be %NULL to use default item (the
 *        same happens when @name does not exist).
 * @newname: Name of new item, it must not exist yet.  It may be %NULL, the
 *           new name is based on @name then.
 *
 * Creates a new item as a copy of existing one and inserts it to inventory.
 *
 * The newly created item can be called differently than @newname if that
 * already exists.
 *
 * Returns: The newly added item.
 **/
gpointer
gwy_inventory_new_item(GwyInventory *inventory,
                       const gchar *name,
                       const gchar *newname)
{
    guint i = 0;
    gpointer item = NULL;

    g_return_val_if_fail(GWY_IS_INVENTORY(inventory), NULL);
    g_return_val_if_fail(!inventory->is_const, NULL);
    g_return_val_if_fail(inventory->can_make_copies, NULL);

    /* Find which item we should base copy on */
    if (!name && inventory->has_default)
        name = inventory->default_key->str;

    if ((!name || !(i = gwy_inventory_lookup(inventory, name)))
        && inventory->items->len)
        i = 1;
    if (i) {
        item = g_ptr_array_index(inventory->items, i-1);
        name = inventory->item_type.get_name(item);
    }

    if (!name || !item) {
        g_warning("No default item to base new item on");
        return NULL;
    }

    /* Find new name */
    if (!newname)
        newname = gwy_inventory_invent_name(inventory, name);
    else if (gwy_inventory_lookup(inventory, newname))
        newname = gwy_inventory_invent_name(inventory, newname);

    /* Create new item */
    item = inventory->item_type.copy(item);
    inventory->item_type.rename(item, newname);
    gwy_inventory_insert_item(inventory, item);

    return item;
}
예제 #4
0
static void
cnew(G_GNUC_UNUSED GwyContainer *data, GwyRunType run)
{
    GwyDataField *dfield;
    CNewArgs args;
    gboolean ok;
    gint oldid, i, j, k, n;
    GwyCalibration *calibration;
    GwyCalData *caldata = NULL, *old;
    gchar *filename;
    gchar *contents;
    gsize len = 0;
    GError *err = NULL;
    gsize pos = 0;
    GString *str;
    gdouble *x, *y, *z, *xunc, *yunc, *zunc, *xerr, *yerr, *zerr;
    FILE *fh;


    g_return_if_fail(run & CNEW_RUN_MODES);
    gwy_app_data_browser_get_current(GWY_APP_DATA_FIELD, &dfield,
                                     GWY_APP_DATA_FIELD_ID, &oldid,
                                     0);
    g_return_if_fail(dfield);

    cnew_load_args(gwy_app_settings_get(), &args);
    if (run == GWY_RUN_INTERACTIVE) {
        ok = cnew_dialog(&args, dfield);
        cnew_save_args(gwy_app_settings_get(), &args);
        if (!ok)
            return;
    }

    /*create the caldata*/
    caldata = gwy_caldata_new(8);
    x = gwy_caldata_get_x(caldata);
    y = gwy_caldata_get_y(caldata);
    z = gwy_caldata_get_z(caldata);
    xerr = gwy_caldata_get_xerr(caldata);
    yerr = gwy_caldata_get_yerr(caldata);
    zerr = gwy_caldata_get_zerr(caldata);
    xunc = gwy_caldata_get_xunc(caldata);
    yunc = gwy_caldata_get_yunc(caldata);
    zunc = gwy_caldata_get_zunc(caldata);
    gwy_caldata_set_si_unit_x(caldata, gwy_data_field_get_si_unit_xy(dfield));
    gwy_caldata_set_si_unit_y(caldata, gwy_data_field_get_si_unit_xy(dfield));
    gwy_caldata_set_si_unit_z(caldata, gwy_data_field_get_si_unit_z(dfield));

    n = 0;
    for (i=0; i<2; i++)
    {
        for (j=0; j<2; j++)
        {
            for (k=0; k<2; k++)
            {

                if (i) x[n] = args.xrange_from;
                else x[n] = args.xrange_to;
                if (j) y[n] = args.yrange_from;
                else y[n] = args.yrange_to;
                if (k) z[n] = args.zrange_from;
                else z[n] = args.zrange_to;

                if (i) xerr[n] = (args.xrange_to-args.xrange_from)*(args.xmult-1);
                else xerr[n] = 0;
                if (j) yerr[n] = (args.yrange_to-args.yrange_from)*(args.ymult-1);
                else yerr[n] = 0;
                if (k) zerr[n] = (args.zrange_to-args.zrange_from)*(args.zmult-1);
                else zerr[n] = 0;

                xunc[n] = args.xunc;
                yunc[n] = args.yunc;
                zunc[n] = args.zunc;
                n++;
            }
        }
    }

    if (args.duplicate == DUPLICATE_APPEND && (calibration = gwy_inventory_get_item(gwy_calibrations(), args.name)))
        {
        filename = g_build_filename(gwy_get_user_dir(), "caldata", calibration->filename, NULL);
        if (!g_file_get_contents(filename,
                                 &contents, &len, &err) || !len)
        {
             g_warning("Error loading file: %s", err->message);
             g_clear_error(&err);
             return;
        }
        else {
            if (len)
              old = GWY_CALDATA(gwy_serializable_deserialize(contents, len, &pos));
            g_free(contents);
        }

        gwy_caldata_append(old, caldata);
        g_object_unref(caldata);
        caldata = old;

    }

    gwy_caldata_set_range(caldata, args.xrange_from, args.xrange_to,
                          args.yrange_from, args.yrange_to,
                          args.zrange_from, args.zrange_to);

    //printf("Saving %g %g %g %g\n", args.xrange_from, args.xrange_to, args.yrange_from, args.yrange_to);

    /*now create and save the resource*/
    if ((calibration = GWY_CALIBRATION(gwy_inventory_get_item(gwy_calibrations(), args.name)))==NULL)
    {
        calibration = gwy_calibration_new(args.name, g_strconcat(args.name, ".dat", NULL));
        gwy_inventory_insert_item(gwy_calibrations(), calibration);
        g_object_unref(calibration);
    }
    calibration->caldata = caldata;

    filename = gwy_resource_build_filename(GWY_RESOURCE(calibration));
    if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
        g_mkdir(g_build_filename(gwy_get_user_dir(), "calibrations", NULL), 0700);
    }
    fh = gwy_fopen(filename, "wb");
    if (!fh) {
        g_warning(_("Cannot save preset: %s"), filename);
        g_free(filename);
        return;
    }
    g_free(filename);

    str = gwy_resource_dump(GWY_RESOURCE(calibration));
    fwrite(str->str, 1, str->len, fh);
    fclose(fh);
    g_string_free(str, TRUE);

    gwy_resource_data_saved(GWY_RESOURCE(calibration));
    gwy_resource_data_changed(GWY_RESOURCE(calibration));

    /*now save the calibration data*/
    //gwy_caldata_debug(caldata, "Saving: ");

    gwy_caldata_save_data(caldata, calibration->filename);

}
예제 #5
0
static void
cload(GwyContainer *data, GwyRunType run)
{
    GwyDataField *dfield;
    CLoadArgs args;
    gboolean ok;
    gint oldid, i, n;
    GwyCalibration *calibration;
    GwyCalData *caldata = NULL;
    gchar *filename;
    gchar *contents;
    gsize len;
    GError *err = NULL;
    gsize pos = 0;
    GString *str;
    GByteArray *barray;
    FILE *fh;


    g_return_if_fail(run & CNEW_RUN_MODES);
    gwy_app_data_browser_get_current(GWY_APP_DATA_FIELD, &dfield,
                                     GWY_APP_DATA_FIELD_ID, &oldid,
                                     0);
    g_return_if_fail(dfield);

    //cload_load_args(gwy_app_settings_get(), &args);
    if (run == GWY_RUN_INTERACTIVE) {
        ok = cload_dialog(&args, dfield);
        //cload_save_args(gwy_app_settings_get(), &args);
        if (!ok)
            return;
    }

    /*if append requested, copy newly created calibration into old one*/
    if (args.duplicate == DUPLICATE_APPEND && (calibration = gwy_inventory_get_item(gwy_calibrations(), args.name)))
    {
        
        filename = g_build_filename(gwy_get_user_dir(), "caldata", calibration->filename, NULL);
        if (!g_file_get_contents(filename,
                                 &contents, &len, &err))
        {
             g_warning("Error loading file: %s\n", err->message);
             g_clear_error(&err);
             return;
        }
        else {
            if (len)
              caldata = GWY_CALDATA(gwy_serializable_deserialize(contents, len, &pos));
            g_free(contents);
        }
        n = caldata->ndata + args.caldata->ndata;

        //add to args->caldata
        args.caldata->x = g_realloc(args.caldata->x, n*sizeof(gdouble));
        args.caldata->y = g_realloc(args.caldata->y, n*sizeof(gdouble));
        args.caldata->z = g_realloc(args.caldata->z, n*sizeof(gdouble));
        args.caldata->xerr = g_realloc(args.caldata->xerr, n*sizeof(gdouble));
        args.caldata->yerr = g_realloc(args.caldata->yerr, n*sizeof(gdouble));
        args.caldata->zerr = g_realloc(args.caldata->zerr, n*sizeof(gdouble));
        args.caldata->xunc = g_realloc(args.caldata->xunc, n*sizeof(gdouble));
        args.caldata->yunc = g_realloc(args.caldata->yunc, n*sizeof(gdouble));
        args.caldata->zunc = g_realloc(args.caldata->zunc, n*sizeof(gdouble));

        for (i=args.caldata->ndata; i<n; i++)
        {
           args.caldata->x[i] = caldata->x[i-caldata->ndata];
           args.caldata->y[i] = caldata->y[i-caldata->ndata];
           args.caldata->z[i] = caldata->z[i-caldata->ndata];
           args.caldata->xerr[i] = caldata->xerr[i-caldata->ndata];
           args.caldata->yerr[i] = caldata->yerr[i-caldata->ndata];
           args.caldata->zerr[i] = caldata->zerr[i-caldata->ndata];
           args.caldata->xunc[i] = caldata->xunc[i-caldata->ndata];
           args.caldata->yunc[i] = caldata->yunc[i-caldata->ndata];
           args.caldata->zunc[i] = caldata->zunc[i-caldata->ndata];
        }
        args.caldata->ndata = n; 
    }

    /*now create and save the resource*/
    if ((calibration = GWY_CALIBRATION(gwy_inventory_get_item(gwy_calibrations(), args.name)))==NULL)
    {
        calibration = gwy_calibration_new(args.name, 8, g_strconcat(args.name, ".dat", NULL));
        gwy_inventory_insert_item(gwy_calibrations(), calibration);
        g_object_unref(calibration);
    }

    filename = gwy_resource_build_filename(GWY_RESOURCE(calibration));
    fh = g_fopen(filename, "w");
    if (!fh) {
        g_warning("Cannot save preset: %s", filename);
        g_free(filename);
        return;
    }
    g_free(filename);

    str = gwy_resource_dump(GWY_RESOURCE(calibration));
    fwrite(str->str, 1, str->len, fh);
    fclose(fh);
    g_string_free(str, TRUE);

    gwy_resource_data_saved(GWY_RESOURCE(calibration));

    //debugcal(args.caldata);

    /*now save the calibration data*/
    if (!g_file_test(g_build_filename(gwy_get_user_dir(), "caldata", NULL), G_FILE_TEST_EXISTS)) {
        g_mkdir(g_build_filename(gwy_get_user_dir(), "caldata", NULL), 0700);
    }
    fh = g_fopen(g_build_filename(gwy_get_user_dir(), "caldata", calibration->filename, NULL), "w");
    if (!fh) {
        g_warning("Cannot save caldata\n");
        return;
    }
    barray = gwy_serializable_serialize(G_OBJECT(args.caldata), NULL);
    //g_file_set_contents(fh, barray->data, sizeof(guint8)*barray->len, NULL);
    fwrite(barray->data, sizeof(guint8), barray->len, fh);
    fclose(fh);

}