2017-11-04 07:03:53 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* watch.cpp - A watch with FSwitch widgets *
|
|
|
|
* *
|
2020-07-08 21:32:47 +02:00
|
|
|
* This file is part of the FINAL CUT widget toolkit *
|
2017-11-04 07:03:53 +01:00
|
|
|
* *
|
2020-02-02 22:34:27 +01:00
|
|
|
* Copyright 2015-2020 Markus Gans *
|
2017-11-04 07:03:53 +01:00
|
|
|
* *
|
2020-07-08 21:32:47 +02: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. *
|
|
|
|
* *
|
2020-07-08 21:32:47 +02:00
|
|
|
* 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/>. *
|
|
|
|
***********************************************************************/
|
2015-07-04 23:05:28 +02:00
|
|
|
|
|
|
|
#include <ctime>
|
2017-10-31 00:41:59 +01:00
|
|
|
#include <final/final.h>
|
2015-07-04 23:05:28 +02:00
|
|
|
|
2019-01-21 03:42:18 +01:00
|
|
|
using finalcut::FPoint;
|
|
|
|
using finalcut::FSize;
|
|
|
|
|
2017-10-02 07:32:33 +02:00
|
|
|
|
2015-07-04 23:05:28 +02:00
|
|
|
//----------------------------------------------------------------------
|
2017-09-20 06:19:27 +02:00
|
|
|
// class Watch
|
2015-07-04 23:05:28 +02:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2020-04-14 23:46:42 +02:00
|
|
|
class Watch final : public finalcut::FDialog
|
2015-07-04 23:05:28 +02:00
|
|
|
{
|
2017-09-11 03:06:02 +02:00
|
|
|
public:
|
|
|
|
// Constructor
|
2018-12-10 01:48:26 +01:00
|
|
|
explicit Watch (finalcut::FWidget* = nullptr);
|
2018-12-24 18:11:16 +01:00
|
|
|
|
2018-12-09 22:04:55 +01:00
|
|
|
// Disable copy constructor
|
|
|
|
Watch (const Watch&) = delete;
|
2018-12-24 18:11:16 +01:00
|
|
|
|
2017-09-11 03:06:02 +02:00
|
|
|
// Destructor
|
2020-02-19 21:59:13 +01:00
|
|
|
~Watch() override;
|
2017-09-11 03:06:02 +02:00
|
|
|
|
2020-04-15 23:44:08 +02:00
|
|
|
// Disable copy assignment operator (=)
|
2018-12-09 22:04:55 +01:00
|
|
|
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-04-13 12:40:11 +02:00
|
|
|
void cb_clock (const finalcut::FWidget*, const FDataPtr);
|
|
|
|
void cb_seconds (const finalcut::FWidget*, const FDataPtr);
|
2017-09-11 03:06:02 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
// Method
|
2019-08-06 23:45:28 +02:00
|
|
|
void adjustSize() override;
|
2017-09-11 03:06:02 +02:00
|
|
|
|
|
|
|
private:
|
2019-09-04 23:57:31 +02:00
|
|
|
// Data members
|
2018-12-03 03:22:36 +01:00
|
|
|
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};
|
2015-07-04 23:05:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-09-20 06:19:27 +02:00
|
|
|
Watch::Watch (FWidget* parent)
|
2020-05-24 23:55:08 +02:00
|
|
|
: finalcut::FDialog{parent}
|
2015-07-04 23:05:28 +02:00
|
|
|
{
|
2020-04-19 20:38:52 +02:00
|
|
|
// Dialog settings
|
|
|
|
// Avoids calling a virtual function from the constructor
|
|
|
|
// (CERT, OOP50-CPP)
|
|
|
|
FDialog::setText ("Watch");
|
2020-02-02 22:34:27 +01:00
|
|
|
const int pw = int(getParentWidget()->getWidth());
|
2020-05-02 00:07:35 +02:00
|
|
|
FDialog::setGeometry (FPoint{1 + (pw - 22) / 2, 3}, FSize{22, 13});
|
2015-07-04 23:05:28 +02:00
|
|
|
|
2018-10-03 22:23:55 +02:00
|
|
|
// Labels
|
2020-05-02 00:07:35 +02:00
|
|
|
time_label.setGeometry(FPoint{5, 2}, FSize{5, 1});
|
2018-10-03 22:23:55 +02:00
|
|
|
time_label.setEmphasis();
|
2020-05-02 00:07:35 +02:00
|
|
|
time_str.setGeometry(FPoint{10, 2}, FSize{8, 1});
|
2015-07-04 23:05:28 +02:00
|
|
|
|
2018-10-03 22:23:55 +02:00
|
|
|
// Checkbox buttons
|
2020-05-02 00:07:35 +02:00
|
|
|
clock_sw.setGeometry(FPoint{4, 4}, FSize{9, 1});
|
|
|
|
seconds_sw.setGeometry(FPoint{2, 6}, FSize{11, 1});
|
2018-10-03 22:23:55 +02:00
|
|
|
sec = seconds_sw.setChecked();
|
2015-07-04 23:05:28 +02:00
|
|
|
|
2018-10-03 22:23:55 +02:00
|
|
|
// Quit button
|
2020-05-02 00:07:35 +02:00
|
|
|
quit_btn.setGeometry(FPoint{6, 9}, FSize{9, 1});
|
2015-07-04 23:05:28 +02:00
|
|
|
|
|
|
|
// Connect switch signal "toggled" with a callback member function
|
2018-10-03 22:23:55 +02:00
|
|
|
clock_sw.addCallback
|
2015-07-04 23:05:28 +02:00
|
|
|
(
|
|
|
|
"toggled",
|
2017-09-20 06:19:27 +02:00
|
|
|
F_METHOD_CALLBACK (this, &Watch::cb_clock)
|
2015-07-04 23:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Connect switch signal "toggled" with a callback member function
|
2018-10-03 22:23:55 +02:00
|
|
|
seconds_sw.addCallback
|
2015-07-04 23:05:28 +02:00
|
|
|
(
|
|
|
|
"toggled",
|
2017-09-20 06:19:27 +02:00
|
|
|
F_METHOD_CALLBACK (this, &Watch::cb_seconds)
|
2015-07-04 23:05:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Connect button signal "clicked" with a callback member function
|
2018-10-03 22:23:55 +02:00
|
|
|
quit_btn.addCallback
|
2015-07-04 23:05:28 +02:00
|
|
|
(
|
|
|
|
"clicked",
|
2018-09-20 23:59:01 +02:00
|
|
|
F_METHOD_CALLBACK (this, &finalcut::FApplication::cb_exitApp)
|
2015-07-04 23:05:28 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-09-20 06:19:27 +02:00
|
|
|
Watch::~Watch()
|
2020-04-13 12:40:11 +02:00
|
|
|
{ }
|
2015-07-04 23:05:28 +02:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-09-20 06:19:27 +02:00
|
|
|
void Watch::printTime()
|
2015-07-04 23:05:28 +02:00
|
|
|
{
|
2019-08-25 22:16:00 +02:00
|
|
|
finalcut::FString str{};
|
|
|
|
std::tm now{};
|
2015-08-11 00:11:07 +02:00
|
|
|
|
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);
|
2015-07-04 23:05:28 +02:00
|
|
|
|
|
|
|
if ( sec )
|
2017-09-11 03:06:02 +02:00
|
|
|
str.sprintf("%02d:%02d:%02d", now.tm_hour, now.tm_min, now.tm_sec);
|
2015-07-04 23:05:28 +02:00
|
|
|
else
|
2017-09-11 03:06:02 +02:00
|
|
|
str.sprintf("%02d:%02d ", now.tm_hour, now.tm_min);
|
2015-07-04 23:05:28 +02:00
|
|
|
|
2018-10-03 22:23:55 +02:00
|
|
|
time_str = str;
|
|
|
|
time_str.redraw();
|
2015-07-04 23:05:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2018-09-20 23:59:01 +02:00
|
|
|
void Watch::onTimer (finalcut::FTimerEvent*)
|
2015-07-04 23:05:28 +02:00
|
|
|
{
|
|
|
|
printTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2018-09-20 23:59:01 +02:00
|
|
|
void Watch::onClose (finalcut::FCloseEvent* ev)
|
2015-07-04 23:05:28 +02:00
|
|
|
{
|
2018-09-20 23:59:01 +02:00
|
|
|
finalcut::FApplication::closeConfirmationDialog (this, ev);
|
2015-07-04 23:05:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2020-04-13 12:40:11 +02:00
|
|
|
void Watch::cb_clock (const finalcut::FWidget*, const FDataPtr)
|
2015-07-04 23:05:28 +02:00
|
|
|
{
|
2018-10-03 22:23:55 +02:00
|
|
|
if ( clock_sw.isChecked() )
|
2015-07-04 23:05:28 +02:00
|
|
|
{
|
|
|
|
printTime();
|
2020-04-04 20:58:47 +02:00
|
|
|
addTimer(1000); // Call onTimer() every second (1000 ms)
|
2015-07-04 23:05:28 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-04 20:58:47 +02:00
|
|
|
delAllTimer(); // Delete all timers and stop updating the time
|
2018-10-03 22:23:55 +02:00
|
|
|
time_str = "--:--:--";
|
|
|
|
time_str.redraw();
|
2015-07-04 23:05:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2020-04-13 12:40:11 +02:00
|
|
|
void Watch::cb_seconds (const finalcut::FWidget*, const FDataPtr)
|
2015-07-04 23:05:28 +02:00
|
|
|
{
|
2018-10-03 22:23:55 +02:00
|
|
|
if ( seconds_sw.isChecked() )
|
2015-07-04 23:05:28 +02:00
|
|
|
sec = true;
|
|
|
|
else
|
|
|
|
sec = false;
|
|
|
|
|
2018-10-03 22:23:55 +02:00
|
|
|
if ( clock_sw.isChecked() )
|
2015-07-04 23:05:28 +02:00
|
|
|
printTime();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( sec )
|
2018-10-03 22:23:55 +02:00
|
|
|
time_str = "--:--:--";
|
2015-07-04 23:05:28 +02:00
|
|
|
else
|
2018-10-03 22:23:55 +02:00
|
|
|
time_str = "--:-- ";
|
2017-09-20 05:44:41 +02:00
|
|
|
|
2018-10-03 22:23:55 +02:00
|
|
|
time_str.redraw();
|
2015-07-04 23:05:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-09-20 06:19:27 +02:00
|
|
|
void Watch::adjustSize()
|
2015-07-04 23:05:28 +02:00
|
|
|
{
|
2020-02-02 22:34:27 +01:00
|
|
|
const int pw = int(getParentWidget()->getWidth());
|
2017-08-27 09:50:30 +02:00
|
|
|
setX (1 + (pw - 22) / 2, false);
|
2018-09-20 23:59:01 +02:00
|
|
|
finalcut::FDialog::adjustSize();
|
2015-07-04 23:05:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// main part
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
int main (int argc, char* argv[])
|
|
|
|
{
|
|
|
|
// Create the application object
|
2020-05-02 00:07:35 +02:00
|
|
|
finalcut::FApplication app{argc, argv};
|
2015-07-04 23:05:28 +02:00
|
|
|
|
|
|
|
// Create a simple dialog box
|
2020-05-02 00:07:35 +02:00
|
|
|
Watch w{&app};
|
2015-07-04 23:05:28 +02:00
|
|
|
|
2017-09-19 06:18:03 +02:00
|
|
|
// Set dialog w as main widget
|
2020-04-13 12:40:11 +02:00
|
|
|
finalcut::FWidget::setMainWidget(&w);
|
2017-09-19 06:18:03 +02:00
|
|
|
|
|
|
|
// Show and start the application
|
2015-07-04 23:05:28 +02:00
|
|
|
w.show();
|
|
|
|
return app.exec();
|
|
|
|
}
|