1 #include "BlockLibraryWidget.h"
2 #include "BlockLibraryTree.h"
5 BlockLibraryWidget::BlockLibraryWidget(Dispatcher* _dispatcher,
7 QWidget *parent) : QWidget(parent) {
10 dispatcher = _dispatcher;
17 currentRefBlock = NULL;
19 // creating the widget : tree, buttons, ...
20 QBoxLayout* layout = new QBoxLayout(QBoxLayout::TopToBottom, this);
21 tree = new QTreeWidget(this);
23 buttonAdd = new QPushButton("add to", this);
24 buttonAdd->setEnabled(false);
25 comboScenes = new QComboBox();
27 QHBoxLayout* layAdd = new QHBoxLayout;
28 layAdd->addWidget(buttonAdd);
29 layAdd->addWidget(comboScenes);
31 boxClkRst = new QGroupBox("Clock/reset connection");
34 QString msg = "Connect to main clock/rst ext_clk/reset_0 (";
35 msg += QString::number(params->getGraph()->getClock(0));
37 radAutoClk = new QRadioButton(msg);
38 radChooseClk = new QRadioButton("Choose among available clk/rst");
39 if (params->autoConnMainClk) {
40 radAutoClk->setChecked(true);
43 radChooseClk->setChecked(true);
46 layClkRst = new QGridLayout;
48 QVBoxLayout* layConn = new QVBoxLayout;
49 layConn->addWidget(radAutoClk,0,0);
50 layConn->addWidget(radChooseClk,1,0);
51 stack = new QStackedWidget();
52 QLabel* labDummy = new QLabel("");
53 stack->addWidget(labDummy);
54 QWidget* w = new QWidget();
55 w->setLayout(layClkRst);
57 layConn->addWidget(stack);
59 boxClkRst->setLayout(layConn);
61 QVBoxLayout* layBottom = new QVBoxLayout;
62 layBottom->addLayout(layAdd);
63 layBottom->addWidget(boxClkRst);
65 connect(tree, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(clicked()));
66 connect(tree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), this, SLOT(doubleClicked()));
67 connect(buttonAdd, SIGNAL(clicked()), this, SLOT(addClicked()));
68 connect(radChooseClk, SIGNAL(toggled(bool)), this, SLOT(enableAvailableClocks(bool)));
70 BlockCategory* cat = params->categoryTree->searchCategory(0);
72 QTreeWidgetItem* item = tree->invisibleRootItem();
73 tree->setHeaderLabel("Blocks list");
77 layout->addWidget(tree);
78 layout->addLayout(layBottom);
79 this->setLayout(layout);
81 //this->setFixedSize(300,230);
85 BlockLibraryWidget::~BlockLibraryWidget() {
88 void BlockLibraryWidget::addChild(BlockCategory *catParent,QTreeWidgetItem* itemParent) {
90 QTreeWidgetItem* newItemCat = NULL;
91 QTreeWidgetItem* newItemBlock = NULL;
93 QList<BlockCategory *> childs = catParent->getAllChilds();
94 foreach(BlockCategory* cat, childs){
96 if (cat->getId()<100) {
97 newItemCat = new QTreeWidgetItem(itemParent);
98 newItemCat->setData(0,Qt::DisplayRole, cat->getName());
99 QList<ReferenceBlock*> list = cat->getBlocks();
100 for(int i=0; i<list.length(); i++){
101 newItemBlock = new QTreeWidgetItem(newItemCat);
102 newItemBlock->setData(0,Qt::DisplayRole, list.at(i)->getName());
103 newItemBlock->setData(1,Qt::DisplayRole, cat->getId());
104 newItemBlock->setData(2,Qt::DisplayRole, i);
105 newItemBlock->setIcon(0,QIcon("icons/window_new.png"));
107 addChild(cat,newItemCat);
111 - getting the childs of catParent
112 - for each BlockCategory cat of that list :
113 - create an new TreeWidgetItem (newImteCat), with itemParent as a parent
114 - set the first column of that item with the categry name
115 - get the list of the blocks that are associated with cat
116 - for i = 0 to that list size.
117 - create a new TreeWidgetItem (newItemBlock), with newItemCat as a parent
118 - set the first column of that item with the block name
119 - set the second column of that item with the newItemCat id
120 - set the third column of that item with the value of i
122 - call again addChild with cat and newImteCat as parameters
128 void BlockLibraryWidget::addClicked() {
130 QTreeWidgetItem *item = tree->selectedItems().at(0);
131 if(item->data(1,Qt::DisplayRole).isValid() && item->data(2,Qt::DisplayRole).isValid()){
132 int idCat = item->data(1,Qt::DisplayRole).toInt();
133 int idBlock = item->data(2,Qt::DisplayRole).toInt();
134 QVariant v = comboScenes->currentData();
136 cout << "adding block to scene " << v.toInt() << endl;
138 QHash<QString, int> clkRstToGen;
139 for(int i=1;i<=nbClock+nbRst;i++) {
140 QLayoutItem* item = layClkRst->itemAtPosition(i,0);
141 QLabel* lab = (QLabel *)(item->widget());
142 item = layClkRst->itemAtPosition(i,1);
143 QComboBox* combo = (QComboBox *)(item->widget());
144 clkRstToGen.insert(lab->text(),combo->currentIndex());
145 cout << "addblock: have to connect " << qPrintable(lab->text()) << " to clk/rst n° " << combo->currentIndex() << endl;
149 dispatcher->addBlock(Dispatcher::Design, idCat, idBlock, v.toInt(), clkRstToGen);
152 // only take the first selected
153 // retrieve id of category and id of block
154 // calling dispatcher addBlock() method.
157 void BlockLibraryWidget::clicked() {
158 if(tree->selectedItems().length() > 0) {
160 QTreeWidgetItem *item = tree->selectedItems().at(0);
162 if(item->data(1,Qt::DisplayRole).isValid()) {
165 buttonAdd->setEnabled(true);
166 int idCat = item->data(1,Qt::DisplayRole).toInt();
167 int idBlock = item->data(2,Qt::DisplayRole).toInt();
168 currentRefBlock = params->getReferenceBlock(idCat,idBlock);
169 QList<AbstractInterface*> lstClocks = currentRefBlock->getInterfaces(AbstractInterface::Input, AbstractInterface::Clock);
170 //if there are more than one input clock, force to choose
171 if ((lstClocks.size()>1) || (! params->autoConnMainClk)) {
172 radChooseClk->setChecked(true);
173 //enableAvailableClocks(true);
176 radAutoClk->setChecked(true);
178 updateClkRst(idCat, idBlock);
181 buttonAdd->setEnabled(false);
187 void BlockLibraryWidget::doubleClicked() {
192 void BlockLibraryWidget::updateBoxConn() {
193 if (params->autoConnMainClk) {
194 radAutoClk->setChecked(true);
195 //enableAvailableClocks(false);
198 radChooseClk->setChecked(true);
199 //enableAvailableClocks(true);
201 QString msg = "Auto-connect to main clk/rst (ext_clk_0 at ";
202 msg += QString::number(params->getGraph()->getClock(0));
204 radAutoClk->setText(msg);
207 void BlockLibraryWidget::updateComboScene() {
208 comboScenes->clear();
209 QMap<int,QString> list = dispatcher->getAllGroupNames();
210 QMapIterator<int,QString> iter(list);
211 while (iter.hasNext()) {
213 comboScenes->addItem(iter.value(),QVariant(iter.key()));
217 void BlockLibraryWidget::updateClkRst(int idCat, int idBlock) {
219 while (layClkRst->count() > 0) {
220 QWidget* widget = layClkRst->itemAt(0)->widget();
221 layClkRst->removeWidget(widget);
226 delete [] comboClkGen;
230 delete [] comboRstGen;
234 if ((idCat == -1) || (idBlock == -1)) return;
236 QList<AbstractInterface*> lstClocks = currentRefBlock->getInterfaces(AbstractInterface::Input, AbstractInterface::Clock);
237 nbClock = lstClocks.size();
238 QList<AbstractInterface*> lstRst = currentRefBlock->getInterfaces(AbstractInterface::Input, AbstractInterface::Reset);
239 nbRst = lstRst.size();
240 cout << "For chosen block there are " << nbClock << " clocks and " << nbRst << " resets" << endl;
242 comboClkGen = new QComboBox*[lstClocks.size()];
243 for(int i=0;i<lstClocks.size();i++) {
244 comboClkGen[i] = new QComboBox();
247 foreach(double d, params->getGraph()->getClocks()) {
248 name = "ext_clk_"+QString::number(id)+" (";
249 name += QString::number(d) + " MHz)";
250 comboClkGen[i]->addItem(name);
255 comboRstGen = new QComboBox*[lstRst.size()];
256 for(int i=0;i<lstRst.size();i++) {
257 comboRstGen[i] = new QComboBox();
259 for(int j=0;j<params->getGraph()->getClocks().size();j++) {
260 name = "ext_rst_"+QString::number(j);
261 comboRstGen[i]->addItem(name);
264 layClkRst->addWidget(new QLabel("Clock/Reset name"), 0, 0);
265 layClkRst->addWidget(new QLabel("connect to"), 0, 1);
267 foreach(AbstractInterface* iface, lstClocks) {
268 layClkRst->addWidget(new QLabel(iface->getName()), row,0);
269 layClkRst->addWidget(comboClkGen[row-1],row, 1);
273 foreach(AbstractInterface* iface, lstRst) {
274 layClkRst->addWidget(new QLabel(iface->getName()), row,0);
275 layClkRst->addWidget(comboRstGen[row-1-nbClock],row, 1);
280 void BlockLibraryWidget::enableAvailableClocks(bool state) {
282 if (state == false) {
283 stack->setCurrentIndex(0);
286 stack->setCurrentIndex(1);