Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

"""Widget generators and their signal handlers""" 

from gi.repository import Gtk, GObject, Gdk 

#from lutris.util.log import logger 

from lutris.runners import import_runner 

from lutris import sysoptions 

 

PADDING = 5 

 

 

class Label(Gtk.Label): 

    """ Standardised label for config vboxes""" 

    def __init__(self, message=None): 

        """ Custom init of label """ 

        super(Label, self).__init__(label=message) 

        self.set_alignment(0.1, 0.0) 

        self.set_padding(PADDING, 0) 

        self.set_line_wrap(True) 

 

 

class ConfigBox(Gtk.VBox): 

    """ Dynamically generates a vbox built upon on a python dict. """ 

    def __init__(self, config_type, caller): 

        GObject.GObject.__init__(self) 

        self.set_margin_top(30) 

        self.options = None 

        # Section of the configuration file to save options in. Can be "game", 

        # "runner" or "system" 

        self.config_type = config_type 

        self.caller = caller 

 

    def generate_widgets(self): 

        """ Parses the config dict and generates widget accordingly.""" 

        # Select what data to load based on caller. 

        if self.caller == "system": 

            self.real_config = self.lutris_config.system_config 

        elif self.caller == "runner": 

            self.real_config = self.lutris_config.runner_config 

        elif self.caller == "game": 

            self.real_config = self.lutris_config.game_config 

 

        # Select part of config to load or create it. 

        if self.config_type in self.real_config: 

            config = self.real_config[self.config_type] 

        else: 

            config = self.real_config[self.config_type] = {} 

 

        #Go thru all options. 

        for option in self.options: 

            option_key = option["option"] 

 

            #Load value if there is one. 

            if option_key in config: 

                value = config[option_key] 

            else: 

                value = None 

 

            if value is None and 'default' in option: 

                value = option['default'] 

 

            #Different types of widgets. 

            if option["type"] in ("one_choice", "choice"): 

                self.generate_combobox(option_key, 

                                       option["choices"], 

                                       option["label"], value) 

            elif option["type"] == "bool": 

                self.generate_checkbox(option, value) 

            elif option["type"] == "range": 

                self.generate_range(option_key, 

                                    option["min"], 

                                    option["max"], 

                                    option["label"], value) 

            elif option["type"] == "string": 

                if not 'label' in option: 

                    raise ValueError("Option %s has no label" % option) 

                self.generate_entry(option_key, 

                                    option["label"], value) 

            elif option["type"] == "directory_chooser": 

                self.generate_directory_chooser(option_key, 

                                                option["label"], 

                                                value) 

            elif option["type"] == "file": 

                self.generate_file_chooser(option, value) 

            elif option["type"] == "multiple": 

                self.generate_multiple_file_chooser(option_key, 

                                                    option["label"], value) 

            elif option["type"] == "label": 

                self.generate_label(option["label"]) 

            else: 

                raise ValueError("Unknown widget type %s" % option["type"]) 

            helptext = option.get("help") 

            if helptext: 

                self.generate_label(helptext) 

 

    def generate_label(self, text): 

        """ Generates a simple label. """ 

        label = Label(text) 

        label.show() 

        self.pack_start(label, False, False, PADDING) 

 

    #Checkbox 

    def generate_checkbox(self, option, value=None): 

        """ Generates a checkbox. """ 

        checkbox = Gtk.CheckButton(label=option["label"]) 

        if value: 

            checkbox.set_active(value) 

        checkbox.connect("toggled", self.checkbox_toggle, option['option']) 

        checkbox.set_margin_left(20) 

        checkbox.show() 

        self.pack_start(checkbox, False, False, 0) 

 

    def checkbox_toggle(self, widget, option_name): 

        """ Action for the checkbox's toggled signal.""" 

        self.real_config[self.config_type][option_name] = widget.get_active() 

 

    #Entry 

    def generate_entry(self, option_name, label, value=None): 

        """ Generates an entry box. """ 

        hbox = Gtk.HBox() 

        entry_label = Label(label) 

        entry = Gtk.Entry() 

        if value: 

            entry.set_text(value) 

        entry.connect("changed", self.entry_changed, option_name) 

        hbox.pack_start(entry_label, False, False, 20) 

        hbox.pack_start(entry, True, True, 20) 

        hbox.show_all() 

        self.pack_start(hbox, False, True, PADDING) 

 

    def entry_changed(self, entry, option_name): 

        """ Action triggered for entry 'changed' signal. """ 

        entry_text = entry.get_text() 

        self.real_config[self.config_type][option_name] = entry_text 

 

    #ComboBox 

    def generate_combobox(self, option_name, choices, label, value=None): 

        """ Generates a combobox (drop-down menu). """ 

        hbox = Gtk.HBox() 

        liststore = Gtk.ListStore(str, str) 

        for choice in choices: 

            if type(choice) is str: 

                choice = [choice, choice] 

            liststore.append(choice) 

        combobox = Gtk.ComboBox.new_with_model(liststore) 

        cell = Gtk.CellRendererText() 

        combobox.pack_start(cell, True) 

        combobox.add_attribute(cell, 'text', 0) 

        index = selected_index = -1 

        if value: 

            for choice in choices: 

                if choice[1] == value: 

                    selected_index = index + 1 

                    break 

                index += 1 

        combobox.set_active(selected_index) 

        combobox.connect('changed', self.on_combobox_change, option_name) 

        label = Label(label) 

        hbox.pack_start(label, False, False, 20) 

        hbox.pack_start(combobox, True, True, 20) 

        hbox.show_all() 

        self.pack_start(hbox, False, False, PADDING) 

 

    def on_combobox_change(self, combobox, option): 

        """ Action triggered on combobox 'changed' signal. """ 

        model = combobox.get_model() 

        active = combobox.get_active() 

        if active < 0: 

            return None 

        option_value = model[active][1] 

        self.real_config[self.config_type][option] = option_value 

 

    def generate_range(self, option_name, min_val, max_val, label, value=None): 

        """ Generates a ranged spin button. """ 

        adjustment = Gtk.Adjustment(float(min_val), float(min_val), 

                                    float(max_val), 1, 0, 0) 

        spin_button = Gtk.SpinButton() 

        spin_button.set_adjustment(adjustment) 

        if value: 

            spin_button.set_value(value) 

        spin_button.connect('changed', 

                            self.on_spin_button_changed, option_name) 

        hbox = Gtk.HBox() 

        label = Label(label) 

        hbox.pack_start(label, False, False, 20) 

        hbox.pack_start(spin_button, True, True, 20) 

        hbox.show_all() 

        self.pack_start(hbox, False, True, 5) 

 

    def on_spin_button_changed(self, spin_button, option): 

        """ Action triggered on spin button 'changed' signal """ 

        value = spin_button.get_value_as_int() 

        self.real_config[self.config_type][option] = value 

 

    def generate_file_chooser(self, option, value=None): 

        """Generates a file chooser button to select a file""" 

        option_name = option['option'] 

        label = option['label'] 

        hbox = Gtk.HBox() 

        file_chooser = Gtk.FileChooserButton("Choose a file for %s" % label) 

        file_chooser.set_size_request(200, 30) 

        if 'default_path' in option: 

            config_key = option['default_path'] 

            if config_key in self.lutris_config.config['system']: 

                default_path = self.lutris_config.config['system'][config_key] 

                file_chooser.set_current_folder(default_path) 

 

        file_chooser.set_action(Gtk.FileChooserAction.OPEN) 

        file_chooser.connect("file-set", self.on_chooser_file_set, option_name) 

        if value: 

            file_chooser.unselect_all() 

            file_chooser.select_filename(value) 

        hbox.pack_start(Label(label), False, False, 20) 

        hbox.pack_start(file_chooser, True, True, 20) 

        self.pack_start(hbox, False, True, PADDING) 

 

    def generate_directory_chooser(self, option_name, label, value=None): 

        """Generates a file chooser button to select a directory""" 

        hbox = Gtk.HBox() 

        Gtklabel = Label(label) 

        directory_chooser = Gtk.FileChooserButton( 

            title="Choose a directory for %s" % label 

        ) 

        directory_chooser.set_action(Gtk.FileChooserAction.SELECT_FOLDER) 

        if value: 

            directory_chooser.set_current_folder(value) 

        directory_chooser.connect("file-set", self.on_chooser_file_set, 

                                  option_name) 

        hbox.pack_start(Gtklabel, False, False, 20) 

        hbox.pack_start(directory_chooser, True, True, 20) 

        self.pack_start(hbox, False, True, PADDING) 

 

    def on_chooser_file_set(self, filechooser_widget, option): 

        """ Action triggered on file select dialog 'file-set' signal. """ 

        filename = filechooser_widget.get_filename() 

        self.real_config[self.config_type][option] = filename 

 

    def generate_multiple_file_chooser(self, option_name, label, value=None): 

        """ Generates a multiple file selector. """ 

        hbox = Gtk.HBox() 

        label = Label(label) 

        hbox.pack_start(label, False, False, PADDING) 

        self.files_chooser_dialog = Gtk.FileChooserDialog( 

            title="Select files", 

            parent=self.get_parent_window(), 

            action=Gtk.FileChooserAction.OPEN, 

            buttons=(Gtk.STOCK_CLOSE, Gtk.ResponseType.CLOSE, 

                     Gtk.STOCK_ADD, Gtk.ResponseType.OK) 

        ) 

        self.files_chooser_dialog.set_select_multiple(True) 

        files_chooser_button = Gtk.FileChooserButton(self.files_chooser_dialog) 

        files_chooser_button.connect('file-set', self.add_files_callback, 

                                     option_name) 

        game_path = self.lutris_config.get_path(self.runner_class) 

        if game_path: 

            files_chooser_button.set_current_folder(game_path) 

        if value: 

            files_chooser_button.set_filename(value[0]) 

 

        hbox.pack_start(files_chooser_button, True, True, 0) 

        self.pack_start(hbox, False, True, PADDING) 

        if value: 

            if type(value) == str: 

                self.files = [value] 

            else: 

                self.files = value 

        else: 

            self.files = [] 

        self.files_list_store = Gtk.ListStore(str) 

        for filename in self.files: 

            self.files_list_store.append([filename]) 

        cell_renderer = Gtk.CellRendererText() 

        files_treeview = Gtk.TreeView(self.files_list_store) 

        files_column = Gtk.TreeViewColumn("Files", cell_renderer, text=0) 

        files_treeview.append_column(files_column) 

        files_treeview.connect('key-press-event', self.on_files_treeview_event, 

                               option_name) 

        treeview_scroll = Gtk.ScrolledWindow() 

        treeview_scroll.set_min_content_height(200) 

        treeview_scroll.set_policy(Gtk.PolicyType.AUTOMATIC, 

                                   Gtk.PolicyType.AUTOMATIC) 

        treeview_scroll.add(files_treeview) 

        self.add(treeview_scroll) 

 

    def on_files_treeview_event(self, treeview, event, option): 

        """ Action triggered when a row is deleted from the filechooser. """ 

        key = event.keyval 

        if key == Gdk.KEY_Delete: 

            selection = treeview.get_selection() 

            (model, treepaths) = selection.get_selected_rows() 

            for treepath in treepaths: 

                row_index = int(str(treepath)) 

                treeiter = model.get_iter(treepath) 

                model.remove(treeiter) 

                self.real_config[self.config_type][option].pop(row_index) 

 

    def add_files_callback(self, button, option=None): 

        """Add several files to the configuration""" 

        filenames = button.get_filenames() 

        files = self.real_config[self.config_type].get(option, []) 

        for filename in filenames: 

            self.files_list_store.append([filename]) 

            if not filename in files: 

                files.append(filename) 

        self.real_config[self.config_type][option] = files 

        self.files_chooser_dialog = None 

 

 

class GameBox(ConfigBox): 

    def __init__(self, lutris_config, caller): 

        ConfigBox.__init__(self, "game", caller) 

        self.lutris_config = lutris_config 

        self.lutris_config.config_type = "game" 

        self.runner_class = self.lutris_config.runner 

        runner = import_runner(self.runner_class)() 

 

        self.options = runner.game_options 

        self.generate_widgets() 

 

 

class RunnerBox(ConfigBox): 

    def __init__(self, lutris_config, caller): 

        runner_classname = lutris_config.runner 

        ConfigBox.__init__(self, runner_classname, caller) 

        runner = import_runner(runner_classname)() 

 

        self.options = runner.runner_options 

        self.lutris_config = lutris_config 

        self.generate_widgets() 

 

 

class SystemBox(ConfigBox): 

    def __init__(self, lutris_config, caller): 

        """Box init""" 

        ConfigBox.__init__(self, "system", caller) 

        self.lutris_config = lutris_config 

        self.options = sysoptions.system_options 

        self.generate_widgets()