Skip to content

Instantly share code, notes, and snippets.

@shibbo
Created July 17, 2017 02:31
Show Gist options
  • Select an option

  • Save shibbo/842f0f5108be3dad9d5f586a88477bdf to your computer and use it in GitHub Desktop.

Select an option

Save shibbo/842f0f5108be3dad9d5f586a88477bdf to your computer and use it in GitHub Desktop.
#ifndef LOCATIONEDITORWIDGET_H
#define LOCATIONEDITORWIDGET_H
#include "objects/object.h"
#include <QWidget>
#include <QListWidget>
#include <QSpinBox>
class LocationEditorWidget : public QWidget
{
Q_OBJECT
public:
LocationEditorWidget(QList<Location*> *locations);
void deselect();
void select(Location *loc);
void updateEditor();
signals:
void updateLevelView();
void selectedLocChanged(Object* loc);
private slots:
void handleLocationListIndexChange(QListWidgetItem *item);
void handleIDChange(int idVal);
private:
QListWidget* locationList;
QWidget* edits;
QSpinBox* id;
void updateList();
void updateInfo();
QList<Location*> *locations;
Location* editLocation;
bool editingALocation = false;
bool handleChanges = true;
};
#endif // LOCATIONEDITORWIDGET_H
#include "locationeditorwidget.h"
#include "unitsconvert.h"
#include <QVBoxLayout>
#include <QLabel>
LocationEditorWidget::LocationEditorWidget(QList<Location*> *locations)
{
this->locations = locations;
QVBoxLayout* layout = new QVBoxLayout();
setLayout(layout);
locationList = new QListWidget();
connect(locationList, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(handleLocationListIndexChange(QListWidgetItem*)));
layout->addWidget(locationList);
edits = new QWidget();
QGridLayout* subLayout = new QGridLayout();
subLayout->setMargin(0);
edits->setLayout(subLayout);
subLayout->addWidget(new QLabel("ID:"), 0, 0, 1, 1, Qt::AlignRight);
id = new QSpinBox();
id->setRange(0, 255);
connect(id, SIGNAL(valueChanged(int)), this, SLOT(handleIDChange(int)));
subLayout->addWidget(id, 0, 1);
layout->addWidget(edits);
updateList();
updateInfo();
}
void LocationEditorWidget::deselect()
{
editingALocation = false;
updateInfo();
locationList->clearSelection();
}
void LocationEditorWidget::select(Location *loc)
{
editLocation = loc;
editingALocation = true;
updateInfo();
locationList->setCurrentRow(locations->indexOf(loc));
}
void LocationEditorWidget::updateList()
{
QModelIndex index;
if (locationList->selectionModel()->selectedIndexes().size() != 0) index = locationList->selectionModel()->selectedIndexes().at(0);
locationList->clear();
foreach (Location* location, *locations)
locationList->addItem(QString("%1 (at %2,%3) (W: %4 H: %5)").arg(location->getID()).arg(to60(location->getx())).arg(to60(location->gety())).arg(to60(location->getWidth())).arg(to60(location->getHeight())));
locationList->setCurrentIndex(index);
}
void LocationEditorWidget::updateInfo()
{
if (editingALocation == false)
{
edits->setHidden(true);
return;
}
edits->setHidden(false);
handleChanges = false;
id->setValue(editLocation->getID());
handleChanges = true;
}
void LocationEditorWidget::updateEditor()
{
updateList();
}
void LocationEditorWidget::handleIDChange(int idVal)
{
if (!handleChanges) return;
editLocation->setID(idVal);
updateList();
emit updateLevelView();
}
void LocationEditorWidget::handleLocationListIndexChange(QListWidgetItem *item)
{
if (!handleChanges) return;
editLocation = locations->at(locationList->row(item));
editingALocation = true;
updateInfo();
emit selectedLocChanged(editLocation);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment