Merge pull request #2237 from defnax/avatar-dialog-improvements

Added to load stickers in Avatars dialog to use as Avatar
This commit is contained in:
defnax 2021-01-22 19:02:51 +01:00 committed by GitHub
commit 85afe64086
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 387 additions and 99 deletions

View File

@ -19,12 +19,35 @@
*******************************************************************************/
#include <QBuffer>
#include <QFile>
#include <QDir>
#include <QGridLayout>
#include <QHash>
#include <QIcon>
#include <QPushButton>
#include <QTabWidget>
#include <QToolTip>
#include <QWidget>
#include <QMessageBox>
#include <QDir>
#include <iostream>
#include <math.h>
#include "AvatarDialog.h"
#include "ui_AvatarDialog.h"
#include "AvatarDefs.h"
#include "util/misc.h"
#include "gui/common/FilesDefs.h"
#include "util/HandleRichText.h"
#include "retroshare/rsinit.h"
#define ICONNAME "groupicon.png"
static QStringList filters;
static QStringList stickerFolders;
static QHash<QString, QString> tooltipcache;
static QHash<QString, QPixmap> iconcache;
/** Constructor */
AvatarDialog::AvatarDialog(QWidget *parent) :
@ -44,6 +67,8 @@ AvatarDialog::AvatarDialog(QWidget *parent) :
connect(ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
updateInterface();
loadAvatarWidget();
}
const int AvatarDialog::RS_AVATAR_DEFAULT_IMAGE_W = 96;
@ -56,18 +81,25 @@ AvatarDialog::~AvatarDialog()
void AvatarDialog::changeAvatar()
{
QPixmap img = misc::getOpenThumbnailedPicture(this, tr("Load Avatar"), RS_AVATAR_DEFAULT_IMAGE_W,RS_AVATAR_DEFAULT_IMAGE_H);
QString image_filename ;
if (img.isNull())
return;
if(!misc::getOpenFileName(this,RshareSettings::LASTDIR_IMAGES,tr("Import image"), tr("Image files (*.jpg *.png);;All files (*)"),image_filename))
return;
ui->avatarLabel->setPixmap(img);
QImage img(image_filename);
ui->avatarLabel->setPicture(QPixmap::fromImage(img));
ui->avatarLabel->setEnableZoom(true);
ui->avatarLabel->setToolTip(tr("Use the mouse to zoom and adjust the image for your avatar."));
// shows the tooltip for a while
QToolTip::showText( ui->avatarLabel->mapToGlobal( QPoint( 0, 0 ) ), ui->avatarLabel->toolTip() );
updateInterface();
}
void AvatarDialog::removeAvatar()
{
ui->avatarLabel->setPixmap(QPixmap());
ui->avatarLabel->setPicture(QPixmap());
updateInterface();
}
@ -83,7 +115,7 @@ void AvatarDialog::updateInterface()
void AvatarDialog::setAvatar(const QPixmap &avatar)
{
ui->avatarLabel->setPixmap(avatar);
ui->avatarLabel->setPicture(avatar);
updateInterface();
}
@ -111,3 +143,194 @@ void AvatarDialog::getAvatar(QByteArray &avatar)
buffer.open(QIODevice::WriteOnly);
pixmap->save(&buffer, "PNG"); // writes image into ba in PNG format
}
void AvatarDialog::load()
{
filters << "*.png" << "*.jpg" << "*.gif";
stickerFolders << (QString::fromStdString(RsAccounts::AccountDirectory()) + "/stickers"); //under account, unique for user
stickerFolders << (QString::fromStdString(RsAccounts::ConfigDirectory()) + "/stickers"); //under .retroshare, shared between users
stickerFolders << (QString::fromStdString(RsAccounts::systemDataDirectory()) + "/stickers"); //exe's folder, shipped with RS
QDir dir(QString::fromStdString(RsAccounts::AccountDirectory()));
dir.mkpath("stickers/imported");
}
void AvatarDialog::loadAvatarWidget()
{
QVector<QString> stickerTabs;
refreshStickerTabs(stickerTabs);
if(stickerTabs.count() == 0) {
ui->nostickersLabel->setText("");
QString message = "No stickers installed.\nYou can install them by putting images into one of these folders:\n" /*+ stickerFolders.join('\n')*/;
message += "RetroShare/stickers\n RetroShare/Data/stickers\n RetroShare/Data/Location/stickers";
ui->nostickersLabel->setText(message);
} else {
ui->infoframe->hide();
}
bool bOnlyOneGroup = (stickerTabs.count() == 1);
QTabWidget *avatarTab = nullptr;
if (! bOnlyOneGroup)
{
avatarTab = new QTabWidget(ui->avatarWidget);
QGridLayout *avatarGLayout = new QGridLayout(ui->avatarWidget);
avatarGLayout->setContentsMargins(0,0,0,0);
avatarGLayout->addWidget(avatarTab);
}
const int buttonWidth = QFontMetricsF(ui->avatarWidget->font()).height()*5;
const int buttonHeight = QFontMetricsF(ui->avatarWidget->font()).height()*5;
int maxRowCount = 0;
int maxCountPerLine = 0;
QVectorIterator<QString> grp(stickerTabs);
while(grp.hasNext())
{
QDir groupDir = QDir(grp.next());
QString groupName = groupDir.dirName();
groupDir.setNameFilters(filters);
QWidget *tabGrpWidget = nullptr;
if (! bOnlyOneGroup)
{
//Lazy load tooltips for the current tab
QObject::connect(avatarTab, &QTabWidget::currentChanged, [=](int index){
QWidget* current = avatarTab->widget(index);
loadToolTips(current);
});
tabGrpWidget = new QWidget(avatarTab);
// (Cyril) Never use an absolute size. It needs to be scaled to the actual font size on the screen.
//
QFontMetricsF fm(font()) ;
avatarTab->setIconSize(QSize(28*fm.height()/14.0,28*fm.height()/14.0));
avatarTab->setMinimumWidth(200);
//avatarTab->setTabPosition(QTabWidget::West);
avatarTab->setStyleSheet("QTabBar::tab { height: 44px; width: 44px; }");
int index;
if (groupDir.exists(ICONNAME)) //use groupicon.png if exists, else the first png as a group icon
index = avatarTab->addTab( tabGrpWidget, FilesDefs::getIconFromQtResourcePath(groupDir.absoluteFilePath(ICONNAME)), "");
else
index = avatarTab->addTab( tabGrpWidget, FilesDefs::getIconFromQtResourcePath(groupDir.entryInfoList(QDir::Files)[0].canonicalFilePath()), "");
avatarTab->setTabToolTip(index, groupName);
} else {
tabGrpWidget = ui->avatarWidget;
}
QGridLayout *tabGLayout = new QGridLayout(tabGrpWidget);
tabGLayout->setContentsMargins(0,0,0,0);
tabGLayout->setSpacing(0);
QFileInfoList group = groupDir.entryInfoList(QDir::Files, QDir::Name);
int rowCount = (int)sqrt((double)group.size());
int countPerLine = (group.size()/rowCount) + ((group.size() % rowCount) ? 1 : 0);
maxRowCount = qMax(maxRowCount, rowCount);
maxCountPerLine = qMax(maxCountPerLine, countPerLine);
int lin = 0;
int col = 0;
for(int i = 0; i < group.length(); ++i)
{
QFileInfo fi = group[i];
if(fi.fileName().compare(ICONNAME, Qt::CaseInsensitive) == 0)
continue;
QPushButton *button = new QPushButton("", tabGrpWidget);
button->setIconSize(QSize(buttonWidth, buttonHeight));
button->setFixedSize(QSize(buttonWidth, buttonHeight));
if(!iconcache.contains(fi.absoluteFilePath()))
{
iconcache.insert(fi.absoluteFilePath(), FilesDefs::getPixmapFromQtResourcePath(fi.absoluteFilePath()).scaled(buttonWidth, buttonHeight, Qt::KeepAspectRatio));
}
button->setIcon(iconcache[fi.absoluteFilePath()]);
button->setToolTip(fi.fileName());
button->setStatusTip(fi.absoluteFilePath());
button->setStyleSheet("QPushButton:hover {border: 3px solid #0099cc; border-radius: 3px;}");
button->setFlat(true);
tabGLayout->addWidget(button,col,lin);
++lin;
if(lin >= countPerLine)
{
lin = 0;
++col;
}
QObject::connect(button, SIGNAL(clicked()), this, SLOT(addAvatar()));
}
}
//Load tooltips for the first page
QWidget * firstpage;
if(bOnlyOneGroup) {
firstpage = ui->avatarWidget;
} else {
firstpage = avatarTab->currentWidget();
}
loadToolTips(firstpage);
//Get widget's size
QSize sizeWidget = ui->avatarWidget->sizeHint();
}
QString AvatarDialog::importedStickerPath()
{
QDir dir(stickerFolders[0]);
return dir.absoluteFilePath("imported");
}
void AvatarDialog::loadToolTips(QWidget *container)
{
QApplication::setOverrideCursor(Qt::WaitCursor);
QList<QPushButton *> children = container->findChildren<QPushButton *>();
for(int i = 0; i < children.length(); ++i) {
if(!children[i]->toolTip().contains('<')) {
if(tooltipcache.contains(children[i]->statusTip())) {
children[i]->setToolTip(tooltipcache[children[i]->statusTip()]);
} else {
QString tooltip;
if(RsHtml::makeEmbeddedImage(children[i]->statusTip(), tooltip, 300*300)) {
tooltipcache.insert(children[i]->statusTip(), tooltip);
children[i]->setToolTip(tooltip);
}
}
}
}
QApplication::restoreOverrideCursor();
}
void AvatarDialog::refreshStickerTabs(QVector<QString>& stickerTabs)
{
for(int i = 0; i < stickerFolders.count(); ++i)
refreshStickerTabs(stickerTabs, stickerFolders[i]);
}
void AvatarDialog::refreshStickerTabs(QVector<QString>& stickerTabs, QString foldername)
{
QDir dir(foldername);
if(!dir.exists()) return;
//If it contains at a least one png then add it as a group
QStringList files = dir.entryList(filters, QDir::Files);
if(files.count() > 0)
stickerTabs.append(foldername);
//Check subfolders
QFileInfoList subfolders = dir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot);
for(int i = 0; i<subfolders.length(); i++)
refreshStickerTabs(stickerTabs, subfolders[i].filePath());
}
void AvatarDialog::addAvatar()
{
QString sticker = qobject_cast<QPushButton*>(sender())->statusTip();
QPixmap pixmap(sticker);
ui->avatarLabel->setPixmap(pixmap);
updateInterface();
}

View File

@ -46,9 +46,19 @@ public:
void getAvatar(QPixmap &avatar);
void getAvatar(QByteArray &avatar);
static QString importedStickerPath();
static void load();
private slots:
void changeAvatar();
void removeAvatar();
void addAvatar();
void loadAvatarWidget();
void loadToolTips(QWidget *container);
void refreshStickerTabs(QVector<QString>& stickerTabs, QString foldername);
void refreshStickerTabs(QVector<QString>& stickerTabs);
private:
void updateInterface();

View File

@ -6,31 +6,37 @@
<rect>
<x>0</x>
<y>0</y>
<width>374</width>
<height>252</height>
<width>589</width>
<height>468</height>
</rect>
</property>
<property name="windowTitle">
<string>Change Avatar</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<property name="spacing">
<layout class="QGridLayout" name="gridLayout_4">
<property name="leftMargin">
<number>0</number>
</property>
<property name="margin">
<property name="topMargin">
<number>0</number>
</property>
<item>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="HeaderFrame" name="headerFrame">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
<enum>QFrame::NoFrame</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
</widget>
</item>
<item>
<item row="1" column="0">
<widget class="QFrame" name="frame">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
@ -38,11 +44,39 @@
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<layout class="QGridLayout" name="gridLayout_2">
<property name="topMargin">
<number>3</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QFrame" name="infoframe">
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<widget class="QLabel" name="nostickersLabel">
<property name="text">
<string>TextLabel</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="0" column="1" rowspan="2">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="avatarLabel">
<widget class="ZoomableLabel" name="avatarLabel">
<property name="minimumSize">
<size>
<width>128</width>
@ -70,73 +104,93 @@
</widget>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="avatarButton">
<property name="text">
<string>Add Avatar</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="removeButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Remove</string>
</property>
</widget>
</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>
<widget class="QPushButton" name="avatarButton">
<property name="text">
<string>Browse...</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="removeButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Remove</string>
</property>
</widget>
</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 row="0" column="2">
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<item row="1" column="0">
<widget class="QScrollArea" name="scrollArea">
<property name="widgetResizable">
<bool>true</bool>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>61</width>
<height>98</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="0" colspan="3">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>218</width>
<height>88</height>
</size>
</property>
</spacer>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>431</width>
<height>359</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
<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>
<item row="0" column="0">
<widget class="QWidget" name="avatarWidget" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<item row="2" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="margin">
<property name="leftMargin">
<number>9</number>
</property>
<property name="topMargin">
<number>9</number>
</property>
<property name="rightMargin">
<number>9</number>
</property>
<property name="bottomMargin">
<number>9</number>
</property>
<item>
@ -175,6 +229,11 @@
<header>gui/common/HeaderFrame.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>ZoomableLabel</class>
<extends>QLabel</extends>
<header>gui/gxschannels/GxsChannelPostThumbnail.h</header>
</customwidget>
</customwidgets>
<resources>
<include location="../images.qrc"/>

View File

@ -88,33 +88,19 @@ void AvatarWidget::mouseReleaseEvent(QMouseEvent */*event*/)
if (!mFlag.isOwnId) {
return;
}
QPixmap img = misc::getOpenThumbnailedPicture(this, tr("Choose avatar"), AvatarDialog::RS_AVATAR_DEFAULT_IMAGE_W,AvatarDialog::RS_AVATAR_DEFAULT_IMAGE_H);
if (img.isNull())
return;
AvatarDialog dialog(this);
setPixmap(img);
QPixmap avatar;
AvatarDefs::getOwnAvatar(avatar, "");
QByteArray data;
QBuffer buffer(&data);
dialog.setAvatar(avatar);
if (dialog.exec() == QDialog::Accepted) {
QByteArray newAvatar;
dialog.getAvatar(newAvatar);
buffer.open(QIODevice::WriteOnly);
img.save(&buffer, "PNG"); // writes image into a in PNG format
rsMsgs->setOwnAvatarData((unsigned char *)(data.data()), data.size()) ; // last char 0 included.
// AvatarDialog dialog(this);
//
// QPixmap avatar;
// AvatarDefs::getOwnAvatar(avatar, "");
//
// dialog.setAvatar(avatar);
// if (dialog.exec() == QDialog::Accepted) {
// QByteArray newAvatar;
// dialog.getAvatar(newAvatar);
//
// rsMsgs->setOwnAvatarData((unsigned char *)(newAvatar.data()), newAvatar.size()) ; // last char 0 included.
// }
rsMsgs->setOwnAvatarData((unsigned char *)(newAvatar.data()), newAvatar.size()) ; // last char 0 included.
}
}
void AvatarWidget::setFrameType(FrameType type)

View File

@ -18,6 +18,14 @@ HeaderFrame > QFrame#frame > QLabel#headerLabel {
color: rgb(255, 255, 255);
}
AvatarDialog QFrame#infoframe
{
border: 1px solid #DCDC41;
border-radius: 6px;
background: #FFFFD7;
background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #FFFFD7, stop:1 #FFFFB2);
}
/* GenCertDialog */
GenCertDialog QLabel#genprofileinfo_label, QLabel#header_label, QLabel#entropy_label

View File

@ -42,6 +42,7 @@ CrashStackTrace gCrashStackTrace;
#include "gui/StartDialog.h"
#include "gui/chat/ChatDialog.h"
#include "gui/connect/ConfCertDialog.h"
#include "gui/common/AvatarDialog.h"
#include "gui/common/Emoticons.h"
#include "gui/FileTransfer/SearchDialog.h"
#include "gui/FileTransfer/TransfersDialog.h"
@ -493,6 +494,7 @@ feenableexcept(FE_INVALID | FE_DIVBYZERO);
RsharePeerSettings::Create();
Emoticons::load();
AvatarDialog::load();
if (Settings->value(QString::fromUtf8("FirstRun"), true).toBool()) {
splashScreen.hide();