{"size": 8128, "ext": "js", "max_stars_repo_path": "XUXUCAM/Resource/Drawing/Draw/Ellipse/EllipseInscribedQuad/EllipseInscribedQuad.js", "max_stars_repo_name": "hananiahhsu/OpenCAD", "max_stars_repo_stars_event_min_datetime": "2018-11-06T12:37:42.000Z", "max_stars_repo_stars_event_max_datetime": "2021-07-22T11:49:35.000Z", "max_issues_repo_path": "XUXUCAM/Resource/Drawing/Draw/Ellipse/EllipseInscribedQuad/EllipseInscribedQuad.js", "max_issues_repo_name": "hananiahhsu/OpenCAD", "max_issues_repo_issues_event_min_datetime": "2020-05-15T07:21:06.000Z", "max_issues_repo_issues_event_max_datetime": "2020-05-28T05:10:35.000Z", "max_forks_repo_path": "XUXUCAM/Resource/Drawing/Draw/Ellipse/EllipseInscribedQuad/EllipseInscribedQuad.js", "max_forks_repo_name": "hananiahhsu/OpenCAD", "max_forks_repo_forks_event_min_datetime": "2018-11-06T12:37:52.000Z", "max_forks_repo_forks_event_max_datetime": "2021-09-25T08:02:27.000Z"}
{"max_stars_count": 13, "max_issues_count": 1, "max_forks_count": 11, "avg_line_length": 28.0275862069, "max_line_length": 73, "alphanum_fraction": 0.6328740157}
/**
 * Copyright (c) 2011-2017 by Andrew Mustun. All rights reserved.
 * 
 * This file is part of the QCAD project.
 *
 * QCAD is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * QCAD is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with QCAD.
 */

include("../Ellipse.js");
include("scripts/ShapeAlgorithms.js");

/**
 * \class EllipseInscribedQuad
 * \brief Circle tangential to three entities (lines, arcs, circles).
 * \ingroup ecma_draw_circle
 */
function EllipseInscribedQuad(guiAction) {
    Ellipse.call(this, guiAction);

    this.entity1 = undefined;
    this.shape1 = undefined;
    this.pos1 = undefined;

    this.entity2 = undefined;
    this.shape2 = undefined;
    this.pos2 = undefined;

    this.entity3 = undefined;
    this.shape3 = undefined;
    this.pos3 = undefined;

    this.entity4 = undefined;
    this.shape4 = undefined;
    this.pos4 = undefined;
}

EllipseInscribedQuad.prototype = new Ellipse();

EllipseInscribedQuad.State = {
    ChoosingShape1 : 0,
    ChoosingShape2 : 1,
    ChoosingShape3 : 2,
    ChoosingShape4 : 3
};

EllipseInscribedQuad.prototype.beginEvent = function() {
    Ellipse.prototype.beginEvent.call(this);

    this.setState(EllipseInscribedQuad.State.ChoosingShape1);
};

EllipseInscribedQuad.prototype.setState = function(state) {
    Ellipse.prototype.setState.call(this, state);

    var di = this.getDocumentInterface();
    this.setCrosshairCursor();
    di.setClickMode(RAction.PickEntity);

    var appWin = RMainWindowQt.getMainWindow();
    switch (this.state) {
    case EllipseInscribedQuad.State.ChoosingShape1:
        this.entity1 = undefined;
        this.shape1 = undefined;
        this.entity2 = undefined;
        this.shape2 = undefined;
        this.entity3 = undefined;
        this.shape3 = undefined;
        this.entity4 = undefined;
        this.shape4 = undefined;
        this.pos4 = undefined;
        var trFirstEntity = qsTr("First line");
        this.setCommandPrompt(trFirstEntity);
        this.setLeftMouseTip(trFirstEntity);
        this.setRightMouseTip(EAction.trCancel);
        break;

    case EllipseInscribedQuad.State.ChoosingShape2:
        this.entity2 = undefined;
        this.shape2 = undefined;
        this.entity3 = undefined;
        this.shape3 = undefined;
        this.entity4 = undefined;
        this.shape4 = undefined;
        this.pos4 = undefined;
        var trSecondEntity = qsTr("Second line");
        this.setCommandPrompt(trSecondEntity);
        this.setLeftMouseTip(trSecondEntity);
        this.setRightMouseTip(EAction.trBack);
        break;

    case EllipseInscribedQuad.State.ChoosingShape3:
        this.entity3 = undefined;
        this.entity3Id = undefined;
        this.shape3 = undefined;
        this.entity4 = undefined;
        this.shape4 = undefined;
        this.pos4 = undefined;
        var trThirdEntity = qsTr("Third line");
        this.setCommandPrompt(trThirdEntity);
        this.setLeftMouseTip(trThirdEntity);
        this.setRightMouseTip(EAction.trBack);
        break;

    case EllipseInscribedQuad.State.ChoosingShape4:
        this.entity4 = undefined;
        this.shape4 = undefined;
        this.pos4 = undefined;
        var trFourthEntity = qsTr("Fourth line");
        this.setCommandPrompt(trFourthEntity);
        this.setLeftMouseTip(trFourthEntity);
        this.setRightMouseTip(EAction.trBack);
        break;
    }

    this.simulateMouseMoveEvent();
};

EllipseInscribedQuad.prototype.escapeEvent = function() {
    switch (this.state) {
    case EllipseInscribedQuad.State.ChoosingShape1:
        EAction.prototype.escapeEvent.call(this);
        break;

    case EllipseInscribedQuad.State.ChoosingShape2:
        this.setState(EllipseInscribedQuad.State.ChoosingShape1);
        break;

    case EllipseInscribedQuad.State.ChoosingShape3:
        this.setState(EllipseInscribedQuad.State.ChoosingShape2);
        break;

    case EllipseInscribedQuad.State.ChoosingShape4:
        this.setState(EllipseInscribedQuad.State.ChoosingShape3);
        break;
    }
};

EllipseInscribedQuad.prototype.pickEntity = function(event, preview) {
    this.error = "";
    var di = this.getDocumentInterface();
    var doc = this.getDocument();
    var entityId = this.getEntityId(event, preview);
    var entity = doc.queryEntity(entityId);
    var pos = event.getModelPosition();
    var shape = undefined;

    if (!isNull(entity)) {
        shape = entity.getClosestSimpleShape(pos);

        if (!isLineBasedShape(shape)) {
            if (!preview) {
                EAction.warnNotLine();
                return;
            }
            shape = undefined;
        }
    }

    switch (this.state) {
    case EllipseInscribedQuad.State.ChoosingShape1:
        this.entity1 = entity;
        this.shape1 = shape;
        this.pos1 = pos;

        if (preview) {
            this.updatePreview();
        }
        else {
            this.setState(EllipseInscribedQuad.State.ChoosingShape2);
        }
        break;

    case EllipseInscribedQuad.State.ChoosingShape2:
        this.entity2 = entity;
        this.shape2 = shape;
        this.pos2 = pos;

        if (preview) {
            this.updatePreview();
        }
        else {
            this.setState(EllipseInscribedQuad.State.ChoosingShape3);
        }
        break;

    case EllipseInscribedQuad.State.ChoosingShape3:
        this.entity3 = entity;
        this.shape3 = shape;
        this.pos3 = pos;

        if (preview) {
            this.updatePreview();
        }
        else {
            this.setState(EllipseInscribedQuad.State.ChoosingShape4);
        }
        break;


    case EllipseInscribedQuad.State.ChoosingShape4:
        this.entity4 = entity;
        this.shape4 = shape;
        this.pos4 = pos;

        if (preview) {
            this.updatePreview();
        }
        else {
            var op = this.getOperation(false);
            if (!isNull(op)) {
                di.applyOperation(op);
                this.setState(EllipseInscribedQuad.State.ChoosingShape1);
            }
        }
        break;
    }

    if (!preview && this.error.length!==0) {
        EAction.handleUserWarning(this.error);
    }
};

EllipseInscribedQuad.prototype.getOperation = function(preview) {
    if (isNull(this.shape1) || isNull(this.shape2) ||
        isNull(this.shape3) || isNull(this.shape4)) {
        return undefined;
    }

    var shape = ShapeAlgorithms.createEllipseInscribedFromLines(
        this.shape1, this.shape2, this.shape3, this.shape4);
    if (isNull(shape)) {
        return undefined;
    }

    var doc = this.getDocument();

    var e = shapeToEntity(doc, shape);
    if (isNull(e)) {
        return undefined;
    }

    return new RAddObjectOperation(e, this.getToolTitle());
};

EllipseInscribedQuad.prototype.getHighlightedEntities = function() {
    var ret = [];
    if (isEntity(this.entity1)) {
        ret.push(this.entity1.getId());
    }
    if (isEntity(this.entity2)) {
        ret.push(this.entity2.getId());
    }
    if (isEntity(this.entity3)) {
        ret.push(this.entity3.getId());
    }
    if (isEntity(this.entity4)) {
        ret.push(this.entity4.getId());
    }
    return ret;
};

EllipseInscribedQuad.prototype.getAuxPreview = function() {
    if (isNull(this.shape1) || isNull(this.shape2) ||
        isNull(this.shape3) || isNull(this.shape4)) {
        return [];
    }
    var quad = ShapeAlgorithms.getQuadrilateral(
        this.shape1, this.shape2, this.shape3, this.shape4);
    if (isNull(quad)) {
        return [];
    }

    var edges = [];
    for (var i=0; i<4; i++) {
        edges[i] = new RLine(quad[i], quad[(i+1)%4]);
    }
    return edges;
};
