62
62
QtGui .QTabWidget , QtGui .QApplication , QtGui .QStackedWidget , QtGui .QDateEdit ,
63
63
QtGui .QDateTimeEdit , QtGui .QFont , QtGui .QFontComboBox , QtGui .QFontDatabase ,
64
64
QtGui .QGridLayout , QtGui .QFormLayout , QtGui .QDoubleValidator )
65
-
65
+
66
66
(Qt , SIGNAL , SLOT , QObject , QSize ,pyqtSignature , pyqtProperty ) = \
67
- (QtCore .Qt , QtCore .SIGNAL , QtCore .SLOT , QtCore .QObject , QtCore .QSize ,
68
- QtCore .pyqtSignature , QtCore .pyqtProperty )
67
+ (QtCore .Qt , QtCore .SIGNAL , QtCore .SLOT , QtCore .QObject , QtCore .QSize ,
68
+ QtCore .Slot , QtCore .Property )
69
69
70
70
import datetime
71
71
@@ -74,22 +74,22 @@ class ColorButton(QPushButton):
74
74
Color choosing push button
75
75
"""
76
76
__pyqtSignals__ = ("colorChanged(QColor)" ,)
77
-
77
+
78
78
def __init__ (self , parent = None ):
79
79
QPushButton .__init__ (self , parent )
80
80
self .setFixedSize (20 , 20 )
81
81
self .setIconSize (QSize (12 , 12 ))
82
82
self .connect (self , SIGNAL ("clicked()" ), self .choose_color )
83
83
self ._color = QColor ()
84
-
84
+
85
85
def choose_color (self ):
86
86
color = QColorDialog .getColor (self ._color ,self .parentWidget (),'' )
87
87
if color .isValid ():
88
88
self .set_color (color )
89
-
89
+
90
90
def get_color (self ):
91
91
return self ._color
92
-
92
+
93
93
@pyqtSignature ("QColor" )
94
94
def set_color (self , color ):
95
95
if color != self ._color :
@@ -98,7 +98,7 @@ def set_color(self, color):
98
98
pixmap = QPixmap (self .iconSize ())
99
99
pixmap .fill (color )
100
100
self .setIcon (QIcon (pixmap ))
101
-
101
+
102
102
color = pyqtProperty ("QColor" , get_color , set_color )
103
103
104
104
@@ -146,11 +146,11 @@ def update_color(self, text):
146
146
147
147
def update_text (self , color ):
148
148
self .lineedit .setText (color .name ())
149
-
149
+
150
150
def text (self ):
151
151
return self .lineedit .text ()
152
-
153
-
152
+
153
+
154
154
def font_is_installed (font ):
155
155
"""Check if font is installed"""
156
156
return [fam for fam in QFontDatabase ().families () if unicode (fam )== font ]
@@ -184,12 +184,12 @@ def __init__(self, value, parent=None):
184
184
QGridLayout .__init__ (self )
185
185
font = tuple_to_qfont (value )
186
186
assert font is not None
187
-
187
+
188
188
# Font family
189
189
self .family = QFontComboBox (parent )
190
190
self .family .setCurrentFont (font )
191
191
self .addWidget (self .family , 0 , 0 , 1 , - 1 )
192
-
192
+
193
193
# Font size
194
194
self .size = QComboBox (parent )
195
195
self .size .setEditable (True )
@@ -201,17 +201,17 @@ def __init__(self, value, parent=None):
201
201
self .size .addItems ([str (s ) for s in sizelist ])
202
202
self .size .setCurrentIndex (sizelist .index (size ))
203
203
self .addWidget (self .size , 1 , 0 )
204
-
204
+
205
205
# Italic or not
206
206
self .italic = QCheckBox (self .tr ("Italic" ), parent )
207
207
self .italic .setChecked (font .italic ())
208
208
self .addWidget (self .italic , 1 , 1 )
209
-
209
+
210
210
# Bold or not
211
211
self .bold = QCheckBox (self .tr ("Bold" ), parent )
212
212
self .bold .setChecked (font .bold ())
213
213
self .addWidget (self .bold , 1 , 2 )
214
-
214
+
215
215
def get_font (self ):
216
216
font = self .family .currentFont ()
217
217
font .setItalic (self .italic .isChecked ())
@@ -223,7 +223,7 @@ def get_font(self):
223
223
def is_edit_valid (edit ):
224
224
text = edit .text ()
225
225
state = edit .validator ().validate (text , 0 )[0 ]
226
-
226
+
227
227
return state == QDoubleValidator .Acceptable
228
228
229
229
class FormWidget (QWidget ):
@@ -242,7 +242,7 @@ def __init__(self, data, comment="", parent=None):
242
242
print "*" * 80
243
243
print "COMMENT:" , comment
244
244
print "*" * 80
245
-
245
+
246
246
def get_dialog (self ):
247
247
"""Return FormDialog instance"""
248
248
dialog = self .parent ()
@@ -315,7 +315,7 @@ def setup(self):
315
315
field = QLineEdit (repr (value ), self )
316
316
self .formlayout .addRow (label , field )
317
317
self .widgets .append (field )
318
-
318
+
319
319
def get (self ):
320
320
valuelist = []
321
321
for index , (label , value ) in enumerate (self .data ):
@@ -356,26 +356,26 @@ def __init__(self, datalist, comment="", parent=None):
356
356
self .setLayout (layout )
357
357
self .combobox = QComboBox ()
358
358
layout .addWidget (self .combobox )
359
-
359
+
360
360
self .stackwidget = QStackedWidget (self )
361
361
layout .addWidget (self .stackwidget )
362
362
self .connect (self .combobox , SIGNAL ("currentIndexChanged(int)" ),
363
363
self .stackwidget , SLOT ("setCurrentIndex(int)" ))
364
-
364
+
365
365
self .widgetlist = []
366
366
for data , title , comment in datalist :
367
367
self .combobox .addItem (title )
368
368
widget = FormWidget (data , comment = comment , parent = self )
369
369
self .stackwidget .addWidget (widget )
370
370
self .widgetlist .append (widget )
371
-
371
+
372
372
def setup (self ):
373
373
for widget in self .widgetlist :
374
374
widget .setup ()
375
375
376
376
def get (self ):
377
377
return [ widget .get () for widget in self .widgetlist ]
378
-
378
+
379
379
380
380
class FormTabWidget (QWidget ):
381
381
def __init__ (self , datalist , comment = "" , parent = None ):
@@ -393,11 +393,11 @@ def __init__(self, datalist, comment="", parent=None):
393
393
index = self .tabwidget .addTab (widget , title )
394
394
self .tabwidget .setTabToolTip (index , comment )
395
395
self .widgetlist .append (widget )
396
-
396
+
397
397
def setup (self ):
398
398
for widget in self .widgetlist :
399
399
widget .setup ()
400
-
400
+
401
401
def get (self ):
402
402
return [ widget .get () for widget in self .widgetlist ]
403
403
@@ -409,7 +409,7 @@ def __init__(self, data, title="", comment="",
409
409
QDialog .__init__ (self , parent )
410
410
411
411
self .apply_callback = apply
412
-
412
+
413
413
# Form
414
414
if isinstance (data [0 ][0 ], (list , tuple )):
415
415
self .formwidget = FormTabWidget (data , comment = comment ,
@@ -418,11 +418,11 @@ def __init__(self, data, title="", comment="",
418
418
self .formwidget = FormComboWidget (data , comment = comment ,
419
419
parent = self )
420
420
else :
421
- self .formwidget = FormWidget (data , comment = comment ,
421
+ self .formwidget = FormWidget (data , comment = comment ,
422
422
parent = self )
423
423
layout = QVBoxLayout ()
424
424
layout .addWidget (self .formwidget )
425
-
425
+
426
426
self .float_fields = []
427
427
self .formwidget .setup ()
428
428
@@ -439,15 +439,15 @@ def __init__(self, data, title="", comment="",
439
439
layout .addWidget (bbox )
440
440
441
441
self .setLayout (layout )
442
-
442
+
443
443
self .setWindowTitle (title )
444
444
if not isinstance (icon , QIcon ):
445
445
icon = QWidget ().style ().standardIcon (QStyle .SP_MessageBoxQuestion )
446
446
self .setWindowIcon (icon )
447
-
447
+
448
448
def register_float_field (self , field ):
449
449
self .float_fields .append (field )
450
-
450
+
451
451
def update_buttons (self ):
452
452
valid = True
453
453
for field in self .float_fields :
@@ -457,18 +457,18 @@ def update_buttons(self):
457
457
btn = self .bbox .button (btn_type )
458
458
if btn is not None :
459
459
btn .setEnabled (valid )
460
-
460
+
461
461
def accept (self ):
462
462
self .data = self .formwidget .get ()
463
463
QDialog .accept (self )
464
-
464
+
465
465
def reject (self ):
466
466
self .data = None
467
467
QDialog .reject (self )
468
-
468
+
469
469
def apply (self ):
470
470
self .apply_callback (self .formwidget .get ())
471
-
471
+
472
472
def get (self ):
473
473
"""Return form result"""
474
474
return self .data
@@ -478,22 +478,22 @@ def fedit(data, title="", comment="", icon=None, parent=None, apply=None):
478
478
"""
479
479
Create form dialog and return result
480
480
(if Cancel button is pressed, return None)
481
-
481
+
482
482
data: datalist, datagroup
483
483
title: string
484
484
comment: string
485
485
icon: QIcon instance
486
486
parent: parent QWidget
487
487
apply: apply callback (function)
488
-
488
+
489
489
datalist: list/tuple of (field_name, field_value)
490
490
datagroup: list/tuple of (datalist *or* datagroup, title, comment)
491
-
491
+
492
492
-> one field for each member of a datalist
493
493
-> one tab for each member of a top-level datagroup
494
494
-> one page (of a multipage widget, each page can be selected with a combo
495
495
box) for each member of a datagroup inside a datagroup
496
-
496
+
497
497
Supported types for field_value:
498
498
- int, float, str, unicode, bool
499
499
- colors: in Qt-compatible text form, i.e. in hex format or name (red,...)
@@ -502,12 +502,12 @@ def fedit(data, title="", comment="", icon=None, parent=None, apply=None):
502
502
* the first element will be the selected index (or value)
503
503
* the other elements can be couples (key, value) or only values
504
504
"""
505
-
505
+
506
506
# Create a QApplication instance if no instance currently exists
507
507
# (e.g. if the module is used directly from the interpreter)
508
508
if QApplication .startingUp ():
509
509
_app = QApplication ([])
510
-
510
+
511
511
dialog = FormDialog (data , title , comment , icon , parent , apply )
512
512
if dialog .exec_ ():
513
513
return dialog .get ()
@@ -531,25 +531,25 @@ def create_datalist_example():
531
531
('date' , datetime .date (2010 , 10 , 10 )),
532
532
('datetime' , datetime .datetime (2010 , 10 , 10 )),
533
533
]
534
-
534
+
535
535
def create_datagroup_example ():
536
536
datalist = create_datalist_example ()
537
537
return ((datalist , "Category 1" , "Category 1 comment" ),
538
538
(datalist , "Category 2" , "Category 2 comment" ),
539
539
(datalist , "Category 3" , "Category 3 comment" ))
540
-
540
+
541
541
#--------- datalist example
542
542
datalist = create_datalist_example ()
543
543
def apply_test (data ):
544
544
print "data:" , data
545
545
print "result:" , fedit (datalist , title = "Example" ,
546
546
comment = "This is just an <b>example</b>." ,
547
547
apply = apply_test )
548
-
548
+
549
549
#--------- datagroup example
550
550
datagroup = create_datagroup_example ()
551
551
print "result:" , fedit (datagroup , "Global title" )
552
-
552
+
553
553
#--------- datagroup inside a datagroup example
554
554
datalist = create_datalist_example ()
555
555
datagroup = create_datagroup_example ()
0 commit comments