From 4aa4c56a3542d330089ffc7582825ef6f0be3f1e Mon Sep 17 00:00:00 2001 From: peri4 Date: Sun, 29 Oct 2023 20:03:46 +0300 Subject: [PATCH] graphic fix --- libs/graphic/graphic.cpp | 37 ++++++++++++++++++++--------------- libs/graphic/graphic_conf.cpp | 15 +++++++++++++- libs/graphic/graphic_conf.h | 4 +++- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/libs/graphic/graphic.cpp b/libs/graphic/graphic.cpp index 5297c48..8a200b9 100644 --- a/libs/graphic/graphic.cpp +++ b/libs/graphic/graphic.cpp @@ -186,6 +186,7 @@ Graphic::Graphic(QWidget * parent): QFrame(parent), canvas(0), line_x_min(this), text_color = palette().color(QPalette::WindowText); grid_pen = QPen(palette().color(QPalette::Disabled, QPalette::WindowText), 0., Qt::DotLine); graphics.append(GraphicType()); + graphics.back().init(); curGraphic = 0; selpen = palette().color(QPalette::WindowText); selpen.setStyle(Qt::DashLine); @@ -207,6 +208,8 @@ Graphic::Graphic(QWidget * parent): QFrame(parent), canvas(0), line_x_min(this), Graphic::~Graphic() { + for (auto & g: graphics) + g.destroy(); #ifdef NO_BUTTONS delete buttons_menu; #endif @@ -1260,6 +1263,7 @@ void Graphic::setCurrentGraphic(int arg) { void Graphic::setGraphicsCount(int count, bool update) { if (count < 0) return; + bool changed = false; while (graphics.size() < count) { GraphicType gt(tr("y(x)")); if (loaded_configs.size() > graphics.size()) @@ -1267,12 +1271,14 @@ void Graphic::setGraphicsCount(int count, bool update) { else gt.pen.setColor(QColor::fromHsv((graphics.size() * 55) % 360, 255, 255 - _G_QRAND_ % 115)); graphics.append(gt); + changed = true; } while (graphics.size() > count) { - delete graphics.back().pb; + graphics.back().destroy(); graphics.pop_back(); + changed = true; } - if (update) updateLegend(); + if (update && changed) updateLegend(); } #undef _G_QRAND_ @@ -1280,7 +1286,7 @@ void Graphic::setGraphicsCount(int count, bool update) { void Graphic::removeGraphic(int arg, bool update) { if (arg < 0 || arg >= graphics.size()) return; - delete graphics[arg].pb; + graphics[arg].destroy(); graphics.remove(arg); if (update) updateLegend(); } @@ -2418,24 +2424,24 @@ void Graphic::updateLegend(bool es) { QLayoutItem * li = ui->layoutLegend->itemAtPosition(r, c); if (!li) continue; if (!li->widget()) continue; - while (li->widget()->actions().isEmpty()) - li->widget()->removeAction(li->widget()->actions()[0]); - delete li->widget(); + ui->layoutLegend->removeWidget(li->widget()); } ui->layoutLegend->invalidate(); for (int i = 0; i < graphics.size(); i++) { - graphics[i].pb = new QCheckBox(graphics[i].name); + if (graphics[i].init()) { + QAction * act = new QAction(tr("Check all"), 0); + act->setCheckable(true); + act->setChecked(true); + graphics[i].pb->addAction(act); + connect(act, SIGNAL(triggered(bool)), this, SLOT(graphicAllVisibleChange(bool))); + connect(graphics[i].pb, SIGNAL(toggled(bool)), this, SLOT(graphicVisibleChange(bool))); + graphics[i].pb->setContextMenuPolicy(Qt::ActionsContextMenu); + } graphics[i].pb->setIconSize(pix.size()); graphics[i].pb->setIcon(graphics[i].icon); graphics[i].pb->setChecked(graphics[i].visible); + graphics[i].pb->setText(graphics[i].name); graphics[i].pb->setProperty("graphic_num", i); - graphics[i].pb->setContextMenuPolicy(Qt::ActionsContextMenu); - QAction * act = new QAction(tr("Check all"), 0); - act->setCheckable(true); - act->setChecked(true); - graphics[i].pb->addAction(act); - connect(act, SIGNAL(triggered(bool)), this, SLOT(graphicAllVisibleChange(bool))); - connect(graphics[i].pb, SIGNAL(toggled(bool)), this, SLOT(graphicVisibleChange(bool))); int cps = graphics[i].pb->sizeHint().width() + 4; if (cps > ps) ps = cps; } @@ -2446,8 +2452,7 @@ void Graphic::updateLegend(bool es) { ui->scrollLegend->hide(); for (int i = 0; i < graphics.size(); i++) { ui->layoutLegend->addWidget(graphics[i].pb, row, col); - QCheckBox * check = graphics[i].pb; - check->show(); + graphics[i].pb->show(); if (leg_sa->minimum_hei == 0) { leg_sa->minimum_hei = ui->widgetLegend->sizeHint().height(); } diff --git a/libs/graphic/graphic_conf.cpp b/libs/graphic/graphic_conf.cpp index f2383ed..dfda63b 100644 --- a/libs/graphic/graphic_conf.cpp +++ b/libs/graphic/graphic_conf.cpp @@ -22,7 +22,6 @@ GraphicType::GraphicType(QString name_, QColor color, Qt::PenStyle style, double name = name_; visible = visible_; pointWidth = 2.; - pb = new QCheckBox(name); } @@ -34,6 +33,20 @@ void GraphicType::removeData() { } +bool GraphicType::init() { + if (pb) return false; + pb = new QCheckBox(name); + return true; +} + + +void GraphicType::destroy() { + if (!pb) return; + delete pb; + pb = nullptr; +} + + // GraphicConf GraphicConf::GraphicConf(QVector & graphics_, QWidget * parent): QDialog(parent), graphics(graphics_) { diff --git a/libs/graphic/graphic_conf.h b/libs/graphic/graphic_conf.h index 7892d5c..12d0182 100644 --- a/libs/graphic/graphic_conf.h +++ b/libs/graphic/graphic_conf.h @@ -40,6 +40,8 @@ struct QAD_GRAPHIC_EXPORT GraphicType { double width = 0., bool visible_ = true); void removeData(); + bool init(); + void destroy(); //~GraphicType() {delete pb;} QString name; QPolygonF polyline; @@ -54,7 +56,7 @@ struct QAD_GRAPHIC_EXPORT GraphicType { double pointWidth; double max_x; double max_x_pause; - QCheckBox * pb; + QCheckBox * pb = nullptr; QIcon icon; bool visible; QRectF cvrect;