Skip to content

Commit

Permalink
[locator] move the config button to the table
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed May 24, 2018
1 parent 61f84ce commit 1905cca
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 99 deletions.
109 changes: 79 additions & 30 deletions src/app/locator/qgslocatoroptionswidget.cpp
Expand Up @@ -15,39 +15,44 @@
* *
***************************************************************************/


#include <QApplication>
#include <QToolButton>
#include <QHBoxLayout>

#include "qgslocatoroptionswidget.h"

#include "qgsapplication.h"
#include "qgslocatorwidget.h"
#include "qgssettings.h"


QgsLocatorOptionsWidget::QgsLocatorOptionsWidget( QgsLocatorWidget *locator, QWidget *parent )
: QWidget( parent )
: QTreeView( parent )
, mLocatorWidget( locator )
, mLocator( locator->locator() )
{
setupUi( this );

mModel = new QgsLocatorFiltersModel( mLocator, this );
mFiltersTreeView->setModel( mModel );
setModel( mModel );

header()->setStretchLastSection( false );
header()->setSectionResizeMode( QgsLocatorFiltersModel::Name, QHeaderView::Stretch );

mFiltersTreeView->header()->setStretchLastSection( false );
mFiltersTreeView->header()->setSectionResizeMode( 0, QHeaderView::Stretch );
setEditTriggers( QAbstractItemView::AllEditTriggers );
setAlternatingRowColors( true );
setSelectionMode( QAbstractItemView::NoSelection );

mConfigureFilterButton->setEnabled( false );
connect( mFiltersTreeView->selectionModel(), &QItemSelectionModel::selectionChanged, this, [ = ]( const QItemSelection & selected, const QItemSelection & )
// add the config button
for ( int row = 0; row < mModel->rowCount(); ++row )
{
if ( selected.count() == 0 || selected.at( 0 ).indexes().count() == 0 )
QModelIndex index = mModel->index( row, QgsLocatorFiltersModel::Config );
QWidget *bt = mModel->configButton( index, this );
if ( bt )
{
mConfigureFilterButton->setEnabled( false );
setIndexWidget( index, bt );
}
else
{
QModelIndex sel = selected.at( 0 ).indexes().at( 0 );
QgsLocatorFilter *filter = mModel->filterForIndex( sel );
mConfigureFilterButton->setEnabled( filter->hasConfigWidget() );
}
} );
connect( mConfigureFilterButton, &QPushButton::clicked, this, &QgsLocatorOptionsWidget::configureCurrentFilter );
}
}

void QgsLocatorOptionsWidget::commitChanges()
Expand All @@ -56,23 +61,23 @@ void QgsLocatorOptionsWidget::commitChanges()
mLocatorWidget->invalidateResults();
}

void QgsLocatorOptionsWidget::configureCurrentFilter()
void QgsLocatorOptionsWidget::dataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles )
{
auto selected = mFiltersTreeView->selectionModel()->selection();
if ( selected.count() == 0 || selected.at( 0 ).indexes().count() == 0 )
for ( int row = topLeft.row(); row < bottomRight.row(); ++row )
{
return;
}
else
{
QModelIndex sel = selected.at( 0 ).indexes().at( 0 );
QgsLocatorFilter *filter = mModel->filterForIndex( sel );
if ( filter )
filter->openConfigWidget( this );
QModelIndex index = mModel->index( row, QgsLocatorFiltersModel::Config );
if ( !indexWidget( index ) )
{
QWidget *bt = mModel->configButton( index, this );
if ( bt )
{
setIndexWidget( index, bt );
}
}
}
QTreeView::dataChanged( topLeft, bottomRight, roles );
}


//
// QgsLocatorFiltersModel
//
Expand All @@ -85,6 +90,35 @@ QgsLocatorFiltersModel::QgsLocatorFiltersModel( QgsLocator *locator, QObject *pa
{
}

QWidget *QgsLocatorFiltersModel::configButton( const QModelIndex &index, QWidget *parent ) const
{
if ( !index.isValid() )
return nullptr;

QgsLocatorFilter *filter = filterForIndex( index );
if ( filter && filter->hasConfigWidget() )
{
// use a layout to get the button center aligned
QWidget *w = new QWidget( parent );
QToolButton *bt = new QToolButton( );
QHBoxLayout *layout = new QHBoxLayout();
layout->setContentsMargins( 0, 0, 0, 0 );
layout->addWidget( bt );
w->setLayout( layout );

connect( bt, &QToolButton::clicked, this, [ = ]() {filter->openConfigWidget( bt );} );
//bt->setMinimumSize( 24, 24 );
bt->setMaximumSize( 24, 24 );
bt->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
bt->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/propertyicons/settings.svg" ) ) );
return w;
}
else
{
return nullptr;
}
}

int QgsLocatorFiltersModel::rowCount( const QModelIndex &parent ) const
{
if ( parent.isValid() )
Expand All @@ -98,7 +132,7 @@ int QgsLocatorFiltersModel::columnCount( const QModelIndex &parent ) const
if ( parent.isValid() )
return 0;

return 4;
return 5;
}

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

case Active:
case Default:
case Config:
return QVariant();
}
break;
Expand All @@ -134,6 +169,7 @@ QVariant QgsLocatorFiltersModel::data( const QModelIndex &index, int role ) cons
{
case Name:
case Prefix:
case Config:
return QVariant();

case Active:
Expand All @@ -153,6 +189,15 @@ QVariant QgsLocatorFiltersModel::data( const QModelIndex &index, int role ) cons
return filterForIndex( index )->useWithoutPrefix() ? Qt::Checked : Qt::Unchecked;
}
break;

case Qt::SizeHintRole:
return QSize( 32, 32 );

case Qt::TextAlignmentRole:
if ( index.column() == Config )
return Qt::AlignCenter;
break;

}

return QVariant();
Expand Down Expand Up @@ -232,6 +277,7 @@ Qt::ItemFlags QgsLocatorFiltersModel::flags( const QModelIndex &index ) const
switch ( index.column() )
{
case Name:
case Config:
break;

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

case Default:
return tr( "Default" );

case Config:
return tr( "Configuration" );
}
}

Expand Down
19 changes: 14 additions & 5 deletions src/app/locator/qgslocatoroptionswidget.h
Expand Up @@ -18,14 +18,19 @@
#ifndef QGSLOCATOROPTIONSWIDGET_H
#define QGSLOCATOROPTIONSWIDGET_H

#include <QItemDelegate>
#include <QTreeView>

#include "qgslocatorfilter.h"
#include "qgslocator.h"
#include "ui_qgslocatoroptionswidgetbase.h"

class QToolButton;

class QgsLocatorFiltersModel;
class QgsLocatorWidget;

class QgsLocatorOptionsWidget : public QWidget, private Ui::QgsLocatorOptionsWidgetBase

class QgsLocatorOptionsWidget : public QTreeView
{
Q_OBJECT

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

void commitChanges();
void configureCurrentFilter();

protected slots:
void dataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles ) override;

private:
QgsLocatorWidget *mLocatorWidget = nullptr;
Expand Down Expand Up @@ -68,14 +75,17 @@ class QgsLocatorFiltersModel : public QAbstractTableModel
Name = 0,
Prefix,
Active,
Default
Default,
Config
};

/**
* Constructor for QgsLocatorFiltersModel.
*/
QgsLocatorFiltersModel( QgsLocator *locator, QObject *parent = nullptr );

QWidget *configButton( const QModelIndex &index, QWidget *parent = nullptr ) const;

int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
Expand All @@ -84,7 +94,6 @@ class QgsLocatorFiltersModel : public QAbstractTableModel
QVariant headerData( int section, Qt::Orientation orientation,
int role = Qt::DisplayRole ) const override;


QgsLocatorFilter *filterForIndex( const QModelIndex &index ) const;

public slots:
Expand Down
64 changes: 0 additions & 64 deletions src/ui/qgslocatoroptionswidgetbase.ui

This file was deleted.

0 comments on commit 1905cca

Please sign in to comment.