"Fossies" - the Fresh Open Source Software Archive

Member "cutter-1.8.2/src/common/JsonModel.cpp" (20 May 2019, 2822 Bytes) of package /linux/privat/cutter-1.8.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "JsonModel.cpp" see the Fossies "Dox" file reference documentation.

    1 #include "JsonModel.h"
    2 
    3 JsonModel::JsonModel(QObject *parent) :
    4     QAbstractItemModel(parent)
    5 {
    6     mRootItem = new JsonTreeItem;
    7     mHeaders.append("key");
    8     mHeaders.append("value");
    9 }
   10 
   11 JsonModel::~JsonModel()
   12 {
   13     delete mRootItem;
   14 }
   15 
   16 bool JsonModel::load(QIODevice *device)
   17 {
   18     return loadJson(device->readAll());
   19 }
   20 
   21 bool JsonModel::loadJson(const QByteArray &json)
   22 {
   23     mDocument = QJsonDocument::fromJson(json);
   24 
   25     if (!mDocument.isNull()) {
   26         beginResetModel();
   27         delete mRootItem;
   28         if (mDocument.isArray()) {
   29             mRootItem = JsonTreeItem::load(QJsonValue(mDocument.array()));
   30         } else {
   31             mRootItem = JsonTreeItem::load(QJsonValue(mDocument.object()));
   32         }
   33         endResetModel();
   34         return true;
   35     }
   36     return false;
   37 }
   38 
   39 QVariant JsonModel::data(const QModelIndex &index, int role) const
   40 {
   41 
   42     if (!index.isValid())
   43         return QVariant();
   44 
   45 
   46     JsonTreeItem *item = static_cast<JsonTreeItem *>(index.internalPointer());
   47 
   48 
   49     if (role == Qt::DisplayRole) {
   50 
   51         if (index.column() == 0)
   52             return QString("%1").arg(item->key());
   53 
   54         if (index.column() == 1)
   55             return QString("%1").arg(item->value());
   56     }
   57 
   58     return QVariant();
   59 }
   60 
   61 QVariant JsonModel::headerData(int section, Qt::Orientation orientation, int role) const
   62 {
   63     if (role != Qt::DisplayRole)
   64         return QVariant();
   65 
   66     if (orientation == Qt::Horizontal) {
   67 
   68         return mHeaders.value(section);
   69     } else
   70         return QVariant();
   71 }
   72 
   73 QModelIndex JsonModel::index(int row, int column, const QModelIndex &parent) const
   74 {
   75     if (!hasIndex(row, column, parent))
   76         return QModelIndex();
   77 
   78     JsonTreeItem *parentItem;
   79 
   80     if (!parent.isValid())
   81         parentItem = mRootItem;
   82     else
   83         parentItem = static_cast<JsonTreeItem *>(parent.internalPointer());
   84 
   85     JsonTreeItem *childItem = parentItem->child(row);
   86     if (childItem)
   87         return createIndex(row, column, childItem);
   88     else
   89         return QModelIndex();
   90 }
   91 
   92 QModelIndex JsonModel::parent(const QModelIndex &index) const
   93 {
   94     if (!index.isValid())
   95         return QModelIndex();
   96 
   97     JsonTreeItem *childItem = static_cast<JsonTreeItem *>(index.internalPointer());
   98     JsonTreeItem *parentItem = childItem->parent();
   99 
  100     if (parentItem == mRootItem)
  101         return QModelIndex();
  102 
  103     return createIndex(parentItem->row(), 0, parentItem);
  104 }
  105 
  106 int JsonModel::rowCount(const QModelIndex &parent) const
  107 {
  108     JsonTreeItem *parentItem;
  109     if (parent.column() > 0)
  110         return 0;
  111 
  112     if (!parent.isValid())
  113         parentItem = mRootItem;
  114     else
  115         parentItem = static_cast<JsonTreeItem *>(parent.internalPointer());
  116 
  117     return parentItem->childCount();
  118 }
  119 
  120 int JsonModel::columnCount(const QModelIndex &parent) const
  121 {
  122     Q_UNUSED(parent)
  123     return 2;
  124 }
  125