[42529] | 1 | /* $Id: UIRichTextString.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[55477] | 3 | * VBox Qt GUI - UIRichTextString class implementation.
|
---|
[42529] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[106061] | 7 | * Copyright (C) 2015-2024 Oracle and/or its affiliates.
|
---|
[42529] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
| 11 | *
|
---|
| 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
| 25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[42529] | 26 | */
|
---|
| 27 |
|
---|
| 28 | /* Qt includes: */
|
---|
[76606] | 29 | #include <QApplication>
|
---|
| 30 | #include <QPalette>
|
---|
[103362] | 31 | #include <QRegularExpression>
|
---|
[42529] | 32 |
|
---|
| 33 | /* GUI includes: */
|
---|
[76606] | 34 | #include "UIRichTextString.h"
|
---|
[42529] | 35 |
|
---|
[55476] | 36 | /* Other VBox includes: */
|
---|
[76606] | 37 | #include "iprt/assert.h"
|
---|
[55476] | 38 |
|
---|
[52730] | 39 |
|
---|
[72167] | 40 | const QString UIRichTextString::s_strAny = QString("[\\s\\S]*");
|
---|
| 41 | const QMap<UIRichTextString::Type, QString> UIRichTextString::s_patterns = populatePatterns();
|
---|
| 42 | const QMap<UIRichTextString::Type, bool> UIRichTextString::s_doPatternHasMeta = populatePatternHasMeta();
|
---|
| 43 |
|
---|
| 44 | UIRichTextString::UIRichTextString(Type enmType /* = Type_None */)
|
---|
| 45 | : m_enmType(enmType)
|
---|
[55476] | 46 | , m_strString(QString())
|
---|
| 47 | , m_strStringMeta(QString())
|
---|
| 48 | {
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[72167] | 51 | UIRichTextString::UIRichTextString(const QString &strString, Type enmType /* = Type_None */, const QString &strStringMeta /* = QString() */)
|
---|
| 52 | : m_enmType(enmType)
|
---|
[55476] | 53 | , m_strString(strString)
|
---|
| 54 | , m_strStringMeta(strStringMeta)
|
---|
| 55 | {
|
---|
| 56 | //printf("Creating new UIRichTextString with string=\"%s\" and string-meta=\"%s\"\n",
|
---|
[58866] | 57 | // m_strString.toUtf8().constData(), m_strStringMeta.toUtf8().constData());
|
---|
[55476] | 58 | parse();
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | UIRichTextString::~UIRichTextString()
|
---|
| 62 | {
|
---|
| 63 | /* Erase the map: */
|
---|
| 64 | qDeleteAll(m_strings.begin(), m_strings.end());
|
---|
| 65 | m_strings.clear();
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | QString UIRichTextString::toString() const
|
---|
| 69 | {
|
---|
| 70 | /* Add own string first: */
|
---|
| 71 | QString strString = m_strString;
|
---|
[55477] | 72 |
|
---|
[55476] | 73 | /* Add all the strings of children finally: */
|
---|
| 74 | foreach (const int &iPosition, m_strings.keys())
|
---|
| 75 | strString.insert(iPosition, m_strings.value(iPosition)->toString());
|
---|
[55477] | 76 |
|
---|
[55476] | 77 | /* Return result: */
|
---|
| 78 | return strString;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[88445] | 81 | QVector<QTextLayout::FormatRange> UIRichTextString::formatRanges(int iShift /* = 0 */) const
|
---|
[55476] | 82 | {
|
---|
| 83 | /* Prepare format range list: */
|
---|
[88445] | 84 | QVector<QTextLayout::FormatRange> ranges;
|
---|
[55477] | 85 |
|
---|
[55476] | 86 | /* Add own format range first: */
|
---|
| 87 | QTextLayout::FormatRange range;
|
---|
| 88 | range.start = iShift;
|
---|
| 89 | range.length = toString().size();
|
---|
[72167] | 90 | range.format = textCharFormat(m_enmType);
|
---|
[55476] | 91 | /* Enable anchor if present: */
|
---|
| 92 | if (!m_strAnchor.isNull())
|
---|
| 93 | {
|
---|
| 94 | range.format.setAnchorHref(m_strAnchor);
|
---|
| 95 | /* Highlight anchor if hovered: */
|
---|
| 96 | if (range.format.anchorHref() == m_strHoveredAnchor)
|
---|
| 97 | range.format.setForeground(qApp->palette().color(QPalette::Link));
|
---|
| 98 | }
|
---|
| 99 | ranges.append(range);
|
---|
[55477] | 100 |
|
---|
[55476] | 101 | /* Add all the format ranges of children finally: */
|
---|
| 102 | foreach (const int &iPosition, m_strings.keys())
|
---|
| 103 | ranges.append(m_strings.value(iPosition)->formatRanges(iShift + iPosition));
|
---|
[55477] | 104 |
|
---|
[55476] | 105 | /* Return result: */
|
---|
| 106 | return ranges;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | void UIRichTextString::setHoveredAnchor(const QString &strHoveredAnchor)
|
---|
| 110 | {
|
---|
| 111 | /* Define own hovered anchor first: */
|
---|
| 112 | m_strHoveredAnchor = strHoveredAnchor;
|
---|
[55477] | 113 |
|
---|
[55476] | 114 | /* Propagate hovered anchor to children finally: */
|
---|
| 115 | foreach (const int &iPosition, m_strings.keys())
|
---|
| 116 | m_strings.value(iPosition)->setHoveredAnchor(m_strHoveredAnchor);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | void UIRichTextString::parse()
|
---|
| 120 | {
|
---|
| 121 | /* Assign the meta to anchor directly for now,
|
---|
| 122 | * will do a separate parsing when there will
|
---|
| 123 | * be more than one type of meta: */
|
---|
| 124 | if (!m_strStringMeta.isNull())
|
---|
| 125 | m_strAnchor = m_strStringMeta;
|
---|
| 126 |
|
---|
| 127 | /* Parse the passed QString with all the known patterns: */
|
---|
[72167] | 128 | foreach (const Type &enmPattern, s_patterns.keys())
|
---|
[55476] | 129 | {
|
---|
| 130 | /* Get the current pattern: */
|
---|
[72167] | 131 | const QString strPattern = s_patterns.value(enmPattern);
|
---|
[55476] | 132 |
|
---|
| 133 | /* Recursively parse the string: */
|
---|
| 134 | int iMaxLevel = 0;
|
---|
| 135 | do
|
---|
| 136 | {
|
---|
| 137 | /* Search for the maximum level of the current pattern: */
|
---|
| 138 | iMaxLevel = searchForMaxLevel(m_strString, strPattern, strPattern);
|
---|
| 139 | //printf(" Maximum level for the pattern \"%s\" is %d.\n",
|
---|
[58866] | 140 | // strPattern.toUtf8().constData(), iMaxLevel);
|
---|
[55476] | 141 | /* If current pattern of at least level 1 is found: */
|
---|
| 142 | if (iMaxLevel > 0)
|
---|
| 143 | {
|
---|
| 144 | /* Compose full pattern of the corresponding level: */
|
---|
| 145 | const QString strFullPattern = composeFullPattern(strPattern, strPattern, iMaxLevel);
|
---|
[58866] | 146 | //printf(" Full pattern: %s\n", strFullPattern.toUtf8().constData());
|
---|
[103362] | 147 | QRegularExpression re(strFullPattern);
|
---|
| 148 | re.setPatternOptions(QRegularExpression::InvertedGreedinessOption);
|
---|
| 149 | const QRegularExpressionMatch mt = re.match(m_strString);
|
---|
| 150 | const int iPosition = mt.capturedStart();
|
---|
[55476] | 151 | AssertReturnVoid(iPosition != -1);
|
---|
[103362] | 152 | /* Cut the found string: */
|
---|
| 153 | m_strString.remove(iPosition, mt.capturedLength());
|
---|
| 154 | /* And paste that string as our child: */
|
---|
| 155 | const bool fPatterHasMeta = s_doPatternHasMeta.value(enmPattern);
|
---|
| 156 | const QString strSubString = !fPatterHasMeta ? mt.captured(1) : mt.captured(2);
|
---|
| 157 | const QString strSubMeta = !fPatterHasMeta ? QString() : mt.captured(1);
|
---|
| 158 | m_strings.insert(iPosition, new UIRichTextString(strSubString, enmPattern, strSubMeta));
|
---|
[55476] | 159 | }
|
---|
| 160 | }
|
---|
| 161 | while (iMaxLevel > 0);
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | /* static */
|
---|
| 166 | QMap<UIRichTextString::Type, QString> UIRichTextString::populatePatterns()
|
---|
| 167 | {
|
---|
| 168 | QMap<Type, QString> patterns;
|
---|
| 169 | patterns.insert(Type_Anchor, QString("<a href=([^>]+)>(%1)</a>"));
|
---|
| 170 | patterns.insert(Type_Bold, QString("<b>(%1)</b>"));
|
---|
| 171 | patterns.insert(Type_Italic, QString("<i>(%1)</i>"));
|
---|
| 172 | return patterns;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | /* static */
|
---|
| 176 | QMap<UIRichTextString::Type, bool> UIRichTextString::populatePatternHasMeta()
|
---|
| 177 | {
|
---|
| 178 | QMap<Type, bool> patternHasMeta;
|
---|
| 179 | patternHasMeta.insert(Type_Anchor, true);
|
---|
| 180 | patternHasMeta.insert(Type_Bold, false);
|
---|
| 181 | patternHasMeta.insert(Type_Italic, false);
|
---|
| 182 | return patternHasMeta;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | /* static */
|
---|
| 186 | int UIRichTextString::searchForMaxLevel(const QString &strString, const QString &strPattern,
|
---|
| 187 | const QString &strCurrentPattern, int iCurrentLevel /* = 0 */)
|
---|
| 188 | {
|
---|
[103362] | 189 | QRegularExpression re(strCurrentPattern.arg(s_strAny));
|
---|
| 190 | re.setPatternOptions(QRegularExpression::InvertedGreedinessOption);
|
---|
| 191 | const QRegularExpressionMatch mt = re.match(strString);
|
---|
| 192 | if (mt.hasMatch())
|
---|
[55476] | 193 | return searchForMaxLevel(strString, strPattern,
|
---|
[72167] | 194 | strCurrentPattern.arg(s_strAny + strPattern + s_strAny),
|
---|
[55476] | 195 | iCurrentLevel + 1);
|
---|
| 196 | return iCurrentLevel;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | /* static */
|
---|
| 200 | QString UIRichTextString::composeFullPattern(const QString &strPattern,
|
---|
| 201 | const QString &strCurrentPattern, int iCurrentLevel)
|
---|
| 202 | {
|
---|
| 203 | if (iCurrentLevel > 1)
|
---|
| 204 | return composeFullPattern(strPattern,
|
---|
[72167] | 205 | strCurrentPattern.arg(s_strAny + strPattern + s_strAny),
|
---|
[55476] | 206 | iCurrentLevel - 1);
|
---|
[72167] | 207 | return strCurrentPattern.arg(s_strAny);
|
---|
[55476] | 208 | }
|
---|
| 209 |
|
---|
| 210 | /* static */
|
---|
[72167] | 211 | QTextCharFormat UIRichTextString::textCharFormat(Type enmType)
|
---|
[55476] | 212 | {
|
---|
| 213 | QTextCharFormat format;
|
---|
[72167] | 214 | switch (enmType)
|
---|
[55476] | 215 | {
|
---|
| 216 | case Type_Anchor:
|
---|
| 217 | {
|
---|
| 218 | format.setAnchor(true);
|
---|
| 219 | break;
|
---|
| 220 | }
|
---|
| 221 | case Type_Bold:
|
---|
| 222 | {
|
---|
| 223 | QFont font = format.font();
|
---|
| 224 | font.setBold(true);
|
---|
| 225 | format.setFont(font);
|
---|
| 226 | break;
|
---|
| 227 | }
|
---|
| 228 | case Type_Italic:
|
---|
| 229 | {
|
---|
| 230 | QFont font = format.font();
|
---|
| 231 | font.setItalic(true);
|
---|
| 232 | format.setFont(font);
|
---|
| 233 | break;
|
---|
| 234 | }
|
---|
[63318] | 235 |
|
---|
| 236 | case Type_None: break; /* Shut up MSC */
|
---|
[55476] | 237 | }
|
---|
| 238 | return format;
|
---|
| 239 | }
|
---|