3 // Originally part of QCad Community Edition by Andrew Mustun
4 // Extensively rewritten and refactored by James L. Hammons
5 // (C) 2010 Underground Software
7 // JLH = James L. Hammons <jlhamm@acm.org>
10 // --- ---------- -----------------------------------------------------------
11 // JLH 05/11/2010 Added this text. :-)
14 #include "qg_layerwidget.h"
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"*/
29 QG_LayerWidget::QG_LayerWidget(QG_ActionHandler * ah, QWidget * parent,
30 const char * name, Qt::WindowFlags 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),
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")
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);
69 // Q3VBoxLayout * lay = new Q3VBoxLayout(this, 0, -1, "lay");
70 QVBoxLayout * lay = new QVBoxLayout(this);
72 /*QLabel* caption = new QLabel(tr("Layer List"), this, "lLayers");
73 caption->setAlignment(Qt::AlignCenter);
74 caption->setPaletteBackgroundColor(black);
75 caption->setPaletteForegroundColor(white);
78 // Q3HBoxLayout * layButtons = new Q3HBoxLayout(NULL, 0, -1, "layButtons");
79 QHBoxLayout * layButtons = new QHBoxLayout();
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);
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);
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);
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);
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);
122 //lay->addWidget(caption);
123 lay->addLayout(layButtons);
124 lay->addWidget(listBox);
126 // connect(listBox, SIGNAL(highlighted(const QString &)), this, SLOT(slotActivated(const QString &)));
127 connect(listBox, SIGNAL(itemSelectionChanged(void)), this, SLOT(slotActivated(void)));
129 //connect(listBox, SIGNAL(doubleClicked(QListBoxItem*)),
130 // actionHandler, SLOT(slotLayersToggleView()));
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 *)));
141 QG_LayerWidget::~QG_LayerWidget()
149 * Sets the layerlist this layer widget should show.
151 * @param showByBlock true: show the layer with the name "ByBlock" if
153 * false: don't show special layer "ByBlock"
155 void QG_LayerWidget::setLayerList(RS_LayerList * layerList, bool showByBlock)
157 this->layerList = layerList;
158 this->showByBlock = showByBlock;
163 * Updates the layer box from the layers in the graphic.
165 void QG_LayerWidget::update()
167 RS_DEBUG->print("QG_LayerWidget::update() begin");
170 // int yPos = listBox->contentsY();
172 RS_Layer * activeLayer = NULL;
174 if (layerList != NULL)
175 activeLayer = layerList->getActive();
177 RS_DEBUG->print("QG_LayerWidget::update() clearing listBox");
181 if (layerList == NULL)
183 RS_DEBUG->print("QG_LayerWidget::update() abort");
187 RS_DEBUG->print("QG_LayerWidget::update() filling in layers");
189 for(uint i=0; i<layerList->count(); ++i)
191 RS_Layer * layer = layerList->at(i);
193 // hide layer "ByBlock"?
194 if (showByBlock || layer->getName()!="ByBlock")
198 if (!layer->isFrozen())
200 if (!layer->isLocked())
202 pm = &pxmLayerStatus10;
206 pm = &pxmLayerStatus11;
211 if (!layer->isLocked())
213 pm = &pxmLayerStatus00;
217 pm = &pxmLayerStatus01;
223 // listBox->insertItem(*pm, layer->getName());
224 listBox->addItem(new QListWidgetItem(*pm, layer->getName()));
229 RS_DEBUG->print("QG_LayerWidget::update() sorting");
232 listBox->sortItems(Qt::AscendingOrder);
234 RS_DEBUG->print("QG_LayerWidget::update() reactivating current layer");
236 RS_Layer * l = lastLayer;
237 highlightLayer(activeLayer);
240 // listBox->setContentsPos(0, yPos);
242 RS_DEBUG->print("QG_LayerWidget::update() end");
246 * Highlights (activates) the given layer and makes it
247 * the active layer in the layerlist.
249 void QG_LayerWidget::highlightLayer(RS_Layer * layer)
251 RS_DEBUG->print("QG_LayerWidget::highlightLayer() begin");
253 if (layer == NULL || layerList == NULL)
255 RS_DEBUG->print("QG_LayerWidget::highlightLayer() abort");
259 QString name = layer->getName();
260 highlightLayer(name);
262 RS_DEBUG->print("QG_LayerWidget::highlightLayer() end");
266 * Highlights (activates) the given layer and makes it
267 * the active layer in the layerlist.
269 void QG_LayerWidget::highlightLayer(const QString & name)
271 RS_DEBUG->print("QG_LayerWidget::highlightLayer(name) begin");
273 if (layerList == NULL)
275 RS_DEBUG->print("QG_LayerWidget::highlightLayer(name) abort");
279 layerList->activate(name);
281 for(int i=0; i<(int)listBox->count(); ++i)
283 QListWidgetItem * item = listBox->item(i);
285 if (item->text() == name)
287 // listBox->setCurrentItem(i);
288 listBox->setCurrentRow(i);
293 RS_DEBUG->print("QG_LayerWidget::highlightLayer(name) end");
296 /*virtual*/ void QG_LayerWidget::layerActivated(RS_Layer * layer)
298 highlightLayer(layer);
301 /*virtual*/ void QG_LayerWidget::layerAdded(RS_Layer * layer)
304 highlightLayer(layer);
307 /*virtual*/ void QG_LayerWidget::layerEdited(RS_Layer *)
312 /*virtual*/ void QG_LayerWidget::layerRemoved(RS_Layer *)
315 highlightLayer(layerList->at(0));
318 /*virtual*/ void QG_LayerWidget::layerToggled(RS_Layer *)
324 * Called when the user activates (highlights) a layer.
326 //void QG_LayerWidget::slotActivated(const QString & layerName)
327 void QG_LayerWidget::slotActivated(void)
329 QString layerName = listBox->currentItem()->text();
330 RS_DEBUG->print("QG_LayerWidget::slotActivated(): %s", layerName.toLatin1().data());
332 if (layerList == NULL)
335 lastLayer = layerList->getActive();
336 layerList->activate(layerName);
340 * Called for every mouse click.
342 //void QG_LayerWidget::slotMouseButtonClicked(int /*button*/, Q3ListBoxItem * item, const QPoint & pos)
343 void QG_LayerWidget::slotMouseButtonClicked(QListWidgetItem * item)
345 RS_DEBUG->print("QG_LayerWidget::slotMouseButtonClicked()");
346 QPoint p = mapFromGlobal(QCursor::pos());
347 // only change state / no activation
348 RS_Layer * l = lastLayer;
352 actionHandler->slotLayersToggleView();
357 actionHandler->slotLayersToggleLock();
362 if (item != NULL && layerList != NULL)
363 lastLayer = layerList->find(item->text());
368 * Shows a context menu for the layer widget. Launched with a right click.
370 void QG_LayerWidget::contextMenuEvent(QContextMenuEvent * e)
372 #warning "Needs porting to Qt4... !!! FIX !!!"
374 if (actionHandler != NULL)
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());
403 * Escape releases focus.
405 void QG_LayerWidget::keyPressEvent(QKeyEvent * e)
414 QWidget::keyPressEvent(e);