Commit 842b5341 authored by Jasem Mutlaq's avatar Jasem Mutlaq

Use FOVManager to handle how FOVs are stored. This fixes a number of memory...

Use FOVManager to handle how FOVs are stored. This fixes a number of memory related bugs. Need to migrate this to SQLite instead of file
parent b524e157
......@@ -32,7 +32,129 @@
#include <QStandardPaths>
#include "kspaths.h"
QList<FOV*> FOV::m_FOVs;
QList<FOV*> FOVManager::m_FOVs;
FOVManager::FOVManager()
{
}
FOVManager::~FOVManager()
{
qDeleteAll(m_FOVs);
}
QList<FOV*> FOVManager::defaults()
{
QList<FOV*> fovs;
fovs << new FOV(i18nc("use field-of-view for binoculars", "7x35 Binoculars" ),
558, 558, 0,0,0, FOV::CIRCLE,"#AAAAAA")
<< new FOV(i18nc("use a Telrad field-of-view indicator", "Telrad" ),
30, 30, 0,0,0, FOV::BULLSEYE,"#AA0000")
<< new FOV(i18nc("use 1-degree field-of-view indicator", "One Degree"),
60, 60, 0,0,0, FOV::CIRCLE,"#AAAAAA")
<< new FOV(i18nc("use HST field-of-view indicator", "HST WFPC2"),
2.4, 2.4, 0,0,0, FOV::SQUARE,"#AAAAAA")
<< new FOV(i18nc("use Radiotelescope HPBW", "30m at 1.3cm" ),
1.79, 1.79, 0,0,0, FOV::SQUARE,"#AAAAAA");
return fovs;
}
bool FOVManager::save()
{
QFile f;
// TODO: Move FOVs to user database instead of file!!
f.setFileName( KSPaths::writableLocation(QStandardPaths::GenericDataLocation) + "fov.dat" ) ;
if ( ! f.open( QIODevice::WriteOnly ) )
{
qDebug() << "Could not open fov.dat.";
return false;
}
QTextStream ostream(&f);
foreach(FOV* fov, m_FOVs)
{
ostream << fov->name() << ':'
<< fov->sizeX() << ':'
<< fov->sizeY() << ':'
<< fov->offsetX() << ':'
<< fov->offsetY() << ':'
<< fov->rotation() << ':'
<< QString::number( fov->shape() ) << ':' //FIXME: is this needed???
<< fov->color() << endl;
}
f.close();
return true;
}
const QList<FOV*> & FOVManager::readFOVs()
{
qDeleteAll(m_FOVs);
m_FOVs.clear();
QFile f;
f.setFileName( KSPaths::writableLocation(QStandardPaths::GenericDataLocation) + "fov.dat" ) ;
if( !f.exists() )
{
m_FOVs = defaults();
save();
return m_FOVs;
}
if( f.open(QIODevice::ReadOnly) )
{
QTextStream istream(&f);
while( !istream.atEnd() ) {
QStringList fields = istream.readLine().split(':');
bool ok;
QString name, color;
float sizeX, sizeY, xoffset, yoffset, rot;
FOV::Shape shape;
if( fields.count() == 8 )
{
name = fields[0];
sizeX = fields[1].toFloat(&ok);
if( !ok ) {
return m_FOVs;
}
sizeY = fields[2].toFloat(&ok);
if( !ok ) {
return m_FOVs;
}
xoffset = fields[3].toFloat(&ok);
if( !ok ) {
return m_FOVs;
}
yoffset = fields[4].toFloat(&ok);
if( !ok ) {
return m_FOVs;
}
rot = fields[5].toFloat(&ok);
if( !ok ) {
return m_FOVs;
}
shape = FOV::intToShape( fields[6].toInt(&ok) );
if( !ok ) {
return m_FOVs;
}
color = fields[7];
} else {
continue;
}
//FIXME: This still shows lost blocks in Valgrind despite the fact memory is always cleared?
m_FOVs.append( new FOV(name, sizeX, sizeY, xoffset, yoffset, rot, shape, color) );
}
}
return m_FOVs;
}
FOV::Shape FOV::intToShape(int s)
{
......@@ -169,109 +291,6 @@ void FOV::setShape( int s)
m_shape = intToShape(s);
}
QList<FOV*> FOV::defaults()
{
QList<FOV*> fovs;
fovs << new FOV(i18nc("use field-of-view for binoculars", "7x35 Binoculars" ),
558, 558, 0,0,0, CIRCLE,"#AAAAAA")
<< new FOV(i18nc("use a Telrad field-of-view indicator", "Telrad" ),
30, 30, 0,0,0, BULLSEYE,"#AA0000")
<< new FOV(i18nc("use 1-degree field-of-view indicator", "One Degree"),
60, 60, 0,0,0, CIRCLE,"#AAAAAA")
<< new FOV(i18nc("use HST field-of-view indicator", "HST WFPC2"),
2.4, 2.4, 0,0,0, SQUARE,"#AAAAAA")
<< new FOV(i18nc("use Radiotelescope HPBW", "30m at 1.3cm" ),
1.79, 1.79, 0,0,0, SQUARE,"#AAAAAA");
return fovs;
}
void FOV::writeFOVs(const QList<FOV*> fovs)
{
QFile f;
f.setFileName( KSPaths::writableLocation(QStandardPaths::GenericDataLocation) + "fov.dat" ) ;
if ( ! f.open( QIODevice::WriteOnly ) ) {
qDebug() << "Could not open fov.dat.";
return;
}
QTextStream ostream(&f);
foreach(FOV* fov, fovs) {
ostream << fov->name() << ':'
<< fov->sizeX() << ':'
<< fov->sizeY() << ':'
<< fov->offsetX() << ':'
<< fov->offsetY() << ':'
<< fov->rotation() << ':'
<< QString::number( fov->shape() ) << ':' //FIXME: is this needed???
<< fov->color() << endl;
}
f.close();
}
const QList<FOV*> & FOV::readFOVs()
{
QFile f;
qDeleteAll(m_FOVs);
m_FOVs.clear();
f.setFileName( KSPaths::writableLocation(QStandardPaths::GenericDataLocation) + "fov.dat" ) ;
if( !f.exists() ) {
m_FOVs = defaults();
writeFOVs(m_FOVs);
return m_FOVs;
}
if( f.open(QIODevice::ReadOnly) )
{
QTextStream istream(&f);
while( !istream.atEnd() ) {
QStringList fields = istream.readLine().split(':');
bool ok;
QString name, color;
float sizeX, sizeY, xoffset, yoffset, rot;
Shape shape;
if( fields.count() == 8 )
{
name = fields[0];
sizeX = fields[1].toFloat(&ok);
if( !ok ) {
return m_FOVs;
}
sizeY = fields[2].toFloat(&ok);
if( !ok ) {
return m_FOVs;
}
xoffset = fields[3].toFloat(&ok);
if( !ok ) {
return m_FOVs;
}
yoffset = fields[4].toFloat(&ok);
if( !ok ) {
return m_FOVs;
}
rot = fields[5].toFloat(&ok);
if( !ok ) {
return m_FOVs;
}
shape = intToShape( fields[6].toInt(&ok) );
if( !ok ) {
return m_FOVs;
}
color = fields[7];
} else {
continue;
}
//FIXME: This still shows lost blocks in Valgrind despite the fact memory is always cleared?
m_FOVs.append( new FOV(name, sizeX, sizeY, xoffset, yoffset, rot, shape, color) );
}
}
return m_FOVs;
}
SkyPoint FOV::center() const
{
return m_center;
......
......@@ -81,13 +81,6 @@ public:
*/
void draw(QPainter &p, float x, float y);
/** @short Fill list with default FOVs*/
static QList<FOV*> defaults();
/** @short Write list of FOVs to "fov.dat" */
static void writeFOVs(const QList<FOV*> fovs);
/** @short Read list of FOVs from "fov.dat" */
static const QList<FOV*> & readFOVs();
SkyPoint center() const;
void setCenter(const SkyPoint &center);
......@@ -107,10 +100,36 @@ private:
float m_northPA;
SkyPoint m_center;
QImage m_image;
bool m_imageDisplay;
bool m_imageDisplay;
static QList<FOV*> m_FOVs;
};
/** @class FOVManager
* A simple class handling FOVs.
* @note Should migrate this from file (fov.dat) to using the user sqlite database
*@author Jasem Mutlaq
*@version 1.0
*/
class FOVManager
{
public:
/** @short Read list of FOVs from "fov.dat" */
static const QList<FOV*> & readFOVs();
static void addFOV(FOV* newFOV) { Q_ASSERT(newFOV); m_FOVs.append(newFOV); }
static void removeFOV(FOV* fov) { Q_ASSERT(fov); m_FOVs.removeOne(fov); }
static const QList<FOV*> & getFOVs() { return m_FOVs; }
/** @short Write list of FOVs to "fov.dat" */
static bool save();
private:
FOVManager();
~FOVManager();
/** @short Fill list with default FOVs*/
static QList<FOV*> defaults();
static QList<FOV*> m_FOVs;
};
#endif
......@@ -97,7 +97,8 @@ FOVDialog::FOVDialog( QWidget* p ) :
connect( fov->RemoveButton, SIGNAL( clicked() ), SLOT( slotRemoveFOV() ) );
// Read list of FOVs and for each FOV create listbox entry, which stores it.
foreach(FOV* f, FOV::readFOVs() ) {
foreach(FOV* f, FOVManager::getFOVs() )
{
addListWidget(f);
}
}
......@@ -105,9 +106,9 @@ FOVDialog::FOVDialog( QWidget* p ) :
FOVDialog::~FOVDialog()
{
// Delete FOVs
for(int i = 0; i < fov->FOVListBox->count(); i++) {
delete getFOV( fov->FOVListBox->item(i) );
}
//for(int i = 0; i < fov->FOVListBox->count(); i++) {
//delete getFOV( fov->FOVListBox->item(i) );
//}
}
QListWidgetItem* FOVDialog::addListWidget(FOV* f)
......@@ -117,14 +118,6 @@ QListWidgetItem* FOVDialog::addListWidget(FOV* f)
return item;
}
void FOVDialog::writeFOVList() {
QList<FOV*> fovs;
for(int i = 0; i < fov->FOVListBox->count(); i++) {
fovs << getFOV( fov->FOVListBox->item(i) );
}
FOV::writeFOVs(fovs);
}
void FOVDialog::slotSelect( int irow ) {
bool enable = irow >= 0;
fov->RemoveButton->setEnabled( enable );
......@@ -138,8 +131,10 @@ void FOVDialog::slotSelect( int irow ) {
void FOVDialog::slotNewFOV() {
QPointer<NewFOV> newfdlg = new NewFOV( this );
if ( newfdlg->exec() == QDialog::Accepted ) {
if ( newfdlg->exec() == QDialog::Accepted )
{
FOV *newfov = new FOV( newfdlg->getFOV() );
FOVManager::addFOV(newfov);
addListWidget( newfov );
fov->FOVListBox->setCurrentRow( fov->FOVListBox->count() -1 );
}
......@@ -163,11 +158,13 @@ void FOVDialog::slotEditFOV() {
delete newfdlg;
}
void FOVDialog::slotRemoveFOV() {
void FOVDialog::slotRemoveFOV()
{
int i = fov->FOVListBox->currentRow();
if( i >= 0 ) {
if( i >= 0 )
{
QListWidgetItem* item = fov->FOVListBox->takeItem(i);
delete getFOV(item);
FOVManager::removeFOV(getFOV(item));
delete item;
}
}
......
......@@ -49,8 +49,6 @@ class FOVDialog : public QDialog
public:
explicit FOVDialog( QWidget *parent = 0 );
virtual ~FOVDialog();
/** Write list of FOVs to disk. */
void writeFOVList();
private slots:
void slotNewFOV();
void slotEditFOV();
......
......@@ -1272,8 +1272,9 @@ void KStars::slotTargetSymbol(bool flag) {
void KStars::slotFOVEdit() {
QPointer<FOVDialog> fovdlg = new FOVDialog( this );
if ( fovdlg->exec() == QDialog::Accepted ) {
fovdlg->writeFOVList();
if ( fovdlg->exec() == QDialog::Accepted )
{
FOVManager::save();
repopulateFOV();
}
delete fovdlg;
......
......@@ -1035,14 +1035,16 @@ void KStarsData::syncFOV()
{
visibleFOVs.clear();
// Add visible FOVs
foreach(FOV* fov, availFOVs) {
foreach(FOV* fov, availFOVs)
{
if( Options::fOVNames().contains( fov->name() ) )
visibleFOVs.append( fov );
}
// Remove unavailable FOVs
QSet<QString> names = QSet<QString>::fromList( Options::fOVNames() );
QSet<QString> all;
foreach(FOV* fov, visibleFOVs) {
foreach(FOV* fov, visibleFOVs)
{
all.insert(fov->name());
}
Options::setFOVNames( all.intersect(names).toList() );
......
......@@ -339,6 +339,7 @@ void KStars::initActions() {
fovActionMenu->setText( i18n("&FOV Symbols" ) );
fovActionMenu->setDelayed(false);
fovActionMenu->setIcon(QIcon::fromTheme("crosshairs"));
FOVManager::readFOVs();
repopulateFOV();
actionCollection()->addAction("geolocation", this, SLOT( slotGeoLocator() ) )
......@@ -557,18 +558,21 @@ void KStars::initActions() {
void KStars::repopulateFOV() {
// Read list of all FOVs
qDeleteAll( data()->availFOVs );
data()->availFOVs = FOV::readFOVs();
//qDeleteAll( data()->availFOVs );
data()->availFOVs = FOVManager::getFOVs();
data()->syncFOV();
// Iterate through FOVs
fovActionMenu->menu()->clear();
foreach(FOV* fov, data()->availFOVs) {
foreach(FOV* fov, data()->availFOVs)
{
KToggleAction *kta = actionCollection()->add<KToggleAction>( fov->name() );
kta->setText( fov->name() );
if( Options::fOVNames().contains( fov->name() ) ) {
if( Options::fOVNames().contains( fov->name() ) )
{
kta->setChecked(true);
}
fovActionMenu->addAction( kta );
connect( kta, SIGNAL( toggled( bool ) ), this, SLOT( slotTargetSymbol(bool) ) );
}
......
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