Hyperfocal Distance and DoF Calculator (desktop application)

It’s been a while since I’ve been listening about Adobe AIR Framework, and I’ve always wanted to make a desktop application using HTML, JavaScript (on jQuery) and CSS. All of that, and it’s cross-platform, finally I wrote my first application that runs on Mac OS and Linux (although I didn’t test it on anything but Windows, so confirmations are very welcome).

About the application

Hyperfocal Distance and Depth of Field Calculator LogoHyperfocal Distance and Depth of Field Calculator is an application which can be used to calculate the Hyperfocal distance and Depth of Field values depending on the following lens and camera sensor properties.

Hyperfocal Distance:

  • Circle of Confusion (depends of Sensor/Film type)
  • Focal Length
  • Aperture

Depth of Field:

  • Circle of Confusion (depends of Sensor/Film type)
  • Focal Length
  • Aperture
  • Hyperfocal distance (calculated from above parameters)
  • Subject distance

Hyperfocal Distance and Depth of Field Calculator is a desktop application developed in Adobe AIR Framework and should run on:

  • Mac OS (some confirmation, please)
  • Linux (some confirmation, please)
  • Microsoft Windows 7 (confirmed)

In order to run Hyperfocal Distance and Depth of Field Calculator, you must:

  1. install Adobe AIR Framework 2.6 (or later),
  2. download Hyperfocal Distance and Depth of Field Calculator installation package,
  3. run the installer (hfd-dof-0.5.0.air)
  4. now you can run the executable…

Posted in Uncategorized | Tagged , , , , | Leave a comment

Using a scanner without dialogs in .Net

I recently had the task to create a sort of a web-interface for a scanner device (amongst other devices). The project is to be done in C# (Microsoft .Net Framework 4.0), so I had many helpful examples on how to do this, either by TWAIN or WIA libraries. Unfortunately all of these examples had one common problem, interrupting dialogs, either for scanning device selection, or the scanning options dialog, which were necessary to make the scan. As this is a web-interface, user will not be seeing any of these dialogs, so displaying them did just blocked the whole process.

After much searching I finally found an actually useful post on Windows Image Acquisition, which by the way also had a blocking dialog, but this was easily modified and in moments I had a simple class with a Scan(string scannerId) function which would do just that, Scan with a selected device and nothing more. More on scanning without dialogs in .Net…

Posted in .Net | Tagged , , , | 9 Comments

Multi-column unique key validation in CakePHP

CakePHP (at least in v1.3) by itself does not support a straight-forward multiple column unique key validation, but in a few steps you can have an elegant solution to this problem. In order to create a validation for multi-column unique key, you can use the CakePHP’s custom validation logic by creating a function which will do the validation, and defining the rule in your validations array of your model.

Unique key validation function

In my opinion the best place to be put this method is in your app_model (you can put it in your model class, but this way you can use this in all your models).

Add this function to your /your_app_root/app/models/app_model.php file:

function checkUnique($data, $fields)
{
    // check if the param contains multiple columns or a single one
    if (!is_array($fields))
    {
        $fields = array($fields);
    }

    // go trough all columns and get their values from the parameters
    foreach($fields as $key)
    {
        $unique[$key] = $this->data[$this->name][$key];
    }

    // primary key value must be different from the posted value
    if (isset($this->data[$this->name][$this->primaryKey]))
    {
        $unique[$this->primaryKey] = "<>" . $this->data[$this->name][$this->primaryKey];
    }

    // use the model's isUnique function to check the unique rule
    return $this->isUnique($unique, false);
}

This function basically receives the form parameters containing the values to be added/updated, creates a key-value pair array containing the name of the field in the unique key, and it’s value, adds the primary key and its value to the check (must be different from an existing one, or your updated wouldn’t work), and uses the CakePHP built-in function “isUnique” to do the validation.

More on creating a multiple column unique key validation in CakePHP…

Posted in CakePHP | Tagged , , , | Leave a comment

CakePHP in place update (with jQuery) plugin

Recently I had a need for an “in-place-update” of some fields on a CakePHP project that used jQuery. I found a lot of articles, examples and helpers that ironically didn’t help as much as I would like them to do, so I decided to write myself a new one.

I found a great example, but it only worked if you wished to edit items in displayed in a list-style or table-style structure, and I needed to basically have a standard CakePHP view view (yes a view view that corresponds to a view action in the controller), on which I can edit the Post title or description without going to the edit view.

A few slight modifications of the provided example, and voilà, I have created a CakePHP plugin for “in place update”. You can download it from github:

CakePHP in place updater plugin

Installation and usage

Extract the plugin

Extract the contents of the pack to the app/plugins/inplace_updater folder.

Add helper to your controller

Include the inplace_updater helper in your controller or app controller like so:

var $helpers = array(... 'InplaceUpdater.InplaceUpdater' ...);

Download and include Jeditable jQuery plugin to your layout

In order for this plugin to work, you will need to include the contents of the Jeditable jQuery plugin. Probably the simplest solution is to save the minified version (jquery.jeditable.mini.js) to your js folder, so the location of the file is “app\webroot\js\jquery.jeditable.mini.js”.

Jeditable jQuery plugin is a plugin for jQuery, so oviously it cannot be run without jQuery. Download the latest jQuery and copy it to “app\webroot\js\jquery.js”.

To include the required javascripts, add the following lines to your layout file (usually app\views\layouts\default.ctp):

<?php
    echo $javascript->link('jquery');
    echo $javascript->link('jquery.jeditable.mini');
?>

Add an in-place-updater control to your view

The updater helper allows you to add an “input” control to your views that will behave like the a div element (by default, or any other HTML element if you wish) on your view until you click/double-click/hover/etc on it, then it will appear as a text input, or a drop-down list, or any element supported by Jeditable jQuery plugin

<?php
echo $inplaceUpdater->input
(
    'Post',
    'title',
    $post['Post']['id'],
    array
    (
        'value'         => $post['Post']['title'],
        'actionName'    => 'inPlaceUpdate',
        'type'          => 'text',
        'cancelText'    => 'Cancel',
        'submitText'    => 'Save',
        'toolTip'       => 'Click to edit Post Title',
        'containerType' => 'h2',
    )
);
?>

Add an action handler in your controller

When the save button is pressed after modifying the in-place-edit element, a post is made to the inPlaceUpdate (by default) controller action. You can add a function like this to handle the in-place-update action.

function inPlaceUpdate($id = null)
{
    if (!$id)
        return;

    if ($this->data)
    {
        // get all the fields with its values (there should be only one, but anyway...)
        foreach($this->data['Post'] as $field => $value)
        {
            // check if the provided field name is acceptable
            switch($field)
            {
                case 'title':
                case 'description':
                    break;
                default:
                    $this->set('updated_value', '');
                    return;
            }

            $this->Post->id = $id;
            $this->Post->saveField($field, $value);

            $this->set('updated_value', $value);
        }
    }
}

Since you have a new action in your controller which is used for the in-place-update functionality, you will need to add the in_place_update view to your model’s views folder. And that view should display the updated result after save.

<?php
	echo $updated_value;
?>

Posted in CakePHP | Tagged , , , , | 2 Comments

Centralized Git Repository

In my recent svn to git adventures, I have tried to make a centralized git repository on numerous occasions and miserably failed. It seemed obvious to create a repository, clone it, do commits and push, but nooo. We need to create a centralized git repository, by declaring it as a “bare” repository when initializing it.

In the following tutorial I am going to create a centralized git repository, two working trees, and check that this actually works.

Central repository:

  • c:/repositories/git/project-x/

Two working threes:

  • c:/work/project-x/
  • c:/tests/project-x/

More on creating a centralized git repository…

Posted in Git | Tagged , , , , , | 2 Comments
Stop ACTA