]> Shamusworld >> Repos - architektonas/blob - src/widgets/layerwidget.cpp
Still in the middle of fixing preview/snapper rendering...
[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 "rs_debug.h"
21 #include "rs_layer.h"
22 #include "rs_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(RS_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         RS_DEBUG->print("LayerWidget::update() begin");
153
154 #warning "!!!"
155 //      int yPos = listBox->contentsY();
156
157         RS_Layer * activeLayer = NULL;
158
159         if (layerList)
160                 activeLayer = layerList->getActive();
161
162         RS_DEBUG->print("LayerWidget::update() clearing listBox");
163         listBox->clear();
164
165         if (!layerList)
166         {
167                 RS_DEBUG->print("LayerWidget::update() abort");
168                 return;
169         }
170
171         RS_DEBUG->print("LayerWidget::update() filling in layers");
172
173         for(uint i=0; i<layerList->count(); ++i)
174         {
175                 RS_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         RS_DEBUG->print("LayerWidget::update() sorting");
193 //      listBox->sort();
194         listBox->sortItems(Qt::AscendingOrder);
195         RS_DEBUG->print("LayerWidget::update() reactivating current layer");
196
197         RS_Layer * l = lastLayer;
198         highlightLayer(activeLayer);
199         lastLayer = l;
200 #warning "!!!"
201 //      listBox->setContentsPos(0, yPos);
202         RS_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(RS_Layer * layer)
210 {
211         RS_DEBUG->print("LayerWidget::highlightLayer() begin");
212
213         if (!layer || !layerList)
214         {
215                 RS_DEBUG->print("LayerWidget::highlightLayer() abort");
216                 return;
217         }
218
219         QString name = layer->getName();
220         highlightLayer(name);
221         RS_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         RS_DEBUG->print("LayerWidget::highlightLayer(name) begin");
231
232         if (!layerList)
233         {
234                 RS_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         RS_DEBUG->print("LayerWidget::highlightLayer(name) end");
253 }
254
255 void LayerWidget::layerActivated(RS_Layer * layer)
256 {
257         highlightLayer(layer);
258 }
259
260 void LayerWidget::layerAdded(RS_Layer * layer)
261 {
262         update();
263         highlightLayer(layer);
264 }
265
266 void LayerWidget::layerEdited(RS_Layer *)
267 {
268         update();
269 }
270
271 void LayerWidget::layerRemoved(RS_Layer *)
272 {
273         update();
274         highlightLayer(layerList->at(0));
275 }
276
277 void LayerWidget::layerToggled(RS_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         RS_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         RS_DEBUG->print("LayerWidget::slotMouseButtonClicked()");
305         QPoint p = mapFromGlobal(QCursor::pos());
306         // only change state / no activation
307         RS_Layer * l = lastLayer;
308
309 #warning "!!! Bad implementation of lock/freeze functionality !!!"
310         if (p.x() < 23)
311         {
312                 actionHandler->slotLayersToggleView();
313                 highlightLayer(l);
314         }
315         else if (p.x() < 34)
316         {
317                 actionHandler->slotLayersToggleLock();
318                 highlightLayer(l);
319         }
320         else
321         {
322                 if (item && layerList)
323                         lastLayer = layerList->find(item->text());
324         }
325 }
326
327 /**
328  * Shows a context menu for the layer widget. Launched with a right click.
329  */
330 void LayerWidget::contextMenuEvent(QContextMenuEvent * e)
331 {
332 #warning "Needs porting to Qt4...  !!! FIX !!!"
333 #if 0
334     if (actionHandler != NULL)
335         {
336         Q3PopupMenu* contextMenu = new Q3PopupMenu(this);
337         QLabel* caption = new QLabel(tr("Layer 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 Layers"), actionHandler,
343                                  SLOT(slotLayersDefreezeAll()), 0);
344         contextMenu->insertItem( tr("&Freeze all Layers"), actionHandler,
345                                  SLOT(slotLayersFreezeAll()), 0);
346         contextMenu->insertItem( tr("&Add Layer"), actionHandler,
347                                  SLOT(slotLayersAdd()), 0);
348         contextMenu->insertItem( tr("&Remove Layer"), actionHandler,
349                                  SLOT(slotLayersRemove()), 0);
350         contextMenu->insertItem( tr("&Edit Layer"), actionHandler,
351                                  SLOT(slotLayersEdit()), 0);
352         contextMenu->insertItem( tr("&Toggle Visibility"), actionHandler,
353                                  SLOT(slotLayersToggleView()), 0);
354         contextMenu->exec(QCursor::pos());
355         delete contextMenu;
356     }
357 #endif
358
359     e->accept();
360 }
361
362 /**
363  * Escape releases focus.
364  */
365 void LayerWidget::keyPressEvent(QKeyEvent * e)
366 {
367     switch (e->key())
368         {
369     case Qt::Key_Escape:
370         emit escape();
371         break;
372
373     default:
374         QWidget::keyPressEvent(e);
375         break;
376     }
377 }