Skip to content

Commit

Permalink
some black magic involving templates which doesn't even work, pushing…
Browse files Browse the repository at this point in the history
… so I can work on it at home
  • Loading branch information
Treeki committed Sep 5, 2012
1 parent f928869 commit 2e52bea
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 6 deletions.
6 changes: 4 additions & 2 deletions LayoutStudio.pro
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ SOURCES += main.cpp \
lstexcoordseteditor.cpp \
lsmaterialeditor.cpp \
lscolorpicker.cpp \
lsmaterialmodel.cpp
lsmaterialmodel.cpp \
lsseteditor.cpp
HEADERS += lsmainwindow.h \
lsglobals.h \
lyt/packagebase.h \
Expand Down Expand Up @@ -85,7 +86,8 @@ HEADERS += lsmainwindow.h \
lstexcoordseteditor.h \
lsmaterialeditor.h \
lscolorpicker.h \
lsmaterialmodel.h
lsmaterialmodel.h \
lsseteditor.h
FORMS +=
RESOURCES += resources.qrc

Expand Down
23 changes: 19 additions & 4 deletions lsmaterialeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ LSMaterialEditor::LSMaterialEditor(QWidget *parent) :
QWidget(parent) {

QGridLayout *grid = new QGridLayout(this);
int gridRow = 0;

m_nameEntry = new QLineEdit(this);

grid->addWidget(new QLabel("Name:", this), 0, 0, 1, 1);
grid->addWidget(m_nameEntry, 0, 1, 1, 1);
grid->addWidget(new QLabel("Name:", this), gridRow, 0, 1, 1);
grid->addWidget(m_nameEntry, gridRow, 1, 1, 1);
gridRow++;

// TEV colour box
QGroupBox *tevCBox = new QGroupBox("TEV Colours", this);
Expand All @@ -24,10 +26,21 @@ LSMaterialEditor::LSMaterialEditor(QWidget *parent) :
tevCLayout->addWidget(m_colourPickers[i], isKonst?3:1, isKonst?(i-3):i, 1, 1);
}

grid->addWidget(tevCBox, 1, 0, 1, 2);
grid->addWidget(tevCBox, gridRow, 0, 1, 2);
gridRow++;

// TEV stages
QGroupBox *stageBox = new QGroupBox("TEV Stages", this);
QVBoxLayout *stageLayout = new QVBoxLayout(stageBox);

grid->setRowStretch(2, 1);
m_tevStageSetEditor = new LSSetEditor<LYTTevStage, LSTevStageEditor>(8, stageBox);
stageLayout->addWidget(m_tevStageSetEditor);

grid->addWidget(stageBox, gridRow, 0, 1, 2);
gridRow++;

// finish it all up
grid->setRowStretch(gridRow, 1);


m_currentlyLoadingMaterial = false;
Expand All @@ -45,6 +58,8 @@ void LSMaterialEditor::setMaterial(LYTMaterial *mat) {
for (int i = 0; i < 4; i++)
m_colourPickers[i+3]->setColor(mat->tevKColour[i]);

m_tevStageSetEditor->setData(&mat->tevStages);

m_currentlyLoadingMaterial = false;
}

Expand Down
13 changes: 13 additions & 0 deletions lsmaterialeditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@
#include <QLineEdit>
#include "lyt/materials/materialcontainer.h"
#include "lscolorpicker.h"
#include "lsseteditor.h"

class LSTevStageEditor : public LSSetEntryEditorBase<LYTTevStage> {
Q_OBJECT
public:
explicit LSTevStageEditor(QWidget *parent = 0) :
LSSetEntryEditorBase(parent) { }

void loadEntryFrom(const LYTTevStage &entry) {
}
};

class LSMaterialEditor : public QWidget {
Q_OBJECT
Expand All @@ -21,6 +32,8 @@ class LSMaterialEditor : public QWidget {
bool m_currentlyLoadingMaterial;
LYTMaterial *m_material;

LSSetEditor<LYTTevStage, LSTevStageEditor> *m_tevStageSetEditor;

private slots:
void handleNameChanged(QString value);
void handleSaveChangedName();
Expand Down
60 changes: 60 additions & 0 deletions lsseteditor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "lsseteditor.h"
#include <QGridLayout>
#include <QLabel>

_LSSetEditorBase::_LSSetEditorBase(int maxEntries, QWidget *parent) :
QWidget(parent) {

m_maxEntries = maxEntries;
m_loadingThings = 0;
}


void _LSSetEditorBase::handleEntrySelected(int index) {
if (!m_loadingThings)
showEntry(index);
}


void _LSSetEditorBase::handleEntryCountChanged(int count) {
if (!m_loadingThings) {
int oldCount = m_chooser->count();
if (oldCount == count)
return;

changeEntryCountTo(count);
resizeDataListTo(count);

// moving from 0 to something...?
if (oldCount == 0) {
m_chooser->setCurrentIndex(0);
showEntry(0);
}

emit dataEdited();
}
}


void _LSSetEditorBase::setup(QWidget *eWidget) {
m_loadingThings++;

m_entryCount = new QSpinBox(this);
m_entryCount->setRange(0, m_maxEntries);
connect(m_entryCount, SIGNAL(valueChanged(int)), SLOT(handleEntryCountChanged(int)));

m_chooser = new QComboBox(this);
connect(m_chooser, SIGNAL(currentIndexChanged(int)), SLOT(handleEntrySelected(int)));

QGridLayout *layout = new QGridLayout(this);

layout->addWidget(new QLabel("Count:", this), 0, 0, 1, 1);
layout->addWidget(m_entryCount, 0, 1, 1, 1);
layout->setColumnMinimumWidth(2, 10);
layout->addWidget(m_chooser, 0, 3, 1, 1);
layout->setColumnStretch(3, 1);

layout->addWidget(eWidget, 1, 0, 1, 4);

m_loadingThings--;
}
148 changes: 148 additions & 0 deletions lsseteditor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#ifndef LSSETEDITOR_H
#define LSSETEDITOR_H

#include <QWidget>
#include <QSpinBox>
#include <QComboBox>

class _LSSetEntryEditorBaseBase : public QWidget {
Q_OBJECT
protected:
explicit _LSSetEntryEditorBaseBase(QWidget *parent = 0) :
QWidget(parent) { }

signals:
void dataEdited();
};

template <typename TData>
class LSSetEntryEditorBase : public _LSSetEntryEditorBaseBase {
protected:
explicit LSSetEntryEditorBase(QWidget *parent = 0) :
_LSSetEntryEditorBaseBase(parent) { }

private:
void setCurrentEntry(TData &entry);
// what??
//friend class LSSetEditor<TData, LSSetEntryEditorBase<TData> >;

TData *m_currentEntry;

protected:
virtual void loadEntryFrom(const TData &entry) = 0;
TData *currentEntry() const { return m_currentEntry; }
};




class _LSSetEditorBase : public QWidget {
Q_OBJECT
public:
explicit _LSSetEditorBase(int maxEntries, QWidget *parent = 0);

protected:
int m_maxEntries;
QSpinBox *m_entryCount;
QComboBox *m_chooser;

QWidget *m_setEditorWidget;

int m_loadingThings;

void setup(QWidget *eWidget);

virtual void changeEntryCountTo(int count) = 0;
virtual void showEntry(int index) = 0;
virtual void resizeDataListTo(int count) = 0;

private slots:
void handleEntryCountChanged(int count);
void handleEntrySelected(int index);

signals:
void dataEdited();

};

template <typename TData, typename TWidget>
class LSSetEditor : public _LSSetEditorBase {
public:
explicit LSSetEditor(int maxEntries, QWidget *parent = 0) :
_LSSetEditorBase(maxEntries, parent) {

TWidget *w = new TWidget(this);
setup(w);

LSSetEntryEditorBase<TData> *checkMe = w;
connect(checkMe, SIGNAL(dataEdited()), SIGNAL(dataEdited()));
}

void setData(QList<TData> *newData) {
m_loadingThings++;

m_data = newData;
m_entryCount->setValue(newData->count());
changeEntryCountTo(newData->count());

m_chooser->setCurrentIndex(newData->count() ? 0 : -1);
showEntry(newData->count() ? 0 : -1);

m_loadingThings--;
}

protected:
QList<TData> *m_data;

void changeEntryCountTo(int count) {
m_loadingThings++;

int existingCount = m_chooser->count();

if (existingCount > count) {
// remove something
int nowSelected = m_chooser->currentIndex();

if (nowSelected >= count) {
// oops, we'll need to select something else
showEntry(count - 1);
m_chooser->setCurrentIndex(count - 1);
}

for (int i = (existingCount - 1); i >= count; i--)
m_chooser->removeItem(i);

} else if (count > existingCount) {
// add something

for (int i = existingCount; i < count; i++)
m_chooser->addItem(QString("Set %1").arg(i + 1));
}

m_loadingThings--;
}

void resizeDataListTo(int count) {
m_data->reserve(count);

while (m_data->count() < count)
m_data->append(TData());
while (m_data->count() > count)
m_data->removeLast();
}

void showEntry(int index) {
m_loadingThings++;

if (index == -1) {
m_setEditorWidget->setEnabled(false);
} else {
const TData &entry = m_data->at(index);
}
}
};




#endif // LSSETEDITOR_H

0 comments on commit 2e52bea

Please sign in to comment.