1# ------------------------------------------------------------------------------
2# Copyright (c) 2008, Enthought, Inc.
3# All rights reserved.
4#
5#  This software is provided without warranty under the terms of the BSD
6#  license included in LICENSE.txt and may be redistributed only
7#  under the conditions described in the aforementioned license.  The license
8#  is also available online at http://www.enthought.com/licenses/BSD.txt
9#
10#  Thanks for using Enthought open source!
11#
12#  Author: David C. Morrill
13#
14# ------------------------------------------------------------------------------
15""" Defines the editor factory for single-selection enumerations, for all traits
16    user interface toolkits.
17"""
18
19
20
21import os
22import sys
23
24from ..editor_factory import EditorWithListFactory
25
26from traits.api import Any, Range, Enum, Bool
27
28from ..toolkit import toolkit_object
29
30# -------------------------------------------------------------------------
31#  Trait definitions:
32# -------------------------------------------------------------------------
33
34# Supported display modes for a custom style editor
35Mode = Enum("radio", "list")
36
37# Supported display modes for a custom style editor
38CompletionMode = Enum("inline", "popup")
39
40# -------------------------------------------------------------------------
41#  'ToolkitEditorFactory' class:
42# -------------------------------------------------------------------------
43
44
45class ToolkitEditorFactory(EditorWithListFactory):
46    """ Editor factory for enumeration editors.
47    """
48
49    # -------------------------------------------------------------------------
50    #  Trait definitions:
51    # -------------------------------------------------------------------------
52
53    #: (Optional) Function used to evaluate text input:
54    evaluate = Any()
55
56    #: Is user input set on every keystroke (when text input is allowed)?
57    auto_set = Bool(True)
58
59    #: Number of columns to use when displayed as a grid:
60    cols = Range(1, 20)
61
62    #: Display modes supported for a custom style editor:
63    mode = Mode
64
65    #: Completion mode for editors with text-entry (Qt only):
66    completion_mode = CompletionMode
67
68    # -------------------------------------------------------------------------
69    #  'Editor' factory methods:
70    # -------------------------------------------------------------------------
71
72    def _get_custom_editor_class(self):
73        """ Returns the editor class to use for "custom" style views.
74        Overridden to return the editor class for the specified mode.
75        """
76        editor_file_name = os.path.basename(
77            sys.modules[self.__class__.__module__].__file__
78        )
79        try:
80            if self.mode == "radio":
81                return toolkit_object(
82                    editor_file_name.split(".")[0] + ":RadioEditor",
83                    raise_exceptions=True,
84                )
85            else:
86                return toolkit_object(
87                    editor_file_name.split(".")[0] + ":ListEditor",
88                    raise_exceptions=True,
89                )
90        except:
91            return super(ToolkitEditorFactory, self)._get_custom_editor_class()
92
93
94# Define the EnumEditor class.
95EnumEditor = ToolkitEditorFactory
96