]> Shamusworld >> Repos - architektonas/blob - src/widgets/blockwidget.cpp
Bugfixes related to removing Snapper class.
[architektonas] / src / widgets / blockwidget.cpp
1 // blockwidget.cpp
2 //
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
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -----------------------------------------------------------
14 // JLH  05/10/2010  Added this text. :-)
15 //
16
17 #include "blockwidget.h"
18
19 #include "actionhandler.h"
20 #include "block.h"
21 #include "blocklist.h"
22 #include "debug.h"
23
24 /**
25  * Constructor.
26  */
27 BlockWidget::BlockWidget(ActionHandler * ah, QWidget * parent, const char * name, Qt::WFlags f):
28 //      QWidget(parent, name, f),
29         QWidget(parent, 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")
39 {
40         actionHandler = ah;
41         blockList = NULL;
42         lastBlock = NULL;
43
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);
51
52 //    Q3VBoxLayout* lay = new Q3VBoxLayout(this, 0, -1, "lay");
53         QVBoxLayout * lay = new QVBoxLayout(this);
54
55         /*
56         QLabel* caption = new QLabel(tr("Block List"), this, "caption");
57         caption->setAlignment(Qt::AlignCenter);
58         caption->setPaletteBackgroundColor(black);
59         caption->setPaletteForegroundColor(white);
60         */
61
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();
66         QToolButton * but;
67         // show all blocks:
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);
76         // hide all blocks:
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);
85         // add block:
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);
94         // remove block:
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);
103         // edit attributes:
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);
112         // edit block:
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);
121         // insert block:
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);
130
131         //lay->addWidget(caption);
132         lay->addLayout(layButtons);
133         lay->addLayout(layButtons2);
134         lay->addWidget(listBox);
135
136         //connect(listBox, SIGNAL(doubleClicked(QListBoxItem*)),
137         //        actionHandler, SLOT(slotBlocksToggleView()));
138
139 //      connect(listBox, SIGNAL(highlighted(const QString&)), this, SLOT(slotActivated(const QString&)));
140         connect(listBox, SIGNAL(itemSelectionChanged(void)), this, SLOT(slotActivated(void)));
141
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 *)));
146
147         //boxLayout()->addWidget(listBox);
148 }
149
150 /**
151  * Destructor
152  */
153 BlockWidget::~BlockWidget()
154 {
155         delete listBox;
156 }
157
158 void BlockWidget::setBlockList(BlockList * blockList)
159 {
160         this->blockList = blockList;
161         update();
162 }
163
164 BlockList * BlockWidget::getBlockList()
165 {
166         return blockList;
167 }
168
169 /**
170  * Updates the block box from the blocks in the graphic.
171  */
172 void BlockWidget::update()
173 {
174         DEBUG->print("BlockWidget::update()");
175
176 #warning "!!!"
177 //      int yPos = listBox->contentsY();
178
179         Block * activeBlock = NULL;
180
181         if (blockList)
182                 activeBlock = blockList->getActive();
183
184         listBox->clear();
185
186         if (!blockList)
187         {
188                 DEBUG->print("BlockWidget::update(): blockList is NULL");
189                 return;
190         }
191
192         for(uint i=0; i<blockList->count(); ++i)
193         {
194                 Block * blk = blockList->at(i);
195
196                 if (!blk->isFrozen())
197 //                      listBox->insertItem(pxmVisible, blk->getName());
198                         listBox->addItem(new QListWidgetItem(pxmVisible, blk->getName()));
199                 else
200 //                      listBox->insertItem(pxmHidden, blk->getName());
201                         listBox->addItem(new QListWidgetItem(pxmHidden, blk->getName()));
202         }
203
204 //      listBox->sort();
205         listBox->sortItems(Qt::AscendingOrder);
206
207         Block * b = lastBlock;
208         highlightBlock(activeBlock);
209         lastBlock = b;
210 #warning "!!!"
211 //      listBox->setContentsPos(0, yPos);
212
213         //highlightBlock(blockList->getActiveBlock());
214         //listBox->setContentsPos(0, yPos);
215         DEBUG->print("BlockWidget::update() done");
216 }
217
218 /**
219  * Highlights (activates) the given block and makes it
220  * the active block in the blocklist.
221  */
222 void BlockWidget::highlightBlock(Block * block)
223 {
224         DEBUG->print("BlockWidget::highlightBlock()");
225
226         if (!block || !listBox)
227                 return;
228
229         blockList->activate(block);
230         QString name = block->getName();
231
232         for(int i=0; i<(int)listBox->count(); ++i)
233         {
234                 QListWidgetItem * item = listBox->item(i);
235
236                 if (item->text() == name)
237                 {
238                         listBox->setCurrentRow(i);
239                         break;
240                 }
241         }
242 }
243
244 /*virtual*/ void BlockWidget::blockAdded(Block *)
245 {
246         update();
247 }
248
249 /*virtual*/ void BlockWidget::blockEdited(Block *)
250 {
251         update();
252 }
253
254 /*virtual*/ void BlockWidget::blockRemoved(Block *)
255 {
256         update();
257 }
258
259 /*virtual*/ void BlockWidget::blockToggled(Block *)
260 {
261         update();
262 }
263
264 /**
265  * Toggles the view of the given block. This is usually called when
266  * an item is double clicked.
267  */
268 /*
269 void BlockWidget::slotToggleView(QListBoxItem* item) {
270     DEBUG->print("BlockWidget::slotToggleView()");
271
272     if (item==NULL || blockList==NULL) {
273         return;
274     }
275
276     int index = listBox->index(item);
277     Block* block = blockList->find(item->text());
278
279     if (block!=NULL) {
280         blockList->toggleBlock(item->text());
281         if (!block->isFrozen()) {
282                 listBox->changeItem(pxmVisible, item->text(), index);
283         } else {
284             listBox->changeItem(*pxmHidden, item->text(), index);
285         }
286
287     }
288 }
289 */
290
291 /**
292  * Called when the user activates (highlights) a block.
293  */
294 //void BlockWidget::slotActivated(const QString & blockName)
295 void BlockWidget::slotActivated(void)
296 {
297         QString blockName = listBox->currentItem()->text();
298         DEBUG->print("BlockWidget::slotActivated(): %s", blockName.toLatin1().data());
299
300         if (!blockList)
301                 return;
302
303         lastBlock = blockList->getActive();
304         blockList->activate(blockName);
305 }
306
307 /**
308  * Called for every mouse click.
309  */
310 //void BlockWidget::slotMouseButtonClicked(int /*button*/, Q3ListBoxItem* item, const QPoint& pos)
311 void BlockWidget::slotMouseButtonClicked(QListWidgetItem * item)
312 {
313         QPoint p = mapFromGlobal(QCursor::pos());
314         Block * b = lastBlock;
315
316 //This is crap. We need to fix this so it's not so shitty.
317         if (p.x() < 23)
318         {
319                 actionHandler->slotBlocksToggleView();
320                 highlightBlock(b);
321         }
322         else
323         {
324                 if (item && blockList)
325                         lastBlock = blockList->find(item->text());
326         }
327 }
328
329 /**
330  * Shows a context menu for the block widget. Launched with a right click.
331  */
332 void BlockWidget::contextMenuEvent(QContextMenuEvent * e)
333 {
334 #if 0
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());
352         delete contextMenu;
353 #else
354 #warning "Figure out how to port this crap (popup menu) to Qt4... !!! FIX !!!"
355 #endif
356
357         e->accept();
358 }
359
360 /**
361  * Escape releases focus.
362  */
363 void BlockWidget::keyPressEvent(QKeyEvent * e)
364 {
365         switch (e->key())
366         {
367         case Qt::Key_Escape:
368                 emit escape();
369                 break;
370
371         default:
372                 QWidget::keyPressEvent(e);
373                 break;
374         }
375 }