Added new class RSTreeWidgetItem derived from QTreeWidgetItem for sorting strings case insensitive without proxy model.

Sort strings case insensitive in NetworkDialog, PeersDialog (changed to RSTreeWidgetItem), MessengerWindow (changed to RSTreeWidgetItem) and SharedFilesDialog (with folders first).
Sort the columns size and age in SharedFilesDialog by internal data and not by the shown string.
Save and load layout of the SharedFilesDialog in profile.


git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3515 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
thunder2 2010-09-20 00:10:51 +00:00
parent 52e0d2a095
commit a51bf3da67
12 changed files with 407 additions and 106 deletions

View File

@ -282,6 +282,7 @@ HEADERS += rshare.h \
gui/common/StatusDefs.h \
gui/common/TagDefs.h \
gui/common/Emoticons.h \
gui/common/RSTreeWidgetItem.h \
gui/MessagesDialog.h \
gui/help/browser/helpbrowser.h \
gui/help/browser/helptextbrowser.h \
@ -476,6 +477,7 @@ SOURCES += main.cpp \
gui/common/StatusDefs.cpp \
gui/common/TagDefs.cpp \
gui/common/Emoticons.cpp \
gui/common/RSTreeWidgetItem.cpp \
gui/settings/rsharesettings.cpp \
gui/settings/RsharePeerSettings.cpp \
gui/settings/rsettings.cpp \

View File

@ -42,8 +42,8 @@
#include "chat/PopupChatDialog.h"
#include "msgs/MessageComposer.h"
#include "ShareManager.h"
#include "gui/notifyqt.h"
#include "gui/connect/ConnectFriendWizard.h"
#include "notifyqt.h"
#include "connect/ConnectFriendWizard.h"
#endif // MINIMAL_RSGUI
#include "PeersDialog.h"
#include "connect/ConfCertDialog.h"
@ -51,6 +51,7 @@
#include "LogoBar.h"
#include "util/Widget.h"
#include "settings/rsharesettings.h"
#include "common/RSTreeWidgetItem.h"
#include "RetroShareLink.h"
@ -87,36 +88,6 @@
MessengerWindow* MessengerWindow::_instance = NULL;
static std::set<std::string> *expandedPeers = NULL;
// quick and dirty for sorting, better use QTreeView and QSortFilterProxyModel
class MyMessengerTreeWidgetItem : public QTreeWidgetItem
{
public:
MyMessengerTreeWidgetItem(QTreeWidget *pWidget, int type) : QTreeWidgetItem(type)
{
m_pWidget = pWidget; // can't access the member "view"
}
bool operator<(const QTreeWidgetItem &other) const
{
int column = m_pWidget ? m_pWidget->sortColumn() : 0;
switch (column) {
case COLUMN_NAME:
{
const QVariant v1 = data(column, ROLE_SORT);
const QVariant v2 = other.data(column, ROLE_SORT);
return (v1.toString().compare (v2.toString(), Qt::CaseInsensitive) < 0);
}
}
// let the standard do the sort
return QTreeWidgetItem::operator<(other);
}
private:
QTreeWidget *m_pWidget; // the member "view" is private
};
/*static*/ void MessengerWindow::showYourself ()
{
if (_instance == NULL) {
@ -156,6 +127,9 @@ MessengerWindow::MessengerWindow(QWidget* parent, Qt::WFlags flags)
setAttribute (Qt::WA_QuitOnClose, true);
#endif // MINIMAL_RSGUI
m_compareRole = new RSTreeWidgetItemCompareRole;
m_compareRole->addRole(COLUMN_NAME, ROLE_SORT);
connect( ui.messengertreeWidget, SIGNAL( customContextMenuRequested( QPoint ) ), this, SLOT( messengertreeWidgetCostumPopupMenu( QPoint ) ) );
#ifndef MINIMAL_RSGUI
connect( ui.messengertreeWidget, SIGNAL(itemDoubleClicked ( QTreeWidgetItem *, int)), this, SLOT(chatfriend(QTreeWidgetItem *)));
@ -265,6 +239,8 @@ MessengerWindow::~MessengerWindow ()
}
#endif // MINIMAL_RSGUI
delete(m_compareRole);
_instance = NULL;
}
@ -521,7 +497,7 @@ void MessengerWindow::insertPeers()
}
if (gpg_item == NULL) {
gpg_item = new MyMessengerTreeWidgetItem(peertreeWidget, 0); //set type to 0 for custom popup menu
gpg_item = new RSTreeWidgetItem(m_compareRole, 0); //set type to 0 for custom popup menu
gpg_item->setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicatorWhenChildless);
}
@ -579,7 +555,7 @@ void MessengerWindow::insertPeers()
}
if (sslItem == NULL) {
sslItem = new MyMessengerTreeWidgetItem(peertreeWidget, 1); //set type to 1 for custom popup menu
sslItem = new RSTreeWidgetItem(m_compareRole, 1); //set type to 1 for custom popup menu
}
/* not displayed, used to find back the item */

View File

@ -29,6 +29,7 @@
class LogoBar;
class PeersDialog;
class PopupChatDialog;
class RSTreeWidgetItemCompareRole;
class MessengerWindow : public RWindow
{
@ -131,6 +132,8 @@ private:
QFont itemFont;
QString m_nickName;
RSTreeWidgetItemCompareRole *m_compareRole;
/** Qt Designer generated object */
Ui::MessengerWindow ui;
};

View File

@ -31,6 +31,7 @@
#include <retroshare/rsdisc.h>
#include "common/vmessagebox.h"
#include "common/RSTreeWidgetItem.h"
#include "NetworkDialog.h"
#include "TrustView.h"
#include "GenCertDialog.h"
@ -410,7 +411,7 @@ void NetworkDialog::insertConnect()
#ifdef NET_DEBUG
std::cerr << "NetworkDialog::insertConnect() creating new tree widget item : " << *it << std::endl;
#endif
item = new QTreeWidgetItem(0);
item = new RSTreeWidgetItem(NULL, 0);
item -> setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicatorWhenChildless);
item -> setSizeHint(0, QSize( 18,18 ) );
@ -503,7 +504,7 @@ void NetworkDialog::insertConnect()
if (list.size() == 1) {
self_item = list.front();
} else {
self_item = new QTreeWidgetItem(0);
self_item = new RSTreeWidgetItem(NULL, 0);
self_item->setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicatorWhenChildless);
}
self_item -> setText(0, "0");

View File

@ -50,11 +50,12 @@
#include "profile/ProfileWidget.h"
#include "profile/StatusMessage.h"
#include "gui/connect/ConnectFriendWizard.h"
#include "gui/forums/CreateForum.h"
#include "gui/channels/CreateChannel.h"
#include "gui/feeds/AttachFileItem.h"
#include "gui/im_history/ImHistoryBrowser.h"
#include "connect/ConnectFriendWizard.h"
#include "forums/CreateForum.h"
#include "channels/CreateChannel.h"
#include "feeds/AttachFileItem.h"
#include "im_history/ImHistoryBrowser.h"
#include "common/RSTreeWidgetItem.h"
#include "RetroShareLink.h"
@ -94,43 +95,6 @@
* #define PEERS_DEBUG 1
*****/
// quick and dirty for sorting, better use QTreeView and QSortFilterProxyModel
class MyPeerTreeWidgetItem : public QTreeWidgetItem
{
public:
MyPeerTreeWidgetItem(QTreeWidget *pWidget, int type) : QTreeWidgetItem(type)
{
m_pWidget = pWidget; // can't access the member "view"
}
bool operator<(const QTreeWidgetItem &other) const
{
int role = Qt::DisplayRole;
int column = m_pWidget ? m_pWidget->sortColumn() : 0;
switch (column) {
case COLUMN_STATE:
// sort by state set in user role
role = ROLE_SORT;
// no break;
case COLUMN_NAME:
{
const QVariant v1 = data(column, role);
const QVariant v2 = other.data(column, role);
return (v1.toString().compare (v2.toString(), Qt::CaseInsensitive) < 0);
}
}
// let the standard do the sort
return QTreeWidgetItem::operator<(other);
}
private:
QTreeWidget *m_pWidget; // the member "view" is private
};
/** Constructor */
PeersDialog::PeersDialog(QWidget *parent)
@ -141,6 +105,9 @@ PeersDialog::PeersDialog(QWidget *parent)
last_status_send_time = 0 ;
m_compareRole = new RSTreeWidgetItemCompareRole;
m_compareRole->addRole(COLUMN_STATE, ROLE_SORT);
connect( ui.peertreeWidget, SIGNAL( customContextMenuRequested( QPoint ) ), this, SLOT( peertreeWidgetCostumPopupMenu( QPoint ) ) );
connect( ui.peertreeWidget, SIGNAL( itemDoubleClicked ( QTreeWidgetItem *, int)), this, SLOT(chatfriend(QTreeWidgetItem *)));
@ -270,6 +237,8 @@ PeersDialog::~PeersDialog ()
{
// save settings
processSettings(false);
delete(m_compareRole);
}
void PeersDialog::processSettings(bool bLoad)
@ -564,7 +533,7 @@ void PeersDialog::insertPeers()
}
if (gpg_item == NULL) {
gpg_item = new MyPeerTreeWidgetItem(peertreeWidget, 0); //set type to 0 for custom popup menu
gpg_item = new RSTreeWidgetItem(m_compareRole, 0); //set type to 0 for custom popup menu
/* Add gpg item to the list. Add here, because for setHidden the item must be added */
peertreeWidget->addTopLevelItem(gpg_item);
@ -628,7 +597,7 @@ void PeersDialog::insertPeers()
}
if (newChild) {
sslItem = new MyPeerTreeWidgetItem(peertreeWidget, 1); //set type to 1 for custom popup menu
sslItem = new RSTreeWidgetItem(m_compareRole, 1); //set type to 1 for custom popup menu
#ifdef PEERS_DEBUG
std::cerr << "PeersDialog::insertPeers() inserting sslItem." << std::endl;

View File

@ -49,6 +49,7 @@ class QTextEdit;
class QTextCharFormat;
class ChatDialog;
class AttachFileItem;
class RSTreeWidgetItemCompareRole;
class PeersDialog : public RsAutoUpdatePage
{
@ -173,6 +174,8 @@ private:
class QWidgetAction *widgetAction;
class QSpacerItem *spacerItem;
RSTreeWidgetItemCompareRole *m_compareRole;
///play the sound when recv a message
void playsound();

View File

@ -228,7 +228,7 @@ QString RemoteDirModel::getAgeIndicatorString(const DirDetails &details) const
FileInfo finfo;
rsFiles->FileDetails(details.hash, 0, finfo);
return QString::fromStdString(finfo.path) ;
return QString::fromUtf8(finfo.path.c_str()) ;
} /* end of FileNameRole */
if (role == Qt::TextColorRole)
@ -310,7 +310,7 @@ QString RemoteDirModel::getAgeIndicatorString(const DirDetails &details) const
switch(coln)
{
case 0:
QString ext = QFileInfo(QString::fromStdString(details.name)).suffix();
QString ext = QFileInfo(QString::fromUtf8(details.name.c_str())).suffix();
if (ext == "jpg" || ext == "jpeg" || ext == "png" || ext == "gif"
|| ext == "bmp" || ext == "ico" || ext == "svg")
{
@ -412,7 +412,7 @@ QString RemoteDirModel::getAgeIndicatorString(const DirDetails &details) const
switch(coln)
{
case 0:
return QString::fromStdString(details.name);
return QString::fromUtf8(details.name.c_str());
break;
case 1:
return QString() ;
@ -490,9 +490,71 @@ QString RemoteDirModel::getAgeIndicatorString(const DirDetails &details) const
}
}
} /* end of DisplayRole */
if (role == SortRole)
{
/*
* Person: name, id, 0, 0;
* File : name, size, rank, (0) ts
* Dir : name, (0) count, (0) path, (0) ts
*/
if (details.type == DIR_TYPE_PERSON) /* Person */
{
switch(coln)
{
case 0:
return QString::fromUtf8(details.name.c_str());
case 1:
return QString();
default:
return QString();
}
}
else if (details.type == DIR_TYPE_FILE) /* File */
{
switch(coln)
{
case 0:
return QString::fromUtf8(details.name.c_str());
case 1:
return details.count;
case 2:
return details.age;
case 3:
return getFlagsString(details.flags);
case 4:
{
QString ind("");
if (ageIndicator != IND_ALWAYS)
ind = getAgeIndicatorString(details);
return ind;
}
default:
return QString(tr("FILE"));
}
}
else if (details.type == DIR_TYPE_DIR) /* Dir */
{
switch(coln)
{
case 0:
return QString::fromUtf8(details.name.c_str());
case 1:
return details.count;
case 2:
return details.min_age;
case 3:
return getFlagsString(details.flags);
default:
return QString(tr("DIR"));
}
}
} /* end of SortRole */
return QVariant();
}
void RemoteDirModel::getAgeIndicatorRec(DirDetails &details, QString &ret) const {
@ -610,7 +672,7 @@ QModelIndex RemoteDirModel::index(int row, int column, const QModelIndex & paren
std::list<DirStub>::iterator it;
int i = 0;
for(it = details.children.begin(); ((i < row) && (it != details.children.end())); ++it,++i) ;
if (it == details.children.end())
{
#ifdef RDM_DEBUG

View File

@ -39,7 +39,7 @@ class RemoteDirModel : public QAbstractItemModel
Q_OBJECT
public:
enum Roles{ FileNameRole = Qt::UserRole+1 };
enum Roles{ FileNameRole = Qt::UserRole+1, SortRole = Qt::UserRole+2 };
RemoteDirModel(bool mode, QObject *parent = 0);

View File

@ -25,6 +25,7 @@
#include <QMenu>
#include <QMovie>
#include <QProcess>
#include <QSortFilterProxyModel>
#include "SharedFilesDialog.h"
#include "settings/AddFileAssociationDialog.h"
@ -54,6 +55,30 @@
const QString Image_AddNewAssotiationForFile = ":/images/kcmsystem24.png";
class SFDSortFilterProxyModel : public QSortFilterProxyModel
{
public:
SFDSortFilterProxyModel(RemoteDirModel *dirModel, QObject *parent) : QSortFilterProxyModel(parent)
{
m_dirModel = dirModel;
};
protected:
virtual bool lessThan(const QModelIndex &left, const QModelIndex &right) const
{
bool dirLeft = m_dirModel->isDir(left);
bool dirRight = m_dirModel->isDir(right);
if (dirLeft ^ dirRight) {
return dirLeft;
}
return QSortFilterProxyModel::lessThan(left, right);
}
private:
RemoteDirModel *m_dirModel;
};
/** Constructor */
SharedFilesDialog::SharedFilesDialog(QWidget *parent)
@ -91,9 +116,26 @@ SharedFilesDialog::SharedFilesDialog(QWidget *parent)
model = new RemoteDirModel(true);
proxyModel = new SFDSortFilterProxyModel(model, this);
proxyModel->setDynamicSortFilter(true);
proxyModel->setSourceModel(model);
proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
proxyModel->setSortRole(RemoteDirModel::SortRole);
proxyModel->sort(0);
ui.remoteDirTreeView->setModel(proxyModel);
localModel = new RemoteDirModel(false);
ui.remoteDirTreeView->setModel(model);
ui.localDirTreeView->setModel(localModel);
localProxyModel = new SFDSortFilterProxyModel(localModel, this);
localProxyModel->setDynamicSortFilter(true);
localProxyModel->setSourceModel(localModel);
localProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
localProxyModel->setSortRole(RemoteDirModel::SortRole);
localProxyModel->sort(0);
ui.localDirTreeView->setModel(localProxyModel);
ui.remoteDirTreeView->setColumnHidden(3,true) ;
ui.remoteDirTreeView->setColumnHidden(4,true) ;
@ -150,6 +192,8 @@ SharedFilesDialog::SharedFilesDialog(QWidget *parent)
ui.remoteDirTreeView->setSelectionMode(QAbstractItemView::ExtendedSelection);
ui.localDirTreeView->setSelectionMode(QAbstractItemView::ExtendedSelection);
// load settings
processSettings(true);
/* Hide platform specific features */
#ifdef Q_WS_WIN
@ -171,8 +215,39 @@ SharedFilesDialog::SharedFilesDialog(QWidget *parent)
connect(openfileAct, SIGNAL(triggered()), this, SLOT(openfile()));
openfolderAct = new QAction(QIcon(IMAGE_OPENFOLDER), tr("Open Folder"), this);
connect(openfolderAct, SIGNAL(triggered()), this, SLOT(openfolder()));
}
SharedFilesDialog::~SharedFilesDialog()
{
// save settings
processSettings(false);
}
void SharedFilesDialog::processSettings(bool bLoad)
{
Settings->beginGroup("SharedFilesDialog");
if (bLoad) {
// load settings
// state of the trees
ui.localDirTreeView->header()->restoreState(Settings->value("LocalDirTreeView").toByteArray());
ui.remoteDirTreeView->header()->restoreState(Settings->value("RemoteDirTreeView").toByteArray());
// state of splitter
ui.splitter->restoreState(Settings->value("Splitter").toByteArray());
} else {
// save settings
// state of trees
Settings->setValue("LocalDirTreeView", ui.localDirTreeView->header()->saveState());
Settings->setValue("RemoteDirTreeView", ui.remoteDirTreeView->header()->saveState());
// state of splitter
Settings->setValue("Splitter", ui.splitter->saveState());
}
Settings->endGroup();
}
void SharedFilesDialog::checkUpdate()
@ -231,6 +306,27 @@ void SharedFilesDialog::shareddirtreeviewCostumPopupMenu( QPoint point )
contextMnu.exec(QCursor::pos());
}
QModelIndexList SharedFilesDialog::getLocalSelected()
{
QModelIndexList list = ui.localDirTreeView->selectionModel()->selectedIndexes();
QModelIndexList proxyList;
for (QModelIndexList::iterator index = list.begin(); index != list.end(); index++) {
proxyList.append(localProxyModel->mapToSource(*index));
}
return proxyList;
}
QModelIndexList SharedFilesDialog::getRemoteSelected()
{
QModelIndexList list = ui.remoteDirTreeView->selectionModel()->selectedIndexes();
QModelIndexList proxyList;
for (QModelIndexList::iterator index = list.begin(); index != list.end(); index++) {
proxyList.append(proxyModel->mapToSource(*index));
}
return proxyList;
}
void SharedFilesDialog::downloadRemoteSelected()
{
@ -239,8 +335,8 @@ void SharedFilesDialog::downloadRemoteSelected()
std::cerr << "Downloading Files";
std::cerr << std::endl;
QItemSelectionModel *qism = ui.remoteDirTreeView->selectionModel();
model -> downloadSelected(qism->selectedIndexes());
QModelIndexList lst = getRemoteSelected();
model -> downloadSelected(lst);
}
void SharedFilesDialog::copyLink (const QModelIndexList& lst, bool remote)
@ -294,13 +390,13 @@ void SharedFilesDialog::copyLink (const QModelIndexList& lst, bool remote)
void SharedFilesDialog::copyLinkRemote()
{
QModelIndexList lst = ui.remoteDirTreeView->selectionModel ()->selectedIndexes ();
QModelIndexList lst = getRemoteSelected();
copyLink (lst, true);
}
void SharedFilesDialog::copyLinkLocal()
{
QModelIndexList lst = ui.localDirTreeView->selectionModel ()->selectedIndexes ();
QModelIndexList lst = getLocalSelected();
copyLink (lst, false);
}
@ -422,10 +518,8 @@ void SharedFilesDialog::playselectedfiles()
std::cerr << "SharedFilesDialog::playselectedfiles()";
std::cerr << std::endl;
QItemSelectionModel *qism = ui.localDirTreeView->selectionModel();
std::list<std::string> paths;
localModel -> getFilePaths(qism->selectedIndexes(), paths);
localModel -> getFilePaths(getLocalSelected(), paths);
std::list<std::string>::iterator it;
QStringList fullpaths;
@ -456,8 +550,7 @@ void SharedFilesDialog::playselectedfiles()
// std::cerr << "Recommending Files";
// std::cerr << std::endl;
//
// QItemSelectionModel *qism = ui.remoteDirTreeView->selectionModel();
// model -> recommendSelected(qism->selectedIndexes());
// model -> recommendSelected(getRemoteSelected());
//}
@ -468,8 +561,7 @@ void SharedFilesDialog::playselectedfiles()
// std::cerr << "Recommending Files";
// std::cerr << std::endl;
//
// QItemSelectionModel *qism = ui.localDirTreeView->selectionModel();
// localModel -> recommendSelected(qism->selectedIndexes());
// localModel -> recommendSelected(getLocalSelected());
//}
@ -484,8 +576,7 @@ void SharedFilesDialog::playselectedfiles()
//
// /* clear current recommend Selection done by model */
//
// QItemSelectionModel *qism = ui.localDirTreeView->selectionModel();
// localModel -> recommendSelectedOnly(qism->selectedIndexes());
// localModel -> recommendSelectedOnly(getLocalSelected());
//}
@ -497,7 +588,7 @@ void SharedFilesDialog::recommendFilesTo( std::string rsid )
std::list<DirDetails> files_info ;
localModel->getFileInfoFromIndexList(ui.localDirTreeView->selectionModel()->selectedIndexes(),files_info);
localModel->getFileInfoFromIndexList(getLocalSelected(),files_info);
if(files_info.empty())
return ;
@ -526,7 +617,7 @@ void SharedFilesDialog::recommendFilesToMsg( std::string rsid )
{
std::list<DirDetails> files_info ;
localModel->getFileInfoFromIndexList(ui.localDirTreeView->selectionModel()->selectedIndexes(),files_info);
localModel->getFileInfoFromIndexList(getLocalSelected(),files_info);
if(files_info.empty())
return ;
@ -556,7 +647,7 @@ void SharedFilesDialog::openfile()
std::cerr << "SharedFilesDialog::openfile" << std::endl;
QModelIndexList qmil = ui.localDirTreeView->selectionModel()->selectedIndexes();
QModelIndexList qmil = getLocalSelected();
localModel->openSelected(qmil, false);
}
@ -565,7 +656,7 @@ void SharedFilesDialog::openfolder()
{
std::cerr << "SharedFilesDialog::openfolder" << std::endl;
QModelIndexList qmil = ui.localDirTreeView->selectionModel()->selectedIndexes();
QModelIndexList qmil = getLocalSelected();
localModel->openSelected(qmil, true);
}

View File

@ -26,6 +26,7 @@
#include "ui_SharedFilesDialog.h"
class RemoteDirModel;
class QSortFilterProxyModel;
class SharedFilesDialog : public RsAutoUpdatePage
{
@ -35,6 +36,7 @@ public:
/** Default Constructor */
SharedFilesDialog(QWidget *parent = 0);
/** Default Destructor */
~SharedFilesDialog();
virtual void updatePage() { checkUpdate() ; }
@ -94,8 +96,13 @@ private:
//QMenu* contextMnu2;
void processSettings(bool bLoad);
void copyLink (const QModelIndexList& lst, bool remote);
QModelIndexList getRemoteSelected();
QModelIndexList getLocalSelected();
/** Defines the actions for the context menu for QTreeWidget */
QAction* openfileAct;
QAction* openfolderAct;
@ -116,7 +123,9 @@ private:
/* RemoteDirModel */
RemoteDirModel *model;
QSortFilterProxyModel *proxyModel;
RemoteDirModel *localModel;
QSortFilterProxyModel *localProxyModel;
QString currentCommand;
QString currentFile;

View File

@ -0,0 +1,129 @@
/****************************************************************
* 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 "RSTreeWidgetItem.h"
RSTreeWidgetItemCompareRole::RSTreeWidgetItemCompareRole()
{
}
void RSTreeWidgetItemCompareRole::addRole(int column, int role)
{
insert(column, role);
}
const int RSTreeWidgetItemCompareRole::findRole(const int column) const
{
RSTreeWidgetItemCompareRole::const_iterator it = find(column);
if (it == end()) {
return Qt::DisplayRole;
}
return it.value();
}
RSTreeWidgetItem::RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, int type) : QTreeWidgetItem(type)
{
m_compareRole = compareRole;
}
RSTreeWidgetItem::RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, const QStringList &strings, int type) : QTreeWidgetItem(strings, type)
{
m_compareRole = compareRole;
}
RSTreeWidgetItem::RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidget *view, const QStringList &strings, int type) : QTreeWidgetItem(view, strings, type)
{
m_compareRole = compareRole;
}
RSTreeWidgetItem::RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidget *view, QTreeWidgetItem *after, int type) : QTreeWidgetItem(view, after, type)
{
m_compareRole = compareRole;
}
RSTreeWidgetItem::RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidgetItem *parent, const QStringList &strings, int type) : QTreeWidgetItem(parent, strings, type)
{
m_compareRole = compareRole;
}
RSTreeWidgetItem::RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidgetItem *parent, QTreeWidgetItem *after, int type) : QTreeWidgetItem(parent, after, type)
{
m_compareRole = compareRole;
}
RSTreeWidgetItem::RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, const QTreeWidgetItem &other) : QTreeWidgetItem(other)
{
m_compareRole = compareRole;
}
static uint typeOfVariant(const QVariant &value)
{
//return 0 for integer, 1 for floating point and 2 for other
switch (value.userType()) {
case QVariant::Bool:
case QVariant::Int:
case QVariant::UInt:
case QVariant::LongLong:
case QVariant::ULongLong:
case QVariant::Char:
case QMetaType::Short:
case QMetaType::UShort:
case QMetaType::UChar:
case QMetaType::ULong:
case QMetaType::Long:
return 0;
case QVariant::Double:
case QMetaType::Float:
return 1;
default:
return 2;
}
}
bool RSTreeWidgetItem::operator<(const QTreeWidgetItem &other) const
{
int column = treeWidget()->sortColumn();
int role = Qt::DisplayRole;
/* Own role for sort defined ? */
if (m_compareRole) {
role = m_compareRole->findRole(column);
}
// taken from "bool QTreeWidgetItem::operator<(const QTreeWidgetItem &other) const"
const QVariant v1 = data(column, role);
const QVariant v2 = other.data(column, role);
// taken from "bool QAbstractItemModelPrivate::variantLessThan(const QVariant &v1, const QVariant &v2)"
switch(qMax(typeOfVariant(v1), typeOfVariant(v2)))
{
case 0: //integer type
return v1.toLongLong() < v2.toLongLong();
case 1: //floating point
return v1.toReal() < v2.toReal();
default:
return (v1.toString().compare (v2.toString(), Qt::CaseInsensitive) < 0);
}
// let the standard do the sort, this code should not reached
return QTreeWidgetItem::operator<(other);
}

View File

@ -0,0 +1,56 @@
/****************************************************************
* 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 _RSTREEWIDGETITEM_H
#define _RSTREEWIDGETITEM_H
#include <QTreeWidgetItem>
/* For definition of the UserRole for comparing */
class RSTreeWidgetItemCompareRole : QMap<int, int>
{
public:
RSTreeWidgetItemCompareRole();
void addRole(int column, int role);
const int findRole(const int column) const;
};
class RSTreeWidgetItem : public QTreeWidgetItem
{
public:
RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole = NULL, int type = Type);
RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, const QStringList &strings, int type = Type);
RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidget *view, const QStringList &strings, int type = Type);
RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidget *view, QTreeWidgetItem *after, int type = Type);
RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidgetItem *parent, const QStringList &strings, int type = Type);
RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, QTreeWidgetItem *parent, QTreeWidgetItem *after, int type = Type);
RSTreeWidgetItem(const RSTreeWidgetItemCompareRole *compareRole, const QTreeWidgetItem &other);
bool operator<(const QTreeWidgetItem &other) const;
private:
const RSTreeWidgetItemCompareRole *m_compareRole;
};
#endif