From 129ffdd6c5021a1eb358dcc3e8f29e0b16e0906c Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 6 Jun 2020 19:07:03 +0200 Subject: [PATCH] fixed auto-layout of thumbnails --- retroshare-gui/src/gui/common/RSTreeView.cpp | 6 ++ retroshare-gui/src/gui/common/RSTreeView.h | 6 +- .../GxsChannelFilesStatusWidget.ui | 4 +- .../gxschannels/GxsChannelPostFilesModel.cpp | 2 +- .../gui/gxschannels/GxsChannelPostsModel.cpp | 21 +++++- .../gui/gxschannels/GxsChannelPostsModel.h | 1 + .../GxsChannelPostsWidgetWithModel.cpp | 64 ++++++++++++++----- .../GxsChannelPostsWidgetWithModel.h | 2 + .../GxsChannelPostsWidgetWithModel.ui | 9 ++- 9 files changed, 89 insertions(+), 26 deletions(-) diff --git a/retroshare-gui/src/gui/common/RSTreeView.cpp b/retroshare-gui/src/gui/common/RSTreeView.cpp index 918fbc558..da1aa5840 100644 --- a/retroshare-gui/src/gui/common/RSTreeView.cpp +++ b/retroshare-gui/src/gui/common/RSTreeView.cpp @@ -19,12 +19,18 @@ *******************************************************************************/ #include +#include #include "RSTreeView.h" RSTreeView::RSTreeView(QWidget *parent) : QTreeView(parent) { } +void RSTreeView::resizeEvent(QResizeEvent *e) +{ + emit sizeChanged(e->size()); +} + void RSTreeView::setPlaceholderText(const QString &text) { placeholderText = text; diff --git a/retroshare-gui/src/gui/common/RSTreeView.h b/retroshare-gui/src/gui/common/RSTreeView.h index 022a643db..f895571f0 100644 --- a/retroshare-gui/src/gui/common/RSTreeView.h +++ b/retroshare-gui/src/gui/common/RSTreeView.h @@ -33,8 +33,12 @@ public: void setPlaceholderText(const QString &text); +signals: + void sizeChanged(QSize); + protected: - void paintEvent(QPaintEvent *event); + virtual void resizeEvent(QResizeEvent *e) override; + virtual void paintEvent(QPaintEvent *event) override; QString placeholderText; }; diff --git a/retroshare-gui/src/gui/gxschannels/GxsChannelFilesStatusWidget.ui b/retroshare-gui/src/gui/gxschannels/GxsChannelFilesStatusWidget.ui index 59fa846da..39e32271d 100644 --- a/retroshare-gui/src/gui/gxschannels/GxsChannelFilesStatusWidget.ui +++ b/retroshare-gui/src/gui/gxschannels/GxsChannelFilesStatusWidget.ui @@ -6,8 +6,8 @@ 0 0 - 367 - 27 + 421 + 29 diff --git a/retroshare-gui/src/gui/gxschannels/GxsChannelPostFilesModel.cpp b/retroshare-gui/src/gui/gxschannels/GxsChannelPostFilesModel.cpp index ab20c2fd8..06ba27a0f 100644 --- a/retroshare-gui/src/gui/gxschannels/GxsChannelPostFilesModel.cpp +++ b/retroshare-gui/src/gui/gxschannels/GxsChannelPostFilesModel.cpp @@ -33,7 +33,7 @@ #include "GxsChannelPostFilesModel.h" -#define DEBUG_CHANNEL_MODEL +//#define DEBUG_CHANNEL_MODEL Q_DECLARE_METATYPE(RsGxsFile) diff --git a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsModel.cpp b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsModel.cpp index 5d82110d5..b7649af26 100644 --- a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsModel.cpp +++ b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsModel.cpp @@ -32,7 +32,7 @@ #include "retroshare/rsgxschannels.h" #include "retroshare/rsexpr.h" -#define DEBUG_CHANNEL_MODEL +//#define DEBUG_CHANNEL_MODEL Q_DECLARE_METATYPE(RsMsgMetaData) @@ -221,6 +221,21 @@ Qt::ItemFlags RsGxsChannelPostsModel::flags(const QModelIndex& index) const return QAbstractItemModel::flags(index); } +void RsGxsChannelPostsModel::setNumColumns(int n) +{ + preMods(); + + beginRemoveRows(QModelIndex(),0,rowCount()-1); + endRemoveRows(); + + mColumns = n; + + beginInsertRows(QModelIndex(),0,rowCount()-1); + endInsertRows(); + + postMods(); +} + quintptr RsGxsChannelPostsModel::getChildRef(quintptr ref,int index) const { if (index < 0) @@ -675,7 +690,7 @@ void RsGxsChannelPostsModel::setPosts(const RsGxsChannelGroup& group, std::vecto { preMods(); - beginRemoveRows(QModelIndex(),0,mPosts.size()-1); + beginRemoveRows(QModelIndex(),0,rowCount()-1); endRemoveRows(); mPosts.clear(); @@ -692,7 +707,7 @@ void RsGxsChannelPostsModel::setPosts(const RsGxsChannelGroup& group, std::vecto // debug_dump(); #endif - beginInsertRows(QModelIndex(),0,mPosts.size()-1); + beginInsertRows(QModelIndex(),0,rowCount()-1); endInsertRows(); postMods(); diff --git a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsModel.h b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsModel.h index de4713e79..60b4065de 100644 --- a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsModel.h +++ b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsModel.h @@ -110,6 +110,7 @@ public: const RsGxsGroupId& currentGroupId() const; void setTreeMode(TreeMode mode) ; + void setNumColumns(int n); #ifdef TODO void setSortMode(SortMode mode) ; diff --git a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.cpp b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.cpp index fd9ddedff..8bfbb5e3d 100644 --- a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.cpp +++ b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.cpp @@ -61,8 +61,21 @@ static const int CHANNEL_TABS_POSTS = 1; /* View mode */ #define VIEW_MODE_FEEDS 1 #define VIEW_MODE_FILES 2 + +// Size of thumbnails as a function of the height of the font. An aspect ratio of 3/4 is good. + +#define THUMBNAIL_W 4 +#define THUMBNAIL_H 6 + +// Determine the Shape and size of cells as a factor of the font height. An aspect ratio of 3/4 is what's needed +// for the image, so it's important that the height is a bit larger so as to leave some room for the text. +// +// #define COLUMN_SIZE_FONT_FACTOR_W 6 -#define COLUMN_SIZE_FONT_FACTOR_H 12 +#define COLUMN_SIZE_FONT_FACTOR_H 10 + +// This variable determines the zoom factor on the text below thumbnails. 2.0 is mostly correct for all screen. +#define THUMBNAIL_OVERSAMPLE_FACTOR 2.0 Q_DECLARE_METATYPE(RsGxsFile) @@ -95,8 +108,8 @@ public: lb->setPixmap(thumbnail); QFontMetricsF fm(font()); - int W = 8 * fm.height() ; - int H = 12 * fm.height() ; + int W = THUMBNAIL_OVERSAMPLE_FACTOR * THUMBNAIL_W * fm.height() ; + int H = THUMBNAIL_OVERSAMPLE_FACTOR * THUMBNAIL_H * fm.height() ; lb->setFixedSize(W,H); @@ -183,18 +196,19 @@ void ChannelPostDelegate::paint(QPainter * painter, const QStyleOptionViewItem & //w.setFixedWidth(option.rect.width()); QPixmap pixmap(w.size()); - pixmap.fill(option.palette.color(QPalette::Background)); // choose the background + pixmap.fill(QRgb(0x00ffffff)); // choose a fully transparent background w.render(&pixmap,QPoint(),QRegion(),QWidget::DrawChildren );// draw the widgets, not the background // debug - if(index.row()==0 && index.column()==0) - { - QFile file("yourFile.png"); - file.open(QIODevice::WriteOnly); - pixmap.save(&file, "PNG"); - std::cerr << "Saved pxmap to png" << std::endl; - } - std::cerr << "option.rect = " << option.rect.width() << "x" << option.rect.height() << ". fm.height()=" << QFontMetricsF(option.font).height() << std::endl; + // if(index.row()==0 && index.column()==0) + // { + // QFile file("yourFile.png"); + // file.open(QIODevice::WriteOnly); + // pixmap.save(&file, "PNG"); + // std::cerr << "Saved pxmap to png" << std::endl; + // } + //std::cerr << "option.rect = " << option.rect.width() << "x" << option.rect.height() << ". fm.height()=" << QFontMetricsF(option.font).height() << std::endl; + painter->drawPixmap(option.rect.topLeft(), pixmap.scaled(option.rect.width(),option.rect.width()*w.height()/(float)w.width(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation)); } @@ -297,7 +311,7 @@ void ChannelPostFilesDelegate::paint(QPainter * painter, const QStyleOptionViewI w.setFixedWidth(option.rect.width()); QPixmap pixmap(w.size()); - pixmap.fill(option.palette.color(QPalette::Background)); // choose the background + pixmap.fill(QRgb(0x00ffffff)); // choose a fully transparent background w.render(&pixmap,QPoint(),QRegion(),QWidget::DrawChildren );// draw the widgets, not the background painter->drawPixmap(option.rect.topLeft(),pixmap); @@ -344,6 +358,7 @@ GxsChannelPostsWidgetWithModel::GxsChannelPostsWidgetWithModel(const RsGxsGroupI ui->channelPostFiles_TV->setModel(mChannelPostFilesModel = new RsGxsChannelPostFilesModel()); ui->channelPostFiles_TV->setItemDelegate(new ChannelPostFilesDelegate()); + ui->channelPostFiles_TV->setPlaceholderText(tr("Post files")); connect(ui->postsTree->selectionModel(),SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)),this,SLOT(showPostDetails())); @@ -386,6 +401,8 @@ GxsChannelPostsWidgetWithModel::GxsChannelPostsWidgetWithModel(const RsGxsGroupI ui->loadingLabel->hide(); ui->progressBar->hide(); + ui->postsTree->setPlaceholderText(tr("Thumbnails")); + ui->postsTree->setMinimumWidth(COLUMN_SIZE_FONT_FACTOR_W*QFontMetricsF(font()).height()+1); //ui->nameLabel->setMinimumWidth(20); @@ -414,10 +431,23 @@ GxsChannelPostsWidgetWithModel::GxsChannelPostsWidgetWithModel(const RsGxsGroupI mEventHandlerId = 0; // Needs to be asynced because this function is called by another thread! - rsEvents->registerEventsHandler( - [this](std::shared_ptr event) - { RsQThreadUtils::postToObject([=](){ handleEvent_main_thread(event); }, this ); }, - mEventHandlerId, RsEventType::GXS_CHANNELS ); + rsEvents->registerEventsHandler( [this](std::shared_ptr event) + { + RsQThreadUtils::postToObject([=](){ handleEvent_main_thread(event); }, this ); + }, mEventHandlerId, RsEventType::GXS_CHANNELS ); + + connect(ui->postsTree,SIGNAL(sizeChanged(QSize)),this,SLOT(handlePostsTreeSizeChange(QSize))); +} + +void GxsChannelPostsWidgetWithModel::handlePostsTreeSizeChange(QSize s) +{ +// adjustSize(); +// + int n_columns = std::max(1,(int)floor(s.width() / (COLUMN_SIZE_FONT_FACTOR_W*QFontMetricsF(font()).height()))); + std::cerr << "nb columns: " << n_columns << std::endl; + + if(n_columns != mChannelPostsModel->columnCount()) + mChannelPostsModel->setNumColumns(n_columns); } void GxsChannelPostsWidgetWithModel::handleEvent_main_thread(std::shared_ptr event) diff --git a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.h b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.h index 51bc79b6b..48caeaf49 100644 --- a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.h +++ b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.h @@ -108,6 +108,7 @@ public: protected: /* GxsMessageFramePostWidget */ virtual void groupNameChanged(const QString &name); + #ifdef TODO virtual bool insertGroupData(const RsGxsGenericGroupData *data) override; #endif @@ -134,6 +135,7 @@ private slots: void filterChanged(int filter); void setViewMode(int viewMode); void settingsChanged(); + void handlePostsTreeSizeChange(QSize s); private: void processSettings(bool load); diff --git a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.ui b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.ui index 66c4d4ba3..0f3405cda 100644 --- a/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.ui +++ b/retroshare-gui/src/gui/gxschannels/GxsChannelPostsWidgetWithModel.ui @@ -412,7 +412,7 @@ p, li { white-space: pre-wrap; } Qt::Vertical - + QAbstractScrollArea::AdjustToContents @@ -509,7 +509,7 @@ p, li { white-space: pre-wrap; } - + QAbstractItemView::CurrentChanged|QAbstractItemView::SelectedClicked @@ -566,6 +566,11 @@ p, li { white-space: pre-wrap; } QLineEdit
gui/common/LineEditClear.h
+ + RSTreeView + QTreeView +
gui/common/RSTreeView.h
+