#include <QGridLayout>
#include <QtPlugin>
#include "ParallelPlugin.h"
#include "Future.h"
using namespace simpleexampleplugin;
ParallelPlugin::ParallelPlugin()
{
}
void
ParallelPlugin::version( int& major, int& minor, int& bugfix ) const
{
major = 1;
minor = 0;
bugfix = 0;
}
QString
ParallelPlugin::name() const
{
return "Parallel Example";
}
QString
ParallelPlugin::getHelpText() const
{
return "Just a simple example.";
}
QWidget*
ParallelPlugin::widget()
{
return widget_;
}
QString
ParallelPlugin::label() const
{
return "Parallel Example";
}
bool
{
this->service = service;
widget_ = new QWidget();
text = new QTextEdit();
text->setTextInteractionFlags( Qt::NoTextInteraction );
QFont font = text->font();
font.setFamily( "Courier" );
font.setStyleHint( QFont::Monospace );
font.setFixedPitch( true );
text->setFont( font );
QVBoxLayout* layout = new QVBoxLayout();
widget_->setLayout( layout );
layout->addWidget( text );
connect( future, SIGNAL( calculationFinished() ), this, SLOT( calculationFinished() ) );
connect( &updateTimer, SIGNAL( timeout() ), this, SLOT( updateView() ) );
return true;
}
void
ParallelPlugin::cubeClosed()
{
updateTimer.disconnect();
service->disconnect( future, SIGNAL( calculationFinished() ) );
delete widget_;
}
void
ParallelPlugin::updateView()
{
std::lock_guard<std::mutex> lockGuard( updateMutex );
QString txt = "";
for ( int i = 0; i < N; i++ )
{
for ( int j = 0; j < N; j++ )
{
int val = data[ i ][ j ];
QString label = val > 0 ? QString( "%1" ).arg( val, 4 ) : " ?";
txt.append( label );
}
txt.append( "\n" );
}
text->setText( txt );
}
void
ParallelPlugin::valuesChanged()
{
future->cancelCalculation();
for ( int i = 0; i < N; i++ )
{
for ( int j = 0; j < N; j++ )
{
data[ i ][ j ] = 0;
future->addCalculation( new ParallelCalculation( i + 1, j + 1, &data[ i ][ j ] ) );
}
}
updateView();
widget()->repaint();
updateTimer.start( 500 );
future->startCalculation( true );
}
void
ParallelPlugin::calculationFinished()
{
updateTimer.stop();
updateView();
QString txt = text->toPlainText().append( "\nCalculation finished" );
text->setText( txt );
}
bool
ParallelCalculation::isReady()
{
bool ready = ( x_ % 2 == 0 || !first );
first = false;
return ready;
}
void
ParallelCalculation::calculate()
{
{
QWaitCondition wc;
QMutex mutex;
QMutexLocker locker( &mutex );
int wait_factor = cubegui::Globals::optionIsSet( cubegui::Single ) ? 10 : 100;
int msecs = ( std::rand() % 5 ) * wait_factor;
wc.wait( &mutex, msecs );
}
*result_ = x_ * y_;
}