00001 00002 /****************************************************************************\ 00003 * REQUIRED HEADERS * 00004 \****************************************************************************/ 00005 #include <QFile> 00006 #include <QFileInfo> 00007 00008 #include "CShaderFile.h" 00009 #include "CEditorProc.h" 00010 00011 /****************************************************************************\ 00012 * PRIVATE DEFINITIONS * 00013 \****************************************************************************/ 00014 int CShaderFile::nextID = 0; 00015 00016 /**************************************************************************\ 00017 * PUBLIC CLASS FUNCTIONS * 00018 \**************************************************************************/ 00019 00020 //Constructor 00021 CShaderFile::CShaderFile(QObject *parent) 00022 : QTextDocument(parent) 00023 { 00024 pFile = NULL; 00025 pEditProcess = NULL; 00026 currentFileName = "newShader"; 00027 editTextReceived = ""; 00028 tabID = -1; 00029 editing = false; 00030 00031 shaderID = 0; 00032 shaderAttached = false; 00033 shaderCompiled = false; 00034 } 00035 //Destructor 00036 CShaderFile::~CShaderFile(){ 00037 SAFE_DELETE(pEditProcess); 00038 } 00039 00040 bool CShaderFile::Create(int ShaderType){ 00041 if(ShaderType == VERTEX_SHADER){ 00042 shaderType = VERTEX_SHADER; 00043 currentFileName = "newVertex"; 00044 pEditProcess = new CEditorProc(QString("edit_vertexShader").append("_").append(QString().setNum(nextID++))); 00045 }else{ 00046 shaderType = FRAGMENT_SHADER; 00047 currentFileName = "newFragment"; 00048 pEditProcess = new CEditorProc(QString("edit_fragmentShader").append("_").append(QString().setNum(nextID++))); 00049 } 00050 return true; 00051 } 00052 00053 QString CShaderFile::GetFileName(){ 00054 return currentFileName; 00055 } 00056 00057 bool CShaderFile::isLoaded(){ 00058 return (pFile != NULL); 00059 } 00060 00061 bool CShaderFile::load(QString & text){ 00062 if(text.size()){ 00063 if(pFile == NULL){ 00064 pFile = new QFile(text,this); 00065 } 00066 pFile->setFileName(text); 00067 if(pFile->open(QIODevice::ReadWrite)){ 00068 setPlainText(pFile->readAll()); 00069 pFile->close(); 00070 setModified(false); 00071 currentFileName = text; 00072 return true; 00073 }else{ 00074 delete pFile; 00075 pFile = NULL; 00076 } 00077 } 00078 return false; 00079 } 00080 00081 bool CShaderFile::isUpToDate(){ 00082 return !isModified(); 00083 } 00084 00085 bool CShaderFile::saveAs(QString & text){ 00086 if(text.size()){ 00087 if(pFile==NULL){ 00088 pFile = new QFile(this); 00089 } 00090 pFile->setFileName(text); 00091 if(pFile->open(QIODevice::WriteOnly)){ 00092 pFile->write(toPlainText().toLocal8Bit()); 00093 pFile->close(); 00094 setModified(false); 00095 currentFileName = text; 00096 return true; 00097 } 00098 } 00099 return false; 00100 } 00101 00102 bool CShaderFile::isModified(){ 00103 return QTextDocument::isModified(); 00104 } 00105 00106 bool CShaderFile::StartEdit(QString & editorPath,QStringList & editorArgs){ 00107 00108 if(pEditProcess){ 00109 if(pEditProcess->StartEdit(editorPath,editorArgs,toPlainText(),QFileInfo(currentFileName).baseName())){ 00110 editing = true; 00111 return true; 00112 } 00113 } 00114 return false; 00115 } 00116 00117 QString & CShaderFile::StopEdit(){ 00118 00119 editing = false; 00120 if(pEditProcess){ 00121 return editTextReceived = pEditProcess->StopEdit(); 00122 }else{ 00123 return editTextReceived; 00124 } 00125 }