forked from bobrnor/ontolis
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathontoliscategorywidget.cpp
More file actions
101 lines (82 loc) · 3.61 KB
/
ontoliscategorywidget.cpp
File metadata and controls
101 lines (82 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "ontoliscategorywidget.h"
#include "ui_ontoliscategorywidget.h"
#include "widgets/OntologyGraph/OLSOntologyGraphNodeItem.h"
#include "widgets/OntologyGraph/OLSOntologyGraphRelationItem.h"
OntolisCategoryWidget::OntolisCategoryWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::OntolisCategoryWidget)
{
ui->setupUi(this);
m_widget = NULL;
connect(ui->saveButton, SIGNAL(clicked()), SLOT(saveSlot()));
connect(ui->cancelButton, SIGNAL(clicked()), SLOT(cancelSlot()));
connect(ui->categoryComboBox, SIGNAL(currentIndexChanged(int)), SLOT(currentIndexChangedSlot(int)));
}
OntolisCategoryWidget::~OntolisCategoryWidget()
{
delete ui;
}
void OntolisCategoryWidget::setWidget(OLSOntologyGraphWidget *widget) {
if (m_widget) {
disconnect(m_widget->m_ontologyView->scene());
}
m_widget = widget;
connect(m_widget->m_ontologyView->scene(), SIGNAL(selectionChanged()), SLOT(selectionChanged()));
update();
}
void OntolisCategoryWidget::update() {
ui->categoryComboBox->clear();
ui->categoryTextEdit->clear();
foreach (QString categoryName, m_widget->dataController()->categoryNames()) {
ui->categoryComboBox->addItem(categoryName);
}
}
void OntolisCategoryWidget::saveSlot() {
qDebug() << "saved";
QJsonDocument doc = QJsonDocument::fromJson(ui->categoryTextEdit->toPlainText().toLocal8Bit());
if (m_widget->m_ontologyView->scene()->selectedItems().length() > 0) {
foreach (QGraphicsItem *item, m_widget->m_ontologyView->scene()->selectedItems()) {
if (item->data(OLSOntologyGraphItemDataKey::kType) == OLSOntologyGraphItemType::kNode) {
OLSOntologyGraphNodeItem *nodeItem = static_cast<OLSOntologyGraphNodeItem *>(item);
m_widget->dataController()->setCategory(nodeItem->name(), doc.object().toVariantMap());
nodeItem->attributesChanged();
}
else if (item->data(OLSOntologyGraphItemDataKey::kType) == OLSOntologyGraphItemType::kRelation) {
OLSOntologyGraphRelationItem *relationItem = static_cast<OLSOntologyGraphRelationItem *>(item);
QVariantMap map = QVariantMap();
map.insert("gui-attributes", doc.object().toVariantMap());
relationItem->setAttributes(map);
}
}
}
else {
m_widget->dataController()->setCategory(ui->categoryComboBox->currentText(), doc.object().toVariantMap());
foreach (QGraphicsItem *item, m_widget->m_ontologyView->scene()->items()) {
if (item->data(OLSOntologyGraphItemDataKey::kType) == OLSOntologyGraphItemType::kRelation) {
OLSOntologyGraphRelationItem *relationItem = static_cast<OLSOntologyGraphRelationItem *>(item);
if (relationItem->name() == ui->categoryComboBox->currentText()) {
relationItem->attributesChanged();
}
}
}
}
}
void OntolisCategoryWidget::cancelSlot() {
qDebug() << "canceled";
currentIndexChangedSlot(ui->categoryComboBox->currentIndex());
}
void OntolisCategoryWidget::currentIndexChangedSlot(int index) {
qDebug() << "changed";
QVariantMap attrs = m_widget->dataController()->getCategory(ui->categoryComboBox->currentText());
QJsonObject object = QJsonObject::fromVariantMap(attrs);
QJsonDocument document = QJsonDocument(object);
ui->categoryTextEdit->setPlainText(QString::fromUtf8(document.toJson()));
}
void OntolisCategoryWidget::selectionChanged() {
if (m_widget->m_ontologyView->scene()->selectedItems().length() > 0) {
ui->categoryComboBox->setEditText("");
}
else {
update();
}
}