Optimized search of feed items in RSFeedWidget by using a map instead of QTreeWidgetItemIterator.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@8013 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
thunder2 2015-03-12 13:59:56 +00:00
parent 6ef761e4e5
commit cdf9a5b4d4
6 changed files with 168 additions and 53 deletions

View File

@ -26,7 +26,8 @@
#include "ui_RSFeedWidget.h"
#include "RSTreeWidgetItem.h"
#include "gui/feeds/FeedItem.h"
#include "gui/gxs/GxsFeedItem.h"
#include <iostream>
#define COLUMN_FEED 0
@ -108,6 +109,7 @@ bool RSFeedWidget::eventFilter(QObject *object, QEvent *event)
FeedItem *feedItem = feedItemFromTreeItem(treeItem);
if (feedItem) {
disconnectSignals(feedItem);
feedRemoved(feedItem);
delete(feedItem);
}
delete(treeItem);
@ -127,6 +129,21 @@ bool RSFeedWidget::eventFilter(QObject *object, QEvent *event)
return QWidget::eventFilter(object, event);
}
void RSFeedWidget::feedAdded(FeedItem *feedItem, QTreeWidgetItem *treeItem)
{
mItems.insert(feedItem, treeItem);
}
void RSFeedWidget::feedRemoved(FeedItem *feedItem)
{
mItems.remove(feedItem);
}
void RSFeedWidget::feedsCleared()
{
mItems.clear();
}
void RSFeedWidget::connectSignals(FeedItem *feedItem)
{
connect(feedItem, SIGNAL(feedItemDestroyed(FeedItem*)), this, SLOT(feedItemDestroyed(FeedItem*)));
@ -167,6 +184,8 @@ void RSFeedWidget::addFeedItem(FeedItem *feedItem, Qt::ItemDataRole sortRole, co
ui->treeWidget->addTopLevelItem(treeItem);
ui->treeWidget->setItemWidget(treeItem, 0, feedItem);
feedAdded(feedItem, treeItem);
connectSignals(feedItem);
filterItem(treeItem, feedItem);
@ -192,6 +211,8 @@ void RSFeedWidget::addFeedItem(FeedItem *feedItem, const QMap<Qt::ItemDataRole,
ui->treeWidget->addTopLevelItem(treeItem);
ui->treeWidget->setItemWidget(treeItem, 0, feedItem);
feedAdded(feedItem, treeItem);
connectSignals(feedItem);
filterItem(treeItem, feedItem);
@ -246,6 +267,8 @@ void RSFeedWidget::clear()
}
}
feedsCleared();
/* Clear items */
ui->treeWidget->clear();
@ -369,6 +392,7 @@ void RSFeedWidget::removeFeedItem(FeedItem *feedItem)
disconnectSignals(feedItem);
QTreeWidgetItem *treeItem = findTreeWidgetItem(feedItem);
feedRemoved(feedItem);
if (treeItem) {
delete(treeItem);
}
@ -396,6 +420,7 @@ void RSFeedWidget::feedItemDestroyed(FeedItem *feedItem)
/* No need to disconnect when object will be destroyed */
QTreeWidgetItem *treeItem = findTreeWidgetItem(feedItem);
feedRemoved(feedItem);
if (treeItem) {
delete(treeItem);
}
@ -407,16 +432,12 @@ void RSFeedWidget::feedItemDestroyed(FeedItem *feedItem)
QTreeWidgetItem *RSFeedWidget::findTreeWidgetItem(FeedItem *feedItem)
{
QTreeWidgetItemIterator it(ui->treeWidget);
QTreeWidgetItem *treeItem;
while ((treeItem = *it) != NULL) {
++it;
if (feedItemFromTreeItem(treeItem) == feedItem) {
return treeItem;
}
QMap<FeedItem*, QTreeWidgetItem*>::iterator it = mItems.find(feedItem);
if (it == mItems.end()) {
return NULL;
}
return NULL;
return it.value();
}
bool RSFeedWidget::scrollTo(FeedItem *feedItem, bool focus)
@ -491,38 +512,3 @@ void RSFeedWidget::selectedFeedItems(QList<FeedItem*> &feedItems)
feedItems.push_back(feedItem);
}
}
struct FindGxsFeedItemData
{
FindGxsFeedItemData(const RsGxsGroupId &groupId, const RsGxsMessageId &messageId) : mGroupId(groupId), mMessageId(messageId) {}
const RsGxsGroupId &mGroupId;
const RsGxsMessageId &mMessageId;
};
static bool findGxsFeedItemCallback(FeedItem *feedItem, void *data)
{
FindGxsFeedItemData *findData = (FindGxsFeedItemData*) data;
if (!findData || findData->mGroupId.isNull() || findData->mMessageId.isNull()) {
return false;
}
GxsFeedItem *item = dynamic_cast<GxsFeedItem*>(feedItem);
if (!item) {
return false;
}
if (item->groupId() != findData->mGroupId ||
item->messageId() != findData->mMessageId) {
return false;
}
return true;
}
GxsFeedItem *RSFeedWidget::findGxsFeedItem(const RsGxsGroupId &groupId, const RsGxsMessageId &messageId)
{
FindGxsFeedItemData data(groupId, messageId);
FeedItem *feedItem = findFeedItem(findGxsFeedItemCallback, &data);
return dynamic_cast<GxsFeedItem*>(feedItem);
}

View File

@ -26,14 +26,11 @@
#include <QWidget>
#include <QMap>
#include "retroshare/rsgxsifacetypes.h"
#define FEED_TREEWIDGET_SORTROLE Qt::UserRole
class FeedItem;
class QTreeWidgetItem;
class RSTreeWidgetItemCompareRole;
class GxsFeedItem;
namespace Ui {
class RSFeedWidget;
@ -79,9 +76,6 @@ public:
void selectedFeedItems(QList<FeedItem*> &feedItems);
/* Convenience functions */
GxsFeedItem *findGxsFeedItem(const RsGxsGroupId &groupId, const RsGxsMessageId &messageId);
signals:
void feedCountChanged();
@ -93,6 +87,9 @@ public slots:
protected:
bool eventFilter(QObject *object, QEvent *event);
virtual void feedAdded(FeedItem *feedItem, QTreeWidgetItem *treeItem);
virtual void feedRemoved(FeedItem *feedItem);
virtual void feedsCleared();
private slots:
void feedItemDestroyed(FeedItem *feedItem);
@ -121,6 +118,9 @@ private:
/* Options */
int mCountChangedDisabled;
/* Items */
QMap<FeedItem*, QTreeWidgetItem*> mItems;
Ui::RSFeedWidget *ui;
};

View File

@ -0,0 +1,75 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2015, RetroShare Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#include "GxsFeedWidget.h"
#include "gui/gxs/GxsFeedItem.h"
#define PAIR(groupId,messageId) QPair<RsGxsGroupId, RsGxsMessageId>(groupId, messageId)
GxsFeedWidget::GxsFeedWidget(QWidget *parent)
: RSFeedWidget(parent)
{
}
GxsFeedWidget::~GxsFeedWidget()
{
}
void GxsFeedWidget::feedAdded(FeedItem *feedItem, QTreeWidgetItem *treeItem)
{
RSFeedWidget::feedAdded(feedItem, treeItem);
GxsFeedItem *gxsFeedItem = dynamic_cast<GxsFeedItem*>(feedItem);
if (!gxsFeedItem) {
return;
}
mGxsItems.insert(PAIR(gxsFeedItem->groupId(), gxsFeedItem->messageId()), feedItem);
}
void GxsFeedWidget::feedRemoved(FeedItem *feedItem)
{
RSFeedWidget::feedRemoved(feedItem);
GxsFeedItem *gxsFeedItem = dynamic_cast<GxsFeedItem*>(feedItem);
if (!gxsFeedItem) {
return;
}
mGxsItems.remove(PAIR(gxsFeedItem->groupId(), gxsFeedItem->messageId()));
}
void GxsFeedWidget::feedsCleared()
{
RSFeedWidget::feedsCleared();
mGxsItems.clear();
}
GxsFeedItem *GxsFeedWidget::findGxsFeedItem(const RsGxsGroupId &groupId, const RsGxsMessageId &messageId)
{
QMap<QPair<RsGxsGroupId, RsGxsMessageId>, FeedItem*>::iterator it = mGxsItems.find(PAIR(groupId, messageId));
if (it == mGxsItems.end()) {
return NULL;
}
return dynamic_cast<GxsFeedItem*>(it.value());
}

View File

@ -0,0 +1,52 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2015, RetroShare Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
****************************************************************/
#ifndef _GXSFEEDTREEWIDGET_H
#define _GXSFEEDTREEWIDGET_H
#include "gui/common/RSFeedWidget.h"
#include "retroshare/rsgxsifacetypes.h"
class GxsFeedItem;
class GxsFeedWidget : public RSFeedWidget
{
Q_OBJECT
public:
GxsFeedWidget(QWidget *parent = 0);
virtual ~GxsFeedWidget();
GxsFeedItem *findGxsFeedItem(const RsGxsGroupId &groupId, const RsGxsMessageId &messageId);
protected:
/* RSFeedWidget */
virtual void feedAdded(FeedItem *feedItem, QTreeWidgetItem *treeItem);
virtual void feedRemoved(FeedItem *feedItem);
virtual void feedsCleared();
private:
/* Items */
QMap<QPair<RsGxsGroupId, RsGxsMessageId>, FeedItem*> mGxsItems;
};
#endif

View File

@ -454,7 +454,7 @@
</widget>
</item>
<item>
<widget class="RSFeedWidget" name="feedWidget" native="true">
<widget class="GxsFeedWidget" name="feedWidget" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
@ -497,9 +497,9 @@
<header>gui/common/SubscribeToolButton.h</header>
</customwidget>
<customwidget>
<class>RSFeedWidget</class>
<class>GxsFeedWidget</class>
<extends>QWidget</extends>
<header>gui/common/RSFeedWidget.h</header>
<header>gui/gxs/GxsFeedWidget.h</header>
<container>1</container>
</customwidget>
<customwidget>

View File

@ -1297,6 +1297,7 @@ gxsgui {
gui/gxs/RsGxsUpdateBroadcastPage.h \
gui/gxs/GxsGroupShareKey.h \
gui/gxs/GxsUserNotify.h \
gui/gxs/GxsFeedWidget.h \
util/TokenQueue.h \
util/RsGxsUpdateBroadcast.h \
@ -1333,6 +1334,7 @@ gxsgui {
gui/gxs/RsGxsUpdateBroadcastWidget.cpp \
gui/gxs/RsGxsUpdateBroadcastPage.cpp \
gui/gxs/GxsUserNotify.cpp \
gui/gxs/GxsFeedWidget.cpp \
util/TokenQueue.cpp \
util/RsGxsUpdateBroadcast.cpp \