Skip to content

Commit

Permalink
Add QChart to Python Chart (4c of 5)
Browse files Browse the repository at this point in the history
Fixup stack view a) minimum height for plots, b) added a
scroll area to manage more plots than fit on the screen
and will layout horizontally or vertically.
  • Loading branch information
liversedge committed Mar 2, 2020
1 parent 85cbbc3 commit 876ce83
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 7 deletions.
80 changes: 75 additions & 5 deletions src/Charts/GenericChart.cpp
Expand Up @@ -24,6 +24,7 @@
#include "Utils.h"

#include <limits>
#include <QScrollArea>

//
// The generic chart manages collections of genericplots
Expand All @@ -39,14 +40,67 @@
//
// Charts have a minimum width and height that needs to be
// honoured too, since multiple series stacked can get
// cramped, so these are placed into a scroll area XXX TODO
// cramped, so these are placed into a scroll area
//
GenericChart::GenericChart(QWidget *parent, Context *context) : QWidget(parent), context(context)
{
// intitialise state info
mainLayout = new QVBoxLayout(this);
// for scrollarea, since we see a little of it.
QPalette palette;
palette.setBrush(QPalette::Background, QBrush(GColor(CRIDEPLOTBACKGROUND)));

// main layout for widget
QVBoxLayout *main=new QVBoxLayout(this);
main->setSpacing(0);
main->setContentsMargins(0,0,0,0);

// main layout used for charts
mainLayout = new QVBoxLayout();
mainLayout->setSpacing(0);
mainLayout->setContentsMargins(0,0,0,0);
lrLayout = new QHBoxLayout();
mainLayout->addLayout(lrLayout);

// stack for scrolling
QWidget *stackWidget = new QWidget(this);
stackWidget->setAutoFillBackground(false);
stackWidget->setLayout(mainLayout);
stackWidget->setPalette(palette);

// put everything inside a scrollarea
stackFrame = new QScrollArea(this);
#ifdef Q_OS_WIN
QStyle *cde = QStyleFactory::create(OS_STYLE);
stackFrame->setStyle(cde);
#endif
stackFrame->setAutoFillBackground(false);
stackFrame->setWidgetResizable(true);
stackFrame->setFrameStyle(QFrame::NoFrame);
stackFrame->setContentsMargins(0,0,0,0);
stackFrame->setPalette(palette);
main->addWidget(stackFrame);
stackFrame->setWidget(stackWidget);

// watch for color/themes change
connect(context, SIGNAL(configChanged(qint32)), this, SLOT(configChanged(qint32)));

configChanged(0);
}

void
GenericChart::configChanged(qint32)
{
setUpdatesEnabled(false);

setProperty("color", GColor(CPLOTBACKGROUND));
QPalette palette;
palette.setBrush(QPalette::Background, QBrush(GColor(CRIDEPLOTBACKGROUND)));
setPalette(palette); // propagates to children

// set style sheets
#ifndef Q_OS_MAC
stackFrame->setStyleSheet(TabView::ourStyleSheet());
#endif
setUpdatesEnabled(true);
}

// set chart settings
Expand Down Expand Up @@ -157,17 +211,33 @@ GenericChart::finaliseChart()
// first we mark all existing plots as deleteme
for(int i=0; i<currentPlots.count(); i++) currentPlots[i].state = GenericPlotInfo::deleteme;

// source and target layouts
QBoxLayout *source, *target;
if (orientation == Qt::Vertical) {
source = lrLayout;
target = mainLayout;
} else {
source = mainLayout;
target = lrLayout;
}

// match what we want with what we got
for(int i=0; i<newPlots.count(); i++) {
int index = GenericPlotInfo::findPlot(currentPlots, newPlots[i]);
if (index <0) {
// new one required
newPlots[i].plot = new GenericPlot(this, context);
mainLayout->addWidget(newPlots[i].plot);
mainLayout->setStretchFactor(newPlots[i].plot, 10);// make them all the same

target->addWidget(newPlots[i].plot);
target->setStretchFactor(newPlots[i].plot, 10);// make them all the same
} else {
newPlots[i].plot = currentPlots[index].plot; // reuse
currentPlots[index].state = GenericPlotInfo::matched; // don't deleteme !

// make sure its in the right layout (might remove and add back to the same layout!)
source->removeWidget(newPlots[i].plot);
target->addWidget(newPlots[i].plot);
target->setStretchFactor(newPlots[i].plot, 10);// make them all the same
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/Charts/GenericChart.h
Expand Up @@ -30,6 +30,8 @@
#include "GenericLegend.h"
#include "GenericPlot.h"

#include <QScrollArea>

// keeping track of the series info
class GenericSeriesInfo {

Expand Down Expand Up @@ -207,10 +209,14 @@ class GenericChart : public QWidget {
// post processing clean up / add decorations / helpers etc
void finaliseChart();

// config changed?
void configChanged(qint32);

protected:

// legend and selector need acces to these
QVBoxLayout *mainLayout;
QHBoxLayout *lrLayout;

// chart settings
QString title;
Expand All @@ -234,5 +240,6 @@ class GenericChart : public QWidget {

private:
Context *context;
QScrollArea *stackFrame;
};
#endif
4 changes: 4 additions & 0 deletions src/Charts/GenericPlot.cpp
Expand Up @@ -28,6 +28,10 @@

GenericPlot::GenericPlot(QWidget *parent, Context *context) : QWidget(parent), context(context)
{

// set a minimum height
setMinimumHeight(gl_minheight *dpiXFactor);

// intitialise state info
charttype=0;
chartview=NULL;
Expand Down
2 changes: 2 additions & 0 deletions src/Charts/GenericPlot.h
Expand Up @@ -61,6 +61,8 @@ class GenericPlot : public QWidget {

Q_OBJECT

static constexpr double gl_minheight = 225;

public:

friend class GenericSelectTool;
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/python/library.py
Expand Up @@ -53,8 +53,8 @@ def __GCconfigAxis(name,visible=True,align=-1,min=-1,max=-1,type=-1,labelcolor="
GC.setAxis=__GCconfigAxis

# orientation
GC_VERTICAL=1
GC_HORIZONTAL=2
GC_HORIZONTAL=1
GC_VERTICAL=2

# line style
GC_LINE_NONE=0
Expand Down

0 comments on commit 876ce83

Please sign in to comment.