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 layout = new QBoxLayout(QBoxLayout::TopToBottom, this);
14 tree = new QTreeWidget(this);
15 buttonAdd = new QPushButton("add", this);
16 buttonAdd->setEnabled(false);
18 connect(tree, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(clicked()));
19 connect(tree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), this, SLOT(doubleClicked()));
20 connect(buttonAdd, SIGNAL(clicked()), this, SLOT(addClicked()));
22 BlockCategory* cat = params->categoryTree->searchCategory(0);
24 QTreeWidgetItem* item = tree->invisibleRootItem();
25 tree->setHeaderLabel("Blocks list");
30 layout->addWidget(tree);
31 layout->addWidget(buttonAdd);
32 this->setLayout(layout);
34 this->setFixedSize(300,230);
38 BlockLibraryWidget::~BlockLibraryWidget() {
41 void BlockLibraryWidget::addChild(BlockCategory *catParent,QTreeWidgetItem* itemParent) {
43 QTreeWidgetItem* newItemCat = NULL;
44 QTreeWidgetItem* newItemBlock = NULL;
46 QList<BlockCategory *> childs = catParent->getAllChilds();
47 foreach(BlockCategory* cat, childs){
48 newItemCat = new QTreeWidgetItem(itemParent);
49 newItemCat->setData(0,Qt::DisplayRole, cat->getName());
50 QList<ReferenceBlock*> list = cat->getBlocks();
51 for(int i=0; i<list.length(); i++){
52 newItemBlock = new QTreeWidgetItem(newItemCat);
53 newItemBlock->setData(0,Qt::DisplayRole, list.at(i)->getName());
54 newItemBlock->setData(1,Qt::DisplayRole, cat->getId());
55 newItemBlock->setData(2,Qt::DisplayRole, i);
56 newItemBlock->setIcon(0,QIcon("icons/window_new.png"));
58 addChild(cat,newItemCat);
61 - getting the childs of catParent
62 - for each BlockCategory cat of that list :
63 - create an new TreeWidgetItem (newImteCat), with itemParent as a parent
64 - set the first column of that item with the categry name
65 - get the list of the blocks that are associated with cat
66 - for i = 0 to that list size.
67 - create a new TreeWidgetItem (newItemBlock), with newItemCat as a parent
68 - set the first column of that item with the block name
69 - set the second column of that item with the newItemCat id
70 - set the third column of that item with the value of i
72 - call again addChild with cat and newImteCat as parameters
78 void BlockLibraryWidget::addClicked() {
80 QTreeWidgetItem *item = tree->selectedItems().at(0);
81 if(item->data(1,Qt::DisplayRole).isValid() && item->data(2,Qt::DisplayRole).isValid()){
82 int idParent = item->data(1,Qt::DisplayRole).toInt();
83 int idBlock = item->data(2,Qt::DisplayRole).toInt();
84 dispatcher->addBlock(idParent, idBlock);
87 // only take the first selected
88 // retrieve id of category and id of block
89 // calling dispatcher addBlock() method.
93 void BlockLibraryWidget::clicked()
95 if(tree->selectedItems().length() > 0){
96 QTreeWidgetItem *item = tree->selectedItems().at(0);
97 if(item->data(1,Qt::DisplayRole).isValid())
98 buttonAdd->setEnabled(true);
100 buttonAdd->setEnabled(false);
104 void BlockLibraryWidget::doubleClicked()