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 "blockwidget.h"
19 #include "actionhandler.h"
21 #include "blocklist.h"
27 BlockWidget::BlockWidget(ActionHandler * ah, QWidget * parent, const char * name, Qt::WFlags f):
28 // QWidget(parent, name, f),
30 pxmVisible(":/res/visibleblock.xpm"),
31 pxmHidden(":/res/hiddenblock.xpm"),
32 pxmAdd(":/res/blockadd.xpm"),
33 pxmRemove(":/res/blockremove.xpm"),
34 pxmAttributes(":/res/blockattributes.xpm"),
35 pxmEdit(":/res/blockedit.xpm"),
36 pxmInsert(":/res/blockinsert.xpm"),
37 pxmDefreezeAll(":/res/visibleblock.xpm"),
38 pxmFreezeAll(":/res/hiddenblock.xpm")
44 // listBox = new Q3ListBox(this, "blockbox");
45 listBox = new QListWidget(this);
46 #warning "!!! !!! !!!"
47 // listBox->setDragSelect(false);
48 // listBox->setMultiSelection(false);
49 // listBox->setSmoothScrolling(true);
50 listBox->setFocusPolicy(Qt::NoFocus);
52 // Q3VBoxLayout* lay = new Q3VBoxLayout(this, 0, -1, "lay");
53 QVBoxLayout * lay = new QVBoxLayout(this);
56 QLabel* caption = new QLabel(tr("Block List"), this, "caption");
57 caption->setAlignment(Qt::AlignCenter);
58 caption->setPaletteBackgroundColor(black);
59 caption->setPaletteForegroundColor(white);
62 // Q3HBoxLayout* layButtons = new Q3HBoxLayout(NULL, 0, -1, "layButtons");
63 // Q3HBoxLayout* layButtons2 = new Q3HBoxLayout(NULL, 0, -1, "layButtons2");
64 QHBoxLayout * layButtons = new QHBoxLayout();
65 QHBoxLayout * layButtons2 = new QHBoxLayout();
68 but = new QToolButton(this);
69 // but->setPixmap(pxmDefreezeAll);
70 but->setIcon(QIcon(pxmDefreezeAll));
71 but->setMinimumSize(QSize(22, 22));
72 // QToolTip::add(but, tr("Show all blocks"));
73 but->setToolTip(tr("Show all blocks"));
74 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksDefreezeAll()));
75 layButtons->addWidget(but);
77 but = new QToolButton(this);
78 // but->setPixmap(pxmFreezeAll);
79 but->setIcon(QIcon(pxmFreezeAll));
80 but->setMinimumSize(QSize(22, 22));
81 // QToolTip::add(but, tr("Hide all blocks"));
82 but->setToolTip(tr("Hide all blocks"));
83 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksFreezeAll()));
84 layButtons->addWidget(but);
86 but = new QToolButton(this);
87 // but->setPixmap(pxmAdd);
88 but->setIcon(QIcon(pxmAdd));
89 but->setMinimumSize(QSize(22, 22));
90 // QToolTip::add(but, tr("Add a block"));
91 but->setToolTip(tr("Add a block"));
92 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksAdd()));
93 layButtons->addWidget(but);
95 but = new QToolButton(this);
96 // but->setPixmap(pxmRemove);
97 but->setIcon(QIcon(pxmRemove));
98 but->setMinimumSize(QSize(22, 22));
99 // QToolTip::add(but, tr("Remove the active block"));
100 but->setToolTip(tr("Remove the active block"));
101 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksRemove()));
102 layButtons->addWidget(but);
104 but = new QToolButton(this);
105 // but->setPixmap(pxmAttributes);
106 but->setIcon(QIcon(pxmAttributes));
107 but->setMinimumSize(QSize(22, 22));
108 // QToolTip::add(but, tr("Rename the active block"));
109 but->setToolTip(tr("Rename the active block"));
110 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksAttributes()));
111 layButtons->addWidget(but);
113 but = new QToolButton(this);
114 // but->setPixmap(pxmEdit);
115 but->setIcon(QIcon(pxmEdit));
116 but->setMinimumSize(QSize(22, 22));
117 // QToolTip::add(but, tr("Edit the active block\nin a separate window"));
118 but->setToolTip(tr("Edit the active block\nin a separate window"));
119 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksEdit()));
120 layButtons2->addWidget(but);
122 but = new QToolButton(this);
123 // but->setPixmap(pxmInsert);
124 but->setIcon(QIcon(pxmInsert));
125 but->setMinimumSize(QSize(22, 22));
126 // QToolTip::add(but, tr("Insert the active block"));
127 but->setToolTip(tr("Insert the active block"));
128 connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksInsert()));
129 layButtons2->addWidget(but);
131 //lay->addWidget(caption);
132 lay->addLayout(layButtons);
133 lay->addLayout(layButtons2);
134 lay->addWidget(listBox);
136 //connect(listBox, SIGNAL(doubleClicked(QListBoxItem*)),
137 // actionHandler, SLOT(slotBlocksToggleView()));
139 // connect(listBox, SIGNAL(highlighted(const QString&)), this, SLOT(slotActivated(const QString&)));
140 connect(listBox, SIGNAL(itemSelectionChanged(void)), this, SLOT(slotActivated(void)));
142 // connect(listBox, SIGNAL(mouseButtonClicked(int, Q3ListBoxItem*, const QPoint&)),
143 // this, SLOT(slotMouseButtonClicked(int, Q3ListBoxItem*, const QPoint&)));
144 connect(listBox, SIGNAL(itemClicked(QListWidgetItem *)),
145 this, SLOT(slotMouseButtonClicked(QListWidgetItem *)));
147 //boxLayout()->addWidget(listBox);
153 BlockWidget::~BlockWidget()
158 void BlockWidget::setBlockList(BlockList * blockList)
160 this->blockList = blockList;
164 BlockList * BlockWidget::getBlockList()
170 * Updates the block box from the blocks in the graphic.
172 void BlockWidget::update()
174 DEBUG->print("BlockWidget::update()");
177 // int yPos = listBox->contentsY();
179 Block * activeBlock = NULL;
182 activeBlock = blockList->getActive();
188 DEBUG->print("BlockWidget::update(): blockList is NULL");
192 for(uint i=0; i<blockList->count(); ++i)
194 Block * blk = blockList->at(i);
196 if (!blk->isFrozen())
197 // listBox->insertItem(pxmVisible, blk->getName());
198 listBox->addItem(new QListWidgetItem(pxmVisible, blk->getName()));
200 // listBox->insertItem(pxmHidden, blk->getName());
201 listBox->addItem(new QListWidgetItem(pxmHidden, blk->getName()));
205 listBox->sortItems(Qt::AscendingOrder);
207 Block * b = lastBlock;
208 highlightBlock(activeBlock);
211 // listBox->setContentsPos(0, yPos);
213 //highlightBlock(blockList->getActiveBlock());
214 //listBox->setContentsPos(0, yPos);
215 DEBUG->print("BlockWidget::update() done");
219 * Highlights (activates) the given block and makes it
220 * the active block in the blocklist.
222 void BlockWidget::highlightBlock(Block * block)
224 DEBUG->print("BlockWidget::highlightBlock()");
226 if (!block || !listBox)
229 blockList->activate(block);
230 QString name = block->getName();
232 for(int i=0; i<(int)listBox->count(); ++i)
234 QListWidgetItem * item = listBox->item(i);
236 if (item->text() == name)
238 listBox->setCurrentRow(i);
244 /*virtual*/ void BlockWidget::blockAdded(Block *)
249 /*virtual*/ void BlockWidget::blockEdited(Block *)
254 /*virtual*/ void BlockWidget::blockRemoved(Block *)
259 /*virtual*/ void BlockWidget::blockToggled(Block *)
265 * Toggles the view of the given block. This is usually called when
266 * an item is double clicked.
269 void BlockWidget::slotToggleView(QListBoxItem* item) {
270 DEBUG->print("BlockWidget::slotToggleView()");
272 if (item==NULL || blockList==NULL) {
276 int index = listBox->index(item);
277 Block* block = blockList->find(item->text());
280 blockList->toggleBlock(item->text());
281 if (!block->isFrozen()) {
282 listBox->changeItem(pxmVisible, item->text(), index);
284 listBox->changeItem(*pxmHidden, item->text(), index);
292 * Called when the user activates (highlights) a block.
294 //void BlockWidget::slotActivated(const QString & blockName)
295 void BlockWidget::slotActivated(void)
297 QString blockName = listBox->currentItem()->text();
298 DEBUG->print("BlockWidget::slotActivated(): %s", blockName.toLatin1().data());
303 lastBlock = blockList->getActive();
304 blockList->activate(blockName);
308 * Called for every mouse click.
310 //void BlockWidget::slotMouseButtonClicked(int /*button*/, Q3ListBoxItem* item, const QPoint& pos)
311 void BlockWidget::slotMouseButtonClicked(QListWidgetItem * item)
313 QPoint p = mapFromGlobal(QCursor::pos());
314 Block * b = lastBlock;
316 //This is crap. We need to fix this so it's not so shitty.
319 actionHandler->slotBlocksToggleView();
324 if (item && blockList)
325 lastBlock = blockList->find(item->text());
330 * Shows a context menu for the block widget. Launched with a right click.
332 void BlockWidget::contextMenuEvent(QContextMenuEvent * e)
335 //QListBoxItem* item = listBox->selectedItem();
336 Q3PopupMenu * contextMenu = new Q3PopupMenu(this);
337 QLabel * caption = new QLabel(tr("Block Menu"), this);
338 caption->setPaletteBackgroundColor(Qt::black);
339 caption->setPaletteForegroundColor(Qt::white);
340 caption->setAlignment(Qt::AlignCenter);
341 contextMenu->insertItem(caption);
342 contextMenu->insertItem(tr("&Defreeze all Blocks"), actionHandler, SLOT(slotBlocksDefreezeAll()), 0);
343 contextMenu->insertItem(tr("&Freeze all Blocks"), actionHandler, SLOT(slotBlocksFreezeAll()), 0);
344 contextMenu->insertItem(tr("&Add Block"), actionHandler, SLOT(slotBlocksAdd()), 0);
345 contextMenu->insertItem(tr("&Remove Block"), actionHandler, SLOT(slotBlocksRemove()), 0);
346 contextMenu->insertItem(tr("&Rename Block"), actionHandler, SLOT(slotBlocksAttributes()), 0);
347 contextMenu->insertItem(tr("&Edit Block"), actionHandler, SLOT(slotBlocksEdit()), 0);
348 contextMenu->insertItem(tr("&Insert Block"), actionHandler, SLOT(slotBlocksInsert()), 0);
349 contextMenu->insertItem(tr("&Toggle Visibility"), actionHandler, SLOT(slotBlocksToggleView()), 0);
350 contextMenu->insertItem(tr("&Create New Block"), actionHandler, SLOT(slotBlocksCreate()), 0);
351 contextMenu->exec(QCursor::pos());
354 #warning "Figure out how to port this crap (popup menu) to Qt4... !!! FIX !!!"
361 * Escape releases focus.
363 void BlockWidget::keyPressEvent(QKeyEvent * e)
372 QWidget::keyPressEvent(e);