diff --git a/src/calamares/CalamaresWindow.cpp b/src/calamares/CalamaresWindow.cpp index 46082747d..c0563bf45 100644 --- a/src/calamares/CalamaresWindow.cpp +++ b/src/calamares/CalamaresWindow.cpp @@ -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 @@ -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 ); diff --git a/src/calamares/CalamaresWindow.h b/src/calamares/CalamaresWindow.h index 8fbc106aa..6ea9602a5 100644 --- a/src/calamares/CalamaresWindow.h +++ b/src/calamares/CalamaresWindow.h @@ -1,6 +1,6 @@ /* === This file is part of Calamares - === * - * Copyright 2014, Teo Mrnjavac + * Copyright 2014-2015, Teo Mrnjavac * * 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 #include +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 diff --git a/src/libcalamares/GlobalStorage.cpp b/src/libcalamares/GlobalStorage.cpp index fd4d10c53..f24d83a1c 100644 --- a/src/libcalamares/GlobalStorage.cpp +++ b/src/libcalamares/GlobalStorage.cpp @@ -1,6 +1,6 @@ /* === This file is part of Calamares - === * - * Copyright 2014, Teo Mrnjavac + * Copyright 2014-2015, Teo Mrnjavac * * 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 diff --git a/src/libcalamares/GlobalStorage.h b/src/libcalamares/GlobalStorage.h index 9e72e3c76..20af3978c 100644 --- a/src/libcalamares/GlobalStorage.h +++ b/src/libcalamares/GlobalStorage.h @@ -1,6 +1,6 @@ /* === This file is part of Calamares - === * - * Copyright 2014, Teo Mrnjavac + * Copyright 2014-2015, Teo Mrnjavac * * 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 diff --git a/src/libcalamares/JobQueue.cpp b/src/libcalamares/JobQueue.cpp index d2fd3b98c..6b31ec78d 100644 --- a/src/libcalamares/JobQueue.cpp +++ b/src/libcalamares/JobQueue.cpp @@ -1,6 +1,6 @@ /* === This file is part of Calamares - === * - * Copyright 2014, Teo Mrnjavac + * Copyright 2014-2015, Teo Mrnjavac * * 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 diff --git a/src/libcalamares/JobQueue.h b/src/libcalamares/JobQueue.h index d39929ebb..6e04e9464 100644 --- a/src/libcalamares/JobQueue.h +++ b/src/libcalamares/JobQueue.h @@ -1,6 +1,6 @@ /* === This file is part of Calamares - === * - * Copyright 2014, Teo Mrnjavac + * Copyright 2014-2015, Teo Mrnjavac * * 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 ); diff --git a/src/libcalamares/PythonJob.cpp b/src/libcalamares/PythonJob.cpp index 07283a469..0a2a4bd4f 100644 --- a/src/libcalamares/PythonJob.cpp +++ b/src/libcalamares/PythonJob.cpp @@ -1,6 +1,6 @@ /* === This file is part of Calamares - === * - * Copyright 2014, Teo Mrnjavac + * Copyright 2014-2015, Teo Mrnjavac * * 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, diff --git a/src/libcalamaresui/CMakeLists.txt b/src/libcalamaresui/CMakeLists.txt index 330e6511b..df36f3b0d 100644 --- a/src/libcalamaresui/CMakeLists.txt +++ b/src/libcalamaresui/CMakeLists.txt @@ -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 ) diff --git a/src/libcalamaresui/modulesystem/Module.cpp b/src/libcalamaresui/modulesystem/Module.cpp index 9d161eb17..9ee34757c 100644 --- a/src/libcalamaresui/modulesystem/Module.cpp +++ b/src/libcalamaresui/modulesystem/Module.cpp @@ -1,6 +1,6 @@ /* === This file is part of Calamares - === * - * Copyright 2014, Teo Mrnjavac + * Copyright 2014-2015, Teo Mrnjavac * * 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 ) {} diff --git a/src/libcalamaresui/modulesystem/Module.h b/src/libcalamaresui/modulesystem/Module.h index 3c5f79f85..61888ca23 100644 --- a/src/libcalamaresui/modulesystem/Module.h +++ b/src/libcalamaresui/modulesystem/Module.h @@ -1,6 +1,6 @@ /* === This file is part of Calamares - === * - * Copyright 2014, Teo Mrnjavac + * Copyright 2014-2015, Teo Mrnjavac * * 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 ); diff --git a/src/libcalamaresui/modulesystem/ModuleManager.cpp b/src/libcalamaresui/modulesystem/ModuleManager.cpp index b20a1b3ad..57ba12db8 100644 --- a/src/libcalamaresui/modulesystem/ModuleManager.cpp +++ b/src/libcalamaresui/modulesystem/ModuleManager.cpp @@ -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() diff --git a/src/libcalamaresui/modulesystem/ModuleManager.h b/src/libcalamaresui/modulesystem/ModuleManager.h index 2701c9c08..d2e36faea 100644 --- a/src/libcalamaresui/modulesystem/ModuleManager.h +++ b/src/libcalamaresui/modulesystem/ModuleManager.h @@ -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; }; } diff --git a/src/libcalamaresui/utils/DebugWindow.cpp b/src/libcalamaresui/utils/DebugWindow.cpp new file mode 100644 index 000000000..d94a53947 --- /dev/null +++ b/src/libcalamaresui/utils/DebugWindow.cpp @@ -0,0 +1,107 @@ +/* === This file is part of Calamares - === + * + * Copyright 2015, Teo Mrnjavac + * + * 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 . + */ + +#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 +#include +#include +#include + +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 diff --git a/src/libcalamaresui/utils/DebugWindow.h b/src/libcalamaresui/utils/DebugWindow.h new file mode 100644 index 000000000..ee061991e --- /dev/null +++ b/src/libcalamaresui/utils/DebugWindow.h @@ -0,0 +1,45 @@ +/* === This file is part of Calamares - === + * + * Copyright 2015, Teo Mrnjavac + * + * 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 . + */ + +#ifndef CALAMARES_DEBUGWINDOW_H +#define CALAMARES_DEBUGWINDOW_H + +#include "ui_DebugWindow.h" + +#include + +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 diff --git a/src/libcalamaresui/utils/DebugWindow.ui b/src/libcalamaresui/utils/DebugWindow.ui new file mode 100644 index 000000000..a445e8ad1 --- /dev/null +++ b/src/libcalamaresui/utils/DebugWindow.ui @@ -0,0 +1,61 @@ + + + Calamares::DebugWindow + + + + 0 + 0 + 632 + 497 + + + + Form + + + + + + 0 + + + + GlobalStorage + + + + + + + + + + JobQueue + + + + + + + + + + Modules + + + + + + + + + + + + + + + + + diff --git a/src/libcalamaresui/utils/qjsonitem.cpp b/src/libcalamaresui/utils/qjsonitem.cpp new file mode 100644 index 000000000..76e17f550 --- /dev/null +++ b/src/libcalamaresui/utils/qjsonitem.cpp @@ -0,0 +1,138 @@ +/* === This file is part of Calamares - === + * + * Originally from QJsonModel + * Copyright 2015, Sacha Schutz + * + * 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 . + */ + + +#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(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; +} + diff --git a/src/libcalamaresui/utils/qjsonitem.h b/src/libcalamaresui/utils/qjsonitem.h new file mode 100644 index 000000000..260281236 --- /dev/null +++ b/src/libcalamaresui/utils/qjsonitem.h @@ -0,0 +1,60 @@ +/* === This file is part of Calamares - === + * + * Originally from QJsonModel + * Copyright 2015, Sacha Schutz + * + * 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 . + */ + +#ifndef JSONITEM_H +#define JSONITEM_H +#include +#include +#include +#include +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 mChilds; + QJsonTreeItem * mParent; + + +}; + +#endif // JSONITEM_H diff --git a/src/libcalamaresui/utils/qjsonmodel.cpp b/src/libcalamaresui/utils/qjsonmodel.cpp new file mode 100644 index 000000000..4162e819e --- /dev/null +++ b/src/libcalamaresui/utils/qjsonmodel.cpp @@ -0,0 +1,172 @@ +/* === This file is part of Calamares - === + * + * Originally from QJsonModel + * Copyright 2015, Sacha Schutz + * + * 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 . + */ + + +#include "qjsonmodel.h" +#include +#include +#include +#include +#include +#include + +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(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(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(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(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); +} diff --git a/src/libcalamaresui/utils/qjsonmodel.h b/src/libcalamaresui/utils/qjsonmodel.h new file mode 100644 index 000000000..615c37020 --- /dev/null +++ b/src/libcalamaresui/utils/qjsonmodel.h @@ -0,0 +1,56 @@ +/* === This file is part of Calamares - === + * + * Originally from QJsonModel + * Copyright 2015, Sacha Schutz + * + * 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 . + */ + + +#ifndef QJSONMODEL_H +#define QJSONMODEL_H + +#include +#include "qjsonitem.h" +#include +#include +#include +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 mTypeIcons; + + +}; + +#endif // QJSONMODEL_H