History Browser:

- Fixed crash on delete history items
- Threaded load of messages

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3599 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
thunder2 2010-10-03 23:55:11 +00:00
parent 9c562a97d4
commit 49320c2ed9
6 changed files with 153 additions and 21 deletions

View File

@ -25,7 +25,7 @@
#include "IMHistoryItemPainter.h"
IMHistoryItemPainter::IMHistoryItemPainter(QString text)
IMHistoryItemPainter::IMHistoryItemPainter(const QString &text)
{
itemText = text;

View File

@ -35,7 +35,7 @@ class IMHistoryItemPainter
public:
enum EditMode { /*Editable,*/ ReadOnly };
IMHistoryItemPainter(QString text = "");
IMHistoryItemPainter(const QString &text = "");
void paint(QPainter *painter, const QStyleOptionViewItem &option, EditMode mode) const;
QSize sizeHint() const;

View File

@ -235,7 +235,7 @@ void IMHistoryKeeper::removeMessages(QList<int> &hiids)
if (qFind(hiids, it->hiid) != hiids.end()) {
emit historyRemove(*it);
hitems.erase(it);
it = hitems.erase(it);
changed = true;

View File

@ -27,6 +27,7 @@
#include <QTextEdit>
#include <QClipboard>
#include <QKeyEvent>
#include <QThread>
#include "ImHistoryBrowser.h"
#include "IMHistoryItemDelegate.h"
@ -40,6 +41,40 @@
#define ROLE_PLAINTEXT Qt::UserRole + 1
#define ROLE_OFFLINE Qt::UserRole + 2
ImHistoryBrowserCreateItemsThread::ImHistoryBrowserCreateItemsThread(ImHistoryBrowser *parent, IMHistoryKeeper &histKeeper)
: QThread(parent), m_historyKeeper(histKeeper)
{
m_historyBrowser = parent;
}
ImHistoryBrowserCreateItemsThread::~ImHistoryBrowserCreateItemsThread()
{
// remove all items (when items are available, the thread was terminated)
QList<QListWidgetItem*>::iterator it;
for (it = m_items.begin(); it != m_items.end(); it++) {
delete(*it);
}
m_items.clear();
}
void ImHistoryBrowserCreateItemsThread::run()
{
QList<IMHistoryItem> historyItems;
m_historyKeeper.getMessages(historyItems, 0);
int count = historyItems.count();
int current = 0;
foreach(IMHistoryItem item, historyItems) {
QListWidgetItem *itemWidget = m_historyBrowser->createItem(item);
if (itemWidget) {
m_items.push_back(itemWidget);
emit progress(++current, count);
}
}
}
/** Default constructor */
ImHistoryBrowser::ImHistoryBrowser(const std::string &peerId, IMHistoryKeeper &histKeeper, QTextEdit *edit, QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags), historyKeeper(histKeeper)
@ -82,26 +117,75 @@ ImHistoryBrowser::ImHistoryBrowser(const std::string &peerId, IMHistoryKeeper &h
// call once
privateChatChanged(NOTIFY_LIST_PRIVATE_OUTGOING_CHAT, NOTIFY_TYPE_ADD);
QList<IMHistoryItem> historyItems;
historyKeeper.getMessages(historyItems, 0);
foreach(IMHistoryItem item, historyItems) {
addItem(item);
}
QByteArray geometry = Settings->valueFromGroup("HistorieBrowser", "Geometry", QByteArray()).toByteArray();
if (geometry.isEmpty() == false) {
restoreGeometry(geometry);
}
// dummy call for set butons
// dummy call for set buttons
itemSelectionChanged();
ui.listWidget->installEventFilter(this);
m_createThread = new ImHistoryBrowserCreateItemsThread(this, historyKeeper);
connect(m_createThread, SIGNAL(finished()), this, SLOT(createThreadFinished()));
connect(m_createThread, SIGNAL(terminated()), this, SLOT(createThreadTerminated()));
connect(m_createThread, SIGNAL(progress(int,int)), this, SLOT(createThreadProgress(int,int)));
m_createThread->start();
}
ImHistoryBrowser::~ImHistoryBrowser()
{
Settings->setValueToGroup("HistorieBrowser", "Geometry", saveGeometry());
if (m_createThread) {
m_createThread->terminate();
}
}
void ImHistoryBrowser::createThreadTerminated()
{
if (m_createThread == sender()) {
ui.progressBar->setVisible(false);
m_createThread = NULL;
}
}
void ImHistoryBrowser::createThreadFinished()
{
if (m_createThread == sender()) {
ui.progressBar->setVisible(false);
// append created items
QList<QListWidgetItem*>::iterator it;
for (it = m_createThread->m_items.begin(); it != m_createThread->m_items.end(); it++) {
ui.listWidget->addItem(*it);
}
// clear list
m_createThread->m_items.clear();
filterRegExpChanged();
// dummy call for set buttons
itemSelectionChanged();
delete(m_createThread);
m_createThread = NULL;
QList<IMHistoryItem>::iterator histIt;
for (histIt = m_itemsAddedOnLoad.begin(); histIt != m_itemsAddedOnLoad.end(); histIt++) {
historyAdd(*histIt);
}
m_itemsAddedOnLoad.clear();
}
}
void ImHistoryBrowser::createThreadProgress(int current, int count)
{
if (count) {
ui.progressBar->setValue(current * ui.progressBar->maximum() / count);
}
}
bool ImHistoryBrowser::eventFilter(QObject *obj, QEvent *event)
@ -122,8 +206,15 @@ bool ImHistoryBrowser::eventFilter(QObject *obj, QEvent *event)
void ImHistoryBrowser::historyAdd(IMHistoryItem item)
{
QListWidgetItem *itemWidget = addItem(item);
if (m_createThread) {
// create later
m_itemsAddedOnLoad.push_back(item);
return;
}
QListWidgetItem *itemWidget = createItem(item);
if (itemWidget) {
ui.listWidget->addItem(itemWidget);
filterItems(itemWidget);
}
}
@ -190,14 +281,11 @@ void ImHistoryBrowser::fillItem(QListWidgetItem *itemWidget, IMHistoryItem &item
QTextDocument doc;
doc.setHtml(item.messageText);
itemWidget->setData(ROLE_PLAINTEXT, doc.toPlainText());
filterRegExpChanged();
}
QListWidgetItem *ImHistoryBrowser::addItem(IMHistoryItem &item)
QListWidgetItem *ImHistoryBrowser::createItem(IMHistoryItem &item)
{
QListWidgetItem *itemWidget = new QListWidgetItem;
ui.listWidget->addItem(itemWidget);
fillItem(itemWidget, item);
return itemWidget;
}
@ -415,5 +503,7 @@ void ImHistoryBrowser::privateChatChanged(int list, int type)
fillItem(itemWidget, item);
}
}
filterRegExpChanged();
}
}

View File

@ -32,10 +32,13 @@
#include "ui_ImHistoryBrowser.h"
class QTextEdit;
class ImHistoryBrowserCreateItemsThread;
class ImHistoryBrowser : public QDialog
{
Q_OBJECT
Q_OBJECT
friend class ImHistoryBrowserCreateItemsThread;
public:
/** Default constructor */
@ -47,6 +50,10 @@ protected:
bool eventFilter(QObject *obj, QEvent *ev);
private slots:
void createThreadTerminated();
void createThreadFinished();
void createThreadProgress(int current, int count);
void historyAdd(IMHistoryItem item);
void historyRemove(IMHistoryItem item);
void historyClear();
@ -65,12 +72,14 @@ private slots:
void privateChatChanged(int list, int type);
private:
QListWidgetItem *addItem(IMHistoryItem &item);
QListWidgetItem *createItem(IMHistoryItem &item);
void fillItem(QListWidgetItem *itemWidget, IMHistoryItem &item);
void filterItems(QListWidgetItem *item = NULL);
void getSelectedItems(QList<int> &items);
ImHistoryBrowserCreateItemsThread *m_createThread;
std::string m_peerId;
bool m_isPrivateChat;
QTextEdit *textEdit;
@ -79,9 +88,31 @@ private:
ChatStyle style;
std::list<ChatInfo> m_savedOfflineChat;
QList<IMHistoryItem> m_itemsAddedOnLoad;
/** Qt Designer generated object */
Ui::ImHistoryBrowser ui;
};
class ImHistoryBrowserCreateItemsThread : public QThread
{
Q_OBJECT
public:
ImHistoryBrowserCreateItemsThread(ImHistoryBrowser *parent, IMHistoryKeeper &histKeeper);
~ImHistoryBrowserCreateItemsThread();
void run();
signals:
void progress(int current, int count);
public:
QList<QListWidgetItem*> m_items;
private:
IMHistoryKeeper &m_historyKeeper;
ImHistoryBrowser *m_historyBrowser;
};
#endif

View File

@ -240,7 +240,7 @@ border-image: url(:/images/closepressed.png)
<property name="margin">
<number>9</number>
</property>
<item row="0" column="0">
<item row="1" column="0">
<widget class="QListWidget" name="listWidget">
<property name="contextMenuPolicy">
<enum>Qt::CustomContextMenu</enum>
@ -250,7 +250,7 @@ border-image: url(:/images/closepressed.png)
</property>
</widget>
</item>
<item row="1" column="0">
<item row="2" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="topMargin">
<number>6</number>
@ -283,14 +283,25 @@ border-image: url(:/images/closepressed.png)
</item>
</layout>
</item>
<item row="0" column="0">
<widget class="QProgressBar" name="progressBar">
<property name="maximum">
<number>1000</number>
</property>
<property name="value">
<number>24</number>
</property>
<property name="textVisible">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources>
<include location="../images.qrc"/>
<include location="../images.qrc"/>
<include location="../images.qrc"/>
</resources>
<connections>
<connection>