]> Shamusworld >> Repos - architektonas/blob - src/widgets/qg_blockwidget.cpp
1c94e2296c4e7e4266346c543ffe8e318adbd44b
[architektonas] / src / widgets / qg_blockwidget.cpp
1 // qg_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 "qg_blockwidget.h"
18
19 /**
20  * Constructor.
21  */
22 QG_BlockWidget::QG_BlockWidget(QG_ActionHandler * ah, QWidget * parent, const char * name, Qt::WFlags f):
23 //      QWidget(parent, name, f),
24         QWidget(parent, 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")
34 {
35         actionHandler = ah;
36         blockList = NULL;
37         lastBlock = NULL;
38
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);
46
47 //    Q3VBoxLayout* lay = new Q3VBoxLayout(this, 0, -1, "lay");
48         QVBoxLayout * lay = new QVBoxLayout(this);
49
50         /*
51         QLabel* caption = new QLabel(tr("Block List"), this, "caption");
52         caption->setAlignment(Qt::AlignCenter);
53         caption->setPaletteBackgroundColor(black);
54         caption->setPaletteForegroundColor(white);
55         */
56
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();
61         QToolButton * but;
62         // show all blocks:
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);
71         // hide all blocks:
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);
80         // add block:
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);
89         // remove block:
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);
98         // edit attributes:
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);
107         // edit block:
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);
116         // insert block:
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);
125
126         //lay->addWidget(caption);
127         lay->addLayout(layButtons);
128         lay->addLayout(layButtons2);
129         lay->addWidget(listBox);
130
131         //connect(listBox, SIGNAL(doubleClicked(QListBoxItem*)),
132         //        actionHandler, SLOT(slotBlocksToggleView()));
133
134 //      connect(listBox, SIGNAL(highlighted(const QString&)), this, SLOT(slotActivated(const QString&)));
135         connect(listBox, SIGNAL(itemSelectionChanged(void)), this, SLOT(slotActivated(void)));
136
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 *)));
141
142         //boxLayout()->addWidget(listBox);
143 }
144
145 /**
146  * Destructor
147  */
148 QG_BlockWidget::~QG_BlockWidget()
149 {
150         delete listBox;
151 }
152
153 void QG_BlockWidget::setBlockList(RS_BlockList * blockList)
154 {
155         this->blockList = blockList;
156         update();
157 }
158
159 RS_BlockList * QG_BlockWidget::getBlockList()
160 {
161         return blockList;
162 }
163
164 /**
165  * Updates the block box from the blocks in the graphic.
166  */
167 void QG_BlockWidget::update()
168 {
169         RS_DEBUG->print("QG_BlockWidget::update()");
170
171 #warning "!!!"
172 //      int yPos = listBox->contentsY();
173
174         RS_Block * activeBlock = NULL;
175
176         if (blockList != NULL)
177                 activeBlock = blockList->getActive();
178
179         listBox->clear();
180
181         if (blockList == NULL)
182         {
183                 RS_DEBUG->print("QG_BlockWidget::update(): blockList is NULL");
184                 return;
185         }
186
187         for(uint i=0; i<blockList->count(); ++i)
188         {
189                 RS_Block * blk = blockList->at(i);
190
191                 if (!blk->isFrozen())
192 //                      listBox->insertItem(pxmVisible, blk->getName());
193                         listBox->addItem(new QListWidgetItem(pxmVisible, blk->getName()));
194                 else
195 //                      listBox->insertItem(pxmHidden, blk->getName());
196                         listBox->addItem(new QListWidgetItem(pxmHidden, blk->getName()));
197         }
198
199 //      listBox->sort();
200         listBox->sortItems(Qt::AscendingOrder);
201
202         RS_Block * b = lastBlock;
203         highlightBlock(activeBlock);
204         lastBlock = b;
205 #warning "!!!"
206 //      listBox->setContentsPos(0, yPos);
207
208         //highlightBlock(blockList->getActiveBlock());
209         //listBox->setContentsPos(0, yPos);
210         RS_DEBUG->print("QG_BlockWidget::update() done");
211 }
212
213 /**
214  * Highlights (activates) the given block and makes it
215  * the active block in the blocklist.
216  */
217 void QG_BlockWidget::highlightBlock(RS_Block * block)
218 {
219         RS_DEBUG->print("QG_BlockWidget::highlightBlock()");
220
221         if (block == NULL || listBox == NULL)
222                 return;
223
224         blockList->activate(block);
225         QString name = block->getName();
226
227         for(int i=0; i<(int)listBox->count(); ++i)
228         {
229                 QListWidgetItem * item = listBox->item(i);
230
231 //              if (listBox->text(i) == name)
232                 if (item->text() == name)
233                 {
234 //                      listBox->setCurrentItem(i);
235                         listBox->setCurrentRow(i);
236                         break;
237                 }
238         }
239 }
240
241 /*virtual*/ void QG_BlockWidget::blockAdded(RS_Block *)
242 {
243         update();
244 }
245
246 /*virtual*/ void QG_BlockWidget::blockEdited(RS_Block *)
247 {
248         update();
249 }
250
251 /*virtual*/ void QG_BlockWidget::blockRemoved(RS_Block *)
252 {
253         update();
254 }
255
256 /*virtual*/ void QG_BlockWidget::blockToggled(RS_Block *)
257 {
258         update();
259 }
260
261 /**
262  * Toggles the view of the given block. This is usually called when
263  * an item is double clicked.
264  */
265 /*
266 void QG_BlockWidget::slotToggleView(QListBoxItem* item) {
267     RS_DEBUG->print("QG_BlockWidget::slotToggleView()");
268
269     if (item==NULL || blockList==NULL) {
270         return;
271     }
272
273     int index = listBox->index(item);
274     RS_Block* block = blockList->find(item->text());
275
276     if (block!=NULL) {
277         blockList->toggleBlock(item->text());
278         if (!block->isFrozen()) {
279                 listBox->changeItem(pxmVisible, item->text(), index);
280         } else {
281             listBox->changeItem(*pxmHidden, item->text(), index);
282         }
283
284     }
285 }
286 */
287
288 /**
289  * Called when the user activates (highlights) a block.
290  */
291 //void QG_BlockWidget::slotActivated(const QString & blockName)
292 void QG_BlockWidget::slotActivated(void)
293 {
294         QString blockName = listBox->currentItem()->text();
295         RS_DEBUG->print("QG_BlockWidget::slotActivated(): %s", blockName.toLatin1().data());
296
297         if (blockList == NULL)
298                 return;
299
300         lastBlock = blockList->getActive();
301         blockList->activate(blockName);
302 }
303
304 /**
305  * Called for every mouse click.
306  */
307 //void QG_BlockWidget::slotMouseButtonClicked(int /*button*/, Q3ListBoxItem* item, const QPoint& pos)
308 void QG_BlockWidget::slotMouseButtonClicked(QListWidgetItem * item)
309 {
310         QPoint p = mapFromGlobal(QCursor::pos());
311         RS_Block * b = lastBlock;
312
313         if (p.x() < 23)
314         {
315                 actionHandler->slotBlocksToggleView();
316                 highlightBlock(b);
317         }
318         else
319         {
320                 if (item != NULL && blockList != NULL)
321                         lastBlock = blockList->find(item->text());
322         }
323 }
324
325 /**
326  * Shows a context menu for the block widget. Launched with a right click.
327  */
328 void QG_BlockWidget::contextMenuEvent(QContextMenuEvent * e)
329 {
330 #if 0
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());
348         delete contextMenu;
349 #else
350 #warning "Figure out how to port this crap (popup menu) to Qt4... !!! FIX !!!"
351 #endif
352
353         e->accept();
354 }
355
356 /**
357  * Escape releases focus.
358  */
359 void QG_BlockWidget::keyPressEvent(QKeyEvent * e)
360 {
361         switch (e->key())
362         {
363         case Qt::Key_Escape:
364                 emit escape();
365                 break;
366
367         default:
368                 QWidget::keyPressEvent(e);
369                 break;
370         }
371 }