3 // Originally part of QCad Community Edition by Andrew Mustun
4 // Extensively rewritten and refactored by James L. Hammons
5 // (C) 2010 Underground Software
7 // JLH = James L. Hammons <jlhamm@acm.org>
10 // --- ---------- -----------------------------------------------------------
11 // JLH 05/10/2010 Added this text. :-)
14 #include "qg_blockwidget.h"
16 /*#include "xpm/visibleblock.xpm"
17 #include "xpm/hiddenblock.xpm"
18 #include "xpm/blockadd.xpm"
19 #include "xpm/blockremove.xpm"
20 #include "xpm/blockedit.xpm"
21 #include "xpm/blockattributes.xpm"
22 #include "xpm/blockinsert.xpm"*/
27 QG_BlockWidget::QG_BlockWidget(QG_ActionHandler * ah, QWidget * parent, const char * name, Qt::WFlags f):
28 // QWidget(parent, name, f),
30 // pxmVisible(visibleblock_xpm),
31 // pxmHidden(hiddenblock_xpm),
32 // pxmAdd(blockadd_xpm),
33 // pxmRemove(blockremove_xpm),
34 // pxmAttributes(blockattributes_xpm),
35 // pxmEdit(blockedit_xpm),
36 // pxmInsert(blockinsert_xpm),
37 // pxmDefreezeAll(visibleblock_xpm),
38 // pxmFreezeAll(hiddenblock_xpm)
39 pxmVisible(":/res/visibleblock.xpm"),
40 pxmHidden(":/res/hiddenblock.xpm"),
41 pxmAdd(":/res/blockadd.xpm"),
42 pxmRemove(":/res/blockremove.xpm"),
43 pxmAttributes(":/res/blockattributes.xpm"),
44 pxmEdit(":/res/blockedit.xpm"),
45 pxmInsert(":/res/blockinsert.xpm"),
46 pxmDefreezeAll(":/res/visibleblock.xpm"),
47 pxmFreezeAll(":/res/hiddenblock.xpm")
53 // listBox = new Q3ListBox(this, "blockbox");
54 listBox = new QListWidget(this);
55 #warning "!!! !!! !!!"
56 // listBox->setDragSelect(false);
57 // listBox->setMultiSelection(false);
58 // listBox->setSmoothScrolling(true);
59 listBox->setFocusPolicy(Qt::NoFocus);
61 // Q3VBoxLayout* lay = new Q3VBoxLayout(this, 0, -1, "lay");
62 QVBoxLayout * lay = new QVBoxLayout(this);
65 QLabel* caption = new QLabel(tr("Block List"), this, "caption");
66 caption->setAlignment(Qt::AlignCenter);
67 caption->setPaletteBackgroundColor(black);
68 caption->setPaletteForegroundColor(white);
71 // Q3HBoxLayout* layButtons = new Q3HBoxLayout(NULL, 0, -1, "layButtons");
72 // Q3HBoxLayout* layButtons2 = new Q3HBoxLayout(NULL, 0, -1, "layButtons2");
73 QHBoxLayout * layButtons = new QHBoxLayout();
74 QHBoxLayout * layButtons2 = new QHBoxLayout();
77 but = new QToolButton(this);
78 // but->setPixmap(pxmDefreezeAll);
79 but->setIcon(QIcon(pxmDefreezeAll));
80 but->setMinimumSize(QSize(22, 22));
81 // QToolTip::add(but, tr("Show all blocks"));
82 but->setToolTip(tr("Show all blocks"));
83 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksDefreezeAll()));
84 layButtons->addWidget(but);
86 but = new QToolButton(this);
87 // but->setPixmap(pxmFreezeAll);
88 but->setIcon(QIcon(pxmFreezeAll));
89 but->setMinimumSize(QSize(22, 22));
90 // QToolTip::add(but, tr("Hide all blocks"));
91 but->setToolTip(tr("Hide all blocks"));
92 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksFreezeAll()));
93 layButtons->addWidget(but);
95 but = new QToolButton(this);
96 // but->setPixmap(pxmAdd);
97 but->setIcon(QIcon(pxmAdd));
98 but->setMinimumSize(QSize(22, 22));
99 // QToolTip::add(but, tr("Add a block"));
100 but->setToolTip(tr("Add a block"));
101 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksAdd()));
102 layButtons->addWidget(but);
104 but = new QToolButton(this);
105 // but->setPixmap(pxmRemove);
106 but->setIcon(QIcon(pxmRemove));
107 but->setMinimumSize(QSize(22, 22));
108 // QToolTip::add(but, tr("Remove the active block"));
109 but->setToolTip(tr("Remove the active block"));
110 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksRemove()));
111 layButtons->addWidget(but);
113 but = new QToolButton(this);
114 // but->setPixmap(pxmAttributes);
115 but->setIcon(QIcon(pxmAttributes));
116 but->setMinimumSize(QSize(22, 22));
117 // QToolTip::add(but, tr("Rename the active block"));
118 but->setToolTip(tr("Rename the active block"));
119 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksAttributes()));
120 layButtons->addWidget(but);
122 but = new QToolButton(this);
123 // but->setPixmap(pxmEdit);
124 but->setIcon(QIcon(pxmEdit));
125 but->setMinimumSize(QSize(22, 22));
126 // QToolTip::add(but, tr("Edit the active block\nin a separate window"));
127 but->setToolTip(tr("Edit the active block\nin a separate window"));
128 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksEdit()));
129 layButtons2->addWidget(but);
131 but = new QToolButton(this);
132 // but->setPixmap(pxmInsert);
133 but->setIcon(QIcon(pxmInsert));
134 but->setMinimumSize(QSize(22, 22));
135 // QToolTip::add(but, tr("Insert the active block"));
136 but->setToolTip(tr("Insert the active block"));
137 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksInsert()));
138 layButtons2->addWidget(but);
140 //lay->addWidget(caption);
141 lay->addLayout(layButtons);
142 lay->addLayout(layButtons2);
143 lay->addWidget(listBox);
145 //connect(listBox, SIGNAL(doubleClicked(QListBoxItem*)),
146 // actionHandler, SLOT(slotBlocksToggleView()));
148 // connect(listBox, SIGNAL(highlighted(const QString&)), this, SLOT(slotActivated(const QString&)));
149 connect(listBox, SIGNAL(itemSelectionChanged(void)), this, SLOT(slotActivated(void)));
151 // connect(listBox, SIGNAL(mouseButtonClicked(int, Q3ListBoxItem*, const QPoint&)),
152 // this, SLOT(slotMouseButtonClicked(int, Q3ListBoxItem*, const QPoint&)));
153 connect(listBox, SIGNAL(itemClicked(QListWidgetItem *)),
154 this, SLOT(slotMouseButtonClicked(QListWidgetItem *)));
156 //boxLayout()->addWidget(listBox);
162 QG_BlockWidget::~QG_BlockWidget()
167 void QG_BlockWidget::setBlockList(RS_BlockList * blockList)
169 this->blockList = blockList;
173 RS_BlockList * QG_BlockWidget::getBlockList()
179 * Updates the block box from the blocks in the graphic.
181 void QG_BlockWidget::update()
183 RS_DEBUG->print("QG_BlockWidget::update()");
186 // int yPos = listBox->contentsY();
188 RS_Block * activeBlock = NULL;
190 if (blockList != NULL)
191 activeBlock = blockList->getActive();
195 if (blockList == NULL)
197 RS_DEBUG->print("QG_BlockWidget::update(): blockList is NULL");
201 for(uint i=0; i<blockList->count(); ++i)
203 RS_Block * blk = blockList->at(i);
205 if (!blk->isFrozen())
206 // listBox->insertItem(pxmVisible, blk->getName());
207 listBox->addItem(new QListWidgetItem(pxmVisible, blk->getName()));
209 // listBox->insertItem(pxmHidden, blk->getName());
210 listBox->addItem(new QListWidgetItem(pxmHidden, blk->getName()));
214 listBox->sortItems(Qt::AscendingOrder);
216 RS_Block * b = lastBlock;
217 highlightBlock(activeBlock);
220 // listBox->setContentsPos(0, yPos);
222 //highlightBlock(blockList->getActiveBlock());
223 //listBox->setContentsPos(0, yPos);
224 RS_DEBUG->print("QG_BlockWidget::update() done");
228 * Highlights (activates) the given block and makes it
229 * the active block in the blocklist.
231 void QG_BlockWidget::highlightBlock(RS_Block * block)
233 RS_DEBUG->print("QG_BlockWidget::highlightBlock()");
235 if (block == NULL || listBox == NULL)
238 blockList->activate(block);
239 QString name = block->getName();
241 for(int i=0; i<(int)listBox->count(); ++i)
243 QListWidgetItem * item = listBox->item(i);
245 // if (listBox->text(i) == name)
246 if (item->text() == name)
248 // listBox->setCurrentItem(i);
249 listBox->setCurrentRow(i);
255 /*virtual*/ void QG_BlockWidget::blockAdded(RS_Block *)
260 /*virtual*/ void QG_BlockWidget::blockEdited(RS_Block *)
265 /*virtual*/ void QG_BlockWidget::blockRemoved(RS_Block *)
270 /*virtual*/ void QG_BlockWidget::blockToggled(RS_Block *)
276 * Toggles the view of the given block. This is usually called when
277 * an item is double clicked.
280 void QG_BlockWidget::slotToggleView(QListBoxItem* item) {
281 RS_DEBUG->print("QG_BlockWidget::slotToggleView()");
283 if (item==NULL || blockList==NULL) {
287 int index = listBox->index(item);
288 RS_Block* block = blockList->find(item->text());
291 blockList->toggleBlock(item->text());
292 if (!block->isFrozen()) {
293 listBox->changeItem(pxmVisible, item->text(), index);
295 listBox->changeItem(*pxmHidden, item->text(), index);
303 * Called when the user activates (highlights) a block.
305 //void QG_BlockWidget::slotActivated(const QString & blockName)
306 void QG_BlockWidget::slotActivated(void)
308 QString blockName = listBox->currentItem()->text();
309 RS_DEBUG->print("QG_BlockWidget::slotActivated(): %s", blockName.toLatin1().data());
311 if (blockList == NULL)
314 lastBlock = blockList->getActive();
315 blockList->activate(blockName);
319 * Called for every mouse click.
321 //void QG_BlockWidget::slotMouseButtonClicked(int /*button*/, Q3ListBoxItem* item, const QPoint& pos)
322 void QG_BlockWidget::slotMouseButtonClicked(QListWidgetItem * item)
324 QPoint p = mapFromGlobal(QCursor::pos());
325 RS_Block * b = lastBlock;
329 actionHandler->slotBlocksToggleView();
334 if (item != NULL && blockList != NULL)
335 lastBlock = blockList->find(item->text());
340 * Shows a context menu for the block widget. Launched with a right click.
342 void QG_BlockWidget::contextMenuEvent(QContextMenuEvent * e)
344 #warning "Figure out how to port this crap (popup menu) to Qt4... !!! FIX !!!"
346 //QListBoxItem* item = listBox->selectedItem();
347 Q3PopupMenu * contextMenu = new Q3PopupMenu(this);
348 QLabel * caption = new QLabel(tr("Block Menu"), this);
349 caption->setPaletteBackgroundColor(Qt::black);
350 caption->setPaletteForegroundColor(Qt::white);
351 caption->setAlignment(Qt::AlignCenter);
352 contextMenu->insertItem(caption);
353 contextMenu->insertItem(tr("&Defreeze all Blocks"), actionHandler, SLOT(slotBlocksDefreezeAll()), 0);
354 contextMenu->insertItem(tr("&Freeze all Blocks"), actionHandler, SLOT(slotBlocksFreezeAll()), 0);
355 contextMenu->insertItem(tr("&Add Block"), actionHandler, SLOT(slotBlocksAdd()), 0);
356 contextMenu->insertItem(tr("&Remove Block"), actionHandler, SLOT(slotBlocksRemove()), 0);
357 contextMenu->insertItem(tr("&Rename Block"), actionHandler, SLOT(slotBlocksAttributes()), 0);
358 contextMenu->insertItem(tr("&Edit Block"), actionHandler, SLOT(slotBlocksEdit()), 0);
359 contextMenu->insertItem(tr("&Insert Block"), actionHandler, SLOT(slotBlocksInsert()), 0);
360 contextMenu->insertItem(tr("&Toggle Visibility"), actionHandler, SLOT(slotBlocksToggleView()), 0);
361 contextMenu->insertItem(tr("&Create New Block"), actionHandler, SLOT(slotBlocksCreate()), 0);
362 contextMenu->exec(QCursor::pos());
370 * Escape releases focus.
372 void QG_BlockWidget::keyPressEvent(QKeyEvent * e)
381 QWidget::keyPressEvent(e);