diff --git a/chart_plugin/src/slt_space/ui_slt_widget.cpp b/chart_plugin/src/slt_space/ui_slt_widget.cpp index e543b6134f6e35c955f81ed60229f393a961b481..f289355ad95fc73839cc769bf17c766dd2bbbb26 100755 --- a/chart_plugin/src/slt_space/ui_slt_widget.cpp +++ b/chart_plugin/src/slt_space/ui_slt_widget.cpp @@ -27,6 +27,11 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "ui_slt_widget.h" #include +#include +#include "mgl2/canvas_wnd.h" + +#define MGL_MAX_LINES (INT_MAX - 1000) + namespace chart_plugin { const QString MAIN_WIDGET_STYLE_SHEET = "#scrollAreaWidgetContents,#widget\n{\nbackground-color: rgb(20, 21, 24);\n}"; const QString SCROLLAREA_WIDGET_STYLE_SHEET = "QPushButton{background-color:transparent;" @@ -179,7 +184,7 @@ void chart_plugin::MathGL::wheelEvent(QWheelEvent *event) sum = (x2 + x1) / 2; x1 = sum - diff; x2 = sum + diff; - refresh(); + Refresh(); event->accept(); return; } @@ -197,7 +202,7 @@ void chart_plugin::MathGL::wheelEvent(QWheelEvent *event) ax1 = sum - diff; ax2 = sum + diff; mgl_zoom_axis((HMGL)gr, ax1, ay1, 0, 0, ax2, ay2, 0, 0); - update(); + Update(); event->accept(); } @@ -246,7 +251,7 @@ void MathGL::mouseMoveEvent(QMouseEvent *event) emit phiChanged(static_cast(phi)); posX = x; posY = y; - refresh(); + Refresh(); } void MathGL::mouseReleaseEvent(QMouseEvent *event) @@ -259,4 +264,144 @@ void MathGL::mouseReleaseEvent(QMouseEvent *event) } QMathGL::mouseReleaseEvent(event); } + +void MathGL::Update() +{ + if (draw_func || draw) { + mgl_reset_frames((HMGL)gr); + if (mgl_get_flag((HMGL)gr, MGL_CLF_ON_UPD)) { + mgl_set_def_param((HMGL)gr); + } + mgl_set_alpha((HMGL)gr, alpha); + mgl_set_light((HMGL)gr, light); + if (custDraw) { + emit customDraw(x1, y1, x2, y2, true); + } + Draw(); + } else if (mgl_get_num_frame((HMGL)gr) > 0) { + mgl_set_alpha((HMGL)gr, alpha); + mgl_set_light((HMGL)gr, light); + mgl_get_frame((HMGL)gr, 0); + } + this->AfterPlot(); +} + +void MathGL::Draw() +{ + if (!isHidden()) { + QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); + } + const std::string loc = setlocale(LC_NUMERIC, "C"); + if (mgl_is_frames((HMGL)gr)) { + mgl_new_frame((HMGL)gr); + } + if (draw_func) { + draw_func((HMGL)gr, draw_par); + } else if (draw) { + mglGraph graph((HMGL)gr); + draw->Draw(&graph); + } + if (mgl_is_frames((HMGL)gr)) { + mgl_end_frame((HMGL)gr); + } + setlocale(LC_NUMERIC, loc.c_str()); + gr->AskStop(false); + setMinimumSize(gr->GetWidth(), gr->GetHeight()); +} + +void MathGL::AfterPlot() +{ + emit refreshData(); + emit showWarn(mgl_get_mess((HMGL)gr)); + mousePos = ""; + if (!isHidden()) { + QApplication::restoreOverrideCursor(); + } + this->Refresh(); +} + +void MathGL::Refresh() +{ + if (dotsRefr) { + timerRefr->start(); + gr->SetQuality(MGL_DRAW_DOTS); + } + if (mgl_is_frames((HMGL)gr) && mgl_get_num_frame((HMGL)gr) > 0) { + this->DrawPrim(); + if (custZoom) { + emit customZoom(x1, y1, x2, y2, tet, phi, per); + } else { + mgl_zoom((HMGL)gr, x1, y1, x2, y2); + mgl_ask_perspective((HMGL)gr, per); + if (viewYZ) { + mgl_view((HMGL)gr, 0, -tet, -phi); + } else { + mgl_view((HMGL)gr, -phi, -tet, 0); + } + } + } + + uchar *buffer = nullptr; + const uchar *rgb = mgl_get_rgb((HMGL)gr); + long width = mgl_get_width((HMGL)gr); + long height = mgl_get_height((HMGL)gr); + + const int32_t alpha = 255; + // 4代表rgba4个数据 + buffer = new uchar[4 * width * height]; + for (long i = 0; i < width * height; i++) { + // 将rgb的第3列数据赋值给buffer第1列数据 + buffer[4 * i] = rgb[3 * i + 2]; + // 将rgb的第2列数据赋值给buffer第2列数据 + buffer[4 * i + 1] = rgb[3 * i + 1]; + // 将rgb的第1列数据赋值给buffer第3列数据 + buffer[4 * i + 2] = rgb[3 * i]; + // 将alpha赋值给buffer第4列数据 + buffer[4 * i + 3] = alpha; + } + QImage img(buffer, width, height, QImage::Format_RGB32); + pic = QPixmap::fromImage(img); + + if (pic.size() != size()) { + this->setSize(pic.width(), pic.height()); + } + this->parentWidget()->update(); +} + +void MathGL::DrawPrim() +{ + if (!(HMGL)gr) { + return; + } + mglCanvasWnd *canvas = dynamic_cast((HMGL)gr); + mgl_get_frame((HMGL)gr, canvas ? canvas->GetCurFig() : mgl_get_num_frame((HMGL)gr) - 1); + mglParse parse; + long count = primitives.count('\n'); + mglGraph graph((HMGL)gr); + const std::string locale = setlocale(LC_NUMERIC, "C"); + graph.Push(); + graph.SubPlot(1, 1, 0, "#"); + mglPoint min = gr->Min; + mglPoint max = gr->Max; + // 范围从(-1,-1,-1)点到(1,1,1)点 + graph.SetRanges(mglPoint(-1, -1, -1), mglPoint(1, 1, 1)); + for (long i = 0; i < count; i++) { + mgl_set_obj_id((HMGL)gr, i + MGL_MAX_LINES); + parse.Parse(&graph, primitives.section('\n', i, i).toLocal8Bit().constData(), i + MGL_MAX_LINES); + } + graph.SetRanges(min, max); + graph.Pop(); + setlocale(LC_NUMERIC, locale.c_str()); +} + +void MathGL::SetSize(int32_t width, int32_t height) +{ + resize(width, height); + if (width != pic.width() || height != pic.height()) { + mgl_set_size((HMGL)gr, width, height); + this->Update(); + setMinimumSize(gr->GetWidth(), gr->GetHeight()); + } +} + } // namespace chart_plugin diff --git a/chart_plugin/src/slt_space/ui_slt_widget.h b/chart_plugin/src/slt_space/ui_slt_widget.h index a08263d657566a51e3fb5ec5dcebc3f370328ba2..c2b0e4870810802c794f4ba250616d964944f371 100755 --- a/chart_plugin/src/slt_space/ui_slt_widget.h +++ b/chart_plugin/src/slt_space/ui_slt_widget.h @@ -132,12 +132,27 @@ public: explicit MathGL(QWidget *parent = nullptr, Qt::WindowFlags flag = 0) : QMathGL(parent, flag) {} ~MathGL() override = default; + /* + * 功能:设置窗口大小 + * 输入参数:width-宽度,height-高度 + * 输出参数:无 + * 返回值:无 + */ + void SetSize(int32_t width, int32_t height); + protected: void wheelEvent(QWheelEvent *event) override; void mousePressEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override; +private: + void Update(); + void Draw(); + void AfterPlot(); + void Refresh(); + void DrawPrim(); + private: int32_t posX {}; int32_t posY {}; diff --git a/chart_plugin/src/slt_space_display.cpp b/chart_plugin/src/slt_space_display.cpp index 28e663820f30c3c585aee2d4898def5f51954c25..fc848c4dd7a40b718948e441c858edf72732bdfc 100755 --- a/chart_plugin/src/slt_space_display.cpp +++ b/chart_plugin/src/slt_space_display.cpp @@ -63,7 +63,7 @@ bool SltSpaceDisplay::eventFilter(QObject *obj, QEvent *e) return QObject::eventFilter(obj, e); } - ui.GetQMathGl()->setSize(ui.GetQMathGl()->parentWidget()->width(), ui.GetQMathGl()->parentWidget()->height()); + dynamic_cast(ui.GetQMathGl())->SetSize(ui.GetQMathGl()->parentWidget()->width(), ui.GetQMathGl()->parentWidget()->height()); UpdateLegend(); return true; }