2017-10-02 07:32:33 +02:00
|
|
|
/************************************************************************
|
|
|
|
* watch.cpp - A watch with FSwitch widgets *
|
|
|
|
* *
|
|
|
|
* This file is part of the Final Cut widget toolkit *
|
|
|
|
* *
|
|
|
|
* Copyright 2015-2017 Markus Gans *
|
|
|
|
* *
|
|
|
|
* The Final Cut is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU 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 General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU 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)
|
|
|
|
|
2017-09-20 06:19:27 +02:00
|
|
|
class Watch : public FDialog
|
2015-07-04 23:05:28 +02:00
|
|
|
{
|
2017-09-11 03:06:02 +02:00
|
|
|
public:
|
|
|
|
// Constructor
|
2017-09-20 06:19:27 +02:00
|
|
|
explicit Watch (FWidget* = 0);
|
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
|
|
|
|
|
|
|
// Method
|
|
|
|
void printTime();
|
|
|
|
|
|
|
|
// Event handlers
|
|
|
|
void onTimer (FTimerEvent*);
|
|
|
|
void onClose (FCloseEvent*);
|
|
|
|
|
|
|
|
// Callback methods
|
|
|
|
void cb_clock (FWidget*, data_ptr);
|
|
|
|
void cb_seconds (FWidget*, data_ptr);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Method
|
|
|
|
void adjustSize();
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Disable copy constructor
|
2017-09-20 06:19:27 +02:00
|
|
|
Watch (const Watch&);
|
2017-09-11 03:06:02 +02:00
|
|
|
|
|
|
|
// Disable assignment operator (=)
|
2017-09-20 06:19:27 +02:00
|
|
|
Watch& operator = (const Watch&);
|
2017-09-11 03:06:02 +02:00
|
|
|
|
|
|
|
// Data Members
|
|
|
|
bool sec;
|
|
|
|
FLabel* time_label;
|
|
|
|
FLabel* time_str;
|
|
|
|
FSwitch* clock_sw;
|
|
|
|
FSwitch* seconds_sw;
|
2015-07-04 23:05:28 +02:00
|
|
|
};
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-09-20 06:19:27 +02:00
|
|
|
Watch::Watch (FWidget* parent)
|
2015-09-22 04:18:20 +02:00
|
|
|
: FDialog(parent)
|
|
|
|
, sec(true)
|
|
|
|
, time_label(0)
|
|
|
|
, time_str(0)
|
|
|
|
, clock_sw(0)
|
|
|
|
, seconds_sw(0)
|
2015-07-04 23:05:28 +02:00
|
|
|
{
|
|
|
|
setText ("Watch");
|
2016-07-03 20:08:39 +02:00
|
|
|
int pw = 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
|
|
|
|
|
|
|
// Create labels
|
|
|
|
time_label = new FLabel(L"Time", this);
|
|
|
|
time_label->setGeometry(5, 2, 5, 1);
|
|
|
|
time_label->setEmphasis();
|
|
|
|
time_str = new FLabel(L"--:--:--", this);
|
|
|
|
time_str->setGeometry(10, 2, 8, 1);
|
|
|
|
|
|
|
|
// Create checkbox buttons
|
|
|
|
clock_sw = new FSwitch(L"Clock", this);
|
|
|
|
seconds_sw = new FSwitch(L"Seconds", this);
|
|
|
|
clock_sw->setGeometry(4, 4, 9, 1);
|
|
|
|
seconds_sw->setGeometry(2, 6, 11, 1);
|
2015-07-06 22:43:56 +02:00
|
|
|
sec = seconds_sw->setChecked();
|
2015-07-04 23:05:28 +02:00
|
|
|
|
|
|
|
// Create button
|
2015-09-20 05:44:50 +02:00
|
|
|
FButton* quit_btn = new FButton(L"&Quit", this);
|
|
|
|
quit_btn->setGeometry(6, 9, 9, 1);
|
2015-07-04 23:05:28 +02:00
|
|
|
|
|
|
|
// Connect switch signal "toggled" with a callback member function
|
|
|
|
clock_sw->addCallback
|
|
|
|
(
|
|
|
|
"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
|
|
|
|
seconds_sw->addCallback
|
|
|
|
(
|
|
|
|
"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
|
2015-09-20 05:44:50 +02:00
|
|
|
quit_btn->addCallback
|
2015-07-04 23:05:28 +02:00
|
|
|
(
|
|
|
|
"clicked",
|
2017-10-29 18:27:38 +01:00
|
|
|
F_METHOD_CALLBACK (this, &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
|
|
|
{
|
|
|
|
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
|
|
|
|
2017-09-20 05:44:41 +02:00
|
|
|
*time_str = str;
|
2015-07-04 23:05:28 +02:00
|
|
|
time_str->redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-09-20 06:19:27 +02:00
|
|
|
void Watch::onTimer (FTimerEvent*)
|
2015-07-04 23:05:28 +02:00
|
|
|
{
|
|
|
|
printTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-09-20 06:19:27 +02:00
|
|
|
void Watch::onClose (FCloseEvent* ev)
|
2015-07-04 23:05:28 +02:00
|
|
|
{
|
2017-10-30 20:56:00 +01:00
|
|
|
FApplication::closeConfirmationDialog (this, ev);
|
2015-07-04 23:05:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-09-20 06:19:27 +02:00
|
|
|
void Watch::cb_clock (FWidget*, data_ptr)
|
2015-07-04 23:05:28 +02:00
|
|
|
{
|
|
|
|
if ( clock_sw->isChecked() )
|
|
|
|
{
|
|
|
|
printTime();
|
|
|
|
addTimer(1000);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delAllTimer();
|
2017-09-20 05:44:41 +02:00
|
|
|
*time_str = "--:--:--";
|
2015-07-04 23:05:28 +02:00
|
|
|
time_str->redraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-09-20 06:19:27 +02:00
|
|
|
void Watch::cb_seconds (FWidget*, data_ptr)
|
2015-07-04 23:05:28 +02:00
|
|
|
{
|
|
|
|
if ( seconds_sw->isChecked() )
|
|
|
|
sec = true;
|
|
|
|
else
|
|
|
|
sec = false;
|
|
|
|
|
|
|
|
if ( clock_sw->isChecked() )
|
|
|
|
printTime();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( sec )
|
2017-09-20 05:44:41 +02:00
|
|
|
*time_str = "--:--:--";
|
2015-07-04 23:05:28 +02:00
|
|
|
else
|
2017-09-20 05:44:41 +02:00
|
|
|
*time_str = "--:-- ";
|
|
|
|
|
2015-07-04 23:05:28 +02:00
|
|
|
time_str->redraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-09-20 06:19:27 +02:00
|
|
|
void Watch::adjustSize()
|
2015-07-04 23:05:28 +02:00
|
|
|
{
|
2016-07-03 20:08:39 +02:00
|
|
|
int pw = getParentWidget()->getWidth();
|
2017-08-27 09:50:30 +02:00
|
|
|
setX (1 + (pw - 22) / 2, false);
|
2015-07-04 23:05:28 +02:00
|
|
|
FDialog::adjustSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// main part
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
int main (int argc, char* argv[])
|
|
|
|
{
|
|
|
|
// Create the application object
|
|
|
|
FApplication app(argc, argv);
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|