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