Skip to content

Commit 1905cca

Browse files
committedMay 24, 2018
[locator] move the config button to the table
1 parent 61f84ce commit 1905cca

File tree

3 files changed

+93
-99
lines changed

3 files changed

+93
-99
lines changed
 

‎src/app/locator/qgslocatoroptionswidget.cpp

Lines changed: 79 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,44 @@
1515
* *
1616
***************************************************************************/
1717

18+
19+
#include <QApplication>
20+
#include <QToolButton>
21+
#include <QHBoxLayout>
22+
1823
#include "qgslocatoroptionswidget.h"
1924

25+
#include "qgsapplication.h"
2026
#include "qgslocatorwidget.h"
2127
#include "qgssettings.h"
2228

29+
2330
QgsLocatorOptionsWidget::QgsLocatorOptionsWidget( QgsLocatorWidget *locator, QWidget *parent )
24-
: QWidget( parent )
31+
: QTreeView( parent )
2532
, mLocatorWidget( locator )
2633
, mLocator( locator->locator() )
2734
{
28-
setupUi( this );
2935

3036
mModel = new QgsLocatorFiltersModel( mLocator, this );
31-
mFiltersTreeView->setModel( mModel );
37+
setModel( mModel );
38+
39+
header()->setStretchLastSection( false );
40+
header()->setSectionResizeMode( QgsLocatorFiltersModel::Name, QHeaderView::Stretch );
3241

33-
mFiltersTreeView->header()->setStretchLastSection( false );
34-
mFiltersTreeView->header()->setSectionResizeMode( 0, QHeaderView::Stretch );
42+
setEditTriggers( QAbstractItemView::AllEditTriggers );
43+
setAlternatingRowColors( true );
44+
setSelectionMode( QAbstractItemView::NoSelection );
3545

36-
mConfigureFilterButton->setEnabled( false );
37-
connect( mFiltersTreeView->selectionModel(), &QItemSelectionModel::selectionChanged, this, [ = ]( const QItemSelection & selected, const QItemSelection & )
46+
// add the config button
47+
for ( int row = 0; row < mModel->rowCount(); ++row )
3848
{
39-
if ( selected.count() == 0 || selected.at( 0 ).indexes().count() == 0 )
49+
QModelIndex index = mModel->index( row, QgsLocatorFiltersModel::Config );
50+
QWidget *bt = mModel->configButton( index, this );
51+
if ( bt )
4052
{
41-
mConfigureFilterButton->setEnabled( false );
53+
setIndexWidget( index, bt );
4254
}
43-
else
44-
{
45-
QModelIndex sel = selected.at( 0 ).indexes().at( 0 );
46-
QgsLocatorFilter *filter = mModel->filterForIndex( sel );
47-
mConfigureFilterButton->setEnabled( filter->hasConfigWidget() );
48-
}
49-
} );
50-
connect( mConfigureFilterButton, &QPushButton::clicked, this, &QgsLocatorOptionsWidget::configureCurrentFilter );
55+
}
5156
}
5257

5358
void QgsLocatorOptionsWidget::commitChanges()
@@ -56,23 +61,23 @@ void QgsLocatorOptionsWidget::commitChanges()
5661
mLocatorWidget->invalidateResults();
5762
}
5863

59-
void QgsLocatorOptionsWidget::configureCurrentFilter()
64+
void QgsLocatorOptionsWidget::dataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles )
6065
{
61-
auto selected = mFiltersTreeView->selectionModel()->selection();
62-
if ( selected.count() == 0 || selected.at( 0 ).indexes().count() == 0 )
66+
for ( int row = topLeft.row(); row < bottomRight.row(); ++row )
6367
{
64-
return;
65-
}
66-
else
67-
{
68-
QModelIndex sel = selected.at( 0 ).indexes().at( 0 );
69-
QgsLocatorFilter *filter = mModel->filterForIndex( sel );
70-
if ( filter )
71-
filter->openConfigWidget( this );
68+
QModelIndex index = mModel->index( row, QgsLocatorFiltersModel::Config );
69+
if ( !indexWidget( index ) )
70+
{
71+
QWidget *bt = mModel->configButton( index, this );
72+
if ( bt )
73+
{
74+
setIndexWidget( index, bt );
75+
}
76+
}
7277
}
78+
QTreeView::dataChanged( topLeft, bottomRight, roles );
7379
}
7480

75-
7681
//
7782
// QgsLocatorFiltersModel
7883
//
@@ -85,6 +90,35 @@ QgsLocatorFiltersModel::QgsLocatorFiltersModel( QgsLocator *locator, QObject *pa
8590
{
8691
}
8792

93+
QWidget *QgsLocatorFiltersModel::configButton( const QModelIndex &index, QWidget *parent ) const
94+
{
95+
if ( !index.isValid() )
96+
return nullptr;
97+
98+
QgsLocatorFilter *filter = filterForIndex( index );
99+
if ( filter && filter->hasConfigWidget() )
100+
{
101+
// use a layout to get the button center aligned
102+
QWidget *w = new QWidget( parent );
103+
QToolButton *bt = new QToolButton( );
104+
QHBoxLayout *layout = new QHBoxLayout();
105+
layout->setContentsMargins( 0, 0, 0, 0 );
106+
layout->addWidget( bt );
107+
w->setLayout( layout );
108+
109+
connect( bt, &QToolButton::clicked, this, [ = ]() {filter->openConfigWidget( bt );} );
110+
//bt->setMinimumSize( 24, 24 );
111+
bt->setMaximumSize( 24, 24 );
112+
bt->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
113+
bt->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/settings.svg" ) ) );
114+
return w;
115+
}
116+
else
117+
{
118+
return nullptr;
119+
}
120+
}
121+
88122
int QgsLocatorFiltersModel::rowCount( const QModelIndex &parent ) const
89123
{
90124
if ( parent.isValid() )
@@ -98,7 +132,7 @@ int QgsLocatorFiltersModel::columnCount( const QModelIndex &parent ) const
98132
if ( parent.isValid() )
99133
return 0;
100134

101-
return 4;
135+
return 5;
102136
}
103137

104138
QVariant QgsLocatorFiltersModel::data( const QModelIndex &index, int role ) const
@@ -124,6 +158,7 @@ QVariant QgsLocatorFiltersModel::data( const QModelIndex &index, int role ) cons
124158

125159
case Active:
126160
case Default:
161+
case Config:
127162
return QVariant();
128163
}
129164
break;
@@ -134,6 +169,7 @@ QVariant QgsLocatorFiltersModel::data( const QModelIndex &index, int role ) cons
134169
{
135170
case Name:
136171
case Prefix:
172+
case Config:
137173
return QVariant();
138174

139175
case Active:
@@ -153,6 +189,15 @@ QVariant QgsLocatorFiltersModel::data( const QModelIndex &index, int role ) cons
153189
return filterForIndex( index )->useWithoutPrefix() ? Qt::Checked : Qt::Unchecked;
154190
}
155191
break;
192+
193+
case Qt::SizeHintRole:
194+
return QSize( 32, 32 );
195+
196+
case Qt::TextAlignmentRole:
197+
if ( index.column() == Config )
198+
return Qt::AlignCenter;
199+
break;
200+
156201
}
157202

158203
return QVariant();
@@ -232,6 +277,7 @@ Qt::ItemFlags QgsLocatorFiltersModel::flags( const QModelIndex &index ) const
232277
switch ( index.column() )
233278
{
234279
case Name:
280+
case Config:
235281
break;
236282

237283
case Prefix:
@@ -264,6 +310,9 @@ QVariant QgsLocatorFiltersModel::headerData( int section, Qt::Orientation orient
264310

265311
case Default:
266312
return tr( "Default" );
313+
314+
case Config:
315+
return tr( "Configuration" );
267316
}
268317
}
269318

‎src/app/locator/qgslocatoroptionswidget.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,19 @@
1818
#ifndef QGSLOCATOROPTIONSWIDGET_H
1919
#define QGSLOCATOROPTIONSWIDGET_H
2020

21+
#include <QItemDelegate>
22+
#include <QTreeView>
23+
2124
#include "qgslocatorfilter.h"
2225
#include "qgslocator.h"
23-
#include "ui_qgslocatoroptionswidgetbase.h"
26+
27+
class QToolButton;
2428

2529
class QgsLocatorFiltersModel;
2630
class QgsLocatorWidget;
2731

28-
class QgsLocatorOptionsWidget : public QWidget, private Ui::QgsLocatorOptionsWidgetBase
32+
33+
class QgsLocatorOptionsWidget : public QTreeView
2934
{
3035
Q_OBJECT
3136

@@ -36,7 +41,9 @@ class QgsLocatorOptionsWidget : public QWidget, private Ui::QgsLocatorOptionsWid
3641
public slots:
3742

3843
void commitChanges();
39-
void configureCurrentFilter();
44+
45+
protected slots:
46+
void dataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles ) override;
4047

4148
private:
4249
QgsLocatorWidget *mLocatorWidget = nullptr;
@@ -68,14 +75,17 @@ class QgsLocatorFiltersModel : public QAbstractTableModel
6875
Name = 0,
6976
Prefix,
7077
Active,
71-
Default
78+
Default,
79+
Config
7280
};
7381

7482
/**
7583
* Constructor for QgsLocatorFiltersModel.
7684
*/
7785
QgsLocatorFiltersModel( QgsLocator *locator, QObject *parent = nullptr );
7886

87+
QWidget *configButton( const QModelIndex &index, QWidget *parent = nullptr ) const;
88+
7989
int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
8090
int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
8191
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
@@ -84,7 +94,6 @@ class QgsLocatorFiltersModel : public QAbstractTableModel
8494
QVariant headerData( int section, Qt::Orientation orientation,
8595
int role = Qt::DisplayRole ) const override;
8696

87-
8897
QgsLocatorFilter *filterForIndex( const QModelIndex &index ) const;
8998

9099
public slots:

‎src/ui/qgslocatoroptionswidgetbase.ui

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.