📄 menumanager.cpp
字号:
/******************************************************************************** Copyright (C) 2005-2007 Trolltech ASA. All rights reserved.**** This file is part of the demonstration applications of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "menumanager.h"#include "colors.h"#include "menucontent.h"#include "examplecontent.h"MenuManager *MenuManager::pInstance = 0;MenuManager * MenuManager::instance(){ if (!MenuManager::pInstance) MenuManager::pInstance = new MenuManager(); return MenuManager::pInstance;}MenuManager::MenuManager(){ this->ticker = 0; this->assistant = new QAssistantClient(QLibraryInfo::location(QLibraryInfo::BinariesPath), this); this->score = new Score(); this->currentMenu = "-no menu"; this->currentExample = "-no example"; this->readXmlDocument();}MenuManager::~MenuManager(){ delete this->score; delete this->contentsDoc;}void MenuManager::readXmlDocument(){ this->contentsDoc = new QDomDocument(); QString errorStr; int errorLine; int errorColumn; QFile file(":/xml/examples.xml"); bool statusOK = this->contentsDoc->setContent(&file, true, &errorStr, &errorLine, &errorColumn); if (!statusOK){ QMessageBox::critical(0, QObject::tr("DOM Parser"), QObject::tr("Could not read or find the contents document. Error at line %1, column %2:\n%3") .arg(errorLine).arg(errorColumn).arg(errorStr) ); exit(-1); }}void MenuManager::itemSelected(int userCode, const QString &menuName){ if (userCode == LAUNCH) this->launchExample(this->currentExample); else if (userCode == DOCUMENTATION) this->assistant->showPage(info[this->currentExample]["docfile"]); else if (userCode == QUIT){ this->window->loop = false; QCoreApplication::quit(); } else if (userCode == FULLSCREEN) this->window->toggleFullscreen(); else { if (userCode == ROOT){ this->score->queueMovie(this->currentMenu + " -out", Score::FROM_START, Score::LOCK_ITEMS); this->score->queueMovie(this->currentExample + " -out"); this->score->queueMovie(this->currentExample + " -buttons -out"); this->score->queueMovie("Qt Examples and Demos", Score::FROM_START, Score::UNLOCK_ITEMS); this->score->queueMovie("Qt Examples and Demos -info"); if (!Colors::noTicker) this->ticker->useGuideQt(); this->score->queueMovie("ticker", Score::NEW_ANIMATION_ONLY); this->window->switchTimerOnOff(true); } else if (userCode == MENU1){ this->currentMenu = menuName; this->currentExample = menuName + " -info"; this->score->queueMovie("Qt Examples and Demos -out", Score::FROM_START, Score::LOCK_ITEMS); this->score->queueMovie("Qt Examples and Demos -info -out"); this->score->queueMovie(this->currentMenu, Score::FROM_START, Score::UNLOCK_ITEMS); this->score->queueMovie(this->currentExample); if (!Colors::noTicker) this->ticker->useGuideTt(); } else if (userCode == MENU2){ this->score->queueMovie(this->currentExample + " -out", Score::NEW_ANIMATION_ONLY); this->score->queueMovie(this->currentExample + " -buttons -out", Score::NEW_ANIMATION_ONLY); this->score->queueMovie("ticker -out", Score::NEW_ANIMATION_ONLY); this->score->queueMovie(this->currentMenu + " -shake"); this->score->queueMovie(menuName, Score::NEW_ANIMATION_ONLY); this->score->queueMovie(menuName + " -buttons", Score::NEW_ANIMATION_ONLY); this->currentExample = menuName; this->window->switchTimerOnOff(false); } this->score->playQue(); } // Playing new movies might include // loading etc. So ignore the FPS // at this point this->window->fpsHistory.clear();}void MenuManager::launchExample(const QString &name){ QString executable = this->info[name]["executable"]; QProcess *process = new QProcess(this); connect(process, SIGNAL(finished(int)), this, SLOT(exampleFinished())); connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(exampleError(QProcess::ProcessError))); #ifdef Q_OS_WIN //make sure it finds the dlls on windows QString curpath = QString::fromLocal8Bit(qgetenv("PATH").constData()); QString newpath = QString("PATH=%1;%2").arg(QLibraryInfo::location(QLibraryInfo::BinariesPath), curpath); process->setEnvironment(QStringList(newpath));#endif if (info[name]["changedirectory"] != "false"){ QDir dir(info[name]["executable"]); dir.cdUp(); process->setWorkingDirectory(dir.absolutePath()); if (Colors::verbose) qDebug() << "Setting working directory:" << dir.absolutePath(); } if (Colors::verbose) qDebug() << "Launching:" << executable; process->start(executable);}void MenuManager::exampleFinished(){}void MenuManager::exampleError(QProcess::ProcessError error){ if (error != QProcess::Crashed) QMessageBox::critical(0, tr("Failed to launch the example"), tr("Could not launch the example. Ensure that it has been build."), QMessageBox::Cancel);}void MenuManager::init(MainWindow *window){ this->window = window; // Create ticker: this->createTicker(); // Create first level menu: QDomElement rootElement = this->contentsDoc->documentElement(); this->createLeftMenu1(rootElement); // Create second level menus: QDomNode level2MenuNode = rootElement.firstChild(); while (!level2MenuNode.isNull()){ QDomElement level2MenuElement = level2MenuNode.toElement(); this->createRightMenu1(level2MenuElement); // create leaf menu and example info: QDomNode exampleNode = level2MenuElement.firstChild(); while (!exampleNode.isNull()){ QDomElement exampleElement = exampleNode.toElement(); this->readInfoAboutExample(exampleElement); this->createLeafMenu(exampleElement); exampleNode = exampleNode.nextSibling(); } level2MenuNode = level2MenuNode.nextSibling(); }}void MenuManager::readInfoAboutExample(const QDomElement &example){ QString name = example.attribute("name"); if (this->info.contains(name)) qWarning() << "WARNING: MenuManager::readInfoAboutExample: Demo/example with name" << name << "appears twize in the xml-file!"; this->info[name]["filename"] = example.attribute("filename"); this->info[name]["changedirectory"] = example.attribute("changedirectory"); this->info[name]["executable"] = this->resolveExecutable(example); this->info[name]["docfile"] = this->resolveDocFile(example); this->info[name]["imgfile"] = this->resolveImgFile(example);}QString MenuManager::resolveExecutable(const QDomElement &example){ QDomElement parent = example.parentNode().toElement(); QDir dir; if (parent.tagName() == "demos") dir = QDir(QLibraryInfo::location(QLibraryInfo::DemosPath)); else dir = QDir(QLibraryInfo::location(QLibraryInfo::ExamplesPath)); QString fileName = example.attribute("filename"); dir.cd(parent.attribute("dirname")); dir.cd(fileName); QFile unixFile(dir.path() + "/" + fileName); if (unixFile.exists()) return unixFile.fileName(); QFile winR(dir.path() + "\\release\\" + fileName + ".exe"); if (winR.exists()) return winR.fileName(); QFile winD(dir.path() + "\\debug\\" + fileName + ".exe"); if (winD.exists()) return winD.fileName(); QFile mac(dir.path() + "/" + fileName + ".app"); if (mac.exists()) return mac.fileName(); return "QtDemo: Executable not found!";}QString MenuManager::resolveDocFile(const QDomElement &example){ QDomElement parent = example.parentNode().toElement(); QString docRootPath = QLibraryInfo::location(QLibraryInfo::DocumentationPath); if (parent.tagName() == "demos") return docRootPath + "/html/demos-" + example.attribute("filename") + ".html"; else return docRootPath + "/html/" + parent.attribute("dirname") + "-" + example.attribute("filename") + ".html";}QString MenuManager::resolveImgFile(const QDomElement &example){ // Scan the html document and look for an image: QDomDocument domDoc; QFile docFile(this->info[example.attribute("name")]["docfile"]); domDoc.setContent(&docFile); QDomNodeList images = domDoc.elementsByTagName("img"); QDir docRootDir = QDir(QLibraryInfo::location(QLibraryInfo::DocumentationPath)); docRootDir.cd("html"); QStringList imageFiles; for (int i = 0; i< int(images.length()); ++i) { QDomElement imageElement = images.item(i).toElement(); QString imagePath = imageElement.attribute("src"); if (!imagePath.contains("-logo")) imageFiles.append(docRootDir.absoluteFilePath(imagePath)); } if (imageFiles.size() > 0) return imageFiles[0]; else return QLatin1String("No image found in document: ") + this->info[example.attribute("name")]["docfile"];}void MenuManager::createLeftMenu1(const QDomElement &el){ Movie *movie_in = new Movie(); Movie *movie_out = new Movie(); this->score->insertMovie(el.attribute("name"), movie_in); this->score->insertMovie(el.attribute("name")+ " -out", movie_out); qreal sw = this->window->scene->sceneRect().width(); int xOffset = 15; int yOffset = 10; QDomNode n = el.firstChild(); int i=0; while (!n.isNull()){ QDomElement e = n.toElement(); TextButton *item = new TextButton(e.attribute("name"), TextButton::LEFT, MENU1, this->window->scene, 0); item->setRecursiveVisible(false); item->setZValue(10); qreal ih = item->sceneBoundingRect().height(); qreal iw = item->sceneBoundingRect().width(); qreal ihp = ih + 3;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -