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
10 // JLH = James L. Hammons <jlhamm@acm.org>
13 // --- ---------- -----------------------------------------------------------
14 // JLH 05/11/2010 Added this text. :-)
17 #include "layerwidget.h"
19 #include "actionhandler.h"
22 #include "rs_layerlist.h"
27 QG_LayerWidget::QG_LayerWidget(QG_ActionHandler * ah, QWidget * parent,
28 const char * name, Qt::WindowFlags 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")
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);
56 // Q3VBoxLayout * lay = new Q3VBoxLayout(this, 0, -1, "lay");
57 QVBoxLayout * lay = new QVBoxLayout(this);
59 /*QLabel* caption = new QLabel(tr("Layer List"), this, "lLayers");
60 caption->setAlignment(Qt::AlignCenter);
61 caption->setPaletteBackgroundColor(black);
62 caption->setPaletteForegroundColor(white);
65 // Q3HBoxLayout * layButtons = new Q3HBoxLayout(NULL, 0, -1, "layButtons");
66 QHBoxLayout * layButtons = new QHBoxLayout();
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);
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);
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);
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);
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);
109 //lay->addWidget(caption);
110 lay->addLayout(layButtons);
111 lay->addWidget(listBox);
113 // connect(listBox, SIGNAL(highlighted(const QString &)), this, SLOT(slotActivated(const QString &)));
114 connect(listBox, SIGNAL(itemSelectionChanged(void)), this, SLOT(slotActivated(void)));
116 //connect(listBox, SIGNAL(doubleClicked(QListBoxItem*)),
117 // actionHandler, SLOT(slotLayersToggleView()));
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 *)));
128 QG_LayerWidget::~QG_LayerWidget()
136 * Sets the layerlist this layer widget should show.
138 * @param showByBlock true: show the layer with the name "ByBlock" if
140 * false: don't show special layer "ByBlock"
142 void QG_LayerWidget::setLayerList(RS_LayerList * layerList, bool showByBlock)
144 this->layerList = layerList;
145 this->showByBlock = showByBlock;
150 * Updates the layer box from the layers in the graphic.
152 void QG_LayerWidget::update()
154 RS_DEBUG->print("QG_LayerWidget::update() begin");
157 // int yPos = listBox->contentsY();
159 RS_Layer * activeLayer = NULL;
161 if (layerList != NULL)
162 activeLayer = layerList->getActive();
164 RS_DEBUG->print("QG_LayerWidget::update() clearing listBox");
168 if (layerList == NULL)
170 RS_DEBUG->print("QG_LayerWidget::update() abort");
174 RS_DEBUG->print("QG_LayerWidget::update() filling in layers");
176 for(uint i=0; i<layerList->count(); ++i)
178 RS_Layer * layer = layerList->at(i);
180 // hide layer "ByBlock"?
181 if (showByBlock || layer->getName()!="ByBlock")
185 if (!layer->isFrozen())
187 if (!layer->isLocked())
189 pm = &pxmLayerStatus10;
193 pm = &pxmLayerStatus11;
198 if (!layer->isLocked())
200 pm = &pxmLayerStatus00;
204 pm = &pxmLayerStatus01;
210 // listBox->insertItem(*pm, layer->getName());
211 listBox->addItem(new QListWidgetItem(*pm, layer->getName()));
216 RS_DEBUG->print("QG_LayerWidget::update() sorting");
219 listBox->sortItems(Qt::AscendingOrder);
221 RS_DEBUG->print("QG_LayerWidget::update() reactivating current layer");
223 RS_Layer * l = lastLayer;
224 highlightLayer(activeLayer);
227 // listBox->setContentsPos(0, yPos);
229 RS_DEBUG->print("QG_LayerWidget::update() end");
233 * Highlights (activates) the given layer and makes it
234 * the active layer in the layerlist.
236 void QG_LayerWidget::highlightLayer(RS_Layer * layer)
238 RS_DEBUG->print("QG_LayerWidget::highlightLayer() begin");
240 if (layer == NULL || layerList == NULL)
242 RS_DEBUG->print("QG_LayerWidget::highlightLayer() abort");
246 QString name = layer->getName();
247 highlightLayer(name);
249 RS_DEBUG->print("QG_LayerWidget::highlightLayer() end");
253 * Highlights (activates) the given layer and makes it
254 * the active layer in the layerlist.
256 void QG_LayerWidget::highlightLayer(const QString & name)
258 RS_DEBUG->print("QG_LayerWidget::highlightLayer(name) begin");
260 if (layerList == NULL)
262 RS_DEBUG->print("QG_LayerWidget::highlightLayer(name) abort");
266 layerList->activate(name);
268 for(int i=0; i<(int)listBox->count(); ++i)
270 QListWidgetItem * item = listBox->item(i);
272 if (item->text() == name)
274 // listBox->setCurrentItem(i);
275 listBox->setCurrentRow(i);
280 RS_DEBUG->print("QG_LayerWidget::highlightLayer(name) end");
283 /*virtual*/ void QG_LayerWidget::layerActivated(RS_Layer * layer)
285 highlightLayer(layer);
288 /*virtual*/ void QG_LayerWidget::layerAdded(RS_Layer * layer)
291 highlightLayer(layer);
294 /*virtual*/ void QG_LayerWidget::layerEdited(RS_Layer *)
299 /*virtual*/ void QG_LayerWidget::layerRemoved(RS_Layer *)
302 highlightLayer(layerList->at(0));
305 /*virtual*/ void QG_LayerWidget::layerToggled(RS_Layer *)
311 * Called when the user activates (highlights) a layer.
313 //void QG_LayerWidget::slotActivated(const QString & layerName)
314 void QG_LayerWidget::slotActivated(void)
316 QString layerName = listBox->currentItem()->text();
317 RS_DEBUG->print("QG_LayerWidget::slotActivated(): %s", layerName.toLatin1().data());
319 if (layerList == NULL)
322 lastLayer = layerList->getActive();
323 layerList->activate(layerName);
327 * Called for every mouse click.
329 //void QG_LayerWidget::slotMouseButtonClicked(int /*button*/, Q3ListBoxItem * item, const QPoint & pos)
330 void QG_LayerWidget::slotMouseButtonClicked(QListWidgetItem * item)
332 RS_DEBUG->print("QG_LayerWidget::slotMouseButtonClicked()");
333 QPoint p = mapFromGlobal(QCursor::pos());
334 // only change state / no activation
335 RS_Layer * l = lastLayer;
339 actionHandler->slotLayersToggleView();
344 actionHandler->slotLayersToggleLock();
349 if (item != NULL && layerList != NULL)
350 lastLayer = layerList->find(item->text());
355 * Shows a context menu for the layer widget. Launched with a right click.
357 void QG_LayerWidget::contextMenuEvent(QContextMenuEvent * e)
359 #warning "Needs porting to Qt4... !!! FIX !!!"
361 if (actionHandler != NULL)
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());
390 * Escape releases focus.
392 void QG_LayerWidget::keyPressEvent(QKeyEvent * e)
401 QWidget::keyPressEvent(e);