Skip to content

Commit

Permalink
ScreenGrabber test
Browse files Browse the repository at this point in the history
  • Loading branch information
kibsoft committed Apr 9, 2013
1 parent e1c54af commit 4196869
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 23 deletions.
74 changes: 72 additions & 2 deletions examples/TestExample/mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,52 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QDesktopWidget>
#include <QTimer>
#include <ScreenGrabber>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
, ui(new Ui::MainWindow)
, m_screenGrabber(new ScreenGrabber(this))
, m_helper(new MouseHelper(this))
, m_grabbingTime(0)
, m_grabbedFrameCount(0)
{
ui->setupUi(this);

setWindowFlags(Qt::WindowStaysOnTopHint);

//set up gui
QRect fullScreenRect = qApp->desktop()->screen()->rect();

ui->widthSpinBox->setMaximum(fullScreenRect.width());
ui->heightSpinBox->setMaximum(fullScreenRect.height());

ui->widthSpinBox->setValue(fullScreenRect.width());
ui->heightSpinBox->setValue(fullScreenRect.height());

//connect signals
connect(m_helper, SIGNAL(mouseEvent(MouseEvent)), this, SLOT(updateMHStatusLabel(MouseEvent)));
connect(m_screenGrabber, SIGNAL(frameAvailable(QImage,int)), this, SLOT(updateAverageFrameRate(QImage,int)));
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::closeEvent(QCloseEvent *event)
{
if (m_screenGrabber->state() == ScreenGrabber::ActiveState) {
m_screenGrabber->stop();

while (m_screenGrabber->state() != ScreenGrabber::StoppedState) {}
}

QMainWindow::closeEvent(event);
}

void MainWindow::on_MHStart_clicked()
{
ui->MHStart->setEnabled(false);
Expand All @@ -38,8 +66,6 @@ void MainWindow::on_MHStop_clicked()

void MainWindow::updateMHStatusLabel(const MouseEvent &event)
{
Q_UNUSED(event)

QString str(tr("%1 button %2\nPosition: %3, %4"));
str = str.arg(event.button == MouseEvent::LeftButton ? tr("Left") : tr("Right"));

Expand Down Expand Up @@ -68,3 +94,47 @@ void MainWindow::restoreLabelBackground()
{
ui->MHStatusLabel->setPalette(ui->MHStatus->palette());
}

void MainWindow::on_SGStart_clicked()
{
m_screenGrabber->setCaptureRect(QRect(0, 0, ui->widthSpinBox->value(), ui->heightSpinBox->value()));
m_screenGrabber->setLatency(ui->latencySpinBox->value());
m_screenGrabber->start();

ui->SGStart->setEnabled(false);
ui->SGStop->setEnabled(true);
ui->latencySpinBox->setEnabled(false);
ui->widthSpinBox->setEnabled(false);
ui->heightSpinBox->setEnabled(false);
}

void MainWindow::on_SGStop_clicked()
{
m_screenGrabber->stop();

ui->SGStop->setEnabled(false);
ui->SGStart->setEnabled(true);
ui->latencySpinBox->setEnabled(true);
ui->widthSpinBox->setEnabled(true);
ui->heightSpinBox->setEnabled(true);

ui->SCFrameRateLabel->setText("-");

m_grabbedFrameCount = 0;
m_grabbingTime = 0;
}

void MainWindow::updateAverageFrameRate(const QImage &frame, int duration)
{
Q_UNUSED(frame)

if (m_screenGrabber->state() == ScreenGrabber::ActiveState) {
++m_grabbedFrameCount;
m_grabbingTime += duration;

if (m_grabbingTime != 0) {
double frameRate = m_grabbedFrameCount / (m_grabbingTime / 1000.0);//frame per second
ui->SCFrameRateLabel->setText(QString("%1 fps").arg(QString::number(frameRate, 'g', 4)));
}
}
}
15 changes: 14 additions & 1 deletion examples/TestExample/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,37 @@ namespace Ui {
class MainWindow;
}

class ScreenGrabber;

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();


protected:
void closeEvent(QCloseEvent *event);

private slots:
void on_MHStart_clicked();
void on_MHStop_clicked();

void updateMHStatusLabel(const MouseEvent &event);
void restoreLabelBackground();

void on_SGStart_clicked();
void on_SGStop_clicked();
void updateAverageFrameRate(const QImage &frame, int duration);

private:
Ui::MainWindow *ui;
MouseHelper *m_helper;
ScreenGrabber *m_screenGrabber;

int m_grabbingTime;
int m_grabbedFrameCount;
};

#endif // MAINWINDOW_H
106 changes: 86 additions & 20 deletions examples/TestExample/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -79,37 +79,103 @@
<property name="title">
<string>Screen Grabber</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="QPushButton" name="SGStart">
<property name="text">
<string>Start</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="SGStop">
<property name="text">
<string>Stop</string>
</property>
</widget>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="SGFrameRate">
<widget class="QPushButton" name="SGStart">
<property name="text">
<string>Instant frame rate:</string>
<string>Start</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="SCFrameRateLabel">
<widget class="QPushButton" name="SGStop">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>-</string>
<string>Stop</string>
</property>
</widget>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="SGFrameRate">
<property name="text">
<string>Average frame rate:</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="SCFrameRateLabel">
<property name="text">
<string>-</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Latency:</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="latencySpinBox">
<property name="maximum">
<number>150</number>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Image width:</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="widthSpinBox"/>
</item>
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>Image height:</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="heightSpinBox"/>
</item>
</layout>
</item>
</layout>
</item>
</layout>
Expand Down

0 comments on commit 4196869

Please sign in to comment.