2018-11-14 15:14:40 -05:00
|
|
|
/*******************************************************************************
|
|
|
|
* retroshare-gui/src/gui/gxs/GxsIdTreeWidgetItem.cpp *
|
|
|
|
* *
|
|
|
|
* Copyright 2012-2013 by Robert Fernie <retroshare.project@gmail.com> *
|
|
|
|
* *
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU Affero General Public License as *
|
|
|
|
* published by the Free Software Foundation, either version 3 of the *
|
|
|
|
* License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU Affero General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Affero General Public License *
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
|
|
|
* *
|
|
|
|
*******************************************************************************/
|
2013-03-15 17:02:43 -04:00
|
|
|
|
2015-03-09 16:41:14 -04:00
|
|
|
#include "rshare.h"
|
2012-11-19 17:14:45 -05:00
|
|
|
#include "GxsIdTreeWidgetItem.h"
|
2014-02-02 06:25:11 -05:00
|
|
|
#include "GxsIdDetails.h"
|
2015-02-04 18:05:56 -05:00
|
|
|
#include "util/HandleRichText.h"
|
2015-10-10 21:25:06 -04:00
|
|
|
|
|
|
|
#define BANNED_IMAGE ":/icons/yellow_biohazard64.png"
|
2014-02-02 06:25:11 -05:00
|
|
|
|
2012-11-19 17:14:45 -05:00
|
|
|
/** Constructor */
|
2020-02-11 16:14:55 -05:00
|
|
|
GxsIdRSTreeWidgetItem::GxsIdRSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, uint32_t icon_mask,bool auto_tooltip,QTreeWidget *parent)
|
|
|
|
: QObject(NULL), RSTreeWidgetItem(compareRole, parent), mColumn(0), mIconTypeMask(icon_mask),mAutoTooltip(auto_tooltip)
|
2012-11-19 17:14:45 -05:00
|
|
|
{
|
2012-11-21 13:55:52 -05:00
|
|
|
init();
|
|
|
|
}
|
2012-11-19 17:14:45 -05:00
|
|
|
|
2013-03-12 20:33:14 -04:00
|
|
|
void GxsIdRSTreeWidgetItem::init()
|
2012-11-19 17:14:45 -05:00
|
|
|
{
|
2015-03-09 16:41:14 -04:00
|
|
|
mIdFound = false;
|
|
|
|
mRetryWhenFailed = false;
|
2016-03-09 21:49:24 -05:00
|
|
|
mBannedState = false ;
|
2012-11-19 17:14:45 -05:00
|
|
|
}
|
|
|
|
|
2014-11-26 19:55:48 -05:00
|
|
|
static void fillGxsIdRSTreeWidgetItemCallback(GxsIdDetailsType type, const RsIdentityDetails &details, QObject *object, const QVariant &/*data*/)
|
2012-11-19 17:14:45 -05:00
|
|
|
{
|
2015-03-09 16:41:14 -04:00
|
|
|
GxsIdRSTreeWidgetItem *item = dynamic_cast<GxsIdRSTreeWidgetItem*>(object);
|
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-04 18:05:56 -05:00
|
|
|
|
2015-03-09 16:41:14 -04:00
|
|
|
QList<QIcon> icons;
|
2015-02-04 18:05:56 -05:00
|
|
|
|
2015-03-09 16:41:14 -04:00
|
|
|
switch (type) {
|
|
|
|
case GXS_ID_DETAILS_TYPE_EMPTY:
|
|
|
|
item->processResult(true);
|
|
|
|
break;
|
2015-02-04 18:05:56 -05:00
|
|
|
|
2015-03-09 16:41:14 -04:00
|
|
|
case GXS_ID_DETAILS_TYPE_FAILED:
|
|
|
|
item->processResult(false);
|
|
|
|
break;
|
2015-02-04 18:05:56 -05:00
|
|
|
|
2015-03-09 16:41:14 -04:00
|
|
|
case GXS_ID_DETAILS_TYPE_LOADING:
|
|
|
|
icons.push_back(GxsIdDetails::getLoadingIcon(details.mId));
|
|
|
|
break;
|
2015-02-04 18:05:56 -05:00
|
|
|
|
2015-03-09 16:41:14 -04:00
|
|
|
case GXS_ID_DETAILS_TYPE_DONE:
|
2015-03-14 15:19:34 -04:00
|
|
|
GxsIdDetails::getIcons(details, icons, item->iconTypeMask());
|
2015-03-09 16:41:14 -04:00
|
|
|
item->processResult(true);
|
|
|
|
break;
|
2015-10-10 21:25:06 -04:00
|
|
|
|
|
|
|
case GXS_ID_DETAILS_TYPE_BANNED:
|
2015-10-11 22:08:19 -04:00
|
|
|
icons.push_back(QIcon("BANNED_IMAGE")) ;
|
2015-10-10 21:25:06 -04:00
|
|
|
break ;
|
2015-03-09 16:41:14 -04:00
|
|
|
}
|
2015-02-04 18:05:56 -05:00
|
|
|
|
2015-03-09 16:41:14 -04:00
|
|
|
int column = item->idColumn();
|
2020-02-11 16:14:55 -05:00
|
|
|
|
|
|
|
if(item->autoTooltip())
|
|
|
|
item->setToolTip(column, GxsIdDetails::getComment(details));
|
2015-02-04 18:05:56 -05:00
|
|
|
|
2015-03-09 16:41:14 -04:00
|
|
|
item->setText(column, GxsIdDetails::getNameForType(type, details));
|
|
|
|
item->setData(column, Qt::UserRole, QString::fromStdString(details.mId.toStdString()));
|
|
|
|
|
|
|
|
QPixmap combinedPixmap;
|
|
|
|
if (!icons.empty()) {
|
2015-10-12 10:58:24 -04:00
|
|
|
GxsIdDetails::GenerateCombinedPixmap(combinedPixmap, icons, QFontMetricsF(item->font(item->idColumn())).height()*1.4);
|
2015-03-09 16:41:14 -04:00
|
|
|
}
|
|
|
|
item->setData(column, Qt::DecorationRole, combinedPixmap);
|
2015-06-24 15:27:52 -04:00
|
|
|
item->setAvatar(details.mAvatar);
|
2013-03-12 20:33:14 -04:00
|
|
|
}
|
|
|
|
|
2015-03-09 16:41:14 -04:00
|
|
|
void GxsIdRSTreeWidgetItem::setId(const RsGxsId &id, int column, bool retryWhenFailed)
|
2013-03-12 20:33:14 -04:00
|
|
|
{
|
2014-11-26 19:55:48 -05:00
|
|
|
//std::cerr << " GxsIdRSTreeWidgetItem::setId(" << id << "," << column << ")";
|
|
|
|
//std::cerr << std::endl;
|
2013-03-12 20:33:14 -04:00
|
|
|
|
2017-01-14 05:02:02 -05:00
|
|
|
if (mIdFound && mColumn == column && mId == id && (mBannedState == rsReputations->isIdentityBanned(mId)))
|
2015-03-09 16:41:14 -04:00
|
|
|
return;
|
2015-02-25 06:55:31 -05:00
|
|
|
|
2017-01-14 05:02:02 -05:00
|
|
|
mBannedState = rsReputations->isIdentityBanned(mId);
|
2015-03-09 16:41:14 -04:00
|
|
|
mIdFound = false;
|
|
|
|
mRetryWhenFailed = retryWhenFailed;
|
|
|
|
|
2013-03-12 20:33:14 -04:00
|
|
|
mId = id;
|
|
|
|
mColumn = column;
|
2014-11-26 19:55:48 -05:00
|
|
|
|
2015-03-09 16:41:14 -04:00
|
|
|
startProcess();
|
|
|
|
}
|
|
|
|
|
2016-03-09 21:49:24 -05:00
|
|
|
void GxsIdRSTreeWidgetItem::updateBannedState()
|
|
|
|
{
|
2017-01-13 12:31:50 -05:00
|
|
|
if(mBannedState != rsReputations->isIdentityBanned(mId))
|
2016-03-09 21:49:24 -05:00
|
|
|
forceUpdate() ;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GxsIdRSTreeWidgetItem::forceUpdate()
|
|
|
|
{
|
|
|
|
mIdFound = false;
|
2017-01-13 12:31:50 -05:00
|
|
|
mBannedState = (rsReputations->isIdentityBanned(mId));
|
2016-03-09 21:49:24 -05:00
|
|
|
|
|
|
|
startProcess();
|
|
|
|
}
|
|
|
|
|
2015-03-09 16:41:14 -04:00
|
|
|
void GxsIdRSTreeWidgetItem::startProcess()
|
|
|
|
{
|
|
|
|
if (mRetryWhenFailed) {
|
|
|
|
disconnect(rApp, SIGNAL(minuteTick()), this, SLOT(startProcess()));
|
|
|
|
}
|
|
|
|
|
2014-11-26 19:55:48 -05:00
|
|
|
GxsIdDetails::process(mId, fillGxsIdRSTreeWidgetItemCallback, this);
|
2013-03-12 20:33:14 -04:00
|
|
|
}
|
2012-11-19 17:14:45 -05:00
|
|
|
|
2014-11-26 19:55:48 -05:00
|
|
|
bool GxsIdRSTreeWidgetItem::getId(RsGxsId &id)
|
2013-03-12 20:33:14 -04:00
|
|
|
{
|
|
|
|
id = mId;
|
2012-11-19 17:14:45 -05:00
|
|
|
return true;
|
|
|
|
}
|
2015-03-09 16:41:14 -04:00
|
|
|
|
|
|
|
void GxsIdRSTreeWidgetItem::processResult(bool success)
|
|
|
|
{
|
|
|
|
mIdFound = success;
|
|
|
|
|
|
|
|
if (!mIdFound && mRetryWhenFailed) {
|
|
|
|
/* Try again */
|
|
|
|
connect(rApp, SIGNAL(minuteTick()), this, SLOT(startProcess()));
|
|
|
|
}
|
|
|
|
}
|
2015-06-24 15:27:52 -04:00
|
|
|
|
|
|
|
void GxsIdRSTreeWidgetItem::setAvatar(const RsGxsImage &avatar)
|
|
|
|
{
|
|
|
|
mAvatar = avatar;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant GxsIdRSTreeWidgetItem::data(int column, int role) const
|
|
|
|
{
|
2019-02-16 09:42:22 -05:00
|
|
|
if (column == idColumn())
|
|
|
|
{
|
|
|
|
if (role == Qt::ToolTipRole)
|
|
|
|
{
|
|
|
|
QString t = RSTreeWidgetItem::data(column, role).toString();
|
2019-06-03 17:52:29 -04:00
|
|
|
QPixmap pix;
|
|
|
|
|
|
|
|
if(mId.isNull())
|
|
|
|
return RSTreeWidgetItem::data(column, role);
|
|
|
|
else if( rsReputations->overallReputationLevel(mId) == RsReputationLevel::LOCALLY_NEGATIVE )
|
|
|
|
pix = QPixmap(BANNED_IMAGE);
|
2019-06-04 05:49:26 -04:00
|
|
|
else if ( mAvatar.mSize == 0 || !GxsIdDetails::loadPixmapFromData(mAvatar.mData, mAvatar.mSize, pix,GxsIdDetails::LARGE) )
|
|
|
|
pix = GxsIdDetails::makeDefaultIcon(mId,GxsIdDetails::LARGE);
|
2019-02-16 09:42:22 -05:00
|
|
|
|
|
|
|
int S = QFontMetricsF(font(column)).height();
|
|
|
|
|
|
|
|
QString embeddedImage;
|
2019-06-03 17:52:29 -04:00
|
|
|
|
|
|
|
if ( RsHtml::makeEmbeddedImage( pix.scaled(QSize(4*S,4*S), Qt::KeepAspectRatio, Qt::SmoothTransformation ).toImage(), embeddedImage, 8*S * 8*S ) )
|
|
|
|
t = "<table><tr><td>" + embeddedImage + "</td><td>" + t + "</td></table>";
|
2019-02-16 09:42:22 -05:00
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return RSTreeWidgetItem::data(column, role);
|
2015-06-24 15:27:52 -04:00
|
|
|
}
|
2020-02-18 04:50:55 -05:00
|
|
|
|
|
|
|
QSize GxsIdTreeItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
RsGxsId id(index.data(Qt::UserRole).toString().toStdString());
|
|
|
|
|
|
|
|
if(id.isNull())
|
|
|
|
return QStyledItemDelegate::sizeHint(option,index);
|
|
|
|
|
|
|
|
QStyleOptionViewItemV4 opt = option;
|
|
|
|
initStyleOption(&opt, index);
|
|
|
|
|
|
|
|
// disable default icon
|
|
|
|
opt.icon = QIcon();
|
|
|
|
const QRect r = option.rect;
|
|
|
|
QString str;
|
|
|
|
QList<QIcon> icons;
|
|
|
|
QString comment;
|
|
|
|
|
|
|
|
QFontMetricsF fm(option.font);
|
|
|
|
float f = fm.height();
|
|
|
|
|
|
|
|
QIcon icon ;
|
|
|
|
|
|
|
|
if(!GxsIdDetails::MakeIdDesc(id, true, str, icons, comment,GxsIdDetails::ICON_TYPE_AVATAR))
|
|
|
|
{
|
|
|
|
icon = GxsIdDetails::getLoadingIcon(id);
|
|
|
|
launchAsyncLoading();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
icon = *icons.begin();
|
|
|
|
|
|
|
|
QPixmap pix = icon.pixmap(r.size());
|
|
|
|
|
|
|
|
return QSize(1.2*(pix.width() + fm.width(str)),std::max(1.1*pix.height(),1.4*fm.height()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GxsIdTreeItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
if(!index.isValid())
|
|
|
|
{
|
|
|
|
std::cerr << "(EE) attempt to draw an invalid index." << std::endl;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
RsGxsId id(index.data(Qt::UserRole).toString().toStdString());
|
|
|
|
|
|
|
|
if(id.isNull())
|
|
|
|
return QStyledItemDelegate::paint(painter,option,index);
|
|
|
|
|
|
|
|
QStyleOptionViewItemV4 opt = option;
|
|
|
|
initStyleOption(&opt, index);
|
|
|
|
|
|
|
|
// disable default icon
|
|
|
|
opt.icon = QIcon();
|
|
|
|
// draw default item
|
|
|
|
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter, 0);
|
|
|
|
|
|
|
|
QRect r = option.rect;
|
|
|
|
|
|
|
|
QString str;
|
|
|
|
QString comment;
|
|
|
|
|
|
|
|
QFontMetricsF fm(painter->font());
|
|
|
|
float f = fm.height();
|
|
|
|
|
|
|
|
QIcon icon ;
|
|
|
|
|
|
|
|
if(id.isNull())
|
|
|
|
{
|
|
|
|
str = tr("[Notification]");
|
|
|
|
icon = QIcon(":/icons/logo_128.png");
|
|
|
|
}
|
|
|
|
else if(! computeNameIconAndComment(id,str,icon,comment))
|
|
|
|
if(mReloadPeriod > 3)
|
|
|
|
{
|
|
|
|
str = tr("[Unknown]");
|
|
|
|
icon = QIcon();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
icon = GxsIdDetails::getLoadingIcon(id);
|
|
|
|
launchAsyncLoading();
|
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap pix = icon.pixmap(r.size());
|
|
|
|
const QPoint p = QPoint(r.height()/2.0, (r.height() - pix.height())/2);
|
|
|
|
|
|
|
|
// draw pixmap at center of item
|
|
|
|
painter->drawPixmap(r.topLeft() + p, pix);
|
|
|
|
//painter->drawText(r.topLeft() + QPoint(r.height()+ f/2.0 + f/2.0,f*1.0), str);
|
|
|
|
|
|
|
|
//cr.adjust(margin(), margin(), -margin(), -margin());
|
|
|
|
|
|
|
|
QRect mRectElision;
|
|
|
|
|
|
|
|
r.adjust(pix.height()+f,(r.height()-f)/2.0,0,0);
|
|
|
|
|
|
|
|
bool didElide = ElidedLabel::paintElidedLine(*painter,str,r,Qt::AlignLeft,false,false,mRectElision);
|
|
|
|
|
|
|
|
}
|