/*------------------------------------------------------------------------------ -- This file is a part of the QLop Software -- Copyright (C) 2015, Plasma Physics Laboratory - CNRS -- -- This program 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. -- -- This program 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 this program; if not, write to the Free Software -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -------------------------------------------------------------------------------*/ /*-- Author : Alexis Jeandet -- Mail : alexis.jeandet@member.fsf.org ----------------------------------------------------------------------------*/ #include "qlopdatabase.h" #include QList* QLopDataBase::m_dataBase=NULL; QLopDataBase* QLopDataBase::_self=NULL; QDockWidget* QLopDataBase::m_gui=NULL; #define INIT() \ if(Q_UNLIKELY(_self==NULL))\ {\ init();\ } QLopDataBase::QLopDataBase(bool noGUI,QObject *parent) : QLopService(parent) { this->m_dataBase = new QList(); m_serviceName="QLopDataBase"; m_noGui=noGUI; } QLopDataBase::~QLopDataBase() { } QDockWidget *QLopDataBase::getGUI() { if(!m_noGui && (m_gui==NULL)) { m_gui=new QLopDataBaseViewer(); m_gui->setFeatures(QDockWidget::DockWidgetMovable|QDockWidget::DockWidgetFloatable); } return m_gui; } void QLopDataBase::init(bool noGUI, QObject *parent) { _self=new QLopDataBase(noGUI,parent); } const QString &QLopDataBase::serviceName() { INIT(); return m_serviceName; } int QLopDataBase::addData(QLopData *data) { INIT(); if(!m_dataBase->contains(data)) { m_dataBase->append(data); emit QLopDataBase::self()->DBChanged(); return 1; } return 0; } int QLopDataBase::addData(const QLopDataList& data) { INIT(); int addedData=0; for (int i = 0; i < data.count(); i++) { addedData += addData(data.at(i)); } emit QLopDataBase::self()->DBChanged(); return addedData; } int QLopDataBase::removeData(QLopData *data) { INIT(); if(m_dataBase->contains(data)) { m_dataBase->removeAll(data); emit QLopDataBase::self()->DBChanged(); return 1; } return 0; } int QLopDataBase::removeData(const QLopDataList &data) { INIT(); int removedData=0; for (int i = 0; i < data.count(); i++) { removedData += removeData(data.at(i)); } emit QLopDataBase::self()->DBChanged(); return removedData; } QLopDataBase *QLopDataBase::self() { INIT(); return _self; } int QLopDataBase::count() { INIT(); return m_dataBase->count(); } QLopData *QLopDataBase::getData(const QString &name) { INIT(); for (int i = 0; i < m_dataBase->count(); i++) { if(Q_UNLIKELY(m_dataBase->at(i)->name==name)) { return m_dataBase->at(i); } } return NULL; } QLopData *QLopDataBase::getData(int ID) { INIT(); for (int i = 0; i < m_dataBase->count(); i++) { if(Q_UNLIKELY(m_dataBase->at(i)->ID==ID)) { return m_dataBase->at(i); } } return NULL; } QLopData *QLopDataBase::getDataFromIdex(int index) { if((index>=0)&&(indexcount())) return m_dataBase->at(index); else return NULL; }