mainwindow.cc

来自「c++的guiQt做的开发」· CC 代码 · 共 1,250 行 · 第 1/3 页

CC
1,250
字号
 @param useBareFormat Use bare format without descriptions*/QStringList MainWindow::filters(bool useGeneric/*=true*/,bool useBareFormat/*=false*/) { /** \todo only supported formats */ QStringList out; out << "ANALYZE7.5/NIFTI (*.hdr *.nii)"; out << "ASCII (*.asc)"; out << "BMP (*.bmp)"; out << "CImg RAW (*.cimg)"; out << "DLM - Matlab ASCII (*.dlm *.txt)"; out << "Dicom (*.dcm *.dicom)"; out << "GIF (*.gif)"; out << "INRIMAGE-4 (*.inr)"; out << "JPEG (*.jpg *.jpeg)"; out << "PANDORE-5 (*.pan)"; out << "PAR-REC - Philips (*.par *.rec)"; out << "PCX (*.pcx)"; out << "PNG (*.png)"; out << "PNM - Portable Pixmap (*.ppm *.pgm *.pnm *.pbm)"; out << "TARGA (*.tga)"; out << "TIFF (*.tif)"; if (useGeneric) {  QRegExp r("\\((.*)\\)");  QString allMask;  QStringList allMasks;  for (int i=0;i<out.size();i++) {   if (r.lastIndexIn(out[i])) allMasks << r.cap(1);  }  allMask=tr("All images")+" ("+allMasks.join(" ")+")";  //Fallback :)  out = QStringList(allMask) + out + QStringList(tr("All files")+" (*)"); } if (useBareFormat) {  QRegExp r("\\((.*)\\)");  int sz=out.size();  for (int i=0;i<sz;i++) {   if (r.lastIndexIn(out[i])) out[i]=r.cap(1);   QStringList l=out[i].split(" ");   if (l.size()>1) {    //Multiple filemasks separated by spaces: separate into extra filters    out[i]=l[0];    for (int j=1;j<l.size();j++) {     //Add new filters to end     out << l[j];    }   }  } } return out;}/** Open image with given name Parameter specifies name of image If no parameter is specified, dialog is invoked to ask for one @param param Command parameters*/void MainWindow::load(const QStringList &param) { QString fileName; if (param.count()<1) {  //No parameters  fileName=openFileDialog(this,tr("Open Image"),"loadimage",filters(),"loadimagepath"); } else {  fileName=param[0]; } if (fileName.isNull()) return; //Dialog cancelled openFile(fileName);}/** Save image under given name Parameter specifies name of image. If name is given in parameter and not with dialog, no questions about overwriting will be asked. If no parameter is specified, dialog is invoked to ask for one @param param Command parameters*/void MainWindow::save(const QStringList &param) { QString fileName; Image *i=getImage(); if (!i) return; if (param.count()<1) {  //No parameters  fileName=saveFileDialog(this,i->name(),true,tr("Save Image"),"saveimage",filters(),"saveimagepath"); } else {  fileName=param[0]; } if (fileName.isNull()) return; //Dialog cancelled main->saveImage(fileName); setTitle(); postOp(i,false);}/** Convert image to specified number of channels Parameter specifies angle in degrees If no parameter is specified, 90 degrees are assumed @param param Command parameters*/void MainWindow::set_channels(const QStringList &param) { Image *i=getImageAndParams(param); if (!i) return; int c=getParamInt("channels"); if (c<=0) return; i->convertChannels(c); postOp(i);}/** Rotate image by arbitrary angle Parameter specifies angle in degrees If no parameter is specified, 90 degrees are assumed @param param Command parameters*/void MainWindow::rotate(const QStringList &param) { Image *i=getImageAndParams(param); if (!i) return; float angle=getParamFloat("angle"); i->rotate(angle); postOp(i);}/** Function to call after performing an operation. Redraw the image and handle any errors @param i Image that was processed @param doUpdate If true, update image on screen.                 In some operations it may be unnecessary to update, like if the image was not modified,                 or if the update is done in other way. Default is to update*/void MainWindow::postOp(Image *i,bool doUpdate/*=true*/) { main->cancelRect();//Will ensure that updateMenus() will be called in the process if (doUpdate) main->update(); QString infoText; if (i) {  QString err=i->error();  if (!err.isNull()) {   cmdLine->addError(tr("Error during image operation"));   cmdLine->addError(err);  }  infoText=QString::number(i->x())+"x"+QString::number(i->y())+" ";  infoText+=main->channelsToString(i->channels()); } else {  infoText=tr("No image"); } status->receiveInfoText(infoText);}/** Perform an undo (if enabled)*/void MainWindow::undo() { Image *i=getImage(); if (!i) return; i->undo(); postOp(i);}/** Flip image by specified axis If no parameter is specified, "h" is assumed @param param Command parameters*/void MainWindow::flip(const QStringList &param) { Image *i=getImage(); if (!i) return; char axis='h';; if (param.count()>=1) {  QString ax=param[0].toLower();  if (ax.length()>0) axis=ax[0].toAscii(); } if (axis=='h') axis='x'; if (axis=='v') axis='y'; if (axis=='c') axis='v'; if (axis=='w') axis='v'; i->flip(axis); postOp(i);}/** Convert image to greyscale*/void MainWindow::greyscale() { Image *i=getImage(); if (!i) return; i->toGrey(); postOp(i);}/** If all parameters are valid for given function, set them in globalParams and return true. Otherwise return false and print any error messages to console @param funcName name of function @param param parameters to check*/bool MainWindow::getParamsForFunction(const QString &funcName,const QStringList &param) { int needp=minParamCount(funcName); if (param.size()<needp) {  minParam(needp,funcName);  return false; } //Ok, convert params to map paramsToMap(funcName,param); return true;}/** If all parameters are valid for currently invoked function, set them in globalParams and return true. Otherwise return false and print any error messages to console @param param parameters to check*/bool MainWindow::getParams(const QStringList &param) { return getParamsForFunction(currentFunction(),param);}/** If all parameters are valid for given function, and image is loaded, return image. Otherwise return NULL and print any error messages to console @param param parameters to check*/Image* MainWindow::getImageAndParams(const QStringList &param) { if(!getParams(param)) return NULL;  //Get image and return it Image *i=getImage(); return i;}/** Signal received when zoom level is changed and also when image is loaded @param zoom new zoom level*/void MainWindow::zoomChanged(int zoom) { status->receiveZoomText(QString::number(zoom)+"%");}/** Resize image @param param Command parameters*/void MainWindow::resize(const QStringList &param) { Image *i=getImageAndParams(param); if (!i) return; int x=getParamScaleInt("x"); int y=getParamScaleInt("y"); int interp=getParamInt("int"); int border=getParamInt("border"); i->resize(x,y,interp,border); postOp(i);}/** Crop image @param param Command parameters*/void MainWindow::crop(const QStringList &param) { Image *i=getImageAndParams(param); if (!i) return; int x1=getParamScaleInt("x1"); int y1=getParamScaleInt("y1"); int x2=getParamScaleInt("x2"); int y2=getParamScaleInt("y2"); //Negative values are to be treated as percentages if (x1<0) x1=-x1*i->x()/100; if (y1<0) y1=-y1*i->y()/100; if (x2<0) x2=-x2*i->x()/100; if (y2<0) y2=-y2*i->y()/100; //Out of image values will be cropped x1=min(x1,i->x()-1); y1=min(y1,i->y()-1); x2=min(x2,i->x()-1); y2=min(y2,i->y()-1); //Swap coords if necessary (x1,y1 must be top-left corner) if (x1>x2) swap(x1,x2); if (y1>y2) swap(y1,y2); i->crop(x1,y1,x2,y2); postOp(i);}/** Blur image @param param Command parameters*/void MainWindow::blur(const QStringList &param) { Image *i=getImageAndParams(param); if (!i) return; int sig=getParamScaleInt("sigma"); int cond=getParamScaleInt("cond"); i->blur(sig,cond); postOp(i);}/** Blur image anisotropically @param param Command parameters*/void MainWindow::blur_a(const QStringList &param) { Image *i=getImageAndParams(param); if (!i) return; int sigx=getParamScaleInt("sigmax"); int sigy=getParamScaleInt("sigmay"); int cond=getParamScaleInt("cond"); i->blur(sigx,sigy,0,cond); postOp(i);}/** Set zoom level @param param Command parameters*/void MainWindow::zoom(const QStringList &param) { Image *i=getImageAndParams(param); if (!i) return; main->setZoom(getParamInt("zoomlevel"));}/** Set zoom level N steps in/out @param param Command parameters*/void MainWindow::zoomStep(const QStringList &param) { Image *i=getImageAndParams(param); if (!i) return; main->zoomSteps(getParamInt("steps"));}/** Error so serious, that it deserves a modal dialog @param text text of the error*/void MainWindow::seriousError(const QString &text) { cmdLine->addError(text); QMessageBox::warning(this,QObject::tr("Warning"),text);}void MainWindow::deleteImage(const QStringList &param) { if(!getParams(param)) return;  Image *i=main->getImage(); QString fileName=i->absoluteName();  if (globalSettings->readBool("view/confirm_delete")) {  QMessageBox mb(this);  mb.setText(tr("Do you want to delete file %1?").arg(fileName));  QAbstractButton *rm=mb.addButton(tr("Delete"),QMessageBox::YesRole);  mb.setEscapeButton(mb.addButton(tr("Cancel"),QMessageBox::RejectRole));  mb.setWindowTitle(tr("Delete file?"));  mb.setIcon(QMessageBox::Question);  mb.exec();  if (mb.clickedButton()!=rm) return; } if (QFile::remove(fileName)) {  cmdLine->addString(tr("Deleted image: %1").arg(fileName)); } else {  cmdLine->addError(tr("Unable to delete image %1").arg(fileName)); } fileSwitch(getParamInt("offset"));}/** Switch fo first/next/previous/last file @param param Command parameters*/void MainWindow::fileSwitch(const QStringList &param) { if(!getParams(param)) return;  fileSwitch(getParamInt("offset"));}/** Switch fo first/next/previous/last file @param offset Offset to move*/void MainWindow::fileSwitch(int offset) { //Get image and return it Image *i=main->getImage(); if (offset==0) return;//No move QString fileName; QString baseName; QDir dir; if (i) {  //Start from image  fileName=i->absoluteName();  baseName=i->baseName();  dir=QFileInfo(fileName).absoluteDir(); } else {  //Start from "start of directory"  fileName="";  baseName="";  dir=QDir::current(); } if (!dir.exists()) {  //Someone removed the dir in which the file was  seriousError(tr("Cannot move to next or previous file")+" - "+tr("directory was removed"));  return; } QStringList files=dir.entryList(filters(false,true),QDir::Files | QDir::Readable | QDir::Hidden,QDir::Name | QDir::IgnoreCase); int nFiles=files.size(); if (!nFiles) {  //Dir is empty. Original file is also gone  seriousError(tr("Cannot move to next or previous file")+" - "+tr("no files in directory"));  return; } SMap filesSeq; QString baseKey=baseName.toLower()+"/"+baseName; for (int f=0;f<nFiles;f++) {  QString value=files[f];  QString key=value;  key=key.toLower()+"/"+key;//Order case insensitively, then case sensitively if equal  filesSeq[key]=value; } if (offset<-1) {  //Move to start  SMap::const_iterator up=filesSeq.constBegin();  fileName=up.value(); } else if (offset>1) {  //Move to end  SMap::const_iterator up=filesSeq.constEnd();  --up;  fileName=up.value(); } else if (offset==1) {  SMap::const_iterator up=filesSeq.upperBound(baseKey);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?