2016-10-31 11:28:26 -04:00
|
|
|
#include <QLayout>
|
|
|
|
#include <QDialogButtonBox>
|
2012-11-02 13:26:20 -04:00
|
|
|
#include <retroshare/rspeers.h>
|
|
|
|
#include "GroupSelectionBox.h"
|
2012-11-08 19:46:41 -05:00
|
|
|
#include "GroupDefs.h"
|
|
|
|
#include "gui/notifyqt.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#define ROLE_ID Qt::UserRole
|
2012-11-02 13:26:20 -04:00
|
|
|
|
|
|
|
GroupSelectionBox::GroupSelectionBox(QWidget *parent)
|
|
|
|
: QListWidget(parent)
|
|
|
|
{
|
2012-11-08 19:46:41 -05:00
|
|
|
setSelectionMode(QAbstractItemView::SingleSelection);
|
|
|
|
|
|
|
|
connect(NotifyQt::getInstance(), SIGNAL(groupsChanged(int)), this, SLOT(fillGroups()));
|
2012-11-02 13:26:20 -04:00
|
|
|
|
|
|
|
// Fill with available groups
|
2012-11-08 19:46:41 -05:00
|
|
|
fillGroups();
|
|
|
|
}
|
|
|
|
void GroupSelectionBox::fillGroups()
|
|
|
|
{
|
2016-07-05 23:49:43 -04:00
|
|
|
std::list<RsNodeGroupId> selectedIds;
|
2012-11-08 19:46:41 -05:00
|
|
|
selectedGroupIds(selectedIds);
|
|
|
|
|
|
|
|
clear();
|
2012-11-02 13:26:20 -04:00
|
|
|
|
2012-11-08 19:46:41 -05:00
|
|
|
std::list<RsGroupInfo> groupIds;
|
|
|
|
rsPeers->getGroupInfoList(groupIds);
|
2012-11-03 10:15:03 -04:00
|
|
|
|
2012-11-08 19:46:41 -05:00
|
|
|
for (std::list<RsGroupInfo>::const_iterator it(groupIds.begin()); it != groupIds.end(); ++it) {
|
|
|
|
QListWidgetItem *item = new QListWidgetItem(GroupDefs::name(*it));
|
2016-07-05 23:49:43 -04:00
|
|
|
item->setData(ROLE_ID, QString::fromStdString(it->id.toStdString()));
|
2012-11-08 19:46:41 -05:00
|
|
|
item->setBackgroundColor(QColor(183,236,181));
|
|
|
|
addItem(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
setSelectedGroupIds(selectedIds);
|
2012-11-02 13:26:20 -04:00
|
|
|
}
|
|
|
|
|
2016-07-05 23:49:43 -04:00
|
|
|
void GroupSelectionBox::selectedGroupIds(std::list<RsNodeGroupId> &groupIds) const
|
2012-11-02 13:26:20 -04:00
|
|
|
{
|
2012-11-08 19:46:41 -05:00
|
|
|
int itemCount = count();
|
2012-11-02 13:26:20 -04:00
|
|
|
|
2012-11-08 19:46:41 -05:00
|
|
|
for (int i = 0; i < itemCount; ++i) {
|
|
|
|
QListWidgetItem *listItem = item(i);
|
|
|
|
if (listItem->checkState() == Qt::Checked) {
|
2016-07-05 23:49:43 -04:00
|
|
|
groupIds.push_back(RsNodeGroupId(item(i)->data(ROLE_ID).toString().toStdString()));
|
2015-12-02 09:18:17 -05:00
|
|
|
std::cerr << "Adding selected item " << groupIds.back() << std::endl;
|
2012-11-08 19:46:41 -05:00
|
|
|
}
|
2012-11-03 11:25:25 -04:00
|
|
|
}
|
2012-11-08 19:46:41 -05:00
|
|
|
}
|
2012-11-02 13:26:20 -04:00
|
|
|
|
2016-07-05 23:49:43 -04:00
|
|
|
void GroupSelectionBox::setSelectedGroupIds(const std::list<RsNodeGroupId>& groupIds)
|
2012-11-08 19:46:41 -05:00
|
|
|
{
|
|
|
|
int itemCount = count();
|
|
|
|
|
|
|
|
for (int i = 0; i < itemCount; ++i) {
|
|
|
|
QListWidgetItem *listItem = item(i);
|
|
|
|
|
2016-07-05 23:49:43 -04:00
|
|
|
if (std::find(groupIds.begin(), groupIds.end(), RsNodeGroupId(listItem->data(ROLE_ID).toString().toStdString())) != groupIds.end()) {
|
2012-11-08 19:46:41 -05:00
|
|
|
listItem->setCheckState(Qt::Checked);
|
|
|
|
} else {
|
|
|
|
listItem->setCheckState(Qt::Unchecked);
|
|
|
|
}
|
|
|
|
}
|
2012-11-02 13:26:20 -04:00
|
|
|
}
|
|
|
|
|
2012-11-08 19:46:41 -05:00
|
|
|
void GroupSelectionBox::selectedGroupNames(QList<QString> &groupNames) const
|
2012-11-02 13:26:20 -04:00
|
|
|
{
|
2012-11-08 19:46:41 -05:00
|
|
|
int itemCount = count();
|
2012-11-02 13:26:20 -04:00
|
|
|
|
2012-11-08 19:46:41 -05:00
|
|
|
for (int i = 0; i < itemCount; ++i) {
|
|
|
|
QListWidgetItem *listItem = item(i);
|
|
|
|
if (listItem->checkState() == Qt::Checked) {
|
|
|
|
groupNames.push_back(item(i)->text());
|
2015-12-02 09:18:17 -05:00
|
|
|
std::cerr << "Adding selected item " << groupNames.back().toUtf8().constData() << std::endl;
|
2012-11-08 19:46:41 -05:00
|
|
|
}
|
2012-11-02 13:26:20 -04:00
|
|
|
}
|
|
|
|
}
|
2016-10-31 11:28:26 -04:00
|
|
|
|
|
|
|
std::list<RsNodeGroupId> GroupSelectionDialog::selectGroups(const std::list<RsNodeGroupId>& default_groups)
|
|
|
|
{
|
|
|
|
GroupSelectionDialog gsd(NULL) ;
|
|
|
|
|
|
|
|
gsd.mBox->setSelectedGroupIds(default_groups) ;
|
|
|
|
|
|
|
|
gsd.exec();
|
|
|
|
|
|
|
|
std::list<RsNodeGroupId> selected_groups ;
|
|
|
|
gsd.mBox->selectedGroupIds(selected_groups);
|
|
|
|
|
|
|
|
return selected_groups ;
|
|
|
|
}
|
|
|
|
|
|
|
|
GroupSelectionDialog::~GroupSelectionDialog()
|
|
|
|
{
|
|
|
|
delete mBox ;
|
|
|
|
}
|
2016-10-22 17:48:19 -04:00
|
|
|
GroupSelectionDialog::GroupSelectionDialog(QWidget* /*parent*/)
|
2016-10-31 11:28:26 -04:00
|
|
|
{
|
|
|
|
mBox = new GroupSelectionBox(this) ;
|
|
|
|
|
|
|
|
QLayout *l = new QVBoxLayout ;
|
|
|
|
setLayout(l) ;
|
|
|
|
|
|
|
|
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
|
|
|
|
|
|
|
|
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
|
|
|
|
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
|
|
|
|
|
|
|
|
l->addWidget(mBox) ;
|
|
|
|
l->addWidget(buttonBox) ;
|
|
|
|
l->update() ;
|
|
|
|
}
|
|
|
|
|