void MainWindow::onMenuClicked(QAction* e){ int action = e->property("action").toInt(); switch (action) { case 0://打开 try{ QString openPath = QFileDialog::getOpenFileName( this,UTF8("打开文件"),QString(),QString("*.css") ); if(openPath.length()) OpenFile(openPath); }catch(AlertException e){ e.Alert(); } break; case 1://保存 if( CurrentFilePath.length() ){ SaveFile(CurrentFilePath); break; } //no break; case 2://另存为 { QString savePath = QFileDialog::getSaveFileName( this,UTF8("打开文件"),QString(),QString("*.css") ); if(savePath.length()){ if(!savePath.endsWith(QString(".css"))) savePath += QString(".css"); SaveFile(savePath); } } break; case 3://退出 this->close(); break; case 100://添加图形 { QWidget* wnd = static_cast<QWidget*>(CreateObject("Image")); if(!wnd) throw AlertException("程序错误","无法实例化Image"); wm->AddWindow(wnd); } break; case 101://添加区域 { QWidget* wnd = static_cast<QWidget*>(CreateObject("Area")); if(!wnd) throw AlertException("程序错误","无法实例化Area"); wm->AddWindow(wnd); } break; case 200://图像设定 CurrentSetting->show(); break; case 201://导出PNG { QString savePath = QFileDialog::getSaveFileName( this,UTF8("导出图片"),QString(),QString("*.png") ); if(savePath.length()){ CurrentSetting->setProperty("lastpng",savePath);//记住上次导出位置 QImage image = wm->Screenshot(); image.save(savePath); setStatusTip(UTF8("导出PNG图像:")+savePath); } } break; case 202://导出PNG到上次的目标 { QString savePath = CurrentSetting->property("lastpng").toString(); if( savePath.length() ){ QImage image = wm->Screenshot(); image.save(savePath); setStatusTip(UTF8("导出到上次目标:")+savePath); }else{ throw new AlertException("错误","没有上次记录,请点击导出图片"); } } break; case 203://导出CSS到自身 if( CurrentFilePath.length() ){ ExportCss(CurrentFilePath); break; } // NO break; case 204://导出CSS { QString exportPath = QFileDialog::getSaveFileName( this,UTF8("打开文件"),QString(),QString("*.css") ); if(exportPath.length()){ ExportCss(exportPath); } } break; case 205://导出CSS并显示 { static dlgTextWindow txtwnd(0); txtwnd.setText(ExportCss()); txtwnd.show(); } break; case 300://打开右边栏 { static bool isShow = false; if(isShow){ ui->frmProperty->show(); }else{ ui->frmProperty->hide(); } isShow = !isShow; } break; default: if(action>=1000){ wm->setCurrentWindow(action-1000); }else{ qWarning()<<"Unknown Action "<<action; } break; } }
void MainWindow::OpenFile(const QString& file){ setStatusTip(UTF8("打开文件:")+file); bool test_ok = false; QFile fp(file); test_ok = fp.exists(); if(!test_ok) throw AlertException(UTF8("打开失败"),UTF8("文件不存在:\n")+file); test_ok = fp.open(QFile::ReadOnly); if(!test_ok) throw AlertException(UTF8("打开失败"),UTF8("无法读取文件:\n")+file); QTextStream CurrentFile(&fp); //读取文件中的配置 QString Line; QString OData; QTextStream ProjectData(&OData); bool switcher = false; while( !CurrentFile.atEnd() ){ Line = CurrentFile.readLine(); if( !switcher ){ //非 配置or导出 部分 if(Line.startsWith(StartIndent) || Line.startsWith(AutoGenIndent)){ switcher = true; continue; } }else{ //配置or导出 部分 switcher = !(Line.startsWith(EndIndent) || Line.startsWith(AutoGenEndIndent)); ProjectData<<Line<<endl; } } bool start = false; while(!ProjectData.atEnd()){ QString Line = ProjectData.readLine(); if( Line == "[GLOBAL]" ){ start = true; continue; } if( Line == "[END]" ){ start = false; continue; } if(!start) continue; Line = Line.trimmed(); QString ID = Line.section(' ',0,0).toLower(); ID.truncate(ID.length()-1); QString Value = multiLine( Line.section(' ',1) ); CurrentSetting->setProperty(ID.toStdString().data(),Value); } if(!test_ok) throw AlertException(UTF8("打开失败"),UTF8("无法在文件中识别本程序的格式")); ProjectData.reset(); wm->restart(); //移动当前目录 QString stuff_dir = file + STUFF_FOLDER; QDir stuff(stuff_dir); if( !stuff.exists() ){ stuff.mkdir(stuff_dir); } stuff.setCurrent(stuff_dir); qDebug()<<"[OPEN FILE]Current dir is "<<QDir::currentPath(); wm->SessionResume(OData); CurrentFilePath = file; this->setWindowTitle(file+QString(" - cssSprites")); }
void MainWindow::SaveFile(const QString& file){ setStatusTip(UTF8("保存当前进度到:")+file); QFile fp(file); fp.open(QFile::ReadOnly); QTextStream CurrentFile(&fp); QString OData; QTextStream SaveingFile(&OData); SaveingFile.setCodec("UTF-8"); SaveingFile << StartIndent <<endl; SaveingFile <<endl; SaveingFile<< "[GLOBAL]" <<endl; SaveingFile<<"\tSCREENHEIGHT: "<<singleLine( CurrentSetting->property("screenheight").toString() )<<endl; SaveingFile<<"\tSCREENWIDTH: "<<singleLine( CurrentSetting->property("screenwidth").toString() )<<endl; SaveingFile<<"\tURL: "<<singleLine( CurrentSetting->property("url").toString() )<<endl; SaveingFile<<"\tTEMPLATE: "<<singleLine( CurrentSetting->property("template").toString() )<<endl; SaveingFile<<"\tNAMESPACE: "<<singleLine( CurrentSetting->property("namespace").toString() )<<endl; SaveingFile<<"\tBGSELECTOR: "<<singleLine( CurrentSetting->property("bgselector").toString() )<<endl; SaveingFile<<"\tGENERATEBGIMAGE: "<<singleLine( CurrentSetting->property("generatebgimage").toString() )<<endl; SaveingFile<<"\tLASTPNG: "<<singleLine( CurrentSetting->property("lastpng").toString() )<<endl; SaveingFile<<"[END]"<<endl; SaveingFile <<endl; SaveingFile << wm->SessionSave(); SaveingFile << endl << EndIndent <<endl; //读取文件中的其他部分 QString Line; bool switcher = false; while( !CurrentFile.atEnd() ){ Line = CurrentFile.readLine(); if( !switcher ){ //非 配置or导出 部分 if(Line.startsWith(StartIndent) || Line.startsWith(AutoGenIndent)){ switcher = true; continue; } SaveingFile<<Line<<endl; }else{ //配置or导出 部分 switcher = !(Line.startsWith(EndIndent) || Line.startsWith(AutoGenEndIndent)); } } fp.close(); if(!fp.open(QFile::WriteOnly)) throw AlertException(UTF8("保存失败"),UTF8("无法写入文件")); CurrentFile.reset(); CurrentFilePath = file; this->setWindowTitle(file+QString(" - cssSprites")); qDebug()<<"File Save"<<file; CurrentFile<<OData; fp.close(); QDir fsd; QString target = fsd.absoluteFilePath(CurrentFilePath); target += STUFF_FOLDER; if(fsd.currentPath() != target){ //转移文件 fsd.setPath(target); if( !fsd.exists() ){ if(! fsd.mkdir(target) ) throw AlertException(UTF8("保存错误"),("目标路径无法写入(请检查权限)\n")+target); } QStringList stuff = QDir::current().entryList(QDir::Files); QString fName; foreach(fName,stuff){ qWarning(QString("Copy File ================\n\t\tfrom %1(%2)\n\t\t to %3").arg(fName).arg(QDir::currentPath()).arg(target+fName).toStdString().data()); QFile::copy(fName,target+fName); }
void checkAlert() throw(AlertException) { if (alerted_) { throw AlertException(""); } }