bool QmlConsoleItemModel::insertRows(int position, int rows, const QModelIndex &parent) { ConsoleItem *parentItem = getItem(parent); bool success; beginInsertRows(parent, position, position + rows - 1); success = parentItem->insertChildren(position, rows); endInsertRows(); return success; }
bool QmlConsoleItemModel::removeRows(int position, int rows, const QModelIndex &parent) { ConsoleItem *parentItem = getItem(parent); bool success = true; beginRemoveRows(parent, position, position + rows - 1); success = parentItem->removeChildren(position, rows); endRemoveRows(); return success; }
Qt::ItemFlags QmlConsoleItemModel::flags(const QModelIndex &index) const { if (!index.isValid()) return 0; ConsoleItem *item = getItem(index); if (m_hasEditableRow && item->parent() == m_rootItem && index.row() == m_rootItem->childCount() - 1) return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable; return Qt::ItemIsEnabled | Qt::ItemIsSelectable; }
void ConsoleItem::fetchMore() { if (m_doFetch) { m_doFetch(this); m_doFetch = std::function<void(ConsoleItem *)>(); } foreach (TreeItem *child, children()) { ConsoleItem *item = static_cast<ConsoleItem *>(child); if (item->m_doFetch) { item->m_doFetch(item); item->m_doFetch = m_doFetch; } }
QModelIndex QmlConsoleItemModel::parent(const QModelIndex &index) const { if (!index.isValid()) return QModelIndex(); ConsoleItem *childItem = getItem(index); ConsoleItem *parentItem = childItem->parent(); if (parentItem == m_rootItem) return QModelIndex(); if (!parentItem) return QModelIndex(); return createIndex(parentItem->childNumber(), 0, parentItem); }
QModelIndex QmlConsoleItemModel::index(int row, int column, const QModelIndex &parent) const { if (parent.isValid() && parent.column() != 0) return QModelIndex(); if (column > 0) return QModelIndex(); ConsoleItem *parentItem = getItem(parent); ConsoleItem *childItem = parentItem->child(row); if (childItem) return createIndex(row, column, childItem); else return QModelIndex(); }
QVariant QmlConsoleItemModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); ConsoleItem *item = getItem(index); if (role == Qt::DisplayRole ) return item->text(); else if (role == QmlConsoleItemModel::TypeRole) return int(item->itemType); else if (role == QmlConsoleItemModel::FileRole) return item->file; else if (role == QmlConsoleItemModel::LineRole) return item->line; else if (role == QmlConsoleItemModel::ExpressionRole) return item->expression(); else return QVariant(); }
bool QmlConsoleItemModel::setData(const QModelIndex &index, const QVariant &value, int role) { ConsoleItem *item = getItem(index); bool result = false; if (role == Qt::DisplayRole) { item->setText(value.toString()); result = true; } else if (role == QmlConsoleItemModel::TypeRole) { item->itemType = (ConsoleItem::ItemType)value.toInt(); result = true; } else if (role == QmlConsoleItemModel::FileRole) { item->file = value.toString(); result = true; } else if (role == QmlConsoleItemModel::LineRole) { item->line = value.toInt(); result = true; } if (result) emit dataChanged(index, index); return result; }
ConsoleItem *constructLogItemTree(ConsoleItem *parent, const QVariant &result, const QString &key = QString()) { if (!result.isValid()) return 0; ConsoleItem *item = new ConsoleItem(parent); if (result.type() == QVariant::Map) { if (key.isEmpty()) item->setText(QLatin1String("Object")); else item->setText(key + QLatin1String(" : Object")); QMapIterator<QString, QVariant> i(result.toMap()); while (i.hasNext()) { i.next(); ConsoleItem *child = constructLogItemTree(item, i.value(), i.key()); if (child) item->insertChild(child, true); } } else if (result.type() == QVariant::List) { if (key.isEmpty()) item->setText(QLatin1String("List")); else item->setText(QString(QLatin1String("[%1] : List")).arg(key)); QVariantList resultList = result.toList(); for (int i = 0; i < resultList.count(); i++) { ConsoleItem *child = constructLogItemTree(item, resultList.at(i), QString::number(i)); if (child) item->insertChild(child, true); } } else if (result.canConvert(QVariant::String)) { item->setText(result.toString()); } else { item->setText(QLatin1String("Unknown Value")); } return item; }
int QmlConsoleItemModel::rowCount(const QModelIndex &parent) const { ConsoleItem *parentItem = getItem(parent); return parentItem->childCount(); }