File: checklist.cpp

package info (click to toggle)
finalcut 0.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,832 kB
  • sloc: cpp: 90,264; makefile: 546; sh: 412
file content (203 lines) | stat: -rw-r--r-- 6,565 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/***********************************************************************
* checklist.cpp - Example for FListView widget with checkboxes         *
*                                                                      *
* This file is part of the FINAL CUT widget toolkit                    *
*                                                                      *
* Copyright 2017-2022 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/>.                                      *
***********************************************************************/

#include <array>
#include <iostream>
#include <fstream>
#include <string>

#include <final/final.h>

using finalcut::FPoint;
using finalcut::FSize;


//----------------------------------------------------------------------
// class CheckList
//----------------------------------------------------------------------

class CheckList final : public finalcut::FDialog
{
  public:
    // Constructor
    explicit CheckList (finalcut::FWidget* = nullptr);

  private:
    // Method
    void populate();
    void initLayout() override;
    void adjustSize() override;

    // Event handlers
    void onKeyPress (finalcut::FKeyEvent*) override;
    void onClose (finalcut::FCloseEvent*) override;

    // Callback method
    void cb_showList();

    // Data members
    finalcut::FListView  listview{this};
    finalcut::FStatusBar status_bar{this};
};

//----------------------------------------------------------------------
CheckList::CheckList (finalcut::FWidget* parent)
  : finalcut::FDialog{parent}
{
  setShadow();  // Instead of the transparent window shadow
  listview.ignorePadding();

  // Add columns to the view
  listview.addColumn ("Item");
  listview.addColumn ("Priority", 9);

  // Set the type of sorting
  listview.setColumnSortType (1, finalcut::SortType::Name);
  listview.setColumnSortType (2, finalcut::SortType::Name);

  // Statusbar at the bottom
  finalcut::FString separator{};
  separator << ' ' << finalcut::UniChar::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", this, &CheckList::cb_showList);
}

//----------------------------------------------------------------------
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" }}
  }};

  for (const auto& line : list)
  {
    const finalcut::FStringList string_line (line.cbegin(), line.cend());
    auto iter = listview.insert (string_line);
    auto item = static_cast<finalcut::FListViewItem*>(*iter);
    item->setCheckable(true);
  }
}

//----------------------------------------------------------------------
void CheckList::initLayout()
{
  finalcut::FDialog::setText (L"Shopping list");
  const auto is_newfont = finalcut::FVTerm::getFOutput()->isNewFont();
  const std::size_t nf_offset = is_newfont ? 1 : 0;
  listview.setGeometry ( FPoint{1 + int(nf_offset), 2}
                       , FSize{getWidth() - nf_offset, getHeight() - 1} );
  finalcut::FDialog::initLayout();
}

//----------------------------------------------------------------------
void CheckList::adjustSize()
{
  const auto is_newfont = finalcut::FVTerm::getFOutput()->isNewFont();
  const std::size_t nf_offset = is_newfont ? 1 : 0;
  const std::size_t w{28 + nf_offset};
  const std::size_t h{13};
  const int x = 1 + int((getDesktopWidth() - getWidth()) / 2);
  const int y = 5;
  setGeometry (FPoint{x, y}, FSize{w, h}, false);
  finalcut::FDialog::adjustSize();
}

//----------------------------------------------------------------------
void CheckList::onKeyPress (finalcut::FKeyEvent* ev)
{
  if ( ! ev )
    return;

  if ( ev->key() == finalcut::FKey('q') || isEscapeKey(ev->key()) )
  {
    close();
    ev->accept();
  }
  else
    finalcut::FDialog::onKeyPress(ev);
}

//----------------------------------------------------------------------
void CheckList::onClose (finalcut::FCloseEvent* ev)
{
  finalcut::FApplication::closeConfirmationDialog (this, ev);
}

//----------------------------------------------------------------------
void CheckList::cb_showList()
{
  finalcut::FString shopping_list{};

  for (const auto& item : listview.getData())
  {
    if ( item->isChecked() )
      shopping_list << finalcut::UniChar::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
//----------------------------------------------------------------------

auto main (int argc, char* argv[]) -> int
{
  // Create the application object
  finalcut::FApplication app(argc, argv);

  // Create main dialog object
  CheckList d(&app);

  // Set dialog d as main widget
  finalcut::FWidget::setMainWidget(&d);

  // Show and start the application
  d.show();
  return app.exec();
}