00001
00002
00003
00004
00005
00006 #include <QFileDialog>
00007 #include <QFile>
00008 #include <QFileInfo>
00009 #include <QMessageBox>
00010 #include <QTemporaryFile>
00011 #include <QTextEdit>
00012 #include <QProcess>
00013 #include "CShaderEditor.h"
00014 #include "CShaderFile.h"
00015 #include "vstudio.h"
00016 #include "VStudioRenderer.h"
00017 #include "GLProgram.h"
00018
00019
00020
00021
00022
00023 #define SHADER_LOAD_FAILED 0
00024 #define SHADER_LOAD_SUCCESS 1
00025 #define SHADER_LOAD_ABORTED 2
00026
00027 #define SHADER_SAVE_FAILED 0
00028 #define SHADER_SAVE_SUCCESS 1
00029 #define SHADER_SAVE_ABORTED 2
00030 #define SHADER_SAVE_NOSAVE 3
00031
00032
00033
00034
00035
00036 CShaderEditor::CShaderEditor(VStudio * const control,QWidget *parent)
00037 : QWidget(parent),pControl(control)
00038 {
00039 ui.setupUi(this);
00040
00041
00042 pQuestionSave = new QMessageBox("Question","Shader not saved, save Shader ?",QMessageBox::NoIcon,QMessageBox::Yes|QMessageBox::Default,QMessageBox::No,QMessageBox::NoButton,this);
00043 pOpenSaveDialog = new QFileDialog(this,"Save Vertex Shader as","./");
00044
00045 pOpenSaveDialog->setViewMode(QFileDialog::List);
00046 pOpenSaveDialog->setConfirmOverwrite(true);
00047
00048
00049 ui.tabWidget->removeTab(0);
00050
00051
00052 ResetUI();
00053
00054
00055 shaderLoadResult = -1;
00056 shaderSaveResult = -1;
00057 editStringSent = "";
00058 lastShaderFilePath = "../DATA/Shader";
00059 }
00060
00061
00062 CShaderEditor::~CShaderEditor(){}
00063
00064
00065
00066 bool CShaderEditor::OpenAndApplyVertex()
00067 {
00068 CShaderFile * newShader = new CShaderFile(this);
00069 newShader->Create(VERTEX_SHADER);
00070 pCSFile = newShader;
00071 if(OpenShader()){
00072 AddShaderTab(newShader);
00073 Attach();
00074 return true;
00075 }else{
00076 delete newShader;
00077 return false;
00078 }
00079 }
00080
00081
00082
00083 bool CShaderEditor::OpenAndApplyFragment()
00084 {
00085 CShaderFile * newShader = new CShaderFile(this);
00086 newShader->Create(FRAGMENT_SHADER);
00087 pCSFile = newShader;
00088 if(OpenShader()){
00089 AddShaderTab(newShader);
00090 Attach();
00091 return true;
00092 }else{
00093 delete newShader;
00094 return false;
00095 }
00096 }
00097
00098
00099
00100 bool CShaderEditor::LoadVertex(QString & fileName)
00101 {
00102 CShaderFile * newShader = new CShaderFile(this);
00103 newShader->Create(VERTEX_SHADER);
00104 pCSFile = newShader;
00105 if(LoadShader(fileName)){
00106 AddShaderTab(newShader);
00107 return true;
00108 }else{
00109 return false;
00110 }
00111 }
00112
00113
00114
00115 bool CShaderEditor::LoadFragment(QString & fileName)
00116 {
00117 CShaderFile * newShader = new CShaderFile(this);
00118 newShader->Create(FRAGMENT_SHADER);
00119 pCSFile = newShader;
00120 if(LoadShader(fileName)){
00121 AddShaderTab(newShader);
00122 return true;
00123 }else{
00124 return false;
00125 }
00126 }
00127
00128 void CShaderEditor::SaveState(QSettings & sceneFile)
00129 {
00130 sceneFile.beginWriteArray("shader");
00131 for(int i = 0 ; i < shaderList.size() ; i++){
00132
00133
00134 if(!shaderList.at(i)->isLoaded()){
00135 if(shaderList.at(i)->isUpToDate()){
00136 continue;
00137 }else{
00138
00139 if(ShowQuestionDialog()){
00140
00141 QString saveAs = ShowSaveDialog();
00142
00143 if(shaderList.at(i)->saveAs(saveAs))
00144 {
00145 shaderSaveResult = SHADER_SAVE_SUCCESS;
00146
00147
00148 sceneFile.setArrayIndex(i);
00149 sceneFile.setValue("filename",QDir(".").relativeFilePath(shaderList.at(i)->GetFileName()));
00150 sceneFile.setValue("type",shaderList.at(i)->GetShaderType());
00151 sceneFile.setValue("state",shaderList.at(i)->GetAttached());
00152
00153 }else{
00154 shaderSaveResult |= SHADER_SAVE_FAILED;
00155 }
00156 }else{
00157 continue;
00158 }
00159 }
00160 }else{
00161 if(shaderList.at(i)->saveAs(shaderList.at(i)->GetFileName()))
00162 {
00163 shaderSaveResult = SHADER_SAVE_SUCCESS;
00164
00165
00166 sceneFile.setArrayIndex(i);
00167 sceneFile.setValue("filename",QDir(".").relativeFilePath(shaderList.at(i)->GetFileName()));
00168 sceneFile.setValue("type",shaderList.at(i)->GetShaderType());
00169 sceneFile.setValue("state",shaderList.at(i)->GetAttached());
00170
00171 }else{
00172 shaderSaveResult |= SHADER_SAVE_FAILED;
00173 }
00174 }
00175 logShaderSaveResult();
00176 }
00177 sceneFile.endArray();
00178 }
00179
00180 void CShaderEditor::OpenState(QSettings & sceneFile)
00181 {
00182 while(!shaderList.isEmpty()){
00183 on_tabWidget_currentChanged(shaderList.last()->GetTabID());
00184 Close();
00185 }
00186
00187 int size = sceneFile.beginReadArray("shader");
00188 for(int i = 0 ; i < size ; i++){
00189 sceneFile.setArrayIndex(i);
00190
00191 if(sceneFile.value("type").toUInt() == VERTEX_SHADER){
00192 LoadVertex(sceneFile.value("filename").toString());
00193 }else{
00194 LoadFragment(sceneFile.value("filename").toString());
00195 }
00196 if(sceneFile.value("state").toBool()){
00197 on_attach_btn_released();
00198 }
00199 }
00200 sceneFile.endArray();
00201 }
00202
00203 void CShaderEditor::ResetState(){
00204
00205 }
00206
00207
00208
00209
00210
00211 bool CShaderEditor::OpenShader()
00212 {
00213 shaderLoadResult = SHADER_LOAD_FAILED;
00214 shaderSaveResult = SHADER_SAVE_NOSAVE;
00215
00216
00217 if(pCSFile->isLoaded()){
00218
00219 if(!pCSFile->isUpToDate()){
00220
00221 if(ShowQuestionDialog()){
00222
00223 QString saveAs = ShowSaveDialog();
00224 if(pCSFile->saveAs(saveAs)){
00225
00226 shaderSaveResult = SHADER_SAVE_SUCCESS;
00227 ui.currentFile_label->setText(pCSFile->GetFileName());
00228
00229 QString openNew = ShowOpenDialog();
00230 if(pCSFile->load(openNew)){
00231
00232 shaderLoadResult = SHADER_LOAD_SUCCESS;
00233 ui.currentFile_label->setText(pCSFile->GetFileName());
00234 }else{
00235 shaderLoadResult |= SHADER_LOAD_FAILED;
00236 }
00237 }else{
00238 shaderSaveResult |= SHADER_SAVE_FAILED;
00239 }
00240 }else{
00241
00242
00243 QString openNew = ShowOpenDialog();
00244 if(pCSFile->load(openNew)){
00245 shaderLoadResult = SHADER_LOAD_SUCCESS;
00246 ui.currentFile_label->setText(pCSFile->GetFileName());
00247 }else{
00248 shaderLoadResult |= SHADER_LOAD_FAILED;
00249 }
00250 }
00251 }else{
00252
00253 QString openNew = ShowOpenDialog();
00254 if(pCSFile->load(openNew)){
00255 shaderLoadResult = SHADER_LOAD_SUCCESS;
00256 ui.currentFile_label->setText(pCSFile->GetFileName());
00257 }else{
00258 shaderLoadResult |= SHADER_LOAD_FAILED;
00259 }
00260 }
00261
00262 }else{
00263
00264 if(!pCSFile->isUpToDate()){
00265
00266 if(pQuestionSave->exec()==QMessageBox::Yes){
00267
00268 QString saveAs = ShowSaveDialog();
00269 if(pCSFile->saveAs(saveAs)){
00270 shaderSaveResult = SHADER_SAVE_SUCCESS;
00271 ui.currentFile_label->setText(pCSFile->GetFileName());
00272
00273 QString openNew = ShowOpenDialog();
00274 if(pCSFile->load(openNew)){
00275 shaderLoadResult = SHADER_LOAD_SUCCESS;
00276 ui.currentFile_label->setText(pCSFile->GetFileName());
00277 }else{
00278 shaderLoadResult |= SHADER_LOAD_FAILED;
00279 }
00280 }else{
00281 shaderSaveResult |= SHADER_SAVE_FAILED;
00282 }
00283 }else{
00284
00285 QString openNew = ShowOpenDialog();
00286 if(pCSFile->load(openNew)){
00287 shaderLoadResult = SHADER_LOAD_SUCCESS;
00288 ui.currentFile_label->setText(pCSFile->GetFileName());
00289 }else{
00290 shaderLoadResult |= SHADER_LOAD_FAILED;
00291 }
00292 }
00293 }else{
00294
00295 QString openNew = ShowOpenDialog();
00296 if(pCSFile->load(openNew)){
00297 shaderLoadResult = SHADER_LOAD_SUCCESS;
00298 ui.currentFile_label->setText(pCSFile->GetFileName());
00299 }else{
00300 shaderLoadResult |= SHADER_LOAD_FAILED;
00301 }
00302 }
00303 }
00304
00305 logShaderSaveResult();
00306 logShaderLoadResult();
00307 return shaderLoadResult == SHADER_LOAD_SUCCESS;
00308 }
00309
00310
00311 QString CShaderEditor::ShowOpenDialog()
00312 {
00313 pOpenSaveDialog->setAcceptMode(QFileDialog::AcceptOpen);
00314 pOpenSaveDialog->setFileMode(QFileDialog::ExistingFile);
00315 if(pCSFile->GetShaderType()== FRAGMENT_SHADER){
00316 pOpenSaveDialog->setFilter("*.fs");
00317 pOpenSaveDialog->setDefaultSuffix("fs");
00318 pOpenSaveDialog->setWindowTitle("Open Fragment Shader");
00319 }else{
00320 pOpenSaveDialog->setFilter("*.vs");
00321 pOpenSaveDialog->setDefaultSuffix("vs");
00322 pOpenSaveDialog->setWindowTitle("Open Vertex Shader");
00323 }
00324 pOpenSaveDialog->setDirectory(lastShaderFilePath);
00325
00326 pOpenSaveDialog->exec();
00327
00328 if(pOpenSaveDialog->result() == QDialog::Accepted){
00329 QStringList list = pOpenSaveDialog->selectedFiles();
00330 if(list.isEmpty()){
00331 return "";
00332 }else{
00333 lastShaderFilePath = QFileInfo(list.first()).filePath();
00334 return QString().append(list.first());
00335 }
00336 }else{
00337 shaderLoadResult = SHADER_LOAD_ABORTED;
00338 return "";
00339 }
00340 }
00341
00342
00343 QString CShaderEditor::ShowSaveDialog()
00344 {
00345 pOpenSaveDialog->setAcceptMode(QFileDialog::AcceptSave);
00346 pOpenSaveDialog->setFileMode(QFileDialog::AnyFile);
00347 if(pCSFile->GetShaderType() == FRAGMENT_SHADER){
00348 pOpenSaveDialog->setFilters(QStringList("*.fs") << "*.*" );
00349 pOpenSaveDialog->setDefaultSuffix("fs");
00350 pOpenSaveDialog->setWindowTitle("Save Fragment Shader");
00351 }else{
00352 pOpenSaveDialog->setFilters(QStringList("*.vs") << "*.*" );
00353 pOpenSaveDialog->setDefaultSuffix("vs");
00354 pOpenSaveDialog->setWindowTitle("Save Vertex Shader");
00355 }
00356 pOpenSaveDialog->setDirectory(lastShaderFilePath);
00357
00358 pOpenSaveDialog->exec();
00359
00360 if(pOpenSaveDialog->result() == QDialog::Accepted){
00361 QStringList list = pOpenSaveDialog->selectedFiles();
00362 if(list.isEmpty()){
00363 return "";
00364 }else{
00365 lastShaderFilePath = QFileInfo(list.first()).filePath();
00366 return QString().append(list.first());
00367 }
00368 }else{
00369 shaderSaveResult = SHADER_SAVE_ABORTED;
00370 return "";
00371 }
00372 }
00373
00374
00375 bool CShaderEditor::ShowQuestionDialog(){
00376 pQuestionSave->setText("File : " + pCSFile->GetFileName() + " has been modified, Save changes?");
00377 return pQuestionSave->exec()==QMessageBox::Yes;
00378 }
00379
00380
00381 bool CShaderEditor::LoadShader(QString & fileName)
00382 {
00383 shaderLoadResult = SHADER_LOAD_FAILED;
00384 shaderSaveResult = SHADER_SAVE_NOSAVE;
00385
00386
00387 if(!pCSFile->isUpToDate()){
00388
00389 if(ShowQuestionDialog()){
00390
00391 QString saveAs = ShowSaveDialog();
00392 if(pCSFile->saveAs(saveAs)){
00393 shaderSaveResult = SHADER_SAVE_SUCCESS;
00394 ui.currentFile_label->setText(pCSFile->GetFileName());
00395
00396 if(pCSFile->load(fileName)){
00397 shaderLoadResult = SHADER_LOAD_SUCCESS;
00398 ui.currentFile_label->setText(pCSFile->GetFileName());
00399 }else{
00400 shaderLoadResult = SHADER_LOAD_FAILED;
00401 }
00402 }else{
00403 shaderSaveResult |= SHADER_SAVE_FAILED;
00404 }
00405 }else{
00406
00407 if(pCSFile->load(fileName)){
00408 shaderLoadResult = SHADER_LOAD_SUCCESS;
00409 ui.currentFile_label->setText(pCSFile->GetFileName());
00410 }else{
00411 shaderLoadResult = SHADER_LOAD_FAILED;
00412 }
00413 }
00414 }else{
00415
00416 if(pCSFile->load(fileName)){
00417 shaderLoadResult = SHADER_LOAD_SUCCESS;
00418 ui.currentFile_label->setText(pCSFile->GetFileName());
00419 }else{
00420 shaderLoadResult = SHADER_LOAD_FAILED;
00421 }
00422 }
00423
00424 logShaderSaveResult();
00425 logShaderLoadResult();
00426 return shaderLoadResult == SHADER_LOAD_SUCCESS;
00427 }
00428
00429
00430 bool CShaderEditor::SaveShader()
00431 {
00432 QString saveAs = ShowSaveDialog();
00433 if(pCSFile->saveAs(saveAs)){
00434 shaderSaveResult = SHADER_SAVE_SUCCESS;
00435 ui.currentFile_label->setText(pCSFile->GetFileName());
00436 ui.tabWidget->setTabText(pCSFile->GetTabID(),QFileInfo(pCSFile->GetFileName()).fileName());
00437 }else{
00438 shaderSaveResult |= SHADER_SAVE_FAILED;
00439 }
00440 logShaderSaveResult();
00441 return shaderSaveResult == SHADER_SAVE_SUCCESS;
00442 }
00443
00444
00445 QString CShaderEditor::Validate(QString & shaderText)
00446 {
00447 QString workDirPath = QFileInfo(validatorPath).path();
00448
00449
00450 QProcess proc(this);
00451 proc.setWorkingDirectory(workDirPath);
00452
00453
00454 QFile tmpFile(workDirPath + QString("/tmp_") + QFileInfo(pCSFile->GetFileName()).fileName());
00455 if(tmpFile.open(QIODevice::WriteOnly)){
00456 tmpFile.write(shaderText.toAscii());
00457 tmpFile.close();
00458 }
00459
00460
00461 QString args;
00462 if(pCSFile->GetShaderType() == VERTEX_SHADER){
00463 args = " /v ";
00464 }else{
00465 args = " /f ";
00466 }
00467
00468 proc.start(validatorPath + args + QFileInfo(tmpFile).fileName(),QIODevice::ReadOnly);
00469
00470 bool finished = false;
00471 while(!(finished = proc.waitForFinished(100))){
00472 qApp->processEvents(QEventLoop::AllEvents);
00473 }
00474 tmpFile.remove();
00475
00476 QFile output;
00477 if(pCSFile->GetShaderType() == VERTEX_SHADER){
00478 output.setFileName(workDirPath.append("/vertex.log"));
00479 }else{
00480 output.setFileName(workDirPath.append("/fragment.log"));
00481 }
00482
00483 if(output.open(QIODevice::ReadOnly)){
00484 QString result(output.readAll());
00485 if(result.contains("Success",Qt::CaseInsensitive)){
00486 return QString("Success");
00487 }else{
00488 return result;
00489 }
00490 }else{
00491 return "Failed";
00492 }
00493 output.close();
00494 }
00495
00496
00497 void CShaderEditor::EditInEditor()
00498 {
00499
00500 pCSFile->setModified(false);
00501
00502
00503 editStringSent = pCSFile->toPlainText();
00504
00505
00506 if(pCSFile->StartEdit(editorPath,editorArgs)){
00507 SetFileEdit(false);
00508 }
00509 }
00510
00511
00512 void CShaderEditor::Fetch()
00513 {
00514 QString *editStringReceived;
00515
00516
00517 if(!pCSFile->isUpToDate()){
00518
00519 if(ShowQuestionDialog()){
00520
00521 if(SaveShader()){
00522
00523 editStringReceived = &pCSFile->StopEdit();
00524 }else{
00525
00526 }
00527 }else{
00528
00529 editStringReceived = &pCSFile->StopEdit();
00530 }
00531 }else{
00532
00533 editStringReceived = &pCSFile->StopEdit();
00534 }
00535
00536 if(editStringReceived->compare(editStringSent)!=0){
00537 SetFileEdit(true);
00538 pCSFile->setPlainText(*editStringReceived);
00539 }else{
00540
00541 }
00542 }
00543
00544
00545 void CShaderEditor::Attach()
00546 {
00547
00548 std::string shaderString = pCSFile->toPlainText().toStdString();
00549 const char * shaderCString = shaderString.c_str();
00550
00551 int nmbOfchars = shaderString.size();
00552 int nmbOfLines = 1;
00553
00554 char * shaderSource = new char [nmbOfchars+1];
00555 memcpy(shaderSource,shaderCString,nmbOfchars);
00556 shaderSource[nmbOfchars] = '\0';
00557
00558
00559 unsigned int shaderID = pCSFile->GetShaderID();
00560 if(shaderID == 0){
00561 int shaderType = pCSFile->GetShaderType();
00562 pCSFile->SetShaderID(pControl->GetProgram()->CreateShader(shaderType));
00563 }
00564
00565
00566 if(!pCSFile->GetAttached()){
00567 pControl->GetProgram()->AttachShader(pCSFile->GetShaderID());
00568 pCSFile->SetAttached(true);
00569 }
00570
00571
00572 char * cResult;
00573 cResult = pControl->GetProgram()->Compile(pCSFile->GetShaderID(),(const char **)&shaderSource,nmbOfLines);
00574
00575
00576 delete [] shaderSource;
00577
00578
00579 if(cResult != NULL){
00580 QString action("Compile Result: ");action.append(pCSFile->GetFileName());
00581 AppendOutput(action,QString(cResult).trimmed());
00582 pCSFile->SetCompiled(false);
00583 }else{
00584 QString action("Compile Result: ");action.append(pCSFile->GetFileName());
00585 AppendOutput(action,QString("Success"));
00586 pCSFile->SetCompiled(true);
00587 }
00588
00589
00590 char * lResult = pControl->GetProgram()->Link();
00591
00592 if(lResult != NULL){
00593 QString action("Link Result: ");action.append(pCSFile->GetFileName());
00594 AppendOutput(action,QString(lResult).trimmed());
00595 }else{
00596 QString action("Link Result: ");action.append(pCSFile->GetFileName());
00597 AppendOutput(action,QString("Success"));
00598 }
00599 SetShaderStatus();
00600 }
00601
00602
00603 void CShaderEditor::Detach()
00604 {
00605 if(pCSFile->GetShaderID()!=0){
00606
00607 if(pCSFile->GetAttached()){
00608
00609
00610 pControl->GetProgram()->Detach(pCSFile->GetShaderID());
00611 pCSFile->SetAttached(false);
00612 }
00613
00614
00615 char * lResult = pControl->GetProgram()->Link();
00616
00617 if(lResult != NULL){
00618 QString action("Link Result: ");action.append(pCSFile->GetFileName());
00619 AppendOutput(action,QString(lResult).trimmed());
00620 }else{
00621 QString action("Link Result: ");action.append(pCSFile->GetFileName());
00622 AppendOutput(action,QString("Success"));
00623 }
00624 }else{
00625
00626 }
00627
00628 SetShaderStatus();
00629 }
00630
00631
00632 void CShaderEditor::Close()
00633 {
00634
00635 if(!pCSFile->isUpToDate()){
00636
00637 if(ShowQuestionDialog()){
00638
00639 QString saveAs = ShowSaveDialog();
00640 if(pCSFile->saveAs(saveAs)){
00641 shaderSaveResult = SHADER_SAVE_SUCCESS;
00642 }else{
00643 shaderSaveResult |= SHADER_SAVE_FAILED;
00644 }
00645 }
00646 }
00647
00648 logShaderSaveResult();
00649
00650
00651 if(pCSFile->GetAttached()){
00652 Detach();
00653 }
00654
00655
00656 int removeID = pCSFile->GetTabID();
00657 for(int i = removeID+1 ; i < shaderList.size() ; i++){
00658 shaderList[i]->SetTabID(i-1);
00659 }
00660
00661 shaderList.removeAt(removeID);
00662 ui.tabWidget->removeTab(removeID);
00663
00664 if(removeID == 0 && !shaderList.isEmpty()){
00665 on_tabWidget_currentChanged(0);
00666 }else{
00667 on_tabWidget_currentChanged(ui.tabWidget->currentIndex());
00668 }
00669 }
00670
00671
00672
00673
00674 void CShaderEditor::ResetUI()
00675 {
00676 ui.close_btn->setEnabled(false);
00677 ui.edit_btn->setEnabled(false);
00678 ui.save_btn->setEnabled(false);
00679 ui.validate_btn->setEnabled(false);
00680 ui.attach_btn->setEnabled(false);
00681 ui.detach_btn->setEnabled(false);
00682 ui.currentFile_label->setText("");
00683 }
00684
00685
00686 void CShaderEditor::SetFileEdit(bool enable)
00687 {
00688 ui.edit_btn->setEnabled(enable);
00689 ui.fetch_btn->setEnabled(!enable);
00690 }
00691
00692
00693 void CShaderEditor::SetShaderStatus()
00694 {
00695 if(pCSFile->GetAttached()){
00696 if(pCSFile->GetCompiled()){
00697 ui.tabWidget->setTabIcon(pCSFile->GetTabID(),QIcon(QString::fromUtf8(":/VStudio/20x20/compiled.png")));
00698 }else{
00699 ui.tabWidget->setTabIcon(pCSFile->GetTabID(),QIcon(QString::fromUtf8(":/VStudio/20x20/notCompiled.png")));
00700 }
00701 }else{
00702 if(pCSFile->GetShaderType()==VERTEX_SHADER){
00703 ui.tabWidget->setTabIcon(pCSFile->GetTabID(),QIcon(QString::fromUtf8(":/VStudio/20x20/vNotAttached.png")));
00704 }else{
00705 ui.tabWidget->setTabIcon(pCSFile->GetTabID(),QIcon(QString::fromUtf8(":/VStudio/20x20/fNotAttached.png")));
00706 }
00707 }
00708 }
00709
00710
00711 void CShaderEditor::AppendOutput(QString & action,QString &content)
00712 {
00713 ui.output->setTextColor(QColor("blue"));
00714 ui.output->append(action);
00715 ui.output->setTextColor(QColor("black"));
00716 ui.output->append(content);
00717 ui.output->ensureCursorVisible();
00718 }
00719
00720
00721 void CShaderEditor::AddShaderTab(CShaderFile *file)
00722 {
00723 QTextEdit *textEdit = new QTextEdit();
00724 textEdit->setDocument(file);
00725 textEdit->setAcceptRichText(false);
00726 textEdit->setLineWrapMode(QTextEdit::NoWrap);
00727 textEdit->setTabStopWidth(30);
00728 textEdit->setTabChangesFocus(false);
00729 connect(file,SIGNAL(contentsChanged()),this,SLOT(FileModified()));
00730
00731 ui.tabWidget->addTab(textEdit,QFileInfo(file->GetFileName()).fileName());
00732 shaderList.append(file);
00733 file->SetTabID(shaderList.size()-1);
00734
00735 ui.tabWidget->setCurrentIndex(file->GetTabID());
00736 on_tabWidget_currentChanged(file->GetTabID());
00737
00738 SetShaderStatus();
00739 SetFileEdit(true);
00740
00741 ui.save_btn->setEnabled(true);
00742 ui.close_btn->setEnabled(true);
00743 ui.validate_btn->setEnabled(true);
00744 ui.attach_btn->setEnabled(true);
00745 ui.detach_btn->setEnabled(true);
00746 }
00747
00748
00749 void CShaderEditor::logShaderLoadResult()
00750 {
00751 switch(shaderLoadResult){
00752 case SHADER_LOAD_SUCCESS:
00753 pControl->GetConsole().Log(QString("Loading Shader: " + pCSFile->GetFileName() + " Success!"),MAIN_THREAD);
00754 break;
00755 case SHADER_LOAD_FAILED:
00756 pControl->GetConsole().Log(QString("Loading Shader: " + pCSFile->GetFileName() + " Failed!"),MAIN_THREAD);
00757 break;
00758 case SHADER_LOAD_ABORTED:
00759 pControl->GetConsole().Log(QString("Loading Shader: aborted"),MAIN_THREAD);
00760 }
00761 }
00762
00763
00764 void CShaderEditor::logShaderSaveResult()
00765 {
00766 switch(shaderSaveResult){
00767 case SHADER_SAVE_SUCCESS:
00768 pControl->GetConsole().Log(QString("Saving Shader: " + pCSFile->GetFileName() + " Success!"),MAIN_THREAD);
00769 break;
00770 case SHADER_SAVE_FAILED:
00771 pControl->GetConsole().Log(QString("Saving Shader: " + pCSFile->GetFileName() + " Failed!"),MAIN_THREAD);
00772 break;
00773 case SHADER_SAVE_ABORTED:
00774 pControl->GetConsole().Log(QString("Saving Shader: Aborted!"),MAIN_THREAD);
00775 }
00776 }
00777
00778
00779
00780
00781
00782
00783 void CShaderEditor::FileModified()
00784 {
00785 ui.currentFile_label->setText(pCSFile->GetFileName() + " (*)");
00786 }
00787
00788
00789 void CShaderEditor::on_tabWidget_currentChanged(int index)
00790 {
00791 if(index >= 0){
00792
00793
00794 pCSFile = shaderList[index];
00795
00796
00797 SetFileEdit(!shaderList[index]->isEditing());
00798
00799
00800 if(!pCSFile->isUpToDate()){
00801 FileModified();
00802 }else{
00803 ui.currentFile_label->setText(pCSFile->GetFileName());
00804 }
00805
00806 }else{
00807
00808
00809 ResetUI();
00810 }
00811 }
00812
00813
00814 void CShaderEditor::on_new_Vertex_released()
00815 {
00816 CShaderFile * newShader = new CShaderFile(this);
00817 newShader->Create(VERTEX_SHADER);
00818 AddShaderTab(newShader);
00819 }
00820
00821
00822 void CShaderEditor::on_new_fragment_released()
00823 {
00824 CShaderFile * newShader = new CShaderFile(this);
00825 newShader->Create(FRAGMENT_SHADER);
00826 AddShaderTab(newShader);
00827 }
00828
00829
00830 void CShaderEditor::on_open_vertex_released()
00831 {
00832 CShaderFile * newShader = new CShaderFile(this);
00833 newShader->Create(VERTEX_SHADER);
00834 pCSFile = newShader;
00835 if(OpenShader()){
00836 AddShaderTab(newShader);
00837 }else{
00838 delete newShader;
00839 }
00840 }
00841
00842
00843 void CShaderEditor::on_open_fragment_released()
00844 {
00845 CShaderFile * newShader = new CShaderFile(this);
00846 newShader->Create(FRAGMENT_SHADER);
00847 pCSFile = newShader;
00848 if(OpenShader()){
00849 AddShaderTab(newShader);
00850 }else{
00851 delete newShader;
00852 }
00853 }
00854
00855
00856 void CShaderEditor::on_save_btn_released()
00857 {
00858 SaveShader();
00859 }
00860
00861
00862 void CShaderEditor::on_validate_btn_released()
00863 {
00864 QString action("Validate: ");action.append(pCSFile->GetFileName());
00865 QString result = Validate(pCSFile->toPlainText());
00866 AppendOutput(action,result);
00867 }
00868
00869
00870 void CShaderEditor::on_edit_btn_released()
00871 {
00872 EditInEditor();
00873 }
00874
00875
00876 void CShaderEditor::on_fetch_btn_released()
00877 {
00878 Fetch();
00879 }
00880
00881
00882 void CShaderEditor::on_attach_btn_released()
00883 {
00884 Attach();
00885 }
00886
00887
00888 void CShaderEditor::on_detach_btn_released()
00889 {
00890 Detach();
00891 }
00892
00893
00894 void CShaderEditor::on_close_btn_clicked()
00895 {
00896 Close();
00897 }