|
| 1 | +/*************************************************************************** |
| 2 | + qgsattributeselectiondialog.cpp |
| 3 | + ------------------------------- |
| 4 | + begin : January 2010 |
| 5 | + copyright : (C) 2010 by Marco Hugentobler |
| 6 | + email : marco at hugis dot net |
| 7 | + ***************************************************************************/ |
| 8 | + |
| 9 | +/*************************************************************************** |
| 10 | + * * |
| 11 | + * This program is free software; you can redistribute it and/or modify * |
| 12 | + * it under the terms of the GNU General Public License as published by * |
| 13 | + * the Free Software Foundation; either version 2 of the License, or * |
| 14 | + * (at your option) any later version. * |
| 15 | + * * |
| 16 | + ***************************************************************************/ |
| 17 | + |
| 18 | +#include "qgsattributeselectiondialog.h" |
| 19 | +#include "qgsvectorlayer.h" |
| 20 | +#include <QCheckBox> |
| 21 | +#include <QDialogButtonBox> |
| 22 | +#include <QGridLayout> |
| 23 | +#include <QLabel> |
| 24 | +#include <QLineEdit> |
| 25 | + |
| 26 | +QgsAttributeSelectionDialog::QgsAttributeSelectionDialog(const QgsVectorLayer* vLayer, const QSet<int>& enabledAttributes, const QMap<int, QString>& aliasMap, \ |
| 27 | + QWidget * parent, Qt::WindowFlags f): QDialog(parent, f), mVectorLayer(vLayer) |
| 28 | +{ |
| 29 | + if( vLayer ) |
| 30 | + { |
| 31 | + mGridLayout = new QGridLayout(this); |
| 32 | + QLabel* attributeLabel = new QLabel(QString("<b>") + tr("Attribute") + QString("</b>"), this ); |
| 33 | + attributeLabel->setTextFormat(Qt::RichText); |
| 34 | + mGridLayout->addWidget(attributeLabel, 0, 0); |
| 35 | + QLabel* aliasLabel = new QLabel(QString("<b>") + tr("Alias") + QString("</b>"), this ); |
| 36 | + aliasLabel->setTextFormat(Qt::RichText); |
| 37 | + mGridLayout->addWidget(aliasLabel, 0, 1); |
| 38 | + |
| 39 | + QgsFieldMap fieldMap = vLayer->pendingFields(); |
| 40 | + QgsFieldMap::const_iterator fieldIt = fieldMap.constBegin(); |
| 41 | + int layoutRowCounter = 1; |
| 42 | + for(; fieldIt != fieldMap.constEnd(); ++fieldIt) |
| 43 | + { |
| 44 | + QCheckBox* attributeCheckBox = new QCheckBox(fieldIt.value().name(), this); |
| 45 | + if( enabledAttributes.size() < 1 || enabledAttributes.contains(fieldIt.key()) ) |
| 46 | + { |
| 47 | + attributeCheckBox->setCheckState(Qt::Checked); |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + attributeCheckBox->setCheckState(Qt::Unchecked); |
| 52 | + } |
| 53 | + mGridLayout->addWidget(attributeCheckBox, layoutRowCounter, 0); |
| 54 | + |
| 55 | + QLineEdit* attributeLineEdit = new QLineEdit(this); |
| 56 | + QMap<int, QString>::const_iterator aliasIt = aliasMap.find( fieldIt.key() ); |
| 57 | + if(aliasIt != aliasMap.constEnd()) |
| 58 | + { |
| 59 | + attributeLineEdit->setText(aliasIt.value()); |
| 60 | + } |
| 61 | + mGridLayout->addWidget(attributeLineEdit, layoutRowCounter, 1); |
| 62 | + ++layoutRowCounter; |
| 63 | + } |
| 64 | + |
| 65 | + QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this); |
| 66 | + QObject::connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); |
| 67 | + QObject::connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); |
| 68 | + mGridLayout->addWidget( buttonBox, layoutRowCounter, 0, 3, 1); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +QgsAttributeSelectionDialog::~QgsAttributeSelectionDialog() |
| 73 | +{ |
| 74 | + |
| 75 | +} |
| 76 | + |
| 77 | +QSet<int> QgsAttributeSelectionDialog::enabledAttributes() const |
| 78 | +{ |
| 79 | + QSet<int> result; |
| 80 | + if(!mGridLayout || !mVectorLayer) |
| 81 | + { |
| 82 | + return result; |
| 83 | + } |
| 84 | + |
| 85 | + for( int i = 1; i < mGridLayout->rowCount(); ++i) |
| 86 | + { |
| 87 | + bool boxChecked = false; |
| 88 | + QLayoutItem* checkBoxItem = mGridLayout->itemAtPosition(i, 0); |
| 89 | + if(checkBoxItem) |
| 90 | + { |
| 91 | + QWidget* checkBoxWidget = checkBoxItem->widget(); |
| 92 | + if(checkBoxWidget) |
| 93 | + { |
| 94 | + QCheckBox* checkBox = dynamic_cast<QCheckBox*>(checkBoxWidget); |
| 95 | + if(checkBox) |
| 96 | + { |
| 97 | + if(checkBox->checkState() == Qt::Checked) |
| 98 | + { |
| 99 | + result.insert(mVectorLayer->fieldNameIndex(checkBox->text())); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return result; |
| 107 | +} |
| 108 | + |
| 109 | +QMap<int, QString> QgsAttributeSelectionDialog::aliasMap() const |
| 110 | +{ |
| 111 | + QMap<int, QString> result; |
| 112 | + if(!mGridLayout || !mVectorLayer) |
| 113 | + { |
| 114 | + return result; |
| 115 | + } |
| 116 | + |
| 117 | + for( int i = 1; i < mGridLayout->rowCount(); ++i) |
| 118 | + { |
| 119 | + QLayoutItem* lineEditItem = mGridLayout->itemAtPosition(i, 1); |
| 120 | + QLayoutItem* checkBoxItem = mGridLayout->itemAtPosition(i, 0); |
| 121 | + if(lineEditItem && checkBoxItem) |
| 122 | + { |
| 123 | + QWidget* lineEditWidget = lineEditItem->widget(); |
| 124 | + QWidget* checkBoxWidget = checkBoxItem->widget(); |
| 125 | + if(lineEditWidget) |
| 126 | + { |
| 127 | + QLineEdit* lineEdit = dynamic_cast<QLineEdit*>(lineEditWidget); |
| 128 | + QCheckBox* checkBox = dynamic_cast<QCheckBox*>(checkBoxWidget); |
| 129 | + if(lineEdit) |
| 130 | + { |
| 131 | + QString aliasText = lineEdit->text(); |
| 132 | + if(!aliasText.isEmpty()) |
| 133 | + { |
| 134 | + //insert into map |
| 135 | + int fieldIndex = mVectorLayer->fieldNameIndex( checkBox->text() ); |
| 136 | + result.insert(fieldIndex, aliasText); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + return result; |
| 143 | +} |
| 144 | + |
0 commit comments