/
qgsattributedialog.cpp
148 lines (129 loc) · 4.65 KB
/
qgsattributedialog.cpp
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
/***************************************************************************
qgsattributedialog.cpp - description
-------------------
begin : October 2004
copyright : (C) 2004 by Marco Hugentobler
email : marco.hugentobler@autoform.ch
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/* $Id$ */
#include "qgsattributedialog.h"
#include "qgsfield.h"
#include "qgslogger.h"
#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"
#include "qgsuniquevaluerenderer.h"
#include "qgssymbol.h"
#include "qgsattributeeditor.h"
#include <QTableWidgetItem>
#include <QSettings>
#include <QLabel>
#include <QFrame>
#include <QScrollArea>
QgsAttributeDialog::QgsAttributeDialog( QgsVectorLayer *vl, QgsFeature *thepFeature )
: QDialog(),
mSettingsPath( "/Windows/AttributeDialog/" ),
mLayer( vl ),
mpFeature( thepFeature )
{
setupUi( this );
if ( mpFeature == NULL || vl->dataProvider() == NULL )
return;
const QgsFieldMap &theFieldMap = vl->pendingFields();
if ( theFieldMap.isEmpty() )
return;
QgsAttributeMap myAttributes = mpFeature->attributeMap();
//
//Set up dynamic inside a scroll box
//
QVBoxLayout * mypOuterLayout = new QVBoxLayout();
mypOuterLayout->setContentsMargins( 0, 0, 0, 0 );
//transfers layout ownership so no need to call delete
mFrame->setLayout( mypOuterLayout );
QScrollArea * mypScrollArea = new QScrollArea();
//transfers scroll area ownership so no need to call delete
mypOuterLayout->addWidget( mypScrollArea );
QFrame * mypInnerFrame = new QFrame();
mypInnerFrame->setFrameShape( QFrame::NoFrame );
mypInnerFrame->setFrameShadow( QFrame::Plain );
//transfers frame ownership so no need to call delete
mypScrollArea->setWidget( mypInnerFrame );
mypScrollArea->setWidgetResizable( true );
QGridLayout * mypInnerLayout = new QGridLayout( mypInnerFrame );
int index = 0;
for ( QgsAttributeMap::const_iterator it = myAttributes.begin();
it != myAttributes.end();
++it )
{
const QgsField &field = theFieldMap[it.key()];
//show attribute alias if available
QString myFieldName = vl->attributeDisplayName( it.key() );
int myFieldType = field.type();
QWidget *myWidget = QgsAttributeEditor::createAttributeEditor( 0, vl, it.key(), it.value() );
if ( !myWidget )
continue;
QLabel * mypLabel = new QLabel();
mypInnerLayout->addWidget( mypLabel, index, 0 );
if ( myFieldType == QVariant::Int )
{
mypLabel->setText( myFieldName + tr( " (int)" ) );
}
else if ( myFieldType == QVariant::Double )
{
mypLabel->setText( myFieldName + tr( " (dbl)" ) );
}
else //string
{
//any special behaviour for string goes here
mypLabel->setText( myFieldName + tr( " (txt)" ) );
}
mypInnerLayout->addWidget( myWidget, index, 1 );
mpIndizes << it.key();
mpWidgets << myWidget;
++index;
}
// Set focus to first widget in list, to help entering data without moving the mouse.
if ( mpWidgets.size() > 0 )
{
mpWidgets.first()->setFocus( Qt::OtherFocusReason );
}
restoreGeometry();
}
QgsAttributeDialog::~QgsAttributeDialog()
{
saveGeometry();
}
void QgsAttributeDialog::accept()
{
//write the new values back to the feature
QgsAttributeMap myAttributes = mpFeature->attributeMap();
int myIndex = 0;
for ( QgsAttributeMap::const_iterator it = myAttributes.begin();
it != myAttributes.end();
++it )
{
QVariant value;
int idx = mpIndizes.value( myIndex );
if ( QgsAttributeEditor::retrieveValue( mpWidgets.value( myIndex ), mLayer, idx, value ) )
mpFeature->changeAttribute( idx, value );
++myIndex;
}
QDialog::accept();
}
void QgsAttributeDialog::saveGeometry()
{
QSettings settings;
settings.setValue( mSettingsPath + "geometry", QDialog::saveGeometry() );
}
void QgsAttributeDialog::restoreGeometry()
{
QSettings settings;
QDialog::restoreGeometry( settings.value( mSettingsPath + "geometry" ).toByteArray() );
}