Commit 7ba14443 authored by Karel Slaný's avatar Karel Slaný

Added DataboxListModel::removeEntry().

parent f21c1d31
......@@ -294,6 +294,7 @@ int main(int argc, char *argv[])
/* Register types into QML. */
AccountListModel::declareQML();
DataboxListModel::declareQML();
DataboxModelEntry::declareQML();
Dialogues::declareQML();
FileListModel::declareQML();
InteractionFilesystem::declareQML();
......
......@@ -171,22 +171,22 @@ void DataboxListModel::declareQML(void)
DataboxListModel::DataboxListModel(QObject *parent)
: QAbstractListModel(parent),
m_databoxes(),
m_selected()
m_boxIds(),
m_entries()
{
}
DataboxListModel::DataboxListModel(const DataboxListModel &model,
QObject *parent)
: QAbstractListModel(parent),
m_databoxes(model.m_databoxes),
m_selected(model.m_selected)
m_boxIds(model.m_boxIds),
m_entries(model.m_entries)
{
}
int DataboxListModel::rowCount(const QModelIndex &parent) const
{
return !parent.isValid() ? m_databoxes.size() : 0;
return !parent.isValid() ? m_boxIds.size() : 0;
}
QHash<int, QByteArray> DataboxListModel::roleNames(void) const
......@@ -207,36 +207,37 @@ QHash<int, QByteArray> DataboxListModel::roleNames(void) const
QVariant DataboxListModel::data(const QModelIndex &index, int role) const
{
if ((index.row() < 0) || (index.row() >= m_databoxes.size())) {
if ((index.row() < 0) || (index.row() >= m_boxIds.size())) {
return QVariant();
}
const DataboxModelEntry &databox(m_databoxes.at(index.row()));
const QString &boxId(m_boxIds.at(index.row()));
const DataboxModelEntry &entry(m_entries[boxId].entry);
switch (role) {
case ROLE_DB_ID:
return databox.dbID();
return entry.dbID();
break;
case ROLE_DB_TYPE:
return databox.dbType();
return entry.dbType();
break;
case ROLE_DB_NAME:
return databox.dbName();
return entry.dbName();
break;
case ROLE_DB_ADDRESS:
return databox.dbAddress();
return entry.dbAddress();
break;
case ROLE_DB_IC:
return databox.dbIC();
return entry.dbIC();
break;
case ROLE_DB_EFFECTIVE_OVM:
return databox.dbEffectiveOVM();
return entry.dbEffectiveOVM();
break;
case ROLE_DB_SEND_OPTION:
return databox.dbSendOptions();
return entry.dbSendOptions();
break;
case ROLE_DB_SELECTED:
return m_selected.at(index.row());
return m_entries[boxId].selected;
break;
default:
/* Do nothing. */
......@@ -248,13 +249,23 @@ QVariant DataboxListModel::data(const QModelIndex &index, int role) const
bool DataboxListModel::addEntry(const DataboxModelEntry &entry)
{
if (Q_UNLIKELY(entry.dbID().isEmpty())) {
qDebug("%s", "BBB001");
const QString key(entry.dbID());
if (Q_UNLIKELY(key.isEmpty())) {
qDebug("%s", "BBB002");
return false;
}
QMap<QString, InternalEntry>::const_iterator it = m_entries.find(key);
if (it != m_entries.end()) {
return false;
}
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_databoxes.append(entry);
m_selected.append(false);
m_boxIds.append(key);
m_entries[key] = InternalEntry(entry, false);
endInsertRows();
return true;
......@@ -270,20 +281,39 @@ bool DataboxListModel::addEntry(const QVariant &entryVariant)
return addEntry(*entry);
}
DataboxModelEntry *DataboxListModel::entry(const QString &boxId) const
bool DataboxListModel::removeEntry(const QString &boxId)
{
if (Q_UNLIKELY(boxId.isEmpty())) {
Q_ASSERT(0);
return new (std::nothrow) DataboxModelEntry;
return false;
}
QMap<QString, InternalEntry>::const_iterator it = m_entries.find(boxId);
if (it == m_entries.end()) {
return false;
}
foreach (const DataboxModelEntry &entry, m_databoxes) {
if (entry.dbID() == boxId) {
return new (std::nothrow) DataboxModelEntry(entry);
for (int r = (m_boxIds.size() - 1); r >= 0; --r) {
if (m_boxIds.at(r) == boxId) {
beginRemoveRows(QModelIndex(), r, r);
m_boxIds.removeAt(r);
endRemoveRows();
}
}
return new (std::nothrow) DataboxModelEntry;
m_entries.remove(boxId);
return true;
}
DataboxModelEntry *DataboxListModel::entry(const QString &boxId) const
{
if (Q_UNLIKELY(boxId.isEmpty())) {
Q_ASSERT(0);
return new (std::nothrow) DataboxModelEntry;
}
return new (std::nothrow) DataboxModelEntry(
m_entries.value(boxId,
InternalEntry(DataboxModelEntry(), false)).entry);
}
bool DataboxListModel::selectEntry(const QString &boxId, bool selected)
......@@ -292,9 +322,14 @@ bool DataboxListModel::selectEntry(const QString &boxId, bool selected)
return false;
}
for (int i = 0; i < m_databoxes.size(); ++i) {
if (boxId == m_databoxes.at(i).dbID()) {
m_selected[i] = selected;
QMap<QString, InternalEntry>::iterator it = m_entries.find(boxId);
if (it == m_entries.end()) {
return false;
}
for (int i = 0; i < m_boxIds.size(); ++i) {
if (boxId == m_boxIds.at(i)) {
it->selected = selected;
emit dataChanged(QAbstractListModel::index(i, 0),
QAbstractListModel::index(i, 0));
......@@ -317,19 +352,20 @@ int DataboxListModel::setQuery(QSqlQuery &query, const QString &dbId,
beginResetModel();
if (!isAppend) {
m_databoxes.clear();
m_selected.clear();
m_boxIds.clear();
m_entries.clear();
}
query.first();
while (query.isActive() && query.isValid()) {
if (dbId != query.value(0).toString()) {
// some model items are not available in local database
m_databoxes.append(DataboxModelEntry(
DataboxModelEntry entry(
query.value(0).toString(), QString(),
query.value(1).toString(), query.value(2).toString(),
QString(), QString(), QString()));
m_selected.append(false);
QString(), QString(), QString());
m_boxIds.append(entry.dbID());
m_entries[entry.dbID()] = InternalEntry(entry, false);
databoxCnt++;
}
query.next();
......@@ -348,8 +384,8 @@ Qt::ItemFlags DataboxListModel::flags(const QModelIndex &index) const
void DataboxListModel::clearAll(void)
{
beginResetModel();
m_databoxes.clear();
m_selected.clear();
m_boxIds.clear();
m_entries.clear();
endResetModel();
}
......
......@@ -210,6 +210,15 @@ public:
Q_INVOKABLE
bool addEntry(const QVariant &entryVariant);
/*!
* @brief Remove model entry.
*
* @param[in] boxId Data box identifier of the removed box entry.
* @return True if entry was really removed.
*/
Q_INVOKABLE
bool removeEntry(const QString &boxId);
/*!
* @brief Return entry at given row.
*
......@@ -270,8 +279,27 @@ public:
DataboxListModel *fromVariant(const QVariant &modelVariant);
private:
QList<DataboxModelEntry> m_databoxes; /*!< List of box entries. */
QList<bool> m_selected; /*!< Holds information whether entry is selected. */
/*!
* @brief Convenience structure.
*/
class InternalEntry {
public:
InternalEntry(void)
: entry(), selected(false)
{
}
InternalEntry(const DataboxModelEntry &e, bool s)
: entry(e), selected(s)
{
}
DataboxModelEntry entry;
bool selected;
};
QList<QString> m_boxIds; /*!< List of box identifiers (map keys). */
QMap<QString, InternalEntry> m_entries; /*!< Data box entries. */
};
/* 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