File indexing completed on 2024-12-01 03:34:40
0001 /* 0002 File : TextLabelTest.cpp 0003 Project : LabPlot 0004 Description : Tests for TextLabel 0005 -------------------------------------------------------------------- 0006 SPDX-FileCopyrightText: 2022 Stefan Gerlach <stefan.gerlach@uni.kn> 0007 0008 SPDX-License-Identifier: GPL-2.0-or-later 0009 */ 0010 0011 #include "TextLabelTest.h" 0012 #include "backend/core/Project.h" 0013 #include "backend/lib/trace.h" 0014 #include "backend/worksheet/TextLabel.h" 0015 #include "backend/worksheet/TextLabelPrivate.h" 0016 #include "kdefrontend/widgets/LabelWidget.h" 0017 0018 struct TextProperties { 0019 QColor fontColor; 0020 QColor backgroundColor; 0021 QString text; 0022 QString plainText; 0023 QFont font; 0024 int weigth; 0025 bool italic; 0026 bool underline; 0027 }; 0028 0029 QColor getColorFromHTMLText(const QString& text, const QString& colortype) { 0030 const QString htmlColorPattern(QStringLiteral("(#[0-9A-Fa-f]{6})|(transparent)")); 0031 QRegularExpression fontColorPattern(colortype + QStringLiteral(":") + htmlColorPattern); 0032 QRegularExpressionMatch matchColor = fontColorPattern.match(text); 0033 // QVERIFY(matchColor.hasMatch()); 0034 // QCOMPARE(matchColor.capturedTexts().count(), 1); 0035 const auto& splitted = matchColor.capturedTexts().at(0).split(colortype + QStringLiteral(":")); 0036 QColor c; 0037 if (splitted.length() > 1) { 0038 QString color = splitted.at(1); 0039 int r = color.mid(1, 2).toInt(nullptr, 16); 0040 int g = color.mid(3, 2).toInt(nullptr, 16); 0041 int b = color.mid(5, 2).toInt(nullptr, 16); 0042 0043 c = QColor(r, g, b); 0044 } else { 0045 c = QColor(Qt::transparent); 0046 } 0047 return c; 0048 } 0049 0050 #define STORETEXTPROPERTIES(label, propertyVariable) \ 0051 TextProperties propertyVariable; \ 0052 { \ 0053 propertyVariable.text = label->text().text; \ 0054 QTextEdit te(label->text().text); \ 0055 propertyVariable.font = te.font(); \ 0056 /* For some reason the two below don't work */ \ 0057 /* propertyVariable.fontColor = te.textColor(); */ \ 0058 /* propertyVariable.backgroundColor = te.textBackgroundColor(); */ \ 0059 propertyVariable.fontColor = getColorFromHTMLText(label->text().text, QStringLiteral("color")); \ 0060 propertyVariable.backgroundColor = getColorFromHTMLText(label->text().text, QStringLiteral("background-color")); \ 0061 propertyVariable.italic = te.fontItalic(); \ 0062 propertyVariable.underline = te.fontUnderline(); \ 0063 propertyVariable.plainText = te.toPlainText(); \ 0064 propertyVariable.weigth = te.fontWeight(); \ 0065 } 0066 0067 #define COMPARETEXTPROPERTIES(actual, expected) \ 0068 QCOMPARE(actual.backgroundColor, expected.backgroundColor); \ 0069 QCOMPARE(actual.fontColor, expected.fontColor); \ 0070 QCOMPARE(actual.font, expected.font); \ 0071 QCOMPARE(actual.italic, expected.italic); \ 0072 QCOMPARE(actual.plainText, expected.plainText); \ 0073 QCOMPARE(actual.underline, expected.underline); \ 0074 QCOMPARE(actual.weigth, expected.weigth); \ 0075 /* QCOMPARE(actual.text, expected.text); Cannot be used, because then also in the expected html text the color must be replaced*/ 0076 0077 #define COMPARETEXTPROPERTIESLABEL(label, expected) \ 0078 { \ 0079 STORETEXTPROPERTIES(label, propertyVariable) \ 0080 COMPARETEXTPROPERTIES(propertyVariable, expected) \ 0081 } 0082 0083 #define VERIFYLABELCOLORS(label, fontcolor_, backgroundColor_) \ 0084 { \ 0085 QCOMPARE(getColorFromHTMLText(label->text().text, QStringLiteral("color")), fontcolor_); \ 0086 QCOMPARE(getColorFromHTMLText(label->text().text, QStringLiteral("background-color")), backgroundColor_); \ 0087 } 0088 0089 void TextLabelTest::addPlot() { 0090 Project project; 0091 auto* ws = new Worksheet(QStringLiteral("worksheet")); 0092 QVERIFY(ws != nullptr); 0093 project.addChild(ws); 0094 0095 auto* p = new CartesianPlot(QStringLiteral("plot")); 0096 QVERIFY(p != nullptr); 0097 ws->addChild(p); 0098 0099 auto* l = new TextLabel(QStringLiteral("Label")); 0100 QVERIFY(l != nullptr); 0101 l->setText(QStringLiteral("TextLabelText")); 0102 ws->addChild(l); 0103 0104 QCOMPARE(l->text().mode, TextLabel::Mode::Text); 0105 VERIFYLABELCOLORS(l, Qt::black, Qt::transparent); 0106 QCOMPARE(l->fontColor(), Qt::black); 0107 QCOMPARE(l->backgroundColor(), Qt::transparent); 0108 0109 // add title? 0110 0111 // add axes? 0112 // check axis label 0113 } 0114 0115 /*! 0116 * \brief TextLabelTest::multiLabelEdit 0117 * Test if changing the background color of one label 0118 * only the background color will be changed for the second 0119 * label, but no other properties 0120 */ 0121 void TextLabelTest::multiLabelEditColorChange() { 0122 Project project; 0123 auto* ws = new Worksheet(QStringLiteral("worksheet")); 0124 QVERIFY(ws != nullptr); 0125 project.addChild(ws); 0126 0127 auto* p = new CartesianPlot(QStringLiteral("plot")); 0128 QVERIFY(p != nullptr); 0129 ws->addChild(p); 0130 0131 auto* l1 = new TextLabel(QStringLiteral("Label")); 0132 QVERIFY(l1 != nullptr); 0133 l1->setText(QStringLiteral("Text1")); 0134 ws->addChild(l1); 0135 0136 VERIFYLABELCOLORS(l1, Qt::black, Qt::transparent); 0137 0138 auto* l2 = new TextLabel(QStringLiteral("Label")); 0139 QVERIFY(l2 != nullptr); 0140 l2->setText(QStringLiteral("Text2")); 0141 ws->addChild(l2); 0142 0143 STORETEXTPROPERTIES(l1, l1InitProperties); 0144 STORETEXTPROPERTIES(l2, l2InitProperties); 0145 0146 LabelWidget w(nullptr); 0147 w.setLabels({l1}); // change only one textlabel 0148 w.fontColorChanged(Qt::red); 0149 w.backgroundColorChanged(Qt::blue); 0150 0151 TextProperties l1p = l1InitProperties; 0152 l1p.fontColor = Qt::red; 0153 l1p.backgroundColor = Qt::blue; 0154 COMPARETEXTPROPERTIESLABEL(l1, l1p); 0155 COMPARETEXTPROPERTIESLABEL(l2, l2InitProperties); 0156 0157 w.setLabels({l1, l2}); 0158 w.fontColorChanged(Qt::cyan); 0159 w.backgroundColorChanged(Qt::green); 0160 0161 l1p = l1InitProperties; 0162 l1p.fontColor = Qt::cyan; 0163 l1p.backgroundColor = Qt::green; 0164 COMPARETEXTPROPERTIESLABEL(l1, l1p); 0165 TextProperties l2p = l2InitProperties; 0166 l2p.fontColor = Qt::cyan; 0167 l2p.backgroundColor = Qt::green; 0168 COMPARETEXTPROPERTIESLABEL(l2, l2p); 0169 } 0170 0171 /*! 0172 * \brief TextLabelTest::multiLabelEditTextChange 0173 * Changing the text of two labels changes the text content and more text related 0174 * properties 0175 */ 0176 void TextLabelTest::multiLabelEditTextChange() { 0177 Project project; 0178 auto* ws = new Worksheet(QStringLiteral("worksheet")); 0179 QVERIFY(ws != nullptr); 0180 project.addChild(ws); 0181 0182 auto* p = new CartesianPlot(QStringLiteral("plot")); 0183 QVERIFY(p != nullptr); 0184 ws->addChild(p); 0185 0186 auto* l1 = new TextLabel(QStringLiteral("Label")); 0187 QVERIFY(l1 != nullptr); 0188 l1->setText(QStringLiteral("Text1")); 0189 ws->addChild(l1); 0190 0191 auto* l2 = new TextLabel(QStringLiteral("Label")); 0192 QVERIFY(l2 != nullptr); 0193 l2->setText(QStringLiteral("Text2")); 0194 ws->addChild(l2); 0195 0196 LabelWidget w(nullptr); 0197 w.setLabels({l1}); // change only one textlabel 0198 w.fontColorChanged(Qt::red); 0199 w.backgroundColorChanged(Qt::blue); 0200 w.fontSuperScriptChanged(true); 0201 w.fontStrikeOutChanged(true); 0202 w.fontUnderlineChanged(true); 0203 w.fontItalicChanged(true); 0204 w.fontBoldChanged(true); 0205 w.fontChanged(QFont(QStringLiteral("AkrutiTml2"))); 0206 0207 STORETEXTPROPERTIES(l1, l1InitProperties); 0208 0209 w.setLabels({l1, l2}); 0210 w.ui.teLabel->setText(QStringLiteral("New text")); 0211 0212 TextProperties l1p = l1InitProperties; 0213 l1p.plainText = QStringLiteral("New text"); 0214 COMPARETEXTPROPERTIESLABEL(l1, l1p); 0215 COMPARETEXTPROPERTIESLABEL(l2, l1p); // textlabel2 received all properties of the first label, because the text content changes 0216 } 0217 0218 void TextLabelTest::multiLabelEditColorChangeSelection() { 0219 Project project; 0220 auto* ws = new Worksheet(QStringLiteral("worksheet")); 0221 QVERIFY(ws != nullptr); 0222 project.addChild(ws); 0223 0224 auto* p = new CartesianPlot(QStringLiteral("plot")); 0225 QVERIFY(p != nullptr); 0226 ws->addChild(p); 0227 0228 LabelWidget w(nullptr); 0229 0230 auto* l1 = new TextLabel(QStringLiteral("Label")); 0231 QVERIFY(l1 != nullptr); 0232 ws->addChild(l1); 0233 l1->setText(QStringLiteral("This is the text of label 1")); 0234 0235 VERIFYLABELCOLORS(l1, Qt::black, Qt::transparent); 0236 0237 auto* l2 = new TextLabel(QStringLiteral("Label")); 0238 QVERIFY(l2 != nullptr); 0239 ws->addChild(l2); 0240 w.setLabels({l2}); // This setlabels is important, otherwise a specific scenario does not occur 0241 l2->setText(QStringLiteral("Text label 2")); 0242 0243 STORETEXTPROPERTIES(l1, l1InitProperties); 0244 STORETEXTPROPERTIES(l2, l2InitProperties); 0245 0246 w.setLabels({l1, l2}); 0247 auto cursor = w.ui.teLabel->textCursor(); 0248 // marks character 2 to 4 0249 cursor.setPosition(2); 0250 cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 2); 0251 w.ui.teLabel->setTextCursor(cursor); 0252 w.fontColorChanged(Qt::cyan); 0253 w.backgroundColorChanged(Qt::green); 0254 w.fontBoldChanged(true); 0255 w.fontItalicChanged(true); 0256 0257 { 0258 QTextEdit te(l1->text().text); 0259 QTextCursor cTest = te.textCursor(); 0260 cTest.setPosition(2); 0261 cTest.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 2); 0262 te.setTextCursor(cTest); 0263 auto ccf = te.currentCharFormat(); 0264 QCOMPARE(ccf.foreground().color(), Qt::cyan); 0265 QCOMPARE(ccf.background().color(), Qt::green); 0266 QCOMPARE(te.fontItalic(), true); 0267 QCOMPARE(te.fontWeight(), QFont::Bold); 0268 QCOMPARE(te.toPlainText(), QStringLiteral("This is the text of label 1")); 0269 } 0270 0271 { 0272 QTextEdit te(l2->text().text); 0273 QTextCursor cTest = te.textCursor(); 0274 cTest.setPosition(2); 0275 cTest.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 2); 0276 te.setTextCursor(cTest); 0277 auto ccf = te.currentCharFormat(); 0278 QCOMPARE(ccf.foreground().color(), Qt::cyan); 0279 QCOMPARE(ccf.background().color(), Qt::green); 0280 QCOMPARE(te.fontItalic(), true); 0281 QCOMPARE(te.fontWeight(), QFont::Bold); 0282 QCOMPARE(te.toPlainText(), QStringLiteral("Text label 2")); 0283 } 0284 0285 w.fontBoldChanged(false); 0286 w.fontItalicChanged(false); 0287 0288 { 0289 QTextEdit te(l1->text().text); 0290 QTextCursor cTest = te.textCursor(); 0291 cTest.setPosition(2); 0292 cTest.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 2); 0293 te.setTextCursor(cTest); 0294 auto ccf = te.currentCharFormat(); 0295 QCOMPARE(ccf.foreground().color(), Qt::cyan); 0296 QCOMPARE(ccf.background().color(), Qt::green); 0297 QCOMPARE(te.fontItalic(), false); 0298 QCOMPARE(te.fontWeight(), QFont::Normal); 0299 QCOMPARE(te.toPlainText(), QStringLiteral("This is the text of label 1")); 0300 } 0301 0302 { 0303 QTextEdit te(l2->text().text); 0304 QTextCursor cTest = te.textCursor(); 0305 cTest.setPosition(2); 0306 cTest.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 2); 0307 te.setTextCursor(cTest); 0308 auto ccf = te.currentCharFormat(); 0309 QCOMPARE(ccf.foreground().color(), Qt::cyan); 0310 QCOMPARE(ccf.background().color(), Qt::green); 0311 QCOMPARE(te.fontItalic(), false); 0312 QCOMPARE(te.fontWeight(), QFont::Normal); 0313 QCOMPARE(te.toPlainText(), QStringLiteral("Text label 2")); 0314 } 0315 } 0316 0317 QTEST_MAIN(TextLabelTest)