]> Shamusworld >> Repos - architektonas/blob - src/widgets/qg_blockwidget.cpp
Start of bringing back missing forms/dialogs
[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 /**
17  * Constructor.
18  */
19 QG_BlockWidget::QG_BlockWidget(QG_ActionHandler * ah, QWidget * parent, const char * name, Qt::WFlags f):
20 //      QWidget(parent, name, f),
21         QWidget(parent, f),
22         pxmVisible(":/res/visibleblock.xpm"),
23         pxmHidden(":/res/hiddenblock.xpm"),
24         pxmAdd(":/res/blockadd.xpm"),
25         pxmRemove(":/res/blockremove.xpm"),
26         pxmAttributes(":/res/blockattributes.xpm"),
27         pxmEdit(":/res/blockedit.xpm"),
28         pxmInsert(":/res/blockinsert.xpm"),
29         pxmDefreezeAll(":/res/visibleblock.xpm"),
30         pxmFreezeAll(":/res/hiddenblock.xpm")
31 {
32         actionHandler = ah;
33         blockList = NULL;
34         lastBlock = NULL;
35
36 //    listBox = new Q3ListBox(this, "blockbox");
37         listBox = new QListWidget(this);
38 #warning "!!! !!! !!!"
39 //    listBox->setDragSelect(false);
40 //    listBox->setMultiSelection(false);
41 //    listBox->setSmoothScrolling(true);
42         listBox->setFocusPolicy(Qt::NoFocus);
43
44 //    Q3VBoxLayout* lay = new Q3VBoxLayout(this, 0, -1, "lay");
45         QVBoxLayout * lay = new QVBoxLayout(this);
46
47         /*
48         QLabel* caption = new QLabel(tr("Block List"), this, "caption");
49         caption->setAlignment(Qt::AlignCenter);
50         caption->setPaletteBackgroundColor(black);
51         caption->setPaletteForegroundColor(white);
52         */
53
54 //    Q3HBoxLayout* layButtons = new Q3HBoxLayout(NULL, 0, -1, "layButtons");
55 //    Q3HBoxLayout* layButtons2 = new Q3HBoxLayout(NULL, 0, -1, "layButtons2");
56         QHBoxLayout * layButtons = new QHBoxLayout();
57         QHBoxLayout * layButtons2 = new QHBoxLayout();
58         QToolButton * but;
59         // show all blocks:
60         but = new QToolButton(this);
61 //    but->setPixmap(pxmDefreezeAll);
62         but->setIcon(QIcon(pxmDefreezeAll));
63         but->setMinimumSize(QSize(22, 22));
64 //      QToolTip::add(but, tr("Show all blocks"));
65         but->setToolTip(tr("Show all blocks"));
66         connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksDefreezeAll()));
67         layButtons->addWidget(but);
68         // hide all blocks:
69         but = new QToolButton(this);
70 //    but->setPixmap(pxmFreezeAll);
71         but->setIcon(QIcon(pxmFreezeAll));
72         but->setMinimumSize(QSize(22, 22));
73 //      QToolTip::add(but, tr("Hide all blocks"));
74         but->setToolTip(tr("Hide all blocks"));
75         connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksFreezeAll()));
76         layButtons->addWidget(but);
77         // add block:
78         but = new QToolButton(this);
79 //    but->setPixmap(pxmAdd);
80         but->setIcon(QIcon(pxmAdd));
81         but->setMinimumSize(QSize(22, 22));
82 //      QToolTip::add(but, tr("Add a block"));
83         but->setToolTip(tr("Add a block"));
84         connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksAdd()));
85         layButtons->addWidget(but);
86         // remove block:
87         but = new QToolButton(this);
88 //    but->setPixmap(pxmRemove);
89         but->setIcon(QIcon(pxmRemove));
90         but->setMinimumSize(QSize(22, 22));
91 //      QToolTip::add(but, tr("Remove the active block"));
92         but->setToolTip(tr("Remove the active block"));
93         connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksRemove()));
94         layButtons->addWidget(but);
95         // edit attributes:
96         but = new QToolButton(this);
97 //      but->setPixmap(pxmAttributes);
98         but->setIcon(QIcon(pxmAttributes));
99         but->setMinimumSize(QSize(22, 22));
100 //      QToolTip::add(but, tr("Rename the active block"));
101         but->setToolTip(tr("Rename the active block"));
102         connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksAttributes()));
103         layButtons->addWidget(but);
104         // edit block:
105         but = new QToolButton(this);
106 //      but->setPixmap(pxmEdit);
107         but->setIcon(QIcon(pxmEdit));
108         but->setMinimumSize(QSize(22, 22));
109 //      QToolTip::add(but, tr("Edit the active block\nin a separate window"));
110         but->setToolTip(tr("Edit the active block\nin a separate window"));
111         connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksEdit()));
112         layButtons2->addWidget(but);
113         // insert block:
114         but = new QToolButton(this);
115 //      but->setPixmap(pxmInsert);
116         but->setIcon(QIcon(pxmInsert));
117         but->setMinimumSize(QSize(22, 22));
118 //      QToolTip::add(but, tr("Insert the active block"));
119         but->setToolTip(tr("Insert the active block"));
120         connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotBlocksInsert()));
121         layButtons2->addWidget(but);
122
123         //lay->addWidget(caption);
124         lay->addLayout(layButtons);
125         lay->addLayout(layButtons2);
126         lay->addWidget(listBox);
127
128         //connect(listBox, SIGNAL(doubleClicked(QListBoxItem*)),
129         //        actionHandler, SLOT(slotBlocksToggleView()));
130
131 //      connect(listBox, SIGNAL(highlighted(const QString&)), this, SLOT(slotActivated(const QString&)));
132         connect(listBox, SIGNAL(itemSelectionChanged(void)), this, SLOT(slotActivated(void)));
133
134 //      connect(listBox, SIGNAL(mouseButtonClicked(int, Q3ListBoxItem*, const QPoint&)),
135 //              this, SLOT(slotMouseButtonClicked(int, Q3ListBoxItem*, const QPoint&)));
136         connect(listBox, SIGNAL(itemClicked(QListWidgetItem *)),
137                 this, SLOT(slotMouseButtonClicked(QListWidgetItem *)));
138
139         //boxLayout()->addWidget(listBox);
140 }
141
142 /**
143  * Destructor
144  */
145 QG_BlockWidget::~QG_BlockWidget()
146 {
147         delete listBox;
148 }
149
150 void QG_BlockWidget::setBlockList(RS_BlockList * blockList)
151 {
152         this->blockList = blockList;
153         update();
154 }
155
156 RS_BlockList * QG_BlockWidget::getBlockList()
157 {
158         return blockList;
159 }
160
161 /**
162  * Updates the block box from the blocks in the graphic.
163  */
164 void QG_BlockWidget::update()
165 {
166         RS_DEBUG->print("QG_BlockWidget::update()");
167
168 #warning "!!!"
169 //      int yPos = listBox->contentsY();
170
171         RS_Block * activeBlock = NULL;
172
173         if (blockList != NULL)
174                 activeBlock = blockList->getActive();
175
176         listBox->clear();
177
178         if (blockList == NULL)
179         {
180                 RS_DEBUG->print("QG_BlockWidget::update(): blockList is NULL");
181                 return;
182         }
183
184         for(uint i=0; i<blockList->count(); ++i)
185         {
186                 RS_Block * blk = blockList->at(i);
187
188                 if (!blk->isFrozen())
189 //                      listBox->insertItem(pxmVisible, blk->getName());
190                         listBox->addItem(new QListWidgetItem(pxmVisible, blk->getName()));
191                 else
192 //                      listBox->insertItem(pxmHidden, blk->getName());
193                         listBox->addItem(new QListWidgetItem(pxmHidden, blk->getName()));
194         }
195
196 //      listBox->sort();
197         listBox->sortItems(Qt::AscendingOrder);
198
199         RS_Block * b = lastBlock;
200         highlightBlock(activeBlock);
201         lastBlock = b;
202 #warning "!!!"
203 //      listBox->setContentsPos(0, yPos);
204
205         //highlightBlock(blockList->getActiveBlock());
206         //listBox->setContentsPos(0, yPos);
207         RS_DEBUG->print("QG_BlockWidget::update() done");
208 }
209
210 /**
211  * Highlights (activates) the given block and makes it
212  * the active block in the blocklist.
213  */
214 void QG_BlockWidget::highlightBlock(RS_Block * block)
215 {
216         RS_DEBUG->print("QG_BlockWidget::highlightBlock()");
217
218         if (block == NULL || listBox == NULL)
219                 return;
220
221         blockList->activate(block);
222         QString name = block->getName();
223
224         for(int i=0; i<(int)listBox->count(); ++i)
225         {
226                 QListWidgetItem * item = listBox->item(i);
227
228 //              if (listBox->text(i) == name)
229                 if (item->text() == name)
230                 {
231 //                      listBox->setCurrentItem(i);
232                         listBox->setCurrentRow(i);
233                         break;
234                 }
235         }
236 }
237
238 /*virtual*/ void QG_BlockWidget::blockAdded(RS_Block *)
239 {
240         update();
241 }
242
243 /*virtual*/ void QG_BlockWidget::blockEdited(RS_Block *)
244 {
245         update();
246 }
247
248 /*virtual*/ void QG_BlockWidget::blockRemoved(RS_Block *)
249 {
250         update();
251 }
252
253 /*virtual*/ void QG_BlockWidget::blockToggled(RS_Block *)
254 {
255         update();
256 }
257
258 /**
259  * Toggles the view of the given block. This is usually called when
260  * an item is double clicked.
261  */
262 /*
263 void QG_BlockWidget::slotToggleView(QListBoxItem* item) {
264     RS_DEBUG->print("QG_BlockWidget::slotToggleView()");
265
266     if (item==NULL || blockList==NULL) {
267         return;
268     }
269
270     int index = listBox->index(item);
271     RS_Block* block = blockList->find(item->text());
272
273     if (block!=NULL) {
274         blockList->toggleBlock(item->text());
275         if (!block->isFrozen()) {
276                 listBox->changeItem(pxmVisible, item->text(), index);
277         } else {
278             listBox->changeItem(*pxmHidden, item->text(), index);
279         }
280
281     }
282 }
283 */
284
285 /**
286  * Called when the user activates (highlights) a block.
287  */
288 //void QG_BlockWidget::slotActivated(const QString & blockName)
289 void QG_BlockWidget::slotActivated(void)
290 {
291         QString blockName = listBox->currentItem()->text();
292         RS_DEBUG->print("QG_BlockWidget::slotActivated(): %s", blockName.toLatin1().data());
293
294         if (blockList == NULL)
295                 return;
296
297         lastBlock = blockList->getActive();
298         blockList->activate(blockName);
299 }
300
301 /**
302  * Called for every mouse click.
303  */
304 //void QG_BlockWidget::slotMouseButtonClicked(int /*button*/, Q3ListBoxItem* item, const QPoint& pos)
305 void QG_BlockWidget::slotMouseButtonClicked(QListWidgetItem * item)
306 {
307         QPoint p = mapFromGlobal(QCursor::pos());
308         RS_Block * b = lastBlock;
309
310         if (p.x() < 23)
311         {
312                 actionHandler->slotBlocksToggleView();
313                 highlightBlock(b);
314         }
315         else
316         {
317                 if (item != NULL && blockList != NULL)
318                         lastBlock = blockList->find(item->text());
319         }
320 }
321
322 /**
323  * Shows a context menu for the block widget. Launched with a right click.
324  */
325 void QG_BlockWidget::contextMenuEvent(QContextMenuEvent * e)
326 {
327 #warning "Figure out how to port this crap (popup menu) to Qt4... !!! FIX !!!"
328 #if 0
329         //QListBoxItem* item = listBox->selectedItem();
330         Q3PopupMenu * contextMenu = new Q3PopupMenu(this);
331         QLabel * caption = new QLabel(tr("Block Menu"), this);
332         caption->setPaletteBackgroundColor(Qt::black);
333         caption->setPaletteForegroundColor(Qt::white);
334         caption->setAlignment(Qt::AlignCenter);
335         contextMenu->insertItem(caption);
336         contextMenu->insertItem(tr("&Defreeze all Blocks"), actionHandler, SLOT(slotBlocksDefreezeAll()), 0);
337         contextMenu->insertItem(tr("&Freeze all Blocks"), actionHandler, SLOT(slotBlocksFreezeAll()), 0);
338         contextMenu->insertItem(tr("&Add Block"), actionHandler, SLOT(slotBlocksAdd()), 0);
339         contextMenu->insertItem(tr("&Remove Block"), actionHandler, SLOT(slotBlocksRemove()), 0);
340         contextMenu->insertItem(tr("&Rename Block"), actionHandler, SLOT(slotBlocksAttributes()), 0);
341         contextMenu->insertItem(tr("&Edit Block"), actionHandler, SLOT(slotBlocksEdit()), 0);
342         contextMenu->insertItem(tr("&Insert Block"), actionHandler, SLOT(slotBlocksInsert()), 0);
343         contextMenu->insertItem(tr("&Toggle Visibility"), actionHandler, SLOT(slotBlocksToggleView()), 0);
344         contextMenu->insertItem(tr("&Create New Block"), actionHandler, SLOT(slotBlocksCreate()), 0);
345         contextMenu->exec(QCursor::pos());
346         delete contextMenu;
347 #endif
348
349         e->accept();
350 }
351
352 /**
353  * Escape releases focus.
354  */
355 void QG_BlockWidget::keyPressEvent(QKeyEvent * e)
356 {
357         switch (e->key())
358         {
359         case Qt::Key_Escape:
360                 emit escape();
361                 break;
362
363         default:
364                 QWidget::keyPressEvent(e);
365                 break;
366         }
367 }