/*
 * This file is part of OpenModelica.
 *
 * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC),
 * c/o Linköpings universitet, Department of Computer and Information Science,
 * SE-58183 Linköping, Sweden.
 *
 * All rights reserved.
 *
 * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF AGPL VERSION 3 LICENSE OR
 * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8.
 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
 * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GNU AGPL
 * VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
 *
 * The OpenModelica software and the OSMC (Open Source Modelica Consortium)
 * Public License (OSMC-PL) are obtained from OSMC, either from the above
 * address, from the URLs:
 * http://www.openmodelica.org or
 * https://github.com/OpenModelica/ or
 * http://www.ida.liu.se/projects/OpenModelica,
 * and in the OpenModelica distribution.
 *
 * GNU AGPL version 3 is obtained from:
 * https://www.gnu.org/licenses/licenses.html#GPL
 *
 * This program is distributed WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
 * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
 *
 * See the full OSMC Public License conditions for more details.
 *
 */

#ifndef LatexCell_H_
#define LatexCell_H_


//QT Headers
#include <QtGlobal>
#include <QtWidgets>

//IAEX Headers
#include "cell.h"
#include "document.h"

namespace IAEX
{
  enum latexCellStates {Finished_l, Eval_l, Error_l, Modified_l};

  class MyTextEdit3;
  class LatexCell : public Cell
  {
    Q_OBJECT

  public:
    LatexCell(Document *doc, QWidget *parent=0);
    virtual ~LatexCell() {};
    QString text() override;
    QString textHtml() override;
    QTextDocument* document() override;
    virtual QString textOutput();
    virtual QString textOutputHtml();
    virtual QTextCursor textCursor() override;
    virtual QTextEdit* textEdit() override;
    virtual QTextEdit* textEditOutput();
    void viewExpression(bool) override;
    void cutText() override;
    void copyText() override;
    void pasteText() override;
    bool findText(const QString &exp, QTextDocument::FindFlags options) override;

    void clearSelection() override;
    void moveCursor(QTextCursor::MoveOperation operation) override;

    virtual void addCellWidgets() override;
    virtual void removeCellWidgets() override;
    virtual void accept(Visitor &v) override;
    virtual bool isClosed() const override;
    virtual bool isEditable() const override;
    virtual bool isEvaluated();

  signals:
    void textChanged();
    void textChanged( bool );
    void clickedOutput( Cell* );
    void forwardAction( int );
    void updatePos(int, int);
    void newState(QString);
    void setStatusMenu(QList<QAction*>);

  public slots:
    void eval(bool silent=false);
    void clickEvent();
    void clickEventOutput();
    void contentChanged();
    void setText(QString text) override;
    void setTextHtml(QString html) override;
    virtual void setTextOutput(QString output);
    virtual void setTextOutputHtml(QString html);
    void setStyle(const QString &stylename) override;
    void setStyle(CellStyle style) override;
    void setChapterCounter(QString number);
    QString ChapterCounter();
    QString ChapterCounterHtml();
    void setReadOnly(bool readonly) override;
    void setEvaluated(bool evaluated);
    void setClosed(bool closed, bool update = true) override;
    virtual void setFocus(bool focus) override;
    virtual void setFocusOutput(bool focus);
    void setExpr(QString);
    void setState(int state);

  protected:
    void resizeEvent(QResizeEvent *event) override;
    void mouseDoubleClickEvent(QMouseEvent *) override;
    void clear();

  private slots:
    void addToHighlighter();
    void charFormatChanged(const QTextCharFormat &);

  private:
    void createLatexCell();
    void createOutputCell();
    void createChapterCounter();
    void setOutputStyle();

  private:
    bool evaluated_;
    bool closed_;
    int oldHeight_;

  public:
    MyTextEdit3* input_;
    MyTextEdit3* output_;

  private:
    QTextBrowser *chaptercounter_;
    QGridLayout *layout_;
    Document *document_;
  };


  // QTextBrowser (browser better when working with images)
  class MyTextEdit3 : public QTextBrowser
  {
    Q_OBJECT

  public:
    MyTextEdit3(QWidget *parent=0);
    virtual ~MyTextEdit3();

    bool isStopingHighlighter();
    int state;

  public slots:
    void updatePosition();
    void setModified();

  signals:
    void clickOnCell();
    void wheelMove( QWheelEvent* );
    void eval();
    void forwardAction( int );
    void updatePos(int, int);
    void setState(int);
    //void showVariableButton(bool);

  protected:
    void mousePressEvent(QMouseEvent *event) override;
    void wheelEvent(QWheelEvent *event) override;
    void keyPressEvent(QKeyEvent *event ) override;
    void insertFromMimeData(const QMimeData *source) override;
    void focusInEvent(QFocusEvent* event) override;

  private:
  };

  class MyAction1: public QAction
  {
    Q_OBJECT
  public:
    MyAction1(const QString& text, QObject* parent);
  public slots:
    void triggered2();
  signals:
    void urlClicked(const QUrl& u);

  };

}
#endif
