検索

5.6.2. グラフィカルユーザーインターフェイス

download PDF
本セクションでは、グラフィカルユーザーインターフェイス (GUI) のサポートをアドオンに追加する方法を説明します。これを開始する前に、前のセクションで説明した Kickstart のサポートがアドオンに含まれていることを確認してください。
注記
グラフィカルインターフェイスをサポートするアドオンの開発を開始する前に、SpokeWindow などの Anaconda 固有の Gtk ウィジェットを含む anaconda-widgets および anaconda-widgets-devel パッケージを必ずインストールしてください。
5.6.2.1. 基本的機能
アドオンの Kickstart サポートと同様に、GUI サポートでもアドオンのすべてのパートで、API で定義された特定のクラスから継承されたクラスの定義があるモジュールが少なくとも 1 つ含まれている必要があります。グラフィカルサポートの場合は、推奨される唯一のクラスは NormalSpoke で、これは pyanaconda.ui.gui.spokes で定義されます。クラス名の通り、ノーマルスポーク タイプのスクリーン用のクラスです。「ハブ & スポークモデル」 .
NormalSpoke から継承した新たなクラスを実装するには、API が必要とする以下のクラス属性を定義する必要があります。
  • builderObjects: スポークの .glade ファイルからすべてのトップレベルオブジェクトを一覧表示します。これらのオブジェクトは、その子オブジェクトとともに (反復的に) スポークに公開されるべきものです。ただし、すべてをスポークに公開する場合 (非推奨) は、空のリストにします。
  • mainWidgetName: メインウィンドウウィジェットの ID が含まれます。 [4] .glade ファイルで定義されている設定
  • uiFile: .glade ファイルの名前が含まれます。
  • category: スポークが属するカテゴリーのクラスが含まれます。
  • icon: ハブ上のスポークに使用するアイコンの識別子が含まれます。
  • title: ハブ上のスポークに使用するタイトルを定義します。
以下に必須の定義すべてを含むモジュール例を示します。

例8 Normalspoke クラスに必須の属性の定義

# will never be translated
_ = lambda x: x
N_ = lambda x: x

# the path to addons is in sys.path so we can import things from org_fedora_hello_world
from org_fedora_hello_world.gui.categories.hello_world import HelloWorldCategory
from pyanaconda.ui.gui.spokes import NormalSpoke

# export only the spoke, no helper functions, classes or constants
__all__ = ["HelloWorldSpoke"]

class HelloWorldSpoke(NormalSpoke):
    """
    Class for the Hello world spoke. This spoke will be in the Hello world
    category and thus on the Summary hub. It is a very simple example of
    a unit for the Anaconda's graphical user interface.

    :see: pyanaconda.ui.common.UIObject
    :see: pyanaconda.ui.common.Spoke
    :see: pyanaconda.ui.gui.GUIObject

    """

    ### class attributes defined by API ###

    # list all top-level objects from the .glade file that should be exposed
    # to the spoke or leave empty to extract everything
    builderObjects = ["helloWorldSpokeWindow", "buttonImage"]

    # the name of the main window widget
    mainWidgetName = "helloWorldSpokeWindow"

    # name of the .glade file in the same directory as this source
    uiFile = "hello_world.glade"

    # category this spoke belongs to
    category = HelloWorldCategory

    # spoke icon (will be displayed on the hub)
    # preferred are the -symbolic icons as these are used in Anaconda's spokes
    icon = "face-cool-symbolic"

    # title of the spoke (will be displayed on the hub)
    title = N_("_HELLO WORLD")
__all__ 属性はスポーククラスのエクスポートに使用され、これまでに説明した属性の定義を含む定義の最初の行がその後に続きます。これらの属性値は、com_example_hello_world/gui/spokes/hello.glade ファイルで定義されるウィジェットを参照します。
他に注目すべき 2 つの属性があります。1 番目の値は category で、com_example_hello_world.gui.categories モジュールの HelloWorldCategory クラスからインポートされます。のHelloWorldCategoryclass については後で説明しますが、ここでは、com_example_hello_world パッケージからインポートできるように、アドオンへのパスが sys.path にあることに注意してください。
2 つ目の notable 属性は title で、定義に 2 つのアンダースコアが含まれています。最初の 1 つは、翻訳の文字列のマークをマークする N_ 関数名の一部ですが、変換されていないバージョンの文字列を返します (変換は後で行われます)。2 つ目のアンダースコアはタイトル自体の始まりをマークし、Alt+H キーボードのショートカットを使用してスポークに移動できるようにします。
クラス定義とクラス属性定義の後に続くのは、通常、クラスのインスタンスを初期化するコンストラクターです。Anaconda グラフィカルインターフェイスオブジェクトの場合、__init__ メソッドと initialize メソッドという 2 つの方法があります。
これら 2 つの関数があるのは、GUI オブジェクトがメモリーで作成される時期とこれが完全に初期化される時期 (長時間かかる場合があります) が別になる可能性があるためです。そのため、__init__ メソッドは親の __init__ メソッドのみを呼び出してから、GUI 以外の属性を初期化します。一方、インストーラーのグラフィカルユーザーインターフェイスの初期化時に呼び出される initialize メソッドは、スポークの完全な初期化を完了する必要があります。
Hello World アドオンの例では、これら 2 つのメソッドは以下のように定義されます (__init__ メソッドに渡される属性の数と記述に注意):

例9 __init__ および initialize メソッドの定義

def __init__(self, data, storage, payload, instclass):
    """
    :see: pyanaconda.ui.common.Spoke.__init__
    :param data: data object passed to every spoke to load/store data
    from/to it
    :type data: pykickstart.base.BaseHandler
    :param storage: object storing storage-related information
    (disks, partitioning, bootloader, etc.)
    :type storage: blivet.Blivet
    :param payload: object storing packaging-related information
    :type payload: pyanaconda.packaging.Payload
    :param instclass: distribution-specific information
    :type instclass: pyanaconda.installclass.BaseInstallClass

    """

    NormalSpoke.__init__(self, data, storage, payload, instclass)

def initialize(self):
    """
    The initialize method that is called after the instance is created.
    The difference between __init__ and this method is that this may take
    a long time and thus could be called in a separated thread.

    :see: pyanaconda.ui.common.UIObject.initialize

    """

    NormalSpoke.initialize(self)
    self._entry = self.builder.get_object("textEntry")
data パラメーターが __init__ メソッドに渡されていることに留意してください。これは、すべてのデータが保存されている Kickstart ファイルのインメモリーのツリー構造を表します。ancestor の __init__ メソッドのいずれかで、self.data 属性に格納されます。これにより、クラス内の他のすべてのメソッドで構造の読み取りおよび修正が可能になります。
なぜならHelloWorldDataクラスはすでに定義されています「キックスタートのサポート」、すでにサブツリーがありますself.dataこのアドオンのルート (クラスのインスタンス) は次のように利用できます。self.data.addons.com_example_hello_world .
親の __init__ は他にも、スポークの .glade ファイルがある GtkBuilder のインスタンスを初期化し、これを self.builder として保存します。これは、kickstart ファイルの %addon セクションからのテキストの表示およびその修正に使用される GtkTextEntry の取得に initialize で使用されます。
__init__ および initialize メソッドは両方とも、スポークの作成時に重要となります。ただし、スポークの主要なロールは、ユーザーがこのスポークで表示、設定される値を変更または見直すことです。これを有効にするには、その他の 3 つの方法を使用できます。
  • refresh: スポークをユーザーが表示する際に呼び出されます。このメソッドはスポークの状態を更新し (主に UI 要素)、self.data 構造に保存されている現行値を表示します。
  • apply: スポークが残っている場合に呼び出され、UI 要素の値を self.data 構造に戻す際に使用されます。
  • execute: スポークが残され、スポークの新しい状態に基づいてランタイム変更を実行する場合に呼び出されます。
これらの関数は、以下のように Hello World アドオンのサンプルに実装されます。

例10 更新、適用、および実行メソッドの定義

def refresh(self):
    """
    The refresh method that is called every time the spoke is displayed.
    It should update the UI elements according to the contents of
    self.data.

    :see: pyanaconda.ui.common.UIObject.refresh

    """

    self._entry.set_text(self.data.addons.org_fedora_hello_world.text)

def apply(self):
    """
    The apply method that is called when the spoke is left. It should
    update the contents of self.data with values set in the GUI elements.

    """

    self.data.addons.org_fedora_hello_world.text = self._entry.get_text()

def execute(self):
  """
  The excecute method that is called when the spoke is left. It is
  supposed to do all changes to the runtime environment according to
  the values set in the GUI elements.

  """

  # nothing to do here
  pass
スポークの状態の管理には、以下のメソッドを使用できます。
  • ready: スポークを表示する準備ができているかどうかを判断します。値が false の場合は、スポークにアクセスできません (例: パッケージソース設定前の Package Selection スポークなど)。
  • completed: スポークが完了しているかどうかを確認します。
  • mandatory: スポークが必須かどうかを判断します (例: 自動パーティション設定を使用する場合でも、インストール先 スポークは表示する必要があります)。
これらの属性はすべて、インストールプロセスの現在の状態に基づいて動的に決定する必要があります。以下は Hello World アドオンでのこれらの属性の実装例です。値によっては HelloWorldData クラスの text 属性で設定する必要があるものもあります。

例11 準備完了、完了、および必須メソッドの定義

@property
def ready(self):
    """
    The ready property that tells whether the spoke is ready (can be visited)
    or not. The spoke is made (in)sensitive based on the returned value.

    :rtype: bool

    """

    # this spoke is always ready
    return True

@property
def completed(self):
    """
    The completed property that tells whether all mandatory items on the
    spoke are set, or not. The spoke will be marked on the hub as completed
    or uncompleted acording to the returned value.

    :rtype: bool

    """

    return bool(self.data.addons.org_fedora_hello_world.text)

@property
def mandatory(self):
    """
    The mandatory property that tells whether the spoke is mandatory to be
    completed to continue in the installation process.

    :rtype: bool

    """

    # this is an optional spoke that is not mandatory to be completed
    return False
これらのプロパティーを定義したら、スポークはアクセスと完全性を制御できますが、その中で設定した値の概要を提供することはできません。これを確認するにはそのスポークを表示する必要がありますが、これは推奨されません。このため、status と呼ばれる別のプロパティーがあり、これには設定した値の簡潔な概要がテキスト 1 行で含まれています。これはハブ内のスポークタイトルの下で表示できます。
status プロパティーは、以下のように Hello World の例のアドオンで定義されます。

例12 status プロパティーの定義

@property
def status(self):
    """
    The status property that is a brief string describing the state of the
    spoke. It should describe whether all values are set and if possible
    also the values themselves. The returned value will appear on the hub
    below the spoke's title.

    :rtype: str

    """

    text = self.data.addons.org_fedora_hello_world.text

    # If --reverse was specified in the kickstart, reverse the text
    if self.data.addons.org_fedora_hello_world.reverse:
        text = text[::-1]

    if text:
        return _("Text set: %s") % text
    else:
        return _("Text not set")
本章にあるプロパティーをすべて定義したら、アドオンはグラフィカルユーザーインターフェイスと Kickstart に完全対応となります。ここでの例は非常に簡素化されたもので、管理機能はないことに注意してください。関数、GUI での対話式スポークの開発には Python Gtk プログラミングの知識が必要になります。
制限のうちで注意すべきものは、各スポークには SpokeWindow ウィジェットのインスタンスのメインウィンドウが必要になるという点です。このウィジェットは、Anaconda に固有の他のウィジェットとともに、anaconda-widgets パッケージにあります。GUI をサポートするアドオンの開発に必要なその他のファイル (Glade 定義など) は、anaconda-widgets-devel パッケージにあります。
グラフィカルインターフェイスサポートモジュールに必要なメソッドがすべて含まれたら、次のセクションに進んでテキストベースのユーザーインターフェイスのサポートを追加するか、「Anaconda アドオンのデプロイおよびテスト」アドオンをテストします。


[4] Anaconda インストーラー用に作成されたカスタムウィジェットである SpokeWindow ウィジェットのインスタンス
Red Hat logoGithubRedditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

© 2024 Red Hat, Inc.