2626#include < QScrollArea>
2727
2828QgsAttributeSelectionDialog::QgsAttributeSelectionDialog ( const QgsVectorLayer* vLayer, const QSet<int >& enabledAttributes, const QMap<int , QString>& aliasMap,
29- QWidget * parent, Qt::WindowFlags f ): QDialog( parent, f ), mVectorLayer( vLayer )
29+ const QList< QPair< int , bool > >& sortColumns, QWidget * parent, Qt::WindowFlags f ): QDialog( parent, f ), mVectorLayer( vLayer )
3030{
31+ setupUi ( this );
3132 if ( vLayer )
3233 {
33- QScrollArea* attributeScrollArea = new QScrollArea ( this );
34- QWidget* attributeWidget = new QWidget ();
35-
36- mAttributeGridLayout = new QGridLayout ( attributeWidget );
37- QLabel* attributeLabel = new QLabel ( QString ( " <b>" ) + tr ( " Attribute" ) + QString ( " </b>" ), this );
38- attributeLabel->setTextFormat ( Qt::RichText );
39- mAttributeGridLayout ->addWidget ( attributeLabel, 0 , 0 );
40- QLabel* aliasLabel = new QLabel ( QString ( " <b>" ) + tr ( " Alias" ) + QString ( " </b>" ), this );
41- aliasLabel->setTextFormat ( Qt::RichText );
42- mAttributeGridLayout ->addWidget ( aliasLabel, 0 , 1 );
43-
4434 QgsFieldMap fieldMap = vLayer->pendingFields ();
4535 QgsFieldMap::const_iterator fieldIt = fieldMap.constBegin ();
4636 int layoutRowCounter = 1 ;
4737 for ( ; fieldIt != fieldMap.constEnd (); ++fieldIt )
4838 {
39+ // insert field into sorting combo first
40+ mSortColumnComboBox ->addItem ( fieldIt.value ().name (), QVariant ( fieldIt.key () ) );
41+
42+ // and into enabled / alias list
4943 QCheckBox* attributeCheckBox = new QCheckBox ( fieldIt.value ().name (), this );
5044 if ( enabledAttributes.size () < 1 || enabledAttributes.contains ( fieldIt.key () ) )
5145 {
@@ -55,38 +49,32 @@ QgsAttributeSelectionDialog::QgsAttributeSelectionDialog( const QgsVectorLayer*
5549 {
5650 attributeCheckBox->setCheckState ( Qt::Unchecked );
5751 }
58- mAttributeGridLayout ->addWidget ( attributeCheckBox, layoutRowCounter, 0 );
52+ mAttributeGridLayout ->addWidget (( QWidget* ) attributeCheckBox, layoutRowCounter, 0 , 1 , 1 );
5953
6054 QLineEdit* attributeLineEdit = new QLineEdit ( this );
6155 QMap<int , QString>::const_iterator aliasIt = aliasMap.find ( fieldIt.key () );
6256 if ( aliasIt != aliasMap.constEnd () )
6357 {
6458 attributeLineEdit->setText ( aliasIt.value () );
6559 }
66- mAttributeGridLayout ->addWidget ( attributeLineEdit, layoutRowCounter, 1 );
60+ mAttributeGridLayout ->addWidget (( QWidget* ) attributeLineEdit, layoutRowCounter, 1 , 1 , 1 );
6761 ++layoutRowCounter;
6862 }
6963
70- attributeScrollArea->setWidget ( attributeWidget );
71-
72- QVBoxLayout* verticalLayout = new QVBoxLayout ( this );
73- verticalLayout->addWidget ( attributeScrollArea );
74-
75- QHBoxLayout* selectClearLayout = new QHBoxLayout ( this );
76- QPushButton* mSelectAllButton = new QPushButton ( tr ( " Select all" ), this );
77- QObject::connect ( mSelectAllButton , SIGNAL ( clicked () ), this , SLOT ( selectAllAttributes () ) );
78- QPushButton* mClearButton = new QPushButton ( tr ( " Clear" ), this );
79- QObject::connect ( mClearButton , SIGNAL ( clicked () ), this , SLOT ( clearAttributes () ) );
80- selectClearLayout->addWidget ( mSelectAllButton );
81- selectClearLayout->addWidget ( mClearButton );
82- verticalLayout->addLayout ( selectClearLayout );
83-
84-
85- QDialogButtonBox* buttonBox = new QDialogButtonBox ( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this );
86- QObject::connect ( buttonBox, SIGNAL ( accepted () ), this , SLOT ( accept () ) );
87- QObject::connect ( buttonBox, SIGNAL ( rejected () ), this , SLOT ( reject () ) );
88- verticalLayout->addWidget ( buttonBox );
64+ // sort columns
65+ QList< QPair<int , bool > >::const_iterator sortIt = sortColumns.constBegin ();
66+ for ( ; sortIt != sortColumns.constEnd (); ++sortIt )
67+ {
68+ QTreeWidgetItem* item = new QTreeWidgetItem ();
69+ item->setText ( 0 , fieldMap[sortIt->first ].name () );
70+ item->setData ( 0 , Qt::UserRole, sortIt->first );
71+ item->setText ( 1 , sortIt->second ? tr ( " Ascending" ) : tr ( " Descending" ) );
72+ mSortColumnTreeWidget ->addTopLevelItem ( item );
73+ }
8974 }
75+
76+ mOrderComboBox ->insertItem ( 0 , tr ( " Ascending" ) );
77+ mOrderComboBox ->insertItem ( 0 , tr ( " Descending" ) );
9078}
9179
9280QgsAttributeSelectionDialog::~QgsAttributeSelectionDialog ()
@@ -149,12 +137,28 @@ QMap<int, QString> QgsAttributeSelectionDialog::aliasMap() const
149137 return result;
150138}
151139
152- void QgsAttributeSelectionDialog::selectAllAttributes ()
140+ QList< QPair<int , bool > > QgsAttributeSelectionDialog::attributeSorting () const
141+ {
142+ QList< QPair<int , bool > > sortingList;
143+
144+ for ( int i = 0 ; i < mSortColumnTreeWidget ->topLevelItemCount (); ++i )
145+ {
146+ QTreeWidgetItem* item = mSortColumnTreeWidget ->topLevelItem ( i );
147+ if ( item )
148+ {
149+ sortingList.push_back ( qMakePair ( item->data ( 0 , Qt::UserRole ).toInt (), item->text ( 1 ) == tr ( " Ascending" ) ) );
150+ }
151+ }
152+
153+ return sortingList;
154+ }
155+
156+ void QgsAttributeSelectionDialog::on_mSelectAllButton_clicked ()
153157{
154158 setAllEnabled ( true );
155159}
156160
157- void QgsAttributeSelectionDialog::clearAttributes ()
161+ void QgsAttributeSelectionDialog::on_mClearButton_clicked ()
158162{
159163 setAllEnabled ( false );
160164}
@@ -183,3 +187,48 @@ void QgsAttributeSelectionDialog::setAllEnabled( bool enabled )
183187 }
184188}
185189
190+ void QgsAttributeSelectionDialog::on_mAddPushButton_clicked ()
191+ {
192+ QTreeWidgetItem* item = new QTreeWidgetItem ();
193+ item->setText ( 0 , mSortColumnComboBox ->currentText () );
194+ item->setData ( 0 , Qt::UserRole, mSortColumnComboBox ->itemData ( mSortColumnComboBox ->currentIndex () ) );
195+ item->setText ( 1 , mOrderComboBox ->currentText () );
196+ mSortColumnTreeWidget ->addTopLevelItem ( item );
197+ }
198+
199+ void QgsAttributeSelectionDialog::on_mRemovePushButton_clicked ()
200+ {
201+ int currentIndex = mSortColumnTreeWidget ->indexOfTopLevelItem ( mSortColumnTreeWidget ->currentItem () );
202+ if ( currentIndex != -1 )
203+ {
204+ delete ( mSortColumnTreeWidget ->takeTopLevelItem ( currentIndex ) );
205+ }
206+ }
207+
208+ void QgsAttributeSelectionDialog::on_mUpPushButton_clicked ()
209+ {
210+ int currentIndex = mSortColumnTreeWidget ->indexOfTopLevelItem ( mSortColumnTreeWidget ->currentItem () );
211+ if ( currentIndex != -1 )
212+ {
213+ if ( currentIndex > 0 )
214+ {
215+ QTreeWidgetItem* item = mSortColumnTreeWidget ->takeTopLevelItem ( currentIndex );
216+ mSortColumnTreeWidget ->insertTopLevelItem ( currentIndex - 1 , item );
217+ mSortColumnTreeWidget ->setCurrentItem ( item );
218+ }
219+ }
220+ }
221+
222+ void QgsAttributeSelectionDialog::on_mDownPushButton_clicked ()
223+ {
224+ int currentIndex = mSortColumnTreeWidget ->indexOfTopLevelItem ( mSortColumnTreeWidget ->currentItem () );
225+ if ( currentIndex != -1 )
226+ {
227+ if ( currentIndex < ( mSortColumnTreeWidget ->topLevelItemCount () - 1 ) )
228+ {
229+ QTreeWidgetItem* item = mSortColumnTreeWidget ->takeTopLevelItem ( currentIndex );
230+ mSortColumnTreeWidget ->insertTopLevelItem ( currentIndex + 1 , item );
231+ mSortColumnTreeWidget ->setCurrentItem ( item );
232+ }
233+ }
234+ }
0 commit comments