Merge pull request #207 from calamares/debugwindow

New debug window for Calamares testing, adaptation and deployment
This commit is contained in:
Teo Mrnjavac 2015-03-11 16:58:24 +01:00
commit a25c0cf490
19 changed files with 800 additions and 41 deletions

View File

@ -23,6 +23,9 @@
#include "utils/CalamaresUtilsGui.h"
#include "utils/CalamaresStyle.h"
#include "utils/Logger.h"
#include "utils/DebugWindow.h"
#include "utils/Retranslator.h"
#include "Settings.h"
#include "Branding.h"
#include <QApplication>
@ -33,6 +36,7 @@
CalamaresWindow::CalamaresWindow( QWidget* parent )
: QWidget( parent )
, m_debugWindow( nullptr )
{
// Hide close button
setWindowFlags( Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint );
@ -83,6 +87,40 @@ CalamaresWindow::CalamaresWindow( QWidget* parent )
ProgressTreeView* tv = new ProgressTreeView( sideBox );
sideLayout->addWidget( tv );
tv->setFocusPolicy( Qt::NoFocus );
if ( Calamares::Settings::instance()->debugMode() )
{
QPushButton* debugWindowBtn = new QPushButton;
CALAMARES_RETRANSLATE(
debugWindowBtn->setText( tr( "Show debug information" ) );
)
sideLayout->addWidget( debugWindowBtn );
debugWindowBtn->setFlat( true );
debugWindowBtn->setCheckable( true );
connect( debugWindowBtn, &QPushButton::clicked,
[ this, debugWindowBtn ]( bool checked )
{
if ( checked )
{
m_debugWindow = new Calamares::DebugWindow();
m_debugWindow->show();
connect( m_debugWindow, &Calamares::DebugWindow::closed,
[ this, debugWindowBtn ]
{
m_debugWindow->deleteLater();
debugWindowBtn->setChecked( false );
} );
}
else
{
if ( m_debugWindow )
{
m_debugWindow->deleteLater();
}
}
} );
}
CalamaresUtils::unmarginLayout( sideLayout );
CalamaresUtils::unmarginLayout( mainLayout );

View File

@ -1,6 +1,6 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
* Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
*
* Calamares is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -19,8 +19,14 @@
#ifndef CALAMARESWINDOW_H
#define CALAMARESWINDOW_H
#include <QPointer>
#include <QWidget>
namespace Calamares
{
class DebugWindow;
}
class CalamaresWindow : public QWidget
{
Q_OBJECT
@ -28,6 +34,8 @@ public:
CalamaresWindow( QWidget* parent = nullptr );
virtual ~CalamaresWindow() {}
private:
QPointer< Calamares::DebugWindow > m_debugWindow;
};
#endif //CALAMARESWINDOW_H

View File

@ -1,6 +1,6 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
* Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
*
* Calamares is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -31,6 +31,7 @@ namespace bp = boost::python;
namespace Calamares {
GlobalStorage::GlobalStorage()
: QObject( nullptr )
{
}
@ -53,6 +54,7 @@ void
GlobalStorage::insert( const QString& key, const QVariant& value )
{
m.insert( key, value );
emit changed();
}
@ -67,6 +69,7 @@ int
GlobalStorage::remove( const QString& key )
{
return m.remove( key );
emit changed();
}
@ -76,47 +79,63 @@ GlobalStorage::value( const QString& key ) const
return m.value( key );
}
} // namespace Calamares
#ifdef WITH_PYTHON
bool
GlobalStorage::python_contains( const std::string& key ) const
namespace CalamaresPython
{
return contains( QString::fromStdString( key ) );
GlobalStoragePythonWrapper::GlobalStoragePythonWrapper( Calamares::GlobalStorage* gs )
: m_gs( gs )
{}
bool
GlobalStoragePythonWrapper::contains( const std::string& key ) const
{
return m_gs->contains( QString::fromStdString( key ) );
}
int
GlobalStoragePythonWrapper::count() const
{
return m_gs->count();
}
void
GlobalStorage::python_insert( const std::string& key,
GlobalStoragePythonWrapper::insert( const std::string& key,
const bp::object& value )
{
insert( QString::fromStdString( key ),
CalamaresPython::variantFromPyObject( value ) );
m_gs->insert( QString::fromStdString( key ),
CalamaresPython::variantFromPyObject( value ) );
}
bp::list
GlobalStorage::python_keys() const
GlobalStoragePythonWrapper::keys() const
{
bp::list pyList;
foreach( const QString& key, keys() )
foreach( const QString& key, m_gs->keys() )
pyList.append( key.toStdString() );
return pyList;
}
int
GlobalStorage::python_remove( const std::string& key )
GlobalStoragePythonWrapper::remove( const std::string& key )
{
return remove( QString::fromStdString( key ) );
return m_gs->remove( QString::fromStdString( key ) );
}
bp::object
GlobalStorage::python_value( const std::string& key ) const
GlobalStoragePythonWrapper::value( const std::string& key ) const
{
return CalamaresPython::variantToPyObject( value( QString::fromStdString( key ) ) );
return CalamaresPython::variantToPyObject( m_gs->value( QString::fromStdString( key ) ) );
}
#endif // WITH_PYTHON
} // namespace CalamaresPython
} // namespace Calamares
#endif // WITH_PYTHON

View File

@ -1,6 +1,6 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
* Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
*
* Calamares is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -40,8 +40,11 @@ class list;
namespace Calamares
{
class GlobalStorage
class DebugWindow;
class GlobalStorage : public QObject
{
Q_OBJECT
public:
explicit GlobalStorage();
@ -54,17 +57,37 @@ public:
int remove( const QString& key );
QVariant value( const QString& key ) const;
#ifdef WITH_PYTHON
bool python_contains( const std::string& key ) const;
void python_insert( const std::string& key, const boost::python::api::object& value );
boost::python::list python_keys() const;
int python_remove( const std::string& key );
boost::python::api::object python_value( const std::string& key ) const;
#endif
signals:
void changed();
private:
QVariantMap m;
friend DebugWindow;
};
} // namespace Calamares
#ifdef WITH_PYTHON
namespace CalamaresPython
{
class GlobalStoragePythonWrapper
{
public:
explicit GlobalStoragePythonWrapper( Calamares::GlobalStorage* gs );
bool contains( const std::string& key ) const;
int count() const;
void insert( const std::string& key, const boost::python::api::object& value );
boost::python::list keys() const;
int remove( const std::string& key );
boost::python::api::object value( const std::string& key ) const;
private:
Calamares::GlobalStorage* m_gs;
};
} // namespace CalamaresPython
#endif
#endif // CALAMARES_GLOBALSTORAGE_H

View File

@ -1,6 +1,6 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
* Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
*
* Calamares is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -158,6 +158,7 @@ JobQueue::enqueue( const Calamares::job_ptr& job )
{
Q_ASSERT( !m_thread->isRunning() );
m_jobs.append( job );
emit queueChanged( m_jobs );
}
@ -166,6 +167,7 @@ JobQueue::enqueue( const QList< job_ptr >& jobs )
{
Q_ASSERT( !m_thread->isRunning() );
m_jobs.append( jobs );
emit queueChanged( m_jobs );
}
} // namespace Calamares

View File

@ -1,6 +1,6 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
* Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
*
* Calamares is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -46,6 +46,7 @@ public:
void start();
signals:
void queueChanged( const QList< Calamares::job_ptr >& jobs );
void progress( qreal percent, const QString& prettyName );
void finished();
void failed( const QString& message, const QString& details );

View File

@ -1,6 +1,6 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
* Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
*
* Calamares is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -79,13 +79,13 @@ BOOST_PYTHON_MODULE( libcalamares )
"as a real number between 0 and 1."
);
bp::class_< Calamares::GlobalStorage >( "GlobalStorage", bp::init<>() )
.def( "contains", &Calamares::GlobalStorage::python_contains )
.def( "count", &Calamares::GlobalStorage::count )
.def( "insert", &Calamares::GlobalStorage::python_insert )
.def( "keys", &Calamares::GlobalStorage::python_keys )
.def( "remove", &Calamares::GlobalStorage::python_remove )
.def( "value", &Calamares::GlobalStorage::python_value );
bp::class_< CalamaresPython::GlobalStoragePythonWrapper >( "GlobalStorage", bp::init< Calamares::GlobalStorage* >() )
.def( "contains", &CalamaresPython::GlobalStoragePythonWrapper::contains )
.def( "count", &CalamaresPython::GlobalStoragePythonWrapper::count )
.def( "insert", &CalamaresPython::GlobalStoragePythonWrapper::insert )
.def( "keys", &CalamaresPython::GlobalStoragePythonWrapper::keys )
.def( "remove", &CalamaresPython::GlobalStoragePythonWrapper::remove )
.def( "value", &CalamaresPython::GlobalStoragePythonWrapper::value );
// libcalamares.utils submodule starts here
bp::object utilsModule( bp::handle<>( bp::borrowed( PyImport_AddModule( "libcalamares.utils" ) ) ) );
@ -271,7 +271,8 @@ PythonJob::exec()
bp::dict calamaresNamespace = bp::extract< bp::dict >( calamaresModule.attr( "__dict__" ) );
calamaresNamespace[ "job" ] = CalamaresPython::PythonJobInterface( this );
calamaresNamespace[ "globalstorage" ] = bp::ptr( JobQueue::instance()->globalStorage() );
calamaresNamespace[ "globalstorage" ] = CalamaresPython::GlobalStoragePythonWrapper(
JobQueue::instance()->globalStorage() );
bp::object execResult = bp::exec_file( scriptFI.absoluteFilePath().toLocal8Bit().data(),
scriptNamespace,

View File

@ -7,9 +7,13 @@ list( APPEND ${CALAMARESUI_LIBRARY_TARGET}_SOURCES
modulesystem/ViewModule.cpp
utils/CalamaresUtilsGui.cpp
utils/DebugWindow.cpp
utils/ImageRegistry.cpp
utils/YamlUtils.cpp
utils/qjsonmodel.cpp
utils/qjsonitem.cpp
viewpages/AbstractPage.cpp
viewpages/ViewStep.cpp
@ -24,7 +28,7 @@ list( APPEND ${CALAMARESUI_LIBRARY_TARGET}_SOURCES
)
list( APPEND ${CALAMARESUI_LIBRARY_TARGET}_UI
utils/DebugWindow.ui
)
if( WITH_PYTHON )

View File

@ -1,6 +1,6 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
* Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
*
* Calamares is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -213,6 +213,13 @@ Module::isLoaded() const
}
QVariantMap
Module::configurationMap()
{
return m_configurationMap;
}
Module::Module()
: m_loaded( false )
{}

View File

@ -1,6 +1,6 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2014, Teo Mrnjavac <teo@kde.org>
* Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
*
* Calamares is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -73,6 +73,8 @@ public:
virtual QList< job_ptr > jobs() const = 0;
QVariantMap configurationMap();
protected:
explicit Module();
virtual void initFrom( const YAML::Node& node );

View File

@ -32,11 +32,24 @@
namespace Calamares
{
ModuleManager* ModuleManager::s_instance = nullptr;
ModuleManager*
ModuleManager::instance()
{
return s_instance;
}
ModuleManager::ModuleManager( const QStringList& paths, QObject* parent )
: QObject( parent )
, m_paths( paths )
, m_lastPhaseLoaded( Phase_NULL )
{
Q_ASSERT( !s_instance );
s_instance = this;
}
ModuleManager::~ModuleManager()

View File

@ -38,6 +38,8 @@ public:
explicit ModuleManager( const QStringList& paths, QObject* parent = nullptr );
virtual ~ModuleManager();
static ModuleManager* instance();
/**
* @brief init goes through the module search directories and gets a list of
* modules available for loading, along with their metadata.
@ -64,10 +66,10 @@ private:
void checkDependencies();
QMap< QString, Module* > m_availableModules;
QStringList m_paths;
Phase m_lastPhaseLoaded;
static ModuleManager* s_instance;
};
}

View File

@ -0,0 +1,107 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2015, Teo Mrnjavac <teo@kde.org>
*
* Calamares 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.
*
* Calamares 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 Calamares. If not, see <http://www.gnu.org/licenses/>.
*/
#include "DebugWindow.h"
#include "utils/Retranslator.h"
#include "utils/qjsonmodel.h"
#include "JobQueue.h"
#include "Job.h"
#include "GlobalStorage.h"
#include "modulesystem/ModuleManager.h"
#include "modulesystem/Module.h"
#include <QJsonDocument>
#include <QSplitter>
#include <QStringListModel>
#include <QTreeView>
namespace Calamares {
DebugWindow::DebugWindow()
: QWidget( nullptr )
{
setupUi( this );
// GlobalStorage page
QJsonModel* jsonModel = new QJsonModel( this );
globalStorageView->setModel( jsonModel );
GlobalStorage* gs = JobQueue::instance()->globalStorage();
connect( gs, &GlobalStorage::changed, [ = ]
{
jsonModel->loadJson( QJsonDocument::fromVariant( gs->m ).toJson() );
globalStorageView->expandAll();
} );
jsonModel->loadJson( QJsonDocument::fromVariant( gs->m ).toJson() );
globalStorageView->expandAll();
// JobQueue page
jobQueueText->setReadOnly( true );
connect( JobQueue::instance(), &JobQueue::queueChanged,
[ this ]( const QList< Calamares::job_ptr >& jobs )
{
QStringList text;
foreach( auto job, jobs )
{
text.append( job->prettyName() );
}
jobQueueText->setText( text.join( '\n' ) );
} );
// Modules page
QSplitter* splitter = new QSplitter( modulesTab );
modulesTab->layout()->addWidget( splitter );
splitter->addWidget( modulesListView );
splitter->addWidget( moduleConfigView );
QStringListModel* modulesModel = new QStringListModel( ModuleManager::instance()->availableModules() );
modulesListView->setModel( modulesModel );
modulesListView->setSelectionMode( QAbstractItemView::SingleSelection );
QJsonModel* moduleConfigModel = new QJsonModel( this );
moduleConfigView->setModel( moduleConfigModel );
connect( modulesListView->selectionModel(), &QItemSelectionModel::selectionChanged,
[ this, moduleConfigModel ]
{
QString moduleName = modulesListView->currentIndex().data().toString();
Module* module = ModuleManager::instance()->module( moduleName );
if ( module )
{
moduleConfigModel->loadJson( QJsonDocument::fromVariant( module->configurationMap() ).toJson() );
moduleConfigView->expandAll();
}
} );
CALAMARES_RETRANSLATE(
retranslateUi( this );
setWindowTitle( tr( "Debug information" ) );
)
}
void
DebugWindow::closeEvent( QCloseEvent* e )
{
Q_UNUSED( e )
emit closed();
}
} // namespace Calamares

View File

@ -0,0 +1,45 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2015, Teo Mrnjavac <teo@kde.org>
*
* Calamares 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.
*
* Calamares 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 Calamares. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CALAMARES_DEBUGWINDOW_H
#define CALAMARES_DEBUGWINDOW_H
#include "ui_DebugWindow.h"
#include <QWidget>
namespace Calamares {
class DebugWindow : public QWidget, private Ui::DebugWindow
{
Q_OBJECT
public:
explicit DebugWindow();
signals:
void closed();
protected:
void closeEvent( QCloseEvent* e ) override;
};
} // namespace Calamares
#endif // CALAMARES_DEBUGWINDOW_H

View File

@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Calamares::DebugWindow</class>
<widget class="QWidget" name="Calamares::DebugWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>632</width>
<height>497</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="globalStorageTab">
<attribute name="title">
<string>GlobalStorage</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QTreeView" name="globalStorageView"/>
</item>
</layout>
</widget>
<widget class="QWidget" name="jobQueueTab">
<attribute name="title">
<string>JobQueue</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QTextEdit" name="jobQueueText"/>
</item>
</layout>
</widget>
<widget class="QWidget" name="modulesTab">
<attribute name="title">
<string>Modules</string>
</attribute>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QListView" name="modulesListView"/>
</item>
<item>
<widget class="QTreeView" name="moduleConfigView"/>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -0,0 +1,138 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Originally from QJsonModel <https://github.com/dridk/QJsonmodel>
* Copyright 2015, Sacha Schutz <sacha@labsquare.org>
*
* Calamares 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.
*
* Calamares 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 Calamares. 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

@ -0,0 +1,60 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Originally from QJsonModel <https://github.com/dridk/QJsonmodel>
* Copyright 2015, Sacha Schutz <sacha@labsquare.org>
*
* Calamares 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.
*
* Calamares 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 Calamares. If not, see <http://www.gnu.org/licenses/>.
*/
#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

@ -0,0 +1,172 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Originally from QJsonModel <https://github.com/dridk/QJsonmodel>
* Copyright 2015, Sacha Schutz <sacha@labsquare.org>
*
* Calamares 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.
*
* Calamares 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 Calamares. If not, see <http://www.gnu.org/licenses/>.
*/
#include "qjsonmodel.h"
#include <QFile>
#include <QDebug>
#include <QJsonDocument>
#include <QJsonObject>
#include <QIcon>
#include <QFont>
QJsonModel::QJsonModel(QObject *parent) :
QAbstractItemModel(parent)
{
mRootItem = new QJsonTreeItem;
mHeaders.append("key");
mHeaders.append("value");
}
bool QJsonModel::load(const QString &fileName)
{
QFile file(fileName);
bool success = false;
if (file.open(QIODevice::ReadOnly)) {
success = load(&file);
file.close();
}
else success = false;
return success;
}
bool QJsonModel::load(QIODevice *device)
{
return loadJson(device->readAll());
}
bool QJsonModel::loadJson(const QByteArray &json)
{
mDocument = QJsonDocument::fromJson(json);
if (!mDocument.isNull())
{
beginResetModel();
mRootItem = QJsonTreeItem::load(QJsonValue(mDocument.object()));
endResetModel();
return true;
}
return false;
}
QVariant QJsonModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
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)
return QString("%1").arg(item->key());
if (index.column() == 1)
return QString("%1").arg(item->value());
}
return QVariant();
}
QVariant QJsonModel::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 QJsonModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
QJsonTreeItem *parentItem;
if (!parent.isValid())
parentItem = mRootItem;
else
parentItem = static_cast<QJsonTreeItem*>(parent.internalPointer());
QJsonTreeItem *childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
QModelIndex QJsonModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
QJsonTreeItem *childItem = static_cast<QJsonTreeItem*>(index.internalPointer());
QJsonTreeItem *parentItem = childItem->parent();
if (parentItem == mRootItem)
return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
}
int QJsonModel::rowCount(const QModelIndex &parent) const
{
QJsonTreeItem *parentItem;
if (parent.column() > 0)
return 0;
if (!parent.isValid())
parentItem = mRootItem;
else
parentItem = static_cast<QJsonTreeItem*>(parent.internalPointer());
return parentItem->childCount();
}
int QJsonModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return 2;
}
void QJsonModel::setIcon(const QJsonValue::Type &type, const QIcon &icon)
{
mTypeIcons.insert(type,icon);
}

View File

@ -0,0 +1,56 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Originally from QJsonModel <https://github.com/dridk/QJsonmodel>
* Copyright 2015, Sacha Schutz <sacha@labsquare.org>
*
* Calamares 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.
*
* Calamares 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 Calamares. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef QJSONMODEL_H
#define QJSONMODEL_H
#include <QAbstractItemModel>
#include "qjsonitem.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QIcon>
class QJsonModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit QJsonModel(QObject *parent = 0);
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);
private:
QJsonTreeItem * mRootItem;
QJsonDocument mDocument;
QStringList mHeaders;
QHash<QJsonValue::Type, QIcon> mTypeIcons;
};
#endif // QJSONMODEL_H