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=0;i<layClkRst->rowCount();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());
147 dispatcher->addBlock(Dispatcher::Design, idCat, idBlock, v.toInt(), clkRstToGen);
150 // only take the first selected
151 // retrieve id of category and id of block
152 // calling dispatcher addBlock() method.
155 void BlockLibraryWidget::clicked() {
156 if(tree->selectedItems().length() > 0) {
158 QTreeWidgetItem *item = tree->selectedItems().at(0);
160 if(item->data(1,Qt::DisplayRole).isValid()) {
163 buttonAdd->setEnabled(true);
164 int idCat = item->data(1,Qt::DisplayRole).toInt();
165 int idBlock = item->data(2,Qt::DisplayRole).toInt();
166 currentRefBlock = params->getReferenceBlock(idCat,idBlock);
167 QList<AbstractInterface*> lstClocks = currentRefBlock->getInterfaces(AbstractInterface::Input, AbstractInterface::Clock);
168 //if there are more than one input clock, force to choose
169 if ((lstClocks.size()>1) || (! params->autoConnMainClk)) {
170 radChooseClk->setChecked(true);
171 //enableAvailableClocks(true);
174 radAutoClk->setChecked(true);
176 updateClkRst(idCat, idBlock);
179 buttonAdd->setEnabled(false);
185 void BlockLibraryWidget::doubleClicked() {
190 void BlockLibraryWidget::updateBoxConn() {
191 if (params->autoConnMainClk) {
192 radAutoClk->setChecked(true);
193 //enableAvailableClocks(false);
196 radChooseClk->setChecked(true);
197 //enableAvailableClocks(true);
199 QString msg = "Auto-connect to main clk/rst (ext_clk_0 at ";
200 msg += QString::number(params->clocks.at(0));
202 radAutoClk->setText(msg);
205 void BlockLibraryWidget::updateComboScene() {
206 comboScenes->clear();
207 QMap<int,QString> list = dispatcher->getAllGroupNames();
208 QMapIterator<int,QString> iter(list);
209 while (iter.hasNext()) {
211 comboScenes->addItem(iter.value(),QVariant(iter.key()));
215 void BlockLibraryWidget::updateClkRst(int idCat, int idBlock) {
217 while (layClkRst->count() > 0) {
218 QWidget* widget = layClkRst->itemAt(0)->widget();
219 layClkRst->removeWidget(widget);
224 delete [] comboClkGen;
228 delete [] comboRstGen;
232 if ((idCat == -1) || (idBlock == -1)) return;
234 QList<AbstractInterface*> lstClocks = currentRefBlock->getInterfaces(AbstractInterface::Input, AbstractInterface::Clock);
235 nbClock = lstClocks.size();
236 QList<AbstractInterface*> lstRst = currentRefBlock->getInterfaces(AbstractInterface::Input, AbstractInterface::Reset);
237 nbRst = lstRst.size();
239 comboClkGen = new QComboBox*[lstClocks.size()];
240 for(int i=0;i<lstClocks.size();i++) {
241 comboClkGen[i] = new QComboBox();
244 foreach(double d, params->clocks) {
245 name = "ext_clk_"+QString::number(id)+" (";
246 name += QString::number(d) + " MHz)";
247 comboClkGen[i]->addItem(name);
252 comboRstGen = new QComboBox*[lstRst.size()];
253 for(int i=0;i<lstRst.size();i++) {
254 comboRstGen[i] = new QComboBox();
256 for(int j=0;j<params->clocks.size();j++) {
257 name = "ext_rst_"+QString::number(j);
258 comboRstGen[i]->addItem(name);
261 layClkRst->addWidget(new QLabel("Clock/Reset name"), 0, 0);
262 layClkRst->addWidget(new QLabel("connect to"), 0, 1);
264 foreach(AbstractInterface* iface, lstClocks) {
265 layClkRst->addWidget(new QLabel(iface->getName()), row,0);
266 layClkRst->addWidget(comboClkGen[row-1],row, 1);
270 foreach(AbstractInterface* iface, lstRst) {
271 layClkRst->addWidget(new QLabel(iface->getName()), row,0);
272 layClkRst->addWidget(comboRstGen[row-1-nbClock],row, 1);
277 void BlockLibraryWidget::enableAvailableClocks(bool state) {
279 if (state == false) {
280 stack->setCurrentIndex(0);
283 stack->setCurrentIndex(1);