1 #include "BlockLibraryWidget.h"
2 #include "BlockLibraryTree.h"
4 BlockLibraryWidget::BlockLibraryWidget(Dispatcher* _dispatcher,
6 QWidget *parent) : QWidget(parent) {
9 dispatcher = _dispatcher;
12 // creating the widget : tree, buttons, ...
13 QBoxLayout* layout = new QBoxLayout(QBoxLayout::TopToBottom, this);
14 tree = new QTreeWidget(this);
16 buttonAdd = new QPushButton("add to", this);
17 buttonAdd->setEnabled(false);
18 comboScenes = new QComboBox();
20 QHBoxLayout* layBottom = new QHBoxLayout;
21 layBottom->addWidget(buttonAdd);
22 layBottom->addWidget(comboScenes);
24 connect(tree, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(clicked()));
25 connect(tree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), this, SLOT(doubleClicked()));
26 connect(buttonAdd, SIGNAL(clicked()), this, SLOT(addClicked()));
28 BlockCategory* cat = params->categoryTree->searchCategory(0);
30 QTreeWidgetItem* item = tree->invisibleRootItem();
31 tree->setHeaderLabel("Blocks list");
35 layout->addWidget(tree);
36 layout->addLayout(layBottom);
37 this->setLayout(layout);
39 this->setFixedSize(300,230);
43 BlockLibraryWidget::~BlockLibraryWidget() {
46 void BlockLibraryWidget::addChild(BlockCategory *catParent,QTreeWidgetItem* itemParent) {
48 QTreeWidgetItem* newItemCat = NULL;
49 QTreeWidgetItem* newItemBlock = NULL;
51 QList<BlockCategory *> childs = catParent->getAllChilds();
52 foreach(BlockCategory* cat, childs){
54 if (cat->getId()<100) {
55 newItemCat = new QTreeWidgetItem(itemParent);
56 newItemCat->setData(0,Qt::DisplayRole, cat->getName());
57 QList<ReferenceBlock*> list = cat->getBlocks();
58 for(int i=0; i<list.length(); i++){
59 newItemBlock = new QTreeWidgetItem(newItemCat);
60 newItemBlock->setData(0,Qt::DisplayRole, list.at(i)->getName());
61 newItemBlock->setData(1,Qt::DisplayRole, cat->getId());
62 newItemBlock->setData(2,Qt::DisplayRole, i);
63 newItemBlock->setIcon(0,QIcon("icons/window_new.png"));
65 addChild(cat,newItemCat);
69 - getting the childs of catParent
70 - for each BlockCategory cat of that list :
71 - create an new TreeWidgetItem (newImteCat), with itemParent as a parent
72 - set the first column of that item with the categry name
73 - get the list of the blocks that are associated with cat
74 - for i = 0 to that list size.
75 - create a new TreeWidgetItem (newItemBlock), with newItemCat as a parent
76 - set the first column of that item with the block name
77 - set the second column of that item with the newItemCat id
78 - set the third column of that item with the value of i
80 - call again addChild with cat and newImteCat as parameters
86 void BlockLibraryWidget::addClicked() {
88 QTreeWidgetItem *item = tree->selectedItems().at(0);
89 if(item->data(1,Qt::DisplayRole).isValid() && item->data(2,Qt::DisplayRole).isValid()){
90 int idParent = item->data(1,Qt::DisplayRole).toInt();
91 int idBlock = item->data(2,Qt::DisplayRole).toInt();
92 QVariant v = comboScenes->currentData();
93 cout << "adding block to scene " << v.toInt() << endl;
94 dispatcher->addBlock(idParent, idBlock, v.toInt());
97 // only take the first selected
98 // retrieve id of category and id of block
99 // calling dispatcher addBlock() method.
102 void BlockLibraryWidget::clicked() {
103 if(tree->selectedItems().length() > 0){
104 QTreeWidgetItem *item = tree->selectedItems().at(0);
105 if(item->data(1,Qt::DisplayRole).isValid())
106 buttonAdd->setEnabled(true);
108 buttonAdd->setEnabled(false);
112 void BlockLibraryWidget::doubleClicked() {
116 void BlockLibraryWidget::updateComboScene() {
117 comboScenes->clear();
118 QMap<int,QString> list = dispatcher->getAllGroupNames();
119 QMapIterator<int,QString> iter(list);
120 while (iter.hasNext()) {
122 comboScenes->addItem(iter.value(),QVariant(iter.key()));