13
13
* (at your option) any later version. *
14
14
* *
15
15
***************************************************************************/
16
- #include < qdir.h>
17
- #include < qfile.h>
18
- #include < qsettings.h>
19
- #include < qpixmap.h>
20
- #include < q3listbox.h>
21
- #include < qstringlist.h>
22
- #include < qlabel.h>
23
- #include < QComboBox>
24
- #include < qspinbox.h>
25
- #include < qmessagebox.h>
26
- #include < qinputdialog.h>
27
- #include < qsettings.h>
28
- #include < qpainter.h>
29
- #include < qpixmap.h>
30
- #include < qpen.h>
31
- #include < q3pointarray.h>
32
- #include < qcursor.h>
33
- #include < qnamespace.h>
34
- #include < qtabwidget.h>
35
- #include < q3table.h>
36
- #include < qsettings.h>
37
- #include < qevent.h>
38
- // Added by qt3to4:
39
- #include < QKeyEvent>
40
-
41
- #include " qgis.h"
42
- #include " qgsmapcanvas.h"
43
- #include " qgsmaplayer.h"
44
- #include " qgsvectorlayer.h"
45
- #include " qgsdataprovider.h"
46
- #include " qgsmaptopixel.h"
47
16
48
- extern " C"
49
- {
50
- #include < grass/gis.h>
51
- #include < grass/Vect.h>
52
- }
53
-
54
- #include " qgsgrass.h"
55
- #include " qgsgrassprovider.h"
56
- #include " qgsgrassedit.h"
57
17
#include " qgsgrassattributes.h"
18
+ #include " qgsgrassedit.h"
19
+ #include " qgsgrassprovider.h"
20
+
58
21
#include " qgslogger.h"
59
22
60
- QgsGrassAttributesKeyPress::QgsGrassAttributesKeyPress ( Q3Table *tab )
23
+ #include < QHeaderView>
24
+ #include < QKeyEvent>
25
+ #include < QMessagebox>
26
+ #include < QSettings>
27
+ #include < QTableWidget>
28
+
29
+
30
+ QgsGrassAttributesKeyPress::QgsGrassAttributesKeyPress ( QTableWidget *tab )
61
31
{
62
32
mTable = tab;
63
33
}
@@ -72,7 +42,7 @@ bool QgsGrassAttributesKeyPress::eventFilter( QObject *o, QEvent *e )
72
42
73
43
if ( k->key () == Qt::Key_Tab )
74
44
{
75
- if ( mTable ->currentRow () < mTable ->numRows () - 1 )
45
+ if ( mTable ->currentRow () < mTable ->rowCount () - 1 )
76
46
{
77
47
mTable ->setCurrentCell ( mTable ->currentRow () + 1 , mTable ->currentColumn () );
78
48
}
@@ -99,13 +69,13 @@ QgsGrassAttributes::QgsGrassAttributes( QgsGrassEdit *edit, QgsGrassProvider *pr
99
69
// Remove old
100
70
while ( tabCats->count () )
101
71
{
102
- tabCats->removePage ( tabCats->currentPage () );
72
+ tabCats->removeTab ( tabCats->currentIndex () );
103
73
}
104
74
105
75
connect ( this , SIGNAL ( destroyed () ), mEdit , SLOT ( attributesClosed () ) );
106
76
107
77
// TODO: does not work:
108
- connect ( tabCats, SIGNAL ( void currentChanged ( QWidget * ) ), this , SLOT ( tabChanged ( QWidget * ) ) );
78
+ connect ( tabCats, SIGNAL ( currentChanged ( int ) ), this , SLOT ( tabChanged ( int ) ) );
109
79
110
80
resetButtons ();
111
81
restorePosition ();
@@ -132,24 +102,27 @@ void QgsGrassAttributes::saveWindowLocation()
132
102
settings.setValue ( " /GRASS/windows/attributes/geometry" , saveGeometry () );
133
103
}
134
104
105
+ void QgsGrassAttributes::setRowReadOnly ( QTableWidget* table, int row, bool ro )
106
+ {
107
+ for ( int i = 0 ; i < table->columnCount (); ++i )
108
+ {
109
+ QTableWidgetItem *item = table->item ( row, i );
110
+ item->setFlags ( ro ? item->flags () & ~Qt::ItemIsEditable : item->flags () | Qt::ItemIsEditable );
111
+ }
112
+ }
113
+
135
114
int QgsGrassAttributes::addTab ( const QString & label )
136
115
{
137
116
QgsDebugMsg ( " entered." );
138
117
139
- Q3Table *tb = new Q3Table ( 2 , 3 );
140
- tb->setColumnReadOnly ( 0 , TRUE );
141
- tb->setColumnReadOnly ( 2 , TRUE );
142
- tb->setRowReadOnly ( 0 , TRUE );
143
- tb->setRowReadOnly ( 1 , TRUE );
144
-
145
- tb->horizontalHeader ()->setLabel ( 0 , tr ( " Column" ) );
146
- tb->horizontalHeader ()->setLabel ( 1 , tr ( " Value" ) );
147
- tb->horizontalHeader ()->setLabel ( 2 , tr ( " Type" ) ); // Internal use
118
+ QTableWidget *tb = new QTableWidget ( 2 , 3 );
148
119
149
- tb->setLeftMargin ( 0 ); // hide row labels
120
+ tb->setHorizontalHeaderLabels ( QStringList ()
121
+ << tr ( " Column" )
122
+ << tr ( " Value" )
123
+ << tr ( " Type" ) ); // Internal use
150
124
151
- tb->setText ( 0 , 0 , tr ( " Layer" ) );
152
- tb->setText ( 1 , 0 , " Cat" );
125
+ tb->verticalHeader ()->hide ();
153
126
154
127
tabCats->addTab ( tb, label );
155
128
@@ -164,12 +137,12 @@ int QgsGrassAttributes::addTab( const QString & label )
164
137
QString path = " /GRASS/windows/attributes/columnWidth/" ;
165
138
for ( int i = 0 ; i < 2 ; i++ )
166
139
{
167
- bool ok;
168
- int cw = settings.readNumEntry ( path + QString::number ( i ), 30 , &ok );
140
+ bool ok = settings. contains ( path + QString::number ( i ) ) ;
141
+ int cw = settings.value ( path + QString::number ( i ), 30 ). toInt ( );
169
142
if ( ok ) tb->setColumnWidth ( i, cw );
170
143
}
171
144
172
- connect ( tb->horizontalHeader (), SIGNAL ( sizeChange ( int , int , int ) ),
145
+ connect ( tb->horizontalHeader (), SIGNAL ( sectionResized ( int , int , int ) ),
173
146
this , SLOT ( columnSizeChanged ( int , int , int ) ) );
174
147
175
148
return ( tabCats->count () - 1 );
@@ -179,45 +152,53 @@ void QgsGrassAttributes::setField( int tab, int field )
179
152
{
180
153
QgsDebugMsg ( " entered." );
181
154
182
- Q3Table *tb = ( Q3Table * ) tabCats->page ( tab );
155
+ QTableWidget *tb = static_cast <QTableWidget *>( tabCats->widget ( tab ) );
156
+
157
+ tb->setItem ( 0 , 0 , new QTableWidgetItem ( tr ( " Layer" ) ) );
183
158
184
159
QString str;
185
160
str.sprintf ( " %d" , field );
186
161
187
- tb->setText ( 0 , 1 , str );
162
+ tb->setItem ( 0 , 1 , new QTableWidgetItem ( str ) );
163
+
164
+ tb->setItem ( 0 , 2 , new QTableWidgetItem () );
165
+
166
+ setRowReadOnly ( tb, 0 , TRUE );
188
167
}
189
168
190
169
void QgsGrassAttributes::setCat ( int tab, const QString & name, int cat )
191
170
{
192
171
QgsDebugMsg ( " entered." );
193
172
194
- Q3Table *tb = ( Q3Table * ) tabCats->page ( tab );
173
+ QTableWidget *tb = static_cast <QTableWidget *>( tabCats->widget ( tab ) );
195
174
196
- tb->setText ( 1 , 0 , name );
175
+ tb->setItem ( 1 , 0 , new QTableWidgetItem ( name ) );
197
176
198
177
QString str;
199
178
str.sprintf ( " %d" , cat );
200
179
201
- tb->setText ( 1 , 1 , str );
180
+ tb->setItem ( 1 , 1 , new QTableWidgetItem ( str ) );
181
+
182
+ tb->setItem ( 1 , 2 , new QTableWidgetItem () );
183
+
184
+ setRowReadOnly ( tb, 1 , TRUE );
202
185
}
203
186
204
187
void QgsGrassAttributes::addAttribute ( int tab, const QString &name, const QString &value,
205
188
const QString &type )
206
189
{
207
190
QgsDebugMsg ( QString ( " name=%1 value=%2" ).arg ( name ).arg ( value ) );
208
191
209
- Q3Table *tb = ( Q3Table * ) tabCats->page ( tab );
210
-
211
- tb->setNumRows ( tb->numRows () + 1 );
192
+ QTableWidget *tb = static_cast <QTableWidget *>( tabCats->widget ( tab ) );
212
193
213
- int row = tb->numRows () - 1 ;
214
- tb->setText ( row, 0 , name );
194
+ tb->setRowCount ( tb->rowCount () + 1 );
215
195
216
- // I have no rational explanation why fromLocal8Bit is necessary, value should be in unicode
217
- // because QgsGrassProvider::attributes is using mEncoding->toUnicode()
218
- // tb->setText ( row, 1, QString::fromLocal8Bit(value) );
219
- tb->setText ( row, 1 , value );
220
- tb->setText ( row, 2 , type );
196
+ int row = tb->rowCount () - 1 ;
197
+ tb->setItem ( row, 0 , new QTableWidgetItem ( name ) );
198
+ tb->item ( row, 0 )->setFlags ( tb->item ( row, 0 )->flags () & ~Qt::ItemIsEditable );
199
+ tb->setItem ( row, 1 , new QTableWidgetItem ( value ) );
200
+ tb->setItem ( row, 2 , new QTableWidgetItem ( type ) );
201
+ tb->item ( row, 2 )->setFlags ( tb->item ( row, 2 )->flags () & ~Qt::ItemIsEditable );
221
202
222
203
resetButtons ();
223
204
}
@@ -226,13 +207,14 @@ void QgsGrassAttributes::addTextRow( int tab, const QString &text )
226
207
{
227
208
QgsDebugMsg ( " entered." );
228
209
229
- Q3Table *tb = ( Q3Table * ) tabCats->page ( tab );
210
+ QTableWidget *tb = static_cast <QTableWidget *>( tabCats->widget ( tab ) );
230
211
231
- tb->setNumRows ( tb->numRows () + 1 );
212
+ tb->setRowCount ( tb->rowCount () + 1 );
232
213
233
- int row = tb->numRows () - 1 ;
234
- tb->setText ( row, 0 , text );
235
- tb->item ( row, 0 )->setSpan ( 1 , 3 ); // must be after setText() whe item exists
214
+ int row = tb->rowCount () - 1 ;
215
+ tb->setItem ( row, 0 , new QTableWidgetItem ( text ) );
216
+ tb->item ( row, 0 )->setFlags ( tb->item ( row, 0 )->flags () & ~Qt::ItemIsEditable );
217
+ tb->setSpan ( row, 0 , 1 , 3 );
236
218
}
237
219
238
220
void QgsGrassAttributes::updateAttributes ( )
@@ -241,46 +223,43 @@ void QgsGrassAttributes::updateAttributes( )
241
223
242
224
if ( tabCats->count () == 0 ) return ;
243
225
244
- Q3Table *tb = ( Q3Table * ) tabCats->currentPage ( );
226
+ QTableWidget *tb = static_cast <QTableWidget *>( tabCats->currentWidget () );
245
227
246
- if ( tb->numRows () > 2 )
228
+ if ( tb->rowCount () > 2 )
247
229
{
248
230
249
- // Stop editing (trick)
250
- tb->setColumnReadOnly ( 1 , TRUE );
251
- tb->setColumnReadOnly ( 1 , FALSE );
252
- tb->setRowReadOnly ( 0 , TRUE );
253
- tb->setRowReadOnly ( 1 , TRUE );
231
+ // Stop editing
232
+ QApplication::focusWidget ()->clearFocus ();
254
233
255
234
QString sql;
256
235
257
- for ( int i = 2 ; i < tb->numRows (); i++ )
236
+ for ( int i = 2 ; i < tb->rowCount (); i++ )
258
237
{
259
238
if ( i > 2 ) sql.append ( " , " );
260
239
261
- QString val = tb->text ( i, 1 ). stripWhiteSpace ();
240
+ QString val = tb->item ( i, 1 )-> text (). trimmed ();
262
241
263
242
if ( val.isEmpty () )
264
243
{
265
- sql.append ( tb->text ( i, 0 ) + " = null" );
244
+ sql.append ( tb->item ( i, 0 )-> text ( ) + " = null" );
266
245
}
267
246
else
268
247
{
269
- if ( tb->text ( i, 2 ) == " int" || tb->text ( i, 2 ) == " double" )
248
+ if ( tb->item ( i, 2 )-> text () == " int" || tb->item ( i, 2 )-> text ( ) == " double" )
270
249
{
271
- sql.append ( tb->text ( i, 0 ) + " = " + val );
250
+ sql.append ( tb->item ( i, 0 )-> text ( ) + " = " + val );
272
251
}
273
252
else
274
253
{
275
254
val.replace ( " '" , " ''" );
276
- sql.append ( tb->text ( i, 0 ) + " = '" + val + " '" );
255
+ sql.append ( tb->item ( i, 0 )-> text ( ) + " = '" + val + " '" );
277
256
}
278
257
}
279
258
}
280
259
281
260
QgsDebugMsg ( QString ( " sql: %1" ).arg ( sql ) );
282
261
283
- QString *error = mProvider ->updateAttributes ( tb->text ( 0 , 1 ).toInt (), tb->text ( 1 , 1 ).toInt (), sql );
262
+ QString *error = mProvider ->updateAttributes ( tb->item ( 0 , 1 )-> text () .toInt (), tb->item ( 1 , 1 )-> text ( ).toInt (), sql );
284
263
285
264
if ( !error->isEmpty () )
286
265
{
@@ -303,7 +282,7 @@ void QgsGrassAttributes::addCat( )
303
282
mEdit ->addCat ( mLine );
304
283
305
284
// Select new tab
306
- tabCats->setCurrentPage ( tabCats->count () - 1 );
285
+ tabCats->setCurrentIndex ( tabCats->count () - 1 );
307
286
308
287
resetButtons ();
309
288
}
@@ -314,14 +293,14 @@ void QgsGrassAttributes::deleteCat( )
314
293
315
294
if ( tabCats->count () == 0 ) return ;
316
295
317
- Q3Table *tb = ( Q3Table * ) tabCats->currentPage ( );
296
+ QTableWidget *tb = static_cast <QTableWidget *>( tabCats->currentWidget () );
318
297
319
- int field = tb->text ( 0 , 1 ).toInt ();
320
- int cat = tb->text ( 1 , 1 ).toInt ();
298
+ int field = tb->item ( 0 , 1 )-> text ( ).toInt ();
299
+ int cat = tb->item ( 1 , 1 )-> text ( ).toInt ();
321
300
322
301
mEdit ->deleteCat ( mLine , field, cat );
323
302
324
- tabCats->removePage ( tb );
303
+ tabCats->removeTab ( tabCats-> indexOf ( tb ) );
325
304
delete tb;
326
305
resetButtons ();
327
306
}
@@ -332,19 +311,17 @@ void QgsGrassAttributes::clear( )
332
311
333
312
while ( tabCats->count () > 0 )
334
313
{
335
- Q3Table *tb = ( Q3Table * ) tabCats->currentPage ( );
336
- tabCats->removePage ( tb );
314
+ QTableWidget *tb = static_cast <QTableWidget *>( tabCats->currentWidget () );
315
+ tabCats->removeTab ( tabCats-> indexOf ( tb ) );
337
316
delete tb;
338
317
}
339
318
resetButtons ();
340
319
}
341
320
342
- void QgsGrassAttributes::tabChanged ( QWidget *widget )
321
+ void QgsGrassAttributes::tabChanged ( int index )
343
322
{
344
323
QgsDebugMsg ( " entered." );
345
324
346
- // Q3Table *tb = (Q3Table *) tabCats->currentPage();
347
-
348
325
resultLabel->setText ( " " );
349
326
}
350
327
@@ -373,5 +350,5 @@ void QgsGrassAttributes::columnSizeChanged( int section, int oldSize, int newSiz
373
350
QString path = " /GRASS/windows/attributes/columnWidth/"
374
351
+ QString::number ( section );
375
352
QgsDebugMsg ( QString ( " path = %1 newSize = %2" ).arg ( path ).arg ( newSize ) );
376
- settings.writeEntry ( path, newSize );
353
+ settings.setValue ( path, newSize );
377
354
}
0 commit comments