📄 table_panel.hpp
字号:
/* * =========================================================================== * PRODUCTION $Log: table_panel.hpp,v $ * PRODUCTION Revision 1000.4 2004/06/01 19:52:33 gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.15 * PRODUCTION * =========================================================================== */#ifndef GUI_WIDGETS_TABLE___TABLE_PANEL__HPP#define GUI_WIDGETS_TABLE___TABLE_PANEL__HPP/* $Id: table_panel.hpp,v 1000.4 2004/06/01 19:52:33 gouriano Exp $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's official duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * =========================================================================== * * Authors: Mike DiCuccio * * File Description: * CTablePanel -- Extends Flv_Table to provide notions of hidden and visible * columns * CListPanel -- Extends CTablePanel to provide a row-selectable * multicolumn list widget that stores arbitrary data with * each row. */#include <corelib/ncbiobj.hpp>#include <gui/gui.hpp>#include <FL/Enumerations.H>#include <FL/Fl.H>#include <FL/fl_draw.H>#include <gui/widgets/Fl_Table/Fl_Table_Row.H>#include <gui/utils/fltk_utils.hpp>#include <algorithm>#include <memory>#include <string>#include <vector>/** @addtogroup GUI_FltkWidgets * * @{ */BEGIN_NCBI_SCOPEclass NCBI_GUIWIDGETS_TABLE_EXPORT CTablePanelBase : public Fl_Table_Row{public: CTablePanelBase(int x, int y, int w, int h, const char* label = NULL); // safe (virtual) accessors virtual size_t GetRows() const; virtual void SetRows(size_t r); virtual size_t GetCols() const; virtual void SetCols(size_t c); // overrides from Fl_Table_Row (these may be dangerous - they're not // virtual) void rows(int r); int rows() const; void cols(int c); int cols() const; // draw() - enforces clipping void draw(); // handle() - traps double-clicks on headers int handle(int event); // check if a given row is selected. The base class row_selected() is // non-const, for reasons that baffle me. bool IsRowSelected(size_t row) const; // access the FLTK box type used for drawing a cell Fl_Boxtype GetCellBox(void) const; void SetCellBox(CFltkUtils::EBoxType); void SetCellBox(Fl_Boxtype); // access the FLTK box type used for drawing row header cells Fl_Boxtype GetRowHeaderBox(void) const; void SetRowHeaderBox(CFltkUtils::EBoxType); void SetRowHeaderBox(Fl_Boxtype); Fl_Boxtype GetColHeaderBox(void) const; void SetColHeaderBox(CFltkUtils::EBoxType); void SetColHeaderBox(Fl_Boxtype); // set the box type for both rows and cols simultaneously void SetHeaderBox(Fl_Boxtype); void SetHeaderBox(CFltkUtils::EBoxType);protected: // GUI event processor CGUIEvent m_Handler; // box styles for cells and headers Fl_Boxtype m_CellBox; Fl_Boxtype m_RowHeaderBox; Fl_Boxtype m_ColHeaderBox; // internal handlers for cut/copy/paste operations virtual int x_OnCut(); virtual int x_OnCopy(); virtual int x_OnPaste(); // internal handlers for header cell clicks virtual int x_HandleRowHeaderClick(size_t row); virtual int x_HandleColHeaderClick(size_t col); virtual int x_HandleCellClick(size_t row, size_t col);};class CColumnSelectDlg;//// clas ITableData defines the data interface used by all table classes//template <class RowData>class ITableData : public CObject{public: virtual ~ITableData() {} virtual void Reserve (size_t rows, size_t cols) = 0; virtual void SetSize (size_t rows, size_t cols) = 0; virtual void SetRows (size_t rows) = 0; virtual void SetCols (size_t cols) = 0; virtual void AddRow (void) = 0; virtual void RemoveRow(size_t row) = 0; virtual size_t GetRows (void) const = 0; virtual size_t GetCols (void) const = 0; virtual const vector<string>& GetRow(size_t row) const = 0; virtual vector<string>& SetRow(size_t row) = 0; virtual const string& GetCell(size_t row, size_t col) const = 0; virtual string& SetCell(size_t row, size_t col) = 0; virtual const string& GetRowHeader(size_t row) const = 0; virtual string& SetRowHeader(size_t row) = 0; // access a given row's data virtual const RowData& GetData(size_t row) const = 0; virtual RowData& SetData(size_t row) = 0; // sort the data virtual void SortByCol(size_t col, bool ascending, bool numeric) = 0;};//// class CTableData defines a specialization of the table data// structure with a concrete type stored in each row//template <class RowData>class CTableData : public ITableData<RowData>{public: CTableData(); // interface requirements virtual void Reserve (size_t rows, size_t cols); virtual void SetSize (size_t rows, size_t cols); virtual void SetRows (size_t rows); virtual void SetCols (size_t cols); virtual void AddRow (void); virtual void RemoveRow(size_t row); virtual size_t GetRows (void) const; virtual size_t GetCols (void) const; virtual const vector<string>& GetRow(size_t row) const; virtual vector<string>& SetRow(size_t row); virtual const string& GetCell(size_t row, size_t col) const; virtual string& SetCell(size_t row, size_t col); virtual const string& GetRowHeader(size_t row) const; virtual string& SetRowHeader(size_t row); // access a given row's data const RowData& GetData(size_t row) const; RowData& SetData(size_t row); // sort the data void SortByCol(size_t col, bool ascending, bool numeric);private: // typedefs and nested classes for our columns and data // NB: these are public to avoid compiler errors regarding use of a // protected typedef in a protected nested class class CRow { public: RowData m_Data; string m_RowHeader; vector<string> m_Columns; }; typedef CRow TRow; typedef vector<TRow> TTable; size_t m_Cols; TTable m_Data; // // internal functors for sorting columns // struct SColSorter { size_t col_idx; bool sort_ascending; bool sort_numeric; SColSorter(size_t col, bool asc, bool numeric) : col_idx(col), sort_ascending(asc), sort_numeric(numeric) { } bool operator() (const TRow& row0, const TRow& row1) const { _ASSERT(col_idx < row0.m_Columns.size()); _ASSERT(col_idx < row1.m_Columns.size()); _ASSERT(row0.m_Columns.size() == row1.m_Columns.size()); if (sort_ascending) { if (sort_numeric) { return (NStr::StringToDouble(row0.m_Columns[col_idx]) < NStr::StringToDouble(row1.m_Columns[col_idx])); } else { return (row0.m_Columns[col_idx] < row1.m_Columns[col_idx]); } } else { if (sort_numeric) { return (NStr::StringToDouble(row0.m_Columns[col_idx]) > NStr::StringToDouble(row1.m_Columns[col_idx])); } else { return (row0.m_Columns[col_idx] > row1.m_Columns[col_idx]); } } } }; struct SColSorter_Asc_Numeric { size_t col_idx; SColSorter_Asc_Numeric(size_t col) : col_idx(col) { } bool operator() (const TRow& row0, const TRow& row1) const { _ASSERT(col_idx < row0.m_Columns.size()); _ASSERT(col_idx < row1.m_Columns.size()); _ASSERT(row0.m_Columns.size() == row1.m_Columns.size()); return (NStr::StringToDouble(row0.m_Columns[col_idx]) < NStr::StringToDouble(row1.m_Columns[col_idx])); } }; struct SColSorter_Desc_Numeric { size_t col_idx; SColSorter_Desc_Numeric(size_t col) : col_idx(col) { } bool operator() (const TRow& row0, const TRow& row1) const { _ASSERT(col_idx < row0.m_Columns.size()); _ASSERT(col_idx < row1.m_Columns.size()); _ASSERT(row0.m_Columns.size() == row1.m_Columns.size()); return (NStr::StringToDouble(row0.m_Columns[col_idx]) > NStr::StringToDouble(row1.m_Columns[col_idx])); } }; struct SColSorter_Asc_Text { size_t col_idx; SColSorter_Asc_Text(size_t col) : col_idx(col) { } bool operator() (const TRow& row0, const TRow& row1) const { _ASSERT(col_idx < row0.m_Columns.size()); _ASSERT(col_idx < row1.m_Columns.size()); _ASSERT(row0.m_Columns.size() == row1.m_Columns.size()); return (row0.m_Columns[col_idx] < row1.m_Columns[col_idx]); } }; struct SColSorter_Desc_Text { size_t col_idx; SColSorter_Desc_Text(size_t col) : col_idx(col) { } bool operator() (const TRow& row0, const TRow& row1) const { _ASSERT(col_idx < row0.m_Columns.size()); _ASSERT(col_idx < row1.m_Columns.size()); _ASSERT(row0.m_Columns.size() == row1.m_Columns.size()); return (row0.m_Columns[col_idx] > row1.m_Columns[col_idx]); } };};//// class CTablePanel extends Fl_Table with a few convenience functions.//// The main provision is for things such as uniform handling of column headers,// widths, alignments, and visibility.//template <class RowData>class CTablePanel : public CTablePanelBase{public: typedef ITableData<RowData> TTableData; // enumerated list of sort states for our columns enum ESortState { eNotSorted, eAscending, eDescending }; // enumerated list of column representations enum EColumnType { eString, eNumeric }; CTablePanel(int x, int y, int w, int h, const char* label = NULL); virtual ~CTablePanel() { } // clear the contents of the table virtual void Clear(); // reserve space in the table for a certain number of rows void Reserve(size_t rows, size_t cols); void Reserve(size_t rows); // set the size of the table in rows and cols void SetSize(size_t rows, size_t cols); // set the number of rows in the table void SetRows(size_t rows); // set the number of columns in the table void SetCols(size_t cols); // get the number of rows in the table size_t GetRows(void) const; // get the number of columns in the table size_t GetCols(void) const; // add a row to the table, returning its index size_t AddRow (void); // remove a row to the table, returning its index void RemoveRow(size_t row); // return a single row from the table const vector<string>& GetRow(size_t row) const; vector<string>& SetRow(size_t row); // access a single cell in the table const string& GetCell(size_t row, size_t col) const; string& SetCell(size_t row, size_t col); void SetCell(size_t row, size_t col, const string& val); // access the row headers const string& GetRowHeader(size_t row) const; string& SetRowHeader(size_t row); void SetRowHeader(size_t row, const string& val); // access the col headers const string& GetColHeader(size_t col) const; string& SetColHeader(size_t col); void SetColHeader(size_t col, const string& val); // access a given row's data const RowData& GetData(size_t row) const; RowData& SetData(size_t row); void SetData(size_t row, const RowData& data); // FLTK overload: resize the widget virtual void resize(int x, int y, int w, int h); // get/set the column data type. This determines behavior on sorting. void SetColType(size_t col, EColumnType type); EColumnType GetColType(size_t col) const; // get/set the column relative widths // These are a vector of floats that will be normalized ( = sum to 1.0) to // compute actual widths void SetRelWidth(size_t col, float width_frac); float GetRelWidth(size_t col) const; // get/set the absolute column widths, in pixels void SetWidth(size_t col, float width); float GetWidth(size_t col) const; // set the default alignment for a given column void SetColAlign(size_t col, Fl_Align align); Fl_Align GetColAlign(size_t col) const; // set all column parameters at once void SetColumn(size_t col, const string& header, EColumnType type, Fl_Align align, float rel_width); // function to set up the width and alignments for the current // visible columns. void SetupColumns(); // sort by an indicated column in a given sort order void SortByCol(size_t col_idx, ESortState sort_order = eAscending); // set the virtual column for a given index void SetVirtualCol(int visible_col, int actual_col); // clear all virtual columns. NOTE: this will leave the table with no // columns! void ClearVirtualCols(); // show the column selector dialog and process its results. This allows // users to select from the list of virtual columns. //void ShowColSelectDlg();protected: CRef<TTableData> m_Data; // safe accessors for m_Data TTableData& x_SetData(void); const TTableData& x_GetData(void) const; // general information we store concerning columns // this allows us to be smart about such things as sorting, visibility, // etc. struct SColInfo {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -