fixed sizes

This commit is contained in:
csoler 2020-08-27 22:04:09 +02:00
parent 931c1a7286
commit e7ece78673
4 changed files with 73 additions and 83 deletions

View File

@ -63,43 +63,22 @@ void ChannelPostThumbnailView::init(const RsGxsChannelPost& post)
if(mFlags & FLAG_ALLOW_PAN)
mPostImage->setToolTip(tr("Use mouse to center and zoom into the image"));
mPostTitle = new QLabel(this);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(mPostImage);
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() ;
mPostImage->setFixedSize(W,H);
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);
layout->addSpacing(Qt::Horizontal);
setLayout(layout);
}
else
{
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(mPostImage);
mPostTitle = new QLabel(this);
layout->addWidget(mPostTitle);
setLayout(layout);
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() ;
mPostImage->setFixedSize(W,H);
QString ss = (msg.length() > 30)? (msg.left(30)+"..."):msg;
@ -117,6 +96,7 @@ void ChannelPostThumbnailView::init(const RsGxsChannelPost& post)
mPostTitle->setWordWrap(true);
}
setLayout(layout);
adjustSize();
update();
}

View File

@ -67,7 +67,7 @@ void RsGxsChannelPostsModel::setMode(TreeMode mode)
mTreeMode = mode;
if(mode == TREE_MODE_LIST)
setNumColumns(1);
setNumColumns(2);
// needs some update here
}
@ -212,7 +212,10 @@ int RsGxsChannelPostsModel::rowCount(const QModelIndex& parent) const
return 0;
if(!parent.isValid())
return (mFilteredPosts.size() + mColumns-1)/mColumns; // mFilteredPosts always has an item at 0, so size()>=1, and mColumn>=1
if(mTreeMode == TREE_MODE_GRID)
return (mFilteredPosts.size() + mColumns-1)/mColumns; // mFilteredPosts always has an item at 0, so size()>=1, and mColumn>=1
else
return mFilteredPosts.size();
RsErr() << __PRETTY_FUNCTION__ << " rowCount cannot figure out the porper number of rows." << std::endl;
return 0;
@ -220,7 +223,10 @@ int RsGxsChannelPostsModel::rowCount(const QModelIndex& parent) const
int RsGxsChannelPostsModel::columnCount(const QModelIndex &/*parent*/) const
{
return std::min((int)mFilteredPosts.size(),(int)mColumns) ;
if(mTreeMode == TREE_MODE_GRID)
return std::min((int)mFilteredPosts.size(),(int)mColumns) ;
else
return 2;
}
bool RsGxsChannelPostsModel::getPostData(const QModelIndex& i,RsGxsChannelPost& fmpe) const
@ -287,7 +293,7 @@ QModelIndex RsGxsChannelPostsModel::index(int row, int column, const QModelIndex
if(row < 0 || column < 0 || column >= (int)mColumns)
return QModelIndex();
quintptr ref = getChildRef(parent.internalId(),column + row*mColumns);
quintptr ref = getChildRef(parent.internalId(),(mTreeMode == TREE_MODE_GRID)?(column + row*mColumns):row);
#ifdef DEBUG_CHANNEL_MODEL
std::cerr << "index-3(" << row << "," << column << " parent=" << parent << ") : " << createIndex(row,column,ref) << std::endl;
@ -723,15 +729,18 @@ QModelIndex RsGxsChannelPostsModel::getIndexOfMessage(const RsGxsMessageId& mid)
for(uint32_t i=0;i<mFilteredPosts.size();++i)
{
// First look into msg versions, in case the msg is a version of an existing message
// First look into msg versions, in case the msg is a version of an existing message
for(auto& msg_id:mPosts[mFilteredPosts[i]].mOlderVersions)
if(msg_id == postId)
{
quintptr ref ;
convertTabEntryToRefPointer(i,ref); // we dont use i+1 here because i is not a row, but an index in the mPosts tab
quintptr ref ;
convertTabEntryToRefPointer(i,ref); // we dont use i+1 here because i is not a row, but an index in the mPosts tab
return createIndex(i/mColumns,i%mColumns, ref);
if(mTreeMode == TREE_MODE_GRID)
return createIndex(i/mColumns,i%mColumns, ref);
else
return createIndex(i,0, ref);
}
if(mPosts[mFilteredPosts[i]].mMeta.mMsgId == postId)
@ -739,7 +748,10 @@ QModelIndex RsGxsChannelPostsModel::getIndexOfMessage(const RsGxsMessageId& mid)
quintptr ref ;
convertTabEntryToRefPointer(i,ref); // we dont use i+1 here because i is not a row, but an index in the mPosts tab
return createIndex(i/mColumns,i%mColumns, ref);
if(mTreeMode == TREE_MODE_GRID)
return createIndex(i/mColumns,i%mColumns, ref);
else
return createIndex(i,0, ref);
}
}

View File

@ -84,12 +84,12 @@ Q_DECLARE_METATYPE(ChannelPostFileInfo)
//=== ChannelPostDelegate ===//
//===============================================================================================================================================//
int ChannelPostDelegate::cellSize(const QFont& font,uint32_t parent_width) const
int ChannelPostDelegate::cellSize(int col,const QFont& font,uint32_t parent_width) const
{
if(mUseGrid)
if(mUseGrid || col==0)
return mZoom*COLUMN_SIZE_FONT_FACTOR_W*QFontMetricsF(font).height();
else
return parent_width;
return parent_width - mZoom*COLUMN_SIZE_FONT_FACTOR_W*QFontMetricsF(font).height();
}
void ChannelPostDelegate::zoom(bool zoom_or_unzoom)
@ -104,20 +104,20 @@ void ChannelPostDelegate::zoom(bool zoom_or_unzoom)
void ChannelPostDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
// prepare
painter->save();
painter->setClipRect(option.rect);
// prepare
painter->save();
painter->setClipRect(option.rect);
RsGxsChannelPost post = index.data(Qt::UserRole).value<RsGxsChannelPost>() ;
RsGxsChannelPost post = index.data(Qt::UserRole).value<RsGxsChannelPost>() ;
painter->fillRect( option.rect, option.backgroundBrush);
painter->restore();
uint32_t flags = (mUseGrid)?0:(ChannelPostThumbnailView::FLAG_SHOW_TEXT);
ChannelPostThumbnailView w(post,flags);
if(mUseGrid)
if(mUseGrid || index.column()==0)
{
uint32_t flags = (mUseGrid)?(ChannelPostThumbnailView::FLAG_SHOW_TEXT):0;
ChannelPostThumbnailView w(post,flags);
QPixmap pixmap(w.size());
if((option.state & QStyle::State_Selected) && post.mMeta.mPublishTs > 0) // check if post is selected and is not empty (end of last row)
@ -127,7 +127,7 @@ void ChannelPostDelegate::paint(QPainter * painter, const QStyleOptionViewItem &
w.render(&pixmap,QPoint(),QRegion(),QWidget::DrawChildren );// draw the widgets, not the background
if(mUseGrid)
if(mUseGrid || index.column()==0)
{
if(mZoom != 1.0)
pixmap = pixmap.scaled(mZoom*pixmap.size(),Qt::KeepAspectRatio,Qt::SmoothTransformation);
@ -145,19 +145,27 @@ void ChannelPostDelegate::paint(QPainter * painter, const QStyleOptionViewItem &
}
else
{
QPixmap pixmap(option.rect.size());
// We're drawing the text on the second column
if((option.state & QStyle::State_Selected) && post.mMeta.mPublishTs > 0) // check if post is selected and is not empty (end of last row)
pixmap.fill(QRgb(0xff308dc7)); // I dont know how to grab the backgroud color for selected objects automatically.
else
pixmap.fill(QRgb(0x00ffffff)); // choose a fully transparent background
uint32_t font_height = QFontMetricsF(option.font).height();
QPoint p = option.rect.topLeft();
float y = p.y() + font_height;
w.setFixedSize(option.rect.size());
w.update();
painter->save();
w.render(&pixmap,QPoint(),QRegion(),QWidget::DrawChildren );// draw the widgets, not the background
if(IS_MSG_UNREAD(post.mMeta.mMsgStatus) || IS_MSG_NEW(post.mMeta.mMsgStatus))
{
QFont font(option.font);
font.setBold(true);
painter->setFont(font);
}
painter->drawText(QPoint(p.x(),y),QString::fromUtf8(post.mMeta.mMsgName.c_str()));
y += font_height;
painter->drawPixmap(option.rect.topLeft(), pixmap) ;
painter->drawText(QPoint(p.x(),y),QDateTime::fromSecsSinceEpoch(post.mMeta.mPublishTs).toString());
y += font_height;
painter->restore();
}
}
@ -167,15 +175,10 @@ QSize ChannelPostDelegate::sizeHint(const QStyleOptionViewItem& option, const QM
QFontMetricsF fm(option.font);
if(mUseGrid)
if(mUseGrid || index.column()==0)
return QSize(mZoom*COLUMN_SIZE_FONT_FACTOR_W*fm.height(),mZoom*COLUMN_SIZE_FONT_FACTOR_H*fm.height());
else
{
RsGxsChannelPost post = index.data(Qt::UserRole).value<RsGxsChannelPost>() ;
uint32_t flags = (mUseGrid)?0:(ChannelPostThumbnailView::FLAG_SHOW_TEXT);
return QSize(option.rect.width(),ChannelPostThumbnailView(post,flags).height());
}
return QSize(option.rect.width()-mZoom*COLUMN_SIZE_FONT_FACTOR_W*fm.height(),mZoom*COLUMN_SIZE_FONT_FACTOR_H*fm.height());
}
void ChannelPostDelegate::setWidgetGrid(bool use_grid)
@ -314,9 +317,7 @@ GxsChannelPostsWidgetWithModel::GxsChannelPostsWidgetWithModel(const RsGxsGroupI
if(mChannelPostsModel->getMode() == RsGxsChannelPostsModel::TREE_MODE_GRID)
for(int i=0;i<mChannelPostsModel->columnCount();++i)
ui->postsTree->setColumnWidth(i,mChannelPostsDelegate->cellSize(font(),ui->postsTree->width()));
else
ui->postsTree->setColumnWidth(0,ui->postsTree->width());
ui->postsTree->setColumnWidth(i,mChannelPostsDelegate->cellSize(i,font(),ui->postsTree->width()));
/* Setup UI helper */
@ -381,17 +382,14 @@ GxsChannelPostsWidgetWithModel::GxsChannelPostsWidgetWithModel(const RsGxsGroupI
void GxsChannelPostsWidgetWithModel::updateZoomFactor(bool zoom_or_unzoom)
{
if(mChannelPostsModel->getMode() == RsGxsChannelPostsModel::TREE_MODE_LIST)
return;
mChannelPostsDelegate->zoom(zoom_or_unzoom);
for(int i=0;i<mChannelPostsModel->columnCount();++i)
ui->postsTree->setColumnWidth(i,mChannelPostsDelegate->cellSize(font(),ui->postsTree->width()));
ui->postsTree->setColumnWidth(i,mChannelPostsDelegate->cellSize(i,font(),ui->postsTree->width()));
QSize s = ui->postsTree->size();
int n_columns = std::max(1,(int)floor(s.width() / (mChannelPostsDelegate->cellSize(font(),s.width()))));
int n_columns = std::max(1,(int)floor(s.width() / (mChannelPostsDelegate->cellSize(0,font(),s.width()))));
std::cerr << "nb columns: " << n_columns << std::endl;
mChannelPostsModel->setNumColumns(n_columns); // forces the update
@ -436,7 +434,7 @@ void GxsChannelPostsWidgetWithModel::switchView()
mChannelPostsModel->setMode(RsGxsChannelPostsModel::TREE_MODE_LIST);
ui->postsTree->setColumnWidth(0,ui->postsTree->width());
ui->postsTree->setUniformRowHeights(true);
//ui->postsTree->setUniformRowHeights(true);
}
else
{
@ -444,9 +442,9 @@ void GxsChannelPostsWidgetWithModel::switchView()
mChannelPostsModel->setMode(RsGxsChannelPostsModel::TREE_MODE_GRID);
for(int i=0;i<mChannelPostsModel->columnCount();++i)
ui->postsTree->setColumnWidth(i,mChannelPostsDelegate->cellSize(font(),ui->postsTree->width()));
ui->postsTree->setColumnWidth(i,mChannelPostsDelegate->cellSize(i,font(),ui->postsTree->width()));
ui->postsTree->setUniformRowHeights(false);
//ui->postsTree->setUniformRowHeights(false);
handlePostsTreeSizeChange(ui->postsTree->size());
}
@ -494,7 +492,7 @@ void GxsChannelPostsWidgetWithModel::editPost()
void GxsChannelPostsWidgetWithModel::handlePostsTreeSizeChange(QSize s)
{
int n_columns = std::max(1,(int)floor(s.width() / (mChannelPostsDelegate->cellSize(font(),ui->postsTree->width()))));
int n_columns = std::max(1,(int)floor(s.width() / (mChannelPostsDelegate->cellSize(0,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,uint32_t parent_width) const;
int cellSize(int col, const QFont& font, uint32_t parent_width) const;
void zoom(bool zoom_or_unzoom) ;
void setWidgetGrid(bool use_grid) ;