TextureMapping.cpp
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 #include "PreCompiled.h"
00025 
00026 #ifndef _PreComp_
00027 # include <Inventor/nodes/SoGroup.h>
00028 # include <Inventor/nodes/SoTexture2.h>
00029 # include <QImage>
00030 # include <QMessageBox>
00031 # include <QImageReader>
00032 #endif
00033 
00034 #include <Inventor/nodes/SoTextureCoordinateEnvironment.h>
00035 #include <QDialogButtonBox>
00036 
00037 #include <App/Application.h>
00038 
00039 #include "TextureMapping.h"
00040 #include "BitmapFactory.h"
00041 #include "ui_TextureMapping.h"
00042 #include "Application.h"
00043 #include "Document.h"
00044 #include "View3DInventor.h"
00045 #include "View3DInventorViewer.h"
00046 
00047 using namespace Gui::Dialog;
00048 
00049 
00050 
00051 TextureMapping::TextureMapping(QWidget* parent, Qt::WFlags fl)
00052   : QDialog(parent, fl), grp(0), tex(0), env(0)
00053 {
00054     ui = new Ui_TextureMapping();
00055     ui->setupUi(this);
00056     ui->checkGlobal->hide();
00057 
00058     
00059     QStringList formats;
00060     QList<QByteArray> qtformats = QImageReader::supportedImageFormats();
00061     for (QList<QByteArray>::Iterator it = qtformats.begin(); it != qtformats.end(); ++it) {
00062         formats << QString::fromAscii("*.%1").arg(QLatin1String(*it));
00063     }
00064     
00065     ui->fileChooser->setFilter(tr("Image files (%1)").arg(formats.join(QLatin1String(" "))));
00066 
00067     this->tex = new SoTexture2();
00068     this->tex->ref();
00069     this->env = new SoTextureCoordinateEnvironment();
00070     this->env->ref();
00071 
00072     std::string path = App::GetApplication().Config()["TextureImage"];
00073     if (!path.empty()) {
00074         QString file = QString::fromUtf8(path.c_str());
00075         ui->fileChooser->setFileName(file);
00076         on_fileChooser_fileNameSelected(file);
00077     }
00078 }
00079 
00080 TextureMapping::~TextureMapping()
00081 {
00082     this->tex->unref();
00083     this->env->unref();
00084     delete ui;
00085 }
00086 
00087 void TextureMapping::accept()
00088 {
00089     QDialog::accept();
00090 }
00091 
00092 void TextureMapping::reject()
00093 {
00094     if (this->grp) {
00095         this->grp->removeChild(this->tex);
00096         if (this->grp->findChild(this->env) > -1)
00097             this->grp->removeChild(this->env);
00098         this->grp->unref();
00099     }
00100 
00101     QDialog::reject();
00102 }
00103 
00104 void TextureMapping::changeEvent(QEvent *e)
00105 {
00106     if (e->type() == QEvent::LanguageChange) {
00107         ui->retranslateUi(this);
00108     }
00109     else {
00110         QDialog::changeEvent(e);
00111     }
00112 }
00113 
00114 void TextureMapping::on_fileChooser_fileNameSelected(const QString& s)
00115 {
00116     QImage image;
00117     if (!image.load(s)) {
00118         QMessageBox::warning(this, tr("No image"), tr("The specified file is not a valid image file."));
00119         return;
00120     }
00121 
00122     if (!this->grp) {
00123         Gui::Document* doc = Gui::Application::Instance->activeDocument();
00124         if (doc) {
00125             Gui::MDIView* mdi = doc->getActiveView();
00126             if (mdi && mdi->isDerivedFrom(View3DInventor::getClassTypeId())) {
00127                 Gui::View3DInventorViewer* view = static_cast<View3DInventor*>(mdi)->getViewer();
00128                 this->grp = static_cast<SoGroup *>(view->getSceneGraph());
00129                 this->grp->ref();
00130                 this->grp->insertChild(this->tex,1);
00131                 if (ui->checkEnv->isChecked())
00132                     this->grp->insertChild(this->env,2);
00133             }
00134         }
00135     }
00136 
00137     if (!this->grp) {
00138         QMessageBox::warning(this, tr("No 3d view"), tr("No active 3d view found."));
00139         return;
00140     }
00141 
00142     SoSFImage texture;
00143     Gui::BitmapFactory().convert(image, texture);
00144     this->tex->image = texture;
00145     
00146     App::GetApplication().Config()["TextureImage"] = (const char*)s.toUtf8();
00147 }
00148 
00149 void TextureMapping::on_checkEnv_toggled(bool b)
00150 {
00151     if (!this->grp)
00152         return;
00153     if (b) {
00154         this->grp->insertChild(this->env,2);
00155     }
00156     else {
00157         this->grp->removeChild(this->env);
00158     }
00159 }
00160 
00161 
00162 
00163 TaskTextureMapping::TaskTextureMapping()
00164 {
00165     dialog = new TextureMapping();
00166     taskbox = new Gui::TaskView::TaskBox(QPixmap(), dialog->windowTitle(), true, 0);
00167     taskbox->groupLayout()->addWidget(dialog);
00168     Content.push_back(taskbox);
00169 }
00170 
00171 TaskTextureMapping::~TaskTextureMapping()
00172 {
00173     
00174 }
00175 
00176 bool TaskTextureMapping::accept()
00177 {
00178     dialog->accept();
00179     return (dialog->result() == QDialog::Accepted);
00180 }
00181 
00182 bool TaskTextureMapping::reject()
00183 {
00184     dialog->reject();
00185     return (dialog->result() == QDialog::Rejected);
00186 }
00187 
00188 #include "moc_TextureMapping.cpp"