From 27811246174d49e772f4da3cc27826c4aef5429b Mon Sep 17 00:00:00 2001 From: Ivan Pelipenko Date: Wed, 24 Feb 2021 17:15:12 +0300 Subject: [PATCH] Graphic legend improvements using scroll area --- CMakeLists.txt | 2 +- libs/graphic/graphic.cpp | 52 ++++++++++++++++++++++---- libs/graphic/graphic.ui | 80 +++++++++++++++++++++++----------------- 3 files changed, 92 insertions(+), 42 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7edd012..298ac56 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_policy(SET CMP0017 NEW) # need include() with .cmake project(qad) set(qad_MAJOR 1) set(qad_MINOR 17) -set(qad_REVISION 0) +set(qad_REVISION 1) set(qad_SUFFIX ) set(qad_COMPANY SHS) set(qad_DOMAIN org.SHS) diff --git a/libs/graphic/graphic.cpp b/libs/graphic/graphic.cpp index 036b13f..8470f91 100644 --- a/libs/graphic/graphic.cpp +++ b/libs/graphic/graphic.cpp @@ -9,6 +9,8 @@ #include #include #include +#include +#include #if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)) # include #endif @@ -25,6 +27,28 @@ const double rad2deg_qpie = 45. / atan(1.); __GraphicRegistrator__ __graphic_registrator__; +class LegendScrollArea: public QScrollArea { +public: + LegendScrollArea(QWidget * w): QScrollArea() { + setFrameShape(QFrame::NoFrame); + setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); + setWidgetResizable(true); + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); + setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents); + setWidget(w); + viewport()->setAutoFillBackground(false); + w->setAutoFillBackground(false); + } +protected: + virtual QSize sizeHint() const { + QSize ret; + if (!widget()) return ret; + ret = widget()->sizeHint(); + return ret; + } +}; + Graphic::Graphic(QWidget * parent): QFrame(parent), canvas(0), line_x_min(this), line_x_max(this), line_y_min(this), line_y_max(this) { canvas_gl = 0; @@ -39,6 +63,8 @@ Graphic::Graphic(QWidget * parent): QFrame(parent), canvas(0), line_x_min(this), #endif ui = new Ui::Graphic(); ui->setupUi(this); + ui->scrollLegend->layout()->addWidget(new LegendScrollArea(ui->widgetLegend)); + ui->scrollLegend->hide(); QActionGroup * agroup = new QActionGroup(this); agroup->addAction(ui->graphic_actionGuidesFree ); agroup->addAction(ui->graphic_actionGuidesTraceX); @@ -143,7 +169,12 @@ void Graphic::changeEvent(QEvent * e) { void Graphic::resizeEvent(QResizeEvent *) { - if (leg_update) updateLegend(false); + if (leg_update) +#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0) + QTimer::singleShot(0, this, [this](){updateLegend(false);}); +#else + updateLegend(false); +#endif } @@ -1530,7 +1561,7 @@ void Graphic::updateLegend(bool es) { p.end(); graphics[i].icon = QIcon(pix); } - if (!ui->widgetLegend->isVisibleTo(this)) { + if (!ui->scrollLegend->isVisibleTo(this)) { if (es) emit graphicSettingsChanged(); return; } @@ -1564,15 +1595,18 @@ void Graphic::updateLegend(bool es) { } int maxcol = qMax(ui->widgetLegend->width() / ps - 1, 1); int row = 0, col = 0; - bool lv = ui->widgetLegend->isVisibleTo(this); - ui->widgetLegend->hide(); + bool lv = ui->scrollLegend->isVisibleTo(this); + ui->scrollLegend->hide(); for (int i = 0; i < graphics.size(); i++) { ui->layoutLegend->addWidget(graphics[i].pb,row,col); graphics[i].pb->show(); col++; if (col > maxcol) {col = 0; row++;} } - ui->widgetLegend->setVisible(lv); + ui->gridLayout->invalidate(); + ui->scrollLegend->setVisible(lv); + ui->scrollLegend->layout()->itemAt(0)->widget()->updateGeometry(); + //ui->gridLayout->invalidate(); leg_update = true; if (es) emit graphicSettingsChanged(); } @@ -1672,7 +1706,7 @@ bool Graphic::statusVisible() const { bool Graphic::legendVisible() const { - return ui->widgetLegend->isVisible(); + return ui->scrollLegend->isVisible(); } @@ -1777,9 +1811,13 @@ void Graphic::setStatusVisible(bool visible) { void Graphic::setLegendVisible(bool visible) { - ui->widgetLegend->setVisible(visible); + ui->scrollLegend->setVisible(visible); ui->graphic_checkLegend->setChecked(visible); +#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0) + QTimer::singleShot(0, this, [this](){updateLegend();}); +#else updateLegend(); +#endif } diff --git a/libs/graphic/graphic.ui b/libs/graphic/graphic.ui index 2de6978..0d00a0f 100644 --- a/libs/graphic/graphic.ui +++ b/libs/graphic/graphic.ui @@ -7,7 +7,7 @@ 0 0 564 - 433 + 484 @@ -32,6 +32,16 @@ 2 + + + + Qt::RichText + + + Qt::AlignCenter + + + @@ -216,7 +226,7 @@ 0 - 79 + 0 @@ -225,19 +235,16 @@ - - - - Qt::RichText - - - Qt::AlignCenter - - - + + + + Cursor: ( ; ) + + + @@ -359,39 +366,44 @@ + + + + + 1 + + + 1 + + + 1 + + + 1 + + + 4 + + + + - - - - Cursor: ( ; ) - - - - - - - 0 - 0 - - - + + - 1 + 0 - 1 + 0 - 1 + 0 - 1 - - - 4 + 0