230 lines
6.8 KiB
C++
Executable File
230 lines
6.8 KiB
C++
Executable File
/**
|
|
* @RDE_HEADER@
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
* Created by Philipp Last, SLSE, 2017-11-10
|
|
*/
|
|
|
|
#include "participant_tree_edit_handler.h"
|
|
#include "participant_edit_model.h"
|
|
|
|
#include <QAction>
|
|
#include <QMenu>
|
|
#include <QTreeView>
|
|
|
|
#include <QDebug>
|
|
|
|
namespace ParticipantManagement
|
|
{
|
|
ParticipantTreeEditHandler::ParticipantTreeEditHandler(LtsDataView::LtsDataView *dataView,
|
|
const std::shared_ptr<ParticipantManagement::Interface::Dtc> &dtc,
|
|
const std::shared_ptr<ParticipantManagement::Interface::Ftc> &ftc,
|
|
bool asSupportService,
|
|
QObject *parent)
|
|
: QObject(parent)
|
|
, m_dataView(dataView)
|
|
, m_model(qobject_cast<ParticipantEditModel *>(m_dataView->sourceModel()))
|
|
, m_asSupportService(asSupportService)
|
|
, m_editParticipantsAllowed(false)
|
|
, m_dtc(dtc)
|
|
, m_ftc(ftc)
|
|
, m_requestHelper(nullptr)
|
|
{
|
|
Q_ASSERT(m_model);
|
|
m_dataView->setRootIsDecorated(true);
|
|
auto treeView = m_dataView->treeView();
|
|
connect(treeView, &QTreeView::customContextMenuRequested,
|
|
this, &ParticipantTreeEditHandler::showContextMenu);
|
|
|
|
QItemSelectionModel *selectionModel = treeView->selectionModel();
|
|
connect(selectionModel, &QItemSelectionModel::selectionChanged,
|
|
this, &ParticipantTreeEditHandler::signalSelectionChanged);
|
|
selectionModel->clearSelection();
|
|
|
|
connect(treeView, &QTreeView::doubleClicked,
|
|
this, &ParticipantTreeEditHandler::signalDoubleClicked);
|
|
}
|
|
|
|
//private
|
|
QModelIndex ParticipantTreeEditHandler::selectedIndexView() const
|
|
{
|
|
const QModelIndexList indexL = m_dataView->selectedRows();
|
|
return indexL.isEmpty() ? QModelIndex() : indexL.at(0);
|
|
}
|
|
|
|
bool ParticipantTreeEditHandler::isContainer() const
|
|
{
|
|
const QModelIndex idx = selectedIndexView();
|
|
if (!idx.isValid())
|
|
return false;
|
|
const int typeId = m_model->privateData(idx)->participant.typeId();
|
|
if (typeId <= 0)
|
|
return false;
|
|
|
|
if (m_asSupportService)
|
|
return m_dtc->type(typeId).typeClass() == ParticipantManagement::TypeClass_def::SupportUnit;
|
|
|
|
return m_dtc->type(typeId).typeClass() == ParticipantManagement::TypeClass_def::Unit;
|
|
}
|
|
|
|
QVector<int> ParticipantTreeEditHandler::usedEquipmentNumbers() const
|
|
{
|
|
return m_model->usedEquipmentNumbers();
|
|
}
|
|
|
|
bool ParticipantTreeEditHandler::isSelectedIndexValid() const
|
|
{
|
|
return selectedIndexView().isValid();
|
|
}
|
|
|
|
|
|
// is selected Index is equiped, it can't be removed
|
|
bool ParticipantTreeEditHandler::isSelectedIndexRemovable() const
|
|
{
|
|
const QModelIndex idx = selectedIndexView();
|
|
if (!idx.isValid() || !idx.parent().isValid())
|
|
return false;
|
|
return m_model->isRowRemovable(m_model->privateData(idx));
|
|
}
|
|
|
|
ParticipantManagement::ParticipantData ParticipantTreeEditHandler::getSelectedData() const
|
|
{
|
|
const QModelIndex idx = selectedIndexView();
|
|
return idx.isValid() ? m_model->privateData(idx)->participant : ParticipantData();
|
|
}
|
|
|
|
|
|
void ParticipantTreeEditHandler::removeCurrentRow()
|
|
{
|
|
const QModelIndex idx = selectedIndexView();
|
|
if (!idx.isValid())
|
|
return;
|
|
m_model->onDeleteRow(idx, true);
|
|
}
|
|
|
|
|
|
void ParticipantTreeEditHandler::onRemoveItem()
|
|
{
|
|
removeCurrentRow();
|
|
}
|
|
|
|
|
|
void ParticipantTreeEditHandler::beautifyTreeView(bool keepExpanded)
|
|
{
|
|
m_dataView->expandAll();
|
|
m_dataView->resizeHeadersByContent();
|
|
/*
|
|
m_dataView->resizeSection(0, m_dataView->treeView()->columnWidth(0) +
|
|
m_dataView->treeView()->iconSize().width() + 10);
|
|
*/
|
|
if (!keepExpanded)
|
|
m_dataView->collapseAll();
|
|
}
|
|
|
|
void ParticipantTreeEditHandler::partyHasChanged(int newPartyId, bool keepExpanded)
|
|
{
|
|
if (m_model->partyId() == newPartyId)
|
|
return;
|
|
|
|
if (m_model->onReinitModel(newPartyId))
|
|
beautifyTreeView(keepExpanded);
|
|
}
|
|
|
|
|
|
bool ParticipantTreeEditHandler::onApplyChanges()
|
|
{
|
|
ModifyParticipantDataRequest req;
|
|
req.data(m_model->participantData());
|
|
if (req.data().size() == 0)
|
|
return true;
|
|
|
|
if (!m_requestHelper)
|
|
m_requestHelper = new LtsUtils::RequestHelper(this);
|
|
m_requestHelper->addRequest([this, req]()
|
|
{
|
|
return m_ftc->modifyParticipantDataRequest(req, this);
|
|
});
|
|
return m_requestHelper->startRequests();
|
|
}
|
|
|
|
/*
|
|
* If 'expand' is true all subnodes of the currently selected node will be expanded.
|
|
* If 'expand' is false all subnodes of the currently selected node will be collapsed.
|
|
*/
|
|
void ParticipantTreeEditHandler::expandNodeChildren(QTreeView *pTreeView, const QModelIndex &index, bool expand)
|
|
{
|
|
if (!index.isValid())
|
|
return;
|
|
|
|
int childCount = index.model()->rowCount(index);
|
|
for (int i = 0; i < childCount; i++) {
|
|
const QModelIndex &child = index.model()->index(i,0, index);
|
|
// Recursively call the function for each child node.
|
|
expandNodeChildren(pTreeView, child, expand);
|
|
}
|
|
|
|
if(expand)
|
|
pTreeView->expand(index);
|
|
else
|
|
pTreeView->collapse(index);
|
|
}
|
|
|
|
|
|
void ParticipantTreeEditHandler::showContextMenu(const QPoint &position)
|
|
{
|
|
QTreeView *tv = qobject_cast<QTreeView*>(sender());
|
|
if (!tv)
|
|
return;
|
|
const QModelIndex curIdx = tv->currentIndex();
|
|
const QModelIndex idx = curIdx.column() == 0 ? curIdx : tv->model()->index(curIdx.row(), 0, curIdx.parent());
|
|
if (!idx.isValid())
|
|
return;
|
|
|
|
QMenu menu;
|
|
if (m_editParticipantsAllowed)
|
|
{
|
|
QAction *editParticipantAction = new QAction(tr("Edit"), &menu);
|
|
connect(editParticipantAction, &QAction::triggered, this, [this]() { emit signalEditParticipant(); });
|
|
menu.addAction(editParticipantAction);
|
|
}
|
|
if (tv->model()->rowCount(idx) != 0)
|
|
{
|
|
if (tv->isExpanded(idx))
|
|
{
|
|
QAction *collapseAction = new QAction(tr("Collapse"), &menu);
|
|
connect(collapseAction, &QAction::triggered, this, [tv, idx]() { tv->collapse(idx);});
|
|
menu.addAction(collapseAction);
|
|
|
|
QAction *collapseAllAction = new QAction(tr("Collapse all"), &menu);
|
|
connect(collapseAllAction, &QAction::triggered, this, [this, tv, idx]() { expandNodeChildren(tv, idx, false); });
|
|
menu.addAction(collapseAllAction);
|
|
}
|
|
else
|
|
{
|
|
QAction *expandAction = new QAction(tr("Expand"), &menu);
|
|
connect(expandAction, &QAction::triggered, this, [tv, idx]() { tv->expand(idx);});
|
|
menu.addAction(expandAction);
|
|
}
|
|
QAction *expandAllAction = new QAction(tr("Expand all"), &menu);
|
|
connect(expandAllAction, &QAction::triggered, this, [this, tv, idx]() { expandNodeChildren(tv, idx, true); });
|
|
menu.addAction(expandAllAction);
|
|
}
|
|
|
|
if (!menu.actions().isEmpty())
|
|
menu.exec(tv->viewport()->mapToGlobal(position));
|
|
}
|
|
|
|
|
|
void ParticipantTreeEditHandler::onModifyParticipantDataReply(const Core::FunctionReplyObject &replyObject)
|
|
{
|
|
if (!m_requestHelper)
|
|
return;
|
|
if (!replyObject.error())
|
|
m_model->dataCommitted();
|
|
emit signalChangesMade();
|
|
m_requestHelper->requestReply(replyObject);
|
|
}
|
|
|
|
}
|