added switch between grid and list views in channels.

This commit is contained in:
csoler 2020-08-27 17:14:55 +02:00
parent 4472f1cef4
commit 38f1df2b36
4 changed files with 61 additions and 22 deletions

View file

@ -44,7 +44,7 @@ Q_DECLARE_METATYPE(RsGxsChannelPost)
std::ostream& operator<<(std::ostream& o, const QModelIndex& i);// defined elsewhere std::ostream& operator<<(std::ostream& o, const QModelIndex& i);// defined elsewhere
RsGxsChannelPostsModel::RsGxsChannelPostsModel(QObject *parent) RsGxsChannelPostsModel::RsGxsChannelPostsModel(QObject *parent)
: QAbstractItemModel(parent), mTreeMode(TREE_MODE_PLAIN), mColumns(6) : QAbstractItemModel(parent), mTreeMode(RsGxsChannelPostsModel::TREE_MODE_GRID), mColumns(6)
{ {
initEmptyHierarchy(); initEmptyHierarchy();
@ -62,6 +62,15 @@ RsGxsChannelPostsModel::~RsGxsChannelPostsModel()
rsEvents->unregisterEventsHandler(mEventHandlerId); rsEvents->unregisterEventsHandler(mEventHandlerId);
} }
void RsGxsChannelPostsModel::setMode(TreeMode mode)
{
mTreeMode = mode;
if(mode == TREE_MODE_LIST)
setNumColumns(1);
// needs some update here
}
void RsGxsChannelPostsModel::handleEvent_main_thread(std::shared_ptr<const RsEvent> event) void RsGxsChannelPostsModel::handleEvent_main_thread(std::shared_ptr<const RsEvent> event)
{ {
const RsGxsChannelEvent *e = dynamic_cast<const RsGxsChannelEvent*>(event.get()); const RsGxsChannelEvent *e = dynamic_cast<const RsGxsChannelEvent*>(event.get());
@ -124,18 +133,12 @@ void RsGxsChannelPostsModel::initEmptyHierarchy()
mPosts.clear(); mPosts.clear();
mFilteredPosts.clear(); mFilteredPosts.clear();
// mPosts.resize(1); // adds a sentinel item
// mPosts[0].mMeta.mMsgName = "Root sentinel post" ;
// mFilteredPosts.resize(1);
// mFilteredPosts[0] = 1;
postMods(); postMods();
} }
void RsGxsChannelPostsModel::preMods() void RsGxsChannelPostsModel::preMods()
{ {
//emit layoutAboutToBeChanged(); //Generate SIGSEGV when click on button move next/prev.
beginResetModel(); beginResetModel();
} }
void RsGxsChannelPostsModel::postMods() void RsGxsChannelPostsModel::postMods()

View file

@ -92,8 +92,8 @@ public:
#endif #endif
enum TreeMode{ TREE_MODE_UNKWN = 0x00, enum TreeMode{ TREE_MODE_UNKWN = 0x00,
TREE_MODE_PLAIN = 0x01, TREE_MODE_GRID = 0x01,
TREE_MODE_FILES = 0x02, TREE_MODE_LIST = 0x02,
}; };
#ifdef TODO #ifdef TODO
@ -112,6 +112,8 @@ public:
const RsGxsGroupId& currentGroupId() const; const RsGxsGroupId& currentGroupId() const;
void setNumColumns(int n); void setNumColumns(int n);
void setMode(TreeMode mode);
TreeMode getMode() const { return mTreeMode; }
// Retrieve the full list of files for all posts. // Retrieve the full list of files for all posts.

View file

@ -74,11 +74,16 @@ static const int CHANNEL_TABS_FILES = 2;
#define STAR_OVERLAY_IMAGE ":icons/star_overlay_128.png" #define STAR_OVERLAY_IMAGE ":icons/star_overlay_128.png"
#define IMAGE_COPYLINK ":/images/copyrslink.png" #define IMAGE_COPYLINK ":/images/copyrslink.png"
#define IMAGE_GRID_VIEW ":icons/png/menu.png"
Q_DECLARE_METATYPE(ChannelPostFileInfo) Q_DECLARE_METATYPE(ChannelPostFileInfo)
// Delegate used to paint into the table of thumbnails // Delegate used to paint into the table of thumbnails
//===============================================================================================================================================//
//=== ChannelPostDelegate ===//
//===============================================================================================================================================//
int ChannelPostDelegate::cellSize(const QFont& font) const int ChannelPostDelegate::cellSize(const QFont& font) const
{ {
return mZoom*COLUMN_SIZE_FONT_FACTOR_W*QFontMetricsF(font).height(); return mZoom*COLUMN_SIZE_FONT_FACTOR_W*QFontMetricsF(font).height();
@ -126,16 +131,6 @@ void ChannelPostDelegate::paint(QPainter * painter, const QStyleOptionViewItem &
p.drawPixmap(mZoom*QPoint(6.2*fm.height(),6.9*fm.height()),FilesDefs::getPixmapFromQtResourcePath(STAR_OVERLAY_IMAGE).scaled(mZoom*7*fm.height(),mZoom*7*fm.height(),Qt::KeepAspectRatio,Qt::SmoothTransformation)); p.drawPixmap(mZoom*QPoint(6.2*fm.height(),6.9*fm.height()),FilesDefs::getPixmapFromQtResourcePath(STAR_OVERLAY_IMAGE).scaled(mZoom*7*fm.height(),mZoom*7*fm.height(),Qt::KeepAspectRatio,Qt::SmoothTransformation));
} }
// 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;
painter->drawPixmap(option.rect.topLeft(), painter->drawPixmap(option.rect.topLeft(),
pixmap.scaled(option.rect.width(),option.rect.width()*w.height()/(float)w.width(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation)); pixmap.scaled(option.rect.width(),option.rect.width()*w.height()/(float)w.width(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
} }
@ -146,9 +141,21 @@ QSize ChannelPostDelegate::sizeHint(const QStyleOptionViewItem& option, const QM
QFontMetricsF fm(option.font); QFontMetricsF fm(option.font);
return QSize(mZoom*COLUMN_SIZE_FONT_FACTOR_W*fm.height(),mZoom*COLUMN_SIZE_FONT_FACTOR_H*fm.height()); if(mUseGrid)
return QSize(mZoom*COLUMN_SIZE_FONT_FACTOR_W*fm.height(),mZoom*COLUMN_SIZE_FONT_FACTOR_H*fm.height());
else
return QSize(option.rect.width(),mZoom*COLUMN_SIZE_FONT_FACTOR_H*fm.height());
} }
void ChannelPostDelegate::setWidgetGrid(bool use_grid)
{
mUseGrid = use_grid;
}
//===============================================================================================================================================//
//=== ChannelPostFilesDelegate ===//
//===============================================================================================================================================//
QWidget *ChannelPostFilesDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex& index) const QWidget *ChannelPostFilesDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex& index) const
{ {
ChannelPostFileInfo file = index.data(Qt::UserRole).value<ChannelPostFileInfo>() ; ChannelPostFileInfo file = index.data(Qt::UserRole).value<ChannelPostFileInfo>() ;
@ -222,6 +229,10 @@ QSize ChannelPostFilesDelegate::sizeHint(const QStyleOptionViewItem& option, con
} }
} }
//===============================================================================================================================================//
//=== GxsChannelPostWidgetWithModel ===//
//===============================================================================================================================================//
/** Constructor */ /** Constructor */
GxsChannelPostsWidgetWithModel::GxsChannelPostsWidgetWithModel(const RsGxsGroupId &channelId, QWidget *parent) : GxsChannelPostsWidgetWithModel::GxsChannelPostsWidgetWithModel(const RsGxsGroupId &channelId, QWidget *parent) :
GxsMessageFrameWidget(rsGxsChannels, parent), GxsMessageFrameWidget(rsGxsChannels, parent),
@ -366,7 +377,13 @@ void GxsChannelPostsWidgetWithModel::postContextMenu(const QPoint&)
{ {
QMenu menu(this); QMenu menu(this);
menu.addAction(FilesDefs::getIconFromQtResourcePath(IMAGE_COPYLINK), tr("Copy RetroShare Link"), this, SLOT(copyMessageLink())); if(mChannelPostsModel->getMode() == RsGxsChannelPostsModel::TREE_MODE_GRID)
menu.addAction(FilesDefs::getIconFromQtResourcePath(IMAGE_GRID_VIEW), tr("Switch to list view"), this, SLOT(switchView()));
else
menu.addAction(FilesDefs::getIconFromQtResourcePath(IMAGE_GRID_VIEW), tr("Switch to grid view"), this, SLOT(switchView()));
menu.addSeparator();
menu.addAction(FilesDefs::getIconFromQtResourcePath(IMAGE_COPYLINK), tr("Copy RetroShare Link"), this, SLOT(copyMessageLink()));
if(IS_GROUP_PUBLISHER(mGroup.mMeta.mSubscribeFlags)) if(IS_GROUP_PUBLISHER(mGroup.mMeta.mSubscribeFlags))
menu.addAction(FilesDefs::getIconFromQtResourcePath(":/images/edit_16.png"), tr("Edit"), this, SLOT(editPost())); menu.addAction(FilesDefs::getIconFromQtResourcePath(":/images/edit_16.png"), tr("Edit"), this, SLOT(editPost()));
@ -374,6 +391,19 @@ void GxsChannelPostsWidgetWithModel::postContextMenu(const QPoint&)
menu.exec(QCursor::pos()); menu.exec(QCursor::pos());
} }
void GxsChannelPostsWidgetWithModel::switchView()
{
if(mChannelPostsModel->getMode() == RsGxsChannelPostsModel::TREE_MODE_GRID)
{
mChannelPostsDelegate->setWidgetGrid(false);
mChannelPostsModel->setMode(RsGxsChannelPostsModel::TREE_MODE_LIST);
}
else
{
mChannelPostsDelegate->setWidgetGrid(true);
mChannelPostsModel->setMode(RsGxsChannelPostsModel::TREE_MODE_GRID);
}
}
void GxsChannelPostsWidgetWithModel::copyMessageLink() void GxsChannelPostsWidgetWithModel::copyMessageLink()
{ {
try try

View file

@ -68,13 +68,16 @@ class ChannelPostDelegate: public QAbstractItemDelegate
int cellSize(const QFont& font) const; int cellSize(const QFont& font) const;
void zoom(bool zoom_or_unzoom) ; void zoom(bool zoom_or_unzoom) ;
private: void setWidgetGrid(bool use_grid) ;
private:
static constexpr float IMAGE_MARGIN_FACTOR = 1.0; static constexpr float IMAGE_MARGIN_FACTOR = 1.0;
static constexpr float IMAGE_SIZE_FACTOR_W = 4.0 ; static constexpr float IMAGE_SIZE_FACTOR_W = 4.0 ;
static constexpr float IMAGE_SIZE_FACTOR_H = 6.0 ; static constexpr float IMAGE_SIZE_FACTOR_H = 6.0 ;
static constexpr float IMAGE_ZOOM_FACTOR = 1.0; static constexpr float IMAGE_ZOOM_FACTOR = 1.0;
float mZoom; // zoom factor for the whole thumbnail float mZoom; // zoom factor for the whole thumbnail
bool mUseGrid; // wether we use the grid widget or the list widget
}; };
class GxsChannelPostsWidgetWithModel: public GxsMessageFrameWidget class GxsChannelPostsWidgetWithModel: public GxsMessageFrameWidget
@ -147,6 +150,7 @@ private slots:
void postContextMenu(const QPoint&); void postContextMenu(const QPoint&);
void copyMessageLink(); void copyMessageLink();
void updateZoomFactor(bool zoom_or_unzoom); void updateZoomFactor(bool zoom_or_unzoom);
void switchView();
public slots: public slots:
void sortColumnFiles(int col,Qt::SortOrder so); void sortColumnFiles(int col,Qt::SortOrder so);