/*
* Project: MoleCuilder
* Description: creates and alters molecular systems
* Copyright (C) 2013 Frederik Heber. All rights reserved.
*
*
* This file is part of MoleCuilder.
*
* MoleCuilder 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 2 of the License, or
* (at your option) any later version.
*
* MoleCuilder 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 MoleCuilder. If not, see .
*/
/*
* QtTimeLine.cpp
*
* Created on: Jul 11, 2013
* Author: heber
*/
// include config.h
#ifdef HAVE_CONFIG_H
#include
#endif
// make sure Qt includes come before MemDebug
#include "QtTimeLine.hpp"
//#include "CodePatterns/MemDebug.hpp"
#include "CodePatterns/Assert.hpp"
#include "CodePatterns/Log.hpp"
#include "Actions/WorldAction/SetWorldTimeAction.hpp"
#include "Atom/atom.hpp"
#include "Atom/AtomObserver.hpp"
#include "WorldTime.hpp"
QtTimeLine::QtTimeLine(QWidget * _parent) :
QWidget(_parent),
Observer("QtTimeLine"),
atomobserver_enlisted(false),
worldtime_enlisted(false),
SetWorldTime_called(false)
{
slider = new QSlider(Qt::Horizontal, this);
// set initial values
slider->setMinimum(0);
slider->setMaximum(0);
slider->setValue(0);
slider->setTickInterval(1);
slider->setSingleStep(1);
slider->setTracking(true);
spinbox = new QSpinBox(this);
spinbox->setMinimum(0);
spinbox->setMaximum(0);
spinbox->setValue(0);
layout = new QHBoxLayout(this);
layout->addWidget(slider);
layout->addWidget(spinbox);
// sign on to observable
WorldTime::getInstance().signOn(this, WorldTime::TimeChanged);
worldtime_enlisted = true;
AtomObserver::getInstance().signOn(this, AtomObservable::TrajectoryChanged);
atomobserver_enlisted = true;
// connect to QSlider's valueChanged() signal
bool result = connect(slider,SIGNAL(valueChanged(int)),this,SLOT(StepUpdate(int)));
result = result && connect(spinbox,SIGNAL(valueChanged(int)),this,SLOT(StepUpdate(int)));
if (!result)
ELOG(0, "Could not connect to QSlider::valueChanged.");
}
QtTimeLine::~QtTimeLine()
{
if (worldtime_enlisted)
WorldTime::getInstance().signOff(this, WorldTime::TimeChanged);
if (atomobserver_enlisted)
AtomObserver::getInstance().signOff(this, AtomObservable::TrajectoryChanged);
}
void QtTimeLine::paintEvent(QPaintEvent * event)
{
boost::recursive_mutex::scoped_lock lock(refill_mutex);
// layout->paintEvent(event);
}
void QtTimeLine::subjectKilled(Observable *publisher)
{
// as long as we are enlisted to only one channel, we have nothing to worry.
// Otherwise we should also signOn to global changes for subjectKilled() and
// sign off from the channels
if (static_cast(publisher) == AtomObserver::getPointer()) {
atomobserver_enlisted = false;
} else if (static_cast(publisher) == WorldTime::getPointer()) {
worldtime_enlisted = false;
}
}
void QtTimeLine::update(Observable *publisher)
{
ELOG(0, "We are not enlisted for general updates.");
}
void QtTimeLine::recieveNotification(Observable *publisher, Notification_ptr notification)
{
boost::recursive_mutex::scoped_lock lock(refill_mutex);
if (static_cast(publisher) == WorldTime::getPointer()) {
SetWorldTime_called = false;
const int timestep = WorldTime::getTime();
// check whether we are beyond maximum
if (timestep > slider->maximum()) {
slider->setMaximum(timestep);
spinbox->setMaximum(timestep);
}
// set slider position to new time step
slider->setValue( timestep );
spinbox->setValue( timestep );
} else
//if (static_cast(publisher) == AtomObserver::getPointer())
{
// calculate max trajectory (need dynamic_cast due to virtual base)
const atom *_atom = dynamic_cast(publisher);
const int MaxTrajectory = _atom->getTrajectorySize()-1;
if (MaxTrajectory > slider->maximum()) {
slider->setMaximum(MaxTrajectory);
spinbox->setMaximum(MaxTrajectory);
}
}
}
void QtTimeLine::StepUpdate(int position)
{
boost::recursive_mutex::scoped_lock lock(refill_mutex);
if (!SetWorldTime_called) {
if (WorldTime::getTime() != (unsigned int)position) {
SetWorldTime_called = true;
MoleCuilder::WorldSetWorldTime(position);
}
}
}