finalcut/examples/listbox.cpp

206 lines
5.9 KiB
C++
Raw Normal View History

2017-11-04 07:03:53 +01:00
/***********************************************************************
* listbox.cpp - Example for using a FListBox widget *
* *
* This file is part of the FINAL CUT widget toolkit *
2017-11-04 07:03:53 +01:00
* *
* Copyright 2017-2021 Markus Gans *
2017-11-04 07:03:53 +01:00
* *
* FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of *
2017-11-04 07:03:53 +01:00
* the License, or (at your option) any later version. *
* *
* FINAL CUT is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
2017-11-04 07:03:53 +01:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
***********************************************************************/
#include <iostream>
2017-09-11 03:06:02 +02:00
#include <list>
#include <map>
2019-08-06 23:45:28 +02:00
#include <memory>
#include <fstream>
#include <final/final.h>
2018-12-26 23:41:49 +01:00
using namespace finalcut;
using finalcut::FPoint;
using finalcut::FSize;
// Global application object
static std::weak_ptr<FString> temp_str;
// Function prototypes
2018-12-26 23:41:49 +01:00
void doubleToItem ( FListBoxItem&
, FDataAccess* container
, std::size_t index);
2018-12-26 23:41:49 +01:00
FString& doubleToString (std::list<double>::const_iterator iter);
FString& mapToString ( std::map<FString
, FString>::const_iterator iter );
2019-10-05 23:20:07 +02:00
// Lazy conversion insert function
2018-12-26 23:41:49 +01:00
void doubleToItem ( FListBoxItem& item
, FDataAccess* container
, std::size_t index )
{
using DblList = std::list<double>;
2020-09-18 22:40:51 +02:00
DblList& dbl_list = flistboxhelper::getContainer<DblList>(container);
std::list<double>::iterator iter = dbl_list.begin();
std::advance (iter, index);
2018-12-26 23:41:49 +01:00
item.setText (FString() << *iter);
item.setData (*iter);
}
2019-10-05 23:20:07 +02:00
// Insert converter functions
2018-12-26 23:41:49 +01:00
FString& doubleToString (std::list<double>::const_iterator iter)
{
auto temp = temp_str.lock();
return temp->setNumber(*iter);
}
2018-12-26 23:41:49 +01:00
FString& mapToString ( std::map<FString
, FString>::const_iterator iter )
{
auto temp = temp_str.lock();
return *temp = iter->first + ": " + iter->second;
}
//----------------------------------------------------------------------
// class Listbox
//----------------------------------------------------------------------
2020-04-14 23:46:42 +02:00
class Listbox final : public FDialog
{
2017-09-11 03:06:02 +02:00
public:
// Constructor
explicit Listbox (FWidget* = nullptr);
// Disable copy constructor
Listbox (const Listbox&) = delete;
2017-09-11 03:06:02 +02:00
// Destructor
2020-11-24 21:06:39 +01:00
~Listbox() override = default;
2017-09-11 03:06:02 +02:00
// Disable copy assignment operator (=)
Listbox& operator = (const Listbox&) = delete;
2017-09-11 03:06:02 +02:00
private:
// Method
void initLayout() override;
2017-09-11 03:06:02 +02:00
// Event handlers
2019-08-06 23:45:28 +02:00
void onClose (FCloseEvent*) override;
2017-09-11 03:06:02 +02:00
// Data member
std::list<double> double_list{};
2018-12-26 23:41:49 +01:00
FListBox list1{this};
FListBox list2{this};
FListBox list3{this};
FButton quit{this};
};
//----------------------------------------------------------------------
2018-12-26 23:41:49 +01:00
Listbox::Listbox (FWidget* parent)
: FDialog{parent}
{
auto temp = std::make_shared<FString>();
temp_str = temp;
// listbox 1
//----------
list1.setText ("FListBoxItem");
2020-10-25 01:21:45 +02:00
for (auto i{1}; i < 30; i++)
list1.insert (L"----- " + (FString{} << i) + L" -----");
// listbox 2
//----------
2020-10-25 01:21:45 +02:00
for (auto i{1}; i <= 15; i++)
2020-04-13 12:40:11 +02:00
double_list.push_back(2 * double(i) + (double(i) / 100));
list2.setText ("double");
//
2019-10-05 23:20:07 +02:00
// Insert via lazy conversion on print
//
list2.insert (double_list, doubleToItem); // (container, converter)
//
2019-10-05 23:20:07 +02:00
// Direct insert of the complete list
//
//list2.insert (double_list.begin(), double_list.end(), doubleToString);
// listbox 3
//----------
2018-12-26 23:41:49 +01:00
std::map<FString, FString> TLD;
TLD["com"] = "Commercial";
TLD["org"] = "Organization";
TLD["net"] = "Network";
TLD["edu"] = "Education";
TLD["gov"] = "Government";
list3.insert (TLD.begin(), TLD.end(), mapToString);
list3.setText ("key: value");
// Quit button
quit.setText (L"&Quit");
// Add quit button function callback
quit.addCallback
(
"clicked",
2020-08-11 23:04:46 +02:00
finalcut::getFApplication(),
&finalcut::FApplication::cb_exitApp,
this
);
}
//----------------------------------------------------------------------
void Listbox::initLayout()
{
list1.setGeometry(FPoint{2, 1}, FSize{18, 10});
list2.setGeometry(FPoint{21, 1}, FSize{10, 10});
list3.setGeometry(FPoint{32, 1}, FSize{21, 10});
quit.setGeometry(FPoint{42, 12}, FSize{10, 1});
FDialog::initLayout();
}
//----------------------------------------------------------------------
2018-12-26 23:41:49 +01:00
void Listbox::onClose (FCloseEvent* ev)
{
2018-12-26 23:41:49 +01:00
FApplication::closeConfirmationDialog (this, ev);
}
//----------------------------------------------------------------------
// main part
//----------------------------------------------------------------------
int main (int argc, char* argv[])
{
// Create the application object
2018-12-26 23:41:49 +01:00
FApplication app(argc, argv);
// Create main dialog object
Listbox d(&app);
d.setText (L"Listbox");
d.setGeometry ( FPoint{int(1 + (app.getWidth() - 56) / 2), 5}
, FSize{56, 16} );
d.setShadow();
// Set dialog d as main widget
2020-04-13 12:40:11 +02:00
finalcut::FWidget::setMainWidget(&d);
// Show and start the application
d.show();
return app.exec();
}