Commit 40e6ce31 authored by Karel Slaný's avatar Karel Slaný Committed by Martin Straka

Added UploadHierarchyListModel::selectedFullNames().

parent 7115a762
......@@ -36,7 +36,7 @@ UploadHierarchyListModel::UploadHierarchyListModel(QObject *parent)
: QAbstractListModel(parent),
m_hierarchy(),
m_workingRoot(Q_NULLPTR),
m_selectedIds()
m_selected()
{
}
......@@ -45,7 +45,7 @@ UploadHierarchyListModel::UploadHierarchyListModel(
: QAbstractListModel(parent),
m_hierarchy(model.m_hierarchy),
m_workingRoot(m_hierarchy.root()),
m_selectedIds(model.m_selectedIds)
m_selected(model.m_selected)
{
}
......@@ -191,10 +191,10 @@ QVariant UploadHierarchyListModel::data(const QModelIndex &index,
return isSelectable(entry);
break;
case ROLE_SELECTED:
return idInSet(entry, m_selectedIds, false);
return idInSelection(entry, m_selected, false);
break;
case ROLE_SELECTED_RECURSIVE:
return idInSet(entry, m_selectedIds, true);
return idInSelection(entry, m_selected, true);
break;
default:
return QVariant();
......@@ -229,7 +229,7 @@ void UploadHierarchyListModel::setHierarchy(const UploadHierarchyResp &uhr)
beginResetModel();
m_hierarchy = uhr;
m_workingRoot = m_hierarchy.root();
m_selectedIds.clear();
m_selected.clear();
endResetModel();
}
......@@ -268,27 +268,48 @@ void UploadHierarchyListModel::navigateSub(int row)
}
}
QString UploadHierarchyListModel::navigatedRootName(bool takeSuper,
const QString &sep) const
/*!
* @brief Return node name.
*
* @param[in] node Node pointer.
* @param[in] takeSuper If true, then full path across all
* superordinate nodes is taken.
* @param[in] sep Separator to be used. It is always appended to
* the end of the returned string.
* @return Name or path of names to the node.
*/
static
QString nodeName(const UploadHierarchyResp::NodeEntry *node, bool takeSuper,
const QString &sep)
{
if ((!m_hierarchy.isValid() || (m_workingRoot == Q_NULLPTR))) {
return sep;
if (Q_UNLIKELY(node == Q_NULLPTR)) {
return QString();
}
QString rootName(m_workingRoot->name() + sep);
QString nodeName(node->name() + sep);
if (takeSuper) {
const UploadHierarchyResp::NodeEntry *node =
m_workingRoot->super();
node = node->super();
while ((node != Q_NULLPTR) && (!node->name().isEmpty())) {
rootName = node->name() + sep + rootName;
nodeName = node->name() + sep + nodeName;
node = node->super();
}
}
if ((!sep.isEmpty()) && (0 != rootName.indexOf(sep))) {
rootName = sep + rootName;
if ((!sep.isEmpty()) && (0 != nodeName.indexOf(sep))) {
nodeName = sep + nodeName;
}
return nodeName;
}
QString UploadHierarchyListModel::navigatedRootName(bool takeSuper,
const QString &sep) const
{
if ((!m_hierarchy.isValid() || (m_workingRoot == Q_NULLPTR))) {
return sep;
}
return rootName;
return nodeName(m_workingRoot, takeSuper, sep);
}
bool UploadHierarchyListModel::toggleNodeSelection(int row)
......@@ -302,10 +323,11 @@ bool UploadHierarchyListModel::toggleNodeSelection(int row)
const UploadHierarchyResp::NodeEntry *sub =
m_workingRoot->sub().at(row);
if ((sub != Q_NULLPTR) && isSelectable(sub)) {
if (idInSet(sub, m_selectedIds, false)) {
m_selectedIds.remove(sub->id());
if (idInSelection(sub, m_selected, false)) {
m_selected.remove(sub->id());
} else {
m_selectedIds.insert(sub->id());
m_selected.insert(sub->id(),
nodeName(sub, true, "/")); /* TODO -- Make the separator configurable. */
}
QModelIndex changedIdx(index(row, 0, QModelIndex()));
QVector<int> roles(2);
......@@ -321,7 +343,14 @@ bool UploadHierarchyListModel::toggleNodeSelection(int row)
QStringList UploadHierarchyListModel::selectedIds(void) const
{
return m_selectedIds.toList();
return m_selected.keys();
}
QStringList UploadHierarchyListModel::selectedFullNames(bool sort) const
{
/* TODO -- Enable sorting. */
return m_selected.values();
}
UploadHierarchyListModel *UploadHierarchyListModel::fromVariant(
......@@ -336,7 +365,8 @@ UploadHierarchyListModel *UploadHierarchyListModel::fromVariant(
bool UploadHierarchyListModel::showRootName(void) const
{
return m_hierarchy.isValid() && (m_workingRoot != Q_NULLPTR) && !m_workingRoot->name().isEmpty();
return m_hierarchy.isValid() && (m_workingRoot != Q_NULLPTR) &&
!m_workingRoot->name().isEmpty();
}
QStringList UploadHierarchyListModel::filterData(
......@@ -379,9 +409,9 @@ QStringList UploadHierarchyListModel::filterDataRecursive(
return res;
}
bool UploadHierarchyListModel::idInSet(
const UploadHierarchyResp::NodeEntry *entry, const QSet<QString> &idSet,
bool takeSub)
bool UploadHierarchyListModel::idInSelection(
const UploadHierarchyResp::NodeEntry *entry,
const QMap<QString, QString> &idMap, bool takeSub)
{
if (Q_UNLIKELY(entry == Q_NULLPTR)) {
Q_ASSERT(0);
......@@ -391,13 +421,13 @@ bool UploadHierarchyListModel::idInSet(
int present = false;
const QString &id(entry->id());
if (!id.isEmpty()) {
present = idSet.contains(id);
present = idMap.contains(id);
}
if (takeSub) {
int i = 0;
while ((!present) && (i < entry->sub().size())) {
present = idInSet(entry->sub().at(i), idSet, true);
present = idInSelection(entry->sub().at(i), idMap, true);
++i;
}
}
......
......@@ -24,7 +24,7 @@
#pragma once
#include <QAbstractListModel>
#include <QSet>
#include <QMap>
#include <QString>
#include "src/datovka_shared/records_management/json/upload_hierarchy.h"
......@@ -178,13 +178,22 @@ public:
bool toggleNodeSelection(int row);
/*!
* @brief return unsorted list of selected node identifiers.
* @brief Return unsorted list of selected node identifiers.
*
* @return List of node identifiers.
*/
Q_INVOKABLE
QStringList selectedIds(void) const;
/*!
* @brief Return list of selected node names.
*
* @param[in] sort Set to true if list should be sorted alpabetically.
* @return List of full names.
*/
Q_INVOKABLE
QStringList selectedFullNames(bool sort) const;
/*!
* @brief Converts QVariant obtained from QML into model pointer.
*
......@@ -233,19 +242,19 @@ private:
* @brief Check whether identifier of node is in set.
*
* @param[in] entry Node entry.
* @param[in] idSet Identifier set to search in.
* @param[in] idMap Identifier map to search in.
* @param[in] takeSub Whether sub-nodes are to be taken into
* consideration.
* @return true if node identifier is in set or any of its sub-nodes if
* takeSub enabled.
*/
static
bool idInSet(const UploadHierarchyResp::NodeEntry *entry,
const QSet<QString> &idSet, bool takeSub);
bool idInSelection(const UploadHierarchyResp::NodeEntry *entry,
const QMap<QString, QString> &idMap, bool takeSub);
UploadHierarchyResp m_hierarchy; /*!< Upload hierarchy structure. */
const UploadHierarchyResp::NodeEntry *m_workingRoot; /*!< Node acting as working root. */
QSet<QString> m_selectedIds; /*!< List of selected identifiers. */
QMap<QString, QString> m_selected; /*!< Keys are ids, values are full names. */
};
/* QML passes its arguments via QVariant. */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment