paysages : Layer sorting (WIP).

git-svn-id: https://subversion.assembla.com/svn/thunderk/paysages@377 b1fd45b6-86a6-48da-8261-f70d1f35bdcc
This commit is contained in:
Michaël Lemaire 2012-07-05 11:37:50 +00:00 committed by ThunderK
parent 4b050362c5
commit d3718bde00
8 changed files with 199 additions and 131 deletions

View file

@ -20,8 +20,8 @@ BaseForm::BaseForm(QWidget* parent, bool auto_apply, bool with_layers) : QWidget
QWidget* layers; QWidget* layers;
QLabel* label; QLabel* label;
this->auto_apply = auto_apply; this->_auto_apply = auto_apply;
this->with_layers = with_layers; this->_with_layers = with_layers;
setLayout(new QHBoxLayout()); setLayout(new QHBoxLayout());
setObjectName("_base_form_"); setObjectName("_base_form_");
@ -31,6 +31,8 @@ BaseForm::BaseForm(QWidget* parent, bool auto_apply, bool with_layers) : QWidget
control->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding); control->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
layout()->addWidget(control); layout()->addWidget(control);
_layer_count = 0;
if (with_layers) if (with_layers)
{ {
layers = new QWidget(this); layers = new QWidget(this);
@ -41,58 +43,74 @@ BaseForm::BaseForm(QWidget* parent, bool auto_apply, bool with_layers) : QWidget
label->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); label->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
layers->layout()->addWidget(label); layers->layout()->addWidget(label);
layer_list = new QComboBox(layers); _layer_list = new QComboBox(layers);
layers->layout()->addWidget(layer_list); layers->layout()->addWidget(_layer_list);
QObject::connect(layer_list, SIGNAL(currentIndexChanged(int)), this, SLOT(layerListChanged())); QObject::connect(_layer_list, SIGNAL(currentIndexChanged(int)), this, SLOT(layerListChanged()));
layer_new = new QPushButton(tr("Add layer"), layers); _layer_new = new QPushButton(QIcon("images/layer_add.png"), "", layers);
layers->layout()->addWidget(layer_new); _layer_new->setToolTip(tr("Add layer"));
QObject::connect(layer_new, SIGNAL(clicked()), this, SLOT(layerAddClicked())); _layer_new->setMaximumSize(30, 30);
layers->layout()->addWidget(_layer_new);
QObject::connect(_layer_new, SIGNAL(clicked()), this, SLOT(layerAddClicked()));
layer_del = new QPushButton(tr("Delete layer"), layers); _layer_del = new QPushButton(QIcon("images/layer_del.png"), "", layers);
layers->layout()->addWidget(layer_del); _layer_del->setToolTip(tr("Delete layer"));
QObject::connect(layer_del, SIGNAL(clicked()), this, SLOT(layerDelClicked())); _layer_del->setMaximumSize(30, 30);
layers->layout()->addWidget(_layer_del);
QObject::connect(_layer_del, SIGNAL(clicked()), this, SLOT(layerDelClicked()));
_layer_up = new QPushButton(QIcon("images/layer_up.png"), "", layers);
_layer_up->setToolTip(tr("Move layer upward"));
_layer_up->setMaximumSize(30, 30);
layers->layout()->addWidget(_layer_up);
QObject::connect(_layer_up, SIGNAL(clicked()), this, SLOT(layerUpClicked()));
_layer_down = new QPushButton(QIcon("images/layer_down.png"), "", layers);
_layer_down->setToolTip(tr("Move layer downward"));
_layer_down->setMaximumSize(30, 30);
layers->layout()->addWidget(_layer_down);
QObject::connect(_layer_down, SIGNAL(clicked()), this, SLOT(layerDownClicked()));
control->layout()->addWidget(layers); control->layout()->addWidget(layers);
control->layout()->setAlignment(buttons, Qt::AlignTop); control->layout()->setAlignment(_buttons, Qt::AlignTop);
} }
previews = new QWidget(this); _previews = new QWidget(this);
previews->setLayout(new QVBoxLayout()); _previews->setLayout(new QVBoxLayout());
previews->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); _previews->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
layout()->addWidget(previews); layout()->addWidget(_previews);
layout()->setAlignment(previews, Qt::AlignTop); layout()->setAlignment(_previews, Qt::AlignTop);
form = new QWidget(this); _form = new QWidget(this);
form->setLayout(new QHBoxLayout()); _form->setLayout(new QHBoxLayout());
control->layout()->addWidget(form); control->layout()->addWidget(_form);
control->layout()->setAlignment(form, Qt::AlignTop); control->layout()->setAlignment(_form, Qt::AlignTop);
form_labels = new QWidget(form); _form_labels = new QWidget(_form);
form_labels->setLayout(new QVBoxLayout()); _form_labels->setLayout(new QVBoxLayout());
form->layout()->addWidget(form_labels); _form->layout()->addWidget(_form_labels);
form_previews = new QWidget(form); _form_previews = new QWidget(_form);
form_previews->setLayout(new QVBoxLayout()); _form_previews->setLayout(new QVBoxLayout());
form->layout()->addWidget(form_previews); _form->layout()->addWidget(_form_previews);
form_controls = new QWidget(form); _form_controls = new QWidget(_form);
form_controls->setLayout(new QVBoxLayout()); _form_controls->setLayout(new QVBoxLayout());
form->layout()->addWidget(form_controls); _form->layout()->addWidget(_form_controls);
buttons = new QWidget(this); _buttons = new QWidget(this);
buttons->setLayout(new QHBoxLayout()); _buttons->setLayout(new QHBoxLayout());
control->layout()->addWidget(buttons); control->layout()->addWidget(_buttons);
control->layout()->setAlignment(buttons, Qt::AlignBottom); control->layout()->setAlignment(_buttons, Qt::AlignBottom);
button_apply = addButton(tr("Apply")); _button_apply = addButton(tr("Apply"));
button_apply->setEnabled(false); _button_apply->setEnabled(false);
connect(button_apply, SIGNAL(clicked()), this, SLOT(applyConfig())); connect(_button_apply, SIGNAL(clicked()), this, SLOT(applyConfig()));
button_revert = addButton(tr("Revert")); _button_revert = addButton(tr("Revert"));
button_revert->setEnabled(false); _button_revert->setEnabled(false);
connect(button_revert, SIGNAL(clicked()), this, SLOT(revertConfig())); connect(_button_revert, SIGNAL(clicked()), this, SLOT(revertConfig()));
auto_update_previews = true; _auto_update_previews = true;
if (auto_apply) if (auto_apply)
{ {
@ -102,15 +120,15 @@ BaseForm::BaseForm(QWidget* parent, bool auto_apply, bool with_layers) : QWidget
void BaseForm::hideButtons() void BaseForm::hideButtons()
{ {
button_apply->hide(); _button_apply->hide();
button_revert->hide(); _button_revert->hide();
} }
void BaseForm::savePack(PackStream* stream) void BaseForm::savePack(PackStream* stream)
{ {
// Save previews status // Save previews status
// TODO Ensure same order in save and load // TODO Ensure same order in save and load
QList<BasePreview*> list_previews = previews->findChildren<BasePreview*>("_form_preview_"); QList<BasePreview*> list_previews = _previews->findChildren<BasePreview*>("_form_preview_");
for (int i = 0; i < list_previews.size(); i++) for (int i = 0; i < list_previews.size(); i++)
{ {
list_previews[i]->savePack(stream); list_previews[i]->savePack(stream);
@ -121,7 +139,7 @@ void BaseForm::loadPack(PackStream* stream)
{ {
// Load previews status // Load previews status
// TODO Ensure same order in save and load // TODO Ensure same order in save and load
QList<BasePreview*> list_previews = previews->findChildren<BasePreview*>("_form_preview_"); QList<BasePreview*> list_previews = _previews->findChildren<BasePreview*>("_form_preview_");
for (int i = 0; i < list_previews.size(); i++) for (int i = 0; i < list_previews.size(); i++)
{ {
list_previews[i]->loadPack(stream); list_previews[i]->loadPack(stream);
@ -130,20 +148,20 @@ void BaseForm::loadPack(PackStream* stream)
void BaseForm::configChangeEvent() void BaseForm::configChangeEvent()
{ {
if (auto_apply) if (_auto_apply)
{ {
applyConfig(); applyConfig();
} }
else else
{ {
button_apply->setEnabled(true); _button_apply->setEnabled(true);
button_revert->setEnabled(true); _button_revert->setEnabled(true);
} }
QList<BaseInput*> inputs = form->findChildren<BaseInput*>("_form_input_"); QList<BaseInput*> inputs = _form->findChildren<BaseInput*>("_form_input_");
for (int i = 0; i < inputs.size(); i++) for (int i = 0; i < inputs.size(); i++)
{ {
if (with_layers && layer_list->count() == 0) if (_with_layers && _layer_list->count() == 0)
{ {
inputs[i]->checkVisibility(false); inputs[i]->checkVisibility(false);
} }
@ -153,7 +171,7 @@ void BaseForm::configChangeEvent()
} }
} }
if (auto_update_previews) if (_auto_update_previews)
{ {
updatePreviews(); updatePreviews();
} }
@ -161,70 +179,129 @@ void BaseForm::configChangeEvent()
void BaseForm::revertConfig() void BaseForm::revertConfig()
{ {
QList<BaseInput*> inputs = form->findChildren<BaseInput*>("_form_input_"); QList<BaseInput*> inputs = _form->findChildren<BaseInput*>("_form_input_");
for (int i = 0; i < inputs.size(); i++) for (int i = 0; i < inputs.size(); i++)
{ {
inputs[i]->revert(); inputs[i]->revert();
} }
if (with_layers) if (_with_layers)
{ {
if (layer_list->currentIndex() < 0 && layer_list->count() > 0) if (_layer_list->currentIndex() < 0 && _layer_list->count() > 0)
{ {
layer_list->setCurrentIndex(0); _layer_list->setCurrentIndex(0);
} }
} }
updatePreviews(); updatePreviews();
//configChangeEvent(); //configChangeEvent();
button_apply->setEnabled(false); _button_apply->setEnabled(false);
button_revert->setEnabled(false); _button_revert->setEnabled(false);
} }
void BaseForm::applyConfig() void BaseForm::applyConfig()
{ {
button_apply->setEnabled(false); _button_apply->setEnabled(false);
button_revert->setEnabled(false); _button_revert->setEnabled(false);
emit(configApplied()); emit(configApplied());
} }
void BaseForm::rebuildLayerList()
{
if (_with_layers)
{
int selected = _layer_list->currentIndex();
_layer_list->clear();
for (int i = 0; i < _layer_count; i++)
{
_layer_list->addItem(QString(tr("Layer %1")).arg(i + 1));
}
if (selected >= 0)
{
if (selected >= _layer_count)
{
_layer_list->setCurrentIndex(_layer_count - 1);
}
else
{
_layer_list->setCurrentIndex(selected);
}
}
}
}
void BaseForm::layerAddClicked() void BaseForm::layerAddClicked()
{ {
layerAddedEvent(); layerAddedEvent();
button_apply->setEnabled(true); rebuildLayerList();
button_revert->setEnabled(true); _layer_list->setCurrentIndex(_layer_list->count() - 1);
_button_apply->setEnabled(true);
_button_revert->setEnabled(true);
} }
void BaseForm::layerDelClicked() void BaseForm::layerDelClicked()
{ {
layerDeletedEvent(layer_list->currentIndex()); if (_layer_list->currentIndex() >= 0)
{
layerDeletedEvent(_layer_list->currentIndex());
rebuildLayerList();
_button_apply->setEnabled(true);
_button_revert->setEnabled(true);
}
}
button_apply->setEnabled(true); void BaseForm::layerUpClicked()
button_revert->setEnabled(true); {
if (_layer_list->currentIndex() < _layer_count - 1)
{
layerMovedEvent(_layer_list->currentIndex(), _layer_list->currentIndex() + 1);
rebuildLayerList();
_button_apply->setEnabled(true);
_button_revert->setEnabled(true);
}
}
void BaseForm::layerDownClicked()
{
if (_layer_list->currentIndex() > 0)
{
layerMovedEvent(_layer_list->currentIndex(), _layer_list->currentIndex() - 1);
rebuildLayerList();
_button_apply->setEnabled(true);
_button_revert->setEnabled(true);
}
} }
void BaseForm::layerListChanged() void BaseForm::layerListChanged()
{ {
bool changed = button_apply->isEnabled(); bool changed = _button_apply->isEnabled();
layerSelectedEvent(layer_list->currentIndex()); layerSelectedEvent(_layer_list->currentIndex());
button_apply->setEnabled(changed); _button_apply->setEnabled(changed);
button_revert->setEnabled(changed); _button_revert->setEnabled(changed);
} }
void BaseForm::addPreview(BasePreview* preview, QString label) void BaseForm::addPreview(BasePreview* preview, QString label)
{ {
QLabel* label_widget; QLabel* label_widget;
label_widget = new QLabel(label, previews); label_widget = new QLabel(label, _previews);
label_widget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed); label_widget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
previews->layout()->addWidget(label_widget); _previews->layout()->addWidget(label_widget);
previews->layout()->addWidget(preview); _previews->layout()->addWidget(preview);
preview->setObjectName("_form_preview_"); preview->setObjectName("_form_preview_");
} }
@ -232,7 +309,7 @@ void BaseForm::addPreview(BasePreview* preview, QString label)
QPushButton* BaseForm::addButton(QString label) QPushButton* BaseForm::addButton(QString label)
{ {
QPushButton* button = new QPushButton(label); QPushButton* button = new QPushButton(label);
buttons->layout()->addWidget(button); _buttons->layout()->addWidget(button);
return button; return button;
} }
@ -240,9 +317,9 @@ BaseInput* BaseForm::addInput(BaseInput* input)
{ {
int row_height = 30; int row_height = 30;
form_labels->layout()->addWidget(input->label()); _form_labels->layout()->addWidget(input->label());
form_previews->layout()->addWidget(input->preview()); _form_previews->layout()->addWidget(input->preview());
form_controls->layout()->addWidget(input->control()); _form_controls->layout()->addWidget(input->control());
input->label()->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed); input->label()->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
input->label()->setMinimumSize(150, row_height); input->label()->setMinimumSize(150, row_height);
@ -266,52 +343,52 @@ BaseInput* BaseForm::addInput(BaseInput* input)
BaseInput* BaseForm::addInputInt(QString label, int* value, int min, int max, int small_step, int large_step) BaseInput* BaseForm::addInputInt(QString label, int* value, int min, int max, int small_step, int large_step)
{ {
return addInput(new InputInt(form, label, value, min, max, small_step, large_step)); return addInput(new InputInt(_form, label, value, min, max, small_step, large_step));
} }
BaseInput* BaseForm::addInputDouble(QString label, double* value, double min, double max, double small_step, double large_step) BaseInput* BaseForm::addInputDouble(QString label, double* value, double min, double max, double small_step, double large_step)
{ {
return addInput(new InputDouble(form, label, value, min, max, small_step, large_step)); return addInput(new InputDouble(_form, label, value, min, max, small_step, large_step));
} }
BaseInput* BaseForm::addInputBoolean(QString label, int* value) BaseInput* BaseForm::addInputBoolean(QString label, int* value)
{ {
return addInput(new InputBoolean(form, label, value)); return addInput(new InputBoolean(_form, label, value));
} }
BaseInput* BaseForm::addInputColor(QString label, Color* value) BaseInput* BaseForm::addInputColor(QString label, Color* value)
{ {
return addInput(new InputColor(form, label, value)); return addInput(new InputColor(_form, label, value));
} }
BaseInput* BaseForm::addInputColorGradation(QString label, ColorGradation* value) BaseInput* BaseForm::addInputColorGradation(QString label, ColorGradation* value)
{ {
return addInput(new InputColorGradation(form, label, value)); return addInput(new InputColorGradation(_form, label, value));
} }
BaseInput* BaseForm::addInputNoise(QString label, NoiseGenerator* value) BaseInput* BaseForm::addInputNoise(QString label, NoiseGenerator* value)
{ {
return addInput(new InputNoise(form, label, value)); return addInput(new InputNoise(_form, label, value));
} }
BaseInput* BaseForm::addInputCurve(QString label, Curve* value, double xmin, double xmax, double ymin, double ymax, QString xlabel, QString ylabel) BaseInput* BaseForm::addInputCurve(QString label, Curve* value, double xmin, double xmax, double ymin, double ymax, QString xlabel, QString ylabel)
{ {
return addInput(new InputCurve(form, label, value, xmin, xmax, ymin, ymax, xlabel, ylabel)); return addInput(new InputCurve(_form, label, value, xmin, xmax, ymin, ymax, xlabel, ylabel));
} }
BaseInput* BaseForm::addInputMaterial(QString label, SurfaceMaterial* material) BaseInput* BaseForm::addInputMaterial(QString label, SurfaceMaterial* material)
{ {
return addInput(new InputMaterial(form, label, material)); return addInput(new InputMaterial(_form, label, material));
} }
BaseInput* BaseForm::addInputEnum(QString label, int* value, const QStringList& values) BaseInput* BaseForm::addInputEnum(QString label, int* value, const QStringList& values)
{ {
return addInput(new InputEnum(form, label, value, values)); return addInput(new InputEnum(_form, label, value, values));
} }
void BaseForm::updatePreviews() void BaseForm::updatePreviews()
{ {
QList<BasePreview*> list_previews = previews->findChildren<BasePreview*>("_form_preview_"); QList<BasePreview*> list_previews = _previews->findChildren<BasePreview*>("_form_preview_");
for (int i = 0; i < list_previews.size(); i++) for (int i = 0; i < list_previews.size(); i++)
{ {
list_previews[i]->redraw(); list_previews[i]->redraw();
@ -320,14 +397,14 @@ void BaseForm::updatePreviews()
void BaseForm::disablePreviewsUpdate() void BaseForm::disablePreviewsUpdate()
{ {
auto_update_previews = false; _auto_update_previews = false;
} }
int BaseForm::currentLayer() int BaseForm::currentLayer()
{ {
if (with_layers) if (_with_layers)
{ {
return layer_list->currentIndex(); return _layer_list->currentIndex();
} }
else else
{ {
@ -337,52 +414,32 @@ int BaseForm::currentLayer()
void BaseForm::setLayerCount(int layer_count) void BaseForm::setLayerCount(int layer_count)
{ {
int i, selected; this->_layer_count = layer_count;
rebuildLayerList();
if (with_layers)
{
selected = layer_list->currentIndex();
layer_list->clear();
for (i = 0; i < layer_count; i++)
{
layer_list->addItem(QString(tr("Layer %1")).arg(i + 1));
}
if (selected >= 0)
{
if (selected > layer_count)
{
layer_list->setCurrentIndex(layer_count - 1);
}
else
{
layer_list->setCurrentIndex(selected);
}
}
}
} }
void BaseForm::layerAddedEvent() void BaseForm::layerAddedEvent()
{ {
setLayerCount(layer_list->count() + 1);
layer_list->setCurrentIndex(layer_list->count() - 1);
} }
void BaseForm::layerDeletedEvent(int layer) void BaseForm::layerDeletedEvent(int layer)
{ {
layer_list->removeItem(layer); }
void BaseForm::layerMovedEvent(int layer, int new_position)
{
} }
void BaseForm::layerSelectedEvent(int layer) void BaseForm::layerSelectedEvent(int layer)
{ {
QList<BaseInput*> inputs = form->findChildren<BaseInput*>("_form_input_"); QList<BaseInput*> inputs = _form->findChildren<BaseInput*>("_form_input_");
for (int i = 0; i < inputs.size(); i++) for (int i = 0; i < inputs.size(); i++)
{ {
inputs[i]->revert(); inputs[i]->revert();
inputs[i]->checkVisibility(layer >= 0); inputs[i]->checkVisibility(layer >= 0);
} }
QList<BasePreview*> list_previews = previews->findChildren<BasePreview*>("_form_preview_"); QList<BasePreview*> list_previews = _previews->findChildren<BasePreview*>("_form_preview_");
for (int i = 0; i < list_previews.size(); i++) for (int i = 0; i < list_previews.size(); i++)
{ {
list_previews[i]->redraw(); list_previews[i]->redraw();

View file

@ -33,8 +33,11 @@ protected slots:
virtual void configChangeEvent(); virtual void configChangeEvent();
private slots: private slots:
void rebuildLayerList();
void layerAddClicked(); void layerAddClicked();
void layerDelClicked(); void layerDelClicked();
void layerUpClicked();
void layerDownClicked();
void layerListChanged(); void layerListChanged();
protected: protected:
@ -59,23 +62,27 @@ protected:
virtual void layerAddedEvent(); virtual void layerAddedEvent();
virtual void layerDeletedEvent(int layer); virtual void layerDeletedEvent(int layer);
virtual void layerMovedEvent(int layer, int new_position);
virtual void layerSelectedEvent(int layer); virtual void layerSelectedEvent(int layer);
private: private:
bool auto_update_previews; bool _auto_update_previews;
bool auto_apply; bool _auto_apply;
bool with_layers; bool _with_layers;
QComboBox* layer_list; QComboBox* _layer_list;
QPushButton* layer_new; int _layer_count;
QPushButton* layer_del; QPushButton* _layer_new;
QWidget* previews; QPushButton* _layer_del;
QWidget* form; QPushButton* _layer_up;
QWidget* form_labels; QPushButton* _layer_down;
QWidget* form_previews; QWidget* _previews;
QWidget* form_controls; QWidget* _form;
QWidget* buttons; QWidget* _form_labels;
QPushButton* button_apply; QWidget* _form_previews;
QPushButton* button_revert; QWidget* _form_controls;
QWidget* _buttons;
QPushButton* _button_apply;
QPushButton* _button_revert;
}; };
#endif // _PAYSAGES_QT_BASEFORM_H_ #endif

View file

@ -196,6 +196,7 @@ void FormClouds::layerAddedEvent()
{ {
if (cloudsAddLayer(&_definition) >= 0) if (cloudsAddLayer(&_definition) >= 0)
{ {
setLayerCount(cloudsGetLayerCount(&_definition));
BaseForm::layerAddedEvent(); BaseForm::layerAddedEvent();
} }
} }
@ -203,6 +204,7 @@ void FormClouds::layerAddedEvent()
void FormClouds::layerDeletedEvent(int layer) void FormClouds::layerDeletedEvent(int layer)
{ {
cloudsDeleteLayer(&_definition, layer); cloudsDeleteLayer(&_definition, layer);
setLayerCount(cloudsGetLayerCount(&_definition));
BaseForm::layerDeletedEvent(layer); BaseForm::layerDeletedEvent(layer);
} }

View file

@ -185,6 +185,7 @@ void FormTextures::layerAddedEvent()
{ {
if (texturesAddLayer(&_definition) >= 0) if (texturesAddLayer(&_definition) >= 0)
{ {
setLayerCount(texturesGetLayerCount(&_definition));
BaseForm::layerAddedEvent(); BaseForm::layerAddedEvent();
} }
} }
@ -192,6 +193,7 @@ void FormTextures::layerAddedEvent()
void FormTextures::layerDeletedEvent(int layer) void FormTextures::layerDeletedEvent(int layer)
{ {
texturesDeleteLayer(&_definition, layer); texturesDeleteLayer(&_definition, layer);
setLayerCount(texturesGetLayerCount(&_definition));
BaseForm::layerDeletedEvent(layer); BaseForm::layerDeletedEvent(layer);
} }

BIN
images/layer_add.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

BIN
images/layer_del.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

BIN
images/layer_down.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

BIN
images/layer_up.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB