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

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

from PySide.QtGui import QDialog, QFileDialog, QDialogButtonBox, QColorDialog 

from PySide.QtGui import QColor, QFont, QMessageBox 

from pySUMOQt.Designer.NewOntologyDialog import Ui_NewOntologyDialog 

import os 

from pysumo.syntaxcontroller import Ontology 

from pySUMOQt.Designer.OpenRemoteOntologyDialog import Ui_OpenRemoteOntologyDialog 

from pysumo import updater 

from pySUMOQt.Designer.OptionDialog import Ui_Dialog 

from functools import partial 

from pySUMOQt.Designer import OntologyPropertyDialog 

 

 

class NewOntologyDialog(QDialog, Ui_NewOntologyDialog): 

    """ Dialog to create a new ontology. """ 

 

    def __init__(self, parent, defPath): 

        """  

        Initializes the new ontology dialog.  

         

        Parameter : 

         

        - parent : The main window. 

        - defPath : The default output path as a string. 

        """ 

        super(NewOntologyDialog, self).__init__(parent) 

        self.setupUi(self) 

        self.defPath = defPath 

        self.ontologyPath.setText(self.defPath) 

        self.browseFolderBtn.clicked.connect(self.chooseOntologyPath) 

        restoreDefsBtn = self.buttonBox.button(QDialogButtonBox.RestoreDefaults) 

        restoreDefsBtn.clicked.connect(self.restoreDefaults) 

 

    def chooseOntologyPath(self): 

        """ 

        Chooses a path in from the QFileDialog. 

        """ 

        path = self.ontologyPath.text() 

        path = QFileDialog.getExistingDirectory(self, 'Choose Directory', path) 

        self.ontologyPath.setText(path) 

 

    def restoreDefaults(self): 

        """ 

        Restore default values in fields. 

        """ 

        self.ontologyPath.setText(self.defPath) 

 

    def accept(self): 

        """  

        Commit the input and dispose the dialog. 

        """ 

        path = self.ontologyPath.text() 

        if not os.path.exists(path): 

            os.makedirs(path) 

        path = ''.join([path, '/', self.ontologyName.text(), '.kif']) 

        path = os.path.normpath(path) 

 

        # create the ontology file. 

        try: 

            with open(path, 'x') as f: 

                f.close() 

        except FileExistsError: 

            ret = QMessageBox.warning(self, "The ontology file already exists.", "Do you want to override the existing ontology file?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No) 

            if ret == QMessageBox.Yes : 

                with open(path, 'w') as f: 

                    f.close() 

            elif ret == QMessageBox.No : 

                return 

            else : 

                raise RuntimeError 

        ontology = Ontology(path, name=self.ontologyName.text()) 

        self.parent().addOntology(ontology) 

        super(NewOntologyDialog, self).accept() 

 

class OpenRemoteOntologyDialog(QDialog, Ui_OpenRemoteOntologyDialog): 

    """ The dialog to open a remote ontology. """ 

 

    def __init__(self, parent, defPath): 

        """  

        Initializes the open remote ontology dialog. 

         

        Parameter : 

         

        - parent : The main window. 

        - defPath : The default output path. 

        """ 

        super(OpenRemoteOntologyDialog, self).__init__(parent) 

        self.setupUi(self) 

        self.defPath = defPath 

        self.path.setText(self.defPath) 

        self.browseBtn.clicked.connect(self.chooseOntologyPath) 

        restoreDefsBtn = self.buttonBox.button(QDialogButtonBox.RestoreDefaults) 

        restoreDefsBtn.clicked.connect(self.restoreDefaults) 

 

    def chooseOntologyPath(self): 

        """ 

        Choose a path from the QFileDialog. 

        """ 

        path = self.path.text() 

        path = QFileDialog.getExistingDirectory(self, 'Choose Directory', path) 

        self.path.setText(path) 

 

    def restoreDefaults(self): 

        """ 

        Restore default values in fields. 

        """ 

        self.path.setText(self.defPath) 

 

    def accept(self): 

        """ 

        Commit the input and dispose the dialog. 

        """ 

        path = self.path.text() 

        if not os.path.exists(path) : 

            os.makedirs(path) 

        path += "/" 

        path += self.name.text() 

        path += ".kif" 

        path = os.path.normpath(path) 

 

        # create the ontology file. 

        try: 

            with open(path, 'x') as f: 

                f.close() 

        except FileExistsError: 

            ret = QMessageBox.warning(self, "The ontology file already exists.", "Do you want to override the existing ontology file?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No) 

            if ret == QMessageBox.Yes : 

                with open(path, 'w') as f: 

                    f.close() 

            elif ret == QMessageBox.No : 

                return 

            else : 

                raise RuntimeError 

        ontology = Ontology(path, name=self.name.text(), url=self.url.text()) 

        # download the ontology (user must save to store ontology on disk) 

        updater.update(ontology, lambda x: self.parent().addOntology(ontology, newversion=x.getvalue().decode('utf8'))) 

        super(OpenRemoteOntologyDialog, self).accept() 

 

class OntologyPropertyDialog(QDialog, OntologyPropertyDialog.Ui_Dialog): 

    """ 

    The ontology property dialog. 

    """ 

 

    def __init__(self, parent, ontology): 

        """ 

        Initializes an ontology property dialog. 

         

        Parameter : 

         

        - parent : The main window. 

        - ontology : The ontology. 

        """ 

 

        super(OntologyPropertyDialog, self).__init__(parent) 

        self.setupUi(self) 

        self.ontologyName.setText(ontology.name) 

        self.ontologyPath.setText(ontology.path) 

        self.ontologyUrl.setText(ontology.url) 

        self.ontologyLogPath.setText(ontology.action_log.log_io.path) 

 

class HelpDialog(QDialog): 

 

    """ The help dialog for the pySUMO main window. It contains information 

    about Ontologies, SUMO and pySUMO such as the pySUMO API reference and the 

    homepage for SUMO.  It can display both locally stored documentation as 

    well as documentation retrieved from the internet.  An about box displays 

    general information about pySUMO's authors and the license. """ 

 

    def __init__(self): 

        """ Initializes the help dialog. """ 

        pass 

 

    def initView(self): 

        """ Initializes the view of the help dialog. """ 

        pass 

 

def str_to_bool(s): 

    """ Convert a string value of a boolean in boolean. 

     

    Parameter :  

     

    - s : A boolean as String. 

    """ 

    s = str(s) 

    if s.lower() == 'true': 

        return True 

    elif s.lower() == 'false': 

        return False 

    else: 

        raise ValueError 

 

class OptionDialog(QDialog, Ui_Dialog): 

    """ The option dialog is the displays and allows modification of settings 

    for pySUMO. It displays options for the GUI, Widgets and library. The 

    settings are organized by type and owner for ease of use. It also 

    contains a plugin manager which enables loading and unloading of 

    plugins.  The class also provides settings persistence by writing 

    storing them in a file and reading from it on init. 

 

    Attributes: 

 

    - options: The options dictionary to manage in the option's dialog. 

 

    Methods: 

 

    - createView: Creates the view of the options dialog. 

    - save: Saves the options. 

    - load: Loads the options. 

 

    """ 

 

    def __init__(self, parent, settings): 

        """ Initializes the OptionDialog. """ 

        super(OptionDialog, self).__init__(parent) 

        self.setupUi(self) 

        self.settings = settings 

        self.changes = dict() 

        self.initialize() 

        self.configPath.textChanged.connect(partial(self.onOptionChanged, self.configPath)) 

        self.configPathChooser.clicked.connect(partial(self.directoryPathChooserClicked, self.configPath)) 

        self.maxQueueSize.valueChanged.connect(partial(self.onOptionChanged, self.maxQueueSize)) 

        self.maxUndoRedoQueueSize.valueChanged.connect(partial(self.onOptionChanged, self.maxUndoRedoQueueSize)) 

        self.flushWriteQueuesTimeout.valueChanged.connect(partial(self.onOptionChanged, self.flushWriteQueuesTimeout)) 

        self.logOutputPath.textChanged.connect(partial(self.onOptionChanged, self.logOutputPath)) 

        self.socketOutputPath.textChanged.connect(partial(self.onOptionChanged, self.socketOutputPath)) 

        self.logLevel.valueChanged.connect(partial(self.onOptionChanged, self.logLevel)) 

        self.keywordsFontFamily.currentFontChanged.connect(partial(self.onOptionChanged, self.keywordsFontFamily)) 

        self.keywordsFontSize.valueChanged.connect(partial(self.onOptionChanged, self.keywordsFontSize)) 

        self.keywordsFontColor.textChanged.connect(partial(self.onOptionChanged, self.keywordsFontColor)) 

        self.keywordsFontColorChooser.clicked.connect(partial(self.colorChooserClicked, self.keywordsFontColor)) 

        self.keywordsBoldStyle.toggled.connect(partial(self.onOptionChanged, self.keywordsBoldStyle)) 

        self.keywordsItalicStyle.toggled.connect(partial(self.onOptionChanged, self.keywordsItalicStyle)) 

        self.keywordsUnderlinedStyle.toggled.connect(partial(self.onOptionChanged, self.keywordsUnderlinedStyle)) 

        self.logicExprFontFamily.currentFontChanged.connect(partial(self.onOptionChanged, self.logicExprFontFamily)) 

        self.logicExprFontSize.valueChanged.connect(partial(self.onOptionChanged, self.logicExprFontSize)) 

        self.logicExprFontColor.textChanged.connect(partial(self.onOptionChanged, self.logicExprFontColor)) 

        self.logicExprFontColorChooser.clicked.connect(partial(self.colorChooserClicked, self.logicExprFontColor)) 

        self.logicExprBoldStyle.toggled.connect(partial(self.onOptionChanged, self.logicExprBoldStyle)) 

        self.logicExprItalicStyle.toggled.connect(partial(self.onOptionChanged, self.logicExprItalicStyle)) 

        self.logicExprUnderlinedStyle.toggled.connect(partial(self.onOptionChanged, self.logicExprUnderlinedStyle)) 

        self.commentFontFamily.currentFontChanged.connect(partial(self.onOptionChanged, self.commentFontFamily)) 

        self.commentFontSize.valueChanged.connect(partial(self.onOptionChanged, self.commentFontSize)) 

        self.commentFontColor.textChanged.connect(partial(self.onOptionChanged, self.commentFontColor)) 

        self.commentFontColorChooser.clicked.connect(partial(self.colorChooserClicked, self.commentFontColor)) 

        self.commentBoldStyle.toggled.connect(partial(self.onOptionChanged, self.commentBoldStyle)) 

        self.commentItalicStyle.toggled.connect(partial(self.onOptionChanged, self.commentItalicStyle)) 

        self.commentUnderlinedStyle.toggled.connect(partial(self.onOptionChanged, self.commentUnderlinedStyle)) 

        self.stringsFontFamily.currentFontChanged.connect(partial(self.onOptionChanged, self.stringsFontFamily)) 

        self.stringsFontSize.valueChanged.connect(partial(self.onOptionChanged, self.stringsFontSize)) 

        self.stringsFontColor.textChanged.connect(partial(self.onOptionChanged, self.stringsFontColor)) 

        self.stringsFontColorChooser.clicked.connect(partial(self.colorChooserClicked, self.stringsFontColor)) 

        self.stringsBoldStyle.toggled.connect(partial(self.onOptionChanged, self.stringsBoldStyle)) 

        self.stringsItalicStyle.toggled.connect(partial(self.onOptionChanged, self.stringsItalicStyle)) 

        self.stringsUnderlinedStyle.toggled.connect(partial(self.onOptionChanged, self.stringsUnderlinedStyle)) 

        self.maxTextEditorWidgets.valueChanged.connect(partial(self.onOptionChanged, self.maxTextEditorWidgets)) 

        self.maxDocumentationWidgets.valueChanged.connect(partial(self.onOptionChanged, self.maxDocumentationWidgets)) 

        self.maxHierarchyWidgets.valueChanged.connect(partial(self.onOptionChanged, self.maxHierarchyWidgets)) 

        self.maxGraphWidgets.valueChanged.connect(partial(self.onOptionChanged, self.maxGraphWidgets)) 

        self.defaultFontFamily.currentFontChanged.connect(partial(self.onOptionChanged, self.defaultFontFamily)) 

        self.defaultFontSize.valueChanged.connect(partial(self.onOptionChanged, self.defaultFontSize)) 

        self.defaultFontColor.textChanged.connect(partial(self.onOptionChanged, self.defaultFontColor)) 

        self.defaultFontColorChooser.clicked.connect(partial(self.colorChooserClicked, self.defaultFontColor)) 

        self.useHighlightingFontSize.toggled.connect(partial(self.onOptionChanged, self.useHighlightingFontSize)) 

        button = self.buttonBox.button(QDialogButtonBox.Apply) 

        button.clicked.connect(self.onApplyClicked) 

        button = self.buttonBox.button(QDialogButtonBox.Reset) 

        button.clicked.connect(self.onResetClicked) 

        button = self.buttonBox.button(QDialogButtonBox.RestoreDefaults) 

        button.clicked.connect(self.onRestoreDefaultsClicked) 

 

    def initialize(self): 

        """ 

        Fill the dialog fields with the value from the setting file. 

        """ 

        self.loadTextSetting(self.configPath) 

        self.loadIntSetting(self.maxQueueSize) 

        self.loadIntSetting(self.maxUndoRedoQueueSize) 

        self.loadIntSetting(self.flushWriteQueuesTimeout) 

        self.loadTextSetting(self.logOutputPath) 

        self.loadTextSetting(self.socketOutputPath) 

        self.loadIntSetting(self.logLevel) 

        self.loadIntSetting(self.maxTextEditorWidgets) 

        self.loadIntSetting(self.defaultFontSize) 

        self.loadComboBoxSetting(self.defaultFontFamily) 

        self.loadColorSetting(self.defaultFontColor) 

        self.loadComboBoxSetting(self.keywordsFontFamily) 

        self.loadIntSetting(self.keywordsFontSize) 

        self.loadColorSetting(self.keywordsFontColor) 

        self.loadBoolSetting(self.keywordsBoldStyle) 

        self.loadBoolSetting(self.keywordsItalicStyle) 

        self.loadBoolSetting(self.keywordsUnderlinedStyle) 

        self.loadComboBoxSetting(self.logicExprFontFamily) 

        self.loadIntSetting(self.logicExprFontSize) 

        self.loadColorSetting(self.logicExprFontColor) 

        self.loadBoolSetting(self.logicExprBoldStyle) 

        self.loadBoolSetting(self.logicExprItalicStyle) 

        self.loadBoolSetting(self.logicExprUnderlinedStyle) 

        self.loadComboBoxSetting(self.commentFontFamily) 

        self.loadIntSetting(self.commentFontSize) 

        self.loadColorSetting(self.commentFontColor) 

        self.loadBoolSetting(self.commentBoldStyle) 

        self.loadBoolSetting(self.commentItalicStyle) 

        self.loadBoolSetting(self.commentUnderlinedStyle) 

        self.loadComboBoxSetting(self.stringsFontFamily) 

        self.loadIntSetting(self.stringsFontSize) 

        self.loadColorSetting(self.stringsFontColor) 

        self.loadBoolSetting(self.stringsBoldStyle) 

        self.loadBoolSetting(self.stringsItalicStyle) 

        self.loadBoolSetting(self.stringsUnderlinedStyle) 

        self.loadIntSetting(self.maxDocumentationWidgets) 

        self.loadIntSetting(self.maxHierarchyWidgets) 

        self.loadIntSetting(self.maxGraphWidgets) 

        self.loadBoolSetting(self.useHighlightingFontSize) 

 

    def onApplyClicked(self): 

        """ 

        QT Slot to handle the click on apply button. 

        """ 

        self.save() 

 

    def onResetClicked(self): 

        """ 

        QT Slot to handle the click on reset button. 

        """ 

        self.changes.clear() 

        self.initialize() 

 

    def onRestoreDefaultsClicked(self): 

        """ 

        QT Slot to handle click on restore defaults button. 

        """ 

        self.changes.clear() 

        self.settings.loadDefaults() 

        self.initialize() 

 

    def loadBoolSetting(self, checkbox): 

        """  

        Load setting in the checkbox. 

         

        Parameter : 

         

        - checkbox : The QCheckBox where to load the setting with the object name as property key. 

        """ 

        checkbox.setChecked(str_to_bool(self.settings.value(checkbox.objectName()))) 

 

    def loadComboBoxSetting(self, combobox): 

        """ 

        Load setting in the combo box. 

         

        Parameter : 

         

        - combobox : The QComboBox where to load the setting with the object name as property key. 

        """ 

        idx = combobox.findText(self.settings.value(combobox.objectName())) 

        combobox.setCurrentIndex(idx) 

 

    def loadColorSetting(self, textfield): 

        """ 

        Load color setting in the text field. 

         

        Parameter : 

         

        - textfield : The QLineEdit where to load the setting with the object name as property key. 

        """ 

        self.updateColorField(textfield, self.settings.value(textfield.objectName())) 

 

    def loadIntSetting(self, spinbox): 

        """ 

        Load integer setting in the spinbox. 

         

        Parameter : 

         

        - spinbox : The QSpinBox where to load the setting with the object name as property. 

        """ 

        spinbox.setValue(int(self.settings.value(spinbox.objectName()))) 

 

    def loadTextSetting(self, textField): 

        """ 

        Load text setting in the textfield. 

         

        Parameter : 

         

        - textfield : The QLineEdit where to load the setting with the object name as property. 

        """ 

        textField.setText(self.settings.value(textField.objectName())) 

 

    def colorChooserClicked(self, textfield): 

        """ 

        QT Slot handles when a color chooser is clicked. 

         

        Parameter : 

         

        - textfield : The QLineEdit where to output the name of the choosen color. 

        """ 

        color = textfield.text() 

        colorChooser = QColorDialog(self) 

        if color : 

            colorChooser.setCurrentColor(QColor(color)) 

        if colorChooser.exec_() : 

            ret = colorChooser.currentColor() 

            if ret is not None and ret.isValid() : 

                color = ret.name() 

        self.updateColorField(textfield, color) 

 

    def updateColorField(self, textfield, color): 

        """ 

        Update the color field by seting it's text color with the given color. 

         

        Parameter : 

         

        - textfield : The QLineEdit to output the choosen color. 

        - color :  The color name. 

        """ 

        stylesheet = "QLineEdit { color: "; 

        stylesheet += color 

        stylesheet += "}" 

        textfield.setText(color) 

        textfield.setStyleSheet(stylesheet) 

 

    def directoryPathChooserClicked(self, textfield): 

        """  

        QT Slot handles when the directory chooser is clicked. 

         

        Parameter :  

         

        - textfield : The QLineEdit where to output the choosen path. 

        """ 

        path = textfield.text() 

        path = QFileDialog.getExistingDirectory(self, "Select Folder", path) 

        textfield.setText(path) 

 

    def onOptionChanged(self, qItem, newValue): 

        """ 

        QT Slot handles when an option field changed. 

         

        Parameter : 

         

        - qItem : The QWidget which changed, which the object name as property key. 

        - newValue : The new value of the changed option. 

        """ 

        optionName = qItem.objectName() 

        if type(newValue) is QFont : 

            newValue = newValue.family() 

        oldValue = None 

        try : 

            oldValue = self.changes.pop(optionName) 

        except KeyError : 

            pass 

        if newValue == oldValue : 

            return 

        self.changes[optionName] = newValue 

 

    def accept(self, *args, **kwargs): 

        """ 

        Commit changes and disposes the dialog. 

        """ 

        # Save the options. 

        self.save() 

        return QDialog.accept(self, *args, **kwargs) 

 

    def setSelectedPage(self, pageIndex): 

        """ 

        QT Slot handles when a page is selected. 

         

        Parameter : 

         

        - pageIndex : The index of the page selected. 

        """ 

        self.listWidget.setCurrentRow(pageIndex) 

 

    def changePage(self, current, previous): 

        """ 

        QT Slot handles when a page is selected. 

         

        Parameter : 

         

        - current : The index of the page selected. 

        - previous : The index of the old selected page. 

        """ 

        if not current is None : 

            self.stackedWidget.setCurrentIndex(self.listWidget.row(current)) 

 

    def save(self): 

        """ Saves the settings to the given path. 

 

        Arguments: 

 

        - path: The path to which the settings will be written. 

 

        Raises: 

 

        - IOError 

 

        """ 

        for key in self.changes.keys() : 

            self.settings.setValue(key, self.changes.get(key)) 

        self.changes.clear()