finalcut/examples/watch.cpp

224 lines
6.0 KiB
C++
Raw Normal View History

2017-11-04 07:03:53 +01:00
/***********************************************************************
* watch.cpp - A watch with FSwitch widgets *
* *
* This file is part of the FINAL CUT widget toolkit *
2017-11-04 07:03:53 +01:00
* *
* Copyright 2015-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 <ctime>
#include <final/final.h>
using finalcut::FPoint;
using finalcut::FSize;
//----------------------------------------------------------------------
2017-09-20 06:19:27 +02:00
// class Watch
//----------------------------------------------------------------------
2020-04-14 23:46:42 +02:00
class Watch final : public finalcut::FDialog
{
2017-09-11 03:06:02 +02:00
public:
// Constructor
explicit Watch (finalcut::FWidget* = nullptr);
// Disable copy constructor
Watch (const Watch&) = delete;
2017-09-11 03:06:02 +02:00
// Destructor
2020-11-24 21:06:39 +01:00
~Watch() override = default;
2017-09-11 03:06:02 +02:00
// Disable copy assignment operator (=)
Watch& operator = (const Watch&) = delete;
2017-09-11 03:06:02 +02:00
// Method
void printTime();
// Event handlers
2019-08-06 23:45:28 +02:00
void onTimer (finalcut::FTimerEvent*) override;
void onClose (finalcut::FCloseEvent*) override;
2017-09-11 03:06:02 +02:00
// Callback methods
2020-08-11 23:04:46 +02:00
void cb_clock();
void cb_seconds();
2017-09-11 03:06:02 +02:00
protected:
// Method
void initLayout() override;
2019-08-06 23:45:28 +02:00
void adjustSize() override;
2017-09-11 03:06:02 +02:00
private:
// Data members
bool sec{true};
finalcut::FLabel time_label{L"Time", this};
finalcut::FLabel time_str{L"--:--:--", this};
finalcut::FSwitch clock_sw{L"Clock", this};
finalcut::FSwitch seconds_sw{L"Seconds", this};
finalcut::FButton quit_btn{L"&Quit", this};
};
//----------------------------------------------------------------------
2017-09-20 06:19:27 +02:00
Watch::Watch (FWidget* parent)
: finalcut::FDialog{parent}
{
// Labels
time_label.setEmphasis();
// Switch
sec = seconds_sw.setChecked();
// Connect switch signal "toggled" with a callback member function
clock_sw.addCallback
(
"toggled",
2020-08-11 23:04:46 +02:00
this, &Watch::cb_clock
);
// Connect switch signal "toggled" with a callback member function
seconds_sw.addCallback
(
"toggled",
2020-08-11 23:04:46 +02:00
this, &Watch::cb_seconds
);
// Connect button signal "clicked" with a callback member function
quit_btn.addCallback
(
"clicked",
2020-08-11 23:04:46 +02:00
finalcut::getFApplication(),
&finalcut::FApplication::cb_exitApp,
this
);
}
//----------------------------------------------------------------------
2017-09-20 06:19:27 +02:00
void Watch::printTime()
{
2019-08-25 22:16:00 +02:00
finalcut::FString str{};
std::tm now{};
2020-02-19 21:59:13 +01:00
const std::time_t t = std::time(nullptr); // get current time
2017-09-11 03:06:02 +02:00
localtime_r(&t, &now);
if ( sec )
2017-09-11 03:06:02 +02:00
str.sprintf("%02d:%02d:%02d", now.tm_hour, now.tm_min, now.tm_sec);
else
2017-09-11 03:06:02 +02:00
str.sprintf("%02d:%02d ", now.tm_hour, now.tm_min);
time_str = str;
time_str.redraw();
}
//----------------------------------------------------------------------
void Watch::onTimer (finalcut::FTimerEvent*)
{
printTime();
}
//----------------------------------------------------------------------
void Watch::onClose (finalcut::FCloseEvent* ev)
{
finalcut::FApplication::closeConfirmationDialog (this, ev);
}
//----------------------------------------------------------------------
2020-08-11 23:04:46 +02:00
void Watch::cb_clock()
{
if ( clock_sw.isChecked() )
{
printTime();
addTimer(1000); // Call onTimer() every second (1000 ms)
}
else
{
2020-07-19 14:15:02 +02:00
delAllTimers(); // Delete all timers and stop updating the time
time_str = "--:--:--";
time_str.redraw();
}
}
//----------------------------------------------------------------------
2020-08-11 23:04:46 +02:00
void Watch::cb_seconds()
{
if ( seconds_sw.isChecked() )
sec = true;
else
sec = false;
if ( clock_sw.isChecked() )
printTime();
else
{
if ( sec )
time_str = "--:--:--";
else
time_str = "--:-- ";
time_str.redraw();
}
}
//----------------------------------------------------------------------
void Watch::initLayout()
{
// Dialog settings
FDialog::setText ("Watch");
FDialog::setSize ({22, 13}, false);
// Labels
time_label.setGeometry(FPoint{5, 2}, FSize{5, 1});
time_str.setGeometry(FPoint{10, 2}, FSize{8, 1});
// Switches
clock_sw.setGeometry(FPoint{4, 4}, FSize{9, 1});
seconds_sw.setGeometry(FPoint{2, 6}, FSize{11, 1});
// Quit button
quit_btn.setGeometry(FPoint{6, 9}, FSize{9, 1});
FDialog::initLayout();
}
//----------------------------------------------------------------------
2017-09-20 06:19:27 +02:00
void Watch::adjustSize()
{
2020-10-04 02:55:15 +02:00
const auto pw = int(getDesktopWidth());
setX (1 + (pw - 22) / 2, false);
setY (3, false);
finalcut::FDialog::adjustSize();
}
//----------------------------------------------------------------------
// main part
//----------------------------------------------------------------------
int main (int argc, char* argv[])
{
// Create the application object
finalcut::FApplication app{argc, argv};
// Create a simple dialog box
Watch w{&app};
// Set dialog w as main widget
2020-04-13 12:40:11 +02:00
finalcut::FWidget::setMainWidget(&w);
// Show and start the application
w.show();
return app.exec();
}