📄 domtool.cpp
字号:
/******************************************************************************** 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 "domtool.h"#include <QSizePolicy>#include <QColor>#include <QCursor>#include <QDateTime>#include <QRect>#include <QSize>#include <QFont>#include <QDomElement>#include <QByteArray>#include <QtDebug>/* \class DomTool domtool.h \brief The DomTool class provides static functions for Qt Designer and uic. A collection of static functions used by Resource (part of the designer) and Uic.*//* Returns the contents of property \a name of object \a e as a variant or the variant passed as \a defValue if the property does not exist. The \a comment is passed on to the elementToVariant() function. \sa hasProperty()*/QVariant DomTool::readProperty(const QDomElement& e, const QString& name, const QVariant& defValue, QString& comment){ QDomElement n; for (n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement()) { if (n.tagName() == QLatin1String("property")) { if (n.attribute(QLatin1String("name")) != name) continue; return elementToVariant(n.firstChild().toElement(), defValue, comment); } } return defValue;}/* \overload */QVariant DomTool::readProperty(const QDomElement& e, const QString& name, const QVariant& defValue){ QString comment; return readProperty(e, name, defValue, comment);}/* Returns whether object \a e defines property \a name or not. \sa readProperty() */bool DomTool::hasProperty(const QDomElement& e, const QString& name){ QDomElement n; for (n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement()) { if (n.tagName() == QLatin1String("property")) { if (n.attribute(QLatin1String("name")) != name) continue; return true; } } return false;}/* Returns a list of the names of the properties of the given \a type found in the element \a e.*/QStringList DomTool::propertiesOfType(const QDomElement& e, const QString& type){ QStringList result; QDomElement n; for (n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement()) { if (n.tagName() == QLatin1String("property")) { QDomElement n2 = n.firstChild().toElement(); if (n2.tagName() == type) result += n.attribute(QLatin1String("name")); } } return result;}/* \overload*/QVariant DomTool::elementToVariant(const QDomElement& e, const QVariant& defValue){ QString dummy; return elementToVariant(e, defValue, dummy);}/* Interprets element \a e as a variant and returns the result of the interpretation, extracting the data as a text element is the \a comment matches the tag name. If the interpretation fails the \a defValue is returned instead. */QVariant DomTool::elementToVariant(const QDomElement& e, const QVariant& defValue, QString &comment){ Q_UNUSED(defValue); QVariant v; Variant var; if (e.tagName() == QLatin1String("rect")) { QDomElement n3 = e.firstChild().toElement(); int x = 0, y = 0, w = 0, h = 0; while (!n3.isNull()) { if (n3.tagName() == QLatin1String("x")) x = n3.firstChild().toText().data().toInt(); else if (n3.tagName() == QLatin1String("y")) y = n3.firstChild().toText().data().toInt(); else if (n3.tagName() == QLatin1String("width")) w = n3.firstChild().toText().data().toInt(); else if (n3.tagName() == QLatin1String("height")) h = n3.firstChild().toText().data().toInt(); n3 = n3.nextSibling().toElement(); } var.createRect(x, y, w, h); qVariantSetValue(v, var); } else if (e.tagName() == QLatin1String("point")) { QDomElement n3 = e.firstChild().toElement(); int x = 0, y = 0; while (!n3.isNull()) { if (n3.tagName() == QLatin1String("x")) x = n3.firstChild().toText().data().toInt(); else if (n3.tagName() == QLatin1String("y")) y = n3.firstChild().toText().data().toInt(); n3 = n3.nextSibling().toElement(); } var.createPoint(x,y); qVariantSetValue(v, var); } else if (e.tagName() == QLatin1String("size")) { QDomElement n3 = e.firstChild().toElement(); int w = 0, h = 0; while (!n3.isNull()) { if (n3.tagName() == QLatin1String("width")) w = n3.firstChild().toText().data().toInt(); else if (n3.tagName() == QLatin1String("height")) h = n3.firstChild().toText().data().toInt(); n3 = n3.nextSibling().toElement(); } var.createSize(w, h); qVariantSetValue(v, var); } else if (e.tagName() == QLatin1String("color")) { var.color = readColor(e); qVariantSetValue(v, var); } else if (e.tagName() == QLatin1String("font")) { QDomElement n3 = e.firstChild().toElement(); Font f; f.init(); while (!n3.isNull()) { if (n3.tagName() == QLatin1String("family")) f.family = qstrdup(n3.firstChild().toText().data().toLatin1()); else if (n3.tagName() == QLatin1String("pointsize")) f.pointsize = n3.firstChild().toText().data().toInt(); else if (n3.tagName() == QLatin1String("bold")) f.bold = n3.firstChild().toText().data().toInt(); else if (n3.tagName() == QLatin1String("italic")) f.italic = n3.firstChild().toText().data().toInt(); else if (n3.tagName() == QLatin1String("underline")) f.underline = n3.firstChild().toText().data().toInt(); else if (n3.tagName() == QLatin1String("strikeout")) f.strikeout = n3.firstChild().toText().data().toInt(); n3 = n3.nextSibling().toElement(); } var.font = f; qVariantSetValue(v, var); } else if (e.tagName() == QLatin1String("string")) { v = QVariant(e.firstChild().toText().data()); QDomElement n = e; n = n.nextSibling().toElement(); if (n.tagName() == QLatin1String("comment")) comment = n.firstChild().toText().data(); } else if (e.tagName() == QLatin1String("cstring")) { v = QVariant(e.firstChild().toText().data().toAscii()); } else if (e.tagName() == QLatin1String("number")) { bool ok = true; v = QVariant(e.firstChild().toText().data().toInt(&ok)); if (!ok) v = QVariant(e.firstChild().toText().data().toDouble()); } else if (e.tagName() == QLatin1String("bool")) { QString t = e.firstChild().toText().data(); v = QVariant(t == QLatin1String("true") || t == QLatin1String("1")); } else if (e.tagName() == QLatin1String("pixmap")) { v = QVariant(e.firstChild().toText().data()); } else if (e.tagName() == QLatin1String("iconset")) { v = QVariant(e.firstChild().toText().data()); } else if (e.tagName() == QLatin1String("image")) { v = QVariant(e.firstChild().toText().data()); } else if (e.tagName() == QLatin1String("enum")) { v = QVariant(e.firstChild().toText().data()); } else if (e.tagName() == QLatin1String("set")) { v = QVariant(e.firstChild().toText().data()); } else if (e.tagName() == QLatin1String("sizepolicy")) { QDomElement n3 = e.firstChild().toElement(); var.createSizePolicy(); while (!n3.isNull()) { if (n3.tagName() == QLatin1String("hsizetype")) var.sizePolicy.hsizetype = n3.firstChild().toText().data().toInt(); else if (n3.tagName() == QLatin1String("vsizetype")) var.sizePolicy.vsizetype = n3.firstChild().toText().data().toInt(); else if (n3.tagName() == QLatin1String("horstretch")) var.sizePolicy.horstretch = n3.firstChild().toText().data().toInt(); else if (n3.tagName() == QLatin1String("verstretch")) var.sizePolicy.verstretch = n3.firstChild().toText().data().toInt(); n3 = n3.nextSibling().toElement(); } qVariantSetValue(v, var); } else if (e.tagName() == QLatin1String("cursor")) { var.createCursor(e.firstChild().toText().data().toInt()); qVariantSetValue(v, var); } else if (e.tagName() == QLatin1String("stringlist")) { QStringList lst; QDomElement n; for (n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement()) lst << n.firstChild().toText().data(); v = QVariant(lst); } else if (e.tagName() == QLatin1String("date")) { QDomElement n3 = e.firstChild().toElement(); int y, m, d; y = m = d = 0; while (!n3.isNull()) { if (n3.tagName() == QLatin1String("year")) y = n3.firstChild().toText().data().toInt(); else if (n3.tagName() == QLatin1String("month")) m = n3.firstChild().toText().data().toInt(); else if (n3.tagName() == QLatin1String("day")) d = n3.firstChild().toText().data().toInt(); n3 = n3.nextSibling().toElement(); } v = QVariant(QDate(y, m, d)); } else if (e.tagName() == QLatin1String("time")) { QDomElement n3 = e.firstChild().toElement(); int h, m, s; h = m = s = 0; while (!n3.isNull()) { if (n3.tagName() == QLatin1String("hour")) h = n3.firstChild().toText().data().toInt(); else if (n3.tagName() == QLatin1String("minute")) m = n3.firstChild().toText().data().toInt(); else if (n3.tagName() == QLatin1String("second")) s = n3.firstChild().toText().data().toInt(); n3 = n3.nextSibling().toElement(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -