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 "layerlist.h"
27 LayerWidget::LayerWidget(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 LayerWidget::~LayerWidget()
134 * Sets the layerlist this layer widget should show.
136 * @param showByBlock true: show the layer with the name "ByBlock" if
138 * false: don't show special layer "ByBlock"
140 void LayerWidget::setLayerList(LayerList * layerList, bool showByBlock)
142 this->layerList = layerList;
143 this->showByBlock = showByBlock;
148 * Updates the layer box from the layers in the graphic.
150 void LayerWidget::update()
152 DEBUG->print("LayerWidget::update() begin");
155 // int yPos = listBox->contentsY();
157 Layer * activeLayer = NULL;
160 activeLayer = layerList->getActive();
162 DEBUG->print("LayerWidget::update() clearing listBox");
167 DEBUG->print("LayerWidget::update() abort");
171 DEBUG->print("LayerWidget::update() filling in layers");
173 for(uint i=0; i<layerList->count(); ++i)
175 Layer * layer = layerList->at(i);
177 // hide layer "ByBlock"?
178 if (showByBlock || layer->getName() != "ByBlock")
182 if (!layer->isFrozen())
183 pm = (!layer->isLocked() ? &pxmLayerStatus10 : &pxmLayerStatus11);
185 pm = (!layer->isLocked() ? &pxmLayerStatus00 : &pxmLayerStatus01);
187 // listBox->insertItem(*pm, layer->getName());
188 listBox->addItem(new QListWidgetItem(*pm, layer->getName()));
192 DEBUG->print("LayerWidget::update() sorting");
194 listBox->sortItems(Qt::AscendingOrder);
195 DEBUG->print("LayerWidget::update() reactivating current layer");
197 Layer * l = lastLayer;
198 highlightLayer(activeLayer);
201 // listBox->setContentsPos(0, yPos);
202 DEBUG->print("LayerWidget::update() end");
206 * Highlights (activates) the given layer and makes it
207 * the active layer in the layerlist.
209 void LayerWidget::highlightLayer(Layer * layer)
211 DEBUG->print("LayerWidget::highlightLayer() begin");
213 if (!layer || !layerList)
215 DEBUG->print("LayerWidget::highlightLayer() abort");
219 QString name = layer->getName();
220 highlightLayer(name);
221 DEBUG->print("LayerWidget::highlightLayer() end");
225 * Highlights (activates) the given layer and makes it
226 * the active layer in the layerlist.
228 void LayerWidget::highlightLayer(const QString & name)
230 DEBUG->print("LayerWidget::highlightLayer(name) begin");
234 DEBUG->print("LayerWidget::highlightLayer(name) abort");
238 layerList->activate(name);
240 for(int i=0; i<(int)listBox->count(); ++i)
242 QListWidgetItem * item = listBox->item(i);
244 if (item->text() == name)
246 // listBox->setCurrentItem(i);
247 listBox->setCurrentRow(i);
252 DEBUG->print("LayerWidget::highlightLayer(name) end");
255 void LayerWidget::layerActivated(Layer * layer)
257 highlightLayer(layer);
260 void LayerWidget::layerAdded(Layer * layer)
263 highlightLayer(layer);
266 void LayerWidget::layerEdited(Layer *)
271 void LayerWidget::layerRemoved(Layer *)
274 highlightLayer(layerList->at(0));
277 void LayerWidget::layerToggled(Layer *)
283 * Called when the user activates (highlights) a layer.
285 //void LayerWidget::slotActivated(const QString & layerName)
286 void LayerWidget::slotActivated(void)
288 QString layerName = listBox->currentItem()->text();
289 DEBUG->print("LayerWidget::slotActivated(): %s", layerName.toLatin1().data());
294 lastLayer = layerList->getActive();
295 layerList->activate(layerName);
299 * Called for every mouse click.
301 //void LayerWidget::slotMouseButtonClicked(int /*button*/, Q3ListBoxItem * item, const QPoint & pos)
302 void LayerWidget::slotMouseButtonClicked(QListWidgetItem * item)
304 DEBUG->print("LayerWidget::slotMouseButtonClicked()");
305 QPoint p = mapFromGlobal(QCursor::pos());
306 // only change state / no activation
307 Layer * l = lastLayer;
309 #warning "!!! Bad implementation of lock/freeze functionality !!!"
313 actionHandler->slotLayersToggleView();
318 actionHandler->slotLayersToggleLock();
323 if (item && layerList)
324 lastLayer = layerList->find(item->text());
329 * Shows a context menu for the layer widget. Launched with a right click.
331 void LayerWidget::contextMenuEvent(QContextMenuEvent * e)
333 #warning "Needs porting to Qt4... !!! FIX !!!"
335 if (actionHandler != NULL)
337 Q3PopupMenu* contextMenu = new Q3PopupMenu(this);
338 QLabel* caption = new QLabel(tr("Layer Menu"), this);
339 caption->setPaletteBackgroundColor(Qt::black);
340 caption->setPaletteForegroundColor(Qt::white);
341 caption->setAlignment( Qt::AlignCenter );
342 contextMenu->insertItem( caption );
343 contextMenu->insertItem( tr("&Defreeze all Layers"), actionHandler,
344 SLOT(slotLayersDefreezeAll()), 0);
345 contextMenu->insertItem( tr("&Freeze all Layers"), actionHandler,
346 SLOT(slotLayersFreezeAll()), 0);
347 contextMenu->insertItem( tr("&Add Layer"), actionHandler,
348 SLOT(slotLayersAdd()), 0);
349 contextMenu->insertItem( tr("&Remove Layer"), actionHandler,
350 SLOT(slotLayersRemove()), 0);
351 contextMenu->insertItem( tr("&Edit Layer"), actionHandler,
352 SLOT(slotLayersEdit()), 0);
353 contextMenu->insertItem( tr("&Toggle Visibility"), actionHandler,
354 SLOT(slotLayersToggleView()), 0);
355 contextMenu->exec(QCursor::pos());
364 * Escape releases focus.
366 void LayerWidget::keyPressEvent(QKeyEvent * e)
375 QWidget::keyPressEvent(e);