finalcut/examples/checklist.cpp

219 lines
6.5 KiB
C++
Raw Normal View History

/***********************************************************************
* checklist.cpp - Example for FListView widget with checkboxes *
* *
* This file is part of the FINAL CUT widget toolkit *
* *
* Copyright 2017-2020 Markus Gans *
* *
* 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 *
* 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 *
* 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/>. *
***********************************************************************/
2020-10-08 16:46:14 +02:00
#include <array>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <final/final.h>
namespace fc = finalcut::fc;
using finalcut::FPoint;
using finalcut::FRect;
using finalcut::FSize;
//----------------------------------------------------------------------
// class CheckList
//----------------------------------------------------------------------
2020-04-14 23:46:42 +02:00
class CheckList final : public finalcut::FDialog
{
public:
// Using-declaration
using FDialog::setGeometry;
// Constructor
explicit CheckList (finalcut::FWidget* = nullptr);
// Disable copy constructor
CheckList (const CheckList&) = delete;
// Destructor
2020-02-19 21:59:13 +01:00
~CheckList() override;
// Disable copy assignment operator (=)
CheckList& operator = (const CheckList&) = delete;
private:
// Method
void populate();
void adjustSize() override;
// Event handlers
2019-08-06 23:45:28 +02:00
void onKeyPress (finalcut::FKeyEvent*) override;
void onClose (finalcut::FCloseEvent*) override;
// Callback method
2020-08-11 23:04:46 +02:00
void cb_showList();
// Data members
finalcut::FListView listview{this};
finalcut::FStatusBar status_bar{this};
};
//----------------------------------------------------------------------
CheckList::CheckList (finalcut::FWidget* parent)
: finalcut::FDialog{parent}
{
2020-04-19 20:38:52 +02:00
// Dialog settings
// Avoids calling a virtual function from the constructor
// (CERT, OOP50-CPP)
FDialog::setText (L"Shopping list");
const std::size_t nf_offset = ( finalcut::FTerm::isNewFont() ) ? 1 : 0;
FDialog::setSize (FSize{28 + nf_offset, 13} );
setShadow(); // Instead of the transparent window shadow
listview.ignorePadding();
listview.setGeometry ( FPoint{1 + int(nf_offset), 2}
, FSize{getWidth() - nf_offset, getHeight() - 1} );
// Add columns to the view
listview.addColumn ("Item");
listview.addColumn ("Priority", 9);
// Set the type of sorting
listview.setColumnSortType (1, fc::by_name);
listview.setColumnSortType (2, fc::by_name);
// Statusbar at the bottom
finalcut::FString separator{};
separator << ' ' << fc::BoxDrawingsVertical << ' ';
listview.setStatusbarMessage ( finalcut::FString{}
<< "<Q> exit" << separator
<< "<Space> select an item" << separator
<< "<Enter> see your pick list");
// Populate FListView with a list of items
populate();
// Add callback method
listview.addCallback
(
"clicked",
2020-08-11 23:04:46 +02:00
this, &CheckList::cb_showList
);
}
//----------------------------------------------------------------------
CheckList::~CheckList() // destructor
{ }
//----------------------------------------------------------------------
void CheckList::populate()
{
constexpr std::array<std::array<const char*, 2>, 10> list =
{{
{{ "Milk", "Highest" }},
{{ "Cheese", "High" }},
{{ "Yoghurt", "Medium" }},
{{ "Bread", "Low" }},
{{ "Eggs", "High" }},
{{ "Toothpaste", "Medium" }},
{{ "Apples", "Lowest" }},
{{ "Bananas", "Medium" }},
{{ "Fish", "Medium" }},
{{ "Lemons", "Low" }}
}};
2019-10-14 01:44:24 +02:00
for (const auto& line : list)
{
const finalcut::FStringList string_line (line.begin(), line.end());
auto iter = listview.insert (string_line);
auto item = static_cast<finalcut::FListViewItem*>(*iter);
item->setCheckable(true);
}
}
//----------------------------------------------------------------------
void CheckList::adjustSize()
{
finalcut::FDialog::adjustSize();
setPos(FPoint{int(1 + (getDesktopWidth() - getWidth()) / 2), 5});
}
//----------------------------------------------------------------------
void CheckList::onKeyPress (finalcut::FKeyEvent* ev)
{
if ( ! ev )
return;
if ( ev->key() == 'q'
|| ev->key() == fc::Fkey_escape
|| ev->key() == fc::Fkey_escape_mintty )
{
close();
ev->accept();
}
else
finalcut::FDialog::onKeyPress(ev);
}
//----------------------------------------------------------------------
void CheckList::onClose (finalcut::FCloseEvent* ev)
{
finalcut::FApplication::closeConfirmationDialog (this, ev);
}
//----------------------------------------------------------------------
2020-08-11 23:04:46 +02:00
void CheckList::cb_showList()
{
finalcut::FString shopping_list{};
for (auto item : listview.getData())
{
if ( item->isChecked() )
shopping_list << fc::Bullet << ' ' << item->getText(1) << '\n';
}
if ( shopping_list.isEmpty() )
return;
// Create and show tooltip for two seconds
finalcut::FToolTip picklist(this);
picklist.setText (shopping_list);
picklist.show();
sleep(2);
}
//----------------------------------------------------------------------
// main part
//----------------------------------------------------------------------
int main (int argc, char* argv[])
{
// Create the application object
finalcut::FApplication app(argc, argv);
// Create main dialog object
CheckList d(&app);
// 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();
}