FListBox: Import of data from a container via "lazy conversion" during item print

This commit is contained in:
Markus Gans 2017-05-19 22:16:50 +02:00
parent ca7ba6ae7e
commit b3505af040
4 changed files with 82 additions and 17 deletions

View File

@ -1,3 +1,7 @@
2017-05-19 Markus Gans <guru.mail@muenster.de>
* FListBox: Import of data from a container via
"lazy conversion" during item print.
2017-04-23 Markus Gans <guru.mail@muenster.de> 2017-04-23 Markus Gans <guru.mail@muenster.de>
* Import of data from a standard container in FListBox * Import of data from a standard container in FListBox
is now possible is now possible
@ -7,7 +11,7 @@
2017-04-17 Markus Gans <guru.mail@muenster.de> 2017-04-17 Markus Gans <guru.mail@muenster.de>
* Speed up FString::setNumber() by using a decimal * Speed up FString::setNumber() by using a decimal
string lookup table string lookup table
* FString allocates no new memory if the size in sufficient * FString allocates no new memory if the size is sufficient
2017-04-15 Markus Gans <guru.mail@muenster.de> 2017-04-15 Markus Gans <guru.mail@muenster.de>
* Fix unsigned integer underflow in FString::_insert() * Fix unsigned integer underflow in FString::_insert()

View File

@ -7,9 +7,6 @@
#include "fscrollbar.h" #include "fscrollbar.h"
#include "fstatusbar.h" #include "fstatusbar.h"
// function pointer
FString& (*FListBox::getString)(FListBox::listBoxItems::iterator);
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// class FListBoxItem // class FListBoxItem
@ -71,7 +68,10 @@ FListBoxItem& FListBoxItem::operator = (const FListBoxItem& item)
//---------------------------------------------------------------------- //----------------------------------------------------------------------
FListBox::FListBox (FWidget* parent) FListBox::FListBox (FWidget* parent)
: FWidget(parent) : FWidget(parent)
, convertToItem(0)
, data() , data()
, source_container(0)
, conv_type(FListBox::no_convert)
, vbar(0) , vbar(0)
, hbar(0) , hbar(0)
, text() , text()
@ -1459,7 +1459,7 @@ void FListBox::adjustSize()
// private methods of FListBox // private methods of FListBox
//---------------------------------------------------------------------- //----------------------------------------------------------------------
FString& FListBox::getString_FListBoxItem (listBoxItems::iterator iter) inline FString& FListBox::getString (listBoxItems::iterator iter)
{ {
return iter->getText(); return iter->getText();
} }
@ -1505,9 +1505,6 @@ void FListBox::init()
setLeftPadding(1); setLeftPadding(1);
setBottomPadding(1); setBottomPadding(1);
setRightPadding(1 + nf_offset); setRightPadding(1 + nf_offset);
// set the new getString function pointer
getString = &FListBox::getString_FListBoxItem;
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
@ -1634,6 +1631,9 @@ void FListBox::drawList()
bool isLineSelected = isSelected(iter); bool isLineSelected = isSelected(iter);
bool isCurrentLine = bool(y + uInt(yoffset) + 1 == uInt(current)); bool isCurrentLine = bool(y + uInt(yoffset) + 1 == uInt(current));
if ( conv_type == lazy_convert && iter->getText().isNull() )
convertToItem (*iter, source_container, int(y) + yoffset);
if ( isLineSelected ) if ( isLineSelected )
{ {
if ( isMonochron() ) if ( isMonochron() )

View File

@ -60,7 +60,6 @@ class FListBoxItem
virtual FString& getText(); virtual FString& getText();
virtual FWidget::data_ptr getData() const; virtual FWidget::data_ptr getData() const;
protected:
// Mutators // Mutators
void setText (const FString&); void setText (const FString&);
void setData (FWidget::data_ptr); void setData (FWidget::data_ptr);
@ -162,6 +161,8 @@ class FListBox : public FWidget
void hide(); void hide();
template <class Iterator, class InsertConverter> template <class Iterator, class InsertConverter>
void insert (Iterator, Iterator, InsertConverter); void insert (Iterator, Iterator, InsertConverter);
template <class Container, class LazyConverter>
void insert (Container, LazyConverter);
void insert (FListBoxItem); void insert (FListBoxItem);
void insert ( const FString& void insert ( const FString&
, fc::brackets_type = fc::NoBrackets , fc::brackets_type = fc::NoBrackets
@ -205,6 +206,13 @@ class FListBox : public FWidget
scrollDownSelect = 4 scrollDownSelect = 4
}; };
enum convert_type
{
no_convert = 0,
direct_convert = 1,
lazy_convert = 2
};
// Disable copy constructor // Disable copy constructor
FListBox (const FListBox&); FListBox (const FListBox&);
@ -212,8 +220,7 @@ class FListBox : public FWidget
FListBox& operator = (const FListBox&); FListBox& operator = (const FListBox&);
// Accessors // Accessors
static FString& getString_FListBoxItem (listBoxItems::iterator); static FString& getString (listBoxItems::iterator);
static FString& (*getString)(listBoxItems::iterator);
// Methods // Methods
void init(); void init();
@ -225,8 +232,15 @@ class FListBox : public FWidget
void processChanged(); void processChanged();
listBoxItems::iterator index2iterator (int); listBoxItems::iterator index2iterator (int);
// Function Pointer
void (*convertToItem) ( FListBoxItem&
, FWidget::data_ptr
, int index );
// Data Members // Data Members
listBoxItems data; listBoxItems data;
FWidget::data_ptr source_container;
convert_type conv_type;
FScrollbar* vbar; FScrollbar* vbar;
FScrollbar* hbar; FScrollbar* hbar;
FString text; FString text;
@ -257,7 +271,10 @@ inline FListBox::FListBox ( Iterator first
, InsertConverter convert , InsertConverter convert
, FWidget* parent ) , FWidget* parent )
: FWidget(parent) : FWidget(parent)
, convertToItem(0)
, data() , data()
, source_container(0)
, conv_type(FListBox::no_convert)
, vbar(0) , vbar(0)
, hbar(0) , hbar(0)
, text() , text()
@ -395,6 +412,8 @@ inline void FListBox::insert ( Iterator first
, Iterator last , Iterator last
, InsertConverter convert ) , InsertConverter convert )
{ {
conv_type = direct_convert;
while ( first != last ) while ( first != last )
{ {
insert (convert(first), fc::NoBrackets, false, &(*first)); insert (convert(first), fc::NoBrackets, false, &(*first));
@ -402,6 +421,19 @@ inline void FListBox::insert ( Iterator first
} }
} }
//----------------------------------------------------------------------
template <class Container, class LazyConverter>
void FListBox::insert (Container container, LazyConverter convert)
{
conv_type = lazy_convert;
source_container = container;
convertToItem = convert;
size_t size = container->size();
if ( size > 0 )
data.resize(size);
}
//---------------------------------------------------------------------- //----------------------------------------------------------------------
inline FListBox::listBoxItems::iterator FListBox::index2iterator (int index) inline FListBox::listBoxItems::iterator FListBox::index2iterator (int index)
{ {

View File

@ -3,15 +3,31 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include "final.h" #include "fapp.h"
#include "fdialog.h"
#include "flistbox.h"
#include "fmessagebox.h"
// Global application object // Global application object
static FString* temp_str = 0; static FString* temp_str = 0;
// Function prototypes // Function prototypes
void doubleToItem (FListBoxItem&, FWidget::data_ptr container, int index);
FString& doubleToString (std::list<double>::const_iterator iter); FString& doubleToString (std::list<double>::const_iterator iter);
FString& mapToString (std::map<FString, FString>::const_iterator iter); FString& mapToString (std::map<FString, FString>::const_iterator iter);
// Lazy conversion import function
void doubleToItem (FListBoxItem& item, FWidget::data_ptr container, int index)
{
typedef std::list<double>* double_list_ptr;
double_list_ptr dbllist = static_cast<double_list_ptr>(container);
std::list<double>::iterator iter = dbllist->begin();
std::advance (iter, index);
item.setText (FString().setNumber(*iter));
item.setData (FWidget::data_ptr(&(*iter)));
}
// Import converter functions // Import converter functions
FString& doubleToString (std::list<double>::const_iterator iter) FString& doubleToString (std::list<double>::const_iterator iter)
{ {
@ -50,12 +66,16 @@ class Listbox : public FDialog
// Callback methods // Callback methods
void cb_exitApp (FWidget*, data_ptr); void cb_exitApp (FWidget*, data_ptr);
// Data Member
std::list<double>* double_list;
}; };
#pragma pack(pop) #pragma pack(pop)
//---------------------------------------------------------------------- //----------------------------------------------------------------------
Listbox::Listbox (FWidget* parent) Listbox::Listbox (FWidget* parent)
: FDialog(parent) : FDialog(parent)
, double_list()
{ {
temp_str = new FString; temp_str = new FString;
@ -72,12 +92,20 @@ Listbox::Listbox (FWidget* parent)
list2->setGeometry(21, 1, 10, 10); list2->setGeometry(21, 1, 10, 10);
list2->setText ("double"); list2->setText ("double");
std::list<double> double_list; double_list = new std::list<double>;
for (double i=1; i<=15; i++) for (double i=1; i<=15; i++)
double_list.push_back(2*i + (i/100)); double_list->push_back(2*i + (i/100));
list2->insert (double_list.begin(), double_list.end(), doubleToString); //
// Import via lazy conversion on print
//
//list2->insert (double_list, doubleToItem);
//
// Direct import of the complete list
//
list2->insert (double_list->begin(), double_list->end(), doubleToString);
// listbox 3 // listbox 3
std::map<FString, FString> TLD; std::map<FString, FString> TLD;
@ -109,6 +137,7 @@ Listbox::Listbox (FWidget* parent)
Listbox::~Listbox() Listbox::~Listbox()
{ {
delete temp_str; delete temp_str;
delete double_list;
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------