]> Shamusworld >> Repos - architektonas/blob - src/widgets/qg_blockwidget.cpp
9b9cbb43791223b2fcbdde81c007c40e484d3b63
[architektonas] / src / widgets / qg_blockwidget.cpp
1 // qg_blockwidget.cpp
2 //
3 // Originally part of QCad Community Edition by Andrew Mustun
4 // Extensively rewritten and refactored by James L. Hammons
5 // (C) 2010 Underground Software
6 //
7 // JLH = James L. Hammons <jlhamm@acm.org>
8 //
9 // Who  When        What
10 // ---  ----------  -----------------------------------------------------------
11 // JLH  05/10/2010  Added this text. :-)
12 //
13
14 #include "qg_blockwidget.h"
15
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"*/
23
24 /**
25  * Constructor.
26  */
27 QG_BlockWidget::QG_BlockWidget(QG_ActionHandler * ah, QWidget * parent, const char * name, Qt::WFlags f):
28 //      QWidget(parent, name, f),
29         QWidget(parent, 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")
48 {
49         actionHandler = ah;
50         blockList = NULL;
51         lastBlock = NULL;
52
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);
60
61 //    Q3VBoxLayout* lay = new Q3VBoxLayout(this, 0, -1, "lay");
62         QVBoxLayout * lay = new QVBoxLayout(this);
63
64         /*
65         QLabel* caption = new QLabel(tr("Block List"), this, "caption");
66         caption->setAlignment(Qt::AlignCenter);
67         caption->setPaletteBackgroundColor(black);
68         caption->setPaletteForegroundColor(white);
69         */
70
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();
75         QToolButton * but;
76         // show all blocks:
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);
85         // hide all blocks:
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);
94         // add block:
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);
103         // remove block:
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);
112         // edit attributes:
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);
121         // edit block:
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);
130         // insert block:
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);
139
140         //lay->addWidget(caption);
141         lay->addLayout(layButtons);
142         lay->addLayout(layButtons2);
143         lay->addWidget(listBox);
144
145         //connect(listBox, SIGNAL(doubleClicked(QListBoxItem*)),
146         //        actionHandler, SLOT(slotBlocksToggleView()));
147
148 //      connect(listBox, SIGNAL(highlighted(const QString&)), this, SLOT(slotActivated(const QString&)));
149         connect(listBox, SIGNAL(itemSelectionChanged(void)), this, SLOT(slotActivated(void)));
150
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 *)));
155
156         //boxLayout()->addWidget(listBox);
157 }
158
159 /**
160  * Destructor
161  */
162 QG_BlockWidget::~QG_BlockWidget()
163 {
164         delete listBox;
165 }
166
167 void QG_BlockWidget::setBlockList(RS_BlockList * blockList)
168 {
169         this->blockList = blockList;
170         update();
171 }
172
173 RS_BlockList * QG_BlockWidget::getBlockList()
174 {
175         return blockList;
176 }
177
178 /**
179  * Updates the block box from the blocks in the graphic.
180  */
181 void QG_BlockWidget::update()
182 {
183         RS_DEBUG->print("QG_BlockWidget::update()");
184
185 #warning "!!!"
186 //      int yPos = listBox->contentsY();
187
188         RS_Block * activeBlock = NULL;
189
190         if (blockList != NULL)
191                 activeBlock = blockList->getActive();
192
193         listBox->clear();
194
195         if (blockList == NULL)
196         {
197                 RS_DEBUG->print("QG_BlockWidget::update(): blockList is NULL");
198                 return;
199         }
200
201         for(uint i=0; i<blockList->count(); ++i)
202         {
203                 RS_Block * blk = blockList->at(i);
204
205                 if (!blk->isFrozen())
206 //                      listBox->insertItem(pxmVisible, blk->getName());
207                         listBox->addItem(new QListWidgetItem(pxmVisible, blk->getName()));
208                 else
209 //                      listBox->insertItem(pxmHidden, blk->getName());
210                         listBox->addItem(new QListWidgetItem(pxmHidden, blk->getName()));
211         }
212
213 //      listBox->sort();
214         listBox->sortItems(Qt::AscendingOrder);
215
216         RS_Block * b = lastBlock;
217         highlightBlock(activeBlock);
218         lastBlock = b;
219 #warning "!!!"
220 //      listBox->setContentsPos(0, yPos);
221
222         //highlightBlock(blockList->getActiveBlock());
223         //listBox->setContentsPos(0, yPos);
224         RS_DEBUG->print("QG_BlockWidget::update() done");
225 }
226
227 /**
228  * Highlights (activates) the given block and makes it
229  * the active block in the blocklist.
230  */
231 void QG_BlockWidget::highlightBlock(RS_Block * block)
232 {
233         RS_DEBUG->print("QG_BlockWidget::highlightBlock()");
234
235         if (block == NULL || listBox == NULL)
236                 return;
237
238         blockList->activate(block);
239         QString name = block->getName();
240
241         for(int i=0; i<(int)listBox->count(); ++i)
242         {
243                 QListWidgetItem * item = listBox->item(i);
244
245 //              if (listBox->text(i) == name)
246                 if (item->text() == name)
247                 {
248 //                      listBox->setCurrentItem(i);
249                         listBox->setCurrentRow(i);
250                         break;
251                 }
252         }
253 }
254
255 /*virtual*/ void QG_BlockWidget::blockAdded(RS_Block *)
256 {
257         update();
258 }
259
260 /*virtual*/ void QG_BlockWidget::blockEdited(RS_Block *)
261 {
262         update();
263 }
264
265 /*virtual*/ void QG_BlockWidget::blockRemoved(RS_Block *)
266 {
267         update();
268 }
269
270 /*virtual*/ void QG_BlockWidget::blockToggled(RS_Block *)
271 {
272         update();
273 }
274
275 /**
276  * Toggles the view of the given block. This is usually called when
277  * an item is double clicked.
278  */
279 /*
280 void QG_BlockWidget::slotToggleView(QListBoxItem* item) {
281     RS_DEBUG->print("QG_BlockWidget::slotToggleView()");
282
283     if (item==NULL || blockList==NULL) {
284         return;
285     }
286
287     int index = listBox->index(item);
288     RS_Block* block = blockList->find(item->text());
289
290     if (block!=NULL) {
291         blockList->toggleBlock(item->text());
292         if (!block->isFrozen()) {
293                 listBox->changeItem(pxmVisible, item->text(), index);
294         } else {
295             listBox->changeItem(*pxmHidden, item->text(), index);
296         }
297
298     }
299 }
300 */
301
302 /**
303  * Called when the user activates (highlights) a block.
304  */
305 //void QG_BlockWidget::slotActivated(const QString & blockName)
306 void QG_BlockWidget::slotActivated(void)
307 {
308         QString blockName = listBox->currentItem()->text();
309         RS_DEBUG->print("QG_BlockWidget::slotActivated(): %s", blockName.toLatin1().data());
310
311         if (blockList == NULL)
312                 return;
313
314         lastBlock = blockList->getActive();
315         blockList->activate(blockName);
316 }
317
318 /**
319  * Called for every mouse click.
320  */
321 //void QG_BlockWidget::slotMouseButtonClicked(int /*button*/, Q3ListBoxItem* item, const QPoint& pos)
322 void QG_BlockWidget::slotMouseButtonClicked(QListWidgetItem * item)
323 {
324         QPoint p = mapFromGlobal(QCursor::pos());
325         RS_Block * b = lastBlock;
326
327         if (p.x() < 23)
328         {
329                 actionHandler->slotBlocksToggleView();
330                 highlightBlock(b);
331         }
332         else
333         {
334                 if (item != NULL && blockList != NULL)
335                         lastBlock = blockList->find(item->text());
336         }
337 }
338
339 /**
340  * Shows a context menu for the block widget. Launched with a right click.
341  */
342 void QG_BlockWidget::contextMenuEvent(QContextMenuEvent * e)
343 {
344 #warning "Figure out how to port this crap (popup menu) to Qt4... !!! FIX !!!"
345 #if 0
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());
363         delete contextMenu;
364 #endif
365
366         e->accept();
367 }
368
369 /**
370  * Escape releases focus.
371  */
372 void QG_BlockWidget::keyPressEvent(QKeyEvent * e)
373 {
374         switch (e->key())
375         {
376         case Qt::Key_Escape:
377                 emit escape();
378                 break;
379
380         default:
381                 QWidget::keyPressEvent(e);
382                 break;
383         }
384 }