2015-09-25 21:37:19 +02:00
|
|
|
// File: watch.cpp
|
2015-07-04 23:05:28 +02:00
|
|
|
|
|
|
|
#include <ctime>
|
|
|
|
|
2017-08-24 00:47:35 +02:00
|
|
|
#include "fapplication.h"
|
2015-07-04 23:05:28 +02:00
|
|
|
#include "flabel.h"
|
|
|
|
#include "fdialog.h"
|
|
|
|
#include "fmessagebox.h"
|
|
|
|
#include "fswitch.h"
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// class watch
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
#pragma pack(push)
|
|
|
|
#pragma pack(1)
|
|
|
|
|
|
|
|
class watch : public FDialog
|
|
|
|
{
|
|
|
|
public:
|
2016-09-30 04:55:28 +02:00
|
|
|
// Constructor
|
|
|
|
explicit watch (FWidget* = 0);
|
2016-11-02 00:37:58 +01:00
|
|
|
|
2016-09-30 04:55:28 +02:00
|
|
|
// Destructor
|
|
|
|
~watch();
|
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
// Method
|
2015-07-04 23:05:28 +02:00
|
|
|
void printTime();
|
2016-09-30 04:55:28 +02:00
|
|
|
|
|
|
|
// Event handlers
|
2015-07-04 23:05:28 +02:00
|
|
|
void onTimer (FTimerEvent*);
|
|
|
|
void onClose (FCloseEvent*);
|
2016-09-30 04:55:28 +02:00
|
|
|
|
|
|
|
// Callback methods
|
2017-02-24 00:30:07 +01:00
|
|
|
void cb_clock (FWidget*, data_ptr);
|
|
|
|
void cb_seconds (FWidget*, data_ptr);
|
|
|
|
void cb_exitApp (FWidget*, data_ptr);
|
2015-07-04 23:05:28 +02:00
|
|
|
|
|
|
|
protected:
|
2016-11-02 00:37:58 +01:00
|
|
|
// Method
|
2015-07-04 23:05:28 +02:00
|
|
|
void adjustSize();
|
2016-11-02 00:37:58 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Disable copy constructor
|
|
|
|
watch (const watch&);
|
|
|
|
|
|
|
|
// Disable assignment operator (=)
|
|
|
|
watch& operator = (const watch&);
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2015-09-22 04:18:20 +02:00
|
|
|
watch::watch (FWidget* parent)
|
|
|
|
: 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-04-09 20:08:53 +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-04-09 20:08:53 +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-04-09 20:08:53 +02:00
|
|
|
F_METHOD_CALLBACK (this, &watch::cb_exitApp)
|
2015-07-04 23:05:28 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
watch::~watch()
|
|
|
|
{
|
2015-12-19 20:49:01 +01:00
|
|
|
delOwnTimer();
|
2015-07-04 23:05:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void watch::printTime()
|
|
|
|
{
|
|
|
|
FString str;
|
2016-10-06 23:15:09 +02:00
|
|
|
std::tm* now;
|
|
|
|
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
|
|
|
|
now = std::localtime(&t);
|
2015-07-04 23:05:28 +02:00
|
|
|
|
|
|
|
if ( sec )
|
|
|
|
str.sprintf("%02d:%02d:%02d", now->tm_hour, now->tm_min, now->tm_sec);
|
|
|
|
else
|
|
|
|
str.sprintf("%02d:%02d ", now->tm_hour, now->tm_min);
|
|
|
|
|
|
|
|
time_str->setText(str);
|
|
|
|
time_str->redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void watch::onTimer (FTimerEvent*)
|
|
|
|
{
|
|
|
|
printTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2015-09-20 05:44:50 +02:00
|
|
|
void watch::onClose (FCloseEvent* ev)
|
2015-07-04 23:05:28 +02:00
|
|
|
{
|
2015-09-22 04:18:20 +02:00
|
|
|
int ret = FMessageBox::info ( this, "Quit"
|
|
|
|
, "Do you really want\n"
|
|
|
|
"to quit the program ?"
|
|
|
|
, FMessageBox::Yes
|
|
|
|
, FMessageBox::No );
|
2015-07-04 23:05:28 +02:00
|
|
|
if ( ret == FMessageBox::Yes )
|
2015-09-20 05:44:50 +02:00
|
|
|
ev->accept();
|
2015-07-04 23:05:28 +02:00
|
|
|
else
|
2015-09-20 05:44:50 +02:00
|
|
|
ev->ignore();
|
2015-07-04 23:05:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-02-24 00:30:07 +01: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();
|
|
|
|
time_str->setText("--:--:--");
|
|
|
|
time_str->redraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-02-24 00:30:07 +01: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 )
|
|
|
|
time_str->setText("--:--:--");
|
|
|
|
else
|
|
|
|
time_str->setText("--:-- ");
|
|
|
|
time_str->redraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2017-02-24 00:30:07 +01:00
|
|
|
void watch::cb_exitApp (FWidget*, data_ptr)
|
2015-07-04 23:05:28 +02:00
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void watch::adjustSize()
|
|
|
|
{
|
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
|
|
|
|
watch w(&app);
|
|
|
|
|
|
|
|
app.setMainWidget(&w);
|
|
|
|
w.show();
|
|
|
|
return app.exec();
|
|
|
|
}
|