]> Shamusworld >> Repos - architektonas/blobdiff - src/layerwidget.cpp
Whitespace changes. :-P
[architektonas] / src / layerwidget.cpp
index 9ac85a87379fab084125300448db2f32ccb2429a..1ab9d79f22ec815e80de126bc84fffe29d7d9a2f 100644 (file)
@@ -1,3 +1,4 @@
+//
 // layerwidget.cpp: Layer add/remove/use widget
 //
 // Part of the Architektonas Project
@@ -15,7 +16,6 @@
 #include "global.h"
 #include "layeritemwidget.h"
 
-
 LayerWidget::LayerWidget(void): QWidget(),
        addLayer(new QToolButton), removeLayer(new QToolButton),
        editLayer(new QToolButton), layerUp(new QToolButton),
@@ -69,20 +69,42 @@ LayerWidget::LayerWidget(void): QWidget(),
        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");
 }
 
-
 LayerWidget::~LayerWidget()
 {
 }
 
+void LayerWidget::Reload(void)
+{
+       list->clear();
+
+       for(int i=0; i<Global::numLayers; i++)
+       {
+               QListWidgetItem * qlwi = new QListWidgetItem();
+               LayerItemWidget * liw = new LayerItemWidget(Global::layerName[i].c_str(), Global::layerHidden[i], Global::layerLocked[i], 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)));
+       }
+
+       int layer = (Global::numLayers - Global::activeLayer) - 1;
+       list->setCurrentRow(layer, QItemSelectionModel::SelectCurrent);
+       SetButtonStates();
+}
 
 void LayerWidget::HandleLayerSelected(int currentRow)
 {
-//printf("LayerWidget::HandleLayerSelected(): currentRow = %i\n", currentRow);
-//     emit(LayerSelected(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:
@@ -93,12 +115,11 @@ void LayerWidget::HandleLayerSelected(int currentRow)
        //
        // which is the opposite of the internal numbering.
        Global::activeLayer = (Global::numLayers - currentRow) - 1;
-//printf("LayerWidget::HandleLayerSelected(): currentRow = %i, numLayers = %i, active = %i\n", currentRow, Global::numLayers, Global::activeLayer);
+
        // 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
@@ -114,10 +135,9 @@ void LayerWidget::HandleHideToggle(QListWidgetItem * qlwi, bool 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());
+       emit LayerToggled();
 }
 
-
 void LayerWidget::HandleLockToggle(QListWidgetItem * qlwi, bool state)
 {
        int currentRow = list->row(qlwi);
@@ -127,16 +147,15 @@ void LayerWidget::HandleLockToggle(QListWidgetItem * qlwi, bool 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...
+       // 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();
@@ -151,12 +170,12 @@ void LayerWidget::AddLayer(void)
        SetButtonStates();
 
        // Fix up the global state
-       Global::layerHidden.insert(Global::layerHidden.begin(), false);
-       Global::layerLocked.insert(Global::layerLocked.begin(), false);
+       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();
@@ -168,7 +187,7 @@ void LayerWidget::DeleteLayer(void)
        //       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));
+       emit LayerDeleted(Global::activeLayer);
 
        int currentRow = list->currentRow();
        QListWidgetItem * qlwi = list->currentItem();
@@ -181,6 +200,7 @@ void LayerWidget::DeleteLayer(void)
        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
@@ -189,7 +209,6 @@ void LayerWidget::DeleteLayer(void)
                Global::activeLayer--;
 }
 
-
 void LayerWidget::EditLayer(void)
 {
        // Get the LayerItemWidget so we can edit it (its name, anyway)...
@@ -201,9 +220,16 @@ void LayerWidget::EditLayer(void)
        QString result = QInputDialog::getText(this, tr("Edit Layer Name"), tr("Layer Name:"), QLineEdit::Normal, s, &ok);
 
        if (ok && !result.isEmpty())
+       {
                li->name->setText(result);
-}
 
+               // We don't reverse the layer # here, like elsewhere, because we're
+               // using the layer # directly instead of having to translate it from
+               // the widget.
+               std::vector<std::string>::iterator i = Global::layerName.begin() + Global::activeLayer;
+               (*i) = result.toUtf8().data();
+       }
+}
 
 void LayerWidget::MoveLayerUp(void)
 {
@@ -236,11 +262,13 @@ void LayerWidget::MoveLayerUp(void)
        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));
+       emit LayersSwapped(layer, layer + 1);
 }
 
-
 void LayerWidget::MoveLayerDown(void)
 {
        // Get information out of the LayerItemWidget (& get it from the list!)
@@ -272,11 +300,13 @@ void LayerWidget::MoveLayerDown(void)
        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));
+       emit LayersSwapped(layer, layer - 1);
 }
 
-
 //
 // Set button states in this widget to sane values
 //
@@ -289,4 +319,3 @@ void LayerWidget::SetButtonStates(void)
        layerUp->setEnabled(currentRow == 0 ? false : true);
        removeLayer->setEnabled(numItems == 1 ? false : true);
 }
-