From ba17c529ffc738e771e0f99c4bdee4384d31683b Mon Sep 17 00:00:00 2001 From: Markus Gans Date: Tue, 1 Aug 2017 00:56:12 +0200 Subject: [PATCH] New methods to set and get FListViewItem and FListView text for a column --- ChangeLog | 4 +++ src/flistbox.h | 2 +- src/flistview.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++-- src/flistview.h | 25 +++++++++++++---- test/listbox.cpp | 4 +-- test/listview.cpp | 24 ++++++++++++++-- 6 files changed, 117 insertions(+), 12 deletions(-) diff --git a/ChangeLog b/ChangeLog index 883aeb8d..70eda37a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2017-07-31 Markus Gans + * New methods to retrieve or modify FListViewItem text or + a FListView column text for a specific column + 2017-07-28 Markus Gans * Possibility for a FListView column to set the alignment (left, center or right) diff --git a/src/flistbox.h b/src/flistbox.h index 19995da6..69776280 100644 --- a/src/flistbox.h +++ b/src/flistbox.h @@ -338,7 +338,7 @@ inline uInt FListBox::getCount() const { return uInt(data.size()); } //---------------------------------------------------------------------- -inline FListBoxItem FListBox::getItem (int index) //const +inline FListBoxItem FListBox::getItem (int index) { listBoxItems::iterator iter = index2iterator(index - 1); return *iter; diff --git a/src/flistview.cpp b/src/flistview.cpp index 629c94bf..a654ea51 100644 --- a/src/flistview.cpp +++ b/src/flistview.cpp @@ -9,6 +9,9 @@ #include "ftermbuffer.h" +// static class attributes +FString FListView::empty_string = FString(""); + //---------------------------------------------------------------------- // class FListViewItem //---------------------------------------------------------------------- @@ -76,6 +79,39 @@ FListViewItem::FListViewItem ( const std::vector& cols FListViewItem::~FListViewItem() { } +// public methods of FListViewItem +//---------------------------------------------------------------------- +FString FListViewItem::getText (int column) const +{ + if (column < 0 || column_line.empty() || column >= int(column_line.size()) ) + return FListView::empty_string; + + return column_line[column]; +} + +//---------------------------------------------------------------------- +void FListViewItem::setText (int column, const FString& text) +{ + if (column < 0 || column_line.empty() || column >= int(column_line.size()) ) + return; + + FObject* parent = getParent(); + + if ( parent && parent->isInstanceOf("FListView") ) + { + FListView* listview = static_cast(parent); + + if ( ! listview->header[uInt(column)].fixed_width ) + { + int length = int(text.getLength()); + + if ( length > listview->header[uInt(column)].width ) + listview->header[uInt(column)].width = length; + } + } + + column_line[column] = text; +} //---------------------------------------------------------------------- // class FListView @@ -113,16 +149,27 @@ FListView::~FListView() // destructor // public methods of FListView //---------------------------------------------------------------------- -fc::text_alignment FListView::getColumnAlignment (int column) +fc::text_alignment FListView::getColumnAlignment (int column) const { // Get the alignment for a column - if ( column < 0 || header.empty() || column > int(header.size()) ) + if ( column < 0 || header.empty() || column >= int(header.size()) ) return fc::alignLeft; return header[uInt(column)].alignment; } +//---------------------------------------------------------------------- +FString FListView::getColumnText (int column) const +{ + // Get the text of column + + if ( column < 0 || header.empty() || column >= int(header.size()) ) + return empty_string; + + return header[uInt(column)].name; +} + //---------------------------------------------------------------------- void FListView::setGeometry (int x, int y, int w, int h, bool adjust) { @@ -151,6 +198,25 @@ void FListView::setColumnAlignment (int column, fc::text_alignment align) header[uInt(column)].alignment = align; } +//---------------------------------------------------------------------- +void FListView::setColumnText (int column, const FString& label) +{ + // Set the text for a column + + if ( column < 0 || header.empty() || column > int(header.size()) ) + return; + + if ( ! header[uInt(column)].fixed_width ) + { + int length = int(label.getLength()); + + if ( length > header[uInt(column)].width ) + header[uInt(column)].width = length; + } + + header[uInt(column)].name = label; +} + //---------------------------------------------------------------------- int FListView::addColumn (const FString& label, int width) { diff --git a/src/flistview.h b/src/flistview.h index d9b92118..6e936ce0 100644 --- a/src/flistview.h +++ b/src/flistview.h @@ -64,14 +64,18 @@ class FListViewItem : public FObject // Accessors const char* getClassName() const; uInt getCount() const; + FString getText (int) const; + + // Mutator + void setText (int, const FString&); private: - // Friend classes - friend class FListView; - // Data Member std::vector column_line; FWidget::data_ptr data_pointer; + + // Friend class + friend class FListView; }; #pragma pack(pop) @@ -110,11 +114,14 @@ class FListView : public FWidget // Accessors const char* getClassName() const; - fc::text_alignment getColumnAlignment (int); + fc::text_alignment getColumnAlignment (int) const; + FString getColumnText (int) const; + FListViewItem* getCurrentItem() const; // Mutators void setGeometry (int, int, int, int, bool = true); void setColumnAlignment (int, fc::text_alignment); + void setColumnText (int, const FString&); // Methods virtual int addColumn (const FString&, int = USE_MAX_SIZE); @@ -150,7 +157,7 @@ class FListView : public FWidget Header() : name() , width (0) - , fixed_width (-1) + , fixed_width (false) , alignment (fc::alignLeft) { } @@ -191,6 +198,7 @@ class FListView : public FWidget void cb_HBarChange (FWidget*, data_ptr); // Data Members + static FString empty_string; listViewItems data; headerItems header; FTermBuffer headerline; @@ -205,6 +213,9 @@ class FListView : public FWidget int yoffset; int nf_offset; int max_line_width; + + // Friend class + friend class FListViewItem; }; #pragma pack(pop) @@ -214,6 +225,10 @@ class FListView : public FWidget inline const char* FListView::getClassName() const { return "FListView"; } +//---------------------------------------------------------------------- +inline FListViewItem* FListView::getCurrentItem() const +{ return data[current-1]; } + //---------------------------------------------------------------------- inline FListView::listViewItems::iterator FListView::index2iterator (int index) { diff --git a/test/listbox.cpp b/test/listbox.cpp index 5b3146a7..ae54a70d 100644 --- a/test/listbox.cpp +++ b/test/listbox.cpp @@ -64,7 +64,7 @@ class Listbox : public FDialog // Event handlers void onClose (FCloseEvent*); - // Callback methods + // Callback method void cb_exitApp (FWidget*, data_ptr); // Data Member @@ -125,7 +125,7 @@ Listbox::Listbox (FWidget* parent) Quit->setGeometry(42, 12, 10, 1); Quit->setText (L"&Quit"); - // Add some function callbacks + // Add quit button function callback Quit->addCallback ( "clicked", diff --git a/test/listview.cpp b/test/listview.cpp index 3ee3e98b..129c8f64 100644 --- a/test/listview.cpp +++ b/test/listview.cpp @@ -10,7 +10,7 @@ //---------------------------------------------------------------------- -// class Listbox +// class Listview //---------------------------------------------------------------------- #pragma pack(push) @@ -35,6 +35,7 @@ class Listview : public FDialog // Callback methods void cb_exitApp (FWidget*, data_ptr); + void cb_showInMessagebox (FWidget*, data_ptr); }; #pragma pack(pop) @@ -123,6 +124,12 @@ Listview::Listview (FWidget* parent) "clicked", F_METHOD_CALLBACK (this, &Listview::cb_exitApp) ); + + listView->addCallback + ( + "clicked", + F_METHOD_CALLBACK (this, &Listview::cb_showInMessagebox) + ); } //---------------------------------------------------------------------- @@ -149,6 +156,19 @@ void Listview::cb_exitApp (FWidget*, data_ptr) close(); } +//---------------------------------------------------------------------- +void Listview::cb_showInMessagebox (FWidget* widget, data_ptr) +{ + FListView* listView = static_cast(widget);listView=listView; + FListViewItem* item = listView->getCurrentItem(); + FMessageBox info ( "Weather in " + item->getText(0) + , " Condition: " + item->getText(1) + "\n" + "Temperature: " + item->getText(2) + "\n" + " Humidity: " + item->getText(3) + "\n" + " Pressure: " + item->getText(4) + , FMessageBox::Ok, 0, 0, this ); + info.show(); +} //---------------------------------------------------------------------- // main part @@ -169,7 +189,7 @@ int main (int argc, char* argv[]) FApplication app(argc, argv); Listview d(&app); - d.setText (L"Listview"); + d.setText (L"Weather data"); d.setGeometry (int(1 + (app.getWidth() - 37) / 2), 3, 37, 20); d.setShadow();