⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dialog.cc

📁 c++的guiQt做的开发
💻 CC
字号:
/** @file Dialog - class with various static dialogs:<br> openFileDialog - pick a filename for opening it<br> saveFileDialog - pick a filename for saving as<br> colorDialog - dialog for selecting color<br> readStringDialog - Ask user a question end expect him to enter some string as answer<br> @author Martin Petricek*/#include "dialog.h"#include "settings.h"#include "version.h"#include <QDir>#include <QInputDialog>#include <QMessageBox>#include <QFileDialog>#include <QColorDialog>#include <QFileInfo>#include <QString>#include "util.h"namespace gui {/** Get current directory from File Dialog @param fd File Dialog @return current directory*/QString getDir(QFileDialog &fd) { QDir d=fd.directory(); return d.absolutePath();}/** Generic "Open file" dialog. Wait for user to select one existing file and return its name. Will return NULL if user cancels the dialog. @param parent Parent widget - will be disabled during the dialog. @param caption Caption used in dialog title @param settingName Key in settings used to save/load dialog window position @param savePath Key in settings used to identify where to save last used directory in the dialog @param filters Filters available to user to restrict shown filetypes in dialog @return selected Filename (or NULL)*/QString openFileDialog(QWidget* parent,const QString &caption/*=QString::null*/,const QString &settingName/*=QString::null*/,const QStringList &filters/*=QString::null*/,const QString &savePath/*=QString::null*/) { QFileDialog fd(parent); if (filters.size()) {  //Set filters if filters specified  fd.setFilters(filters); }// fd.setShowHiddenFiles(TRUE); if (!caption.isNull()) fd.setWindowTitle(caption); if (savePath.isNull()) {  //No save path specified -> start in current directory  fd.setDirectory("."); } else {  //Try to set last used saved path, if it exists  fd.setDirectory(globalSettings->read("history/path/"+savePath,".")); } fd.setFileMode(QFileDialog::ExistingFile); // "Infinite" loop, to restart the dialog if necessary (invalid file selected, etc ... ) for(;;) {  if (!settingName.isNull()) {   // Restore window position from settings if applicable   globalSettings->restoreWindow(&fd,settingName);  }  if (fd.exec()==QDialog::Accepted) {	//Dialog accepted   if (!settingName.isNull()) {    // Save window position to settings if applicable    globalSettings->saveWindow(&fd,settingName);   }   if (!savePath.isNull()) {    //Save the path if desired    if (globalSettings->readBool("history/save_filePath",true)) {     //Note that there is only one central setting "save paths in dialog" for all dialog types     globalSettings->write("history/path/"+savePath,getDir(fd));    }   }   QString name;   QStringList files = fd.selectedFiles();   if (!files.isEmpty()) name = files[0];   if (QFileInfo(name).isDir()) { //directory was selected    /** \todo  test this ! */    fd.setDirectory(name);    continue;//restart dialog   }   return name;  }  return QString::null; }}/** Invoke generic "save file" dialog. Wait for user to select or type a single file and return its name. Will return NULL if user cancels the dialog. @param parent Parent widget - will be disabled during the dialog. @param oldname Name of file to be saved - if specified, this name will be pre-selected. @param askOverwrite If true and selected file exists, user will be asked to confirm overwriting it @param caption Caption used in dialog title @param settingName Key in settings used to save/load dialog window position @param savePath Key in settings used to identify where to save last used directory in the dialog @param filters Filters available to user to restrict shown filetypes in dialog @return selected Filename (or NULL)*/QString saveFileDialog(QWidget* parent,const QString &oldname,bool askOverwrite/*=true*/,const QString &caption/*=QString::null*/,const QString &settingName/*=QString::null*/,const QStringList &filters/*=QString::null*/,const QString &savePath/*=QString::null*/) { QFileDialog fd(parent); if (filters.size()) {  //Set filters if filters specified  fd.setFilters(filters); }// fd.setShowHiddenFiles(TRUE); if (!caption.isNull()) fd.setWindowTitle(caption); if (savePath.isNull()) {  //No save path specified -> start in current directory  fd.setDirectory("."); } else {  //Try to set last used saved path, if it exists  fd.setDirectory(globalSettings->read("history/path/"+savePath,".")); } if (!oldname.isNull()) fd.selectFile(oldname); fd.setFileMode(QFileDialog::AnyFile); //Name that will hold the file (if some is picked) QString name; // "Infinite" loop, to restart the dialog if necessary (invalid file selected, etc ... ) for(;;) {  if (!settingName.isNull()) {   // Restore window position from settings if applicable   globalSettings->restoreWindow(&fd,settingName);  }  if (fd.exec()==QDialog::Accepted) {   if (!settingName.isNull()) {    // Save window position to settings if applicable    globalSettings->saveWindow(&fd,settingName);   }   QStringList files = fd.selectedFiles();   if (!files.isEmpty()) name = files[0]; else name=QString::null;   /** \todo check if not directory */   if (askOverwrite && QFile::exists(name)) {    //File exists : ask if it should be overwritten    int answer=QMessageBox::question(parent,APP_NAME,     QObject::tr("File called \"%1\" already exists. Do you want to overwrite it?").arg(name),     QObject::tr("&Yes"),QObject::tr("&No"),QObject::tr("&Cancel"),1,2);    if (answer==0) {				 //Yes, overwrite is ok     //Break from the cycle mean valid file was selected     break;    }    if (answer==1) continue;			//No, restart dialog and ask for another file    if (answer==2) return QString::null;	//Cancel, do not overwrite and exit   }   //Not asking about overwrite   //Break from the cycle mean valid file was selected   break;  }  //Dialog cancelled  if (!settingName.isNull()) {   // Save window position to settings if applicable   globalSettings->saveWindow(&fd,settingName);  }  return QString::null; } //End of not-so-infinite for cycle if (!savePath.isNull()) {  //Save the path if desired  if (globalSettings->readBool("history/save_filePath",true)) {   //Note that there is only one central setting "save paths in dialog" for all dialog types   globalSettings->write("history/path/"+savePath,getDir(fd));  } } return name;}/** Invoke "read string" dialog. Show message and wait for user to type any string, Will return NULL if user cancels the dialog. @param parent Parent widget - will be disabled during the dialog. @param message Message to show in the dialog @param def Default text that will be pre-typed in the dialog @return typed text, or NULL if dialog cancelled */QString readStringDialog(QWidget* parent,const QString &message, const QString &def) { bool ok=FALSE; QString res=QInputDialog::getText(parent,APP_NAME,message,QLineEdit::Normal,def,&ok); if (ok) return res; return QString::null;}/** Invoke dialog to select color. Last selected color is remembered and offered as default next time. The 'initial default color' is red @param parent Parent widget - will be disabled during the dialog. @return selected color. If dialog is cancelled, 'invalid' color is returned*/QColor colorDialog(QWidget* parent) { static QColor defaultColor=Qt::red; QColor ret=QColorDialog::getColor(defaultColor,parent); if (ret.isValid()) defaultColor=ret; return ret;}/** Asks question with Yes/No answer. "Yes" is default. Return true if user selected "yes", false if user selected "no" @param msg Question to display @param parent Parent widget - will be disabled during the dialog. @return True if yes, false if no*/bool questionDialog(QWidget* parent,const QString &msg) { int answer=QMessageBox::question(parent,APP_NAME,msg,QObject::tr("&Yes"),QObject::tr("&No"),QString::null,0,1); return (answer==0);}} // namespace gui

⌨️ 快捷键说明

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