]> Shamusworld >> Repos - architektonas/blob - src/widgets/layerwidget.cpp
Bugfixes related to removing Snapper class.
[architektonas] / src / widgets / layerwidget.cpp
1 // layerwdget.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/11/2010  Added this text. :-)
15 //
16
17 #include "layerwidget.h"
18
19 #include "actionhandler.h"
20 #include "debug.h"
21 #include "layer.h"
22 #include "layerlist.h"
23
24 /**
25  * Constructor.
26  */
27 LayerWidget::LayerWidget(ActionHandler * ah, QWidget * parent,
28         const char * name, Qt::WindowFlags f):
29         QWidget(parent, f),
30         pxmLayerStatus00(":/res/layerstatus_00.xpm"),
31         pxmLayerStatus01(":/res/layerstatus_01.xpm"),
32         pxmLayerStatus10(":/res/layerstatus_10.xpm"),
33         pxmLayerStatus11(":/res/layerstatus_11.xpm"),
34         pxmVisible(":/res/visibleblock.xpm"),
35         pxmHidden(":/res/hiddenblock.xpm"),
36         pxmAdd(":/res/layeradd.xpm"),
37         pxmRemove(":/res/layerremove.xpm"),
38         pxmEdit(":/res/layeredit.xpm"),
39         pxmDefreezeAll(":/res/visibleblock.xpm"),
40         pxmFreezeAll(":/res/hiddenblock.xpm")
41 {
42         actionHandler = ah;
43         layerList = NULL;
44         showByBlock = false;
45         lastLayer = NULL;
46
47 //      listBox = new Q3ListBox(this, "layerbox");
48         listBox = new QListWidget(this);
49 #warning "!!! The following three lines are commented out !!!"
50 //      listBox->setDragSelect(false);
51 //      listBox->setMultiSelection(false);
52 //      listBox->setSmoothScrolling(true);
53         listBox->setFocusPolicy(Qt::NoFocus);
54         listBox->setMinimumHeight(140);
55
56 //      Q3VBoxLayout * lay = new Q3VBoxLayout(this, 0, -1, "lay");
57         QVBoxLayout * lay = new QVBoxLayout(this);
58
59         /*QLabel* caption = new QLabel(tr("Layer List"), this, "lLayers");
60         caption->setAlignment(Qt::AlignCenter);
61         caption->setPaletteBackgroundColor(black);
62         caption->setPaletteForegroundColor(white);
63         */
64
65 //      Q3HBoxLayout * layButtons = new Q3HBoxLayout(NULL, 0, -1, "layButtons");
66         QHBoxLayout * layButtons = new QHBoxLayout();
67         QToolButton * but;
68         // show all layer:
69         but = new QToolButton(this);
70 //      but->setPixmap(pxmDefreezeAll);
71         but->setIcon(QIcon(pxmDefreezeAll));
72         but->setMinimumSize(QSize(22,22));
73         but->setToolTip(tr("Show all layers"));
74         connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotLayersDefreezeAll()));
75         layButtons->addWidget(but);
76         // hide all layer:
77         but = new QToolButton(this);
78 //      but->setPixmap(pxmFreezeAll);
79         but->setIcon(QIcon(pxmFreezeAll));
80         but->setMinimumSize(QSize(22,22));
81         but->setToolTip(tr("Hide all layers"));
82         connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotLayersFreezeAll()));
83         layButtons->addWidget(but);
84         // add layer:
85         but = new QToolButton(this);
86 //      but->setPixmap(pxmAdd);
87         but->setIcon(QIcon(pxmAdd));
88         but->setMinimumSize(QSize(22,22));
89         but->setToolTip(tr("Add a layer"));
90         connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotLayersAdd()));
91         layButtons->addWidget(but);
92         // remove layer:
93         but = new QToolButton(this);
94 //      but->setPixmap(pxmRemove);
95         but->setIcon(QIcon(pxmRemove));
96         but->setMinimumSize(QSize(22,22));
97         but->setToolTip(tr("Remove the current layer"));
98         connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotLayersRemove()));
99         layButtons->addWidget(but);
100         // rename layer:
101         but = new QToolButton(this);
102 //      but->setPixmap(pxmEdit);
103         but->setIcon(QIcon(pxmEdit));
104         but->setMinimumSize(QSize(22,22));
105         but->setToolTip(tr("Modify layer attributes / rename"));
106         connect(but, SIGNAL(clicked()), actionHandler, SLOT(slotLayersEdit()));
107         layButtons->addWidget(but);
108
109         //lay->addWidget(caption);
110         lay->addLayout(layButtons);
111         lay->addWidget(listBox);
112
113 //      connect(listBox, SIGNAL(highlighted(const QString &)), this, SLOT(slotActivated(const QString &)));
114         connect(listBox, SIGNAL(itemSelectionChanged(void)), this, SLOT(slotActivated(void)));
115
116         //connect(listBox, SIGNAL(doubleClicked(QListBoxItem*)),
117         //        actionHandler, SLOT(slotLayersToggleView()));
118
119 //      connect(listBox, SIGNAL(mouseButtonClicked(int, Q3ListBoxItem*, const QPoint&)),
120 //              this, SLOT(slotMouseButtonClicked(int, Q3ListBoxItem*, const QPoint&)));
121         connect(listBox, SIGNAL(itemClicked(QListWidgetItem *)),
122                 this, SLOT(slotMouseButtonClicked(QListWidgetItem *)));
123 }
124
125 /**
126  * Destructor
127  */
128 LayerWidget::~LayerWidget()
129 {
130         delete listBox;
131 }
132
133 /**
134  * Sets the layerlist this layer widget should show.
135  *
136  * @param showByBlock true: show the layer with the name "ByBlock" if
137  *                    it exists.
138  *                    false: don't show special layer "ByBlock"
139  */
140 void LayerWidget::setLayerList(LayerList * layerList, bool showByBlock)
141 {
142         this->layerList = layerList;
143         this->showByBlock = showByBlock;
144         update();
145 }
146
147 /**
148  * Updates the layer box from the layers in the graphic.
149  */
150 void LayerWidget::update()
151 {
152         DEBUG->print("LayerWidget::update() begin");
153
154 #warning "!!!"
155 //      int yPos = listBox->contentsY();
156
157         Layer * activeLayer = NULL;
158
159         if (layerList)
160                 activeLayer = layerList->getActive();
161
162         DEBUG->print("LayerWidget::update() clearing listBox");
163         listBox->clear();
164
165         if (!layerList)
166         {
167                 DEBUG->print("LayerWidget::update() abort");
168                 return;
169         }
170
171         DEBUG->print("LayerWidget::update() filling in layers");
172
173         for(uint i=0; i<layerList->count(); ++i)
174         {
175                 Layer * layer = layerList->at(i);
176
177                 // hide layer "ByBlock"?
178                 if (showByBlock || layer->getName() != "ByBlock")
179                 {
180                         QPixmap * pm = NULL;
181
182                         if (!layer->isFrozen())
183                                 pm = (!layer->isLocked() ? &pxmLayerStatus10 : &pxmLayerStatus11);
184                         else
185                                 pm = (!layer->isLocked() ? &pxmLayerStatus00 : &pxmLayerStatus01);
186
187 //                      listBox->insertItem(*pm, layer->getName());
188                         listBox->addItem(new QListWidgetItem(*pm, layer->getName()));
189                 }
190         }
191
192         DEBUG->print("LayerWidget::update() sorting");
193 //      listBox->sort();
194         listBox->sortItems(Qt::AscendingOrder);
195         DEBUG->print("LayerWidget::update() reactivating current layer");
196
197         Layer * l = lastLayer;
198         highlightLayer(activeLayer);
199         lastLayer = l;
200 #warning "!!!"
201 //      listBox->setContentsPos(0, yPos);
202         DEBUG->print("LayerWidget::update() end");
203 }
204
205 /**
206  * Highlights (activates) the given layer and makes it
207  * the active layer in the layerlist.
208  */
209 void LayerWidget::highlightLayer(Layer * layer)
210 {
211         DEBUG->print("LayerWidget::highlightLayer() begin");
212
213         if (!layer || !layerList)
214         {
215                 DEBUG->print("LayerWidget::highlightLayer() abort");
216                 return;
217         }
218
219         QString name = layer->getName();
220         highlightLayer(name);
221         DEBUG->print("LayerWidget::highlightLayer() end");
222 }
223
224 /**
225  * Highlights (activates) the given layer and makes it
226  * the active layer in the layerlist.
227  */
228 void LayerWidget::highlightLayer(const QString & name)
229 {
230         DEBUG->print("LayerWidget::highlightLayer(name) begin");
231
232         if (!layerList)
233         {
234                 DEBUG->print("LayerWidget::highlightLayer(name) abort");
235                 return;
236         }
237
238         layerList->activate(name);
239
240         for(int i=0; i<(int)listBox->count(); ++i)
241         {
242                 QListWidgetItem * item = listBox->item(i);
243
244                 if (item->text() == name)
245                 {
246 //                      listBox->setCurrentItem(i);
247                         listBox->setCurrentRow(i);
248                         break;
249                 }
250         }
251
252         DEBUG->print("LayerWidget::highlightLayer(name) end");
253 }
254
255 void LayerWidget::layerActivated(Layer * layer)
256 {
257         highlightLayer(layer);
258 }
259
260 void LayerWidget::layerAdded(Layer * layer)
261 {
262         update();
263         highlightLayer(layer);
264 }
265
266 void LayerWidget::layerEdited(Layer *)
267 {
268         update();
269 }
270
271 void LayerWidget::layerRemoved(Layer *)
272 {
273         update();
274         highlightLayer(layerList->at(0));
275 }
276
277 void LayerWidget::layerToggled(Layer *)
278 {
279         update();
280 }
281
282 /**
283  * Called when the user activates (highlights) a layer.
284  */
285 //void LayerWidget::slotActivated(const QString & layerName)
286 void LayerWidget::slotActivated(void)
287 {
288         QString layerName = listBox->currentItem()->text();
289         DEBUG->print("LayerWidget::slotActivated(): %s", layerName.toLatin1().data());
290
291         if (!layerList)
292                 return;
293
294         lastLayer = layerList->getActive();
295         layerList->activate(layerName);
296 }
297
298 /**
299  * Called for every mouse click.
300  */
301 //void LayerWidget::slotMouseButtonClicked(int /*button*/, Q3ListBoxItem * item, const QPoint & pos)
302 void LayerWidget::slotMouseButtonClicked(QListWidgetItem * item)
303 {
304         DEBUG->print("LayerWidget::slotMouseButtonClicked()");
305         QPoint p = mapFromGlobal(QCursor::pos());
306         // only change state / no activation
307         Layer * l = lastLayer;
308
309 #warning "!!! Bad implementation of lock/freeze functionality !!!"
310 //This is crap.
311         if (p.x() < 23)
312         {
313                 actionHandler->slotLayersToggleView();
314                 highlightLayer(l);
315         }
316         else if (p.x() < 34)
317         {
318                 actionHandler->slotLayersToggleLock();
319                 highlightLayer(l);
320         }
321         else
322         {
323                 if (item && layerList)
324                         lastLayer = layerList->find(item->text());
325         }
326 }
327
328 /**
329  * Shows a context menu for the layer widget. Launched with a right click.
330  */
331 void LayerWidget::contextMenuEvent(QContextMenuEvent * e)
332 {
333 #warning "Needs porting to Qt4...  !!! FIX !!!"
334 #if 0
335     if (actionHandler != NULL)
336         {
337         Q3PopupMenu* contextMenu = new Q3PopupMenu(this);
338         QLabel* caption = new QLabel(tr("Layer Menu"), this);
339         caption->setPaletteBackgroundColor(Qt::black);
340         caption->setPaletteForegroundColor(Qt::white);
341         caption->setAlignment( Qt::AlignCenter );
342         contextMenu->insertItem( caption );
343         contextMenu->insertItem( tr("&Defreeze all Layers"), actionHandler,
344                                  SLOT(slotLayersDefreezeAll()), 0);
345         contextMenu->insertItem( tr("&Freeze all Layers"), actionHandler,
346                                  SLOT(slotLayersFreezeAll()), 0);
347         contextMenu->insertItem( tr("&Add Layer"), actionHandler,
348                                  SLOT(slotLayersAdd()), 0);
349         contextMenu->insertItem( tr("&Remove Layer"), actionHandler,
350                                  SLOT(slotLayersRemove()), 0);
351         contextMenu->insertItem( tr("&Edit Layer"), actionHandler,
352                                  SLOT(slotLayersEdit()), 0);
353         contextMenu->insertItem( tr("&Toggle Visibility"), actionHandler,
354                                  SLOT(slotLayersToggleView()), 0);
355         contextMenu->exec(QCursor::pos());
356         delete contextMenu;
357     }
358 #endif
359
360     e->accept();
361 }
362
363 /**
364  * Escape releases focus.
365  */
366 void LayerWidget::keyPressEvent(QKeyEvent * e)
367 {
368     switch (e->key())
369         {
370     case Qt::Key_Escape:
371         emit escape();
372         break;
373
374     default:
375         QWidget::keyPressEvent(e);
376         break;
377     }
378 }