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>
* Possibility for a FListView column to set
the alignment (left, center or right)

View File

@ -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;

View File

@ -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<FString>& 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<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
@ -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)
{

View File

@ -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<FString> 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)
{

View File

@ -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",

View File

@ -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<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
@ -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();