3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
10 // JLH = James L. Hammons <jlhamm@acm.org>
13 // --- ---------- -----------------------------------------------------------
14 // JLH 05/10/2010 Added this text. :-)
17 #include "qg_blockwidget.h"
22 QG_BlockWidget::QG_BlockWidget(QG_ActionHandler * ah, QWidget * parent, const char * name, Qt::WFlags f):
23 // QWidget(parent, name, f),
25 pxmVisible(":/res/visibleblock.xpm"),
26 pxmHidden(":/res/hiddenblock.xpm"),
27 pxmAdd(":/res/blockadd.xpm"),
28 pxmRemove(":/res/blockremove.xpm"),
29 pxmAttributes(":/res/blockattributes.xpm"),
30 pxmEdit(":/res/blockedit.xpm"),
31 pxmInsert(":/res/blockinsert.xpm"),
32 pxmDefreezeAll(":/res/visibleblock.xpm"),
33 pxmFreezeAll(":/res/hiddenblock.xpm")
39 // listBox = new Q3ListBox(this, "blockbox");
40 listBox = new QListWidget(this);
41 #warning "!!! !!! !!!"
42 // listBox->setDragSelect(false);
43 // listBox->setMultiSelection(false);
44 // listBox->setSmoothScrolling(true);
45 listBox->setFocusPolicy(Qt::NoFocus);
47 // Q3VBoxLayout* lay = new Q3VBoxLayout(this, 0, -1, "lay");
48 QVBoxLayout * lay = new QVBoxLayout(this);
51 QLabel* caption = new QLabel(tr("Block List"), this, "caption");
52 caption->setAlignment(Qt::AlignCenter);
53 caption->setPaletteBackgroundColor(black);
54 caption->setPaletteForegroundColor(white);
57 // Q3HBoxLayout* layButtons = new Q3HBoxLayout(NULL, 0, -1, "layButtons");
58 // Q3HBoxLayout* layButtons2 = new Q3HBoxLayout(NULL, 0, -1, "layButtons2");
59 QHBoxLayout * layButtons = new QHBoxLayout();
60 QHBoxLayout * layButtons2 = new QHBoxLayout();
63 but = new QToolButton(this);
64 // but->setPixmap(pxmDefreezeAll);
65 but->setIcon(QIcon(pxmDefreezeAll));
66 but->setMinimumSize(QSize(22, 22));
67 // QToolTip::add(but, tr("Show all blocks"));
68 but->setToolTip(tr("Show all blocks"));
69 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksDefreezeAll()));
70 layButtons->addWidget(but);
72 but = new QToolButton(this);
73 // but->setPixmap(pxmFreezeAll);
74 but->setIcon(QIcon(pxmFreezeAll));
75 but->setMinimumSize(QSize(22, 22));
76 // QToolTip::add(but, tr("Hide all blocks"));
77 but->setToolTip(tr("Hide all blocks"));
78 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksFreezeAll()));
79 layButtons->addWidget(but);
81 but = new QToolButton(this);
82 // but->setPixmap(pxmAdd);
83 but->setIcon(QIcon(pxmAdd));
84 but->setMinimumSize(QSize(22, 22));
85 // QToolTip::add(but, tr("Add a block"));
86 but->setToolTip(tr("Add a block"));
87 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksAdd()));
88 layButtons->addWidget(but);
90 but = new QToolButton(this);
91 // but->setPixmap(pxmRemove);
92 but->setIcon(QIcon(pxmRemove));
93 but->setMinimumSize(QSize(22, 22));
94 // QToolTip::add(but, tr("Remove the active block"));
95 but->setToolTip(tr("Remove the active block"));
96 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksRemove()));
97 layButtons->addWidget(but);
99 but = new QToolButton(this);
100 // but->setPixmap(pxmAttributes);
101 but->setIcon(QIcon(pxmAttributes));
102 but->setMinimumSize(QSize(22, 22));
103 // QToolTip::add(but, tr("Rename the active block"));
104 but->setToolTip(tr("Rename the active block"));
105 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksAttributes()));
106 layButtons->addWidget(but);
108 but = new QToolButton(this);
109 // but->setPixmap(pxmEdit);
110 but->setIcon(QIcon(pxmEdit));
111 but->setMinimumSize(QSize(22, 22));
112 // QToolTip::add(but, tr("Edit the active block\nin a separate window"));
113 but->setToolTip(tr("Edit the active block\nin a separate window"));
114 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksEdit()));
115 layButtons2->addWidget(but);
117 but = new QToolButton(this);
118 // but->setPixmap(pxmInsert);
119 but->setIcon(QIcon(pxmInsert));
120 but->setMinimumSize(QSize(22, 22));
121 // QToolTip::add(but, tr("Insert the active block"));
122 but->setToolTip(tr("Insert the active block"));
123 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksInsert()));
124 layButtons2->addWidget(but);
126 //lay->addWidget(caption);
127 lay->addLayout(layButtons);
128 lay->addLayout(layButtons2);
129 lay->addWidget(listBox);
131 //connect(listBox, SIGNAL(doubleClicked(QListBoxItem*)),
132 // actionHandler, SLOT(slotBlocksToggleView()));
134 // connect(listBox, SIGNAL(highlighted(const QString&)), this, SLOT(slotActivated(const QString&)));
135 connect(listBox, SIGNAL(itemSelectionChanged(void)), this, SLOT(slotActivated(void)));
137 // connect(listBox, SIGNAL(mouseButtonClicked(int, Q3ListBoxItem*, const QPoint&)),
138 // this, SLOT(slotMouseButtonClicked(int, Q3ListBoxItem*, const QPoint&)));
139 connect(listBox, SIGNAL(itemClicked(QListWidgetItem *)),
140 this, SLOT(slotMouseButtonClicked(QListWidgetItem *)));
142 //boxLayout()->addWidget(listBox);
148 QG_BlockWidget::~QG_BlockWidget()
153 void QG_BlockWidget::setBlockList(RS_BlockList * blockList)
155 this->blockList = blockList;
159 RS_BlockList * QG_BlockWidget::getBlockList()
165 * Updates the block box from the blocks in the graphic.
167 void QG_BlockWidget::update()
169 RS_DEBUG->print("QG_BlockWidget::update()");
172 // int yPos = listBox->contentsY();
174 RS_Block * activeBlock = NULL;
176 if (blockList != NULL)
177 activeBlock = blockList->getActive();
181 if (blockList == NULL)
183 RS_DEBUG->print("QG_BlockWidget::update(): blockList is NULL");
187 for(uint i=0; i<blockList->count(); ++i)
189 RS_Block * blk = blockList->at(i);
191 if (!blk->isFrozen())
192 // listBox->insertItem(pxmVisible, blk->getName());
193 listBox->addItem(new QListWidgetItem(pxmVisible, blk->getName()));
195 // listBox->insertItem(pxmHidden, blk->getName());
196 listBox->addItem(new QListWidgetItem(pxmHidden, blk->getName()));
200 listBox->sortItems(Qt::AscendingOrder);
202 RS_Block * b = lastBlock;
203 highlightBlock(activeBlock);
206 // listBox->setContentsPos(0, yPos);
208 //highlightBlock(blockList->getActiveBlock());
209 //listBox->setContentsPos(0, yPos);
210 RS_DEBUG->print("QG_BlockWidget::update() done");
214 * Highlights (activates) the given block and makes it
215 * the active block in the blocklist.
217 void QG_BlockWidget::highlightBlock(RS_Block * block)
219 RS_DEBUG->print("QG_BlockWidget::highlightBlock()");
221 if (block == NULL || listBox == NULL)
224 blockList->activate(block);
225 QString name = block->getName();
227 for(int i=0; i<(int)listBox->count(); ++i)
229 QListWidgetItem * item = listBox->item(i);
231 // if (listBox->text(i) == name)
232 if (item->text() == name)
234 // listBox->setCurrentItem(i);
235 listBox->setCurrentRow(i);
241 /*virtual*/ void QG_BlockWidget::blockAdded(RS_Block *)
246 /*virtual*/ void QG_BlockWidget::blockEdited(RS_Block *)
251 /*virtual*/ void QG_BlockWidget::blockRemoved(RS_Block *)
256 /*virtual*/ void QG_BlockWidget::blockToggled(RS_Block *)
262 * Toggles the view of the given block. This is usually called when
263 * an item is double clicked.
266 void QG_BlockWidget::slotToggleView(QListBoxItem* item) {
267 RS_DEBUG->print("QG_BlockWidget::slotToggleView()");
269 if (item==NULL || blockList==NULL) {
273 int index = listBox->index(item);
274 RS_Block* block = blockList->find(item->text());
277 blockList->toggleBlock(item->text());
278 if (!block->isFrozen()) {
279 listBox->changeItem(pxmVisible, item->text(), index);
281 listBox->changeItem(*pxmHidden, item->text(), index);
289 * Called when the user activates (highlights) a block.
291 //void QG_BlockWidget::slotActivated(const QString & blockName)
292 void QG_BlockWidget::slotActivated(void)
294 QString blockName = listBox->currentItem()->text();
295 RS_DEBUG->print("QG_BlockWidget::slotActivated(): %s", blockName.toLatin1().data());
297 if (blockList == NULL)
300 lastBlock = blockList->getActive();
301 blockList->activate(blockName);
305 * Called for every mouse click.
307 //void QG_BlockWidget::slotMouseButtonClicked(int /*button*/, Q3ListBoxItem* item, const QPoint& pos)
308 void QG_BlockWidget::slotMouseButtonClicked(QListWidgetItem * item)
310 QPoint p = mapFromGlobal(QCursor::pos());
311 RS_Block * b = lastBlock;
315 actionHandler->slotBlocksToggleView();
320 if (item != NULL && blockList != NULL)
321 lastBlock = blockList->find(item->text());
326 * Shows a context menu for the block widget. Launched with a right click.
328 void QG_BlockWidget::contextMenuEvent(QContextMenuEvent * e)
331 //QListBoxItem* item = listBox->selectedItem();
332 Q3PopupMenu * contextMenu = new Q3PopupMenu(this);
333 QLabel * caption = new QLabel(tr("Block Menu"), this);
334 caption->setPaletteBackgroundColor(Qt::black);
335 caption->setPaletteForegroundColor(Qt::white);
336 caption->setAlignment(Qt::AlignCenter);
337 contextMenu->insertItem(caption);
338 contextMenu->insertItem(tr("&Defreeze all Blocks"), actionHandler, SLOT(slotBlocksDefreezeAll()), 0);
339 contextMenu->insertItem(tr("&Freeze all Blocks"), actionHandler, SLOT(slotBlocksFreezeAll()), 0);
340 contextMenu->insertItem(tr("&Add Block"), actionHandler, SLOT(slotBlocksAdd()), 0);
341 contextMenu->insertItem(tr("&Remove Block"), actionHandler, SLOT(slotBlocksRemove()), 0);
342 contextMenu->insertItem(tr("&Rename Block"), actionHandler, SLOT(slotBlocksAttributes()), 0);
343 contextMenu->insertItem(tr("&Edit Block"), actionHandler, SLOT(slotBlocksEdit()), 0);
344 contextMenu->insertItem(tr("&Insert Block"), actionHandler, SLOT(slotBlocksInsert()), 0);
345 contextMenu->insertItem(tr("&Toggle Visibility"), actionHandler, SLOT(slotBlocksToggleView()), 0);
346 contextMenu->insertItem(tr("&Create New Block"), actionHandler, SLOT(slotBlocksCreate()), 0);
347 contextMenu->exec(QCursor::pos());
350 #warning "Figure out how to port this crap (popup menu) to Qt4... !!! FIX !!!"
357 * Escape releases focus.
359 void QG_BlockWidget::keyPressEvent(QKeyEvent * e)
368 QWidget::keyPressEvent(e);