Commit f5cb85c6 authored by Thiago Santini's avatar Thiago Santini

Refactoring ERWidgets config

parent e0c49286
......@@ -5,8 +5,7 @@ using namespace std;
using namespace cv;
CameraWidget::CameraWidget(QString id, ImageProcessor::Type type, QWidget *parent) :
ERWidget(parent),
id(id),
ERWidget(id, parent),
type(type),
sROI(QPoint(0,0)),
eROI(QPoint(0,0)),
......@@ -17,10 +16,10 @@ CameraWidget::CameraWidget(QString id, ImageProcessor::Type type, QWidget *paren
cameraCalibrationSampleRequested(false),
ui(new Ui::CameraWidget)
{
ui->setupUi(this);
ui->setupUi(this);
ui->viewFinder->setScaledContents(false);
ui->viewFinder->setAttribute(Qt::WA_OpaquePaintEvent, true);
ui->viewFinder->setCursor(Qt::CrossCursor);
ui->viewFinder->setCursor(Qt::CrossCursor);
switch (type) {
case ImageProcessor::Eye:
......@@ -38,7 +37,6 @@ CameraWidget::CameraWidget(QString id, ImageProcessor::Type type, QWidget *paren
gPerformanceMonitor.enrol(id, "Image Processor");
gPerformanceMonitor.enrol(id, "Data Recorder");
setWindowTitle(id + " Widget");
camera = NULL;
imageProcessor = NULL;
......
......@@ -70,7 +70,6 @@ public slots:
void onCameraCalibrationFinished(bool success);
private:
QString id;
ImageProcessor::Type type;
Ui::CameraWidget *ui;
......
......@@ -10,6 +10,12 @@
<height>240</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>320</width>
<height>240</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
......
#include "ERWidget.h"
#include <QDebug>
ERWidget::ERWidget(QWidget *parent) : QMainWindow(parent)
ERWidget::ERWidget(QString id, QWidget *parent) :
id(id),
QMainWindow(parent)
{
setDefaults();
}
......@@ -3,12 +3,25 @@
#include <QMainWindow>
#include <QObject>
#include <QSettings>
// We can't include utils.h here because of the global access used for the log
// and performance monitor widgets. So we define an alternative set function
// here for convenience
template<typename T> void erset(const QSettings *settings, const QString key, T &v)
{
if (!settings)
return;
QVariant variant = settings->value(key);
if (variant.isValid())
v = qvariant_cast<T>(variant);
}
class ERWidget : public QMainWindow
{
Q_OBJECT
public:
explicit ERWidget(QWidget *parent = 0);
explicit ERWidget(QString id, QWidget *parent = 0);
signals:
void closed(bool b);
......@@ -17,11 +30,64 @@ signals:
public slots:
void save(QSettings *settings)
{
settings->sync();
settings->setValue(id + " Pos", pos());
settings->setValue(id + " Size", size());
settings->setValue(id + " Visible", isVisible());
}
void load(QSettings *settings)
{
QPoint pos = defaultPosition;
QSize size = defaultSize;
bool visible = defaultVisible;
// Size sanity
if (defaultSize.width() <= 0 || defaultSize.height() <= 0) {
defaultSize = minimumSize();
// If we don't have minimum sizes set in the form
if (defaultSize.width() == 0)
defaultSize.setWidth(320);
if (defaultSize.height() == 0)
defaultSize.setHeight(240);
}
settings->sync();
erset(settings, id + " Pos", pos);
erset(settings, id + " Size", size);
erset(settings, id + " Visible", visible);
move(pos);
resize(size);
if (visible)
this->show();
else
this->hide();
}
void setDefaults(bool visible = true, QSize size = {0,0}, QPoint pos = {0, 0})
{
defaultVisible = visible;
defaultSize = size;
defaultPosition = pos;
}
void setup() {
setWindowTitle(id);
}
protected:
void hideEvent(QHideEvent *event) Q_DECL_OVERRIDE { Q_UNUSED(event) emit closed(false); }
void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE { Q_UNUSED(event) emit closed(false); }
void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE { emit keyPress(event); }
void keyReleaseEvent(QKeyEvent *event) Q_DECL_OVERRIDE { emit keyRelease(event); }
QString id;
QSize defaultSize;
bool defaultVisible;
QPoint defaultPosition;
};
#endif // ERWIDGET_H
......@@ -8,8 +8,8 @@ static int gCollectionTupleId = qRegisterMetaType<CollectionTuple>("CollectionTu
static int gVectorCollectionTupleId = qRegisterMetaType<std::vector<CollectionTuple> >("std::vector<CollectionTuple>");
static int gCollectionTupleTypeId = qRegisterMetaType<CollectionTuple::TupleType>("CollectionTuple::TupleType");
GazeEstimationWidget::GazeEstimationWidget(QWidget *parent) :
ERWidget(parent),
GazeEstimationWidget::GazeEstimationWidget(QString id, QWidget *parent) :
ERWidget(id, parent),
isCollecting(false),
isSampling(false),
lastStatus(false),
......@@ -19,7 +19,6 @@ GazeEstimationWidget::GazeEstimationWidget(QWidget *parent) :
{
ui->setupUi(this);
setWindowTitle("Gaze Estimation Widget");
setWindowIcon(QIcon(":/icons/gazeEstimationWidget.png"));
gazeEstimationThread = new QThread();
......
......@@ -23,7 +23,7 @@ class GazeEstimationWidget : public ERWidget
Q_OBJECT
public:
explicit GazeEstimationWidget(QWidget *parent = 0);
explicit GazeEstimationWidget(QString id, QWidget *parent = 0);
~GazeEstimationWidget();
signals:
......
#include "LogWidget.h"
#include "ui_LogWidget.h"
LogWidget::LogWidget(QWidget *parent) :
ERWidget(parent),
LogWidget::LogWidget(QString id, QWidget *parent) :
ERWidget(id, parent),
ui(new Ui::LogWidget)
{
ui->setupUi(this);
setWindowTitle("Log Widget");
setWindowIcon(QIcon(":/icons/logWidget.png"));
}
......
......@@ -17,7 +17,7 @@ class LogWidget : public ERWidget
Q_OBJECT
public:
explicit LogWidget(QWidget *parent = 0);
explicit LogWidget(QString id, QWidget *parent = 0);
~LogWidget();
public slots:
......
#include "MainWindow.h"
#include "ui_MainWindow.h"
// TODO: factor config aware class into ERWidget
// TODO: automatically detect suitable window positions on first init
void MainWindow::createExtraMenus()
{
......@@ -10,11 +10,11 @@ void MainWindow::createExtraMenus()
}
MainWindow::MainWindow(QWidget *parent) :
ERWidget(parent),
lEyeWidget(NULL),
rEyeWidget(NULL),
fieldWidget(NULL),
synchronizer(NULL),
ERWidget("EyeRecToo", parent),
lEyeWidget(),
rEyeWidget(),
fieldWidget(),
synchronizer(),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
......@@ -24,8 +24,8 @@ MainWindow::MainWindow(QWidget *parent) :
createExtraMenus();
connect(ui->menuBar, SIGNAL(triggered(QAction*)), this, SLOT(menuOption(QAction*)) );
settings = new QSettings(gCfgDir + "/" + "EyeRecToo", QSettings::IniFormat);
cfg.load(settings);
settings = new QSettings(gCfgDir + "/" + "EyeRecToo", QSettings::IniFormat);
cfg.load(settings);
setWindowTitle(QString("EyeRecToo v%1").arg(GIT_VERSION));
setWindowIcon(QIcon(":/icons/EyeRecToo.png"));
......@@ -36,8 +36,9 @@ MainWindow::MainWindow(QWidget *parent) :
ui->blinker->hide();
logWidget = new LogWidget();
setupWidget(logWidget, cfg.logWidgetPos, cfg.logWidgetSize, cfg.logWidgetVisible, ui->log);
logWidget = new LogWidget("Log Widget");
logWidget->setDefaults( true, {480, 240} );
setupWidget(logWidget, settings, ui->log);
gLogWidget = logWidget;
/*
......@@ -53,17 +54,20 @@ MainWindow::MainWindow(QWidget *parent) :
/*
* Asynchronous elements
*/
lEyeWidget = new CameraWidget("LeftEye", ImageProcessor::Eye);
lEyeWidget->setWindowIcon(QIcon(":/icons/lEyeWidget.png"));
setupWidget(lEyeWidget, cfg.leftEyeWidgetPos, cfg.leftEyeWidgetSize, cfg.leftEyeWidgetVisible, ui->leftEyeCam);
lEyeWidget = new CameraWidget("Left Eye Widget", ImageProcessor::Eye);
lEyeWidget->setDefaults( true, {320, 240} );
lEyeWidget->setWindowIcon(QIcon(":/icons/lEyeWidget.png"));
setupWidget(lEyeWidget, settings, ui->leftEyeCam);
QThread::msleep(200);
rEyeWidget = new CameraWidget("RightEye", ImageProcessor::Eye);
rEyeWidget->setWindowIcon(QIcon(":/icons/rEyeWidget.png"));
setupWidget(rEyeWidget, cfg.rightEyeWidgetPos, cfg.rightEyeWidgetSize, cfg.rightEyeWidgetVisible, ui->rightEyeCam);
rEyeWidget = new CameraWidget("Right Eye Widget", ImageProcessor::Eye);
rEyeWidget->setDefaults( true, {320, 240} );
rEyeWidget->setWindowIcon(QIcon(":/icons/rEyeWidget.png"));
setupWidget(rEyeWidget, settings, ui->rightEyeCam);
QThread::msleep(200);
fieldWidget = new CameraWidget("Field", ImageProcessor::Field);
fieldWidget->setWindowIcon(QIcon(":/icons/fieldWidget.png"));
setupWidget(fieldWidget, cfg.fieldWidgetPos, cfg.fieldWidgetSize, cfg.fieldWidgetVisible, ui->fieldCam);
fieldWidget = new CameraWidget("Field Widget", ImageProcessor::Field);
fieldWidget->setDefaults( true, {854, 480} );
fieldWidget->setWindowIcon(QIcon(":/icons/fieldWidget.png"));
setupWidget(fieldWidget, settings, ui->fieldCam);
/*
* Synchronizer
......@@ -79,8 +83,9 @@ MainWindow::MainWindow(QWidget *parent) :
/*
* Synchronous elements
*/
gazeEstimationWidget = new GazeEstimationWidget();
setupWidget(gazeEstimationWidget, cfg.gazeEstimationWidgetPos, cfg.gazeEstimationWidgetSize, cfg.gazeEstimationWidgetVisible, ui->gazeEstimation);
gazeEstimationWidget = new GazeEstimationWidget("Gaze Estimation Widget");
gazeEstimationWidget->setDefaults( false );
setupWidget(gazeEstimationWidget, settings, ui->gazeEstimation);
connect(synchronizer, SIGNAL(newData(DataTuple)),
gazeEstimationWidget, SIGNAL(inDataTuple(DataTuple)) );
connect(fieldWidget, SIGNAL(newClick(Timestamp,QPoint,QSize)),
......@@ -102,8 +107,9 @@ MainWindow::MainWindow(QWidget *parent) :
networkStream->start(2002);
connect(gazeEstimationWidget, SIGNAL(outDataTuple(DataTuple)), networkStream, SLOT(push(DataTuple)) );
performanceMonitorWidget = new PerformanceMonitorWidget();
setupWidget(performanceMonitorWidget, cfg.performanceMonitorWidgetPos, cfg.performanceMonitorWidgetSize, cfg.performanceMonitorWidgetVisible, ui->performanceMonitor);
performanceMonitorWidget = new PerformanceMonitorWidget("Performance Monitor Widget");
performanceMonitorWidget->setDefaults( false );
setupWidget(performanceMonitorWidget, settings, ui->performanceMonitor);
// GUI to Widgets signals
connect(this, SIGNAL(startRecording()),
......@@ -130,7 +136,7 @@ MainWindow::MainWindow(QWidget *parent) :
loadSoundEffect(recStartSound, "rec-start.wav");
loadSoundEffect(recStopSound, "rec-stop.wav");
setupWidget(this, cfg.mainWindowPos, cfg.mainWindowSize, true);
setupWidget(this, settings);
// Commands
connect(&commandManager, SIGNAL(toggleCalibration()),
......@@ -153,6 +159,7 @@ MainWindow::MainWindow(QWidget *parent) :
evaluation->show();
QMetaObject::invokeMethod(evaluation, "run", Qt::QueuedConnection);
#endif
}
MainWindow::~MainWindow()
......@@ -172,29 +179,17 @@ void MainWindow::closeEvent(QCloseEvent *event)
// ui->recordingToggle->click();
}
cfg.mainWindowPos = pos();
cfg.mainWindowSize = size();
cfg.logWidgetPos = logWidget->pos();
cfg.logWidgetSize = logWidget->size();
cfg.logWidgetVisible = logWidget->isVisible();
cfg.leftEyeWidgetPos = lEyeWidget->pos();
cfg.leftEyeWidgetSize = lEyeWidget->size();
cfg.leftEyeWidgetVisible = lEyeWidget->isVisible();
cfg.rightEyeWidgetPos = rEyeWidget->pos();
cfg.rightEyeWidgetSize = rEyeWidget->size();
cfg.rightEyeWidgetVisible = rEyeWidget->isVisible();
cfg.fieldWidgetPos = fieldWidget->pos();
cfg.fieldWidgetSize = fieldWidget->size();
cfg.fieldWidgetVisible = fieldWidget->isVisible();
cfg.gazeEstimationWidgetPos = gazeEstimationWidget->pos();
cfg.gazeEstimationWidgetSize = gazeEstimationWidget->size();
cfg.gazeEstimationWidgetVisible = gazeEstimationWidget->isVisible();
cfg.performanceMonitorWidgetPos = performanceMonitorWidget->pos();
cfg.performanceMonitorWidgetSize = performanceMonitorWidget->size();
cfg.performanceMonitorWidgetVisible = performanceMonitorWidget->isVisible();
cfg.workingDirectory = QDir::currentPath();
if (settings)
cfg.save(settings);
cfg.workingDirectory = QDir::currentPath();
if (settings) {
cfg.save(settings);
save(settings);
logWidget->save(settings);
lEyeWidget->save(settings);
rEyeWidget->save(settings);
fieldWidget->save(settings);
gazeEstimationWidget->save(settings);
performanceMonitorWidget->save(settings);
}
qInfo() << "Closing Left Eye Widget...";
if ( lEyeWidget ) {
......@@ -556,24 +551,21 @@ void MainWindow::showAboutDialog()
QMessageBox::about(this, "About", msg);
}
void MainWindow::setupWidget(ERWidget *widget, QPoint &position, const QSize &size, const bool &visible, QPushButton *button)
void MainWindow::setupWidget(ERWidget *widget, QSettings* settings, QPushButton *button)
{
// Sanitize position first
// TODO: we might consider eventually move each ERWidget settings to their own file
widget->load(settings);
widget->setup();
// Sanitize position
bool inScreen = false;
for (int i = 0; i < QApplication::desktop()->screenCount(); i++)
inScreen |= QApplication::desktop()->screenGeometry(i).contains(position, true);
inScreen |= QApplication::desktop()->screenGeometry(i).contains(widget->pos(), true);
if (!inScreen)
position = QApplication::desktop()->screenGeometry().topLeft();
widget->move(position);
widget->resize(size);
if (visible)
widget->show();
widget->move(QApplication::desktop()->screenGeometry().topLeft());
if (button) {
button->setChecked(visible);
button->setChecked(widget->isVisible());
connect(widget, SIGNAL(closed(bool)),
button, SLOT(setChecked(bool)) );
}
......
......@@ -37,102 +37,22 @@ class MainWindowConfig
{
public:
MainWindowConfig() :
mainWindowPos( QPoint(0,0) ),
mainWindowSize( QSize(100, 100)),
logWidgetPos( QPoint(0,0) ),
logWidgetSize( QSize(640, 240)),
logWidgetVisible(true),
leftEyeWidgetPos( QPoint(0,0) ),
leftEyeWidgetSize( QSize(320,240) ),
leftEyeWidgetVisible(true),
rightEyeWidgetPos( QPoint(0,0) ),
rightEyeWidgetSize( QSize(320, 240) ),
rightEyeWidgetVisible(true),
fieldWidgetPos( QPoint(0,0) ),
fieldWidgetSize( QSize(320,240) ),
fieldWidgetVisible(true),
gazeEstimationWidgetPos( QPoint(0,0) ),
gazeEstimationWidgetSize( QSize(100, 100) ),
gazeEstimationWidgetVisible(true),
performanceMonitorWidgetPos( QPoint(0,0) ),
performanceMonitorWidgetSize( QSize(640, 240)),
performanceMonitorWidgetVisible(true),
workingDirectory("./")
workingDirectory("./")
{}
QPoint mainWindowPos;
QSize mainWindowSize;
QPoint logWidgetPos;
QSize logWidgetSize;
bool logWidgetVisible;
QPoint leftEyeWidgetPos;
QSize leftEyeWidgetSize;
bool leftEyeWidgetVisible;
QPoint rightEyeWidgetPos;
QSize rightEyeWidgetSize;
bool rightEyeWidgetVisible;
QPoint fieldWidgetPos;
QSize fieldWidgetSize;
bool fieldWidgetVisible;
QPoint gazeEstimationWidgetPos;
QSize gazeEstimationWidgetSize;
bool gazeEstimationWidgetVisible;
QPoint performanceMonitorWidgetPos;
QSize performanceMonitorWidgetSize;
bool performanceMonitorWidgetVisible;
QString workingDirectory;
void save(QSettings *settings)
{
settings->sync();
settings->setValue("mainWindowPos", mainWindowPos);
settings->setValue("mainWindowSize", mainWindowSize);
settings->setValue("logWidgetPos", logWidgetPos);
settings->setValue("logWidgetSize", logWidgetSize);
settings->setValue("logWidgetVisible", logWidgetVisible);
settings->setValue("leftEyeWidgetPos", leftEyeWidgetPos);
settings->setValue("leftEyeWidgetSize", leftEyeWidgetSize);
settings->setValue("leftEyeWidgetVisible", leftEyeWidgetVisible);
settings->setValue("rightEyeWidgetPos", rightEyeWidgetPos);
settings->setValue("rightEyeWidgetSize", rightEyeWidgetSize);
settings->setValue("rightEyeWidgetVisible", rightEyeWidgetVisible);
settings->setValue("fieldWidgetPos", fieldWidgetPos);
settings->setValue("fieldWidgetSize", fieldWidgetSize);
settings->setValue("fieldWidgetVisible", fieldWidgetVisible);
settings->setValue("gazeEstimationWidgetPos", gazeEstimationWidgetPos);
settings->setValue("gazeEstimationWidgetSize", gazeEstimationWidgetSize);
settings->setValue("gazeEstimationWidgetVisible", gazeEstimationWidgetVisible);
settings->setValue("performanceMonitorWidgetPos", performanceMonitorWidgetPos);
settings->setValue("performanceMonitorWidgetSize", performanceMonitorWidgetSize);
settings->setValue("performanceMonitorWidgetVisible", performanceMonitorWidgetVisible);
settings->setValue("workingDirectory", workingDirectory);
settings->setValue("workingDirectory", workingDirectory);
}
void load(QSettings *settings)
{
settings->sync();
set(settings, "mainWindowPos", mainWindowPos);
set(settings, "mainWindowSize", mainWindowSize);
set(settings, "logWidgetPos", logWidgetPos);
set(settings, "logWidgetSize", logWidgetSize);
set(settings, "logWidgetVisible", logWidgetVisible);
set(settings, "leftEyeWidgetPos", leftEyeWidgetPos);
set(settings, "leftEyeWidgetSize", leftEyeWidgetSize);
set(settings, "leftEyeWidgetVisible", leftEyeWidgetVisible);
set(settings, "rightEyeWidgetPos", rightEyeWidgetPos);
set(settings, "rightEyeWidgetSize", rightEyeWidgetSize);
set(settings, "rightEyeWidgetVisible", rightEyeWidgetVisible);
set(settings, "fieldWidgetPos", fieldWidgetPos);
set(settings, "fieldWidgetSize", fieldWidgetSize);
set(settings, "fieldWidgetVisible", fieldWidgetVisible);
set(settings, "gazeEstimationWidgetPos", gazeEstimationWidgetPos);
set(settings, "gazeEstimationWidgetSize", gazeEstimationWidgetSize);
set(settings, "gazeEstimationWidgetVisible", gazeEstimationWidgetVisible);
set(settings, "performanceMonitorWidgetPos", performanceMonitorWidgetPos);
set(settings, "performanceMonitorWidgetSize", performanceMonitorWidgetSize);
set(settings, "performanceMonitorWidgetVisible", performanceMonitorWidgetVisible);
set(settings, "workingDirectory", workingDirectory);
}
}
QString workingDirectory;
};
......@@ -181,7 +101,7 @@ private:
void setWorkingDirectory(QString dir);
void widgetButtonReact(QMainWindow *window, bool checked);
void createExtraMenus();
void setupWidget(ERWidget *widget, QPoint &position, const QSize &size, const bool &visible=true, QPushButton *button=NULL);
void setupWidget(ERWidget *widget, QSettings *settings, QPushButton *button=NULL);
protected:
void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE;
......
......@@ -3,15 +3,14 @@
using namespace std;
PerformanceMonitorWidget::PerformanceMonitorWidget(QWidget *parent) :
ERWidget(parent),
PerformanceMonitorWidget::PerformanceMonitorWidget(QString id, QWidget *parent) :
ERWidget(id, parent),
updateTimeMs(250),
formLayout(NULL),
ui(new Ui::PerformanceMonitorWidget)
{
ui->setupUi(this);
setWindowTitle("Performance Monitor Widget");
setWindowIcon(QIcon(":/icons/performanceMonitorWidget.png"));
QTimer::singleShot(updateTimeMs, this, SLOT(update()) );
......
......@@ -21,7 +21,7 @@ class PerformanceMonitorWidget : public ERWidget
Q_OBJECT
public:
explicit PerformanceMonitorWidget(QWidget *parent = 0);
explicit PerformanceMonitorWidget(QString id, QWidget *parent = 0);
~PerformanceMonitorWidget();
private:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment