Created
December 13, 2020 19:53
-
-
Save anaselli/ef812d011f0669d5902fac8d522f245c to your computer and use it in GitHub Desktop.
A test example to manage sorting on YTable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// g++ -I/usr/include/yui -lyui testSortYTable.cc -o testSortYTable | |
#include <sstream> | |
#include "YUI.h" | |
#include "YApplication.h" | |
#include "YWidgetFactory.h" | |
#include "YDialog.h" | |
#include "YLayoutBox.h" | |
#include "YEvent.h" | |
#include "YTable.h" | |
#include "YTableHeader.h" | |
#include "YCheckBoxFrame.h" | |
#include "YWidget.h" | |
#include "YTimeField.h" | |
#include "YAlignment.h" | |
#include "YPushButton.h" | |
struct obj { | |
std::string name; | |
std::string description; | |
double size; // double if we want to have MB or KB instead of byte that are big for data games for instance | |
obj (const std::string& n, const std::string& d, double s) : name(n), description(d), size(s) {} | |
}; | |
bool compareByASize(const struct obj &a, const struct obj &b) | |
{ | |
return a.size < b.size; | |
} | |
bool compareByDSize(const struct obj &a, const struct obj &b) | |
{ | |
return b.size < a.size; | |
} | |
int main( int argc, char **argv ) | |
{ | |
// let's create our db | |
// don't mind to have a pointer right now | |
std::vector<struct obj> objList { | |
obj("test", "A test object", 1234.5), | |
obj("good", "A good object", 100), | |
obj("bad", "A bad object", 8.2), | |
obj("fun", "A fun object", 12), | |
obj("awful", "An awful object", 3), | |
obj("useless", "An useless object", 2430) | |
}; | |
YDialog * dialog = YUI::widgetFactory()->createPopupDialog(); | |
YLayoutBox * vbox = YUI::widgetFactory()->createVBox( dialog ); | |
YAlignment * minSize = YUI::widgetFactory()->createMinSize( vbox, 60, 10 ); // minWidth, minHeight | |
YUI::widgetFactory()->createLabel ( vbox, "YTable test" ); | |
YTableItem *pItem; | |
YTableHeader* head = new YTableHeader; | |
head->addColumn( "Name", YAlignBegin ); | |
head->addColumn( "Description", YAlignBegin ); | |
head->addColumn( "Size", YAlignEnd ); | |
head->addColumn( "Size (KB)", YAlignEnd ); | |
YTable* table = YUI::widgetFactory()->createTable( YUI::widgetFactory()->createLeft(minSize), head ); | |
// NOTE: let's let the item insertion order | |
table->setKeepSorting( true ); | |
table->setNotify( true ); | |
YItemCollection items; | |
for(struct obj o : objList) { | |
// don't care about precision right now | |
std::string sz = std::to_string(o.size); | |
items.push_back(new YTableItem( o.name, o.description, sz + "K", sz) ); | |
} | |
table->addItems( items ); | |
YLayoutBox * hbox =YUI::widgetFactory()->createHBox( vbox ); | |
YPushButton* sortAButton = YUI::widgetFactory()->createPushButton( hbox, "Sort Size Ascending" ); | |
YPushButton* sortDButton = YUI::widgetFactory()->createPushButton( hbox, "Sort Size Descending" ); | |
YPushButton* quitButton = YUI::widgetFactory()->createPushButton( vbox, "&Quit" ); | |
while ( true ) | |
{ | |
YEvent * event = dialog->waitForEvent(); | |
if ( event ) | |
{ | |
// window manager "close window" button | |
if ( event->eventType() == YEvent::CancelEvent | |
|| event->widget() == quitButton ) | |
break; // leave event loop | |
else if ( event->widget() == sortAButton ) | |
{ | |
std::sort(objList.begin(), objList.end(), compareByASize); | |
table->deleteAllItems(); | |
YItemCollection items; | |
for(struct obj o : objList) { | |
// don't care about precision right now | |
std::string sz = std::to_string(o.size); | |
items.push_back(new YTableItem( o.name, o.description, sz + "K", sz) ); | |
} | |
table->addItems( items ); | |
} | |
else if ( event->widget() == sortDButton ) | |
{ | |
std::sort(objList.begin(), objList.end(), compareByDSize); | |
table->deleteAllItems(); | |
YItemCollection items; | |
for(struct obj o : objList) { | |
// don't care about precision right now | |
std::string sz = std::to_string(o.size); | |
items.push_back(new YTableItem( o.name, o.description, sz + "K", sz) ); | |
} | |
table->addItems( items ); | |
} | |
} | |
} | |
dialog->destroy(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment