30
30
#include < QLabel>
31
31
#include < QFrame>
32
32
#include < QScrollArea>
33
+ #include < QFile>
34
+ #include < QDialogButtonBox>
35
+ #include < QUiLoader>
36
+ #include < QDialog>
37
+ #include < QVBoxLayout>
33
38
34
39
QgsAttributeDialog::QgsAttributeDialog ( QgsVectorLayer *vl, QgsFeature *thepFeature )
35
- : QDialog( ),
40
+ : mDialog( 0 ),
36
41
mSettingsPath( " /Windows/AttributeDialog/" ),
37
42
mLayer( vl ),
38
43
mpFeature( thepFeature )
39
44
{
40
- setupUi ( this );
41
45
if ( mpFeature == NULL || vl->dataProvider () == NULL )
42
46
return ;
43
47
44
48
const QgsFieldMap &theFieldMap = vl->pendingFields ();
45
-
46
49
if ( theFieldMap.isEmpty () )
47
50
return ;
48
51
49
52
QgsAttributeMap myAttributes = mpFeature->attributeMap ();
50
- //
51
- // Set up dynamic inside a scroll box
52
- //
53
- QVBoxLayout * mypOuterLayout = new QVBoxLayout ();
54
- mypOuterLayout->setContentsMargins ( 0 , 0 , 0 , 0 );
55
- // transfers layout ownership so no need to call delete
56
- mFrame ->setLayout ( mypOuterLayout );
57
- QScrollArea * mypScrollArea = new QScrollArea ();
58
- // transfers scroll area ownership so no need to call delete
59
- mypOuterLayout->addWidget ( mypScrollArea );
60
- QFrame * mypInnerFrame = new QFrame ();
61
- mypInnerFrame->setFrameShape ( QFrame::NoFrame );
62
- mypInnerFrame->setFrameShadow ( QFrame::Plain );
63
- // transfers frame ownership so no need to call delete
64
- mypScrollArea->setWidget ( mypInnerFrame );
65
- mypScrollArea->setWidgetResizable ( true );
66
- QGridLayout * mypInnerLayout = new QGridLayout ( mypInnerFrame );
67
-
68
- int index = 0 ;
69
- for ( QgsAttributeMap::const_iterator it = myAttributes.begin ();
70
- it != myAttributes.end ();
71
- ++it )
72
- {
73
- const QgsField &field = theFieldMap[it.key ()];
74
53
75
- // show attribute alias if available
76
- QString myFieldName = vl->attributeDisplayName ( it.key () );
77
- int myFieldType = field.type ();
54
+ QDialogButtonBox *buttonBox = NULL ;
78
55
79
- QWidget *myWidget = QgsAttributeEditor::createAttributeEditor ( 0 , vl, it.key (), it.value () );
80
- if ( !myWidget )
81
- continue ;
56
+ if ( !vl->editForm ().isEmpty () )
57
+ {
58
+ QFile file ( vl->editForm () );
59
+ file.open ( QFile::ReadOnly );
60
+ QUiLoader loader;
61
+ QWidget *myWidget = loader.load ( &file, NULL );
62
+ file.close ();
63
+
64
+ mDialog = qobject_cast<QDialog*>( myWidget );
65
+ buttonBox = myWidget->findChild <QDialogButtonBox*>();
66
+ }
82
67
83
- QLabel * mypLabel = new QLabel ();
84
- mypInnerLayout->addWidget ( mypLabel, index , 0 );
85
- if ( myFieldType == QVariant::Int )
68
+ if ( !mDialog )
69
+ {
70
+ mDialog = new QDialog ();
71
+
72
+ QGridLayout *gridLayout;
73
+ QFrame *mFrame ;
74
+
75
+ if ( mDialog ->objectName ().isEmpty () )
76
+ mDialog ->setObjectName ( QString::fromUtf8 ( " QgsAttributeDialogBase" ) );
77
+
78
+ mDialog ->resize ( 447 , 343 );
79
+ gridLayout = new QGridLayout ( mDialog );
80
+ gridLayout->setSpacing ( 6 );
81
+ gridLayout->setMargin ( 11 );
82
+ gridLayout->setObjectName ( QString::fromUtf8 ( " gridLayout" ) );
83
+ mFrame = new QFrame ( mDialog );
84
+ mFrame ->setObjectName ( QString::fromUtf8 ( " mFrame" ) );
85
+ mFrame ->setFrameShape ( QFrame::StyledPanel );
86
+ mFrame ->setFrameShadow ( QFrame::Raised );
87
+
88
+ gridLayout->addWidget ( mFrame , 0 , 0 , 1 , 1 );
89
+
90
+ buttonBox = new QDialogButtonBox ( mDialog );
91
+ buttonBox->setObjectName ( QString::fromUtf8 ( " buttonBox" ) );
92
+ gridLayout->addWidget ( buttonBox, 2 , 0 , 1 , 1 );
93
+
94
+ //
95
+ // Set up dynamic inside a scroll box
96
+ //
97
+ QVBoxLayout * mypOuterLayout = new QVBoxLayout ();
98
+ mypOuterLayout->setContentsMargins ( 0 , 0 , 0 , 0 );
99
+ // transfers layout ownership so no need to call delete
100
+
101
+ mFrame ->setLayout ( mypOuterLayout );
102
+ QScrollArea * mypScrollArea = new QScrollArea ();
103
+ // transfers scroll area ownership so no need to call delete
104
+ mypOuterLayout->addWidget ( mypScrollArea );
105
+ QFrame *mypInnerFrame = new QFrame ();
106
+ mypInnerFrame->setFrameShape ( QFrame::NoFrame );
107
+ mypInnerFrame->setFrameShadow ( QFrame::Plain );
108
+ // transfers frame ownership so no need to call delete
109
+ mypScrollArea->setWidget ( mypInnerFrame );
110
+ mypScrollArea->setWidgetResizable ( true );
111
+ QGridLayout * mypInnerLayout = new QGridLayout ( mypInnerFrame );
112
+
113
+ int index = 0 ;
114
+ for ( QgsAttributeMap::const_iterator it = myAttributes.begin (); it != myAttributes.end (); ++it )
86
115
{
87
- mypLabel->setText ( myFieldName + tr ( " (int)" ) );
116
+ const QgsField &field = theFieldMap[it.key ()];
117
+
118
+ // show attribute alias if available
119
+ QString myFieldName = vl->attributeDisplayName ( it.key () );
120
+ int myFieldType = field.type ();
121
+
122
+ QWidget *myWidget = QgsAttributeEditor::createAttributeEditor ( 0 , 0 , vl, it.key (), it.value () );
123
+ if ( !myWidget )
124
+ continue ;
125
+
126
+ QLabel * mypLabel = new QLabel ();
127
+ mypInnerLayout->addWidget ( mypLabel, index , 0 );
128
+ if ( myFieldType == QVariant::Int )
129
+ {
130
+ mypLabel->setText ( myFieldName + tr ( " (int)" ) );
131
+ }
132
+ else if ( myFieldType == QVariant::Double )
133
+ {
134
+ mypLabel->setText ( myFieldName + tr ( " (dbl)" ) );
135
+ }
136
+ else // string
137
+ {
138
+ // any special behaviour for string goes here
139
+ mypLabel->setText ( myFieldName + tr ( " (txt)" ) );
140
+ }
141
+
142
+ myWidget->setEnabled ( vl->isEditable () );
143
+
144
+ mypInnerLayout->addWidget ( myWidget, index , 1 );
145
+ mpIndizes << it.key ();
146
+ mpWidgets << myWidget;
147
+ ++index ;
88
148
}
89
- else if ( myFieldType == QVariant::Double )
149
+ // Set focus to first widget in list, to help entering data without moving the mouse.
150
+ if ( mpWidgets.size () > 0 )
90
151
{
91
- mypLabel-> setText ( myFieldName + tr ( " (dbl) " ) );
152
+ mpWidgets. first ()-> setFocus ( Qt::OtherFocusReason );
92
153
}
93
- else // string
154
+ }
155
+ else
156
+ {
157
+ for ( QgsAttributeMap::const_iterator it = myAttributes.begin (); it != myAttributes.end (); ++it )
94
158
{
95
- // any special behaviour for string goes here
96
- mypLabel->setText ( myFieldName + tr ( " (txt)" ) );
97
- }
159
+ const QgsField &field = theFieldMap[it.key ()];
160
+
161
+ QWidget *myWidget = mDialog ->findChild <QWidget*>( field.name () );
162
+ if ( !myWidget )
163
+ continue ;
164
+
165
+ QgsAttributeEditor::createAttributeEditor ( mDialog , myWidget, vl, it.key (), it.value () );
98
166
99
- mypInnerLayout->addWidget ( myWidget, index , 1 );
100
- mpIndizes << it.key ();
101
- mpWidgets << myWidget;
102
- ++index ;
167
+ myWidget->setEnabled ( vl->isEditable () );
168
+
169
+ mpIndizes << it.key ();
170
+ mpWidgets << myWidget;
171
+ }
103
172
}
104
- // Set focus to first widget in list, to help entering data without moving the mouse.
105
- if ( mpWidgets. size () > 0 )
173
+
174
+ if ( buttonBox )
106
175
{
107
- mpWidgets.first ()->setFocus ( Qt::OtherFocusReason );
176
+ buttonBox->clear ();
177
+
178
+ if ( vl->isEditable () )
179
+ {
180
+ buttonBox->setStandardButtons ( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
181
+ connect ( buttonBox, SIGNAL ( accepted () ), mDialog , SLOT ( accept () ) );
182
+ connect ( buttonBox, SIGNAL ( accepted () ), this , SLOT ( accept () ) );
183
+ }
184
+ else
185
+ {
186
+ buttonBox->setStandardButtons ( QDialogButtonBox::Cancel );
187
+ }
188
+
189
+ connect ( buttonBox, SIGNAL ( rejected () ), mDialog , SLOT ( reject () ) );
190
+ connect ( buttonBox, SIGNAL ( rejected () ), this , SLOT ( rejected () ) );
108
191
}
192
+
193
+ QMetaObject::connectSlotsByName ( mDialog );
194
+
109
195
restoreGeometry ();
110
196
}
111
197
@@ -117,12 +203,13 @@ QgsAttributeDialog::~QgsAttributeDialog()
117
203
118
204
void QgsAttributeDialog::accept ()
119
205
{
206
+ if ( !mLayer ->isEditable () )
207
+ return ;
208
+
120
209
// write the new values back to the feature
121
210
QgsAttributeMap myAttributes = mpFeature->attributeMap ();
122
211
int myIndex = 0 ;
123
- for ( QgsAttributeMap::const_iterator it = myAttributes.begin ();
124
- it != myAttributes.end ();
125
- ++it )
212
+ for ( QgsAttributeMap::const_iterator it = myAttributes.begin (); it != myAttributes.end (); ++it )
126
213
{
127
214
QVariant value;
128
215
@@ -132,17 +219,21 @@ void QgsAttributeDialog::accept()
132
219
133
220
++myIndex;
134
221
}
135
- QDialog::accept ();
222
+ }
223
+
224
+ int QgsAttributeDialog::exec ()
225
+ {
226
+ return mDialog ->exec ();
136
227
}
137
228
138
229
void QgsAttributeDialog::saveGeometry ()
139
230
{
140
231
QSettings settings;
141
- settings.setValue ( mSettingsPath + " geometry" , QDialog:: saveGeometry () );
232
+ settings.setValue ( mSettingsPath + " geometry" , mDialog -> saveGeometry () );
142
233
}
143
234
144
235
void QgsAttributeDialog::restoreGeometry ()
145
236
{
146
237
QSettings settings;
147
- QDialog:: restoreGeometry ( settings.value ( mSettingsPath + " geometry" ).toByteArray () );
238
+ mDialog -> restoreGeometry ( settings.value ( mSettingsPath + " geometry" ).toByteArray () );
148
239
}
0 commit comments