mainwindow.cc
来自「c++的guiQt做的开发」· CC 代码 · 共 1,250 行 · 第 1/3 页
CC
1,250 行
if (up==filesSeq.constEnd()) { //We moved off the end - return to begin cmdLine->addError(tr("This was last file in directory, starting from beginning")); up=filesSeq.constBegin(); } fileName=up.value(); } else { //Offset=-1, move one file back SMap::const_iterator up=filesSeq.lowerBound(baseKey); if (up==filesSeq.constBegin()) { //We are moving off the beginning - return to end cmdLine->addError(tr("This was first file in directory, starting from end")); up=filesSeq.constEnd(); } --up; fileName=up.value(); } //Open the file we want openFile(dir.absoluteFilePath(fileName));}/** Equalize histogram*/void MainWindow::equalizeHistogram() { Image *i=getImage(); if (!i) return; i->equalizeHistogram(); postOp(i);}/** application exit handler invoked when "Quit" is selected */void MainWindow::quit() { qApp->closeAllWindows(); //Application will exit after last window is closed}/** application exit handler invoked when "Close window" is selected */void MainWindow::closeWindow() { close();}/** This is called on attempt to close window. If there is unsaved work, dialog asking to save it would appear, otherwise the windows is closed. @param e Close event*/void MainWindow::closeEvent(QCloseEvent *e) {/* if (!closeFile(true)) { //user does not want to close and lose unsaved work e->ignore(); return; }*/ e->accept(); saveWindowState();}/** Creates new windows and displays it. @param fName Name of file to open in new window. If no file specified, new window is initially empty @return Pointer to new window */MainWindow* MainWindow::create(const QString &fName/*=QString::null*/) { MainWindow *main=new MainWindow(fName); main->restoreWindowState(); main->show(); return main;}/** Saves window state to application settings*/void MainWindow::saveWindowState() { globalSettings->saveWindow(this,"main"); globalSettings->saveSplitter(splCmd,"spl_cmd"); globalSettings->saveSplitter(splTop,"spl_top"); menuSystem->saveToolbars();}/** Restores window state from application settings */void MainWindow::restoreWindowState() { globalSettings->restoreWindow(this,"main"); globalSettings->restoreSplitter(splCmd,"spl_cmd");// globalSettings->restoreSplitter(splTop,"spl_top"); menuSystem->restoreToolbars();}/** Signal handler invoked on menu activation @param action Action to invoke */void MainWindow::menuActivated(const QString &action) { if (action.isNull()) return; //no action runScript(action);}/** Signal called when receiving help message. Show it in statusbar @param message Help message*/void MainWindow::receiveHelpText(const QString &message) { status->message(message);}/** Check whether there is something that needs to be saved @return true if yes, false if not*/bool MainWindow::modified() { /** \todo */ return false;}/** Shows "About" window */void MainWindow::about() { AboutWindow *aboutWin= new AboutWindow(this); aboutWin->show();}/** Capture entire screen */void MainWindow::capture() { bool doHide=globalSettings->readBool("capture/hide"); if (doHide) hide(); //Hide the main window qApp->processEvents();//Wait for the window to actually hide qApp->flush();//Flush the changes (for X11 ...) int delay=globalSettings->readNum("capture/delay"); if (delay>0) { msleep(delay); } QDesktopWidget *desk=qApp->desktop(); QPixmap px=QPixmap::grabWindow(desk->winId()); openFile(px); if (doHide) show(); //Show the main window}/** Expand single variable @param var name of the variable @return Value of the variable or empty string if the variable does not exist*/QString MainWindow::expandSingleVariable(const QString &var) { QString envVar; if (var=="SELECTION") envVar=main->getRect(); else if (var=="SELECTION_X1") envVar=main->getRect(1); else if (var=="SELECTION_Y1") envVar=main->getRect(2); else if (var=="SELECTION_X2") envVar=main->getRect(3); else if (var=="SELECTION_Y2") envVar=main->getRect(4); else envVar=globalSettings->expandSingle(var); if (envVar==QString::null) { //variable not found in environment envVar=""; } return envVar;}/** Expand environment variables in given string (like $HOME, etc ..) @param s String to expand @return QString with variables expanded*/QString MainWindow::expandVariables(QString s) { QRegExp r("\\$([a-zA-Z0-9_]+|\\{[_a-zA-Z0-9]+\\})|(\\\\.)"); QString var, envVar; int pos=0; while((pos=r.indexIn(s,pos))!=-1) { //while found some variable if (r.cap(2).length()>0) {// Found \something -> skip envVar=r.cap(2).mid(1); } else { //Found $VARIABLE var=r.cap(1); if (var[0]=='{') var=var.mid(1,var.length()-2); envVar=expandSingleVariable(var); } s=s.replace(pos,r.matchedLength(),envVar); pos+=envVar.length();//Move to end of expanded string } return s;}/** Return displayed image Print out error message if no image is loaded*/Image* MainWindow::getImage() { Image *i=main->getImage(); if (!i) cmdLine->addError(tr("No image is loaded")); return i;}/** Shows "Image information" window for current image.*/void MainWindow::imageInfo() { Image *i=getImage(); if (!i) return; SMap qm=i->info(); Info *info= new Info(qm,tr("Image information"),this,"info"); info->show();}/** Shows help for specific command (if name of command is given) or for all commands @param param Command parameters*/void MainWindow::help(const QStringList ¶m) { if (param.count()<1) { cmdLine->addStringHtml(tr("List of all commands")+":"); const QStringList &cmd=commandList(); for (int i=0;i<cmd.size();i++) { printHelpText(cmd[i]); } cmdLine->addStringHtml(tr("For detailed command help, use 'help [name of command]'")); } else { if (validCommand(param[0])) { printHelpText(param[0],true); } else { cmdLine->addError(tr("Unknown command '%1'").arg(param[0])); } }}/** Print help text for one command to console @param name Name of command @param longText if true, print text in long format*/void MainWindow::printHelpText(const QString &name, bool longText/*=false*/) { cmdLine->addStringHtml(helpTextHtml(name,longText));}/** Invoke options dialog*/void MainWindow::options() { MPViewOptionWindow::optionsDialog(menuSystem);}/** Run a script @param script Script to execute*/void MainWindow::runScript(const QString &script) { if (script.isNull()) return; // Empty command line QString line=script.trimmed(); if (line=="") return; // Empty command line cmdLine->addCommand(line); //Commands are case-insensitive QString cmd=stripParam(line).toLower(); QStringList params; QString str; for(;;) { str=stripParam(line); if (str.isNull()) break; params+=expandVariables(str); } if (!invokeCommand(this,cmd,params)) { cmdLine->addError(tr("Unknown command %1").arg(cmd)); }}/** Echoes all parameters to console @param param parameters*/void MainWindow::echo(const QStringList ¶m) { QString text=""; for (int i=0;i<param.count();i++) { if (i) text+=" "; text+=param[i]; } cmdLine->addString(text);}/** Output error message about insufficient number of parameters @param min_num Minimal number of required parameters @param commandName name of command*/void MainWindow::minParam(int min_num,const QString &commandName) { cmdLine->addError(tr("Command %1 needs at least %2 parameter(s)").arg(commandName).arg(min_num));}/** Set view features for viewing data according to parameters Name of the feature is specified in parameter, second parameter is on (1), off(0) or "toggle" @param param parameters*/void MainWindow::setView(const QStringList ¶m) { if (param.count()<2) { minParam(2,"view"); return; } QString feat=param[0].toLower(); bool o=param[1].toInt()!=0; if (param[1].toLower()=="toggle") o=!main->isSet(feat); main->setFeature(feat,o);}/** Show/hide console according to setting*/void MainWindow::updateConsoleVisibility() { if (globalSettings->readBool("gui/console",true)) { cmdLine->show(); } else { cmdLine->hide(); }}/** Toggle specified boolean setting - if it was on, it will be set to off and vice versa. If it was not set, it is assumed it was false (off) @param param command parameters*/void MainWindow::toggleSetting(const QStringList ¶m) { if (!getParams(param)) return; QString set=getParam("setting"); bool setValue=globalSettings->readBool(set,false); globalSettings->write(set,!setValue);}/** Called when any settings are updated (in script, option editor, etc ...) @param key Key of setting that was updated.*/void MainWindow::settingUpdate(QString key) { if (key=="gui/style") { applyStyle(); } if (key=="gui/console") { updateConsoleVisibility(); updateMenus(); } if (key=="icon/size") { menuSystem->setButtonSize(globalSettings->readNum("icon/size",DEFAULT_TOOLBUTTON_SIZE)); } if (key=="icon/theme/current" || key=="path/icon") { menuSystem->reloadIconCache(); menuSystem->reload(); } if (key.startsWith("view/")) { main->updateFullScreen(); updateMenus(); } if (key=="gui/language") { // Load different translation if overridden in settings switchLang(globalSettings->read("gui/language","")); }}/** Set new language @param newLang Name of new language*/void MainWindow::switchLang(const QString &newLang) { loadLanguage(newLang); setTitle(); unregisterAll(); addFunctions(); menuSystem->reload(); update();}/** Set language @param param Command parameters*/void MainWindow::lang(const QStringList ¶m) { if (!getParams(param)) return; QString lang=getParam("language"); switchLang(lang);}/** Clip values above given level @param param Command parameters*/void MainWindow::clipAbove(const QStringList ¶m) { Image *i=getImageAndParams(param); if (!i) return; int val=getParamInt("value"); i->clipMax(val); postOp(i);}/** Clip values above given level @param param Command parameters*/void MainWindow::clipBelow(const QStringList ¶m) { Image *i=getImageAndParams(param); if (!i) return; int val=getParamInt("value"); i->clipMin(val); postOp(i);}/** Set window title, according to stored baseName and flags */void MainWindow::setTitle() { QString fName=""; Image *i=main->getImage(); if (i) { fName=i->baseName(); if (fName.isNull()) { fName=QString("(")+tr("Not saved")+")"; } fName+=" - "; } setWindowTitle(fName+APP_NAME);}/** default destructor */MainWindow::~MainWindow() { delete menuSystem;}} // namespace gui
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?