X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flayerwidget.cpp;h=9177b206594aa3979a986495f3e8a079d200e89d;hb=790c1a6d97f73f7457c7fad7e82fa29e5b6accd5;hp=b45eed6a6da6a2f167ed0d2f3f7914331e4b3e3f;hpb=a7a9909f617d5dbcebfc6d4baa7053dbe3961751;p=architektonas diff --git a/src/layerwidget.cpp b/src/layerwidget.cpp index b45eed6..9177b20 100644 --- a/src/layerwidget.cpp +++ b/src/layerwidget.cpp @@ -12,78 +12,67 @@ // #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) { + QListWidgetItem * qlwi = new QListWidgetItem(list); + LayerItemWidget * liw = new LayerItemWidget("Background", false, false, qlwi); + list->setItemWidget(qlwi, liw); + + 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")); - // 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). + 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")); - 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); + 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(list, SIGNAL(itemDoubleClicked(QListWidgetItem *)), this, SLOT(HandleDblClick(QListWidgetItem *))); + 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())); + + connect(liw, SIGNAL(HideToggled(QListWidgetItem *, bool)), this, SLOT(HandleHideToggle(QListWidgetItem *, bool))); + connect(liw, SIGNAL(LockToggled(QListWidgetItem *, bool)), this, SLOT(HandleLockToggle(QListWidgetItem *, bool))); + + list->setCurrentRow(0); + + // We set global variables here, since we are 'in charge' of them (mostly) + Global::activeLayer = 0; + Global::numLayers = 1; + Global::layerHidden.clear(); + Global::layerLocked.clear(); + Global::layerName.clear(); + Global::layerHidden.push_back(false); + Global::layerLocked.push_back(false); + Global::layerName.push_back("Background"); } @@ -91,3 +80,253 @@ LayerWidget::~LayerWidget() { } + +void LayerWidget::Reload(void) +{ + list->clear(); + + for(int i=0; iinsertItem(0, qlwi); + list->setItemWidget(qlwi, liw); + + // Set up SIGNAL/SLOTs for this LayerItemWidget + connect(liw, SIGNAL(HideToggled(QListWidgetItem *, bool)), this, SLOT(HandleHideToggle(QListWidgetItem *, bool))); + connect(liw, SIGNAL(LockToggled(QListWidgetItem *, bool)), this, SLOT(HandleLockToggle(QListWidgetItem *, bool))); + } + + int layer = (Global::numLayers - Global::activeLayer) - 1; + list->setCurrentRow(layer, QItemSelectionModel::SelectCurrent); + SetButtonStates(); +} + + +void LayerWidget::HandleLayerSelected(int currentRow) +{ + // If LayerWidget is empty, bail out + if (currentRow == -1) + return; + + // This is numbered opposite of how it's presented. In other words, the + // bottom of the list is 0, and items above it count upwards. So like this: + // + // (2) Layer #2 + // (1) Layer #1 + // (0) Background + // + // which is the opposite of the internal numbering. + Global::activeLayer = (Global::numLayers - currentRow) - 1; + + // Set button states to sane values + SetButtonStates(); +} + + +// +// What happens here is that for every QListWidgetItem we make, we connect it +// to these handlers. But we only have to worry about that when adding and +// moving a layer. However, when toggling states, we need to toggle the global +// state variables too. +// +void LayerWidget::HandleHideToggle(QListWidgetItem * qlwi, bool state) +{ + int currentRow = list->row(qlwi); + int layer = (Global::numLayers - currentRow) - 1; + std::vector::iterator i = Global::layerHidden.begin() + layer; + (*i) = state; +//printf("Item #%i, new hide state is %s\n", currentRow, (state ? "ON" : "off")); +//printf("LayerWidget: New hide state of layer %i is %s.\n", layer, (state ? "ON" : "off")); + // We do this last, because otherwise the Document would get the wrong state + emit LayerToggled(); +} + + +void LayerWidget::HandleLockToggle(QListWidgetItem * qlwi, bool state) +{ + int currentRow = list->row(qlwi); + int layer = (Global::numLayers - currentRow) - 1; + std::vector::iterator i = Global::layerLocked.begin() + layer; + (*i) = state; +// printf("Item #%i, new lock state is %s\n", list->row(qlwi), (state ? "ON" : "off")); +} + + +void LayerWidget::HandleDblClick(QListWidgetItem * /*qlwi*/) +{ + EditLayer(); +} + + +void LayerWidget::AddLayer(void) +{ + // We always stick the newest layer at the top of the list (visually, the + // top of the list is the end, the bottom is the beginning)... + int count = list->count(); + QString text = QString("Layer #%1").arg(count); + QListWidgetItem * qlwi = new QListWidgetItem(); + LayerItemWidget * liw = new LayerItemWidget(text, false, false, qlwi); + list->insertItem(0, qlwi); + list->setItemWidget(qlwi, liw); + + // Set up SIGNAL/SLOTs for this LayerItemWidget + connect(liw, SIGNAL(HideToggled(QListWidgetItem *, bool)), this, SLOT(HandleHideToggle(QListWidgetItem *, bool))); + connect(liw, SIGNAL(LockToggled(QListWidgetItem *, bool)), this, SLOT(HandleLockToggle(QListWidgetItem *, bool))); + + SetButtonStates(); + + // Fix up the global state + Global::layerHidden.push_back(false); + Global::layerLocked.push_back(false); + Global::layerName.push_back(text.toUtf8().data()); + Global::numLayers++; +} + + +void LayerWidget::DeleteLayer(void) +{ + int numItems = list->count(); + + if (numItems == 1) + return; + + // N.B.: This *must* go before the item removal because that causes + // HandleLayerSelected() to be fired off which causes the numbers to + // be off. You have been warned! + // Tell the DrawingView to delete this layer in its Container: + emit LayerDeleted(Global::activeLayer); + + int currentRow = list->currentRow(); + QListWidgetItem * qlwi = list->currentItem(); + list->removeItemWidget(qlwi); + delete qlwi; + + SetButtonStates(); + + // Fix up the global state + int layer = (Global::numLayers - currentRow) - 1; + Global::layerHidden.erase(Global::layerHidden.begin() + layer); + Global::layerLocked.erase(Global::layerLocked.begin() + layer); + Global::layerName.erase(Global::layerName.begin() + layer); + Global::numLayers--; + + // If we're deleting from the top of the list, we have to decrement the + // active layer # by 1 (since we count upward from the bottom of the list). + if (currentRow == 0) + Global::activeLayer--; +} + + +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); + + int layer = (Global::numLayers - Global::activeLayer) - 1; + std::vector::iterator i = Global::layerName.begin() + layer; + (*i) = result.toUtf8().data(); + } +} + + +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->invisible->isChecked(); + bool editible = li->locked->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, qlwi); + list->setItemWidget(qlwi, li); + list->setCurrentItem(qlwi); + + // Set up SIGNAL/SLOTs for this LayerItemWidget + connect(li, SIGNAL(HideToggled(QListWidgetItem *, bool)), this, SLOT(HandleHideToggle(QListWidgetItem *, bool))); + connect(li, SIGNAL(LockToggled(QListWidgetItem *, bool)), this, SLOT(HandleLockToggle(QListWidgetItem *, bool))); + + // Fix up the global state... + // N.B.: Because we handle the button states correctly, we should never + // have a situation where the reference in the vector is bad. + int layer = (Global::numLayers - currentRow) - 1; + bool old = Global::layerHidden[layer]; + Global::layerHidden[layer] = Global::layerHidden[layer + 1]; + Global::layerHidden[layer + 1] = old; + old = Global::layerLocked[layer]; + Global::layerLocked[layer] = Global::layerLocked[layer + 1]; + Global::layerLocked[layer + 1] = old; + std::string oldStr = Global::layerName[layer]; + Global::layerName[layer] = Global::layerName[layer + 1]; + Global::layerName[layer + 1] = oldStr; + // We also have to tell the document to shuffle its layers too + emit LayersSwapped(layer, layer + 1); +} + + +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->invisible->isChecked(); + bool editible = li->locked->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, qlwi); + list->setItemWidget(qlwi, li); + list->setCurrentItem(qlwi); + + // Set up SIGNAL/SLOTs for this LayerItemWidget + connect(li, SIGNAL(HideToggled(QListWidgetItem *, bool)), this, SLOT(HandleHideToggle(QListWidgetItem *, bool))); + connect(li, SIGNAL(LockToggled(QListWidgetItem *, bool)), this, SLOT(HandleLockToggle(QListWidgetItem *, bool))); + + // Fix up the global state... + // N.B.: Because we handle the button states correctly, we should never + // have a situation where the reference in the vector is bad. + int layer = (Global::numLayers - currentRow) - 1; + bool old = Global::layerHidden[layer]; + Global::layerHidden[layer] = Global::layerHidden[layer - 1]; + Global::layerHidden[layer - 1] = old; + old = Global::layerLocked[layer]; + Global::layerLocked[layer] = Global::layerLocked[layer - 1]; + Global::layerLocked[layer - 1] = old; + std::string oldStr = Global::layerName[layer]; + Global::layerName[layer] = Global::layerName[layer - 1]; + Global::layerName[layer - 1] = oldStr; + // We also have to tell the document to shuffle its layers too + emit LayersSwapped(layer, layer - 1); +} + + +// +// 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); +} +