more work on list view in channels

This commit is contained in:
csoler 2020-08-27 18:53:39 +02:00
parent 38f1df2b36
commit f76264b170
4 changed files with 101 additions and 52 deletions

View File

@ -19,49 +19,77 @@
*******************************************************************************/
#include <QWheelEvent>
#include <QDateTime>
#include "gui/common/FilesDefs.h"
#include "gui/gxschannels/GxsChannelPostThumbnail.h"
ChannelPostThumbnailView::ChannelPostThumbnailView(const RsGxsChannelPost& post,QWidget *parent)
: QWidget(parent)
ChannelPostThumbnailView::ChannelPostThumbnailView(const RsGxsChannelPost& post,uint32_t flags,QWidget *parent)
: QWidget(parent),mFlags(flags)
{
// now fill the data
QPixmap thumbnail;
if(post.mThumbnail.mSize > 0)
GxsIdDetails::loadPixmapFromData(post.mThumbnail.mData, post.mThumbnail.mSize, thumbnail,GxsIdDetails::ORIGINAL);
else if(post.mMeta.mPublishTs > 0) // this is for testing that the post is not an empty post (happens at the end of the last row)
thumbnail = FilesDefs::getPixmapFromQtResourcePath(CHAN_DEFAULT_IMAGE);
init(thumbnail, QString::fromUtf8(post.mMeta.mMsgName.c_str()), IS_MSG_UNREAD(post.mMeta.mMsgStatus) || IS_MSG_NEW(post.mMeta.mMsgStatus) );
init(post);
}
ChannelPostThumbnailView::ChannelPostThumbnailView(QWidget *parent)
: QWidget(parent)
ChannelPostThumbnailView::ChannelPostThumbnailView(QWidget *parent,uint32_t flags)
: QWidget(parent),mFlags(flags)
{
init(FilesDefs::getPixmapFromQtResourcePath(CHAN_DEFAULT_IMAGE), QString("New post"),false);
init(RsGxsChannelPost());
}
ChannelPostThumbnailView::~ChannelPostThumbnailView()
{
delete lb;
delete lt;
delete mPostImage;
}
void ChannelPostThumbnailView::init(const QPixmap& thumbnail,const QString& msg,bool is_msg_new)
void ChannelPostThumbnailView::init(const RsGxsChannelPost& post)
{
QString msg = QString::fromUtf8(post.mMeta.mMsgName.c_str());
bool is_msg_new = IS_MSG_UNREAD(post.mMeta.mMsgStatus) || IS_MSG_NEW(post.mMeta.mMsgStatus);
QPixmap thumbnail;
if(post.mThumbnail.mSize > 0)
GxsIdDetails::loadPixmapFromData(post.mThumbnail.mData, post.mThumbnail.mSize, thumbnail,GxsIdDetails::ORIGINAL);
else if(post.mMeta.mPublishTs > 0) // this is for testing that the post is not an empty post (happens at the end of the last row)
thumbnail = FilesDefs::getPixmapFromQtResourcePath(CHAN_DEFAULT_IMAGE);
mPostImage = new ZoomableLabel(this);
mPostImage->setEnableZoom(mFlags & FLAG_ALLOW_PAN);
mPostImage->setScaledContents(true);
mPostImage->setPicture(thumbnail);
if(mFlags & FLAG_ALLOW_PAN)
mPostImage->setToolTip(tr("Use mouse to center and zoom into the image"));
mPostTitle = new QLabel(this);
if(mFlags & FLAG_SHOW_TEXT)
{
QBoxLayout *layout = new QHBoxLayout(this);
layout->addWidget(mPostImage);
QVBoxLayout *vlayout = new QVBoxLayout(this);
mPostTitle->setText(msg);
vlayout->addWidget(mPostTitle);
QLabel *date_label = new QLabel(this);
date_label->setText(QDateTime::fromSecsSinceEpoch(post.mMeta.mPublishTs).toString());
vlayout->addWidget(date_label);
vlayout->addStretch();
layout->addLayout(vlayout);
setLayout(layout);
}
else
{
QVBoxLayout *layout = new QVBoxLayout(this);
lb = new ZoomableLabel(this);
lb->setScaledContents(true);
lb->setToolTip(tr("Use mouse to center and zoom into the image"));
layout->addWidget(lb);
lt = new QLabel(this);
layout->addWidget(lt);
layout->addWidget(mPostImage);
layout->addWidget(mPostTitle);
setLayout(layout);
setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum);
@ -70,28 +98,33 @@ void ChannelPostThumbnailView::init(const QPixmap& thumbnail,const QString& msg,
int W = THUMBNAIL_OVERSAMPLE_FACTOR * THUMBNAIL_W * fm.height() ;
int H = THUMBNAIL_OVERSAMPLE_FACTOR * THUMBNAIL_H * fm.height() ;
lb->setFixedSize(W,H);
lb->setPicture(thumbnail);
mPostImage->setFixedSize(W,H);
setText(msg);
QString ss = (msg.length() > 30)? (msg.left(30)+"..."):msg;
QFont font = lt->font();
mPostTitle->setText(ss);
QFont font = mPostTitle->font();
if(is_msg_new)
{
font.setBold(true);
lt->setFont(font);
font.setBold(true);
mPostTitle->setFont(font);
}
lt->setMaximumWidth(W);
lt->setWordWrap(true);
mPostTitle->setMaximumWidth(W);
mPostTitle->setWordWrap(true);
}
adjustSize();
update();
adjustSize();
update();
}
void ZoomableLabel::mouseMoveEvent(QMouseEvent *me)
{
if(!mZoomEnabled)
return;
float new_center_x = mCenterX - (me->x() - mLastX);
float new_center_y = mCenterY - (me->y() - mLastY);
@ -121,6 +154,9 @@ void ZoomableLabel::mouseReleaseEvent(QMouseEvent *)
}
void ZoomableLabel::wheelEvent(QWheelEvent *me)
{
if(!mZoomEnabled)
return;
float new_zoom_factor = (me->delta() > 0)?(mZoomFactor*1.05):(mZoomFactor/1.05);
float new_center_x = mCenterX;
float new_center_y = mCenterY;

View File

@ -38,9 +38,10 @@
class ZoomableLabel: public QLabel
{
public:
ZoomableLabel(QWidget *parent): QLabel(parent),mZoomFactor(1.0),mCenterX(0.0),mCenterY(0.0) {}
ZoomableLabel(QWidget *parent): QLabel(parent),mZoomFactor(1.0),mCenterX(0.0),mCenterY(0.0),mZoomEnabled(true) {}
void setPicture(const QPixmap& pix);
void setEnableZoom(bool b) { mZoomEnabled = b; }
QPixmap extractCroppedScaledPicture() const;
protected:
@ -57,8 +58,9 @@ protected:
float mCenterX;
float mCenterY;
float mZoomFactor;
int mLastX,mLastY;
int mLastX,mLastY;
bool mMoving;
bool mZoomEnabled;
};
// Class to paint the thumbnails with title
@ -71,6 +73,10 @@ public:
// 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;
static constexpr uint32_t FLAG_NONE = 0x00;
static constexpr uint32_t FLAG_SHOW_TEXT = 0x01;
static constexpr uint32_t FLAG_ALLOW_PAN = 0x02;
// Size of thumbnails as a function of the height of the font. An aspect ratio of 3/4 is good.
static const int THUMBNAIL_W = 4;
@ -79,13 +85,13 @@ public:
static constexpr char *CHAN_DEFAULT_IMAGE = ":images/thumb-default-video.png";
virtual ~ChannelPostThumbnailView();
ChannelPostThumbnailView(QWidget *parent=NULL);
ChannelPostThumbnailView(const RsGxsChannelPost& post,QWidget *parent=NULL);
ChannelPostThumbnailView(QWidget *parent=NULL,uint32_t flags=FLAG_ALLOW_PAN);
ChannelPostThumbnailView(const RsGxsChannelPost& post,uint32_t flags,QWidget *parent=NULL);
void init(const QPixmap& thumbnail,const QString& msg,bool is_msg_new);
void init(const RsGxsChannelPost& post);
void setPixmap(const QPixmap& p) { lb->setPicture(p); }
QPixmap getCroppedScaledPicture() const { return lb->extractCroppedScaledPicture() ; }
void setPixmap(const QPixmap& p) { mPostImage->setPicture(p); }
QPixmap getCroppedScaledPicture() const { return mPostImage->extractCroppedScaledPicture() ; }
void setText(const QString& s)
{
@ -95,11 +101,12 @@ public:
else
ss =s;
lt->setText(ss);
mPostTitle->setText(ss);
}
private:
ZoomableLabel *lb;
QLabel *lt;
ZoomableLabel *mPostImage;
QLabel *mPostTitle;
uint32_t mFlags;
};

View File

@ -84,9 +84,12 @@ Q_DECLARE_METATYPE(ChannelPostFileInfo)
//=== ChannelPostDelegate ===//
//===============================================================================================================================================//
int ChannelPostDelegate::cellSize(const QFont& font) const
int ChannelPostDelegate::cellSize(const QFont& font,uint32_t parent_width) const
{
return mZoom*COLUMN_SIZE_FONT_FACTOR_W*QFontMetricsF(font).height();
if(mUseGrid)
return mZoom*COLUMN_SIZE_FONT_FACTOR_W*QFontMetricsF(font).height();
else
return parent_width;
}
void ChannelPostDelegate::zoom(bool zoom_or_unzoom)
@ -110,7 +113,8 @@ void ChannelPostDelegate::paint(QPainter * painter, const QStyleOptionViewItem &
painter->fillRect( option.rect, option.backgroundBrush);
painter->restore();
ChannelPostThumbnailView w(post);
uint32_t flags = (mUseGrid)?0:(ChannelPostThumbnailView::FLAG_SHOW_TEXT);
ChannelPostThumbnailView w(post,flags);
QPixmap pixmap(w.size());
@ -282,7 +286,7 @@ GxsChannelPostsWidgetWithModel::GxsChannelPostsWidgetWithModel(const RsGxsGroupI
QFontMetricsF fm(font());
for(int i=0;i<mChannelPostsModel->columnCount();++i)
ui->postsTree->setColumnWidth(i,mChannelPostsDelegate->cellSize(font()));
ui->postsTree->setColumnWidth(i,mChannelPostsDelegate->cellSize(font(),ui->postsTree->width()));
/* Setup UI helper */
@ -350,11 +354,11 @@ void GxsChannelPostsWidgetWithModel::updateZoomFactor(bool zoom_or_unzoom)
mChannelPostsDelegate->zoom(zoom_or_unzoom);
for(int i=0;i<mChannelPostsModel->columnCount();++i)
ui->postsTree->setColumnWidth(i,mChannelPostsDelegate->cellSize(font()));
ui->postsTree->setColumnWidth(i,mChannelPostsDelegate->cellSize(font(),ui->postsTree->width()));
QSize s = ui->postsTree->size();
int n_columns = std::max(1,(int)floor(s.width() / (mChannelPostsDelegate->cellSize(font()))));
int n_columns = std::max(1,(int)floor(s.width() / (mChannelPostsDelegate->cellSize(font(),s.width()))));
std::cerr << "nb columns: " << n_columns << std::endl;
mChannelPostsModel->setNumColumns(n_columns); // forces the update
@ -402,6 +406,8 @@ void GxsChannelPostsWidgetWithModel::switchView()
{
mChannelPostsDelegate->setWidgetGrid(true);
mChannelPostsModel->setMode(RsGxsChannelPostsModel::TREE_MODE_GRID);
handlePostsTreeSizeChange(ui->postsTree->size());
}
}
void GxsChannelPostsWidgetWithModel::copyMessageLink()
@ -447,7 +453,7 @@ void GxsChannelPostsWidgetWithModel::editPost()
void GxsChannelPostsWidgetWithModel::handlePostsTreeSizeChange(QSize s)
{
int n_columns = std::max(1,(int)floor(s.width() / (mChannelPostsDelegate->cellSize(font()))));
int n_columns = std::max(1,(int)floor(s.width() / (mChannelPostsDelegate->cellSize(font(),ui->postsTree->width()))));
std::cerr << "nb columns: " << n_columns << std::endl;
if(n_columns != mChannelPostsModel->columnCount())

View File

@ -66,7 +66,7 @@ class ChannelPostDelegate: public QAbstractItemDelegate
void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const override;
QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const override;
int cellSize(const QFont& font) const;
int cellSize(const QFont& font,uint32_t parent_width) const;
void zoom(bool zoom_or_unzoom) ;
void setWidgetGrid(bool use_grid) ;