File indexing completed on 2024-05-05 17:19:08

0001 /***************************************************************************
0002  * SPDX-FileCopyrightText: 2022 S. MANKOWSKI stephane@mankowski.fr
0003  * SPDX-FileCopyrightText: 2022 G. DE BURE support@mankowski.fr
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  ***************************************************************************/
0006 #ifndef SKGTABLEWITHGRAPH_H
0007 #define SKGTABLEWITHGRAPH_H
0008 /** @file
0009  * A table with graph with more features.
0010  *
0011  * @author Stephane MANKOWSKI / Guillaume DE BURE
0012  */
0013 #include <qwidget.h>
0014 
0015 
0016 
0017 #include "skgbasegui_export.h"
0018 #include "skgcombobox.h"
0019 #include "skgservices.h"
0020 #include "ui_skgtablewithgraph.h"
0021 
0022 class SKGGraphicsScene;
0023 class QMenu;
0024 class QTimer;
0025 class QWidgetAction;
0026 class QGraphicsItem;
0027 
0028 /**
0029  * This file is a table with graph with more features
0030  */
0031 class SKGBASEGUI_EXPORT SKGTableWithGraph : public QWidget
0032 {
0033     Q_OBJECT
0034 
0035 public:
0036     /**
0037      * Graph type
0038      */
0039     enum GraphType {STACK,
0040                     HISTOGRAM,
0041                     PIE,
0042                     CONCENTRICPIE,
0043                     POINT,
0044                     LINE,
0045                     STACKAREA,
0046                     BUBBLE,
0047                     STACKCOLUMNS,
0048                     TREEMAP
0049                    };
0050     /**
0051      * Graph type
0052      */
0053     Q_ENUM(GraphType)
0054 
0055     /**
0056      * Additional information to display
0057      */
0058     enum DisplayAdditional {NONE      = 0x0,
0059                             SUM       = 0x1,
0060                             AVERAGE   = 0x2,
0061                             LIMITS    = 0x4,
0062                             ALL       = 255
0063                            };
0064     /**
0065      * Additional information to display
0066      */
0067     Q_ENUM(DisplayAdditional)
0068 
0069     /**
0070      * Additional information to display
0071      */
0072     Q_DECLARE_FLAGS(DisplayAdditionalFlag, DisplayAdditional)
0073 
0074     /**
0075      * Graph Type widget visibility
0076      */
0077     Q_PROPERTY(bool graphTypeSelectorVisible READ isGraphTypeSelectorVisible WRITE setGraphTypeSelectorVisible NOTIFY modified)
0078     /**
0079      * Items selectable or not
0080      */
0081     Q_PROPERTY(bool selectable READ isSelectable WRITE setSelectable NOTIFY modified)
0082     /**
0083      * Items with shadow or not
0084      */
0085     Q_PROPERTY(bool shadow READ isShadowVisible WRITE setShadowVisible NOTIFY modified)
0086     /**
0087      * Graph Type
0088      */
0089     Q_PROPERTY(GraphType graphType READ getGraphType WRITE setGraphType NOTIFY modified)
0090     /**
0091      * Default Constructor
0092      */
0093     SKGTableWithGraph();
0094 
0095     /**
0096      * Constructor
0097      * @param iParent the parent
0098      */
0099     explicit SKGTableWithGraph(QWidget* iParent);
0100 
0101     /**
0102      * Default Destructor
0103      */
0104     ~SKGTableWithGraph() override;
0105 
0106     /**
0107     * Get the current state
0108     * MUST BE OVERWRITTEN
0109     * @return a string containing all information needed to set the same state.
0110     * Could be an XML stream
0111      */
0112     QString getState();
0113 
0114     /**
0115      * Returns the table.
0116      * @return table
0117      */
0118     QTableWidget* table() const;
0119 
0120     /**
0121      * Returns the graph.
0122      * @return graph
0123      */
0124     SKGGraphicsView* graph() const;
0125 
0126     /**
0127      * Returns the text report.
0128      * @return text report
0129      */
0130     SKGWebView* textReport() const;
0131 
0132     /**
0133      * Get the mode for the additional display
0134      * @return the mode
0135      */
0136     SKGTableWithGraph::DisplayAdditionalFlag getAdditionalDisplayMode() const;
0137 
0138     /**
0139      * Get the table content
0140      * @return the table content
0141      */
0142     SKGStringListList getTable();
0143 
0144     /**
0145      * Get a pointer on the contextual menu of the table
0146      * @return contextual menu
0147      */
0148     QMenu* getTableContextualMenu() const;
0149 
0150     /**
0151      * Get a pointer on the contextual menu of the graph
0152      * @return contextual menu
0153      */
0154     QMenu* getGraphContextualMenu() const;
0155 
0156     /**
0157      * Get the visibility of the graph type selector zone
0158      * @return the visibility
0159      */
0160     bool isGraphTypeSelectorVisible() const;
0161 
0162     /**
0163      * Get the selectability of items
0164      * @return the selectability
0165      */
0166     bool isSelectable() const;
0167 
0168     /**
0169      * Get the shadows visibility
0170      * @return the visibility
0171      */
0172     bool isShadowVisible() const;
0173 
0174     /**
0175      * Get the graph type
0176      *  @return the type of graph
0177      */
0178     SKGTableWithGraph::GraphType getGraphType() const;
0179 
0180     /**
0181      * Get the number of columns
0182      * @param iWithComputed with compute columns (average, sum, forecast, …) or not
0183      * @return the number of columns
0184      */
0185     int getNbColumns(bool iWithComputed = false) const;
0186 
0187     /**
0188      * @brief Get show widget
0189      *
0190      * @return SKGShow*
0191      **/
0192     SKGShow* getShowWidget() const;
0193 
0194     /**
0195      * To know if the table is visible
0196      * @return the visibility
0197      */
0198     bool isTableVisible() const;
0199 
0200     /**
0201      * To know if the graph is visible
0202      * @return the visibility
0203      */
0204     bool isGraphVisible() const;
0205 
0206     /**
0207      * To know if the text report is visible
0208      * @return the visibility
0209      */
0210     bool isTextReportVisible() const;
0211 
0212 public Q_SLOTS:
0213 
0214     /**
0215      * Set the current state
0216      * MUST BE OVERWRITTEN
0217      * @param iState must be interpreted to set the state of the widget
0218      */
0219     void setState(const QString& iState);
0220 
0221     /**
0222      * Set Data
0223      * @param iData the data
0224      * @param iPrimaryUnit the primary unit
0225      * @param iSecondaryUnit the secondary unit
0226      * @param iAdditionalInformation show sum and average columns
0227      * @param iNbVirtualColumn number of columns
0228      */
0229     void setData(const SKGStringListList& iData,
0230                  const SKGServices::SKGUnitInfo& iPrimaryUnit,
0231                  const SKGServices::SKGUnitInfo& iSecondaryUnit,
0232                  SKGTableWithGraph::DisplayAdditionalFlag iAdditionalInformation = SKGTableWithGraph::ALL,
0233                  int iNbVirtualColumn = 0);
0234 
0235 
0236     /**
0237      * Set the visibility of the graph type selector zone
0238      * @param iVisible the visibility
0239      */
0240     void setGraphTypeSelectorVisible(bool iVisible);
0241 
0242     /**
0243      * Enable / disable the selectability of items
0244      * @param iSelectable the selectability
0245      */
0246     void setSelectable(bool iSelectable);
0247 
0248     /**
0249      * Enable / disable the shadows
0250      * @param iShadow the shadows
0251      */
0252     void setShadowVisible(bool iShadow);
0253 
0254     /**
0255      * Set the graph type
0256      * @param iType the type of graph
0257      */
0258     void setGraphType(SKGTableWithGraph::GraphType iType);
0259 
0260     /**
0261      * Set tool bar visibility
0262      * @param iVisibility the visibility
0263      */
0264     void setFilterVisibility(bool iVisibility) const;
0265 
0266     /**
0267      * Set the axis color
0268      * @param iColor the color
0269      */
0270     void setAxisColor(const QColor& iColor = Qt::gray);
0271 
0272     /**
0273      * Set the grid color
0274      * @param iColor the color
0275      */
0276     void setGridColor(const QColor& iColor = Qt::lightGray);
0277 
0278     /**
0279      * Set the min color
0280      * @param iColor the color
0281      */
0282     void setMinColor(const QColor& iColor = Qt::red);
0283 
0284     /**
0285      * Set the max color
0286      * @param iColor the color
0287      */
0288     void setMaxColor(const QColor& iColor = Qt::green);
0289 
0290     /**
0291      * Set the pareto color
0292      * @param iColor the color
0293      */
0294     void setParetoColor(const QColor& iColor = Qt::darkRed);
0295 
0296     /**
0297      * Set the average color
0298      * @param iColor the color
0299      */
0300     void setAverageColor(const QColor& iColor = Qt::blue);
0301 
0302     /**
0303      * Set the tendency color
0304      * @param iColor the color
0305      */
0306     void setTendencyColor(const QColor& iColor = Qt::darkYellow);
0307 
0308     /**
0309      * Set the outline color
0310      * @param iColor the color
0311      */
0312     void setOutlineColor(const QColor& iColor = Qt::black);
0313 
0314     /**
0315      * Set the background color
0316      * @param iColor the color
0317      */
0318     void setBackgroundColor(const QColor& iColor = Qt::white);
0319 
0320     /**
0321      * Set the text color
0322      * @param iColor the color
0323      */
0324     void setTextColor(const QColor& iColor = Qt::black);
0325 
0326     /**
0327      * Set antialiasing
0328      * @param iAntialiasing enabled or disabled
0329      */
0330     void setAntialiasing(bool iAntialiasing = true);
0331 
0332     /**
0333      * Redraw the graph after some milliseconds
0334      */
0335     void redrawGraphDelayed();
0336 
0337     /**
0338      * Switch the limits visibility
0339      * @return the new visibility
0340      */
0341     bool switchLimitsVisibility();
0342 
0343     /**
0344      * Switch the average visibility
0345      * @return the new visibility
0346      */
0347     bool switchAverageVisibility();
0348 
0349     /**
0350      * Switch the linear regression visibility
0351      * @return the new visibility
0352      */
0353     bool switchLinearRegressionVisibility();
0354 
0355     /**
0356      * Switch the pareto curve visibility
0357      * @return the new visibility
0358      */
0359     bool switchParetoVisibility();
0360 
0361     /**
0362      * Switch the legend visibility
0363      * @return the new visibility
0364      */
0365     bool switchLegendVisibility();
0366 
0367     /**
0368      * Switch the origin visibility
0369      * @return the new visibility
0370      */
0371     bool swithOriginVisibility();
0372 
0373     /**
0374      * Switch the decimals visibility
0375      * @return the new visibility
0376      */
0377     bool swithDecimalsVisibility();
0378 
0379     /**
0380      * Reset the colors
0381      */
0382     void resetColors();
0383 
0384     /**
0385      * Export to a file
0386      * @param iFileName the file name
0387      * @return an object managing the error
0388      *   @see SKGError
0389      */
0390     SKGError exportInFile(const QString& iFileName);
0391 
0392 Q_SIGNALS:
0393     /**
0394      * Emitted when a cell is double clicked
0395      * @param row row of the cell
0396      * @param column column of the cell
0397      */
0398     void cellDoubleClicked(int row, int column);
0399 
0400     /**
0401      * Selection changed
0402      */
0403     void selectionChanged();
0404 
0405     /**
0406      * The object is modified
0407      */
0408     void modified();
0409 
0410 private Q_SLOTS:
0411     void onExport();
0412     void onSelectionChanged();
0413     void onSelectionChangedInGraph();
0414     void onDoubleClick(int row, int column);
0415     void onDoubleClickGraph();
0416     void onLinkClicked(const QUrl& url);
0417     void onFilterModified();
0418     void onDisplayModeChanged();
0419     void onChangeColor();
0420     void onHorizontalScrollBarChanged(int /*iValue*/);
0421     void refresh();
0422     void redrawText();
0423     void redrawGraph();
0424     void showMenu(QPoint iPos);
0425 
0426 private:
0427     Q_DISABLE_COPY(SKGTableWithGraph)
0428 
0429     double computeStepSize(double iRange, double iTargetSteps);
0430     void addArrow(QPointF iPeak, double iSize, double iArrowAngle = 45, double iDegree = 90);
0431     void addLegend(QPointF iPosition, double iSize, double iScaleText, double iMaxY);
0432     QGraphicsItem* drawPoint(qreal iX, qreal iY, qreal iRadius, int iMode, const QBrush& iBrush);
0433     int getAverageColumnIndex() const;
0434     int getMinColumnIndex() const;
0435 
0436     QStringList getSumItems(const QString& iString) const;
0437     void addSums(SKGStringListList& ioTable, int& iNblines);
0438 
0439     Ui::skgtablewithgraph_base ui{};
0440     SKGGraphicsScene* m_scene;
0441 
0442     SKGStringListList m_data;
0443     QList<bool> m_sumRows;
0444     SKGServices::SKGUnitInfo m_primaryUnit;
0445     SKGServices::SKGUnitInfo m_secondaryUnit;
0446     DisplayAdditionalFlag m_additionalInformation;
0447     int m_nbVirtualColumns;
0448     bool m_selectable;
0449     bool m_toolBarVisible;
0450     bool m_graphTypeVisible;
0451     bool m_limitVisible;
0452     bool m_averageVisible;
0453     bool m_linearRegressionVisible;
0454     bool m_paretoVisible;
0455     bool m_legendVisible;
0456     bool m_graphVisible;
0457     bool m_tableVisible;
0458     bool m_textVisible;
0459     bool m_zeroVisible;
0460     bool m_decimalsVisible;
0461     bool m_shadow;
0462 
0463     QMenu* m_mainMenu;
0464     QTimer m_timer;
0465     QTimer m_timerRedraw;
0466     QAction* m_actShowLimits;
0467     QAction* m_actShowAverage;
0468     QAction* m_actShowLinearRegression;
0469     QAction* m_actShowPareto;
0470     QAction* m_actShowLegend;
0471     QAction* m_actShowZero;
0472     QAction* m_actShowDecimal;
0473     QAction* m_allPositiveMenu;
0474     QWidgetAction* m_displayModeWidget{};
0475 
0476     int m_indexSum;
0477     int m_indexAverage;
0478     int m_indexMin;
0479     int m_indexLinearRegression;
0480     QMap<QString, QColor> m_mapTitleColor;
0481     QMap<QTableWidgetItem*, QGraphicsItem*> m_mapItemGraphic{};
0482 
0483     QColor m_axisColor;
0484     QColor m_backgroundColor;
0485     QColor m_textColor;
0486     QColor m_gridColor;
0487     QColor m_minColor;
0488     QColor m_maxColor;
0489     QColor m_paretoColor;
0490     QColor m_averageColor;
0491     QColor m_tendencyColor;
0492     QColor m_outlineColor;
0493     QBrush m_NegativeColor;
0494     QBrush m_WhiteColor;
0495 
0496     SKGComboBox* m_displayMode{};
0497 
0498     Qt::SortOrder m_sortOrder;
0499     int m_sortColumn;
0500 };
0501 
0502 Q_DECLARE_OPERATORS_FOR_FLAGS(SKGTableWithGraph::DisplayAdditionalFlag)
0503 
0504 #endif  // SKGTABLEWITHGRAPH_H