git-svn-id: svn://db.shs.com.ru/libs@626 a8b55f48-bf90-11e4-a774-851b48703e85
This commit is contained in:
177
qglengine/widgets/materials_editor.cpp
Normal file
177
qglengine/widgets/materials_editor.cpp
Normal file
@@ -0,0 +1,177 @@
|
||||
/*
|
||||
QGLView
|
||||
Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "materials_editor.h"
|
||||
#include "ui_materials_editor.h"
|
||||
#include "material_editor.h"
|
||||
#include "qglview.h"
|
||||
#include "glmaterial.h"
|
||||
#include <qad_types.h>
|
||||
#include <ecombobox.h>
|
||||
#include <QInputDialog>
|
||||
|
||||
|
||||
MaterialsEditor::MaterialsEditor(QWidget * parent): QWidget(parent) {
|
||||
ui = new Ui::MaterialsEditor();
|
||||
ui->setupUi(this);
|
||||
ui->widgetMaterial->setMaterial(0);
|
||||
view = 0;
|
||||
}
|
||||
|
||||
|
||||
void MaterialsEditor::assignQGLView(QGLView * v) {
|
||||
view = v;
|
||||
connect(view, SIGNAL(selectionChanged()), this, SLOT(selectionChanged()));
|
||||
connect(view, SIGNAL(materialsChanged()), this, SLOT(materialsChanged()));
|
||||
connect(view, SIGNAL(materialThumbnailCreated(Material*)), this, SLOT(materialThumbnailCreated(Material*)));
|
||||
materialsChanged();
|
||||
}
|
||||
|
||||
|
||||
bool MaterialsEditor::event(QEvent * e) {
|
||||
if (e->type() == QEvent::FontChange || e->type() == QEvent::Polish) {
|
||||
QSize sz = preferredIconSize(1.5, this);
|
||||
ui->comboMaterial->setIconSize(sz * 3);
|
||||
ui->buttonRename->setIconSize(sz);
|
||||
ui->buttonAdd ->setIconSize(sz);
|
||||
ui->buttonDelete->setIconSize(sz);
|
||||
ui->buttonAssign->setIconSize(sz);
|
||||
}
|
||||
return QWidget::event(e);
|
||||
}
|
||||
|
||||
|
||||
void MaterialsEditor::changeEvent(QEvent * e) {
|
||||
QWidget::changeEvent(e);
|
||||
switch (e->type()) {
|
||||
case QEvent::LanguageChange:
|
||||
ui->retranslateUi(this);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Material * MaterialsEditor::currentMaterial() {
|
||||
if (!view) return 0;
|
||||
int ind = ui->comboMaterial->currentIndex();
|
||||
QVector<Material*> mats = view->scene()->getMaterials();
|
||||
if (ind < 0 || ind >= mats.size()) return 0;
|
||||
return mats[ind];
|
||||
}
|
||||
|
||||
|
||||
void MaterialsEditor::selectMaterial(Material * m) {
|
||||
if (!m) {
|
||||
ui->comboMaterial->setCurrentIndex(-1);
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < ui->comboMaterial->count(); ++i) {
|
||||
if ((Material*)(ui->comboMaterial->itemData(i, Qt::UserRole + 1).value<quintptr>()) == m) {
|
||||
ui->comboMaterial->setCurrentIndex(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int MaterialsEditor::indexByMaterial(Material * m) {
|
||||
if (!m) return -1;
|
||||
for (int i = 0; i < ui->comboMaterial->count(); ++i) {
|
||||
if ((Material*)(ui->comboMaterial->itemData(i, Qt::UserRole + 1).value<quintptr>()) == m)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
void MaterialsEditor::selectionChanged() {
|
||||
if (!view) return;
|
||||
//qDebug() << "selectionChanged";
|
||||
ObjectBase * o = view->selectedObject();
|
||||
if (o) selectMaterial(o->material());
|
||||
}
|
||||
|
||||
|
||||
void MaterialsEditor::materialsChanged() {
|
||||
Material * cm = currentMaterial();
|
||||
ui->comboMaterial->clear();
|
||||
if (!view) return;
|
||||
QVector<Material*> mats = view->scene()->getMaterials();
|
||||
for (int i = 0; i < mats.size(); ++i) {
|
||||
Material * m = mats[i];
|
||||
ui->comboMaterial->addItem(QString("[%1] " + m->name).arg(i + 1), QVariant(m->name));
|
||||
ui->comboMaterial->setItemData(i, QVariant(quintptr(m)), Qt::UserRole + 1);
|
||||
ui->comboMaterial->setItemIcon(i, QIcon(QPixmap::fromImage(view->materialThumbnail(m))));
|
||||
if (cm == m) ui->comboMaterial->setCurrentIndex(i);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void MaterialsEditor::materialThumbnailCreated(Material * m) {
|
||||
//qDebug() << "materialThumbnailCreated" << m;
|
||||
int i = indexByMaterial(m);
|
||||
if (i < 0 || !view) return;
|
||||
//qDebug() << "materialThumbnailCreated set to" << i;
|
||||
ui->comboMaterial->setItemIcon(i, QIcon(QPixmap::fromImage(view->materialThumbnail(m))));
|
||||
}
|
||||
|
||||
|
||||
void MaterialsEditor::on_comboMaterial_currentIndexChanged(int index) {
|
||||
ui->widgetMaterial->setMaterial(currentMaterial());
|
||||
}
|
||||
|
||||
|
||||
void MaterialsEditor::on_buttonRename_clicked() {
|
||||
Material * m = currentMaterial();
|
||||
if (!m) return;
|
||||
QString nn = QInputDialog::getText(this, "Materials editor", "Input new name:", QLineEdit::Normal, m->name);
|
||||
if (nn.isEmpty()) return;
|
||||
m->name = nn;
|
||||
int ind = ui->comboMaterial->currentIndex();
|
||||
ui->comboMaterial->setItemText(ind, QString("[%1] " + nn).arg(ind + 1));
|
||||
ui->comboMaterial->setItemData(ind, nn);
|
||||
}
|
||||
|
||||
|
||||
void MaterialsEditor::on_buttonAdd_clicked() {
|
||||
if (!view) return;
|
||||
Material * m = view->scene()->newMaterial();
|
||||
materialsChanged();
|
||||
selectMaterial(m);
|
||||
}
|
||||
|
||||
|
||||
void MaterialsEditor::on_buttonDelete_clicked() {
|
||||
if (!view) return;
|
||||
Material * m = currentMaterial();
|
||||
if (!m) return;
|
||||
view->scene()->removeMaterial(m);
|
||||
materialsChanged();
|
||||
}
|
||||
|
||||
|
||||
void MaterialsEditor::on_buttonAssign_clicked() {
|
||||
if (!view) return;
|
||||
Material * m = currentMaterial();
|
||||
QList<ObjectBase*> ol = view->selectedObjects();
|
||||
foreach (ObjectBase * o, ol)
|
||||
o->setMaterial(m, true);
|
||||
}
|
||||
Reference in New Issue
Block a user