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){
53 newItemCat = new QTreeWidgetItem(itemParent);
54 newItemCat->setData(0,Qt::DisplayRole, cat->getName());
55 QList<ReferenceBlock*> list = cat->getBlocks();
56 for(int i=0; i<list.length(); i++){
57 newItemBlock = new QTreeWidgetItem(newItemCat);
58 newItemBlock->setData(0,Qt::DisplayRole, list.at(i)->getName());
59 newItemBlock->setData(1,Qt::DisplayRole, cat->getId());
60 newItemBlock->setData(2,Qt::DisplayRole, i);
61 newItemBlock->setIcon(0,QIcon("icons/window_new.png"));
63 addChild(cat,newItemCat);
66 - getting the childs of catParent
67 - for each BlockCategory cat of that list :
68 - create an new TreeWidgetItem (newImteCat), with itemParent as a parent
69 - set the first column of that item with the categry name
70 - get the list of the blocks that are associated with cat
71 - for i = 0 to that list size.
72 - create a new TreeWidgetItem (newItemBlock), with newItemCat as a parent
73 - set the first column of that item with the block name
74 - set the second column of that item with the newItemCat id
75 - set the third column of that item with the value of i
77 - call again addChild with cat and newImteCat as parameters
83 void BlockLibraryWidget::addClicked() {
85 QTreeWidgetItem *item = tree->selectedItems().at(0);
86 if(item->data(1,Qt::DisplayRole).isValid() && item->data(2,Qt::DisplayRole).isValid()){
87 int idParent = item->data(1,Qt::DisplayRole).toInt();
88 int idBlock = item->data(2,Qt::DisplayRole).toInt();
89 QVariant v = comboScenes->currentData();
90 cout << "adding block to scene " << v.toInt() << endl;
91 dispatcher->addBlock(idParent, idBlock, v.toInt());
94 // only take the first selected
95 // retrieve id of category and id of block
96 // calling dispatcher addBlock() method.
99 void BlockLibraryWidget::clicked() {
100 if(tree->selectedItems().length() > 0){
101 QTreeWidgetItem *item = tree->selectedItems().at(0);
102 if(item->data(1,Qt::DisplayRole).isValid())
103 buttonAdd->setEnabled(true);
105 buttonAdd->setEnabled(false);
109 void BlockLibraryWidget::doubleClicked() {
113 void BlockLibraryWidget::updateComboScene() {
114 comboScenes->clear();
115 QMap<int,QString> list = dispatcher->getAllGroupNames();
116 QMapIterator<int,QString> iter(list);
117 while (iter.hasNext()) {
119 comboScenes->addItem(iter.value(),QVariant(iter.key()));