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 *
|
|
|
|
* *
|
2018-09-20 23:59:01 +02:00
|
|
|
* Copyright 2015-2018 Markus Gans *
|
2017-11-04 07:03:53 +01:00
|
|
|
* *
|
|
|
|
* The 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. *
|
|
|
|
* *
|
|
|
|
* The 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/>. *
|
|
|
|
***********************************************************************/
|
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
|
|
|
|
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
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
#pragma pack(push)
|
|
|
|
#pragma pack(1)
|
|
|
|
|
2018-09-20 23:59:01 +02:00
|
|
|
class Watch : 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
|
2017-09-20 06:19:27 +02:00
|
|
|
~Watch();
|
2017-09-11 03:06:02 +02:00
|
|
|
|
2018-12-09 22:04:55 +01:00
|
|
|
// Disable assignment operator (=)
|
|
|
|
Watch& operator = (const Watch&) = delete;
|
|
|
|
|
2017-09-11 03:06:02 +02:00
|
|
|
// Method
|
|
|
|
void printTime();
|
|
|
|
|
|
|
|
// Event handlers
|
2018-12-31 06:18:39 +01:00
|
|
|
virtual void onTimer (finalcut::FTimerEvent*) override;
|
|
|
|
virtual void onClose (finalcut::FCloseEvent*) override;
|
2017-09-11 03:06:02 +02:00
|
|
|
|
|
|
|
// Callback methods
|
2018-12-27 00:14:46 +01:00
|
|
|
void cb_clock (finalcut::FWidget*, FDataPtr);
|
|
|
|
void cb_seconds (finalcut::FWidget*, FDataPtr);
|
2017-09-11 03:06:02 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
// Method
|
2018-12-31 06:18:39 +01:00
|
|
|
virtual void adjustSize() override;
|
2017-09-11 03:06:02 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
// 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
|
|
|
};
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-09-20 06:19:27 +02:00
|
|
|
Watch::Watch (FWidget* parent)
|
2018-09-20 23:59:01 +02:00
|
|
|
: finalcut::FDialog(parent)
|
2015-07-04 23:05:28 +02:00
|
|
|
{
|
|
|
|
setText ("Watch");
|
2018-10-14 06:25:33 +02:00
|
|
|
int pw = int(getParentWidget()->getWidth());
|
2017-08-27 09:50:30 +02:00
|
|
|
setGeometry (1 + (pw - 22) / 2, 3, 22, 13);
|
2015-07-04 23:05:28 +02:00
|
|
|
|
2018-10-03 22:23:55 +02:00
|
|
|
// Labels
|
|
|
|
time_label.setGeometry(5, 2, 5, 1);
|
|
|
|
time_label.setEmphasis();
|
|
|
|
time_str.setGeometry(10, 2, 8, 1);
|
2015-07-04 23:05:28 +02:00
|
|
|
|
2018-10-03 22:23:55 +02:00
|
|
|
// Checkbox buttons
|
|
|
|
clock_sw.setGeometry(4, 4, 9, 1);
|
|
|
|
seconds_sw.setGeometry(2, 6, 11, 1);
|
|
|
|
sec = seconds_sw.setChecked();
|
2015-07-04 23:05:28 +02:00
|
|
|
|
2018-10-03 22:23:55 +02:00
|
|
|
// Quit button
|
|
|
|
quit_btn.setGeometry(6, 9, 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()
|
2015-07-04 23:05:28 +02:00
|
|
|
{
|
2015-12-19 20:49:01 +01:00
|
|
|
delOwnTimer();
|
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
|
|
|
{
|
2018-09-20 23:59:01 +02:00
|
|
|
finalcut::FString str;
|
2017-09-11 03:06:02 +02:00
|
|
|
std::tm now;
|
2016-10-06 23:15:09 +02:00
|
|
|
std::time_t t;
|
2015-08-11 00:11:07 +02:00
|
|
|
|
2016-10-06 23:15:09 +02:00
|
|
|
t = std::time(0); // 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
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2018-12-27 00:14:46 +01:00
|
|
|
void Watch::cb_clock (finalcut::FWidget*, 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();
|
|
|
|
addTimer(1000);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delAllTimer();
|
2018-10-03 22:23:55 +02:00
|
|
|
time_str = "--:--:--";
|
|
|
|
time_str.redraw();
|
2015-07-04 23:05:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2018-12-27 00:14:46 +01:00
|
|
|
void Watch::cb_seconds (finalcut::FWidget*, 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
|
|
|
{
|
2018-10-14 06:25:33 +02:00
|
|
|
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
|
2018-09-20 23:59:01 +02:00
|
|
|
finalcut::FApplication app(argc, argv);
|
2015-07-04 23:05:28 +02:00
|
|
|
|
|
|
|
// Create a simple dialog box
|
2017-09-20 06:19:27 +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
|
2015-07-04 23:05:28 +02:00
|
|
|
app.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();
|
|
|
|
}
|