📄 launcher.cpp
字号:
/******************************************************************************** Copyright (C) 2005-2006 Trolltech ASA. All rights reserved.**** This file is part of the example classes 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://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** 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 <QtGui>#include <QDomDocument>#include <QtAssistant/QAssistantClient>#include "displayshape.h"#include "displaywidget.h"#include "launcher.h"Launcher::Launcher(QWidget *parent) : QMainWindow(parent){ titleFont = font(); titleFont.setWeight(QFont::Bold); buttonFont = font(); fontRatio = 0.8; documentFont = font(); inFullScreenResize = false; currentCategory = "[starting]"; QAction *parentPageAction1 = new QAction(tr("Show Parent Page"), this); QAction *parentPageAction2 = new QAction(tr("Show Parent Page"), this); QAction *parentPageAction3 = new QAction(tr("Show Parent Page"), this); parentPageAction1->setShortcut(QKeySequence(tr("Escape"))); parentPageAction2->setShortcut(QKeySequence(tr("Backspace"))); parentPageAction3->setShortcut(QKeySequence(tr("Alt+Left"))); QAction *fullScreenAction = new QAction(tr("Toggle &Full Screen"), this); fullScreenAction->setShortcut(QKeySequence(tr("Ctrl+F"))); QAction *exitAction = new QAction(tr("E&xit"), this); exitAction->setShortcut(QKeySequence(tr("Ctrl+Q"))); connect(parentPageAction1, SIGNAL(triggered()), this, SIGNAL(showPage())); connect(parentPageAction2, SIGNAL(triggered()), this, SIGNAL(showPage())); connect(parentPageAction3, SIGNAL(triggered()), this, SIGNAL(showPage())); connect(fullScreenAction, SIGNAL(triggered()), this, SLOT(toggleFullScreen())); connect(exitAction, SIGNAL(triggered()), this, SLOT(close())); display = new DisplayWidget; addAction(parentPageAction1); addAction(parentPageAction2); addAction(parentPageAction3); addAction(fullScreenAction); addAction(exitAction); slideshowTimer = new QTimer(this); slideshowTimer->setInterval(5000); resizeTimer = new QTimer(this); resizeTimer->setSingleShot(true); connect(resizeTimer, SIGNAL(timeout()), this, SLOT(redisplayWindow())); assistant = new QAssistantClient("assistant", this); connect(display, SIGNAL(actionRequested(const QString &)), this, SLOT(executeAction(const QString &))); connect(display, SIGNAL(categoryRequested(const QString &)), this, SLOT(showExamples(const QString &))); connect(display, SIGNAL(documentationRequested(const QString &)), this, SLOT(showExampleDocumentation(const QString &))); connect(display, SIGNAL(exampleRequested(const QString &)), this, SLOT(showExampleSummary(const QString &))); connect(display, SIGNAL(launchRequested(const QString &)), this, SLOT(launchExample(const QString &))); connect(this, SIGNAL(showPage()), this, SLOT(showParentPage()), Qt::QueuedConnection); connect(this, SIGNAL(windowResized()), this, SLOT(redisplayWindow()), Qt::QueuedConnection); setCentralWidget(display); setMaximumSize(QApplication::desktop()->screenGeometry().size()); setWindowTitle(tr("Qt Examples and Demos")); setWindowIcon(QPixmap(":/images/qt4-logo.png"));}bool Launcher::setup(){ documentationDir = QDir(QLibraryInfo::location( QLibraryInfo::DocumentationPath)); if (!documentationDir.cd("html")) { // Failed to find the HTML documentation. // We can continue without it. QMessageBox::warning(this, tr("No Documentation Found"), tr("I could not find the Qt documentation."), QMessageBox::Cancel, QMessageBox::NoButton); } imagesDir = documentationDir; if (!imagesDir.cd("images")) { // Failed to find the accompanying images for the documentation. // We can continue without them. QMessageBox::warning(this, tr("No Images Found"), tr("I could not find any images for the Qt documentation."), QMessageBox::Cancel, QMessageBox::NoButton); } maximumLabels = 0; demosDir = QDir(QLibraryInfo::location(QLibraryInfo::DemosPath)); int demoCategories = readInfo(":/demos.xml", demosDir); examplesDir = QDir(QLibraryInfo::location(QLibraryInfo::ExamplesPath)); int exampleCategories = readInfo(":/examples.xml", examplesDir); if (demoCategories + exampleCategories <= 0) { // Failed to find the examples. QMessageBox::warning(this, tr("No Examples or Demos found"), tr("I could not find any Qt examples or demos.\n" "Please ensure that Qt is installed correctly."), QMessageBox::Cancel, QMessageBox::NoButton); return false; } maximumLabels = qMax(demoCategories + exampleCategories, maximumLabels); // For example menus, remember that the menu has to include a Back button. foreach (QString category, categories) maximumLabels = qMax(examples[category].size() + 1, maximumLabels); QString mainDescription = categoryDescriptions["[main]"]; if (!mainDescription.isEmpty()) mainDescription += tr("\n"); categoryDescriptions["[main]"] = mainDescription + tr( "<p>Press <b>Escape</b>, <b>Backspace</b>, or <b>%1</b> to " "return to a previous menu.</p>\n" "<p>Press <b>%2</b> to switch between normal and full screen " "modes.</p>\n" "<p>Use <b>%3</b> to exit the launcher.</p>").arg(QString( QKeySequence(tr("Alt+Left")))).arg(QString( QKeySequence(tr("Ctrl+F")))).arg(QString( QKeySequence(tr("Ctrl+Q")))); emit showPage(); return true;}void Launcher::findDescriptionAndImages(const QString &uniqueName, const QString &docName){ if (documentationDir.exists(docName)) { exampleDetails[uniqueName]["document path"] = \ documentationDir.absoluteFilePath(docName); QDomDocument exampleDoc; QFile exampleFile(exampleDetails[uniqueName]["document path"]); exampleDoc.setContent(&exampleFile); QDomNodeList paragraphs = exampleDoc.elementsByTagName("p"); QString description; for (int p = 0; p < int(paragraphs.length()); ++p) { QDomNode descriptionNode = paragraphs.item(p); description = readExampleDescription(descriptionNode); if (description.indexOf(QRegExp(QString( "((The|This) )?(%1 )?.*(example|demo)").arg( exampleDetails[uniqueName]["name"]), Qt::CaseInsensitive)) != -1) { exampleDetails[uniqueName]["description"] = description; break; } } QDomNodeList images = exampleDoc.elementsByTagName("img"); QStringList imageFiles; for (int i = 0; i < int(images.length()); ++i) { QDomElement imageElement = images.item(i).toElement(); QString source = imageElement.attribute("src"); if (!source.contains("-logo")) imageFiles.append(documentationDir.absoluteFilePath(source)); } if (imageFiles.size() > 0) imagePaths[uniqueName] = imageFiles; }}int Launcher::readInfo(const QString &resource, const QDir &dir){ QFile categoriesFile(resource); QDomDocument document; document.setContent(&categoriesFile); QDomElement documentElement = document.documentElement(); QDomNodeList categoryNodes = documentElement.elementsByTagName("category"); readCategoryDescription(dir, "[main]"); qtLogo.load(imagesDir.absoluteFilePath(":/images/qt4-logo.png")); trolltechLogo.load(imagesDir.absoluteFilePath(":/images/trolltech-logo.png")); for (int i = 0; i < int(categoryNodes.length()); ++i) { QDomNode categoryNode = categoryNodes.item(i); QDomElement element = categoryNode.toElement(); QString categoryName = element.attribute("name"); QString categoryDirName = element.attribute("dirname"); QString categoryDocName = element.attribute("docname"); QString categoryColor = element.attribute("color", "#f0f0f0"); QDir categoryDir = dir; if (categoryDir.cd(categoryDirName)) { readCategoryDescription(categoryDir, categoryName); examples[categoryName] = QStringList(); QDomNodeList exampleNodes = element.elementsByTagName("example"); maximumLabels = qMax(maximumLabels, int(exampleNodes.length())); for (int j = 0; j < int(exampleNodes.length()); ++j) { QDir exampleDir = categoryDir; QDomNode exampleNode = exampleNodes.item(j); element = exampleNode.toElement(); QString exampleName = element.attribute("name"); QString exampleFileName = element.attribute("filename"); QString exampleDirName = element.attribute("dirname"); examples[categoryName].append(exampleName); QString uniqueName = categoryName+"-"+exampleName; exampleOptions[uniqueName] = QMap<QString,QString>(); exampleDetails[uniqueName] = QMap<QString,QString>(); QString docName; if (!categoryDocName.isEmpty()) docName = categoryDocName+"-"+exampleFileName+".html"; else docName = categoryDirName+"-"+exampleFileName+".html"; exampleDetails[uniqueName]["name"] = exampleName; findDescriptionAndImages(uniqueName, docName); exampleOptions[uniqueName]["changedirectory"] = element.attribute("changedirectory", "true"); exampleOptions[uniqueName]["color"] = element.attribute("color", "#f0f0f0"); if (!exampleDirName.isEmpty() && !exampleDir.cd(exampleDirName)) continue; if (element.attribute("executable", "true") != "true") continue; if (exampleDir.cd(exampleFileName)) { QString examplePath = findExecutable(exampleDir); if (!examplePath.isNull()) { exampleDetails[uniqueName]["absolute path"] = exampleDir.absolutePath(); exampleDetails[uniqueName]["path"] = examplePath; } } } categories.append(categoryName); categoryColors[categoryName] = categoryColor; } } return categories.size();}QString Launcher::findExecutable(const QDir &dir) const{ QDir parentDir = dir; parentDir.cdUp(); QFileInfo releaseDir(dir, "release"); if (releaseDir.isDir()) { QDir currentDir(releaseDir.absoluteFilePath()); QString path = findExecutable(currentDir); if (!path.isEmpty()) return path; } foreach (QFileInfo info, dir.entryInfoList(QDir::Dirs)) { if (info.fileName().endsWith(".app")) return info.absoluteFilePath(); QDir currentDir(info.absoluteFilePath()); if (currentDir != dir && currentDir != parentDir) { QString path = findExecutable(currentDir); if (!path.isNull()) return path; } } foreach (QFileInfo info, dir.entryInfoList(QDir::Files)) { if (info.isExecutable()) return info.absoluteFilePath(); } return QString();}void Launcher::readCategoryDescription(const QDir &categoryDir, const QString &categoryName){ if (categoryDir.exists("README")) { QFile file(categoryDir.absoluteFilePath("README")); if (!file.open(QFile::ReadOnly)) return; QTextStream inputStream(&file); QStringList paragraphs; QStringList currentPara; bool openQuote = true; while (!inputStream.atEnd()) { QString line = inputStream.readLine(); int at = 0; while ((at = line.indexOf("\"", at)) != -1) { if (openQuote) line[at] = QChar::Punctuation_InitialQuote; else line[at] = QChar::Punctuation_FinalQuote; openQuote = !openQuote; } if (!line.trimmed().isEmpty()) { currentPara.append(line.trimmed()); } else if (!currentPara.isEmpty()) { paragraphs.append(currentPara.join(" ")); currentPara.clear(); } else break; } if (!currentPara.isEmpty()) paragraphs.append(currentPara.join(" ")); categoryDescriptions[categoryName] = "<p>" + \ paragraphs.join("\n</p><p>") + "</p>\n"; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -