New methods to set and get FListViewItem and FListView text for a column

This commit is contained in:
Markus Gans 2017-08-01 00:56:12 +02:00
parent 0240d782ca
commit ba17c529ff
6 changed files with 117 additions and 12 deletions

View File

@ -1,3 +1,7 @@
2017-07-31 Markus Gans <guru.mail@muenster.de>
* New methods to retrieve or modify FListViewItem text or
a FListView column text for a specific column
2017-07-28 Markus Gans <guru.mail@muenster.de> 2017-07-28 Markus Gans <guru.mail@muenster.de>
* Possibility for a FListView column to set * Possibility for a FListView column to set
the alignment (left, center or right) the alignment (left, center or right)

View File

@ -338,7 +338,7 @@ inline uInt FListBox::getCount() const
{ return uInt(data.size()); } { return uInt(data.size()); }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
inline FListBoxItem FListBox::getItem (int index) //const inline FListBoxItem FListBox::getItem (int index)
{ {
listBoxItems::iterator iter = index2iterator(index - 1); listBoxItems::iterator iter = index2iterator(index - 1);
return *iter; return *iter;

View File

@ -9,6 +9,9 @@
#include "ftermbuffer.h" #include "ftermbuffer.h"
// static class attributes
FString FListView::empty_string = FString("");
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// class FListViewItem // class FListViewItem
//---------------------------------------------------------------------- //----------------------------------------------------------------------
@ -76,6 +79,39 @@ FListViewItem::FListViewItem ( const std::vector<FString>& cols
FListViewItem::~FListViewItem() 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<FListView*>(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 // class FListView
@ -113,16 +149,27 @@ FListView::~FListView() // destructor
// public methods of FListView // 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 // 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 fc::alignLeft;
return header[uInt(column)].alignment; 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) 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; 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) int FListView::addColumn (const FString& label, int width)
{ {

View File

@ -64,14 +64,18 @@ class FListViewItem : public FObject
// Accessors // Accessors
const char* getClassName() const; const char* getClassName() const;
uInt getCount() const; uInt getCount() const;
FString getText (int) const;
// Mutator
void setText (int, const FString&);
private: private:
// Friend classes
friend class FListView;
// Data Member // Data Member
std::vector<FString> column_line; std::vector<FString> column_line;
FWidget::data_ptr data_pointer; FWidget::data_ptr data_pointer;
// Friend class
friend class FListView;
}; };
#pragma pack(pop) #pragma pack(pop)
@ -110,11 +114,14 @@ class FListView : public FWidget
// Accessors // Accessors
const char* getClassName() const; const char* getClassName() const;
fc::text_alignment getColumnAlignment (int); fc::text_alignment getColumnAlignment (int) const;
FString getColumnText (int) const;
FListViewItem* getCurrentItem() const;
// Mutators // Mutators
void setGeometry (int, int, int, int, bool = true); void setGeometry (int, int, int, int, bool = true);
void setColumnAlignment (int, fc::text_alignment); void setColumnAlignment (int, fc::text_alignment);
void setColumnText (int, const FString&);
// Methods // Methods
virtual int addColumn (const FString&, int = USE_MAX_SIZE); virtual int addColumn (const FString&, int = USE_MAX_SIZE);
@ -150,7 +157,7 @@ class FListView : public FWidget
Header() Header()
: name() : name()
, width (0) , width (0)
, fixed_width (-1) , fixed_width (false)
, alignment (fc::alignLeft) , alignment (fc::alignLeft)
{ } { }
@ -191,6 +198,7 @@ class FListView : public FWidget
void cb_HBarChange (FWidget*, data_ptr); void cb_HBarChange (FWidget*, data_ptr);
// Data Members // Data Members
static FString empty_string;
listViewItems data; listViewItems data;
headerItems header; headerItems header;
FTermBuffer headerline; FTermBuffer headerline;
@ -205,6 +213,9 @@ class FListView : public FWidget
int yoffset; int yoffset;
int nf_offset; int nf_offset;
int max_line_width; int max_line_width;
// Friend class
friend class FListViewItem;
}; };
#pragma pack(pop) #pragma pack(pop)
@ -214,6 +225,10 @@ class FListView : public FWidget
inline const char* FListView::getClassName() const inline const char* FListView::getClassName() const
{ return "FListView"; } { return "FListView"; }
//----------------------------------------------------------------------
inline FListViewItem* FListView::getCurrentItem() const
{ return data[current-1]; }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
inline FListView::listViewItems::iterator FListView::index2iterator (int index) inline FListView::listViewItems::iterator FListView::index2iterator (int index)
{ {

View File

@ -64,7 +64,7 @@ class Listbox : public FDialog
// Event handlers // Event handlers
void onClose (FCloseEvent*); void onClose (FCloseEvent*);
// Callback methods // Callback method
void cb_exitApp (FWidget*, data_ptr); void cb_exitApp (FWidget*, data_ptr);
// Data Member // Data Member
@ -125,7 +125,7 @@ Listbox::Listbox (FWidget* parent)
Quit->setGeometry(42, 12, 10, 1); Quit->setGeometry(42, 12, 10, 1);
Quit->setText (L"&Quit"); Quit->setText (L"&Quit");
// Add some function callbacks // Add quit button function callback
Quit->addCallback Quit->addCallback
( (
"clicked", "clicked",

View File

@ -10,7 +10,7 @@
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// class Listbox // class Listview
//---------------------------------------------------------------------- //----------------------------------------------------------------------
#pragma pack(push) #pragma pack(push)
@ -35,6 +35,7 @@ class Listview : public FDialog
// Callback methods // Callback methods
void cb_exitApp (FWidget*, data_ptr); void cb_exitApp (FWidget*, data_ptr);
void cb_showInMessagebox (FWidget*, data_ptr);
}; };
#pragma pack(pop) #pragma pack(pop)
@ -123,6 +124,12 @@ Listview::Listview (FWidget* parent)
"clicked", "clicked",
F_METHOD_CALLBACK (this, &Listview::cb_exitApp) 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(); close();
} }
//----------------------------------------------------------------------
void Listview::cb_showInMessagebox (FWidget* widget, data_ptr)
{
FListView* listView = static_cast<FListView*>(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 // main part
@ -169,7 +189,7 @@ int main (int argc, char* argv[])
FApplication app(argc, argv); FApplication app(argc, argv);
Listview d(&app); Listview d(&app);
d.setText (L"Listview"); d.setText (L"Weather data");
d.setGeometry (int(1 + (app.getWidth() - 37) / 2), 3, 37, 20); d.setGeometry (int(1 + (app.getWidth() - 37) / 2), 3, 37, 20);
d.setShadow(); d.setShadow();