Skip to content

Commit

Permalink
Add dismiss overlay icon to news entries
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 15, 2019
1 parent dc8ab9a commit b0258d6
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 9 deletions.
1 change: 1 addition & 0 deletions images/images.qrc
Expand Up @@ -767,6 +767,7 @@
<file>themes/default/mIconAlignRight.svg</file>
<file>themes/default/mIconArrangeSymbolsLeft.svg</file>
<file>themes/default/mIconArrangeSymbolsRight.svg</file>
<file>themes/default/mIconClearNews.svg</file>
</qresource>
<qresource prefix="/images/tips">
<file alias="symbol_levels.png">qgis_tips/symbol_levels.png</file>
Expand Down
1 change: 1 addition & 0 deletions images/themes/default/mIconClearNews.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions src/app/qgsprojectlistitemdelegate.cpp
Expand Up @@ -17,6 +17,7 @@
#include "qgis.h"
#include "qgsnewsfeedmodel.h"
#include "qgswebframe.h"
#include "qgsapplication.h"

#include <QApplication>
#include <QPainter>
Expand Down Expand Up @@ -181,6 +182,7 @@ bool QgsProjectPreviewImage::isNull() const
QgsNewsItemListItemDelegate::QgsNewsItemListItemDelegate( QObject *parent )
: QStyledItemDelegate( parent )
, mRoundedRectSizePixels( static_cast<int>( Qgis::UI_SCALE_FACTOR * QApplication::fontMetrics().height() * 0.5 ) )
, mDismissRectSize( 20, 20 ) // TODO - hidpi friendly
{

}
Expand Down Expand Up @@ -242,6 +244,12 @@ void QgsNewsItemListItemDelegate::paint( QPainter *painter, const QStyleOptionVi
painter->drawPixmap( option.rect.left() + 1.25 * mRoundedRectSizePixels, option.rect.top() + 1.25 * mRoundedRectSizePixels, icon );
}

// Gross, but not well supported in Qt
mDismissRect = QRect( option.rect.width() - 32, option.rect.top() + 10, mDismissRectSize.width(), mDismissRectSize.height() );
QPixmap pixmap = QgsApplication::getThemeIcon( QStringLiteral( "/mIconClearNews.svg" ) ).pixmap( mDismissRectSize, QIcon::Normal );
painter->drawPixmap( mDismissRect.topLeft(), pixmap );
mDismissRect.setTop( 10 );

painter->translate( option.rect.left() + ( !icon.isNull() ? icon.width() + 3.125 * mRoundedRectSizePixels : 1.875 * mRoundedRectSizePixels ), option.rect.top() + 1.875 * mRoundedRectSizePixels );
ctx.clip = QRect( 0, 0, option.rect.width() - ( !icon.isNull() ? icon.width() - 4.375 * mRoundedRectSizePixels : 3.125 * mRoundedRectSizePixels ), option.rect.height() - 3.125 * mRoundedRectSizePixels );
doc.documentLayout()->draw( painter, ctx );
Expand Down Expand Up @@ -275,3 +283,4 @@ QSize QgsNewsItemListItemDelegate::sizeHint( const QStyleOptionViewItem &option,
return QSize( width, std::max( ( double ) doc.size().height() + 1.25 * mRoundedRectSizePixels, static_cast<double>( icon.height() ) ) + 2.5 * mRoundedRectSizePixels );
}


9 changes: 9 additions & 0 deletions src/app/qgsprojectlistitemdelegate.h
Expand Up @@ -73,10 +73,19 @@ class QgsNewsItemListItemDelegate : public QStyledItemDelegate
void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const override;

/**
* Returns the area corresponding to the dismiss rectangle.
*/
QRect dismissRect() const { return mDismissRect; }

QSize dismissRectSize() const { return mDismissRectSize; }

private:

int mRoundedRectSizePixels = 5;
QColor mColor = Qt::white;
mutable QRect mDismissRect;
QSize mDismissRectSize;
};

#endif // QGSPROJECTLISTITEMDELEGATE_H
37 changes: 29 additions & 8 deletions src/app/qgswelcomepage.cpp
Expand Up @@ -104,16 +104,15 @@ QgsWelcomePage::QgsWelcomePage( bool skipVersionCheck, QWidget *parent )
mNewsFeedListView->setResizeMode( QListView::Adjust );
mNewsFeedListView->setModel( mNewsFeedModel );
mNewsFeedListView->setVerticalScrollMode( QAbstractItemView::ScrollPerPixel );
QgsNewsItemListItemDelegate *newsItemDelegate = new QgsNewsItemListItemDelegate( mNewsFeedListView );
mNewsFeedListView->setItemDelegate( newsItemDelegate );
mNewsDelegate = new QgsNewsItemListItemDelegate( mNewsFeedListView );
mNewsFeedListView->setItemDelegate( mNewsDelegate );
mNewsFeedListView->setContextMenuPolicy( Qt::CustomContextMenu );
mNewsFeedListView->viewport()->installEventFilter( this );
connect( mNewsFeedListView, &QListView::customContextMenuRequested, this, &QgsWelcomePage::showContextMenuForNews );
connect( mNewsFeedParser, &QgsNewsFeedParser::entryDismissed, this, &QgsWelcomePage::updateNewsFeedVisibility );
newsLayout->addWidget( mNewsFeedListView, 1 );
updateNewsFeedVisibility();
connect( mNewsFeedParser, &QgsNewsFeedParser::fetched, this, [ = ]
{
updateNewsFeedVisibility();
} );
connect( mNewsFeedParser, &QgsNewsFeedParser::fetched, this, &QgsWelcomePage::updateNewsFeedVisibility );
mNewsFeedParser->fetch();
newsContainer->setLayout( newsLayout );
mSplitter2->addWidget( newsContainer );
Expand Down Expand Up @@ -351,7 +350,6 @@ void QgsWelcomePage::showContextMenuForNews( QPoint point )
connect( dismissAllAction, &QAction::triggered, this, [this]
{
mNewsFeedParser->dismissAll();
updateNewsFeedVisibility();
} );
menu->addAction( dismissAllAction );
menu->addSeparator();
Expand All @@ -362,7 +360,6 @@ void QgsWelcomePage::showContextMenuForNews( QPoint point )
{
//...sad trombone...
mNewsFeedParser->dismissAll();
updateNewsFeedVisibility();
QgsSettings().setValue( QStringLiteral( "%1/disabled" ).arg( QgsNewsFeedParser::keyForFeed( QStringLiteral( FEED_URL ) ) ), true, QgsSettings::Core );
}
} );
Expand Down Expand Up @@ -392,3 +389,27 @@ void QgsWelcomePage::updateNewsFeedVisibility()
mSplitter2->restoreState( QgsSettings().value( QStringLiteral( "Windows/WelcomePage/SplitState2" ), QVariant(), QgsSettings::App ).toByteArray() );
}
}

bool QgsWelcomePage::eventFilter( QObject *obj, QEvent *event )
{
if ( obj == mNewsFeedListView->viewport() && event->type() == QEvent::MouseButtonRelease )
{
QMouseEvent *mouseEvent = dynamic_cast< QMouseEvent *>( event );
if ( mouseEvent->button() == Qt::LeftButton )
{
QModelIndex index = mNewsFeedListView->indexAt( mouseEvent->pos() );
if ( index.isValid() )
{
const QPoint itemClickPoint = mouseEvent->pos() - mNewsFeedListView->visualRect( index ).topLeft();
if ( QRect( mNewsDelegate->dismissRect().left(), mNewsDelegate->dismissRect().top(), mNewsDelegate->dismissRectSize().width(), mNewsDelegate->dismissRectSize().height() ).contains( itemClickPoint ) )
{
mNewsFeedParser->dismissEntry( index.data( QgsNewsFeedModel::Key ).toInt() );
}
return true;
}
}
}

return QWidget::eventFilter( obj, event );
}

7 changes: 6 additions & 1 deletion src/app/qgswelcomepage.h
Expand Up @@ -29,6 +29,7 @@ class QLabel;
class QSplitter;
class QgsNewsFeedParser;
class QgsNewsFeedProxyModel;
class QgsNewsItemListItemDelegate;

class QgsWelcomePage : public QWidget
{
Expand All @@ -46,17 +47,20 @@ class QgsWelcomePage : public QWidget
void projectPinned( int row );
void projectUnpinned( int row );

protected:
bool eventFilter( QObject *obj, QEvent *event ) override;

private slots:
void recentProjectItemActivated( const QModelIndex &index );
void templateProjectItemActivated( const QModelIndex &index );
void versionInfoReceived();
void showContextMenuForProjects( QPoint point );
void showContextMenuForTemplates( QPoint point );
void showContextMenuForNews( QPoint point );
void updateNewsFeedVisibility();

private:
void updateRecentProjectsVisibility();
void updateNewsFeedVisibility();

QgsRecentProjectItemsModel *mRecentProjectsModel = nullptr;
QTextBrowser *mVersionInformation = nullptr;
Expand All @@ -71,6 +75,7 @@ class QgsWelcomePage : public QWidget
QgsNewsFeedParser *mNewsFeedParser = nullptr;
QgsNewsFeedProxyModel *mNewsFeedModel = nullptr;
QListView *mNewsFeedListView = nullptr;
QgsNewsItemListItemDelegate *mNewsDelegate = nullptr;
};

#endif // QGSWELCOMEPAGE_H

0 comments on commit b0258d6

Please sign in to comment.