Skip to content

Commit

Permalink
[layouts] Fix selected items appear in bold in all widgets
Browse files Browse the repository at this point in the history
instead of just the items list panel

E.g. we don't want selected items to show in bold in the item
combo boxes
  • Loading branch information
nyalldawson committed Dec 17, 2018
1 parent bebc34b commit 4131e0b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 13 deletions.
52 changes: 50 additions & 2 deletions src/app/layout/qgslayoutitemslistview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,54 @@
#include <QHeaderView>
#include <QMouseEvent>


QgsLayoutItemsListViewModel::QgsLayoutItemsListViewModel( QgsLayoutModel *model, QObject *parent )
: QSortFilterProxyModel( parent )
, mModel( model )
{
setSourceModel( mModel );
}

QgsLayoutItem *QgsLayoutItemsListViewModel::itemFromIndex( const QModelIndex &index ) const
{
return mModel->itemFromIndex( mapToSource( index ) );
}

void QgsLayoutItemsListViewModel::setSelected( const QModelIndex &index )
{
mModel->setSelected( mapToSource( index ) );
}

QVariant QgsLayoutItemsListViewModel::data( const QModelIndex &index, int role ) const
{
if ( !index.isValid() )
return QVariant();

QgsLayoutItem *item = itemFromIndex( index );
if ( !item )
{
return QVariant();
}

if ( role == Qt::FontRole )
{
if ( index.column() == QgsLayoutModel::ItemId && item->isSelected() )
{
//draw name of selected items in bold
QFont boldFont;
boldFont.setBold( true );
return boldFont;
}
}

return QSortFilterProxyModel::data( index, role );
}


//
// QgsLayoutItemsListView
//

QgsLayoutItemsListView::QgsLayoutItemsListView( QWidget *parent, QgsLayoutDesignerDialog *designer )
: QTreeView( parent )
, mDesigner( designer )
Expand All @@ -39,7 +87,7 @@ QgsLayoutItemsListView::QgsLayoutItemsListView( QWidget *parent, QgsLayoutDesign
void QgsLayoutItemsListView::setCurrentLayout( QgsLayout *layout )
{
mLayout = layout;
mModel = layout->itemsModel();
mModel = new QgsLayoutItemsListViewModel( layout->itemsModel(), this );
setModel( mModel );

header()->setSectionResizeMode( 0, QHeaderView::Fixed );
Expand All @@ -48,7 +96,7 @@ void QgsLayoutItemsListView::setCurrentLayout( QgsLayout *layout )
setColumnWidth( 1, Qgis::UI_SCALE_FACTOR * fontMetrics().width( QStringLiteral( "xxxx" ) ) );
header()->setSectionsMovable( false );

connect( selectionModel(), &QItemSelectionModel::currentChanged, mLayout->itemsModel(), &QgsLayoutModel::setSelected );
connect( selectionModel(), &QItemSelectionModel::currentChanged, mModel, &QgsLayoutItemsListViewModel::setSelected );
}

void QgsLayoutItemsListView::showContextMenu( QPoint point )
Expand Down
23 changes: 22 additions & 1 deletion src/app/layout/qgslayoutitemslistview.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,31 @@

#include "qgis.h"
#include <QTreeView>
#include <QSortFilterProxyModel>

class QgsLayout;
class QgsLayoutDesignerDialog;
class QgsLayoutModel;
class QgsLayoutItem;

class QgsLayoutItemsListViewModel : public QSortFilterProxyModel
{
Q_OBJECT

public:

QgsLayoutItemsListViewModel( QgsLayoutModel *model, QObject *parent );

QgsLayoutItem *itemFromIndex( const QModelIndex &index ) const; \
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;

public slots:
void setSelected( const QModelIndex &index );

private:

QgsLayoutModel *mModel = nullptr;
};

/**
* A list view for showing items in a layout
Expand All @@ -46,7 +67,7 @@ class QgsLayoutItemsListView : public QTreeView
private:

QgsLayout *mLayout = nullptr;
QgsLayoutModel *mModel = nullptr;
QgsLayoutItemsListViewModel *mModel = nullptr;
QgsLayoutDesignerDialog *mDesigner = nullptr;
};

Expand Down
10 changes: 0 additions & 10 deletions src/core/layout/qgslayoutmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,6 @@ QVariant QgsLayoutModel::data( const QModelIndex &index, int role ) const
return QVariant();
}

case Qt::FontRole:
if ( index.column() == ItemId && item->isSelected() )
{
//draw name of selected items in bold
QFont boldFont;
boldFont.setBold( true );
return boldFont;
}
return QVariant();

default:
return QVariant();
}
Expand Down

0 comments on commit 4131e0b

Please sign in to comment.