📄 q3stylesheet.cpp
字号:
\row \i \c{<code>...</code>} \i Indicates code. By default this is the same as \c{<tt>...</tt>} (typewriter). For larger chunks of code use the block-tag \c{<}\c{pre>}. \row \i \c{<tt>...</tt>} \i Typewriter font style. \row \i \c{<font>...</font>} \i Customizes the font size, family and text color. The tag understands the following attributes: \list \i \c color -- The text color, for example \c color="red" or \c color="#FF0000". \i \c size -- The logical size of the font. Logical sizes 1 to 7 are supported. The value may either be absolute (for example, \c size=3) or relative (\c size=-2). In the latter case the sizes are simply added. \i \c face -- The family of the font, for example \c face=times. \endlist \endtable Special elements are: \table \header \i Special tags \i Notes \row \i \c{<img>} \i An image. The image name for the mime source factory is given in the source attribute, for example \c{<img src="qt.xpm">} The image tag also understands the attributes \c width and \c height that determine the size of the image. If the pixmap does not fit the specified size it will be scaled automatically (by using QImage::smoothScale()). The \c align attribute determines where the image is placed. By default, an image is placed inline just like a normal character. Specify \c left or \c right to place the image at the respective side. \row \i \c{<hr>} \i A horizontal line. \row \i \c{<br>} \i A line break. \row \i \c{<nobr>...</nobr>} \i No break. Prevents word wrap. \endtable In addition, rich text supports simple HTML tables. A table consists of one or more rows each of which contains one or more cells. Cells are either data cells or header cells, depending on their content. Cells which span rows and columns are supported. \table \header \i Table tags \i Notes \row \i \c{<table>...</table>} \i A table. Tables support the following attributes: \list \i \c bgcolor -- The background color. \i \c width -- The table width. This is either an absolute pixel width or a relative percentage of the table's width, for example \c width=80%. \i \c border -- The width of the table border. The default is 0 (= no border). \i \c cellspacing -- Additional space around the table cells. The default is 2. \i \c cellpadding -- Additional space around the contents of table cells. The default is 1. \endlist \row \i \c{<tr>...</tr>} \i A table row. This is only valid within a \c table. Rows support the following attribute: \list \i \c bgcolor -- The background color. \endlist \row \i \c{<th>...</th>} \i A table header cell. Similar to \c td, but defaults to center alignment and a bold font. \row \i \c{<td>...</td>} \i A table data cell. This is only valid within a \c tr. Cells support the following attributes: \list \i \c bgcolor -- The background color. \i \c width -- The cell width. This is either an absolute pixel width or a relative percentage of table's width, for example \c width=50%. \i \c colspan -- Specifies how many columns this cell spans. The default is 1. \i \c rowspan -- Specifies how many rows this cell spans. The default is 1. \i \c align -- Qt::Alignment; possible values are \c left, \c right, and \c center. The default is \c left. \i \c valign -- Qt::Vertical alignment; possible values are \c top, \c middle, and \c bottom. The default is \c middle. \endlist \endtable*//*! Creates a style sheet called \a name, with parent \a parent. Like any QObject it will be deleted when its parent is destroyed (if the child still exists). By default the style sheet has the tag definitions defined above.*/Q3StyleSheet::Q3StyleSheet(QObject *parent, const char *name) : QObject(parent){ setObjectName(QLatin1String(name)); init();}/*! Destroys the style sheet. All styles inserted into the style sheet will be deleted.*/Q3StyleSheet::~Q3StyleSheet(){ QHash<QString, Q3StyleSheetItem *>::iterator it = styles.begin(); while (it != styles.end()) { delete it.value(); ++it; }}/*! \internal Initialized the style sheet to the basic Qt style.*/void Q3StyleSheet::init(){ nullstyle = new Q3StyleSheetItem(this, QString::fromLatin1("")); Q3StyleSheetItem *style; style = new Q3StyleSheetItem(this, QLatin1String("qml")); // compatibility style->setDisplayMode(Q3StyleSheetItem::DisplayBlock); style = new Q3StyleSheetItem(this, QString::fromLatin1("qt")); style->setDisplayMode(Q3StyleSheetItem::DisplayBlock); style = new Q3StyleSheetItem(this, QString::fromLatin1("a")); style->setAnchor(true); style = new Q3StyleSheetItem(this, QString::fromLatin1("em")); style->setFontItalic(true); style = new Q3StyleSheetItem(this, QString::fromLatin1("i")); style->setFontItalic(true); style = new Q3StyleSheetItem(this, QString::fromLatin1("big")); style->setLogicalFontSizeStep(1); style = new Q3StyleSheetItem(this, QString::fromLatin1("large")); // compatibility style->setLogicalFontSizeStep(1); style = new Q3StyleSheetItem(this, QString::fromLatin1("small")); style->setLogicalFontSizeStep(-1); style = new Q3StyleSheetItem(this, QString::fromLatin1("strong")); style->setFontWeight(QFont::Bold); style = new Q3StyleSheetItem(this, QString::fromLatin1("b")); style->setFontWeight(QFont::Bold); style = new Q3StyleSheetItem(this, QString::fromLatin1("h1")); style->setFontWeight(QFont::Bold); style->setLogicalFontSize(6); style->setDisplayMode(Q3StyleSheetItem::DisplayBlock); style-> setMargin(Q3StyleSheetItem::MarginTop, 18); style-> setMargin(Q3StyleSheetItem::MarginBottom, 12); style = new Q3StyleSheetItem(this, QString::fromLatin1("h2")); style->setFontWeight(QFont::Bold); style->setLogicalFontSize(5); style->setDisplayMode(Q3StyleSheetItem::DisplayBlock); style-> setMargin(Q3StyleSheetItem::MarginTop, 16); style-> setMargin(Q3StyleSheetItem::MarginBottom, 12); style = new Q3StyleSheetItem(this, QString::fromLatin1("h3")); style->setFontWeight(QFont::Bold); style->setLogicalFontSize(4); style->setDisplayMode(Q3StyleSheetItem::DisplayBlock); style-> setMargin(Q3StyleSheetItem::MarginTop, 14); style-> setMargin(Q3StyleSheetItem::MarginBottom, 12); style = new Q3StyleSheetItem(this, QString::fromLatin1("h4")); style->setFontWeight(QFont::Bold); style->setLogicalFontSize(3); style->setDisplayMode(Q3StyleSheetItem::DisplayBlock); style-> setMargin(Q3StyleSheetItem::MarginVertical, 12); style = new Q3StyleSheetItem(this, QString::fromLatin1("h5")); style->setFontWeight(QFont::Bold); style->setLogicalFontSize(2); style->setDisplayMode(Q3StyleSheetItem::DisplayBlock); style-> setMargin(Q3StyleSheetItem::MarginTop, 12); style-> setMargin(Q3StyleSheetItem::MarginBottom, 4); style = new Q3StyleSheetItem(this, QString::fromLatin1("p")); style->setDisplayMode(Q3StyleSheetItem::DisplayBlock); style-> setMargin(Q3StyleSheetItem::MarginVertical, 12); style->setSelfNesting(false); style = new Q3StyleSheetItem(this, QString::fromLatin1("center")); style->setDisplayMode(Q3StyleSheetItem::DisplayBlock); style->setAlignment(Qt::AlignCenter); style = new Q3StyleSheetItem(this, QString::fromLatin1("twocolumn")); style->setDisplayMode(Q3StyleSheetItem::DisplayBlock); style->setNumberOfColumns(2); style = new Q3StyleSheetItem(this, QString::fromLatin1("multicol")); style->setDisplayMode(Q3StyleSheetItem::DisplayBlock); (void) new Q3StyleSheetItem(this, QString::fromLatin1("font")); style = new Q3StyleSheetItem(this, QString::fromLatin1("ul")); style->setDisplayMode(Q3StyleSheetItem::DisplayBlock); style->setListStyle(Q3StyleSheetItem::ListDisc); style-> setMargin(Q3StyleSheetItem::MarginVertical, 12); style->setMargin(Q3StyleSheetItem::MarginLeft, 40); style = new Q3StyleSheetItem(this, QString::fromLatin1("ol")); style->setDisplayMode(Q3StyleSheetItem::DisplayBlock); style->setListStyle(Q3StyleSheetItem::ListDecimal); style-> setMargin(Q3StyleSheetItem::MarginVertical, 12); style->setMargin(Q3StyleSheetItem::MarginLeft, 40); style = new Q3StyleSheetItem(this, QString::fromLatin1("li")); style->setDisplayMode(Q3StyleSheetItem::DisplayListItem); style->setSelfNesting(false); style = new Q3StyleSheetItem(this, QString::fromLatin1("code")); style->setFontFamily(QString::fromLatin1("Courier New,courier")); style = new Q3StyleSheetItem(this, QString::fromLatin1("tt")); style->setFontFamily(QString::fromLatin1("Courier New,courier")); new Q3StyleSheetItem(this, QString::fromLatin1("img")); new Q3StyleSheetItem(this, QString::fromLatin1("br")); new Q3StyleSheetItem(this, QString::fromLatin1("hr")); style = new Q3StyleSheetItem(this, QString::fromLatin1("sub")); style->setVerticalAlignment(Q3StyleSheetItem::VAlignSub); style = new Q3StyleSheetItem(this, QString::fromLatin1("sup")); style->setVerticalAlignment(Q3StyleSheetItem::VAlignSuper); style = new Q3StyleSheetItem(this, QString::fromLatin1("pre")); style->setFontFamily(QString::fromLatin1("Courier New,courier")); style->setDisplayMode(Q3StyleSheetItem::DisplayBlock); style->setWhiteSpaceMode(Q3StyleSheetItem::WhiteSpacePre); style-> setMargin(Q3StyleSheetItem::MarginVertical, 12); style = new Q3StyleSheetItem(this, QString::fromLatin1("blockquote")); style->setDisplayMode(Q3StyleSheetItem::DisplayBlock); style->setMargin(Q3StyleSheetItem::MarginHorizontal, 40); style = new Q3StyleSheetItem(this, QString::fromLatin1("head")); style->setDisplayMode(Q3StyleSheetItem::DisplayNone); style = new Q3StyleSheetItem(this, QString::fromLatin1("body")); style->setDisplayMode(Q3StyleSheetItem::DisplayBlock); style = new Q3StyleSheetItem(this, QString::fromLatin1("div")); style->setDisplayMode(Q3StyleSheetItem::DisplayBlock) ; style = new Q3StyleSheetItem(this, QString::fromLatin1("span")); style = new Q3StyleSheetItem(this, QString::fromLatin1("dl")); style-> setMargin(Q3StyleSheetItem::MarginVertical, 8); style->setDisplayMode(Q3StyleSheetItem::DisplayBlock); style = new Q3StyleSheetItem(this, QString::fromLatin1("dt")); style->setDisplayMode(Q3StyleSheetItem::DisplayBlock); style->setContexts(QString::fromLatin1("dl")); style = new Q3StyleSheetItem(this, QString::fromLatin1("dd")); style->setDisplayMode(Q3StyleSheetItem::DisplayBlock); style->setMargin(Q3StyleSheetItem::MarginLeft, 30); style->setContexts(QString::fromLatin1("dt dl")); style = new Q3StyleSheetItem(this, QString::fromLatin1("u")); style->setFontUnderline(true); style = new Q3StyleSheetItem(this, QString::fromLatin1("s")); style->setFontStrikeOut(true); style = new Q3StyleSheetItem(this, QString::fromLatin1("nobr")); style->setWhiteSpaceMode(Q3StyleSheetItem::WhiteSpaceNoWrap); // compatibily with some minor 3.0.x Qt versions that had an // undocumented <wsp> tag. ### Remove 3.1 style = new Q3StyleSheetItem(this, QString::fromLatin1("wsp")); style->setWhiteSpaceMode(Q3StyleSheetItem::WhiteSpacePre); // tables style = new Q3StyleSheetItem(this, QString::fromLatin1("table")); style = new Q3StyleSheetItem(this, QString::fromLatin1("tr")); style->setContexts(QString::fromLatin1("table")); style = new Q3StyleSheetItem(this, QString::fromLatin1("td")); style->setContexts(QString::fromLatin1("tr")); style = new Q3StyleSheetItem(this, QString::fromLatin1("th")); style->setFontWeight(QFont::Bold); style->setAlignment(Qt::AlignCenter); style->setContexts(QString::fromLatin1("tr")); style = new Q3StyleSheetItem(this, QString::fromLatin1("html"));}static Q3StyleSheet* defaultsheet = 0;static Q3SingleCleanupHandler<Q3StyleSheet> qt_cleanup_stylesheet;/*! Returns the application-wide default style sheet. This style sheet is used by rich text rendering classes such as QSimpleRichText, QWhatsThis and QMessageBox to define the rendering style and available tags within rich text documents. It also serves as the initial style sheet for the more complex render widgets, QTextEdit and QTextBrowser. \sa setDefaultSheet()*/Q3StyleSheet* Q3StyleSheet::defaultSheet(){ if (!defaultsheet) { defaultsheet = new Q3StyleSheet(); qt_cleanup_stylesheet.set(&defaultsheet); } return defaultsheet;}/*! Sets the application-wide default style sheet to \a sheet, deleting any style sheet previously set. The ownership is transferred to Q3StyleSheet. \sa defaultSheet()*/void Q3StyleSheet::setDefaultSheet(Q3StyleSheet* sheet){ if (defaultsheet != sheet) { if (defaultsheet) qt_cleanup_stylesheet.reset(); delete defaultsheet; } defaultsheet = sheet; if (defaultsheet) qt_cleanup_stylesheet.set(&defaultsheet);}/*!\internal Inserts \a style. Any tags generated after this time will be bound to this style. Note that \a style becomes owned by the style sheet and will be deleted when the style sheet is destroyed.*/void Q3StyleSheet::insert(Q3StyleSheetItem* style){ styles.insert(style->name(), style);}/*! Returns the style called \a name or 0 if there is no such style.*/Q3StyleSheetItem* Q3StyleSheet::item(const QString& name){ if (name.isNull()) return 0; return styles.value(name);}/*! \overload Returns the style called \a name or 0 if there is no such style (const version)*/const Q3StyleSheetItem* Q3StyleSheet::item(const QString& name) const{ if (name.isNull()) return 0; return styles.value(name);}/*! Auxiliary function. Converts the plain text string \a plain to a rich text formatted paragraph while preserving most of its look. \a mode defines the whitespace mode. Possible values are \c Q3StyleSheetItem::WhiteSpacePre (no wrapping, all whitespaces preserved) and Q3StyleSheetItem::WhiteSpaceNormal (wrapping, simplified whitespaces). \sa escape()*/QString Q3StyleSheet::convertFromPlainText(const QString& plain, Q3StyleSheetItem::WhiteSpaceMode mode){ return Qt::convertFromPlainText(plain, Qt::WhiteSpaceMode(mode));}/*! Auxiliary function. Converts the plain text string \a plain to a rich text formatted string with any HTML meta-characters escaped. \sa convertFromPlainText()*/QString Q3StyleSheet::escape(const QString& plain){ return Qt::escape(plain);}// Must doc this enum somewhere, and it is logically related to Q3StyleSheet/*! Returns true if the string \a text is likely to be rich text; otherwise returns false. This function uses a fast and therefore simple heuristic. It mainly checks whether there is something that looks like a tag before the first line break. Although the result may be correct for common cases, there is no guarantee.*/bool Q3StyleSheet::mightBeRichText(const QString& text){ return Qt::mightBeRichText(text);}/*! \fn void Q3StyleSheet::error(const QString& msg) const This virtual function is called when an error occurs when processing rich text. Reimplement it if you need to catch error messages. Errors might occur if some rich text strings contain tags that are not understood by the stylesheet, if some tags are nested incorrectly, or if tags are not closed properly. \a msg is the error message.*/void Q3StyleSheet::error(const QString&) const{}/*! Scales the font \a font to the appropriate physical point size corresponding to the logical font size \a logicalSize. When calling this function, \a font has a point size corresponding to the logical font size 3. Logical font sizes range from 1 to 7, with 1 being the smallest. \sa Q3StyleSheetItem::logicalFontSize(), Q3StyleSheetItem::logicalFontSizeStep(), QFont::setPointSize() */void Q3StyleSheet::scaleFont(QFont& font, int logicalSize) const{ if (logicalSize < 1) logicalSize = 1; if (logicalSize > 7) logicalSize = 7; int baseSize = font.pointSize(); bool pixel = false; if (baseSize == -1) { baseSize = font.pixelSize(); pixel = true; } int s; switch (logicalSize) { case 1: s = 7*baseSize/10; break; case 2: s = (8 * baseSize) / 10; break; case 4: s = (12 * baseSize) / 10; break; case 5: s = (15 * baseSize) / 10; break; case 6: s = 2 * baseSize; break; case 7: s = (24 * baseSize) / 10; break; default: s = baseSize; } if (pixel) font.setPixelSize(s); else font.setPointSize(s);}#endif // QT_NO_RICHTEXT
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -