192 lines
11 KiB
C++
192 lines
11 KiB
C++
#include "controller/tkrEditorController.h"
|
|
#include "model/tkrTikzDocument.h"
|
|
#include "ui/tkrDiagramScene.h"
|
|
#include "ui/tkrMainWindow.h"
|
|
|
|
#include <QAction>
|
|
#include <QApplication>
|
|
#include <QFile>
|
|
#include <QFontDatabase>
|
|
#include <QGraphicsPathItem>
|
|
#include <QGraphicsView>
|
|
#include <QImage>
|
|
#include <QPainter>
|
|
#include <QPlainTextEdit>
|
|
#include <QStatusBar>
|
|
|
|
using namespace TikzRefinery;
|
|
|
|
namespace {
|
|
|
|
void require(bool bCondition, const char *pMessage)
|
|
{
|
|
if (!bCondition) {
|
|
qFatal("%s", pMessage);
|
|
}
|
|
}
|
|
|
|
struct SegmentExpectation
|
|
{
|
|
QPointF ptFirstTikz;
|
|
QPointF ptSecondTikz;
|
|
QPointF ptFirstScene;
|
|
QPointF ptSecondScene;
|
|
bool bArrowAtStart = false;
|
|
bool bArrowAtEnd = false;
|
|
};
|
|
|
|
void checkCorpus(const QString &strName, const QVector<SegmentExpectation> &vecExpected,
|
|
const QString &strEvidenceDirectory)
|
|
{
|
|
const QString strPath = QStringLiteral(TKR_CORPUS_DIRECTORY) + QLatin1Char('/') + strName + QStringLiteral(".tex");
|
|
QFile fileSource(strPath);
|
|
require(fileSource.open(QIODevice::ReadOnly), "Cannot open segment fixture");
|
|
const QByteArray baOriginal = fileSource.readAll();
|
|
require(fileSource.error() == QFileDevice::NoError, "Cannot read segment fixture");
|
|
tkrTikzDocument document;
|
|
tkrDiagramScene diagramScene;
|
|
tkrEditorController controller(document, diagramScene);
|
|
require(controller.openDocument(strPath).bSucceeded, "Segment file open failed");
|
|
require(document.elements().size() == vecExpected.size()
|
|
&& diagramScene.items().size() == vecExpected.size(), "Wrong segment count or calibration item drawn");
|
|
require(document.sourceBytes() == baOriginal && !document.hasUnsavedChanges(), "Segment display mutated source");
|
|
require(diagramScene.sceneRect() == QRectF(0, 0, 700, 500), "Segment comparison extent mismatch");
|
|
for (qsizetype nElement = 0; nElement < vecExpected.size(); ++nElement) {
|
|
const SegmentExpectation &expected = vecExpected.at(nElement);
|
|
const ElementRecord &elementRecord = document.elements().at(nElement);
|
|
require(elementRecord.uElementId == static_cast<quint64>(nElement + 1)
|
|
&& elementRecord.uDocumentRevision == document.revision(), "Segment identity/revision mismatch");
|
|
require(elementRecord.ptFirstTikzPosition == expected.ptFirstTikz
|
|
&& elementRecord.ptSecondTikzPosition == expected.ptSecondTikz, "Segment source endpoints mismatch");
|
|
require(elementRecord.bArrowAtStart == expected.bArrowAtStart
|
|
&& elementRecord.bArrowAtEnd == expected.bArrowAtEnd, "Arrow direction mismatch");
|
|
require(elementRecord.elementKind == (expected.bArrowAtStart || expected.bArrowAtEnd
|
|
? ElementKind::Arrow : ElementKind::Line), "Line/arrow kind mismatch");
|
|
const SourceRange rangesCoordinates[] = {elementRecord.rangeFirstX, elementRecord.rangeFirstY,
|
|
elementRecord.rangeSecondX, elementRecord.rangeSecondY};
|
|
const qreal rExpected[] = {expected.ptFirstTikz.x(), expected.ptFirstTikz.y(),
|
|
expected.ptSecondTikz.x(), expected.ptSecondTikz.y()};
|
|
for (int iCoordinate = 0; iCoordinate < 4; ++iCoordinate) {
|
|
const SourceRange &rangeSource = rangesCoordinates[iCoordinate];
|
|
bool bConverted = false;
|
|
const qreal rActual = baOriginal.mid(rangeSource.nStart, rangeSource.nLength).toDouble(&bConverted);
|
|
require(bConverted && rActual == rExpected[iCoordinate], "Segment numeric byte range mismatch");
|
|
}
|
|
QGraphicsPathItem *pSegment = nullptr;
|
|
for (QGraphicsItem *pItem : diagramScene.items()) {
|
|
if (pItem->data(0).toULongLong() == elementRecord.uElementId) {
|
|
pSegment = qgraphicsitem_cast<QGraphicsPathItem *>(pItem);
|
|
}
|
|
}
|
|
require(pSegment && pSegment->path().contains((expected.ptFirstScene + expected.ptSecondScene) / 2),
|
|
"Segment shaft not at independent expected scene position");
|
|
require(pSegment->brush().color() == QColor(Qt::black) && pSegment->pen().style() == Qt::NoPen,
|
|
"Segment outline appearance mismatch");
|
|
require(!pSegment->flags().testFlag(QGraphicsItem::ItemIsMovable)
|
|
&& pSegment->flags().testFlag(QGraphicsItem::ItemIsSelectable), "Segment selection/scene-controlled movement mismatch");
|
|
}
|
|
if (!strEvidenceDirectory.isEmpty()) {
|
|
QImage imageCanvas(700, 500, QImage::Format_ARGB32_Premultiplied);
|
|
imageCanvas.fill(Qt::white);
|
|
QPainter painter(&imageCanvas);
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
diagramScene.render(&painter, QRectF(0, 0, 700, 500), QRectF(0, 0, 700, 500));
|
|
painter.end();
|
|
require(imageCanvas.save(strEvidenceDirectory + QLatin1Char('/') + strName + QStringLiteral("-canvas.png")),
|
|
"Cannot save segment canvas evidence");
|
|
}
|
|
// Exercise the same startup-file route used when launching the visible app.
|
|
tkrMainWindow mainWindow(nullptr, strPath);
|
|
mainWindow.show();
|
|
QApplication::processEvents();
|
|
const QGraphicsView *pView = mainWindow.findChild<QGraphicsView *>();
|
|
const QPlainTextEdit *pSourceView = mainWindow.findChild<QPlainTextEdit *>();
|
|
require(pView && pView->scene()->items().size() == vecExpected.size(), "Startup file did not reach window canvas");
|
|
require(pSourceView && pSourceView->isReadOnly()
|
|
&& pSourceView->toPlainText().contains(QStringLiteral("--")), "Startup source not displayed");
|
|
require(mainWindow.statusBar()->currentMessage().startsWith(QStringLiteral("Displayed "))
|
|
&& !mainWindow.isWindowModified(), "Segment window status/clean state mismatch");
|
|
for (const QAction *pAction : mainWindow.findChildren<QAction *>()) {
|
|
if (pAction->shortcut() == QKeySequence(QKeySequence::Save)) {
|
|
require(!pAction->isEnabled(), "Segment display enabled Save");
|
|
}
|
|
}
|
|
if (!strEvidenceDirectory.isEmpty() && strName.endsWith(QStringLiteral("initial"))) {
|
|
require(mainWindow.grab().save(strEvidenceDirectory + QLatin1Char('/') + strName + QStringLiteral("-window.png")),
|
|
"Cannot save segment window evidence");
|
|
}
|
|
mainWindow.close();
|
|
}
|
|
|
|
void checkVariantsAndRefusals()
|
|
{
|
|
tkrTikzParserExplicit parser;
|
|
const QByteArray baSource = QByteArray::fromHex("efbbbf25c3a90d0a")
|
|
+ "\\draw[black, <-, solid, line width = 0.6pt] (+1.25cm, % keep\r\n -.5cm) -- (-2.cm,+3.50cm);\r\n"
|
|
"\\draw (0cm,0cm) -- (0cm,1cm);\r\n"
|
|
"\\draw (1cm,0cm) -- (-1cm,0cm);\r\n"
|
|
"\\draw (0cm,0cm) rectangle (1cm,1cm);\r\n";
|
|
const ParseResult parseResult = parser.parse(baSource, 17);
|
|
require(parseResult.bSafeToEdit && parseResult.vecElements.size() == 4, "Mixed geometry/comments rejected");
|
|
const ElementRecord &elementFirst = parseResult.vecElements.first();
|
|
require(elementFirst.uDocumentRevision == 17 && elementFirst.bArrowAtStart && !elementFirst.bArrowAtEnd,
|
|
"Variant arrow identity/direction mismatch");
|
|
require(baSource.mid(elementFirst.rangeFirstX.nStart, elementFirst.rangeFirstX.nLength) == "+1.25"
|
|
&& baSource.mid(elementFirst.rangeSecondY.nStart, elementFirst.rangeSecondY.nLength) == "+3.50",
|
|
"UTF-8/BOM/comments shifted segment byte offsets");
|
|
tkrTikzDocument document;
|
|
document.loadSource(baSource, QStringLiteral("variants.tex"));
|
|
require(document.applyCoordinateEdits({{elementFirst.rangeSecondY, "+3.50", "4.125", document.revision()}})
|
|
.bSucceeded, "Parsed segment range could not drive guarded edit");
|
|
QByteArray baExpected = baSource;
|
|
baExpected.replace(baSource.indexOf("+3.50"), 5, "4.125");
|
|
require(document.sourceBytes() == baExpected, "Segment coordinate replacement changed unrelated bytes");
|
|
tkrCoordinateMapper mapper;
|
|
require(mapper.configure(QPointF(10, 20), 50).bSucceeded, "Alternate segment mapper rejected");
|
|
tkrDiagramScene diagramScene;
|
|
require(diagramScene.displayElements(parseResult.vecElements, mapper).bSucceeded, "Mixed/vertical/reversed display failed");
|
|
const QList<QByteArray> listUnsupported = {
|
|
"\\draw (0cm,0cm) -- (0cm,0cm);", "\\draw[->,<-] (0cm,0cm) -- (1cm,1cm);",
|
|
"\\draw[-latex] (0cm,0cm) -- (1cm,1cm);", "\\draw[>=stealth,->] (0cm,0cm) -- (1cm,1cm);",
|
|
"\\draw[->] (0cm,0cm) rectangle (1cm,1cm);", "\\draw[dashed] (0cm,0cm) -- (1cm,1cm);",
|
|
"\\draw (0cm,0cm) -- (1cm,1cm) -- (2cm,0cm);", "\\draw (0cm,0cm) -- +(1cm,1cm);",
|
|
"\\draw (0cm,0cm) to (1cm,1cm);", "\\draw (0cm,0cm) .. controls (1cm,0cm) .. (1cm,1cm);",
|
|
"\\begin{scope}[scale=2]\\draw (0cm,0cm) -- (1cm,1cm);\\end{scope}",
|
|
baSource + "\\input{other.tex}"
|
|
};
|
|
for (const QByteArray &baUnsupported : listUnsupported) {
|
|
const ParseResult parseRejected = parser.parse(baUnsupported, 1);
|
|
require(!parseRejected.bSafeToEdit && parseRejected.vecElements.isEmpty()
|
|
&& !parseRejected.listDiagnostics.isEmpty(), "Unsupported segment context accepted/partially displayed");
|
|
}
|
|
const ParseResult parseShort = parser.parse("\\draw[<->] (0cm,0cm) -- (0.001cm,0cm);", 1);
|
|
require(parseShort.bSafeToEdit && !diagramScene.displayElements(parseShort.vecElements, mapper).bSucceeded
|
|
&& diagramScene.items().isEmpty(), "Unsupported short arrow left stale/partial items");
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int iArgumentCount, char *pArguments[])
|
|
{
|
|
QApplication application(iArgumentCount, pArguments);
|
|
#ifdef Q_OS_WIN
|
|
const int iFontId = QFontDatabase::addApplicationFont(
|
|
qEnvironmentVariable("SystemRoot") + QStringLiteral("/Fonts/segoeui.ttf"));
|
|
if (iFontId >= 0) {
|
|
QApplication::setFont(QFont(QFontDatabase::applicationFontFamilies(iFontId).first(), 9));
|
|
}
|
|
#endif
|
|
const QString strEvidenceDirectory = iArgumentCount > 1 ? QString::fromLocal8Bit(pArguments[1]) : QString();
|
|
checkCorpus(QStringLiteral("line-initial"), {{{-1,-0.5},{1,0.5},{100,350},{300,250}}}, strEvidenceDirectory);
|
|
checkCorpus(QStringLiteral("line-translated"), {{{-0.5,0},{1.5,1},{150,300},{350,200}}}, strEvidenceDirectory);
|
|
checkCorpus(QStringLiteral("line-endpoint"), {{{-1,-0.5},{1,1.5},{100,350},{300,150}}}, strEvidenceDirectory);
|
|
const SegmentExpectation backward = {{0,-1},{2,-1},{200,400},{400,400},true,false};
|
|
const SegmentExpectation doubleArrow = {{0,2},{2,2},{200,100},{400,100},true,true};
|
|
checkCorpus(QStringLiteral("arrow-initial"), {{{0,0},{2,1},{200,300},{400,200},false,true}, backward, doubleArrow}, strEvidenceDirectory);
|
|
checkCorpus(QStringLiteral("arrow-translated"), {{{0.5,0.5},{2.5,1.5},{250,250},{450,150},false,true}, backward, doubleArrow}, strEvidenceDirectory);
|
|
checkCorpus(QStringLiteral("arrow-endpoint"), {{{0,0},{2,1.5},{200,300},{400,150},false,true}, backward, doubleArrow}, strEvidenceDirectory);
|
|
checkVariantsAndRefusals();
|
|
qInfo("Line/arrow parsing, byte ranges, scene and startup-window checks passed. Gestures remain untested.");
|
|
return 0;
|
|
}
|