Add a button Certificate in Dashboard (iCj) Fixes #316 (#409)

* Add a button Certificate in Dashboard (iCj) #316
This commit is contained in:
int 0x80 2018-03-17 22:26:51 +03:00 committed by xarkes
parent 00a1f86bba
commit bba02f4b9d
8 changed files with 391 additions and 4 deletions

View File

@ -144,7 +144,9 @@ SOURCES += \
utils/NestedIPyKernel.cpp \
dialogs/R2PluginsDialog.cpp \
widgets/CutterDockWidget.cpp \
widgets/GraphWidget.cpp
widgets/GraphWidget.cpp \
utils/JsonTreeItem.cpp \
utils/JsonModel.cpp
HEADERS += \
Cutter.h \
@ -213,7 +215,9 @@ HEADERS += \
utils/NestedIPyKernel.h \
dialogs/R2PluginsDialog.h \
widgets/CutterDockWidget.h \
widgets/GraphWidget.h
widgets/GraphWidget.h \
utils/JsonTreeItem.h \
utils/JsonModel.h
FORMS += \
dialogs/AboutDialog.ui \

127
src/utils/JsonModel.cpp Normal file
View File

@ -0,0 +1,127 @@
#include "JsonModel.h"
JsonModel::JsonModel(QObject *parent) :
QAbstractItemModel(parent)
{
mRootItem = new JsonTreeItem;
mHeaders.append("key");
mHeaders.append("value");
}
JsonModel::~JsonModel()
{
delete mRootItem;
}
bool JsonModel::load(QIODevice *device)
{
return loadJson(device->readAll());
}
bool JsonModel::loadJson(const QByteArray &json)
{
mDocument = QJsonDocument::fromJson(json);
if (!mDocument.isNull())
{
beginResetModel();
delete mRootItem;
if (mDocument.isArray()) {
mRootItem = JsonTreeItem::load(QJsonValue(mDocument.array()));
} else {
mRootItem = JsonTreeItem::load(QJsonValue(mDocument.object()));
}
endResetModel();
return true;
}
return false;
}
QVariant JsonModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
JsonTreeItem *item = static_cast<JsonTreeItem*>(index.internalPointer());
if (role == Qt::DisplayRole) {
if (index.column() == 0)
return QString("%1").arg(item->key());
if (index.column() == 1)
return QString("%1").arg(item->value());
}
return QVariant();
}
QVariant JsonModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal) {
return mHeaders.value(section);
}
else
return QVariant();
}
QModelIndex JsonModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
JsonTreeItem *parentItem;
if (!parent.isValid())
parentItem = mRootItem;
else
parentItem = static_cast<JsonTreeItem*>(parent.internalPointer());
JsonTreeItem *childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
QModelIndex JsonModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
JsonTreeItem *childItem = static_cast<JsonTreeItem*>(index.internalPointer());
JsonTreeItem *parentItem = childItem->parent();
if (parentItem == mRootItem)
return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
}
int JsonModel::rowCount(const QModelIndex &parent) const
{
JsonTreeItem *parentItem;
if (parent.column() > 0)
return 0;
if (!parent.isValid())
parentItem = mRootItem;
else
parentItem = static_cast<JsonTreeItem*>(parent.internalPointer());
return parentItem->childCount();
}
int JsonModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return 2;
}

36
src/utils/JsonModel.h Normal file
View File

@ -0,0 +1,36 @@
#ifndef JSONMODEL_H
#define JSONMODEL_H
#include <QAbstractItemModel>
#include <QJsonDocument>
#include <QJsonValue>
#include <QJsonArray>
#include <QJsonObject>
#include <QIcon>
#include "JsonTreeItem.h"
class JsonTreeItem;
class JsonModel : public QAbstractItemModel
{
public:
explicit JsonModel(QObject *parent = 0);
bool load(QIODevice * device);
bool loadJson(const QByteArray& json);
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;
~JsonModel();
private:
JsonTreeItem * mRootItem;
QJsonDocument mDocument;
QStringList mHeaders;
};
#endif // JSONMODEL_H

109
src/utils/JsonTreeItem.cpp Normal file
View File

@ -0,0 +1,109 @@
#include "JsonTreeItem.h"
JsonTreeItem::JsonTreeItem(JsonTreeItem *parent)
{
mParent = parent;
}
JsonTreeItem::~JsonTreeItem()
{
qDeleteAll(mChilds);
}
void JsonTreeItem::appendChild(JsonTreeItem *item)
{
mChilds.append(item);
}
JsonTreeItem *JsonTreeItem::child(int row)
{
return mChilds.value(row);
}
JsonTreeItem *JsonTreeItem::parent()
{
return mParent;
}
int JsonTreeItem::childCount() const
{
return mChilds.count();
}
int JsonTreeItem::row() const
{
if (mParent)
return mParent->mChilds.indexOf(const_cast<JsonTreeItem*>(this));
return 0;
}
void JsonTreeItem::setKey(const QString &key)
{
mKey = key;
}
void JsonTreeItem::setValue(const QString &value)
{
mValue = value;
}
void JsonTreeItem::setType(const QJsonValue::Type &type)
{
mType = type;
}
QString JsonTreeItem::key() const
{
return mKey;
}
QString JsonTreeItem::value() const
{
return mValue;
}
QJsonValue::Type JsonTreeItem::type() const
{
return mType;
}
JsonTreeItem* JsonTreeItem::load(const QJsonValue& value, JsonTreeItem* parent)
{
JsonTreeItem * rootItem = new JsonTreeItem(parent);
rootItem->setKey("root");
if ( value.isObject())
{
//Get all QJsonValue childs
for (QString key : value.toObject().keys()){
QJsonValue v = value.toObject().value(key);
JsonTreeItem * 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()){
JsonTreeItem * 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;
}

39
src/utils/JsonTreeItem.h Normal file
View File

@ -0,0 +1,39 @@
#ifndef JSONTREEITEM_H
#define JSONTREEITEM_H
#include <QAbstractItemModel>
#include <QJsonDocument>
#include <QJsonValue>
#include <QJsonArray>
#include <QJsonObject>
#include <QIcon>
#include "JsonModel.h"
class JsonTreeItem
{
public:
JsonTreeItem(JsonTreeItem * parent = 0);
~JsonTreeItem();
void appendChild(JsonTreeItem * item);
JsonTreeItem *child(int row);
JsonTreeItem *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 JsonTreeItem *load(const QJsonValue& value, JsonTreeItem * parent = 0);
private:
QString mKey;
QString mValue;
QJsonValue::Type mType;
QList<JsonTreeItem*> mChilds;
JsonTreeItem *mParent;
};
#endif // JSONTREEITEM_H

View File

@ -1,6 +1,8 @@
#include "Dashboard.h"
#include "ui_Dashboard.h"
#include "utils/Helpers.h"
#include "utils/JsonModel.h"
#include "utils/JsonTreeItem.h"
#include "MainWindow.h"
@ -11,6 +13,11 @@
#include <QJsonDocument>
#include <QFile>
#include <QLayoutItem>
#include <QString>
#include <QMessageBox>
#include <QDialog>
#include <QTreeView>
#include <QTreeWidget>
Dashboard::Dashboard(MainWindow *main, QAction *action) :
CutterDockWidget(main, action),
@ -171,3 +178,47 @@ void Dashboard::updateContents()
// Get stats for the graphs
QStringList stats = CutterCore::getInstance()->getStats();
}
void Dashboard::on_certificateButton_clicked()
{
static QDialog *viewDialog = nullptr;
static QTreeView *view = nullptr;
static JsonModel *model = nullptr;
static QString qstrCertificates;
if(!viewDialog)
{
viewDialog = new QDialog(this);
view = new QTreeView(viewDialog);
model = new JsonModel();
QJsonDocument qjsonCertificatesDoc = Core()->cmdj("iCj");
qstrCertificates = qjsonCertificatesDoc.toJson(QJsonDocument::Compact);
}
if (QString::compare("{}",qstrCertificates))
{
if(!viewDialog->isVisible())
{
std::string strCertificates = qstrCertificates.toUtf8().constData();
model->loadJson(QByteArray::fromStdString(strCertificates));
view->setModel(model);
view->expandAll();
view->resize(900,600);
QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(view->sizePolicy().hasHeightForWidth());
viewDialog->setSizePolicy(sizePolicy);
viewDialog->setMinimumSize(QSize(900, 600));
viewDialog->setMaximumSize(QSize(900, 600));
viewDialog->setSizeGripEnabled(false);
viewDialog->setWindowTitle("Certificates");
viewDialog->show();
}
}
else
{
QMessageBox msgBoxCertificateInf(QMessageBox::Information, "Certificate Information ",
"There is no certificate information",
QMessageBox::NoButton, this);
msgBoxCertificateInf.exec();
}
}

View File

@ -21,6 +21,7 @@ public:
private slots:
void updateContents();
void on_certificateButton_clicked();
private:
std::unique_ptr<Ui::Dashboard> ui;

View File

@ -11,7 +11,7 @@
</rect>
</property>
<property name="styleSheet">
<string notr="true"></string>
<string notr="true"/>
</property>
<property name="windowTitle">
<string notr="true">Dashboard</string>
@ -56,7 +56,7 @@
<x>0</x>
<y>0</y>
<width>1055</width>
<height>974</height>
<height>979</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_5">
@ -866,6 +866,26 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="certificateButton">
<property name="maximumSize">
<size>
<width>100</width>
<height>167</height>
</size>
</property>
<property name="text">
<string>Certificates</string>
</property>
</widget>
</item>
<item>
<widget class="Line" name="line_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<property name="topMargin">