Blame platform-demos/fr/scale.js.page

Packit 1470ea
Packit 1470ea
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="scale.js" xml:lang="fr">
Packit 1470ea
  <info>
Packit 1470ea
  <title type="text">Scale (JavaScript)</title>
Packit 1470ea
    <link type="guide" xref="beginner.js#entry"/>
Packit 1470ea
    <revision version="0.1" date="2012-06-20" status="draft"/>
Packit 1470ea
Packit 1470ea
    <credit type="author copyright">
Packit 1470ea
      <name>Taryn Fox</name>
Packit 1470ea
      <email its:translate="no">jewelfox@fursona.net</email>
Packit 1470ea
      <years>2012</years>
Packit 1470ea
    </credit>
Packit 1470ea
Packit 1470ea
    <desc>Une glissière qui correspond à une valeur numérique</desc>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Luc Rebert,</mal:name>
Packit 1470ea
      <mal:email>traduc@rebert.name</mal:email>
Packit 1470ea
      <mal:years>2011</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Alain Lojewski,</mal:name>
Packit 1470ea
      <mal:email>allomervan@gmail.com</mal:email>
Packit 1470ea
      <mal:years>2011-2012</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Luc Pionchon</mal:name>
Packit 1470ea
      <mal:email>pionchon.luc@gmail.com</mal:email>
Packit 1470ea
      <mal:years>2011</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Bruno Brouard</mal:name>
Packit 1470ea
      <mal:email>annoa.b@gmail.com</mal:email>
Packit 1470ea
      <mal:years>2011-12</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Luis Menina</mal:name>
Packit 1470ea
      <mal:email>liberforce@freeside.fr</mal:email>
Packit 1470ea
      <mal:years>2014</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>Échelle</title>
Packit 1470ea
  <media type="image" mime="image/png" src="media/scalepenguins.png"/>
Packit 1470ea
  

Une échelle est une glissière horizontale ou verticale représentant une valeur comprise dans une fourchette numérique. Pour créer une échelle, vous devez définir sa position par défaut, son étendue numérique et le pas auquel elle avance ou recule. Au lieu d'être obligé de tout recommencer à chaque fois que vous créez une nouvelle échelle, vous pouvez inclure toutes ces données dans un objet nommé Adjustment et qui garde en mémoire tous les paramètres. Ensuite, à chaque création d'une nouvelle échelle, il suffit de lui indiquer de prendre ces réglages.

Packit 1470ea
  

Cette échelle est un simple élément graphique permettant d'ajuster la taille d'un iceberg sur lequel vivent des pingouins. Le nombre de pingouins sur l'iceberg est le produit des valeurs des deux glissières. Faites des essais en modifiant les valeurs et observez le résultat.

Packit 1470ea
    <links type="section"/>
Packit 1470ea
Packit 1470ea
  <section id="imports">
Packit 1470ea
    <title>Bibliothèques à importer</title>
Packit 1470ea
    
Packit 1470ea
#!/usr/bin/gjs
Packit 1470ea
Packit 1470ea
const Gio = imports.gi.Gio;
Packit 1470ea
const Gtk = imports.gi.Gtk;
Packit 1470ea
const Lang = imports.lang;
Packit 1470ea
]]>
Packit 1470ea
    

Ce sont les bibliothèques que nous devons importer pour faire fonctionner cette application. N'oubliez pas que la ligne qui informe GNOME que nous allons utiliser Gjs doit toujours se trouver au début.

Packit 1470ea
    </section>
Packit 1470ea
Packit 1470ea
  <section id="applicationwindow">
Packit 1470ea
    <title>Création de la fenêtre de l'application</title>
Packit 1470ea
    
Packit 1470ea
const ScaleExample = new Lang.Class({
Packit 1470ea
    Name: 'Scale Example',
Packit 1470ea
Packit 1470ea
    // Create the application itself
Packit 1470ea
    _init: function() {
Packit 1470ea
        this.application = new Gtk.Application({
Packit 1470ea
            application_id: 'org.example.jsscale'
Packit 1470ea
        });
Packit 1470ea
Packit 1470ea
    // Connect 'activate' and 'startup' signals to the callback functions
Packit 1470ea
    this.application.connect('activate', Lang.bind(this, this._onActivate));
Packit 1470ea
    this.application.connect('startup', Lang.bind(this, this._onStartup));
Packit 1470ea
    },
Packit 1470ea
Packit 1470ea
    // Callback function for 'activate' signal presents window when active
Packit 1470ea
    _onActivate: function() {
Packit 1470ea
        this._window.present();
Packit 1470ea
    },
Packit 1470ea
Packit 1470ea
    // Callback function for 'startup' signal builds the UI
Packit 1470ea
    _onStartup: function() {
Packit 1470ea
        this._buildUI ();
Packit 1470ea
    },
Packit 1470ea
]]>
Packit 1470ea
    

Tout le code de cet exemple va dans la classe ScaleExemple. Le code ci-dessus crée une <link href="http://www.roojs.com/seed/gir-1.2-gtk-3.0/gjs/Gtk.Application.html">Gtk.Application</link> pour nos éléments graphiques et la fenêtre qui les contient.

Packit 1470ea
    
Packit 1470ea
    // Build the application's UI
Packit 1470ea
    _buildUI: function() {
Packit 1470ea
Packit 1470ea
        // Create the application window
Packit 1470ea
        this._window = new Gtk.ApplicationWindow({
Packit 1470ea
            application: this.application,
Packit 1470ea
            window_position: Gtk.WindowPosition.CENTER,
Packit 1470ea
            border_width: 20,
Packit 1470ea
            title: "Birds on a Floe"});
Packit 1470ea
]]>
Packit 1470ea
    

La fonction _buildUI est l'endroit où nous mettons tout le code nécessaire à la création de l'interface utilisateur de l'application. La première étape consiste à créer une <link xref="GtkApplicationWindow.js">Gtk.ApplicationWindow</link> pour y mettre tous nos éléments graphiques.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="button">
Packit 1470ea
    <title>Création des échelles</title>
Packit 1470ea
    
Packit 1470ea
        // Create the horizontal scale
Packit 1470ea
        this._hScale = Gtk.Scale.new_with_range (Gtk.Orientation.HORIZONTAL, 0.0, 100.0, 5.0);
Packit 1470ea
        this._hScale.set_valign (Gtk.Align.START);
Packit 1470ea
        this._hScale.set_value (50);
Packit 1470ea
        this._hScale.set_digits (0);
Packit 1470ea
        // this._hScale.set_draw_value (false);
Packit 1470ea
]]>
Packit 1470ea
Packit 1470ea
    

La méthode new_with_range est l'un des moyens pour créer un nouvel élément graphique échelle. Les paramètres qu'elle utilise sont <link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.Orientation.html">Gtk.Orientation</link>, la valeur minimum et l'incrément pour une seule graduation. Ensuite, nous utilisons les méthodes de l'échelle pour définir sa valeur de départ et en combien de fractions décimales la diviser. Dans ce cas, il nous faut aussi définir son alignement vertical pour la placer dans la fenêtre.

Packit 1470ea
    

Nous pouvons aussi lui indiquer s'il faut afficher ou non le nombre à côté de la glissière avec la méthode set_draw_value. Les explications se trouvent dans cet exemple.

Packit 1470ea
Packit 1470ea
    
Packit 1470ea
        // Create a master adjustment to use for the vertical (or any other) scale
Packit 1470ea
        this._adjustment = new Gtk.Adjustment ({
Packit 1470ea
            value: 95,
Packit 1470ea
            lower: 0,
Packit 1470ea
            upper: 100,
Packit 1470ea
            step_increment: 5,
Packit 1470ea
            page_increment: 10 });
Packit 1470ea
]]>
Packit 1470ea
Packit 1470ea
    

Un Adjustment est un objet permettant de simplifier la procédure de création d'une nouvelle échelle. La propriété « valeur » de l'Adjustment est la valeur par défaut de l'échelle alors que les valeurs « superieure » et « inferieure » en représentent le début et la fin. Entre les deux, les valeurs de l'incrément définissent le pas auquel l'échelle se déplace lorsque vous cliquez dessus.

Packit 1470ea
Packit 1470ea
    
Packit 1470ea
        // Create a vertical scale using the adjustment we just made
Packit 1470ea
        this._vScale = new Gtk.Scale ({
Packit 1470ea
            orientation: Gtk.Orientation.VERTICAL,
Packit 1470ea
            adjustment: this._adjustment,
Packit 1470ea
            digits: 0,
Packit 1470ea
            // draw_value: false,
Packit 1470ea
            margin_left: 10 });
Packit 1470ea
]]>
Packit 1470ea
Packit 1470ea
    

Ici, nous créons un nouvel objet échelle en utilisant _adjustment comme étant sa propriété d'« ajustement ». Cela est un raccourci important. Cependant, il nous faut encore lui indiquer comment arrondir les décimales. Notez que nous y expliquons aussi la propriété draw_value : comment lui indiquer de ne pas afficher le nombre à côté de l'échelle lorsque vous décidez cette façon de faire.

Packit 1470ea
Packit 1470ea
    
Packit 1470ea
        // Create the label that shows the product of the two values
Packit 1470ea
        this._product = (this._hScale.get_value() * this._vScale.get_value());
Packit 1470ea
        this._label = new Gtk.Label ({
Packit 1470ea
            label: (String(this._product) + " penguins on the iceberg."),
Packit 1470ea
            height_request: 200,
Packit 1470ea
            width_request: 200,
Packit 1470ea
            wrap: true});
Packit 1470ea
Packit 1470ea
        // Connect the two scales to functions which recalculate the label
Packit 1470ea
        this._hScale.connect ("value-changed", Lang.bind (this, this._recalc));
Packit 1470ea
        this._vScale.connect ("value-changed", Lang.bind (this, this._recalc));
Packit 1470ea
]]>
Packit 1470ea
Packit 1470ea
    

La méthode get_value permet de trouver la valeur numérique à laquelle une échelle est définie. Ensuite, nous pouvons en faire ce que nous voulons, y compris multiplier les valeurs des deux échelles et obtenir une <link xref="label.js">étiquette</link> affichant le produit. Paramétrons le texte avec retour à la ligne, car nous avons aussi un message stupide à afficher.

Packit 1470ea
    

Ensuite, connectons les deux signaux « value-changed » à la fonction _recalc, qui recalcule le nombre de pingouins sur l'iceberg et affiche un nouveau message.

Packit 1470ea
Packit 1470ea
    
Packit 1470ea
        // Create a grid to arrange things in
Packit 1470ea
        this._UIGrid = new Gtk.Grid ({
Packit 1470ea
            halign: Gtk.Align.CENTER,
Packit 1470ea
            valign: Gtk.Align.CENTER,
Packit 1470ea
            margin_top: 20,
Packit 1470ea
            margin_left: 20});
Packit 1470ea
Packit 1470ea
        // Attach everything to the grid
Packit 1470ea
        this._UIGrid.attach (this._label, 0, 0, 1, 1);
Packit 1470ea
        this._UIGrid.attach (this._hScale, 0, 1, 1, 1);
Packit 1470ea
        this._UIGrid.attach (this._vScale, 1, 0, 1, 1);
Packit 1470ea
]]>
Packit 1470ea
    

Ici, nous créons une <link xref="grid.js">grille</link> pour tout y mettre et nous lui lions tous nos éléments graphiques. Notez que cette fois (et pour quelques éléments graphiques) nous utilisons des marges pour espacer proprement les éléments.

Packit 1470ea
    
Packit 1470ea
        // Add the grid to the window
Packit 1470ea
        this._window.add (this._UIGrid);
Packit 1470ea
Packit 1470ea
        // Show the window and all child widgets
Packit 1470ea
        this._window.show_all();
Packit 1470ea
    },
Packit 1470ea
]]>
Packit 1470ea
    

Enfin, nous ajoutons la grille à la fenêtre et indiquons à celle-ci de s'afficher toute seule avec tout son contenu.

Packit 1470ea
    </section>
Packit 1470ea
Packit 1470ea
    <section id="scales-handler">
Packit 1470ea
    <title>Fonction prenant en charge les modifications de valeurs de l'échelle</title>
Packit 1470ea
Packit 1470ea
    
Packit 1470ea
    _recalc: function() {
Packit 1470ea
Packit 1470ea
        // Figure out what the product of the two scales' values is
Packit 1470ea
        var product = (this._hScale.get_value() * this._vScale.get_value());
Packit 1470ea
Packit 1470ea
        // Create a blank comment line in case there isn't a silly comment to make
Packit 1470ea
        var comment = "";
Packit 1470ea
Packit 1470ea
        // Make a silly comment based on the number of penguins
Packit 1470ea
        if (product > 9000) {
Packit 1470ea
            comment = "It's over 9000!";
Packit 1470ea
        }
Packit 1470ea
        else if (product < 1000 && product > 0) {
Packit 1470ea
            comment = "They're getting lonely.";
Packit 1470ea
        }
Packit 1470ea
        else if (product == 0) {
Packit 1470ea
            comment = "They're all gone ...";
Packit 1470ea
        }
Packit 1470ea
        else comment = "";
Packit 1470ea
Packit 1470ea
        // Set ._label's new text
Packit 1470ea
        this._label.set_label (String (product) + " penguins on the iceberg. " + comment);
Packit 1470ea
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
});
Packit 1470ea
]]>
Packit 1470ea
    

Souvenez-vous, nous pouvons obtenir la valeur d'une échelle avec la méthode get_value. Ici, nous ne faisons que recalculer le produit des deux valeurs après qu'une des deux échelles a bougé, ajouter un message stupide en fonction du nombre de pingouins restant et modifier le texte de l'étiquette avec le nouveau nombre et le message.

Packit 1470ea
Packit 1470ea
    
Packit 1470ea
// Run the application
Packit 1470ea
let app = new ScaleExample ();
Packit 1470ea
app.application.run (ARGV);
Packit 1470ea
]]>
Packit 1470ea
    

Enfin, nous créons un nouvel exemple de la classe ScaleExemple et démarrons l'application.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="complete">
Packit 1470ea
    <title>Exemple complet de code</title>
Packit 1470ea
#!/usr/bin/gjs
Packit 1470ea
Packit 1470ea
imports.gi.versions.Gtk = '3.0';
Packit 1470ea
Packit 1470ea
const Gio = imports.gi.Gio;
Packit 1470ea
const Gtk = imports.gi.Gtk;
Packit 1470ea
Packit 1470ea
class ScaleExample {
Packit 1470ea
Packit 1470ea
    // Create the application itself
Packit 1470ea
    constructor() {
Packit 1470ea
        this.application = new Gtk.Application({
Packit 1470ea
            application_id: 'org.example.jsscale'
Packit 1470ea
        });
Packit 1470ea
Packit 1470ea
        // Connect 'activate' and 'startup' signals to the callback functions
Packit 1470ea
        this.application.connect('activate', this._onActivate.bind(this));
Packit 1470ea
        this.application.connect('startup', this._onStartup.bind(this));
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
    // Callback function for 'activate' signal presents window when active
Packit 1470ea
    _onActivate() {
Packit 1470ea
        this._window.present();
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
    // Callback function for 'startup' signal builds the UI
Packit 1470ea
    _onStartup() {
Packit 1470ea
        this._buildUI();
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
    // Build the application's UI
Packit 1470ea
    _buildUI() {
Packit 1470ea
Packit 1470ea
        // Create the application window
Packit 1470ea
        this._window = new Gtk.ApplicationWindow({
Packit 1470ea
            application: this.application,
Packit 1470ea
            window_position: Gtk.WindowPosition.CENTER,
Packit 1470ea
            border_width: 20,
Packit 1470ea
            title: "Birds on a Floe"});
Packit 1470ea
Packit 1470ea
        // Create the horizontal scale
Packit 1470ea
        this._hScale = Gtk.Scale.new_with_range (Gtk.Orientation.HORIZONTAL, 0.0, 100.0, 5.0);
Packit 1470ea
        this._hScale.set_valign (Gtk.Align.START);
Packit 1470ea
        this._hScale.set_value (50);
Packit 1470ea
        this._hScale.set_digits (0);
Packit 1470ea
        // this._hScale.set_draw_value (false);
Packit 1470ea
Packit 1470ea
        // Create a master adjustment to use for the vertical (or any other) scale
Packit 1470ea
        this._adjustment = new Gtk.Adjustment ({
Packit 1470ea
            value: 95,
Packit 1470ea
            lower: 0,
Packit 1470ea
            upper: 100,
Packit 1470ea
            step_increment: 5,
Packit 1470ea
            page_increment: 10 });
Packit 1470ea
Packit 1470ea
        // Create a vertical scale using the adjustment we just made
Packit 1470ea
        this._vScale = new Gtk.Scale ({
Packit 1470ea
            orientation: Gtk.Orientation.VERTICAL,
Packit 1470ea
            adjustment: this._adjustment,
Packit 1470ea
            digits: 0,
Packit 1470ea
            // draw_value: false,
Packit 1470ea
            margin_left: 10 });
Packit 1470ea
Packit 1470ea
        // Create the label that shows the product of the two values
Packit 1470ea
        this._product = (this._hScale.get_value() * this._vScale.get_value());
Packit 1470ea
        this._label = new Gtk.Label ({
Packit 1470ea
            label: (String(this._product) + " penguins on the iceberg."),
Packit 1470ea
            height_request: 200,
Packit 1470ea
            width_request: 200,
Packit 1470ea
            wrap: true});
Packit 1470ea
Packit 1470ea
        // Connect the two scales to functions which recalculate the label
Packit 1470ea
        this._hScale.connect ("value-changed", this._recalc.bind(this));
Packit 1470ea
        this._vScale.connect ("value-changed", this._recalc.bind(this));
Packit 1470ea
Packit 1470ea
        // Create a grid to arrange things in
Packit 1470ea
        this._UIGrid = new Gtk.Grid ({
Packit 1470ea
            halign: Gtk.Align.CENTER,
Packit 1470ea
            valign: Gtk.Align.CENTER,
Packit 1470ea
            margin_top: 20,
Packit 1470ea
            margin_left: 20});
Packit 1470ea
Packit 1470ea
        // Attach everything to the grid
Packit 1470ea
        this._UIGrid.attach (this._label, 0, 0, 1, 1);
Packit 1470ea
        this._UIGrid.attach (this._hScale, 0, 1, 1, 1);
Packit 1470ea
        this._UIGrid.attach (this._vScale, 1, 0, 1, 1);
Packit 1470ea
Packit 1470ea
        // Add the grid to the window
Packit 1470ea
        this._window.add (this._UIGrid);
Packit 1470ea
Packit 1470ea
        // Show the window and all child widgets
Packit 1470ea
        this._window.show_all();
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
    _recalc() {
Packit 1470ea
Packit 1470ea
        // Figure out what the product of the two scales' values is
Packit 1470ea
        var product = (this._hScale.get_value() * this._vScale.get_value());
Packit 1470ea
Packit 1470ea
        // Create a blank comment line in case there isn't a silly comment to make
Packit 1470ea
        var comment = "";
Packit 1470ea
Packit 1470ea
        // Make a silly comment based on the number of penguins
Packit 1470ea
        if (product > 9000) {
Packit 1470ea
            comment = "It's over 9000!";
Packit 1470ea
        }
Packit 1470ea
        else if (product < 1000 && product > 0) {
Packit 1470ea
            comment = "They're getting lonely.";
Packit 1470ea
        }
Packit 1470ea
        else if (product == 0) {
Packit 1470ea
            comment = "They're all gone ...";
Packit 1470ea
        }
Packit 1470ea
        else comment = "";
Packit 1470ea
Packit 1470ea
        // Set ._label's new text
Packit 1470ea
        this._label.set_label (String (product) + " penguins on the iceberg. " + comment);
Packit 1470ea
    }
Packit 1470ea
};
Packit 1470ea
Packit 1470ea
// Run the application
Packit 1470ea
let app = new ScaleExample ();
Packit 1470ea
app.application.run (ARGV);
Packit 1470ea
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="in-depth">
Packit 1470ea
    <title>Documentation approfondie</title>
Packit 1470ea
<list>
Packit 1470ea
  <item>

<link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.Adjustment.html">Gtk.Adjustment</link>

</item>
Packit 1470ea
  <item>

<link href="http://www.roojs.com/seed/gir-1.2-gtk-3.0/gjs/Gtk.Application.html">Gtk.Application</link>

</item>
Packit 1470ea
  <item>

<link href="http://developer.gnome.org/gtk3/stable/GtkApplicationWindow.html">Gtk.ApplicationWindow</link>

</item>
Packit 1470ea
  <item>

<link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.Grid.html">Gtk.Grid</link>

</item>
Packit 1470ea
  <item>

<link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.Label.html">Gtk.Label</link>

</item>
Packit 1470ea
  <item>

<link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.Scale.html">Gtk.Scale</link>

</item>
Packit 1470ea
</list>
Packit 1470ea
  </section>
Packit 1470ea
</page>