main.cpp
来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 543 行 · 第 1/2 页
CPP
543 行
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the tools 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 "graphics.h"#include "feature.h"#include "featuretreemodel.h"#include <QtGui>static QString defaultPath;class FeatureTextBrowser : public QTextBrowser { Q_OBJECTpublic: FeatureTextBrowser(QWidget *parent) : QTextBrowser(parent) { QString docRoot; docRoot = QLibraryInfo::location(QLibraryInfo::DocumentationPath) + "/html"; setSearchPaths(searchPaths() << docRoot); }signals: void featureClicked(const QString &feature);public slots: void setSource(const QUrl &url) { if (url.scheme() == "feature") emit featureClicked(url.authority()); else QTextBrowser::setSource(url); }};class Main : public QMainWindow { Q_OBJECTpublic: Main(); ~Main(); void loadFeatures(const QString& filename); void loadConfig(const QString& filename);public slots: void modelChanged(); void showInfo(const QModelIndex &index); void showInfo(const QString &feature); void openConfig(); void saveConfig(); void expandView(); void collapseView(); void about(); void aboutQt(); void quit(); void clear(); void enableAll(); void disableAll();private: QTextBrowser *textBrowser; QTreeView *featureTree; FeatureTreeModel *featureModel; void init(); void updateStatus(int numFeatures = -1); void completelyExpandIndex(const QModelIndex &parent);};template<typename Func>void foreachIndex_helper(const QModelIndex &parent, Func func){ const QAbstractItemModel *model = parent.model(); const int rows = model->rowCount(parent); for (int i = 0; i < rows; ++i) { const QModelIndex child = model->index(i, 0, parent); func(child); foreachIndex_helper(child, func); }}template<typename Func>void foreachIndex(const QAbstractItemModel *model, Func func){ const int rows = model->rowCount(QModelIndex()); for (int i = 0; i < rows; ++i) { const QModelIndex child = model->index(i, 0, QModelIndex()); func(child); foreachIndex_helper(child, func); }}struct CheckStateSetter { CheckStateSetter(Qt::CheckState state, QAbstractItemModel *m) : checkState(state), model(m) {} void operator()(const QModelIndex &index) { model->setData(index, checkState, Qt::CheckStateRole); } Qt::CheckState checkState; QAbstractItemModel *model;};void Main::disableAll(){ QAbstractItemModel *model = featureTree->model(); foreachIndex(model, CheckStateSetter(Qt::Unchecked, model));}void Main::enableAll(){ QAbstractItemModel *model = featureTree->model(); foreachIndex(model, CheckStateSetter(Qt::Checked, model));}Main::Main(){ setWindowIcon(QIcon(QPixmap(logo_xpm))); QSplitter *splitter = new QSplitter(this); featureModel = new FeatureTreeModel(this); featureTree = new QTreeView(splitter); splitter->addWidget(featureTree); featureTree->setRootIsDecorated(true); featureTree->setModel(featureModel); featureTree->show(); textBrowser = new FeatureTextBrowser(splitter); textBrowser->setFrameStyle(QFrame::WinPanel|QFrame::Sunken); splitter->addWidget(textBrowser); textBrowser->show(); connect(textBrowser, SIGNAL(featureClicked(const QString&)), this, SLOT(showInfo(const QString&))); connect(featureTree, SIGNAL(activated(QModelIndex)), this, SLOT(showInfo(QModelIndex))); connect(featureModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(modelChanged())); connect(featureTree, SIGNAL(clicked(QModelIndex)), this, SLOT(showInfo(QModelIndex))); setCentralWidget(splitter); QMenu *file = menuBar()->addMenu("&File"); file->addAction("&Open...", this, SLOT(openConfig()), Qt::CTRL + Qt::Key_O); file->addAction("&Save As...", this, SLOT(saveConfig()), Qt::CTRL + Qt::Key_S); file->addSeparator(); file->addAction("&Reset", this, SLOT(clear())); file->addSeparator(); file->addAction("E&xit", this, SLOT(quit()), Qt::CTRL + Qt::Key_Q); QMenu *edit = menuBar()->addMenu("&Tools"); edit->addAction("&Enable all features", this, SLOT(enableAll())); edit->addAction("&Disable all features", this, SLOT(disableAll())); menuBar()->addSeparator(); QMenu *help = menuBar()->addMenu("&Help"); help->addAction("&About", this, SLOT(about())); help->addAction("About &Qt", this, SLOT(aboutQt())); QToolBar *tb = new QToolBar("Expand/Collapse features"); QToolButton *button; button = new QToolButton(tb); button->setIcon(QIcon(QPixmap(collapsed_xpm))); button->setText("Collapse"); button->setToolTip("Collapse"); connect(button, SIGNAL(clicked()), this, SLOT(collapseView())); tb->addWidget(button); button = new QToolButton(tb); button->setIcon(QIcon(QPixmap(expanded_xpm))); button->setText("Expand"); button->setToolTip("Expand"); connect(button, SIGNAL(clicked()), this, SLOT(expandView())); tb->addWidget(button); addToolBar(tb); init();}Main::~Main(){ delete textBrowser; delete featureModel; delete featureTree;}void Main::clear(){ QSettings settings; settings.clear(); featureModel->clear(); featureTree->reset(); init();}void Main::quit(){ if (isWindowModified()) { int button = QMessageBox::question(this, "Quit Program", "You have unsaved changes.\n" "Do you want to quit anyway?", QMessageBox::Yes, QMessageBox::No); if (static_cast<QMessageBox::Button>(button) != QMessageBox::Yes) return; } QApplication::instance()->quit();}/* Recursively expand expand \a parent and all of its children.*/void Main::completelyExpandIndex(const QModelIndex &parent){ featureTree->setExpanded(parent, true); const QAbstractItemModel *model = featureTree->model(); const int rows = model->rowCount(parent); for (int i = 0; i < rows; ++i) completelyExpandIndex(model->index(i, 0, parent));}void Main::expandView(){ completelyExpandIndex(QModelIndex());}void Main::collapseView(){ const QAbstractItemModel *model = featureTree->model(); const int rows = model->rowCount(QModelIndex()); for (int i = 0; i < rows; ++i) { QModelIndex index = model->index(i, 0, QModelIndex());
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?