|
22 | 22 | #include <QTableWidgetItem>
|
23 | 23 | #include <QSettings>
|
24 | 24 | #include <QLineEdit>
|
25 |
| - |
26 |
| -QgsAttributeDialog::QgsAttributeDialog(const QgsFieldMap& fields, const QgsAttributeMap& attributes) |
| 25 | +#include <QSpinBox> |
| 26 | +#include <QLabel> |
| 27 | +#include <QDoubleSpinBox> |
| 28 | +#include <QFrame> |
| 29 | +#include <QScrollArea> |
| 30 | + |
| 31 | +QgsAttributeDialog::QgsAttributeDialog( |
| 32 | + QgsFieldMap theFieldMap, QgsFeature * thepFeature) |
27 | 33 | : QDialog(),
|
28 |
| - _settingsPath("/Windows/AttributeDialog/"), |
29 |
| - mRowIsDirty(attributes.size(), FALSE) |
| 34 | + mSettingsPath("/Windows/AttributeDialog/"), |
| 35 | + mFieldMap(theFieldMap), |
| 36 | + mpFeature(thepFeature) |
30 | 37 | {
|
31 |
| - restorePositionAndColumnWidth(); |
32 |
| - |
33 |
| - setupUi(this); |
34 |
| - mTable->setRowCount(attributes.size()); |
35 | 38 |
|
36 |
| - int index=0; |
37 |
| - for (QgsAttributeMap::const_iterator it = attributes.begin(); it != attributes.end(); ++it) |
| 39 | + setupUi(this); |
| 40 | + if (mpFeature==NULL) return; |
| 41 | + if (mFieldMap.isEmpty()) return; |
| 42 | + QgsAttributeMap myAttributes = mpFeature->attributeMap(); |
| 43 | + // |
| 44 | + //Set up dynamic inside a scroll box |
| 45 | + // |
| 46 | + QVBoxLayout * mypOuterLayout = new QVBoxLayout(); |
| 47 | + mypOuterLayout->setContentsMargins(0,0,0,0); |
| 48 | + //transfers layout ownership so no need to call delete |
| 49 | + mFrame->setLayout(mypOuterLayout); |
| 50 | + QScrollArea * mypScrollArea = new QScrollArea(); |
| 51 | + //transfers scroll area ownership so no need to call delete |
| 52 | + mypOuterLayout->addWidget(mypScrollArea); |
| 53 | + QFrame * mypInnerFrame = new QFrame(); |
| 54 | + mypInnerFrame->setFrameShape(QFrame::NoFrame); |
| 55 | + mypInnerFrame->setFrameShadow(QFrame::Plain); |
| 56 | + //transfers frame ownership so no need to call delete |
| 57 | + mypScrollArea->setWidget(mypInnerFrame); |
| 58 | + mypScrollArea->setWidgetResizable( true ); |
| 59 | + QGridLayout * mypInnerLayout = new QGridLayout(mypInnerFrame); |
| 60 | + |
| 61 | + int index=0; |
| 62 | + for (QgsAttributeMap::const_iterator it = myAttributes.begin(); |
| 63 | + it != myAttributes.end(); |
| 64 | + ++it) |
| 65 | + { |
| 66 | + QString myFieldName = mFieldMap[it.key()].name(); |
| 67 | + QLabel * mypLabel = new QLabel(); |
| 68 | + mypInnerLayout->addWidget(mypLabel,index,0); |
| 69 | + QVariant myFieldValue = it.value(); |
| 70 | + QLineEdit * mypLineEdit = new QLineEdit(); |
| 71 | + //the provider may have provided a default value so use it |
| 72 | + mypLineEdit->setText(myFieldValue.toString()); |
| 73 | + if( mFieldMap[it.key()].type()==QVariant::Int ) |
38 | 74 | {
|
39 |
| - // set attribute name |
40 |
| - |
41 |
| - QString fieldName = fields[it.key()].name(); |
42 |
| - |
43 |
| - QTableWidgetItem * myFieldItem = new QTableWidgetItem(fieldName); |
44 |
| - myFieldItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); |
45 |
| - mTable->setItem(index, 0, myFieldItem); |
46 |
| - |
47 |
| -#if QT_VERSION >= 0x040400 |
48 |
| - // set attribute value |
49 |
| - QTableWidgetItem * myValueItem = new QTableWidgetItem((*it).toString()); |
50 |
| - mTable->setItem(index, 1, myValueItem); |
51 |
| -#endif |
52 |
| - |
53 |
| - QLineEdit *le = new QLineEdit(); |
54 |
| - |
55 |
| - le->setFrame(false); |
56 |
| - |
57 |
| - if( fields[it.key()].type()==QVariant::Int ) |
58 |
| - { |
59 |
| - le->setValidator( new QIntValidator(le) ); |
60 |
| - } |
61 |
| - else if( fields[it.key()].type()==QVariant::Double ) |
62 |
| - { |
63 |
| - le->setValidator( new QDoubleValidator(le) ); |
64 |
| - } |
65 |
| - |
66 |
| -#if QT_VERSION < 0x040400 |
67 |
| - le->setText((*it).toString()); |
68 |
| -#endif |
69 |
| - |
70 |
| - mTable->setCellWidget(index, 1, le); |
71 |
| - |
72 |
| - ++index; |
| 75 | + mypLineEdit->setValidator( new QIntValidator(mypLineEdit) ); |
| 76 | + mypLabel->setText(myFieldName + tr(" (int)")); |
73 | 77 | }
|
74 |
| - |
75 |
| - // setup the mechanism to track edited attribute values |
76 |
| - // if we do it this way, only edited attributes will |
77 |
| - // be attempted to be saved when the editing session stops. |
78 |
| - connect(mTable, SIGNAL(cellChanged(int, int)), |
79 |
| - this, SLOT (setAttributeValueChanged(int, int))); |
80 |
| - |
81 |
| - mTable->resizeColumnsToContents(); |
82 |
| -} |
83 |
| - |
84 |
| -QgsAttributeDialog::~QgsAttributeDialog() |
85 |
| -{ |
86 |
| - |
| 78 | + else if( mFieldMap[it.key()].type()==QVariant::Double ) |
| 79 | + { |
| 80 | + mypLineEdit->setValidator( new QDoubleValidator(mypLineEdit) ); |
| 81 | + mypLabel->setText(myFieldName + tr(" (dbl)")); |
| 82 | + } |
| 83 | + else //string |
| 84 | + { |
| 85 | + //any special behaviour for string goes here |
| 86 | + mypLabel->setText(myFieldName + tr(" (txt)")); |
| 87 | + } |
| 88 | + mypInnerLayout->addWidget(mypLineEdit,index,1); |
| 89 | + mpWidgets << mypLineEdit; |
| 90 | + ++index; |
| 91 | + } |
| 92 | + restoreGeometry(); |
87 | 93 | }
|
88 | 94 |
|
89 |
| -QString QgsAttributeDialog::value(int row) |
90 |
| -{ |
91 |
| - return static_cast<QLineEdit*>(mTable->cellWidget(row,1))->text(); |
92 |
| -} |
93 | 95 |
|
94 |
| -bool QgsAttributeDialog::isDirty(int row) |
| 96 | +QgsAttributeDialog::~QgsAttributeDialog() |
95 | 97 | {
|
96 |
| - return mRowIsDirty.at(row); |
| 98 | + saveGeometry(); |
97 | 99 | }
|
98 |
| - |
99 |
| -bool QgsAttributeDialog::queryAttributes(const QgsFieldMap& fields, QgsFeature& f) |
| 100 | + |
| 101 | +void QgsAttributeDialog::accept() |
100 | 102 | {
|
101 |
| - QgsAttributeMap featureAttributes = f.attributeMap(); |
102 |
| - QgsAttributeDialog attdialog(fields, featureAttributes); |
103 |
| - |
104 |
| - if (attdialog.exec() == QDialog::Accepted) |
| 103 | + //write the new values back to the feature |
| 104 | + QgsAttributeMap myAttributes = mpFeature->attributeMap(); |
| 105 | + int myIndex=0; |
| 106 | + for (QgsAttributeMap::const_iterator it = myAttributes.begin(); |
| 107 | + it != myAttributes.end(); |
| 108 | + ++it) |
105 | 109 | {
|
106 |
| - int i=0; |
107 |
| - for (QgsAttributeMap::const_iterator it = featureAttributes.begin(); it != featureAttributes.end(); ++it, i++) |
| 110 | + //Q_ASSERT(myIndex <= mpWidgets.size()); |
| 111 | + QString myFieldName = mFieldMap[it.key()].name(); |
| 112 | + bool myFlag=false; |
| 113 | + QString myFieldValue = |
| 114 | + dynamic_cast<QLineEdit *>(mpWidgets.value(myIndex))->text(); |
| 115 | + switch( mFieldMap[it.key()].type() ) |
108 | 116 | {
|
109 |
| - QString value = attdialog.value(i); |
110 |
| - |
111 |
| - if( attdialog.isDirty(i) ) |
112 |
| - { |
113 |
| - switch( fields[it.key()].type() ) |
| 117 | + case QVariant::Int: |
114 | 118 | {
|
115 |
| - case QVariant::Int: |
116 |
| - f.changeAttribute(it.key(), value=="" ? QVariant( QString::null ) : QVariant( value.toInt() ) ); |
117 |
| - break; |
118 |
| - |
119 |
| - case QVariant::Double: |
120 |
| - f.changeAttribute(it.key(), value=="" ? QVariant( QString::null ) : QVariant( value.toDouble() ) ); |
121 |
| - break; |
122 |
| - |
123 |
| - default: |
124 |
| - f.changeAttribute(it.key(), value=="NULL" ? QVariant(QString::null) : QVariant(value) ); |
125 |
| - break; |
| 119 | + int myIntValue = myFieldValue.toInt(&myFlag); |
| 120 | + if (myFlag && ! myFieldValue.isEmpty()) |
| 121 | + { |
| 122 | + mpFeature->changeAttribute( it.key(), QVariant(myIntValue) ); |
| 123 | + } |
| 124 | + else |
| 125 | + { |
| 126 | + mpFeature->changeAttribute( it.key(), QVariant(QString::null) ); |
| 127 | + } |
| 128 | + } |
| 129 | + break; |
| 130 | + case QVariant::Double: |
| 131 | + { |
| 132 | + double myDblValue = myFieldValue.toDouble(&myFlag); |
| 133 | + if (myFlag && ! myFieldValue.isEmpty()) |
| 134 | + { |
| 135 | + mpFeature->changeAttribute( it.key(), QVariant(myDblValue) ); |
| 136 | + } |
| 137 | + else |
| 138 | + { |
| 139 | + mpFeature->changeAttribute( it.key(), QVariant(QString::null) ); |
| 140 | + } |
126 | 141 | }
|
127 |
| - } else { |
128 |
| - f.changeAttribute(it.key(), QVariant(value) ); |
129 |
| - } |
| 142 | + break; |
| 143 | + default: //string |
| 144 | + mpFeature->changeAttribute(it.key(),QVariant( myFieldValue ) ); |
| 145 | + break; |
130 | 146 | }
|
131 |
| - return true; |
132 |
| - } |
133 |
| - else |
134 |
| - { |
135 |
| - return false; |
| 147 | + ++myIndex; |
136 | 148 | }
|
| 149 | + QDialog::accept(); |
137 | 150 | }
|
138 | 151 |
|
139 |
| -void QgsAttributeDialog::savePositionAndColumnWidth() |
| 152 | +void QgsAttributeDialog::saveGeometry() |
140 | 153 | {
|
141 | 154 | QSettings settings;
|
142 |
| - settings.setValue(_settingsPath+"geometry", saveGeometry()); |
| 155 | + settings.setValue(mSettingsPath+"geometry", QDialog::saveGeometry()); |
143 | 156 | }
|
144 | 157 |
|
145 |
| -void QgsAttributeDialog::resizeEvent(QResizeEvent *event) |
146 |
| - { |
147 |
| - savePositionAndColumnWidth(); |
148 |
| - QWidget::resizeEvent(event); |
149 |
| - } |
150 |
| - |
151 |
| -void QgsAttributeDialog::moveEvent(QMoveEvent *event) |
152 |
| - { |
153 |
| - savePositionAndColumnWidth(); |
154 |
| - QWidget::moveEvent(event); |
155 |
| - } |
156 |
| - |
157 |
| -void QgsAttributeDialog::restorePositionAndColumnWidth() |
| 158 | +void QgsAttributeDialog::restoreGeometry() |
158 | 159 | {
|
159 | 160 | QSettings settings;
|
160 |
| - restoreGeometry(settings.value(_settingsPath+"geometry").toByteArray()); |
| 161 | + QDialog::restoreGeometry(settings.value(mSettingsPath+"geometry").toByteArray()); |
161 | 162 | }
|
162 | 163 |
|
163 |
| -void QgsAttributeDialog::setAttributeValueChanged(int row, int column) |
164 |
| -{ |
165 |
| - QgsDebugMsg("Entered with row " + QString::number(row) + |
166 |
| - ", column " + QString::number(column) + "."); |
167 |
| - |
168 |
| - mRowIsDirty.at(row) = TRUE; |
169 |
| -} |
0 commit comments