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