//
#include "layerwidget.h"
+#include "global.h"
#include "layeritemwidget.h"
-#if 0
-OK, what it seems like we should do here, is instead of deriving from QDockWidget,
-we should derive from QWidget (or QScrollArea or somesuch). Then, when creating
-the dockwidget in the main window, we add the LayerWidget as the QDockWidget's
-main widget.
-#endif
-
-LayerWidget::LayerWidget(void): QWidget()
+LayerWidget::LayerWidget(void): QWidget(),
+ addLayer(new QToolButton), removeLayer(new QToolButton),
+ editLayer(new QToolButton), layerUp(new QToolButton),
+ layerDown(new QToolButton), list(new QListWidget)
{
-
- // Make a QScrollArea, put in a QVBoxLayout.
- // Use line widget (two checkboxes, one label), use setIcon() on the
- // checkboxes to change their look (eye open/closed for visibility,
- // lock open/closed for layer lock).
-
- QIcon visible(":/res/eye-open.png");
- visible.addFile(":/res/eye-closed.png", QSize(16, 16), QIcon::Normal, QIcon::On);
- QIcon locked(":/res/lock-open.png");
- locked.addFile(":/res/lock-closed.png", QSize(16, 16), QIcon::Normal, QIcon::On);
+ LayerItemWidget * liw = new LayerItemWidget("Background");
+ QListWidgetItem * qlwi = new QListWidgetItem(list);
+ list->setItemWidget(qlwi, liw);
+
+// QToolButton * pb1 = new QToolButton;
+// QToolButton * pb2 = new QToolButton;
+// QToolButton * pb3 = new QToolButton;
+// QToolButton * pb4 = new QToolButton;
+// QToolButton * pb5 = new QToolButton;
+
+ addLayer->setIcon(QIcon(":/res/layer-add.png"));
+ removeLayer->setIcon(QIcon(":/res/layer-delete.png"));
+ editLayer->setIcon(QIcon(":/res/layer-edit.png"));
+ layerUp->setIcon(QIcon(":/res/layer-up.png"));
+ layerDown->setIcon(QIcon(":/res/layer-down.png"));
+
+ addLayer->setToolTip(tr("Add layer"));
+ removeLayer->setToolTip(tr("Remove layer"));
+ editLayer->setToolTip(tr("Edit layer"));
+ layerUp->setToolTip(tr("Move layer up"));
+ layerDown->setToolTip(tr("Move layer down"));
+
+ QHBoxLayout * hbox1 = new QHBoxLayout;
+ hbox1->addWidget(addLayer);
+ hbox1->addWidget(removeLayer);
+ hbox1->addWidget(editLayer);
+ hbox1->addWidget(layerUp);
+ hbox1->addWidget(layerDown);
+ hbox1->addStretch();
QVBoxLayout * mainLayout = new QVBoxLayout;
- QHBoxLayout * line1 = new QHBoxLayout;
- QHBoxLayout * line2 = new QHBoxLayout;
-
-// QCheckBox * box1 = new QCheckBox("bleah");
-// box1->setIcon(visible);
-// mainLayout->addWidget(box1);
-
- QPushButton * button1 = new QPushButton;
- button1->setFlat(true);
- button1->setIcon(visible);
- button1->setCheckable(true);
- button1->setMaximumSize(QSize(20, 20));
- QPushButton * button2 = new QPushButton;
- button2->setFlat(true);
- button2->setIcon(locked);
- button2->setCheckable(true);
- button2->setMaximumSize(QSize(20, 20));
- QLabel * label1 = new QLabel("Background");
-
- QPushButton * button3 = new QPushButton;
- button3->setFlat(true);
- button3->setIcon(visible);
- button3->setCheckable(true);
- button3->setMaximumSize(QSize(20, 20));
- QPushButton * button4 = new QPushButton;
- button4->setFlat(true);
- button4->setIcon(locked);
- button4->setCheckable(true);
- button4->setMaximumSize(QSize(20, 20));
- QLabel * label2 = new QLabel("Guides");
-
- line1->addWidget(button1);
- line1->addWidget(button2);
- line1->addWidget(label1);
-
- line2->addWidget(button3);
- line2->addWidget(button4);
- line2->addWidget(label2);
-
- LayerItemWidget * liw1 = new LayerItemWidget("Floor #1");
- LayerItemWidget * liw2 = new LayerItemWidget("Mechanical");
-
- mainLayout->addLayout(line1);
- mainLayout->addLayout(line2);
- mainLayout->addWidget(liw1);
- mainLayout->addWidget(liw2);
- mainLayout->addStretch();
+ mainLayout->addWidget(list);
+ mainLayout->addLayout(hbox1);
+
setLayout(mainLayout);
+
+ connect(list, SIGNAL(currentRowChanged(int)), this, SLOT(HandleLayerSelected(int)));
+ connect(addLayer, SIGNAL(clicked()), this, SLOT(AddLayer()));
+ connect(removeLayer, SIGNAL(clicked()), this, SLOT(DeleteLayer()));
+ connect(editLayer, SIGNAL(clicked()), this, SLOT(EditLayer()));
+ connect(layerUp, SIGNAL(clicked()), this, SLOT(MoveLayerUp()));
+ connect(layerDown, SIGNAL(clicked()), this, SLOT(MoveLayerDown()));
+
+ list->setCurrentRow(0);
}
{
}
+
+void LayerWidget::HandleLayerSelected(int currentRow)
+{
+//printf("LayerWidget::HandleLayerSelected(): currentRow = %i\n", currentRow);
+// emit(LayerSelected(currentRow));
+
+ QListWidgetItem * qlwi = list->item(currentRow);
+ LayerItemWidget * li = (LayerItemWidget *)list->itemWidget(qlwi);
+ Global::activeLayer = currentRow;
+ Global::layerIsLocked = li->editibility->isChecked();
+
+ // Set button states to sane values
+ SetButtonStates();
+}
+
+
+void LayerWidget::AddLayer(void)
+{
+ int count = list->count();
+ QString text = QString("Layer #%1").arg(count);
+ LayerItemWidget * liw = new LayerItemWidget(text);
+ QListWidgetItem * qlwi = new QListWidgetItem();
+ list->insertItem(0, qlwi);
+ list->setItemWidget(qlwi, liw);
+
+ SetButtonStates();
+}
+
+
+void LayerWidget::DeleteLayer(void)
+{
+ int numItems = list->count();
+
+ if (numItems == 1)
+ return;
+
+ QListWidgetItem * qlwi = list->currentItem();
+ list->removeItemWidget(qlwi);
+ delete qlwi;
+
+ SetButtonStates();
+
+ // N.B.: Also, we need to delete the layer in the Drawing as well!
+}
+
+
+void LayerWidget::EditLayer(void)
+{
+ // Get the LayerItemWidget so we can edit it (its name, anyway)...
+ QListWidgetItem * qlwi = list->currentItem();
+ LayerItemWidget * li = (LayerItemWidget *)list->itemWidget(qlwi);
+ QString s = li->name->text();
+
+ bool ok;
+ QString result = QInputDialog::getText(this, tr("Edit Layer Name"), tr("Layer Name:"), QLineEdit::Normal, s, &ok);
+
+ if (ok && !result.isEmpty())
+ li->name->setText(result);
+}
+
+
+void LayerWidget::MoveLayerUp(void)
+{
+ // Get information out of the LayerItemWidget (& get it from the list!)
+ int currentRow = list->currentRow();
+ QListWidgetItem * qlwi = list->currentItem();
+ LayerItemWidget * li = (LayerItemWidget *)list->itemWidget(qlwi);
+ QString s = li->name->text();
+ bool visible = li->visibility->isChecked();
+ bool editible = li->editibility->isChecked();
+
+ // We have to make a new LayerItemWidget because it destroys the old one!
+ list->takeItem(currentRow);
+ list->insertItem(currentRow - 1, qlwi);
+ li = new LayerItemWidget(s, visible, editible);
+ list->setItemWidget(qlwi, li);
+ list->setCurrentItem(qlwi);
+}
+
+
+void LayerWidget::MoveLayerDown(void)
+{
+ // Get information out of the LayerItemWidget (& get it from the list!)
+ int currentRow = list->currentRow();
+ QListWidgetItem * qlwi = list->currentItem();
+ LayerItemWidget * li = (LayerItemWidget *)list->itemWidget(qlwi);
+ QString s = li->name->text();
+ bool visible = li->visibility->isChecked();
+ bool editible = li->editibility->isChecked();
+
+ // We have to make a new LayerItemWidget because it destroys the old one!
+ list->takeItem(currentRow);
+ list->insertItem(currentRow + 1, qlwi);
+ li = new LayerItemWidget(s, visible, editible);
+ list->setItemWidget(qlwi, li);
+ list->setCurrentItem(qlwi);
+}
+
+
+//
+// Set button states in this widget to sane values
+//
+void LayerWidget::SetButtonStates(void)
+{
+ int numItems = list->count();
+ int currentRow = list->currentRow();
+
+ layerDown->setEnabled(currentRow == (numItems - 1) ? false : true);
+ layerUp->setEnabled(currentRow == 0 ? false : true);
+ removeLayer->setEnabled(numItems == 1 ? false : true);
+}
+