Skip to content

Commit 62096aa

Browse files
author
jef
committed
add widget support for dials and calendar
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13252 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 735afe8 commit 62096aa

7 files changed

+164
-50
lines changed

src/app/attributetable/qgsattributetabledelegate.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ QWidget *QgsAttributeTableDelegate::createEditor(
6969
QgsAttributeTableView *tv = dynamic_cast<QgsAttributeTableView *>( parent->parentWidget() );
7070
w->setMinimumWidth( tv->columnWidth( index.column() ) );
7171

72-
if ( vl->editType( fieldIdx( index ) ) == QgsVectorLayer::FileName )
72+
if ( vl->editType( fieldIdx( index ) ) == QgsVectorLayer::FileName ||
73+
vl->editType( fieldIdx( index ) ) == QgsVectorLayer::Calendar )
7374
{
7475
QLineEdit *le = w->findChild<QLineEdit*>();
7576
le->adjustSize();

src/app/qgsattributetypedialog.cpp

+37-16
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ void QgsAttributeTypeDialog::setPageForEditType( QgsVectorLayer::EditType editTy
217217

218218
case QgsVectorLayer::EditRange:
219219
case QgsVectorLayer::SliderRange:
220+
case QgsVectorLayer::DialRange:
220221
setPage( 2 );
221222
break;
222223

@@ -252,6 +253,9 @@ void QgsAttributeTypeDialog::setPageForEditType( QgsVectorLayer::EditType editTy
252253
case QgsVectorLayer::TextEdit:
253254
setPage( 10 );
254255

256+
case QgsVectorLayer::Calendar:
257+
setPage( 11 );
258+
255259
case QgsVectorLayer::LineEdit:
256260
setPage( 0 );
257261
break;
@@ -297,7 +301,8 @@ void QgsAttributeTypeDialog::setIndex( int index, int editTypeInt )
297301
//calculate min and max for range for this field
298302
if ( mLayer->pendingFields()[index].type() == QVariant::Int )
299303
{
300-
sliderRadioButton->setDisabled( false );
304+
rangeWidget->clear();
305+
rangeWidget->addItems( QStringList() << tr( "Editable" ) << tr( "Slider" ) << tr( "Dial" ) );
301306
int min = INT_MIN;
302307
int max = INT_MAX;
303308
while ( mLayer->nextFeature( f ) )
@@ -319,8 +324,8 @@ void QgsAttributeTypeDialog::setIndex( int index, int editTypeInt )
319324
double dMin = -DBL_MAX;
320325
double dMax = DBL_MAX;
321326

322-
sliderRadioButton->setDisabled( true );
323-
editableRadioButton->setChecked( true );
327+
rangeWidget->clear();
328+
rangeWidget->addItems( QStringList() << tr( "Editable" ) << tr( "Slider" ) );
324329
while ( mLayer->nextFeature( f ) )
325330
{
326331
QVariant val = f.attributeMap()[index];
@@ -386,6 +391,7 @@ void QgsAttributeTypeDialog::setIndex( int index, int editTypeInt )
386391

387392
case QgsVectorLayer::EditRange:
388393
case QgsVectorLayer::SliderRange:
394+
case QgsVectorLayer::DialRange:
389395
{
390396
if ( mLayer->pendingFields()[mIndex].type() != QVariant::Int )
391397
{
@@ -400,9 +406,17 @@ void QgsAttributeTypeDialog::setIndex( int index, int editTypeInt )
400406
stepDoubleSpinBox->setValue( mLayer->range( index ).mStep.toDouble() );
401407
}
402408
if ( editType == QgsVectorLayer::EditRange )
403-
editableRadioButton->setChecked( true );
404-
else //slider range
405-
sliderRadioButton->setChecked( true );
409+
{
410+
rangeWidget->setCurrentIndex( 0 );
411+
}
412+
else if ( editType == QgsVectorLayer::SliderRange )
413+
{
414+
rangeWidget->setCurrentIndex( 1 );
415+
}
416+
else
417+
{
418+
rangeWidget->setCurrentIndex( 2 );
419+
}
406420
}
407421
break;
408422

@@ -418,13 +432,13 @@ void QgsAttributeTypeDialog::setIndex( int index, int editTypeInt )
418432

419433
void QgsAttributeTypeDialog::setPage( int index )
420434
{
421-
this->selectionComboBox->setCurrentIndex( index );
435+
selectionComboBox->setCurrentIndex( index );
422436
setStackPage( index );
423437
}
424438

425439
void QgsAttributeTypeDialog::setStackPage( int index )
426440
{
427-
this->stackedWidget->setCurrentIndex( index );
441+
stackedWidget->setCurrentIndex( index );
428442

429443
bool okDisabled = false;
430444
if ( index == 2 )
@@ -436,15 +450,15 @@ void QgsAttributeTypeDialog::setStackPage( int index )
436450
}
437451
else if ( mLayer->pendingFields()[mIndex].type() != QVariant::Double )
438452
{
439-
this->rangeStackedWidget->setCurrentIndex( 0 );
453+
rangeStackedWidget->setCurrentIndex( 0 );
440454
//load data
441455
minimumSpinBox->setValue( mRangeData.mMin.toInt() );
442456
maximumSpinBox->setValue( mRangeData.mMax.toInt() );
443457
stepSpinBox->setValue( mRangeData.mStep.toInt() );
444458
}
445459
else
446460
{
447-
this->rangeStackedWidget->setCurrentIndex( 1 );
461+
rangeStackedWidget->setCurrentIndex( 1 );
448462
//load data
449463
minimumDoubleSpinBox->setValue( mRangeData.mMin.toDouble() );
450464
maximumDoubleSpinBox->setValue( mRangeData.mMax.toDouble() );
@@ -497,13 +511,17 @@ void QgsAttributeTypeDialog::accept()
497511
stepDoubleSpinBox->value() );
498512
}
499513
//select correct one
500-
if ( editableRadioButton->isChecked() )
501-
{
502-
mEditType = QgsVectorLayer::EditRange;
503-
}
504-
else
514+
switch ( rangeWidget->currentIndex() )
505515
{
506-
mEditType = QgsVectorLayer::SliderRange;
516+
case 0:
517+
mEditType = QgsVectorLayer::EditRange;
518+
break;
519+
case 1:
520+
mEditType = QgsVectorLayer::SliderRange;
521+
break;
522+
case 2:
523+
mEditType = QgsVectorLayer::DialRange;
524+
break;
507525
}
508526
break;
509527
case 3:
@@ -550,6 +568,9 @@ void QgsAttributeTypeDialog::accept()
550568
case 10:
551569
mEditType = QgsVectorLayer::TextEdit;
552570
break;
571+
case 11:
572+
mEditType = QgsVectorLayer::Calendar;
573+
break;
553574
}
554575

555576
QDialog::accept();

src/app/qgsvectorlayerproperties.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ void QgsVectorLayerProperties::attributeTypeDialog( )
271271
break;
272272
case QgsVectorLayer::EditRange:
273273
case QgsVectorLayer::SliderRange:
274+
case QgsVectorLayer::DialRange:
274275
mRanges.insert( index, attributeTypeDialog.rangeData() );
275276
break;
276277
case QgsVectorLayer::CheckBox:
@@ -532,12 +533,14 @@ void QgsVectorLayerProperties::setupEditTypes()
532533
editTypeMap.insert( QgsVectorLayer::ValueMap, tr( "Value map" ) );
533534
editTypeMap.insert( QgsVectorLayer::EditRange, tr( "Edit range" ) );
534535
editTypeMap.insert( QgsVectorLayer::SliderRange, tr( "Slider range" ) );
536+
editTypeMap.insert( QgsVectorLayer::DialRange, tr( "Dial range" ) );
535537
editTypeMap.insert( QgsVectorLayer::FileName, tr( "File name" ) );
536538
editTypeMap.insert( QgsVectorLayer::Enumeration, tr( "Enumeration" ) );
537539
editTypeMap.insert( QgsVectorLayer::Immutable, tr( "Immutable" ) );
538540
editTypeMap.insert( QgsVectorLayer::Hidden, tr( "Hidden" ) );
539541
editTypeMap.insert( QgsVectorLayer::CheckBox, tr( "Checkbox" ) );
540542
editTypeMap.insert( QgsVectorLayer::TextEdit, tr( "Text edit" ) );
543+
editTypeMap.insert( QgsVectorLayer::Calendar, tr( "Calendar" ) );
541544
}
542545

543546
QString QgsVectorLayerProperties::editTypeButtonText( QgsVectorLayer::EditType type )
@@ -604,7 +607,8 @@ void QgsVectorLayerProperties::apply()
604607
}
605608
}
606609
else if ( editType == QgsVectorLayer::EditRange ||
607-
editType == QgsVectorLayer::SliderRange )
610+
editType == QgsVectorLayer::SliderRange ||
611+
editType == QgsVectorLayer::DialRange )
608612
{
609613
if ( mRanges.contains( idx ) )
610614
{
@@ -628,7 +632,6 @@ void QgsVectorLayerProperties::apply()
628632
}
629633
else
630634
{
631-
632635
QgsSingleSymbolDialog *sdialog =
633636
qobject_cast < QgsSingleSymbolDialog * >( widgetStackRenderers->currentWidget() );
634637
QgsGraduatedSymbolDialog *gdialog =

src/core/qgsvectorlayer.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
7575
CheckBox, /* added in 1.4 */
7676
FileName,
7777
Enumeration,
78-
Immutable, /*The attribute value should not be changed in the attribute form*/
79-
Hidden, /*The attribute value should not be shown in the attribute form @added in 1.4 */
80-
TextEdit /*multiline edit @added in 1.4*/
78+
Immutable, /* The attribute value should not be changed in the attribute form*/
79+
Hidden, /* The attribute value should not be shown in the attribute form @added in 1.4 */
80+
TextEdit, /* multiline edit @added in 1.4*/
81+
Calendar, /* calendar widget @added in 1.5 */
82+
DialRange, /* dial range @added in 1.5 */
8183
};
8284

8385
struct RangeData

src/gui/qgsattributeeditor.cpp

+82-11
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
#include <QCompleter>
3434
#include <QHBoxLayout>
3535
#include <QPlainTextEdit>
36+
#include <QDial>
37+
#include <QCalendarWidget>
38+
#include <QDialogButtonBox>
3639

3740
void QgsAttributeEditor::selectFileName( void )
3841
{
@@ -55,6 +58,42 @@ void QgsAttributeEditor::selectFileName( void )
5558
le->setText( fileName );
5659
}
5760

61+
void QgsAttributeEditor::selectDate( void )
62+
{
63+
QPushButton *pb = qobject_cast<QPushButton *>( sender() );
64+
if ( !pb )
65+
return;
66+
67+
QWidget *hbox = qobject_cast<QWidget *>( pb->parent() );
68+
if ( !hbox )
69+
return;
70+
71+
QLineEdit *le = hbox->findChild<QLineEdit *>();
72+
if ( !le )
73+
return;
74+
75+
QDialog *dlg = new QDialog();
76+
dlg->setWindowTitle( tr( "Select a date" ) );
77+
QVBoxLayout *vl = new QVBoxLayout( dlg );
78+
79+
QCalendarWidget *cw = new QCalendarWidget( dlg );
80+
cw->setSelectedDate( QDate::fromString( le->text(), Qt::ISODate ) );
81+
vl->addWidget( cw );
82+
83+
QDialogButtonBox *buttonBox = new QDialogButtonBox( dlg );
84+
buttonBox->addButton( QDialogButtonBox::Ok );
85+
buttonBox->addButton( QDialogButtonBox::Cancel );
86+
vl->addWidget( buttonBox );
87+
88+
connect( buttonBox, SIGNAL( accepted() ), dlg, SLOT( accept() ) );
89+
connect( buttonBox, SIGNAL( rejected() ), dlg, SLOT( reject() ) );
90+
91+
if ( dlg->exec() == QDialog::Accepted )
92+
{
93+
le->setText( cw->selectedDate().toString( Qt::ISODate ) );
94+
}
95+
}
96+
5897
QComboBox *QgsAttributeEditor::comboBox( QWidget *editor, QWidget *parent )
5998
{
6099
QComboBox *cb = NULL;
@@ -180,6 +219,8 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
180219
}
181220
break;
182221

222+
223+
case QgsVectorLayer::DialRange:
183224
case QgsVectorLayer::SliderRange:
184225
case QgsVectorLayer::EditRange:
185226
{
@@ -208,12 +249,20 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
208249
}
209250
else
210251
{
211-
QSlider *sl = NULL;
252+
QAbstractSlider *sl = NULL;
212253

213254
if ( editor )
214-
sl = qobject_cast<QSlider*>( editor );
255+
{
256+
sl = qobject_cast<QAbstractSlider*>( editor );
257+
}
258+
else if ( editType == QgsVectorLayer::DialRange )
259+
{
260+
sl = new QDial( parent );
261+
}
215262
else
263+
{
216264
sl = new QSlider( Qt::Horizontal, parent );
265+
}
217266

218267
if ( sl )
219268
{
@@ -335,6 +384,7 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
335384
break;
336385

337386
case QgsVectorLayer::FileName:
387+
case QgsVectorLayer::Calendar:
338388
{
339389
QPushButton *pb = NULL;
340390
QLineEdit *le = qobject_cast<QLineEdit *>( editor );
@@ -365,18 +415,17 @@ QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *ed
365415
}
366416

367417
if ( pb )
368-
connect( pb, SIGNAL( clicked() ), new QgsAttributeEditor( pb ), SLOT( selectFileName() ) );
418+
{
419+
if ( editType == QgsVectorLayer::FileName )
420+
connect( pb, SIGNAL( clicked() ), new QgsAttributeEditor( pb ), SLOT( selectFileName() ) );
421+
if ( editType == QgsVectorLayer::Calendar )
422+
connect( pb, SIGNAL( clicked() ), new QgsAttributeEditor( pb ), SLOT( selectDate() ) );
423+
}
369424
}
370425
break;
371426

372427
case QgsVectorLayer::Immutable:
373428
return NULL;
374-
375-
}
376-
377-
if ( editType == QgsVectorLayer::Immutable )
378-
{
379-
myWidget->setEnabled( false );
380429
}
381430

382431
setValue( myWidget, vl, idx, value );
@@ -448,7 +497,7 @@ bool QgsAttributeEditor::retrieveValue( QWidget *widget, QgsVectorLayer *vl, int
448497
text = QString::number( sb->value() );
449498
}
450499

451-
QSlider *slider = qobject_cast<QSlider *>( widget );
500+
QAbstractSlider *slider = qobject_cast<QAbstractSlider *>( widget );
452501
if ( slider )
453502
{
454503
text = QString::number( slider->value() );
@@ -467,6 +516,12 @@ bool QgsAttributeEditor::retrieveValue( QWidget *widget, QgsVectorLayer *vl, int
467516
text = ckb->isChecked() ? states.first : states.second;
468517
}
469518

519+
QCalendarWidget *cw = qobject_cast<QCalendarWidget *>( widget );
520+
if ( cw )
521+
{
522+
text = cw->selectedDate().toString();
523+
}
524+
470525
le = widget->findChild<QLineEdit *>();
471526
if ( le )
472527
{
@@ -505,6 +560,20 @@ bool QgsAttributeEditor::retrieveValue( QWidget *widget, QgsVectorLayer *vl, int
505560
}
506561
}
507562
break;
563+
case QVariant::Date:
564+
{
565+
QDate myDateValue = QDate::fromString( text, Qt::ISODate );
566+
if ( myDateValue.isValid() && !text.isEmpty() )
567+
{
568+
value = myDateValue;
569+
modified = true;
570+
}
571+
else if ( modified )
572+
{
573+
value = QVariant( theField.type() );
574+
}
575+
}
576+
break;
508577
default: //string
509578
modified = true;
510579
value = QVariant( text );
@@ -542,6 +611,7 @@ bool QgsAttributeEditor::setValue( QWidget *editor, QgsVectorLayer *vl, int idx,
542611
}
543612
break;
544613

614+
case QgsVectorLayer::DialRange:
545615
case QgsVectorLayer::SliderRange:
546616
case QgsVectorLayer::EditRange:
547617
{
@@ -556,7 +626,7 @@ bool QgsAttributeEditor::setValue( QWidget *editor, QgsVectorLayer *vl, int idx,
556626
}
557627
else
558628
{
559-
QSlider *sl = qobject_cast<QSlider *>( editor );
629+
QAbstractSlider *sl = qobject_cast<QAbstractSlider *>( editor );
560630
if ( sl == NULL )
561631
return false;
562632
sl->setValue( value.toInt() );
@@ -615,6 +685,7 @@ bool QgsAttributeEditor::setValue( QWidget *editor, QgsVectorLayer *vl, int idx,
615685
break;
616686

617687
case QgsVectorLayer::FileName:
688+
case QgsVectorLayer::Calendar:
618689
{
619690
QLineEdit *le = editor->findChild<QLineEdit *>();
620691
if ( le == NULL )

src/gui/qgsattributeeditor.h

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class GUI_EXPORT QgsAttributeEditor : public QObject
4040

4141
public slots:
4242
void selectFileName( void );
43+
void selectDate( void );
4344
};
4445

4546

0 commit comments

Comments
 (0)