Testing latest pari + WASM + node.js... and it works?! Wow.
License: GPL3
ubuntu2004
/* Copyright (C) 2000 The PARI group.12This file is part of the PARI/GP package.34PARI/GP is free software; you can redistribute it and/or modify it under the5terms of the GNU General Public License as published by the Free Software6Foundation; either version 2 of the License, or (at your option) any later7version. It is distributed in the hope that it will be useful, but WITHOUT8ANY WARRANTY WHATSOEVER.910Check the License for details. You should have received a copy of it, along11with the package; see the file 'COPYING'. If not, write to the Free Software12Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */13/////////////////////////////////////////////////////////////////////////////14//15// High resolution plot using Trolltech's Qt library16//17// You may possibly want to use this file with a "Qt Free Edition"18// which is distributed under the terms of the Q PUBLIC LICENSE (QPL),19// or with a "Qt/Embedded Free Edition" which is20// distributed under the terms of the GNU General Public License (GPL).21// Please check http://www.trolltech.com for details.22//23// ---Nils-Peter Skoruppa (www.countnumber.de)24/////////////////////////////////////////////////////////////////////////////25#include <Qt/qapplication.h>26#include <Qt/qwidget.h>27#include <Qt/qpainter.h>28#include <Qt/qcolor.h>29#include <Qt/qdesktopwidget.h>30#include <Qt/qevent.h>31#include <Qt/qpixmap.h>32#include <Qt/qsignalmapper.h>33#include <Qt/qimage.h>34#include <Qt/qimagewriter.h>35#include <Qt/qmainwindow.h>36#include <Qt/qmenubar.h>37#include <Qt/qtoolbar.h>38#include <Qt/qaction.h>39#include <Qt/qfiledialog.h>40#include <Qt/qmessagebox.h>41#include <Qt/qfile.h>42#include <Qt/qstatusbar.h>43#include <Qt/qimage.h>44#include <Qt/qlabel.h>45#include <Qt/qspinbox.h>46#include <Qt/qlayout.h>4748extern "C" {49#include "pari.h"50#include "rect.h"51#undef grem52}5354using namespace Qt;5556class Plotter: public QWidget {5758Q_OBJECT5960signals:61void clicked();6263protected:64void mouseReleaseEvent(QMouseEvent*);6566public:67Plotter(PARI_plot *T, GEN w, GEN x, GEN y, QWidget* parent = 0);68void save(const QString& s = *plotFile + ".xpm",69const QString& f = QString("XPM"));7071protected:72void paintEvent(QPaintEvent *);7374private:75PARI_plot *T;76GEN w, x, y;77QColor color0;78QFont font;79static QString *plotFile;80void draw(QPainter *p);81};8283QString *Plotter::plotFile = new QString("pariplot");8485static QColor86rgb_color(long c)87{88int r, g, b; long_to_rgb(c, &r, &g, &b);89return QColor(r, g, b);90}91Plotter::Plotter(PARI_plot *T, GEN w, GEN x, GEN y, QWidget* parent)92: QWidget(parent), font("lucida", 9) {93this->w = w;94this->x = x;95this->y = y;96this->T = T;97this->setFont(font);98{99int r, g, b;100color_to_rgb(gel(GP_DATA->colormap,1), &r, &g, &b);101color0 = QColor(r, g, b);102}103QPalette palette;104palette.setColor(backgroundRole(), color0);105setPalette(palette);106}107108static void109SetForeground(void *data, long col)110{111QPainter *p = (QPainter *)data;112p->setPen(rgb_color(col));113}114115static void116DrawPoint(void *data, long x, long y)117{118QPainter *p = (QPainter *)data;119p->drawPoint(x, y);120}121122static void123DrawLine(void *data, long x1, long y1, long x2, long y2)124{125QPainter *p = (QPainter *)data;126p->drawLine(x1, y1, x2, y2);127}128129static void130DrawRectangle(void *data, long x, long y, long w, long h)131{132QPainter *p = (QPainter *)data;133p->drawRect(x, y, w, h);134}135136static void137FillRectangle(void *data, long x, long y, long w, long h)138{139QPainter *p = (QPainter *)data;140p->fillRect(x, y, w, h, p->pen().color());141}142143static void144DrawPoints(void *data, long nb, struct plot_points *pt)145{146QPainter *p = (QPainter *)data;147QPolygon xp = QPolygon(nb);148long i;149for (i = 0;i < nb; i++) xp.setPoint(i,pt[i].x, pt[i].y);150p->drawPoints(xp);151}152153static void154DrawLines(void *data, long nb, struct plot_points *pt)155{156QPainter *p = (QPainter *)data;157QPolygon xp = QPolygon(nb);158long i;159for (i = 0;i < nb; i++) xp.setPoint(i, pt[i].x, pt[i].y);160p->drawPolyline(xp);161}162163static void164DrawString(void *data, long x, long y, char *text, long numtext)165{166QPainter *p = (QPainter *)data;167p->drawText(x, y, QString(text).left(numtext));168}169170void171Plotter::draw(QPainter *p)172{173struct plot_eng plotQt;174plotQt.sc=&SetForeground;175plotQt.pt=&DrawPoint;176plotQt.ln=&DrawLine;177plotQt.bx=&DrawRectangle;178plotQt.fb=&FillRectangle;179plotQt.mp=&DrawPoints;180plotQt.mp=&DrawPoints;181plotQt.ml=&DrawLines;182plotQt.st=&DrawString;183plotQt.pl=T;184plotQt.data=(void *)p;185double xs = double(this->width()) / T->width;186double ys = double(this->height()) / T->height;187gen_draw(&plotQt, this->w, this->x, this->y, xs, ys);188}189190void191Plotter::save(const QString& s, const QString& f)192{193QPixmap pm(this->width(), this->height());194QPainter p;195p.begin(&pm);196p.initFrom(this);197p.fillRect(0, 0, pm.width(), pm.height(), color0);198draw(&p);199p.end();200pm.save(s, f.toAscii().data());201}202203void204Plotter::paintEvent(QPaintEvent *)205{206QPainter p;207p.begin(this);208this->draw(&p);209p.end();210}211212void Plotter::mouseReleaseEvent(QMouseEvent*) { emit clicked(); }213214/* XPM */215static const char * const fullscreen_xpm[] = {216"14 14 2 1",217" c None",218". c #000000",219"..............",220". .. .",221". .. .",222". .... .",223". .. .",224". . .. . .",225"..............",226"..............",227". . .. . .",228". .. .",229". .... .",230". .. .",231". .. .",232".............."};233234class PlotWindow: public QMainWindow235{236Q_OBJECT237238public:239PlotWindow(PARI_plot *T, GEN w, GEN x, GEN y, QWidget* parent = 0);240~PlotWindow();241242protected:243void resizeEvent(QResizeEvent *);244245private slots:246void fullScreen();247void normalView();248void save();249void save(int);250251private:252static const QList<QByteArray> file_formats;253Plotter *plr;254QString saveFileName;255int saveFileFormat;256QLabel *res;257QMenu* menuFile;258QMenu* menuView;259QMenu* menuFormat;260QAction* quitAction;261QAction* saveAction;262QAction* fullScreenAction;263QSignalMapper* signalMapper;264QIcon* icon;265};266267const QList<QByteArray> PlotWindow::file_formats = QImageWriter::supportedImageFormats();268269PlotWindow::PlotWindow(PARI_plot *T, GEN w, GEN x, GEN y, QWidget* parent)270: QMainWindow(parent), saveFileName("pariplot"), saveFileFormat(0)271{272setWindowTitle("Pari QtPlot");273274QPalette palette;275palette.setColor(this->backgroundRole(), white);276this->setPalette(palette);277278menuFile = menuBar()->addMenu("&File");279280saveAction = new QAction("&Save",this);281saveAction->setShortcut(QKeySequence(CTRL+Key_S));282connect (saveAction, SIGNAL(triggered()), this, SLOT(save()));283menuFile->addAction(saveAction);284menuFormat = menuFile->addMenu("Save &as");285286signalMapper = new QSignalMapper(this);287288for(int i = 0; i < file_formats.count(); i++)289{290QAction* tmpAction;291tmpAction = new QAction(QString(file_formats.at(i)),this);292connect (tmpAction, SIGNAL(triggered()), signalMapper, SLOT(map()));293signalMapper->setMapping(tmpAction,i);294menuFormat->addAction(tmpAction);295}296297connect (signalMapper, SIGNAL(mapped(int)), this,SLOT(save(int)));298299quitAction = new QAction("&Quit",this);300quitAction->setShortcut(QKeySequence(CTRL+Key_Q));301connect (quitAction, SIGNAL(triggered()), this, SLOT(close()));302menuFile->addAction(quitAction);303304menuView = menuBar()->addMenu("&View");305306fullScreenAction = new QAction("Use &full screen", this);307fullScreenAction->setShortcut(QKeySequence(CTRL+Key_F));308icon = new QIcon();309icon->addPixmap(QPixmap((const char ** )fullscreen_xpm));310fullScreenAction->setIcon(*icon);311connect(fullScreenAction, SIGNAL(triggered()), this, SLOT(fullScreen()));312menuView->addAction(fullScreenAction);313314// Setting up an instance of plotter315plr = new Plotter(T, w, x, y, this);316connect(plr, SIGNAL(clicked()), this, SLOT(normalView()));317this->setCentralWidget(plr);318319this->resize(T->width, T->height + 24);320res = new QLabel();321statusBar()->addWidget(res);322}323324PlotWindow::~PlotWindow() {}325326void327PlotWindow::resizeEvent(QResizeEvent *e)328{329QMainWindow::resizeEvent(e);330res->setText(QString("Resolution: ") +331QString::number(plr->width()) + "x" +332QString::number(plr->height()));333res->setFixedSize(res->sizeHint());334}335336void337PlotWindow::fullScreen()338{339plr->setParent(0);340plr->showMaximized();341plr->show();342}343344void345PlotWindow::normalView()346{347if (!plr->parentWidget())348{349plr->setParent(this);350this->setCentralWidget(plr);351plr->show();352}353}354355void356PlotWindow::save()357{358QString ff = QString(file_formats.at(saveFileFormat));359QString fn = saveFileName + "." + ff.toLower();360plr->save(fn, ff);361setWindowTitle(QString("Pari QtPlot:") + fn);362}363364void365PlotWindow::save(int id)366{367QString ff(file_formats.at(id));368QString s(ff + " (*." + ff.toLower() +");;All (*)");369QString fn = QFileDialog::getSaveFileName(this, saveFileName + "." + ff,370saveFileName + "." + ff, s);371if (!fn.isEmpty())372{373saveFileName = fn;374int p;375if ((p = saveFileName.lastIndexOf("." + ff, -1)) >= 0)376saveFileName.truncate(p);377saveFileFormat = id;378save();379}380}381382#include "plotQt4.moc.cpp"383384static void385draw(PARI_plot *T, GEN w, GEN x, GEN y)386{387if (pari_daemon()) return; // parent process returns388389// launch Qt window390int argc = 1; // set argc = 2 for cross391const char * argv[] = { "gp", "-qws"}; // development using qvfb392QApplication a(argc, (char**) argv);393PlotWindow *win = new PlotWindow(T, w, x, y);394win->show();395a.exec();396pari_close(); exit(0);397}398399INLINE void400gp_get_display_sizes(long *dwidth, long *dheight, long *fwidth, long *fheight)401{402/* There must be an easier way to get desktop size... */403int argc = 1;404const char * argv[] = { "gp", "-qws"};405QApplication a(argc, (char**) argv);406QDesktopWidget *qw = new QDesktopWidget();407if (qw)408{409QRect rec = qw->screenGeometry();410*dwidth = rec.width(); // screen width411*dheight = rec.height(); // and height412}413else414{415*dwidth = 0;416*dheight = 0;417}418*fwidth = 6; // font width419*fheight = 9; // and height420}421422void423gp_get_plot(PARI_plot *T)424{425gp_get_plot_generic(T,gp_get_display_sizes);426T->draw = &draw;427}428429430