change license to MIT, make code simple, add C++11 feature

This commit is contained in:
dridk 2017-01-06 19:53:45 +01:00
parent 5e6935db6f
commit 5b350248a7
20 changed files with 255 additions and 462 deletions

View File

@ -11,17 +11,13 @@ greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QJsonModel
TEMPLATE = app
SOURCES += \
main.cpp \
qjsonmodel.cpp
HEADERS += \
qjsonmodel.h
SOURCES += main.cpp\
mainwindow.cpp \
qjsonmodel.cpp \
qjsonitem.cpp
HEADERS += mainwindow.h \
qjsonmodel.h \
qjsonitem.h
FORMS += mainwindow.ui
RESOURCES += \
resource.qrc

View File

@ -1,12 +0,0 @@
<RCC>
<qresource prefix="/">
<file>icons/brick.png</file>
<file>icons/bullet_black.png</file>
<file>icons/bullet_blue.png</file>
<file>icons/bullet_green.png</file>
<file>icons/bullet_orange.png</file>
<file>icons/bullet_purple.png</file>
<file>icons/bullet_red.png</file>
<file>icons/table.png</file>
</qresource>
</RCC>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 770 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 792 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 740 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 754 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 773 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 779 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 920 B

View File

@ -17,14 +17,49 @@
**********************************************/
#include "mainwindow.h"
#include <QApplication>
#include <QTreeView>
#include <QFile>
#include <string>
#include "qjsonmodel.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QTreeView * view = new QTreeView;
QJsonModel * model = new QJsonModel;
view->setModel(model);
std::string json = R"({
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address":
{
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber":
[
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
})";
model->loadJson(QByteArray::fromStdString(json));
view->show();
return a.exec();
}

View File

@ -1,84 +0,0 @@
/***********************************************
Copyright (C) 2014 Schutz Sacha
This file is part of QJsonModel (https://github.com/dridk/QJsonmodel).
QJsonModel is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
QJsonModel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with QJsonModel. If not, see <http://www.gnu.org/licenses/>.
**********************************************/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>
#include "qjsonitem.h"
#include "qjsonmodel.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
mModel= new QJsonModel;
ui->treeView->setModel(mModel);
ui->treeView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
connect(ui->actionOpen,SIGNAL(triggered()),this,SLOT(openFile()));
connect(ui->tabWidget,SIGNAL(tabBarClicked(int)),this,SLOT(tabChanged(int)));
mModel->setIcon(QJsonValue::Bool, QIcon(":/icons/bullet_black.png"));
mModel->setIcon(QJsonValue::Double, QIcon(":/icons/bullet_red.png"));
mModel->setIcon(QJsonValue::String, QIcon(":/icons/bullet_blue.png"));
mModel->setIcon(QJsonValue::Array, QIcon(":/icons/table.png"));
mModel->setIcon(QJsonValue::Object, QIcon(":/icons/brick.png"));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::openFile()
{
QString filename = QFileDialog::getOpenFileName(this,tr("Open Json file"),"",tr("Json file (*.json)"));
QFile file(filename);
if (file.open(QIODevice::ReadOnly)){
ui->textEdit->setPlainText(file.readAll());
file.close();
ui->tabWidget->setCurrentIndex(0);
}
}
void MainWindow::tabChanged(int index)
{
if (index == 0) {
if (!mModel->loadJson(ui->textEdit->toPlainText().toUtf8()))
QMessageBox::warning(this,"error","Cannot load Json Data");
}
}

View File

@ -1,30 +0,0 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QJsonDocument>
#include <QJsonObject>
#include "qjsonmodel.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void openFile();
void tabChanged(int index);
private:
Ui::MainWindow *ui;
QJsonDocument mDocument;
QJsonModel * mModel;
};
#endif // MAINWINDOW_H

View File

@ -1,91 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>579</width>
<height>318</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>1</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Tree</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QTreeView" name="treeView"/>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Raw</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QTextEdit" name="textEdit">
<property name="html">
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Ubuntu'; font-size:9pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;{ &amp;quot;firstName&amp;quot;: &amp;quot;John&amp;quot;, &amp;quot;lastName&amp;quot;: &amp;quot;Smith&amp;quot;, &amp;quot;age&amp;quot;: 25, &amp;quot;address&amp;quot;: { &amp;quot;streetAddress&amp;quot;: &amp;quot;21 2nd Street&amp;quot;, &amp;quot;city&amp;quot;: &amp;quot;New York&amp;quot;, &amp;quot;state&amp;quot;: &amp;quot;NY&amp;quot;, &amp;quot;postalCode&amp;quot;: &amp;quot;10021&amp;quot; }, &amp;quot;phoneNumber&amp;quot;: [ { &amp;quot;type&amp;quot;: &amp;quot;home&amp;quot;, &amp;quot;number&amp;quot;: &amp;quot;212 555-1234&amp;quot; }, { &amp;quot;type&amp;quot;: &amp;quot;fax&amp;quot;, &amp;quot;number&amp;quot;: &amp;quot;646 555-4567&amp;quot; } ], &amp;quot;gender&amp;quot;: { &amp;quot;type&amp;quot;: &amp;quot;male&amp;quot; } }&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="acceptRichText">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>579</width>
<height>21</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
</property>
<addaction name="actionOpen"/>
<addaction name="actionClose"/>
</widget>
<addaction name="menuFile"/>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="actionOpen">
<property name="text">
<string>Open ...</string>
</property>
</action>
<action name="actionClose">
<property name="text">
<string>Close</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

View File

@ -1,137 +0,0 @@
/***********************************************
Copyright (C) 2014 Schutz Sacha
This file is part of QJsonModel (https://github.com/dridk/QJsonmodel).
QJsonModel is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
QJsonModel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with QJsonModel. If not, see <http://www.gnu.org/licenses/>.
**********************************************/
#include "qjsonitem.h"
QJsonTreeItem::QJsonTreeItem(QJsonTreeItem *parent)
{
mParent = parent;
}
QJsonTreeItem::~QJsonTreeItem()
{
qDeleteAll(mChilds);
}
void QJsonTreeItem::appendChild(QJsonTreeItem *item)
{
mChilds.append(item);
}
QJsonTreeItem *QJsonTreeItem::child(int row)
{
return mChilds.value(row);
}
QJsonTreeItem *QJsonTreeItem::parent()
{
return mParent;
}
int QJsonTreeItem::childCount() const
{
return mChilds.count();
}
int QJsonTreeItem::row() const
{
if (mParent)
return mParent->mChilds.indexOf(const_cast<QJsonTreeItem*>(this));
return 0;
}
void QJsonTreeItem::setKey(const QString &key)
{
mKey = key;
}
void QJsonTreeItem::setValue(const QString &value)
{
mValue = value;
}
void QJsonTreeItem::setType(const QJsonValue::Type &type)
{
mType = type;
}
QString QJsonTreeItem::key() const
{
return mKey;
}
QString QJsonTreeItem::value() const
{
return mValue;
}
QJsonValue::Type QJsonTreeItem::type() const
{
return mType;
}
QJsonTreeItem* QJsonTreeItem::load(const QJsonValue& value, QJsonTreeItem* parent)
{
QJsonTreeItem * rootItem = new QJsonTreeItem(parent);
rootItem->setKey("root");
if ( value.isObject())
{
//Get all QJsonValue childs
foreach (QString key , value.toObject().keys()){
QJsonValue v = value.toObject().value(key);
QJsonTreeItem * child = load(v,rootItem);
child->setKey(key);
child->setType(v.type());
rootItem->appendChild(child);
}
}
else if ( value.isArray())
{
//Get all QJsonValue childs
int index = 0;
foreach (QJsonValue v , value.toArray()){
QJsonTreeItem * child = load(v,rootItem);
child->setKey(QString::number(index));
child->setType(v.type());
rootItem->appendChild(child);
++index;
}
}
else
{
rootItem->setValue(value.toVariant().toString());
rootItem->setType(value.type());
}
return rootItem;
}

View File

@ -1,41 +0,0 @@
#ifndef JSONITEM_H
#define JSONITEM_H
#include <QtCore>
#include <QJsonValue>
#include <QJsonArray>
#include <QJsonObject>
class QJsonTreeItem
{
public:
QJsonTreeItem(QJsonTreeItem * parent = 0);
~QJsonTreeItem();
void appendChild(QJsonTreeItem * item);
QJsonTreeItem *child(int row);
QJsonTreeItem *parent();
int childCount() const;
int row() const;
void setKey(const QString& key);
void setValue(const QString& value);
void setType(const QJsonValue::Type& type);
QString key() const;
QString value() const;
QJsonValue::Type type() const;
static QJsonTreeItem* load(const QJsonValue& value, QJsonTreeItem * parent = 0);
protected:
private:
QString mKey;
QString mValue;
QJsonValue::Type mType;
QList<QJsonTreeItem*> mChilds;
QJsonTreeItem * mParent;
};
#endif // JSONITEM_H

View File

@ -1,30 +1,147 @@
/***********************************************
Copyright (C) 2014 Schutz Sacha
This file is part of QJsonModel (https://github.com/dridk/QJsonmodel).
QJsonModel is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
QJsonModel is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with QJsonModel. If not, see <http://www.gnu.org/licenses/>.
**********************************************/
/*
* The MIT License (MIT)
*
* Copyright (c) 2011 SCHUTZ Sacha
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "qjsonmodel.h"
#include <QFile>
#include <QDebug>
#include <QJsonDocument>
#include <QJsonObject>
#include <QIcon>
#include <QFont>
QJsonTreeItem::QJsonTreeItem(QJsonTreeItem *parent)
{
mParent = parent;
}
QJsonTreeItem::~QJsonTreeItem()
{
qDeleteAll(mChilds);
}
void QJsonTreeItem::appendChild(QJsonTreeItem *item)
{
mChilds.append(item);
}
QJsonTreeItem *QJsonTreeItem::child(int row)
{
return mChilds.value(row);
}
QJsonTreeItem *QJsonTreeItem::parent()
{
return mParent;
}
int QJsonTreeItem::childCount() const
{
return mChilds.count();
}
int QJsonTreeItem::row() const
{
if (mParent)
return mParent->mChilds.indexOf(const_cast<QJsonTreeItem*>(this));
return 0;
}
void QJsonTreeItem::setKey(const QString &key)
{
mKey = key;
}
void QJsonTreeItem::setValue(const QString &value)
{
mValue = value;
}
void QJsonTreeItem::setType(const QJsonValue::Type &type)
{
mType = type;
}
QString QJsonTreeItem::key() const
{
return mKey;
}
QString QJsonTreeItem::value() const
{
return mValue;
}
QJsonValue::Type QJsonTreeItem::type() const
{
return mType;
}
QJsonTreeItem* QJsonTreeItem::load(const QJsonValue& value, QJsonTreeItem* parent)
{
QJsonTreeItem * rootItem = new QJsonTreeItem(parent);
rootItem->setKey("root");
if ( value.isObject())
{
//Get all QJsonValue childs
for (QString key : value.toObject().keys()){
QJsonValue v = value.toObject().value(key);
QJsonTreeItem * child = load(v,rootItem);
child->setKey(key);
child->setType(v.type());
rootItem->appendChild(child);
}
}
else if ( value.isArray())
{
//Get all QJsonValue childs
int index = 0;
for (QJsonValue v : value.toArray()){
QJsonTreeItem * child = load(v,rootItem);
child->setKey(QString::number(index));
child->setType(v.type());
rootItem->appendChild(child);
++index;
}
}
else
{
rootItem->setValue(value.toVariant().toString());
rootItem->setType(value.type());
}
return rootItem;
}
//=========================================================================
QJsonModel::QJsonModel(QObject *parent) :
QAbstractItemModel(parent)
{
@ -68,6 +185,8 @@ bool QJsonModel::loadJson(const QByteArray &json)
endResetModel();
return true;
}
qDebug()<<Q_FUNC_INFO<<"cannot load json";
return false;
}
@ -82,12 +201,6 @@ QVariant QJsonModel::data(const QModelIndex &index, int role) const
QJsonTreeItem *item = static_cast<QJsonTreeItem*>(index.internalPointer());
if ((role == Qt::DecorationRole) && (index.column() == 0)){
return mTypeIcons.value(item->type());
}
if (role == Qt::DisplayRole) {
if (index.column() == 0)
@ -169,7 +282,3 @@ int QJsonModel::columnCount(const QModelIndex &parent) const
return 2;
}
void QJsonModel::setIcon(const QJsonValue::Type &type, const QIcon &icon)
{
mTypeIcons.insert(type,icon);
}

View File

@ -1,11 +1,75 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2011 SCHUTZ Sacha
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef QJSONMODEL_H
#define QJSONMODEL_H
#include <QAbstractItemModel>
#include "qjsonitem.h"
#include <QJsonDocument>
#include <QJsonValue>
#include <QJsonArray>
#include <QJsonObject>
#include <QIcon>
class QJsonModel;
class QJsonItem;
class QJsonTreeItem
{
public:
QJsonTreeItem(QJsonTreeItem * parent = 0);
~QJsonTreeItem();
void appendChild(QJsonTreeItem * item);
QJsonTreeItem *child(int row);
QJsonTreeItem *parent();
int childCount() const;
int row() const;
void setKey(const QString& key);
void setValue(const QString& value);
void setType(const QJsonValue::Type& type);
QString key() const;
QString value() const;
QJsonValue::Type type() const;
static QJsonTreeItem* load(const QJsonValue& value, QJsonTreeItem * parent = 0);
protected:
private:
QString mKey;
QString mValue;
QJsonValue::Type mType;
QList<QJsonTreeItem*> mChilds;
QJsonTreeItem * mParent;
};
//---------------------------------------------------
class QJsonModel : public QAbstractItemModel
{
Q_OBJECT
@ -14,21 +78,17 @@ public:
bool load(const QString& fileName);
bool load(QIODevice * device);
bool loadJson(const QByteArray& json);
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
QModelIndex index(int row, int column,const QModelIndex &parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex &index) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
void setIcon(const QJsonValue::Type& type, const QIcon& icon);
QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
QVariant headerData(int section, Qt::Orientation orientation, int role) const Q_DECL_OVERRIDE;
QModelIndex index(int row, int column,const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
QModelIndex parent(const QModelIndex &index) const Q_DECL_OVERRIDE;
int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
private:
QJsonTreeItem * mRootItem;
QJsonDocument mDocument;
QStringList mHeaders;
QHash<QJsonValue::Type, QIcon> mTypeIcons;
};

View File

@ -1,12 +0,0 @@
<RCC>
<qresource prefix="/">
<file>icons/brick.png</file>
<file>icons/bullet_black.png</file>
<file>icons/bullet_blue.png</file>
<file>icons/bullet_green.png</file>
<file>icons/bullet_orange.png</file>
<file>icons/bullet_purple.png</file>
<file>icons/bullet_red.png</file>
<file>icons/table.png</file>
</qresource>
</RCC>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 50 KiB