Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/root/TWebCanvas.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Author: Sergey Linev, GSI   7/12/2016
0002 
0003 /*************************************************************************
0004  * Copyright (C) 1995-2023, Rene Brun and Fons Rademakers.               *
0005  * All rights reserved.                                                  *
0006  *                                                                       *
0007  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0008  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0009  *************************************************************************/
0010 
0011 #ifndef ROOT_TWebCanvas
0012 #define ROOT_TWebCanvas
0013 
0014 #include "TCanvasImp.h"
0015 
0016 #include "TString.h"
0017 #include "TList.h"
0018 #include "TWebPadOptions.h"
0019 
0020 #include <ROOT/RWebWindow.hxx>
0021 
0022 #include <vector>
0023 #include <string>
0024 #include <queue>
0025 #include <functional>
0026 #include <map>
0027 
0028 class TPad;
0029 class TPadWebSnapshot;
0030 class TWebPS;
0031 class TObjLink;
0032 class TExec;
0033 class TWebCanvasTimer;
0034 
0035 class TWebCanvas : public TCanvasImp {
0036 
0037 friend class TWebCanvasTimer;
0038 
0039 public:
0040    /// Function type for signals, invoked when canvas drawing or update is completed
0041    using UpdatedSignal_t = std::function<void()>;
0042 
0043    /// Function type for pad-related signals - like activate pad signal
0044    using PadSignal_t = std::function<void(TPad *)>;
0045 
0046    /// Function type for pad-click signals
0047    using PadClickedSignal_t = std::function<void(TPad *, int, int)>;
0048 
0049    /// Function type for signals, invoked when object is selected
0050    using ObjectSelectSignal_t = std::function<void(TPad *, TObject *)>;
0051 
0052 protected:
0053 
0054    /// Function called when pad painting produced
0055    using PadPaintingReady_t = std::function<void(TPadWebSnapshot *)>;
0056 
0057    struct WebConn {
0058       unsigned fConnId{0};             ///<! connection id
0059       Long64_t fCheckedVersion{0};     ///<! canvas version checked before sending
0060       Long64_t fSendVersion{0};        ///<! canvas version send to the client
0061       Long64_t fDrawVersion{0};        ///<! canvas version drawn (confirmed) by client
0062       UInt_t fLastSendHash{0};         ///<! hash of last send draw message, avoid looping
0063       std::map<std::string, std::string> fCtrl; ///<! different ctrl parameters which can be send at once
0064       std::queue<std::string> fSend;   ///<! send queue, processed after sending draw data
0065 
0066       WebConn(unsigned id) : fConnId(id) {}
0067       bool is_batch() const { return fConnId == 0; }
0068       bool match(unsigned id) const { return !is_batch() && ((fConnId == id) || (id == 0)); }
0069       void reset()
0070       {
0071          fCheckedVersion = fSendVersion = fDrawVersion = 0;
0072          fLastSendHash = 0;
0073       }
0074    };
0075 
0076    struct PadStatus {
0077       Long64_t fVersion{0};    ///<! last pad version
0078       bool _detected{false};   ///<! if pad was detected during last scan
0079       bool _modified{false};   ///<! if pad was modified during last scan
0080       bool _has_specials{false}; ///<! are there any special objects with painting
0081    };
0082 
0083    std::vector<WebConn> fWebConn;  ///<! connections
0084    TWebCanvasTimer *fTimer{nullptr}; ///<! timer to submit control messages
0085 
0086    std::map<TPad*, PadStatus> fPadsStatus; ///<! map of pads in canvas and their status flags
0087 
0088    std::shared_ptr<ROOT::RWebWindow> fWindow; ///!< configured display
0089 
0090    Bool_t fReadOnly{kFALSE};       ///<! in read-only mode canvas cannot be changed from client side
0091    Long64_t fCanvVersion{1};       ///<! actual canvas version, changed with every new Modified() call
0092    UInt_t fClientBits{0};          ///<! latest status bits from client like editor visible or not
0093    std::vector<TPad *> fAllPads;   ///<! list of all pads recognized during streaming
0094    Int_t fStyleDelivery{0};        ///<! gStyle delivery to clients: 0:never, 1:once, 2:always
0095    Int_t fPaletteDelivery{1};      ///<! colors palette delivery 0:never, 1:once, 2:always, 3:per subpad
0096    Int_t fPrimitivesMerge{100};    ///<! number of PS primitives, which will be merged together
0097    Int_t fJsonComp{0};             ///<! compression factor for messages send to the client
0098    std::string fCustomScripts;     ///<! custom JavaScript code or URL on JavaScript files to load before start drawing
0099    std::vector<std::string> fCustomClasses;  ///<! list of custom classes, which can be delivered as is to client
0100    Bool_t fCanCreateObjects{kTRUE}; ///<! indicates if canvas allowed to create extra objects for interactive painting
0101    Bool_t fLongerPolling{kFALSE};  ///<! when true, make longer polling in blocking operations
0102    Bool_t fProcessingData{kFALSE}; ///<! flag used to prevent blocking methods when process data is invoked
0103    Bool_t fAsyncMode{kFALSE};      ///<! when true, methods like TCanvas::Update will never block
0104    Long64_t fStyleVersion{0};      ///<! current gStyle object version, checked every time when new snapshot created
0105    UInt_t fStyleHash{0};           ///<! last hash of gStyle
0106    Long64_t fColorsVersion{0};     ///<! current colors/palette version, checked every time when new snapshot created
0107    UInt_t fColorsHash{0};          ///<! last hash of colors/palette
0108    Int_t fTF1UseSave{1};           ///<! use save buffer for TF1/TF2, 0:off, 1:prefer, 2:force
0109    std::vector<int> fWindowGeometry; ///<! last received window geometry
0110    Bool_t fFixedSize{kFALSE};      ///<! is canvas size fixed
0111 
0112    UpdatedSignal_t fUpdatedSignal; ///<! signal emitted when canvas updated or state is changed
0113    PadSignal_t fActivePadChangedSignal; ///<! signal emitted when active pad changed in the canvas
0114    PadClickedSignal_t fPadClickedSignal; ///<! signal emitted when simple mouse click performed on the pad
0115    PadClickedSignal_t fPadDblClickedSignal; ///<! signal emitted when simple mouse click performed on the pad
0116    ObjectSelectSignal_t fObjSelectSignal; ///<! signal emitted when new object selected in the pad
0117 
0118    void Lock() override {}
0119    void Unlock() override {}
0120    Bool_t IsLocked() override { return kFALSE; }
0121 
0122    Bool_t IsWeb() const override { return kTRUE; }
0123    Bool_t PerformUpdate(Bool_t async) override;
0124    TVirtualPadPainter *CreatePadPainter() override;
0125 
0126    UInt_t CalculateColorsHash();
0127    void AddColorsPalette(TPadWebSnapshot &master);
0128    void AddCustomFonts(TPadWebSnapshot &master);
0129 
0130    void CreateObjectSnapshot(TPadWebSnapshot &master, TPad *pad, TObject *obj, const char *opt, TWebPS *masterps = nullptr);
0131    void CreatePadSnapshot(TPadWebSnapshot &paddata, TPad *pad, Long64_t version, PadPaintingReady_t func);
0132 
0133    void CheckPadModified(TPad *pad);
0134 
0135    Bool_t CheckCanvasModified(bool force_modified = false);
0136 
0137    void AddCtrlMsg(unsigned connid, const std::string &key, const std::string &value);
0138 
0139    void AddSendQueue(unsigned connid, const std::string &msg);
0140 
0141    Bool_t CheckDataToSend(unsigned connid = 0);
0142 
0143    Bool_t WaitWhenCanvasPainted(Long64_t ver);
0144 
0145    virtual Bool_t IsJSSupportedClass(TObject *obj, Bool_t many_primitives = kFALSE);
0146 
0147    Bool_t IsFirstConn(unsigned connid) const { return (connid != 0) && (fWebConn.size() > 1) && (fWebConn[1].fConnId == connid); }
0148 
0149    Bool_t IsFirstDrawn() const { return (fWebConn.size() > 1) && (fWebConn[1].fDrawVersion > 0); }
0150 
0151    void ShowCmd(const std::string &arg, Bool_t show);
0152 
0153    void AssignStatusBits(UInt_t bits);
0154 
0155    virtual Bool_t ProcessData(unsigned connid, const std::string &arg);
0156 
0157    virtual Bool_t DecodePadOptions(const std::string &, bool process_execs = false);
0158 
0159    virtual Bool_t CanCreateObject(const std::string &) { return !IsReadOnly() && fCanCreateObjects; }
0160 
0161    TPad *ProcessObjectOptions(TWebObjectOptions &item, TPad *pad, int idcnt = 1);
0162 
0163    TObject *FindPrimitive(const std::string &id, int idcnt = 1, TPad *pad = nullptr, TObjLink **objlnk = nullptr, TPad **objpad = nullptr);
0164 
0165    void ProcessExecs(TPad *pad, TExec *extra = nullptr);
0166 
0167    void ProcessLinesForObject(TObject *obj, const std::string &lines);
0168 
0169 public:
0170    TWebCanvas(TCanvas *c, const char *name, Int_t x, Int_t y, UInt_t width, UInt_t height, Bool_t readonly = kTRUE);
0171    ~TWebCanvas() override;
0172 
0173    void ShowWebWindow(const ROOT::RWebDisplayArgs &user_args = "");
0174 
0175    const std::shared_ptr<ROOT::RWebWindow> &GetWebWindow() const { return fWindow; }
0176 
0177    virtual Bool_t IsReadOnly() const { return fReadOnly; }
0178 
0179    Int_t InitWindow() override;
0180    void Close() override;
0181    void Show() override;
0182 
0183    UInt_t GetWindowGeometry(Int_t &x, Int_t &y, UInt_t &w, UInt_t &h) override;
0184 
0185    void ShowMenuBar(Bool_t show = kTRUE) override { ShowCmd("Menu", show); }
0186    void ShowStatusBar(Bool_t show = kTRUE) override { ShowCmd("StatusBar", show); }
0187    void ShowEditor(Bool_t show = kTRUE) override { ShowCmd("Editor", show); }
0188    void ShowToolBar(Bool_t show = kTRUE) override { ShowCmd("ToolBar", show); }
0189    void ShowToolTips(Bool_t show = kTRUE) override { ShowCmd("ToolTips", show); }
0190 
0191    // web-canvas specific methods
0192 
0193    void ActivateInEditor(TPad *pad, TObject *obj);
0194 
0195    void ForceUpdate() override;
0196 
0197    void   SetWindowPosition(Int_t x, Int_t y) override;
0198    void   SetWindowSize(UInt_t w, UInt_t h) override;
0199    void   SetWindowTitle(const char *newTitle) override;
0200    void   SetCanvasSize(UInt_t w, UInt_t h) override;
0201    void   Iconify() override;
0202    void   RaiseWindow() override;
0203 
0204    /*
0205       virtual void   SetStatusText(const char *text = 0, Int_t partidx = 0);
0206       virtual void   ReallyDelete();
0207     */
0208 
0209    Bool_t HasEditor() const override;
0210    Bool_t HasMenuBar() const override;
0211    Bool_t HasStatusBar() const override;
0212    Bool_t HasToolBar() const override { return kFALSE; }
0213    Bool_t HasToolTips() const override;
0214 
0215    void SetUpdatedHandler(UpdatedSignal_t func) { fUpdatedSignal = func; }
0216    void SetActivePadChangedHandler(PadSignal_t func) { fActivePadChangedSignal = func; }
0217    void SetPadClickedHandler(PadClickedSignal_t func) { fPadClickedSignal = func; }
0218    void SetPadDblClickedHandler(PadClickedSignal_t func) { fPadDblClickedSignal = func; }
0219    void SetObjSelectHandler(ObjectSelectSignal_t func) { fObjSelectSignal = func; }
0220 
0221    void SetCanCreateObjects(Bool_t on = kTRUE) { fCanCreateObjects = on; }
0222    Bool_t GetCanCreateObjects() const { return fCanCreateObjects; }
0223 
0224    void SetStyleDelivery(Int_t val) { fStyleDelivery = val; }
0225    Int_t GetStyleDelivery() const { return fStyleDelivery; }
0226 
0227    void SetPaletteDelivery(Int_t val) { fPaletteDelivery = val; }
0228    Int_t GetPaletteDelivery() const { return fPaletteDelivery; }
0229 
0230    void SetPrimitivesMerge(Int_t cnt) { fPrimitivesMerge = cnt; }
0231    Int_t GetPrimitivesMerge() const { return fPrimitivesMerge; }
0232 
0233    void SetLongerPolling(Bool_t on) { fLongerPolling = on; }
0234    Bool_t GetLongerPolling() const { return fLongerPolling; }
0235 
0236    void SetCustomScripts(const std::string &src);
0237 
0238    void AddCustomClass(const std::string &clname, bool with_derived = false);
0239    bool IsCustomClass(const TClass *cl) const;
0240 
0241    void SetAsyncMode(Bool_t on = kTRUE) { fAsyncMode = on; }
0242    Bool_t IsAsyncMode() const { return fAsyncMode; }
0243 
0244    Bool_t IsFixedSize() const { return fFixedSize; }
0245 
0246    static Font_t AddFont(const char *name, const char *ttffile, Int_t precision = 2);
0247 
0248    static TString CreatePadJSON(TPad *pad, Int_t json_compression = 0, Bool_t batchmode = kFALSE);
0249    static TString CreateCanvasJSON(TCanvas *c, Int_t json_compression = 0, Bool_t batchmode = kFALSE);
0250    static Int_t StoreCanvasJSON(TCanvas *c, const char *filename, const char *option = "");
0251 
0252    static bool ProduceImage(TPad *pad, const char *filename, Int_t width = 0, Int_t height = 0);
0253 
0254    static bool ProduceImages(std::vector<TPad *> pads, const char *filename, Int_t width = 0, Int_t height = 0);
0255 
0256    static TCanvasImp *NewCanvas(TCanvas *c, const char *name, Int_t x, Int_t y, UInt_t width, UInt_t height);
0257 
0258    ClassDefOverride(TWebCanvas, 0) // Web-based implementation for TCanvasImp
0259 };
0260 
0261 #endif