Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:27:33

0001 #include <sstream>
0002 #include "Fireworks/TableWidget/interface/FWTableManagerBase.h"
0003 #include "Fireworks/TableWidget/interface/FWTableWidget.h"
0004 #include "Fireworks/TableWidget/interface/FWCheckedTextTableCellRenderer.h"
0005 
0006 class FWCheckedTextTableCellRenderer;
0007 
0008 class FWTestTableManager : public FWTableManagerBase {
0009    public:
0010       FWTestTableManager(const std::vector<std::string>& iColumns,
0011                          const std::vector<std::string>& iData /*must be a multiple of columns */);
0012       virtual  int numberOfRows() const ;
0013       virtual  int numberOfColumns() const ;
0014       virtual int unsortedRowNumber(int iSortedRowNumber) const;
0015       virtual void implSort(int col, bool sortOrder) ; // sortOrder=true means desc order
0016       virtual std::vector<std::string> getTitles() const;
0017       virtual FWTableCellRendererBase* cellRenderer(int iRow, int iCol) const;
0018       
0019       void selectRow(int);
0020       //responds to a row being selected 
0021       void rowClicked(Int_t row, Int_t btn, Int_t keyMod);
0022       
0023       //responds to a checkbox
0024       void checkBoxClicked();
0025       
0026       virtual bool hasRowHeaders() const ;
0027       virtual FWTableCellRendererBase* rowHeader(int iRow) const ;
0028       
0029    private:
0030       std::vector<std::string> m_columns;
0031       std::vector<std::string> m_content;
0032       std::vector<unsigned int> m_sortOrder;
0033       std::vector<bool> m_rowChecked;
0034       
0035       FWTextTableCellRenderer* m_renderer;
0036       FWCheckedTextTableCellRenderer* m_rowHeaderRenderer;
0037       
0038       int m_selectedRow;
0039       mutable int m_lastRequestedRow;
0040 };
0041 
0042 
0043 
0044 
0045 FWTestTableManager::FWTestTableManager(const std::vector<std::string>& iColumns,
0046                                        const std::vector<std::string>& iData):
0047 m_columns(iColumns),
0048 m_content(iData),
0049 m_rowChecked(m_content.size(),false),
0050 m_renderer( new FWTextTableCellRenderer),
0051 m_rowHeaderRenderer( new FWCheckedTextTableCellRenderer),
0052 m_selectedRow(-1),
0053 m_lastRequestedRow(-1)
0054 {
0055    assert(0== m_content.size()%m_columns.size());
0056    unsigned int nRows = numberOfRows();
0057    m_sortOrder.reserve(nRows);
0058    for(unsigned int i = 0; i < nRows;++i){
0059       m_sortOrder.push_back(i); 
0060    }
0061    m_rowHeaderRenderer->Connect("checkBoxClicked()","FWTestTableManager",this,"checkBoxClicked()");
0062 }
0063 
0064 int 
0065 FWTestTableManager::numberOfRows() const
0066 {
0067    return m_content.size()/m_columns.size();
0068 }
0069 
0070 int 
0071 FWTestTableManager::numberOfColumns() const
0072 {
0073    return m_columns.size();
0074 }
0075 
0076 void 
0077 FWTestTableManager::implSort(int col, bool sortOrder) 
0078 {
0079    if(col >-1 && col < m_columns.size()) {
0080       std::vector<std::pair<std::string,unsigned int> > toSort;
0081       int nRows = numberOfRows();
0082       toSort.reserve(nRows);
0083       
0084       for(int row=0; row< nRows; ++row) {
0085          toSort.push_back( std::make_pair<std::string, unsigned int>(m_content[col+row*numberOfColumns()],row));
0086       }
0087       if(sortOrder){
0088          std::sort(toSort.begin(),toSort.end(),std::greater<std::pair<std::string,unsigned int> >());
0089       } else {
0090          std::sort(toSort.begin(),toSort.end(),std::less<std::pair<std::string,unsigned int> >());
0091       }
0092       std::vector<unsigned int>::iterator itSort = m_sortOrder.begin();
0093       for(std::vector<std::pair<std::string,unsigned int> >::iterator it = toSort.begin(), itEnd=toSort.end();
0094       it != itEnd;
0095       ++it,++itSort) {
0096          *itSort = it->second;
0097       }
0098    }
0099 }
0100 
0101 int 
0102 FWTestTableManager::unsortedRowNumber(int iSortedRowNumber) const
0103 {
0104    return m_sortOrder[iSortedRowNumber];
0105 }
0106 
0107 std::vector<std::string> 
0108 FWTestTableManager::getTitles() const
0109 {
0110    return std::vector<std::string>(m_columns.begin(),m_columns.end());
0111 }
0112 
0113 FWTableCellRendererBase* 
0114 FWTestTableManager::cellRenderer(int iRow, int iCol) const
0115 {
0116    int rowBeforeSort = m_sortOrder[iRow];
0117    m_renderer->setData( *(m_content.begin()+rowBeforeSort*m_columns.size()+iCol),
0118                           rowBeforeSort==m_selectedRow);
0119    return m_renderer;
0120 }
0121 
0122 void 
0123 FWTestTableManager::selectRow(int iRow)
0124 {
0125    if(iRow != m_selectedRow) {
0126       m_selectedRow = iRow;
0127       visualPropertiesChanged();
0128    }
0129 }
0130 
0131 void 
0132 FWTestTableManager::rowClicked(Int_t row, Int_t btn, Int_t keyMod)
0133 {
0134    if(btn==kButton1) {
0135       if(row==m_selectedRow) {
0136          if(keyMod & kKeyShiftMask) {
0137             selectRow(-1);
0138          }
0139       } else {
0140          selectRow(row);
0141       }
0142    }  
0143 }
0144 
0145 bool 
0146 FWTestTableManager::hasRowHeaders() const 
0147 {
0148    return true; 
0149    //return false;
0150 }
0151 FWTableCellRendererBase* 
0152 FWTestTableManager::rowHeader(int iRow) const
0153 {
0154    int rowBeforeSort = m_sortOrder[iRow];
0155    m_lastRequestedRow=rowBeforeSort;
0156    
0157    std::ostringstream s;
0158    s<<rowBeforeSort;
0159    m_rowHeaderRenderer->setData(s.str(),rowBeforeSort==m_selectedRow);
0160    m_rowHeaderRenderer->setChecked(m_rowChecked[rowBeforeSort]);
0161    return m_rowHeaderRenderer;
0162 }
0163 
0164 void
0165 FWTestTableManager::checkBoxClicked()
0166 {
0167    if(-1 != m_lastRequestedRow) {
0168       m_rowChecked[m_lastRequestedRow] = ! m_rowChecked[m_lastRequestedRow];
0169       visualPropertiesChanged();
0170    }
0171 }
0172 
0173 
0174 void tableWidgetTest()
0175 {
0176    int width=440;
0177    int height=400;
0178    // Create a main frame 
0179    TGMainFrame *fMain = new TGMainFrame(gClient->GetRoot(),width,height); 
0180 
0181    TGCompositeFrame *tFrame  = new TGCompositeFrame(fMain, width, height);
0182    TGLayoutHints *tFrameHints = new TGLayoutHints(kLHintsTop|kLHintsLeft|kLHintsExpandX|kLHintsExpandY);
0183    fMain->AddFrame(tFrame,tFrameHints);
0184    
0185     std::vector<std::string> columns;
0186    columns.push_back("one long title");
0187    columns.push_back("two");
0188    columns.push_back("another long one");
0189    columns.push_back("four");
0190    std::vector<std::string> data;
0191    for(int iRow =1; iRow < 1000; ++iRow) {
0192       for(int iCol =1; iCol < 5;++iCol) {
0193          std::stringstream s;
0194          s<<iRow<<" "<<iCol;
0195          data.push_back(s.str());
0196       }
0197    }
0198 
0199    FWTestTableManager* tableM = new FWTestTableManager(columns,data);
0200    FWTableWidget* table = new FWTableWidget(tableM, tFrame);
0201    table->Connect("rowClicked(Int_t,Int_t,Int_t)","FWTestTableManager",tableM,"rowClicked(Int_t,Int_t,Int_t)");
0202    table->sort(0,true);
0203    tFrame->AddFrame(table,tFrameHints);
0204    
0205    fMain->SetWindowName("Header Test");
0206    fMain->MapSubwindows();
0207    fMain->Layout();
0208    fMain->MapWindow();
0209    
0210 }