Skip to content

Commit 7cb2d09

Browse files
author
mhugent
committed
Added selection of display attributes and aliases to composer table item
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12709 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent b891f99 commit 7cb2d09

8 files changed

+376
-18
lines changed

src/app/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ SET(QGIS_APP_SRCS
7373
qgsvectorlayerproperties.cpp
7474
qgsquerybuilder.cpp
7575

76+
composer/qgsattributeselectiondialog.cpp
7677
composer/qgscomposer.cpp
7778
composer/qgscomposerarrowwidget.cpp
7879
composer/qgscomposeritemwidget.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/***************************************************************************
2+
qgsattributeselectiondialog.h
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+
#ifndef QGSATTRIBUTESELECTIONDIALOG_H
19+
#define QGSATTRIBUTESELECTIONDIALOG_H
20+
21+
#include <QDialog>
22+
#include <QMap>
23+
#include <QSet>
24+
25+
class QGridLayout;
26+
class QgsVectorLayer;
27+
28+
/**A dialog to select what attributes to display (in the table item) and with the possibility to set different aliases*/
29+
class QgsAttributeSelectionDialog: public QDialog
30+
{
31+
public:
32+
QgsAttributeSelectionDialog(const QgsVectorLayer* vLayer, const QSet<int>& enabledAttributes, const QMap<int, QString>& aliasMap, QWidget * parent = 0, Qt::WindowFlags f = 0);
33+
~QgsAttributeSelectionDialog();
34+
35+
/**Returns indices of selected attributes*/
36+
QSet<int> enabledAttributes() const;
37+
/**Returns alias map (alias might be different than for vector layer)*/
38+
QMap<int, QString> aliasMap() const;
39+
40+
private:
41+
const QgsVectorLayer* mVectorLayer;
42+
QGridLayout* mGridLayout;
43+
};
44+
45+
#endif // QGSATTRIBUTESELECTIONDIALOG_H

src/app/composer/qgscomposertablewidget.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
***************************************************************************/
1717

1818
#include "qgscomposertablewidget.h"
19+
#include "qgsattributeselectiondialog.h"
1920
#include "qgscomposeritemwidget.h"
2021
#include "qgscomposertable.h"
2122
#include "qgscomposermap.h"
@@ -95,6 +96,23 @@ void QgsComposerTableWidget::on_mLayerComboBox_currentIndexChanged( int index )
9596
}
9697
}
9798

99+
void QgsComposerTableWidget::on_mAttributesPushButton_clicked()
100+
{
101+
if ( !mComposerTable )
102+
{
103+
return;
104+
}
105+
106+
QgsAttributeSelectionDialog d( mComposerTable->vectorLayer(), mComposerTable->displayAttributes(), mComposerTable->fieldAliasMap(), 0 );
107+
if ( d.exec() == QDialog::Accepted )
108+
{
109+
//change displayAttributes and aliases
110+
mComposerTable->setDisplayAttributes( d.enabledAttributes() );
111+
mComposerTable->setFieldAliasMap( d.aliasMap() );
112+
mComposerTable->update();
113+
}
114+
}
115+
98116
void QgsComposerTableWidget::on_mComposerMapComboBox_currentIndexChanged( int index )
99117
{
100118
if ( !mComposerTable )

src/app/composer/qgscomposertablewidget.h

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class QgsComposerTableWidget: public QWidget, private Ui::QgsComposerTableWidget
3939

4040
private slots:
4141
void on_mLayerComboBox_currentIndexChanged( int index );
42+
void on_mAttributesPushButton_clicked();
4243
void on_mComposerMapComboBox_currentIndexChanged( int index );
4344
void on_mMaximumColumnsSpinBox_valueChanged( int i );
4445
void on_mMarginSpinBox_valueChanged( double d );

0 commit comments

Comments
 (0)