From 062f0eb1955dd864802a00e362857f6a573b8ea7 Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 5 Sep 2020 13:57:40 +0200 Subject: [PATCH] added auto-selection of aspect ratio in posts view --- .../gxschannels/GxsChannelPostThumbnail.cpp | 14 ++++++++ .../gui/gxschannels/GxsChannelPostThumbnail.h | 9 +++++ .../gui/gxschannels/GxsChannelPostsModel.h | 1 + .../GxsChannelPostsWidgetWithModel.cpp | 33 +++++++++++++++---- 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/retroshare-gui/src/gui/gxschannels/GxsChannelPostThumbnail.cpp b/retroshare-gui/src/gui/gxschannels/GxsChannelPostThumbnail.cpp index 8ec55ad64..4a547602d 100644 --- a/retroshare-gui/src/gui/gxschannels/GxsChannelPostThumbnail.cpp +++ b/retroshare-gui/src/gui/gxschannels/GxsChannelPostThumbnail.cpp @@ -158,6 +158,20 @@ void ChannelPostThumbnailView::init(const RsGxsChannelPost& post) update(); } +ChannelPostThumbnailView::AspectRatio ChannelPostThumbnailView::bestAspectRatio() +{ + if(mPostImage->originalImage().isNull()) + return ASPECT_RATIO_1_1; + + float as = mPostImage->originalImage().height() / (float)mPostImage->originalImage().width() ; + + if(as < 0.8) + return ASPECT_RATIO_16_9; + else if(as < 1.15) + return ASPECT_RATIO_1_1; + else + return ASPECT_RATIO_2_3; +} QSize ChannelPostThumbnailView::actualSize() const { QFontMetricsF fm(font()); diff --git a/retroshare-gui/src/gui/gxschannels/GxsChannelPostThumbnail.h b/retroshare-gui/src/gui/gxschannels/GxsChannelPostThumbnail.h index bdf56af0e..f8c0f4650 100644 --- a/retroshare-gui/src/gui/gxschannels/GxsChannelPostThumbnail.h +++ b/retroshare-gui/src/gui/gxschannels/GxsChannelPostThumbnail.h @@ -46,6 +46,8 @@ public: QPixmap extractCroppedScaledPicture() const; void updateView(); + const QPixmap& originalImage() const { return mFullImage ; } + protected: void mousePressEvent(QMouseEvent *ev) override; void mouseReleaseEvent(QMouseEvent *ev) override; @@ -108,6 +110,13 @@ public: // The label however has a correct size. It seems that Qt doesn't like widgets with horizontal aspect ratio and forces the size accordingly. QSize actualSize() const ; + + /*! + * \brief bestAspectRatio + * Computes the preferred aspect ratio for the image in the post. The default is 1:1. + * \return the prefered aspect ratio + */ + AspectRatio bestAspectRatio() ; private: static const float DEFAULT_SIZE_IN_FONT_HEIGHT ; diff --git a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsModel.h b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsModel.h index 263e23059..931135840 100644 --- a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsModel.h +++ b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsModel.h @@ -108,6 +108,7 @@ public: std::vector > getPostVersions(const RsGxsMessageId& mid) const; uint32_t getNumberOfPosts() { return mPosts.size() ; } + const RsGxsChannelPost& post(uint32_t i) const { return mPosts[i]; } // This method will asynchroneously update the data void updateChannel(const RsGxsGroupId& channel_group_id); diff --git a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.cpp b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.cpp index 1c6687226..b692e133a 100644 --- a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.cpp +++ b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.cpp @@ -145,13 +145,13 @@ void ChannelPostDelegate::paint(QPainter * painter, const QStyleOptionViewItem & pixmap = pixmap.copy(QRect(0,0,w.actualSize().width(),w.actualSize().height())); - if(index.row()==0 && index.column()==0) - { - QFile file("yourFile.png"); - file.open(QIODevice::WriteOnly); - pixmap.save(&file, "PNG"); - file.close(); - } +// if(index.row()==0 && index.column()==0) +// { +// QFile file("yourFile.png"); +// file.open(QIODevice::WriteOnly); +// pixmap.save(&file, "PNG"); +// file.close(); +// } if(mUseGrid || index.column()==0) { @@ -787,6 +787,25 @@ void GxsChannelPostsWidgetWithModel::postChannelPostLoad() ui->channelFiles_TV->sortByColumn(0, Qt::AscendingOrder); ui->infoPosts->setText(QString::number(mChannelPostsModel->getNumberOfPosts()) + " / " + QString::number(mGroup.mMeta.mVisibleMsgCount)); + + // now compute aspect ratio for posts. We do that by looking at the 5 latest posts and compute the best aspect ratio for them. + + std::vector ar_votes(3,0); + + for(uint32_t i=0;igetNumberOfPosts(),5u);++i) + { + const RsGxsChannelPost& post = mChannelPostsModel->post(i); + ChannelPostThumbnailView v(post,ChannelPostThumbnailView::FLAG_SHOW_TEXT); + + ++ar_votes[ static_cast( v.bestAspectRatio() )]; + } + int best=0; + for(uint32_t i=0;i<3;++i) + if(ar_votes[i] > ar_votes[best]) + best = i; + + mChannelPostsDelegate->setAspectRatio(static_cast(best)); + mChannelPostsModel->triggerViewUpdate(); } void GxsChannelPostsWidgetWithModel::updateDisplay(bool complete)