Created a basic widget of the group tree with the filter out of the channels and used it in the forums tree too.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3937 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
thunder2 2010-12-27 00:15:25 +00:00
parent 7b3da2165a
commit df1e8b7537
14 changed files with 2357 additions and 2646 deletions

View file

@ -0,0 +1,356 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2010, 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 "GroupTreeWidget.h"
#include "ui_GroupTreeWidget.h"
#include "RSItemDelegate.h"
#include "PopularityDefs.h"
#define COLUMN_NAME 0
#define COLUMN_POPULARITY 1
#define COLUMN_COUNT 2
#define COLUMN_DATA COLUMN_NAME
#define ROLE_ID Qt::UserRole
#define ROLE_NAME Qt::UserRole + 1
#define ROLE_DESCRIPTION Qt::UserRole + 2
#define ROLE_LASTPOST Qt::UserRole + 3
#define ROLE_SEARCH_SCORE Qt::UserRole + 4
#define COMBO_NAME_INDEX 0
#define COMBO_DESC_INDEX 1
GroupTreeWidget::GroupTreeWidget(QWidget *parent) :
QWidget(parent), ui(new Ui::GroupTreeWidget)
{
ui->setupUi(this);
/* Connect signals */
connect(ui->clearFilter, SIGNAL(clicked()), this, SLOT(clearFilter()));
connect(ui->filterText, SIGNAL(textChanged(const QString &)), this, SLOT(filterChanged()));
connect(ui->filterCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(filterChanged()));
connect(ui->treeWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customContextMenuRequested(QPoint)));
connect(ui->treeWidget, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), this, SLOT(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)));
/* Add own item delegate */
RSItemDelegate *itemDelegate = new RSItemDelegate(this);
itemDelegate->removeFocusRect(COLUMN_POPULARITY);
itemDelegate->setSpacing(QSize(0, 2));
ui->treeWidget->setItemDelegate(itemDelegate);
/* Initialize tree widget */
ui->treeWidget->setColumnCount(COLUMN_COUNT);
/* Set header resize modes and initial section sizes */
QHeaderView *header = ui->treeWidget->header ();
header->setResizeMode(COLUMN_NAME, QHeaderView::Stretch);
header->resizeSection(COLUMN_NAME, 170);
header->setResizeMode(COLUMN_POPULARITY, QHeaderView::Fixed);
header->resizeSection(COLUMN_POPULARITY, 25);
/* Initialize filter */
ui->clearFilter->hide();
}
GroupTreeWidget::~GroupTreeWidget()
{
delete ui;
}
void GroupTreeWidget::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void GroupTreeWidget::customContextMenuRequested(const QPoint &pos)
{
emit treeCustomContextMenuRequested(pos);
}
void GroupTreeWidget::currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
{
QString id;
if (current) {
id = current->data(COLUMN_DATA, ROLE_ID).toString();
}
emit treeCurrentItemChanged(id);
}
QTreeWidgetItem *GroupTreeWidget::addCategoryItem(const QString &name, const QIcon &icon, bool expand)
{
QFont font = QFont("ARIAL", 10);
font.setBold(true);
QTreeWidgetItem *item = new QTreeWidgetItem();
item->setText(COLUMN_NAME, name);
item->setData(COLUMN_DATA, ROLE_NAME, name);
item->setFont(COLUMN_NAME, font);
item->setIcon(COLUMN_NAME, icon);
item->setSizeHint(COLUMN_NAME, QSize(18, 18));
item->setForeground(COLUMN_NAME, QBrush(QColor(79, 79, 79)));
ui->treeWidget->addTopLevelItem(item);
item->setExpanded(expand);
return item;
}
QString GroupTreeWidget::itemId(QTreeWidgetItem *item)
{
if (item == NULL) {
return "";
}
return item->data(COLUMN_DATA, ROLE_ID).toString();
}
void GroupTreeWidget::fillGroupItems(QTreeWidgetItem *categoryItem, const QList<GroupItemInfo> &itemList)
{
if (categoryItem == NULL) {
return;
}
/* Iterate all items */
QList<GroupItemInfo>::const_iterator it;
for (it = itemList.begin(); it != itemList.end(); it++) {
const GroupItemInfo &itemInfo = *it;
QTreeWidgetItem *item = NULL;
/* Search exisiting item */
int childCount = categoryItem->childCount();
for (int child = 0; child < childCount; child++) {
QTreeWidgetItem *childItem = categoryItem->child(child);
if (childItem->data(COLUMN_DATA, ROLE_ID).toString() == itemInfo.id) {
/* Found child */
item = childItem;
break;
}
}
if (item == NULL) {
item = new GroupTreeWidgetItem();
item->setData(COLUMN_DATA, ROLE_ID, itemInfo.id);
categoryItem->addChild(item);
}
item->setText(COLUMN_NAME, itemInfo.name);
item->setData(COLUMN_DATA, ROLE_NAME, itemInfo.name);
item->setData(COLUMN_DATA, ROLE_DESCRIPTION, itemInfo.description);
/* Set last post */
item->setData(COLUMN_DATA, ROLE_LASTPOST, itemInfo.lastpost);
/* Set icon */
item->setIcon(COLUMN_NAME, itemInfo.icon);
/* Set popularity */
QString tooltip = PopularityDefs::tooltip(itemInfo.popularity);
item->setIcon(COLUMN_POPULARITY, PopularityDefs::icon(itemInfo.popularity));
/* Set tooltip */
if (itemInfo.privatekey) {
tooltip += "\n" + tr("Private Key Available");
}
item->setToolTip(COLUMN_NAME, tooltip);
item->setToolTip(COLUMN_POPULARITY, tooltip);
/* Set color */
QBrush brush;
if (itemInfo.privatekey) {
brush = QBrush(Qt::blue);
}
item->setForeground(COLUMN_NAME, brush);
/* Calculate score */
calculateScore(item);
}
/* Remove all items not in list */
int child = 0;
int childCount = categoryItem->childCount();
while (child < childCount) {
QString id = categoryItem->child(child)->data(COLUMN_DATA, ROLE_ID).toString();
for (it = itemList.begin(); it != itemList.end(); it++) {
if (it->id == id) {
break;
}
}
if (it == itemList.end()) {
delete(categoryItem->takeChild(child));
childCount = categoryItem->childCount();
} else {
child++;
}
}
categoryItem->sortChildren(COLUMN_NAME, Qt::DescendingOrder);
}
void GroupTreeWidget::setUnreadCount(QTreeWidgetItem *item, int unreadCount)
{
if (item == NULL) {
return;
}
QString name = item->data(COLUMN_DATA, ROLE_NAME).toString();
QFont font = item->font(COLUMN_NAME);
if (unreadCount) {
name += QString(" (%1)").arg(unreadCount);
font.setBold(true);
} else {
font.setBold(false);
}
item->setText(COLUMN_NAME, name);
item->setFont(COLUMN_NAME, font);
}
void GroupTreeWidget::calculateScore(QTreeWidgetItem *item)
{
if (item) {
/* Calculate one item */
QString filterText = ui->filterText->text();
int score;
if (filterText.isEmpty()) {
score = 0;
item->setHidden(false);
} else {
QString scoreString;
switch (ui->filterCombo->currentIndex()) {
case COMBO_NAME_INDEX:
scoreString = item->data(COLUMN_DATA, ROLE_NAME).toString();
break;
case COMBO_DESC_INDEX:
scoreString = item->data(COLUMN_DATA, ROLE_DESCRIPTION).toString();
break;
}
score = scoreString.count(filterText, Qt::CaseInsensitive);
if (score) {
item->setHidden(false);
} else {
item->setHidden(true);
}
}
item->setData(COLUMN_DATA, ROLE_SEARCH_SCORE, score);
return;
}
/* Find out which has given word in it */
QTreeWidgetItemIterator itemIterator(ui->treeWidget);
QTreeWidgetItem *item;
while ((item = *itemIterator) != NULL) {
itemIterator++;
if (item->data(COLUMN_DATA, ROLE_ID).toString().isEmpty()) {
continue;
}
calculateScore(item);
}
}
void GroupTreeWidget::filterChanged()
{
if (ui->filterText->text().isEmpty()) {
clearFilter();
return;
}
ui->clearFilter->setEnabled(true);
ui->clearFilter->show();
/* Recalculate score */
calculateScore(NULL);
int count = ui->treeWidget->topLevelItemCount();
for (int child = 0; child < count; child++) {
ui->treeWidget->topLevelItem(child)->sortChildren(COLUMN_NAME, Qt::DescendingOrder);
}
}
void GroupTreeWidget::clearFilter()
{
ui->filterText->clear();
ui->clearFilter->hide();
ui->filterText->setFocus();
/* Recalculate score */
calculateScore(NULL);
int count = ui->treeWidget->topLevelItemCount();
for (int child = 0; child < count; child++) {
ui->treeWidget->topLevelItem(child)->sortChildren(COLUMN_NAME, Qt::DescendingOrder);
}
}
GroupTreeWidgetItem::GroupTreeWidgetItem() : QTreeWidgetItem()
{
}
bool GroupTreeWidgetItem::operator<(const QTreeWidgetItem& other) const
{
QDateTime otherChanTs = other.data(COLUMN_DATA, ROLE_LASTPOST).toDateTime();
QDateTime thisChanTs = this->data(COLUMN_DATA, ROLE_LASTPOST).toDateTime();
uint32_t otherCount = other.data(COLUMN_DATA, ROLE_SEARCH_SCORE).toUInt();
uint32_t thisCount = this->data(COLUMN_DATA, ROLE_SEARCH_SCORE).toUInt();
/* If counts are equal then determine by who has the most recent post */
if (otherCount == thisCount){
if (thisChanTs < otherChanTs) {
return true;
}
}
/* Choose the item where the string occurs the most */
if (thisCount < otherCount) {
return true;
}
if (thisChanTs < otherChanTs) {
return true;
}
/* Compare name */
return text(COLUMN_NAME) < other.text(COLUMN_NAME);
}

View file

@ -0,0 +1,102 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2010, 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 GROUPTREEWIDGET_H
#define GROUPTREEWIDGET_H
#include <QWidget>
#include <QIcon>
#include <QTreeWidgetItem>
#include <QDateTime>
namespace Ui {
class GroupTreeWidget;
}
class GroupItemInfo
{
public:
GroupItemInfo()
{
popularity = 0;
privatekey = false;
}
public:
QString id;
QString name;
QString description;
int popularity;
QDateTime lastpost;
QIcon icon;
bool privatekey;
};
class GroupTreeWidget : public QWidget
{
Q_OBJECT
public:
GroupTreeWidget(QWidget *parent = 0);
~GroupTreeWidget();
// Add a new category item
QTreeWidgetItem *addCategoryItem(const QString &name, const QIcon &icon, bool expand);
// Get id of item
QString itemId(QTreeWidgetItem *item);
// Fill items of a group
void fillGroupItems(QTreeWidgetItem *categoryItem, const QList<GroupItemInfo> &itemList);
// Set the unread count of an item
void setUnreadCount(QTreeWidgetItem *item, int unreadCount);
signals:
void treeCustomContextMenuRequested(const QPoint &pos);
void treeCurrentItemChanged(const QString &id);
protected:
void changeEvent(QEvent *e);
private slots:
void customContextMenuRequested(const QPoint &pos);
void currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous);
void filterChanged();
void clearFilter();
private:
void calculateScore(QTreeWidgetItem *item);
private:
Ui::GroupTreeWidget *ui;
};
class GroupTreeWidgetItem : public QTreeWidgetItem
{
public:
GroupTreeWidgetItem();
/**
* reimplementing comparison operator so GroupTreeWidgetItem can be ordered in terms
* of occurences of property filterText in its data columns
*/
bool operator<(const QTreeWidgetItem &other) const;
};
#endif // GROUPTREEWIDGET_H

View file

@ -0,0 +1,240 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>GroupTreeWidget</class>
<widget class="QWidget" name="GroupTreeWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>316</width>
<height>257</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">QComboBox QAbstractItemView {
background-color:white;
}
QComboBox::down-arrow {
image: url(:/images/combobox_arrow.png);
}
QComboBox:drop-down
{
subcontrol-origin: padding;
subcontrol-position: top right;
border-left-style: none;
border-top-right-radius: 1px;
border-bottom-right-radius: 1px;
}
QComboBox {
border-image: url(:/images/btn_26.png) 4;
border-width: 4;
padding: 0px 6px;
font-size: 12px;
}
QComboBox:hover {
border-image: url(:/images/btn_26_hover.png) 4;
}
QComboBox::disabled {
color:gray;
}</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="spacing">
<number>2</number>
</property>
<property name="margin">
<number>0</number>
</property>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>0</number>
</property>
<item>
<widget class="QFrame" name="searchlineframe">
<property name="minimumSize">
<size>
<width>0</width>
<height>22</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>22</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">QFrame#searchlineframe{
background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,
stop:0 #FEFEFE, stop:1 #E8E8E8);
border: 1px solid #CCCCCC;}
</string>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QGridLayout" name="gridLayout_6">
<property name="horizontalSpacing">
<number>0</number>
</property>
<property name="verticalSpacing">
<number>1</number>
</property>
<property name="margin">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QLineEdit" name="filterText">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>3</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>16</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>1677777</width>
<height>16777215</height>
</size>
</property>
<property name="toolTip">
<string>Enter a Keyword here</string>
</property>
<property name="styleSheet">
<string notr="true">QLineEdit#searchLine{
border: none;}
</string>
</property>
</widget>
</item>
<item row="0" column="3">
<widget class="QComboBox" name="filterCombo">
<item>
<property name="text">
<string>Title</string>
</property>
</item>
<item>
<property name="text">
<string>Description</string>
</property>
</item>
</widget>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="clearFilter">
<property name="minimumSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
<property name="toolTip">
<string>Reset</string>
</property>
<property name="styleSheet">
<string notr="true">QPushButton
{
border-image: url(:/images/closenormal.png)
}
QPushButton:hover
{
border-image: url(:/images/closehover.png)
}
QPushButton:pressed {
border-image: url(:/images/closepressed.png)
}</string>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QTreeWidget" name="treeWidget">
<property name="enabled">
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="sizeIncrement">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="baseSize">
<size>
<width>300</width>
<height>0</height>
</size>
</property>
<property name="font">
<font>
<pointsize>9</pointsize>
</font>
</property>
<property name="contextMenuPolicy">
<enum>Qt::CustomContextMenu</enum>
</property>
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<attribute name="headerVisible">
<bool>false</bool>
</attribute>
<attribute name="headerStretchLastSection">
<bool>false</bool>
</attribute>
<attribute name="headerVisible">
<bool>false</bool>
</attribute>
<attribute name="headerStretchLastSection">
<bool>false</bool>
</attribute>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>