1 #include "BlockLibraryWidget.h"
2 #include "BlockLibraryTree.h"
4 BlockLibraryWidget::BlockLibraryWidget(Dispatcher* _dispatcher,
6 QWidget *parent) : QWidget(parent) {
9 dispatcher = _dispatcher;
16 currentRefBlock = NULL;
18 // creating the widget : tree, buttons, ...
19 QBoxLayout* layout = new QBoxLayout(QBoxLayout::TopToBottom, this);
20 tree = new QTreeWidget(this);
22 buttonAdd = new QPushButton("add to", this);
23 buttonAdd->setEnabled(false);
24 comboScenes = new QComboBox();
26 QHBoxLayout* layAdd = new QHBoxLayout;
27 layAdd->addWidget(buttonAdd);
28 layAdd->addWidget(comboScenes);
30 boxClkRst = new QGroupBox("Clock/reset connection");
33 QString msg = "Connect to main clock/rst ext_clk/reset_0 (";
34 msg += QString::number(params->clocks.at(0));
36 radAutoClk = new QRadioButton(msg);
37 radChooseClk = new QRadioButton("Choose among available clk/rst");
38 if (params->autoConnMainClk) {
39 radAutoClk->setChecked(true);
42 radChooseClk->setChecked(true);
45 layClkRst = new QGridLayout;
47 QVBoxLayout* layConn = new QVBoxLayout;
48 layConn->addWidget(radAutoClk,0,0);
49 layConn->addWidget(radChooseClk,1,0);
50 stack = new QStackedWidget();
51 QLabel* labDummy = new QLabel("");
52 stack->addWidget(labDummy);
53 QWidget* w = new QWidget();
54 w->setLayout(layClkRst);
56 layConn->addWidget(stack);
58 boxClkRst->setLayout(layConn);
60 QVBoxLayout* layBottom = new QVBoxLayout;
61 layBottom->addLayout(layAdd);
62 layBottom->addWidget(boxClkRst);
64 connect(tree, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(clicked()));
65 connect(tree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), this, SLOT(doubleClicked()));
66 connect(buttonAdd, SIGNAL(clicked()), this, SLOT(addClicked()));
67 connect(radChooseClk, SIGNAL(toggled(bool)), this, SLOT(enableAvailableClocks(bool)));
69 BlockCategory* cat = params->categoryTree->searchCategory(0);
71 QTreeWidgetItem* item = tree->invisibleRootItem();
72 tree->setHeaderLabel("Blocks list");
76 layout->addWidget(tree);
77 layout->addLayout(layBottom);
78 this->setLayout(layout);
80 //this->setFixedSize(300,230);
84 BlockLibraryWidget::~BlockLibraryWidget() {
87 void BlockLibraryWidget::addChild(BlockCategory *catParent,QTreeWidgetItem* itemParent) {
89 QTreeWidgetItem* newItemCat = NULL;
90 QTreeWidgetItem* newItemBlock = NULL;
92 QList<BlockCategory *> childs = catParent->getAllChilds();
93 foreach(BlockCategory* cat, childs){
95 if (cat->getId()<100) {
96 newItemCat = new QTreeWidgetItem(itemParent);
97 newItemCat->setData(0,Qt::DisplayRole, cat->getName());
98 QList<ReferenceBlock*> list = cat->getBlocks();
99 for(int i=0; i<list.length(); i++){
100 newItemBlock = new QTreeWidgetItem(newItemCat);
101 newItemBlock->setData(0,Qt::DisplayRole, list.at(i)->getName());
102 newItemBlock->setData(1,Qt::DisplayRole, cat->getId());
103 newItemBlock->setData(2,Qt::DisplayRole, i);
104 newItemBlock->setIcon(0,QIcon("icons/window_new.png"));
106 addChild(cat,newItemCat);
110 - getting the childs of catParent
111 - for each BlockCategory cat of that list :
112 - create an new TreeWidgetItem (newImteCat), with itemParent as a parent
113 - set the first column of that item with the categry name
114 - get the list of the blocks that are associated with cat
115 - for i = 0 to that list size.
116 - create a new TreeWidgetItem (newItemBlock), with newItemCat as a parent
117 - set the first column of that item with the block name
118 - set the second column of that item with the newItemCat id
119 - set the third column of that item with the value of i
121 - call again addChild with cat and newImteCat as parameters
127 void BlockLibraryWidget::addClicked() {
129 QTreeWidgetItem *item = tree->selectedItems().at(0);
130 if(item->data(1,Qt::DisplayRole).isValid() && item->data(2,Qt::DisplayRole).isValid()){
131 int idCat = item->data(1,Qt::DisplayRole).toInt();
132 int idBlock = item->data(2,Qt::DisplayRole).toInt();
133 QVariant v = comboScenes->currentData();
135 cout << "adding block to scene " << v.toInt() << endl;
137 QHash<QString, int> clkRstToGen;
138 for(int i=1;i<nbClock+nbRst;i++) {
139 QLayoutItem* item = layClkRst->itemAtPosition(i,0);
140 QLabel* lab = (QLabel *)(item->widget());
141 item = layClkRst->itemAtPosition(i,1);
142 QComboBox* combo = (QComboBox *)(item->widget());
143 clkRstToGen.insert(lab->text(),combo->currentIndex());
144 cout << "addblock: have to connect " << qPrintable(lab->text()) << " to clk/rst n° " << combo->currentIndex() << endl;
148 dispatcher->addBlock(Dispatcher::Design, idCat, idBlock, v.toInt(), clkRstToGen);
151 // only take the first selected
152 // retrieve id of category and id of block
153 // calling dispatcher addBlock() method.
156 void BlockLibraryWidget::clicked() {
157 if(tree->selectedItems().length() > 0) {
159 QTreeWidgetItem *item = tree->selectedItems().at(0);
161 if(item->data(1,Qt::DisplayRole).isValid()) {
164 buttonAdd->setEnabled(true);
165 int idCat = item->data(1,Qt::DisplayRole).toInt();
166 int idBlock = item->data(2,Qt::DisplayRole).toInt();
167 currentRefBlock = params->getReferenceBlock(idCat,idBlock);
168 QList<AbstractInterface*> lstClocks = currentRefBlock->getInterfaces(AbstractInterface::Input, AbstractInterface::Clock);
169 //if there are more than one input clock, force to choose
170 if ((lstClocks.size()>1) || (! params->autoConnMainClk)) {
171 radChooseClk->setChecked(true);
172 //enableAvailableClocks(true);
175 radAutoClk->setChecked(true);
177 updateClkRst(idCat, idBlock);
180 buttonAdd->setEnabled(false);
186 void BlockLibraryWidget::doubleClicked() {
191 void BlockLibraryWidget::updateBoxConn() {
192 if (params->autoConnMainClk) {
193 radAutoClk->setChecked(true);
194 //enableAvailableClocks(false);
197 radChooseClk->setChecked(true);
198 //enableAvailableClocks(true);
200 QString msg = "Auto-connect to main clk/rst (ext_clk_0 at ";
201 msg += QString::number(params->clocks.at(0));
203 radAutoClk->setText(msg);
206 void BlockLibraryWidget::updateComboScene() {
207 comboScenes->clear();
208 QMap<int,QString> list = dispatcher->getAllGroupNames();
209 QMapIterator<int,QString> iter(list);
210 while (iter.hasNext()) {
212 comboScenes->addItem(iter.value(),QVariant(iter.key()));
216 void BlockLibraryWidget::updateClkRst(int idCat, int idBlock) {
218 while (layClkRst->count() > 0) {
219 QWidget* widget = layClkRst->itemAt(0)->widget();
220 layClkRst->removeWidget(widget);
225 delete [] comboClkGen;
229 delete [] comboRstGen;
233 if ((idCat == -1) || (idBlock == -1)) return;
235 QList<AbstractInterface*> lstClocks = currentRefBlock->getInterfaces(AbstractInterface::Input, AbstractInterface::Clock);
236 nbClock = lstClocks.size();
237 QList<AbstractInterface*> lstRst = currentRefBlock->getInterfaces(AbstractInterface::Input, AbstractInterface::Reset);
238 nbRst = lstRst.size();
239 cout << "For chosen block there are " << nbClock << " clocks and " << nbRst << " resets" << endl;
241 comboClkGen = new QComboBox*[lstClocks.size()];
242 for(int i=0;i<lstClocks.size();i++) {
243 comboClkGen[i] = new QComboBox();
246 foreach(double d, params->clocks) {
247 name = "ext_clk_"+QString::number(id)+" (";
248 name += QString::number(d) + " MHz)";
249 comboClkGen[i]->addItem(name);
254 comboRstGen = new QComboBox*[lstRst.size()];
255 for(int i=0;i<lstRst.size();i++) {
256 comboRstGen[i] = new QComboBox();
258 for(int j=0;j<params->clocks.size();j++) {
259 name = "ext_rst_"+QString::number(j);
260 comboRstGen[i]->addItem(name);
263 layClkRst->addWidget(new QLabel("Clock/Reset name"), 0, 0);
264 layClkRst->addWidget(new QLabel("connect to"), 0, 1);
266 foreach(AbstractInterface* iface, lstClocks) {
267 layClkRst->addWidget(new QLabel(iface->getName()), row,0);
268 layClkRst->addWidget(comboClkGen[row-1],row, 1);
272 foreach(AbstractInterface* iface, lstRst) {
273 layClkRst->addWidget(new QLabel(iface->getName()), row,0);
274 layClkRst->addWidget(comboRstGen[row-1-nbClock],row, 1);
279 void BlockLibraryWidget::enableAvailableClocks(bool state) {
281 if (state == false) {
282 stack->setCurrentIndex(0);
285 stack->setCurrentIndex(1);