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"
19 QG_BlockWidget::QG_BlockWidget(QG_ActionHandler * ah, QWidget * parent, const char * name, Qt::WFlags f):
20 // QWidget(parent, name, f),
22 pxmVisible(":/res/visibleblock.xpm"),
23 pxmHidden(":/res/hiddenblock.xpm"),
24 pxmAdd(":/res/blockadd.xpm"),
25 pxmRemove(":/res/blockremove.xpm"),
26 pxmAttributes(":/res/blockattributes.xpm"),
27 pxmEdit(":/res/blockedit.xpm"),
28 pxmInsert(":/res/blockinsert.xpm"),
29 pxmDefreezeAll(":/res/visibleblock.xpm"),
30 pxmFreezeAll(":/res/hiddenblock.xpm")
36 // listBox = new Q3ListBox(this, "blockbox");
37 listBox = new QListWidget(this);
38 #warning "!!! !!! !!!"
39 // listBox->setDragSelect(false);
40 // listBox->setMultiSelection(false);
41 // listBox->setSmoothScrolling(true);
42 listBox->setFocusPolicy(Qt::NoFocus);
44 // Q3VBoxLayout* lay = new Q3VBoxLayout(this, 0, -1, "lay");
45 QVBoxLayout * lay = new QVBoxLayout(this);
48 QLabel* caption = new QLabel(tr("Block List"), this, "caption");
49 caption->setAlignment(Qt::AlignCenter);
50 caption->setPaletteBackgroundColor(black);
51 caption->setPaletteForegroundColor(white);
54 // Q3HBoxLayout* layButtons = new Q3HBoxLayout(NULL, 0, -1, "layButtons");
55 // Q3HBoxLayout* layButtons2 = new Q3HBoxLayout(NULL, 0, -1, "layButtons2");
56 QHBoxLayout * layButtons = new QHBoxLayout();
57 QHBoxLayout * layButtons2 = new QHBoxLayout();
60 but = new QToolButton(this);
61 // but->setPixmap(pxmDefreezeAll);
62 but->setIcon(QIcon(pxmDefreezeAll));
63 but->setMinimumSize(QSize(22, 22));
64 // QToolTip::add(but, tr("Show all blocks"));
65 but->setToolTip(tr("Show all blocks"));
66 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksDefreezeAll()));
67 layButtons->addWidget(but);
69 but = new QToolButton(this);
70 // but->setPixmap(pxmFreezeAll);
71 but->setIcon(QIcon(pxmFreezeAll));
72 but->setMinimumSize(QSize(22, 22));
73 // QToolTip::add(but, tr("Hide all blocks"));
74 but->setToolTip(tr("Hide all blocks"));
75 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksFreezeAll()));
76 layButtons->addWidget(but);
78 but = new QToolButton(this);
79 // but->setPixmap(pxmAdd);
80 but->setIcon(QIcon(pxmAdd));
81 but->setMinimumSize(QSize(22, 22));
82 // QToolTip::add(but, tr("Add a block"));
83 but->setToolTip(tr("Add a block"));
84 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksAdd()));
85 layButtons->addWidget(but);
87 but = new QToolButton(this);
88 // but->setPixmap(pxmRemove);
89 but->setIcon(QIcon(pxmRemove));
90 but->setMinimumSize(QSize(22, 22));
91 // QToolTip::add(but, tr("Remove the active block"));
92 but->setToolTip(tr("Remove the active block"));
93 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksRemove()));
94 layButtons->addWidget(but);
96 but = new QToolButton(this);
97 // but->setPixmap(pxmAttributes);
98 but->setIcon(QIcon(pxmAttributes));
99 but->setMinimumSize(QSize(22, 22));
100 // QToolTip::add(but, tr("Rename the active block"));
101 but->setToolTip(tr("Rename the active block"));
102 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksAttributes()));
103 layButtons->addWidget(but);
105 but = new QToolButton(this);
106 // but->setPixmap(pxmEdit);
107 but->setIcon(QIcon(pxmEdit));
108 but->setMinimumSize(QSize(22, 22));
109 // QToolTip::add(but, tr("Edit the active block\nin a separate window"));
110 but->setToolTip(tr("Edit the active block\nin a separate window"));
111 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksEdit()));
112 layButtons2->addWidget(but);
114 but = new QToolButton(this);
115 // but->setPixmap(pxmInsert);
116 but->setIcon(QIcon(pxmInsert));
117 but->setMinimumSize(QSize(22, 22));
118 // QToolTip::add(but, tr("Insert the active block"));
119 but->setToolTip(tr("Insert the active block"));
120 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksInsert()));
121 layButtons2->addWidget(but);
123 //lay->addWidget(caption);
124 lay->addLayout(layButtons);
125 lay->addLayout(layButtons2);
126 lay->addWidget(listBox);
128 //connect(listBox, SIGNAL(doubleClicked(QListBoxItem*)),
129 // actionHandler, SLOT(slotBlocksToggleView()));
131 // connect(listBox, SIGNAL(highlighted(const QString&)), this, SLOT(slotActivated(const QString&)));
132 connect(listBox, SIGNAL(itemSelectionChanged(void)), this, SLOT(slotActivated(void)));
134 // connect(listBox, SIGNAL(mouseButtonClicked(int, Q3ListBoxItem*, const QPoint&)),
135 // this, SLOT(slotMouseButtonClicked(int, Q3ListBoxItem*, const QPoint&)));
136 connect(listBox, SIGNAL(itemClicked(QListWidgetItem *)),
137 this, SLOT(slotMouseButtonClicked(QListWidgetItem *)));
139 //boxLayout()->addWidget(listBox);
145 QG_BlockWidget::~QG_BlockWidget()
150 void QG_BlockWidget::setBlockList(RS_BlockList * blockList)
152 this->blockList = blockList;
156 RS_BlockList * QG_BlockWidget::getBlockList()
162 * Updates the block box from the blocks in the graphic.
164 void QG_BlockWidget::update()
166 RS_DEBUG->print("QG_BlockWidget::update()");
169 // int yPos = listBox->contentsY();
171 RS_Block * activeBlock = NULL;
173 if (blockList != NULL)
174 activeBlock = blockList->getActive();
178 if (blockList == NULL)
180 RS_DEBUG->print("QG_BlockWidget::update(): blockList is NULL");
184 for(uint i=0; i<blockList->count(); ++i)
186 RS_Block * blk = blockList->at(i);
188 if (!blk->isFrozen())
189 // listBox->insertItem(pxmVisible, blk->getName());
190 listBox->addItem(new QListWidgetItem(pxmVisible, blk->getName()));
192 // listBox->insertItem(pxmHidden, blk->getName());
193 listBox->addItem(new QListWidgetItem(pxmHidden, blk->getName()));
197 listBox->sortItems(Qt::AscendingOrder);
199 RS_Block * b = lastBlock;
200 highlightBlock(activeBlock);
203 // listBox->setContentsPos(0, yPos);
205 //highlightBlock(blockList->getActiveBlock());
206 //listBox->setContentsPos(0, yPos);
207 RS_DEBUG->print("QG_BlockWidget::update() done");
211 * Highlights (activates) the given block and makes it
212 * the active block in the blocklist.
214 void QG_BlockWidget::highlightBlock(RS_Block * block)
216 RS_DEBUG->print("QG_BlockWidget::highlightBlock()");
218 if (block == NULL || listBox == NULL)
221 blockList->activate(block);
222 QString name = block->getName();
224 for(int i=0; i<(int)listBox->count(); ++i)
226 QListWidgetItem * item = listBox->item(i);
228 // if (listBox->text(i) == name)
229 if (item->text() == name)
231 // listBox->setCurrentItem(i);
232 listBox->setCurrentRow(i);
238 /*virtual*/ void QG_BlockWidget::blockAdded(RS_Block *)
243 /*virtual*/ void QG_BlockWidget::blockEdited(RS_Block *)
248 /*virtual*/ void QG_BlockWidget::blockRemoved(RS_Block *)
253 /*virtual*/ void QG_BlockWidget::blockToggled(RS_Block *)
259 * Toggles the view of the given block. This is usually called when
260 * an item is double clicked.
263 void QG_BlockWidget::slotToggleView(QListBoxItem* item) {
264 RS_DEBUG->print("QG_BlockWidget::slotToggleView()");
266 if (item==NULL || blockList==NULL) {
270 int index = listBox->index(item);
271 RS_Block* block = blockList->find(item->text());
274 blockList->toggleBlock(item->text());
275 if (!block->isFrozen()) {
276 listBox->changeItem(pxmVisible, item->text(), index);
278 listBox->changeItem(*pxmHidden, item->text(), index);
286 * Called when the user activates (highlights) a block.
288 //void QG_BlockWidget::slotActivated(const QString & blockName)
289 void QG_BlockWidget::slotActivated(void)
291 QString blockName = listBox->currentItem()->text();
292 RS_DEBUG->print("QG_BlockWidget::slotActivated(): %s", blockName.toLatin1().data());
294 if (blockList == NULL)
297 lastBlock = blockList->getActive();
298 blockList->activate(blockName);
302 * Called for every mouse click.
304 //void QG_BlockWidget::slotMouseButtonClicked(int /*button*/, Q3ListBoxItem* item, const QPoint& pos)
305 void QG_BlockWidget::slotMouseButtonClicked(QListWidgetItem * item)
307 QPoint p = mapFromGlobal(QCursor::pos());
308 RS_Block * b = lastBlock;
312 actionHandler->slotBlocksToggleView();
317 if (item != NULL && blockList != NULL)
318 lastBlock = blockList->find(item->text());
323 * Shows a context menu for the block widget. Launched with a right click.
325 void QG_BlockWidget::contextMenuEvent(QContextMenuEvent * e)
327 #warning "Figure out how to port this crap (popup menu) to Qt4... !!! FIX !!!"
329 //QListBoxItem* item = listBox->selectedItem();
330 Q3PopupMenu * contextMenu = new Q3PopupMenu(this);
331 QLabel * caption = new QLabel(tr("Block Menu"), this);
332 caption->setPaletteBackgroundColor(Qt::black);
333 caption->setPaletteForegroundColor(Qt::white);
334 caption->setAlignment(Qt::AlignCenter);
335 contextMenu->insertItem(caption);
336 contextMenu->insertItem(tr("&Defreeze all Blocks"), actionHandler, SLOT(slotBlocksDefreezeAll()), 0);
337 contextMenu->insertItem(tr("&Freeze all Blocks"), actionHandler, SLOT(slotBlocksFreezeAll()), 0);
338 contextMenu->insertItem(tr("&Add Block"), actionHandler, SLOT(slotBlocksAdd()), 0);
339 contextMenu->insertItem(tr("&Remove Block"), actionHandler, SLOT(slotBlocksRemove()), 0);
340 contextMenu->insertItem(tr("&Rename Block"), actionHandler, SLOT(slotBlocksAttributes()), 0);
341 contextMenu->insertItem(tr("&Edit Block"), actionHandler, SLOT(slotBlocksEdit()), 0);
342 contextMenu->insertItem(tr("&Insert Block"), actionHandler, SLOT(slotBlocksInsert()), 0);
343 contextMenu->insertItem(tr("&Toggle Visibility"), actionHandler, SLOT(slotBlocksToggleView()), 0);
344 contextMenu->insertItem(tr("&Create New Block"), actionHandler, SLOT(slotBlocksCreate()), 0);
345 contextMenu->exec(QCursor::pos());
353 * Escape releases focus.
355 void QG_BlockWidget::keyPressEvent(QKeyEvent * e)
364 QWidget::keyPressEvent(e);