Skip to content

Commit

Permalink
Update chigraph, add module selection dialog, and module tree model
Browse files Browse the repository at this point in the history
  • Loading branch information
russelltg committed May 7, 2017
1 parent 9e49d38 commit bd368d0
Show file tree
Hide file tree
Showing 8 changed files with 426 additions and 227 deletions.
2 changes: 1 addition & 1 deletion chigraph
12 changes: 8 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH} ${ECM_KDE_MODULE_DIR} ${CMAKE_CURRENT_S

set(CMAKE_AUTORCC ON)

# include(KDEInstallDirs)
# include(KDECMakeSettings)
# include(KDECompilerSettings)
# include(FeatureSummary)
include(KDEInstallDirs)
include(KDECMakeSettings)
#include(KDECompilerSettings)
include(FeatureSummary)

# find qt
find_package(Qt5 5.6.0 REQUIRED COMPONENTS Gui Script Xml Core Widgets Network PrintSupport)
Expand Down Expand Up @@ -76,6 +76,8 @@ set(GUI_SRCS
launchconfigurationmanager.cpp
launchconfigurationdialog.cpp
structedit.cpp
moduletreemodel.cpp
moduleselectiondialog.cpp
)

set(GUI_RESOURCES
Expand All @@ -99,6 +101,8 @@ set(GUI_HEADERS
launchconfigurationmanager.hpp
launchconfigurationdialog.hpp
structedit.hpp
moduletreemodel.hpp
moduleselectiondialog.hpp
)

add_executable(chigraphgui ${GUI_SRCS} ${GUI_HEADERS} ${GUI_RESOURCES})
Expand Down
4 changes: 1 addition & 3 deletions src/chigraphnodemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ class EditCodeDialog : public QDialog {
KTextEditor::Document* doc = editor->createDocument(this);
doc->setText(QString::fromStdString(inst->type().toJSON()["code"]));
doc->setHighlightingMode("C");
connect(doc, &KTextEditor::Document::highlightingModeChanged, this, [](auto* doc) {
std::cout << "Changed to " << doc->highlightingMode().toStdString() << std::endl;
});

// create a widget to display the document
KTextEditor::View* textEdit = doc->createView(nullptr);
// delete save and saveAs actions
Expand Down
222 changes: 3 additions & 219 deletions src/modulebrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,177 +13,12 @@
#include <KActionCollection>

#include "mainwindow.hpp"
#include "moduletreemodel.hpp"

#include <boost/filesystem.hpp>

namespace fs = boost::filesystem;

struct WorkspaceTree {

enum eType {
FUNCTION,
MODULE,
STRUCT,
FOLDER
};


WorkspaceTree* parent = nullptr;
std::vector<std::unique_ptr<WorkspaceTree>> children;
chi::GraphModule* module = nullptr;
chi::GraphFunction* func = nullptr;
chi::GraphStruct* str = nullptr;
QString name;
bool dirty = false;
int row = 0;
eType type;

};

class ModuleTreeModel : public QAbstractItemModel {
public:
ModuleTreeModel(std::unique_ptr<WorkspaceTree> t, chi::Context& ctx)
: tree{std::move(t)}, mCtx{&ctx} {}

int columnCount(const QModelIndex& parent) const override { return 1; }
QModelIndex index(int row, int column, const QModelIndex& parent) const override {
if (!hasIndex(row, column, parent)) { return {}; }

WorkspaceTree* parentItem;
if (parent.isValid()) {
parentItem = static_cast<WorkspaceTree*>(parent.internalPointer());
} else {
parentItem = tree.get();
}

if (row < parentItem->children.size()) {
return createIndex(row, column, parentItem->children[row].get());
}
return {};
}
QModelIndex parent(const QModelIndex& index) const override {
if (!index.isValid()) { return {}; }

auto childItem = static_cast<WorkspaceTree*>(index.internalPointer());
auto parentItem = childItem->parent;

if (parentItem == nullptr) return {};

return createIndex(parentItem->row, 0, parentItem);
}
bool hasChildren(const QModelIndex& index) const override {
if (!index.isValid()) { return true; }

auto item = static_cast<WorkspaceTree*>(index.internalPointer());
return item->type == WorkspaceTree::MODULE || item->type == WorkspaceTree::FOLDER;
}
bool canFetchMore(const QModelIndex& index) const override {
if (!index.isValid()) { return false; }

auto item = static_cast<WorkspaceTree*>(index.internalPointer());

return item->type == WorkspaceTree::MODULE;
}
void fetchMore(const QModelIndex& index) override {
if (!index.isValid()) { return; }

auto item = static_cast<WorkspaceTree*>(index.internalPointer());

if (item->module != nullptr) {
// it's already been fetched
return;
}

// get the name
fs::path p;
{
auto parent = item;
while (parent != nullptr) {
p = parent->name.toStdString() / p;
parent = parent->parent;
}
p = p.string().substr(4);
}

// load it
chi::ChiModule* mod;
chi::Result res = mCtx->loadModule(p, chi::LoadSettings::Default, &mod);
if (!res) {
KMessageBox::detailedError(MainWindow::instance(),
R"(Failed to load JsonModule from file ")" +
QString::fromStdString(p.string()) + R"(")",
QString::fromStdString(res.dump()), "Error Loading");

return;
}

item->module = static_cast<chi::GraphModule*>(mod);

// add functions
for (const auto& func : item->module->functions()) {
auto child = std::make_unique<WorkspaceTree>();
child->func = func.get();
child->parent = item;
child->name = QString::fromStdString(func->name());
child->row = item->children.size();
child->type = WorkspaceTree::FUNCTION;

item->children.push_back(std::move(child));
}

// add structs
for (const auto& str : item->module->structs()) {
auto child = std::make_unique<WorkspaceTree>();
child->str = str.get();
child->name = QString::fromStdString(str->name());
child->parent = item;
child->row = item->children.size();
child->type = WorkspaceTree::STRUCT;

item->children.push_back(std::move(child));
}
}
int rowCount(const QModelIndex& index) const override {
WorkspaceTree* parentItem;
if (index.isValid()) {
parentItem = static_cast<WorkspaceTree*>(index.internalPointer());
} else {
parentItem = tree.get();
}

return parentItem->children.size();
}
QVariant data(const QModelIndex& index, int role) const override {
if (!index.isValid()) { return {}; }

auto item = static_cast<WorkspaceTree*>(index.internalPointer());

switch (role) {
case Qt::DisplayRole:
if (item->dirty) {
return "* " + item->name;
} else {
return item->name;
}
case Qt::DecorationRole:
switch (item->type) {
case WorkspaceTree::MODULE: return QIcon::fromTheme(QStringLiteral("package-available"));
case WorkspaceTree::FUNCTION: return QIcon::fromTheme(QStringLiteral("code-context"));
case WorkspaceTree::STRUCT: return QIcon::fromTheme(QStringLiteral("code-class"));
default: return {};
}
case Qt::FontRole:
if (item->dirty || (item->parent != nullptr && item->parent->dirty)) {
QFont bold;
bold.setBold(true);
return bold;
}
default: return {};
}
}
std::unique_ptr<WorkspaceTree> tree;
chi::Context* mCtx;
};

ModuleBrowser::ModuleBrowser(QWidget* parent) : QTreeView(parent) {
setXMLFile("chigraphmodulebrowserui.rc");
Expand All @@ -203,11 +38,6 @@ ModuleBrowser::ModuleBrowser(QWidget* parent) : QTreeView(parent) {
return;
default: return;
}

if (item->func == nullptr) { // don't do module folders or modules
return;
}
emit functionSelected(*item->func);
});
setContextMenuPolicy(Qt::CustomContextMenu);

Expand All @@ -233,56 +63,10 @@ std::unordered_set<chi::GraphModule*> ModuleBrowser::dirtyModules() { return mDi

void ModuleBrowser::loadWorkspace(chi::Context& context) {
mContext = &context;

auto modules = context.listModulesInWorkspace();

auto tree = std::make_unique<WorkspaceTree>();

// add src object
auto srcTree = std::make_unique<WorkspaceTree>();
srcTree->parent = tree.get();
srcTree->type = WorkspaceTree::FOLDER;
srcTree->row = 0;
srcTree->name = QStringLiteral("src");
mModel = ModuleTreeModel::createFromContext(context);
mTree = mModel->tree.get();


// create the tree
for (const fs::path& mod : modules) {
WorkspaceTree* parent = srcTree.get();

// for each component of mod
for (auto componentIter = mod.begin(); componentIter != mod.end(); ++componentIter) {
fs::path component = *componentIter;
bool isModule = componentIter == --mod.end();

// make sure it exists
bool found = false;
for (const auto& child : parent->children) {
if (child->name.toStdString() == component.string() &&
(child->type == WorkspaceTree::MODULE) == isModule) {
found = true;
parent = child.get();
break;
}
}
if (!found) {
// insert it
auto newChild = std::make_unique<WorkspaceTree>();
newChild->parent = parent;
newChild->type = isModule ? WorkspaceTree::MODULE : WorkspaceTree::FOLDER;
newChild->row = parent->children.size();
newChild->name = QString::fromStdString(component.string());
parent->children.push_back(std::move(newChild));

parent = parent->children[parent->children.size() - 1].get();
}
}
}

tree->children.push_back(std::move(srcTree));
mTree = tree.get();

mModel = std::make_unique<ModuleTreeModel>(std::move(tree), *mContext);
setModel(mModel.get());
}

Expand Down
60 changes: 60 additions & 0 deletions src/moduleselectiondialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "moduleselectiondialog.hpp"

#include <QVBoxLayout>
#include <QDialogButtonBox>
#include <QHeaderView>
#include <QTreeView>

#include <cassert>

ModuleSelectionDialog::ModuleSelectionDialog(chi::Context& ctx, boost::filesystem::path* toFill, QWidget* parent) : QDialog{parent}
{
assert(toFill != nullptr);

auto layout = new QVBoxLayout;
setLayout(layout);

auto treeView = new QTreeView;
layout->addWidget(treeView);

// get model and set it
mModel = ModuleTreeModel::createFromContext(ctx, ModuleTreeModel::Modules);
treeView->setModel(mModel.get());

treeView->setAnimated(true);
treeView->setSortingEnabled(true);
treeView->header()->close();

auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel);
connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);

layout->addWidget(buttonBox);


connect(treeView, &QTreeView::doubleClicked, this, [toFill, this](const QModelIndex& index) {
if (!index.isValid()) {
return;
}

auto item = static_cast<WorkspaceTree*>(index.internalPointer());

if (item->type == WorkspaceTree::MODULE) {
*toFill = item->fullName();
accept();
}

});
}


boost::filesystem::path ModuleSelectionDialog::getModule(QWidget* parent, chi::Context& ctx)
{
boost::filesystem::path ret;
auto dialog = new ModuleSelectionDialog(ctx, &ret, parent);

dialog->exec();


return ret;
}

22 changes: 22 additions & 0 deletions src/moduleselectiondialog.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#ifndef CHIGRAPHGUI_MODULE_SELECTION_DIALOG_HPP
#define CHIGRAPHGUI_MODULE_SELECTION_DIALOG_HPP

#include <QDialog>

#include "moduletreemodel.hpp"

#include <boost/filesystem/path.hpp>

class ModuleSelectionDialog : public QDialog {
public:
ModuleSelectionDialog(chi::Context& ctx, boost::filesystem::path* toFill, QWidget* parent = nullptr);

static boost::filesystem::path getModule(QWidget* parent, chi::Context& ctx);

private:
std::unique_ptr<ModuleTreeModel> mModel;
};

#endif // CHIGRAPHGUI_MODULE_SELECTION_DIALOG_HPP
Loading

0 comments on commit bd368d0

Please sign in to comment.