]> Shamusworld >> Repos - architektonas/blob - src/widgets/layerwidget.cpp
Sanity check stage II: rename classes...
[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         //delete pxmVisible;
132         //delete pxmHidden;
133 }
134
135 /**
136  * Sets the layerlist this layer widget should show.
137  *
138  * @param showByBlock true: show the layer with the name "ByBlock" if
139  *                    it exists.
140  *                    false: don't show special layer "ByBlock"
141  */
142 void LayerWidget::setLayerList(RS_LayerList * layerList, bool showByBlock)
143 {
144         this->layerList = layerList;
145         this->showByBlock = showByBlock;
146         update();
147 }
148
149 /**
150  * Updates the layer box from the layers in the graphic.
151  */
152 void LayerWidget::update()
153 {
154         RS_DEBUG->print("LayerWidget::update() begin");
155
156 #warning "!!!"
157 //      int yPos = listBox->contentsY();
158
159         RS_Layer * activeLayer = NULL;
160
161         if (layerList != NULL)
162                 activeLayer = layerList->getActive();
163
164         RS_DEBUG->print("LayerWidget::update() clearing listBox");
165
166         listBox->clear();
167
168         if (layerList == NULL)
169         {
170                 RS_DEBUG->print("LayerWidget::update() abort");
171                 return;
172         }
173
174         RS_DEBUG->print("LayerWidget::update() filling in layers");
175
176         for(uint i=0; i<layerList->count(); ++i)
177         {
178                 RS_Layer * layer = layerList->at(i);
179
180                 // hide layer "ByBlock"?
181                 if (showByBlock || layer->getName()!="ByBlock")
182                 {
183                         QPixmap * pm = NULL;
184
185                         if (!layer->isFrozen())
186                         {
187                                 if (!layer->isLocked())
188                                 {
189                                         pm = &pxmLayerStatus10;
190                                 }
191                                 else
192                                 {
193                                         pm = &pxmLayerStatus11;
194                                 }
195                         }
196                         else
197                         {
198                                 if (!layer->isLocked())
199                                 {
200                                         pm = &pxmLayerStatus00;
201                                 }
202                                 else
203                                 {
204                                         pm = &pxmLayerStatus01;
205                                 }
206                         }
207
208                         if (pm != NULL)
209                         {
210 //                              listBox->insertItem(*pm, layer->getName());
211                                 listBox->addItem(new QListWidgetItem(*pm, layer->getName()));
212                         }
213                 }
214         }
215
216         RS_DEBUG->print("LayerWidget::update() sorting");
217
218 //      listBox->sort();
219         listBox->sortItems(Qt::AscendingOrder);
220
221         RS_DEBUG->print("LayerWidget::update() reactivating current layer");
222
223         RS_Layer * l = lastLayer;
224         highlightLayer(activeLayer);
225         lastLayer = l;
226 #warning "!!!"
227 //      listBox->setContentsPos(0, yPos);
228
229         RS_DEBUG->print("LayerWidget::update() end");
230 }
231
232 /**
233  * Highlights (activates) the given layer and makes it
234  * the active layer in the layerlist.
235  */
236 void LayerWidget::highlightLayer(RS_Layer * layer)
237 {
238         RS_DEBUG->print("LayerWidget::highlightLayer() begin");
239
240         if (layer == NULL || layerList == NULL)
241         {
242                 RS_DEBUG->print("LayerWidget::highlightLayer() abort");
243                 return;
244         }
245
246         QString name = layer->getName();
247         highlightLayer(name);
248
249         RS_DEBUG->print("LayerWidget::highlightLayer() end");
250 }
251
252 /**
253  * Highlights (activates) the given layer and makes it
254  * the active layer in the layerlist.
255  */
256 void LayerWidget::highlightLayer(const QString & name)
257 {
258         RS_DEBUG->print("LayerWidget::highlightLayer(name) begin");
259
260         if (layerList == NULL)
261         {
262                 RS_DEBUG->print("LayerWidget::highlightLayer(name) abort");
263                 return;
264         }
265
266         layerList->activate(name);
267
268         for(int i=0; i<(int)listBox->count(); ++i)
269         {
270                 QListWidgetItem * item = listBox->item(i);
271
272                 if (item->text() == name)
273                 {
274 //                      listBox->setCurrentItem(i);
275                         listBox->setCurrentRow(i);
276                         break;
277                 }
278         }
279
280         RS_DEBUG->print("LayerWidget::highlightLayer(name) end");
281 }
282
283 /*virtual*/ void LayerWidget::layerActivated(RS_Layer * layer)
284 {
285         highlightLayer(layer);
286 }
287
288 /*virtual*/ void LayerWidget::layerAdded(RS_Layer * layer)
289 {
290         update();
291         highlightLayer(layer);
292 }
293
294 /*virtual*/ void LayerWidget::layerEdited(RS_Layer *)
295 {
296         update();
297 }
298
299 /*virtual*/ void LayerWidget::layerRemoved(RS_Layer *)
300 {
301         update();
302         highlightLayer(layerList->at(0));
303 }
304
305 /*virtual*/ void LayerWidget::layerToggled(RS_Layer *)
306 {
307         update();
308 }
309
310 /**
311  * Called when the user activates (highlights) a layer.
312  */
313 //void LayerWidget::slotActivated(const QString & layerName)
314 void LayerWidget::slotActivated(void)
315 {
316         QString layerName = listBox->currentItem()->text();
317         RS_DEBUG->print("LayerWidget::slotActivated(): %s", layerName.toLatin1().data());
318
319         if (layerList == NULL)
320                 return;
321
322         lastLayer = layerList->getActive();
323         layerList->activate(layerName);
324 }
325
326 /**
327  * Called for every mouse click.
328  */
329 //void LayerWidget::slotMouseButtonClicked(int /*button*/, Q3ListBoxItem * item, const QPoint & pos)
330 void LayerWidget::slotMouseButtonClicked(QListWidgetItem * item)
331 {
332         RS_DEBUG->print("LayerWidget::slotMouseButtonClicked()");
333         QPoint p = mapFromGlobal(QCursor::pos());
334         // only change state / no activation
335         RS_Layer * l = lastLayer;
336
337         if (p.x() < 23)
338         {
339                 actionHandler->slotLayersToggleView();
340                 highlightLayer(l);
341         }
342         else if (p.x() < 34)
343         {
344                 actionHandler->slotLayersToggleLock();
345                 highlightLayer(l);
346         }
347         else
348         {
349                 if (item != NULL && layerList != NULL)
350                         lastLayer = layerList->find(item->text());
351         }
352 }
353
354 /**
355  * Shows a context menu for the layer widget. Launched with a right click.
356  */
357 void LayerWidget::contextMenuEvent(QContextMenuEvent * e)
358 {
359 #warning "Needs porting to Qt4...  !!! FIX !!!"
360 #if 0
361     if (actionHandler != NULL)
362         {
363         Q3PopupMenu* contextMenu = new Q3PopupMenu(this);
364         QLabel* caption = new QLabel(tr("Layer Menu"), this);
365         caption->setPaletteBackgroundColor(Qt::black);
366         caption->setPaletteForegroundColor(Qt::white);
367         caption->setAlignment( Qt::AlignCenter );
368         contextMenu->insertItem( caption );
369         contextMenu->insertItem( tr("&Defreeze all Layers"), actionHandler,
370                                  SLOT(slotLayersDefreezeAll()), 0);
371         contextMenu->insertItem( tr("&Freeze all Layers"), actionHandler,
372                                  SLOT(slotLayersFreezeAll()), 0);
373         contextMenu->insertItem( tr("&Add Layer"), actionHandler,
374                                  SLOT(slotLayersAdd()), 0);
375         contextMenu->insertItem( tr("&Remove Layer"), actionHandler,
376                                  SLOT(slotLayersRemove()), 0);
377         contextMenu->insertItem( tr("&Edit Layer"), actionHandler,
378                                  SLOT(slotLayersEdit()), 0);
379         contextMenu->insertItem( tr("&Toggle Visibility"), actionHandler,
380                                  SLOT(slotLayersToggleView()), 0);
381         contextMenu->exec(QCursor::pos());
382         delete contextMenu;
383     }
384 #endif
385
386     e->accept();
387 }
388
389 /**
390  * Escape releases focus.
391  */
392 void LayerWidget::keyPressEvent(QKeyEvent * e)
393 {
394     switch (e->key())
395         {
396     case Qt::Key_Escape:
397         emit escape();
398         break;
399
400     default:
401         QWidget::keyPressEvent(e);
402         break;
403     }
404 }
405