added selection of thumbnail aspect ratio

This commit is contained in:
csoler 2020-09-04 19:45:06 +02:00
parent 80875dddd7
commit bebc9f2863
5 changed files with 168 additions and 48 deletions

View File

@ -78,10 +78,11 @@ CreateGxsChannelMsg::CreateGxsChannelMsg(const RsGxsGroupId &cId, RsGxsMessageId
connect(thumbNailCb, SIGNAL(toggled(bool)), this, SLOT(allowAutoMediaThumbNail(bool)));
connect(stackedWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenu(QPoint)));
connect(generateCheckBox, SIGNAL(toggled(bool)), generateSpinBox, SLOT(setEnabled(bool)));
connect(aspectRatio_CB,SIGNAL(currentIndexChanged(int)),this,SLOT(changeAspectRatio(int)));
generateSpinBox->setEnabled(false);
preview_W->setPixmap(FilesDefs::getPixmapFromQtResourcePath(ChannelPostThumbnailView::CHAN_DEFAULT_IMAGE));
preview_W->setPixmap(FilesDefs::getPixmapFromQtResourcePath(ChannelPostThumbnailView::CHAN_DEFAULT_IMAGE),true);
preview_W->setText("[Text preview]");
thumbNailCb->setVisible(false);
@ -110,6 +111,19 @@ CreateGxsChannelMsg::~CreateGxsChannelMsg()
#endif
}
void CreateGxsChannelMsg::changeAspectRatio(int s)
{
switch(s)
{
case 0: break;
case 1: preview_W->setAspectRatio(ChannelPostThumbnailView::ASPECT_RATIO_1_1);
break;
case 2: preview_W->setAspectRatio(ChannelPostThumbnailView::ASPECT_RATIO_2_3);
break;
case 3: preview_W->setAspectRatio(ChannelPostThumbnailView::ASPECT_RATIO_16_9);
break;
}
}
void CreateGxsChannelMsg::contextMenu(QPoint /*point*/)
{
QList<RetroShareLink> links ;
@ -734,7 +748,8 @@ void CreateGxsChannelMsg::addThumbnail()
picture = img;
// to show the selected
preview_W->setPixmap(picture);
preview_W->setPixmap(picture, aspectRatio_CB->currentIndex()==0);
}
void CreateGxsChannelMsg::loadOriginalChannelPostInfo()
@ -778,7 +793,7 @@ void CreateGxsChannelMsg::loadOriginalChannelPostInfo()
if(post.mThumbnail.mData != NULL)
{
GxsIdDetails::loadPixmapFromData(post.mThumbnail.mData,post.mThumbnail.mSize,picture,GxsIdDetails::ORIGINAL);
preview_W->setPixmap(picture);
preview_W->setPixmap(picture,true);
}

View File

@ -64,6 +64,7 @@ private slots:
void sendMsg();
void pasteLink() ;
void contextMenu(QPoint) ;
void changeAspectRatio(int s);
void addThumbnail();
void allowAutoMediaThumbNail(bool);

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>736</width>
<height>271</height>
<width>881</width>
<height>383</height>
</rect>
</property>
<property name="acceptDrops">
@ -79,32 +79,28 @@
<number>0</number>
</property>
<widget class="QWidget" name="stackedWidgetPage1">
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="spacing">
<number>6</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="spacing">
<number>6</number>
</property>
<property name="sizeConstraint">
<enum>QLayout::SetFixedSize</enum>
</property>
<item>
<widget class="ChannelPostThumbnailView" name="preview_W" native="true"/>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="ChannelPostThumbnailView" name="preview_W" native="true"/>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
@ -179,6 +175,30 @@ p, li { white-space: pre-wrap; }
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="aspectRatio_CB">
<item>
<property name="text">
<string>Auto</string>
</property>
</item>
<item>
<property name="text">
<string>1:1</string>
</property>
</item>
<item>
<property name="text">
<string>3:4</string>
</property>
</item>
<item>
<property name="text">
<string>16:9</string>
</property>
</item>
</widget>
</item>
<item>
<widget class="QPushButton" name="addfilepushButton">
<property name="text">
@ -309,7 +329,7 @@ p, li { white-space: pre-wrap; }
<rect>
<x>0</x>
<y>0</y>
<width>81</width>
<width>84</width>
<height>24</height>
</rect>
</property>

View File

@ -25,7 +25,7 @@
#include "gui/gxschannels/GxsChannelPostThumbnail.h"
ChannelPostThumbnailView::ChannelPostThumbnailView(const RsGxsChannelPost& post,uint32_t flags,QWidget *parent)
: QWidget(parent),mFlags(flags),mPostTitle(nullptr)
: QWidget(parent),mPostTitle(nullptr),mFlags(flags), mAspectRatio(ASPECT_RATIO_2_3)
{
// now fill the data
@ -33,7 +33,7 @@ ChannelPostThumbnailView::ChannelPostThumbnailView(const RsGxsChannelPost& post,
}
ChannelPostThumbnailView::ChannelPostThumbnailView(QWidget *parent,uint32_t flags)
: QWidget(parent),mFlags(flags)
: QWidget(parent),mFlags(flags), mAspectRatio(ASPECT_RATIO_2_3)
{
init(RsGxsChannelPost());
}
@ -43,6 +43,54 @@ ChannelPostThumbnailView::~ChannelPostThumbnailView()
delete mPostImage;
}
void ChannelPostThumbnailView::setText(const QString& s)
{
if(mPostTitle == NULL)
{
std::cerr << "(EE) calling setText on a ChannelPostThumbnailView without SHOW_TEXT flag!"<< std::endl;
return;
}
QString ss;
if(s.length() > 30)
ss = s.left(30)+"...";
else
ss =s;
mPostTitle->setText(ss);
}
void ChannelPostThumbnailView::setPixmap(const QPixmap& p, bool guess_aspect_ratio)
{
mPostImage->setPicture(p);
if(guess_aspect_ratio)// aspect ratio is automatically guessed.
{
// compute closest aspect ratio
float r = p.width()/(float)p.height();
if(r < 0.8)
setAspectRatio(ChannelPostThumbnailView::ASPECT_RATIO_2_3);
else if(r < 1.15)
setAspectRatio(ChannelPostThumbnailView::ASPECT_RATIO_1_1);
else
setAspectRatio(ChannelPostThumbnailView::ASPECT_RATIO_16_9);
}
}
void ChannelPostThumbnailView::setAspectRatio(AspectRatio r)
{
mAspectRatio = r;
QFontMetricsF fm(font());
int W = THUMBNAIL_OVERSAMPLE_FACTOR * thumbnail_w() * fm.height() ;
int H = THUMBNAIL_OVERSAMPLE_FACTOR * thumbnail_h() * fm.height() ;
mPostImage->setFixedSize(W,H);
mPostImage->reset();
mPostImage->updateView();
}
void ChannelPostThumbnailView::init(const RsGxsChannelPost& post)
{
QString msg = QString::fromUtf8(post.mMeta.mMsgName.c_str());
@ -70,8 +118,8 @@ void ChannelPostThumbnailView::init(const RsGxsChannelPost& post)
setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum);
QFontMetricsF fm(font());
int W = THUMBNAIL_OVERSAMPLE_FACTOR * THUMBNAIL_W * fm.height() ;
int H = THUMBNAIL_OVERSAMPLE_FACTOR * THUMBNAIL_H * fm.height() ;
int W = THUMBNAIL_OVERSAMPLE_FACTOR * thumbnail_w() * fm.height() ;
int H = THUMBNAIL_OVERSAMPLE_FACTOR * thumbnail_h() * fm.height() ;
mPostImage->setFixedSize(W,H);
@ -101,6 +149,39 @@ void ChannelPostThumbnailView::init(const RsGxsChannelPost& post)
update();
}
float ChannelPostThumbnailView::thumbnail_w() const
{
switch(mAspectRatio)
{
default:
case ASPECT_RATIO_1_1:
case ASPECT_RATIO_UNKNOWN: return 5;
case ASPECT_RATIO_2_3: return 4;
case ASPECT_RATIO_16_9: return 5 * 16.0/9.0;
}
}
float ChannelPostThumbnailView::thumbnail_h() const
{
switch(mAspectRatio)
{
default:
case ASPECT_RATIO_1_1:
case ASPECT_RATIO_UNKNOWN: return 5;
case ASPECT_RATIO_2_3: return 6;
case ASPECT_RATIO_16_9: return 5;
}
}
void ZoomableLabel::reset()
{
mCenterX = mFullImage.width()/2.0;
mCenterX = mFullImage.height()/2.0;
mZoomFactor = 1.0/std::max(width() / (float)mFullImage.width(), height()/(float)mFullImage.height());
updateView();
}
void ZoomableLabel::mouseMoveEvent(QMouseEvent *me)
{
if(!mZoomEnabled)
@ -204,3 +285,5 @@ void ZoomableLabel::updateView()

View File

@ -42,7 +42,9 @@ public:
void setPicture(const QPixmap& pix);
void setEnableZoom(bool b) { mZoomEnabled = b; }
void reset();
QPixmap extractCroppedScaledPicture() const;
void updateView();
protected:
void mousePressEvent(QMouseEvent *ev) override;
@ -51,8 +53,6 @@ protected:
void resizeEvent(QResizeEvent *ev) override;
void wheelEvent(QWheelEvent *me) override;
void updateView();
QPixmap mFullImage;
float mCenterX;
@ -70,6 +70,13 @@ class ChannelPostThumbnailView: public QWidget
Q_OBJECT
public:
typedef enum {
ASPECT_RATIO_UNKNOWN = 0x00,
ASPECT_RATIO_2_3 = 0x01,
ASPECT_RATIO_1_1 = 0x02,
ASPECT_RATIO_16_9 = 0x03,
} AspectRatio;
// This variable determines the zoom factor on the text below thumbnails. 2.0 is mostly correct for all screen.
static constexpr float THUMBNAIL_OVERSAMPLE_FACTOR = 2.0;
@ -90,24 +97,18 @@ public:
void init(const RsGxsChannelPost& post);
void setPixmap(const QPixmap& p) { mPostImage->setPicture(p); }
void setAspectRatio(AspectRatio r);
void setPixmap(const QPixmap& p,bool guess_aspect_ratio) ;
QPixmap getCroppedScaledPicture() const { return mPostImage->extractCroppedScaledPicture() ; }
void setText(const QString& s)
{
QString ss;
if(s.length() > 30)
ss = s.left(30)+"...";
else
ss =s;
if(mPostTitle != NULL)
mPostTitle->setText(ss);
}
void setText(const QString& s);
private:
float thumbnail_w() const;
float thumbnail_h() const;
ZoomableLabel *mPostImage;
QLabel *mPostTitle;
uint32_t mFlags;
AspectRatio mAspectRatio;
};