/*
Copyright (C) 2013 Arnaud Champenois arthelion92@gmail.com
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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "logotemplate.h"
#include "kernel.hpp"
#include <QImageReader>
#include <QImageWriter>
#include <QSettings>
#include <QDir>
#include <QtAlgorithms>
/*
* Gestion des templates de logo
* Les templates sont stockés dans le home directory/Videpub
* Il y a un .ini et un .png par logo (on utilise le système de fichiers comme une BD)
*/
LogoTemplate::LogoTemplate()
{
rank = 0;
notLogo = 0;
marginBefore = -1;
marginAfter = -1;
}
/*
* Création d'un logo depuis une image, un nom et une position
*/
LogoTemplate::LogoTemplate(QImage image, QString name, QRect pos, bool isNotLogo, int marginBeforeIn, int marginAfterIn) :
LogoTemplate()
{
this->image = image;
this->name = name;
this->pos = pos;
this->notLogo = isNotLogo;
this->marginAfter = marginAfterIn;
this->marginBefore = marginBeforeIn;
}
/*
* Création d'un logo par lecture d'un fichier
*/
LogoTemplate::LogoTemplate(QString fileName)
{
read(fileName);
}
/*
* API statique de lecture de la liste des templates
*
*/
QList<LogoTemplate> LogoTemplate::readTemplates() {
QList<LogoTemplate> templateList;
QString home = QDir::homePath();
home += "/VidePub";
QDir homeDir(home);
if(homeDir.exists()) {
QStringList filters;
filters << "???-logo.ini";
QStringList fileList = homeDir.entryList(filters);
QStringList::iterator iter;
for(iter = fileList.begin();iter!=fileList.end();iter++) {
QString fileName = (*iter);
kernel::FileNameString fns((home+"/"+fileName));
fns.SetExtension("bmp");
if(QFile(fns.GetPath()).exists()) {
LogoTemplate tmpl(fns.GetPath());
if(tmpl.isValid()) {
templateList.append(tmpl);
}
}
}
}
qSort(templateList);
return templateList;
}
/*
* Sauvegarde d'un logo dans le home directory
*/
void LogoTemplate::write(LogoTemplate tmpl) {
QDir home = QDir::home();
QDir newHome(home.absolutePath()+QString("/VidePub"));
if(!newHome.exists()) {
home.mkdir("VidePub");
}
kernel::FileNameString fns(newHome.absolutePath()+"/logo.ini");
int i=1;
QFile aFile(fns.GetPath(i));
while(aFile.exists())
aFile.setFileName(fns.GetPath(i++));
tmpl.save(aFile.fileName());
}
/*
* Sauvegarde de la liste complète des logos et remise en cohérence du rang
*/
void LogoTemplate::updateTemplates(QList<LogoTemplate> templates) {
QList<LogoTemplate>::iterator anIter;
int i=0;
for(anIter = templates.begin();anIter!=templates.end();anIter++) {
(*anIter).rank = i++;
(*anIter).saveIni();
}
}
/*
* Lecture du logo sur disque
*/
void LogoTemplate::read(QString fileName)
{
this->fileName = fileName;
kernel::FileNameString fns(fileName);
fns.SetExtension("bmp");
QImageReader reader;
reader.setFileName(QString(fns.GetPath(0)));
image = reader.read().convertToFormat(QImage::Format_RGB888);
fns.SetExtension("ini");
QSettings settings(fns.GetPath(0), QSettings::IniFormat);
name = settings.value("name").toString();
pos.setCoords(settings.value("x1").toInt(),
settings.value("y1").toInt(),
settings.value("x2").toInt(),
settings.value("y2").toInt());
rank = settings.value("rank").toInt();
int version = settings.value("version").toInt();
if(version>1) {
notLogo = settings.value("notLogo").toBool();
marginAfter = settings.value("marginAfter").toInt();
marginBefore = settings.value("marginBefore").toInt();
}
else
{
marginAfter = -1;
marginBefore = -1;
notLogo = false;
}
}
/*
* Sauvegarde d'un nouveau logo dans un fichier
*/
void LogoTemplate::save(QString fileName)
{
this->fileName=fileName;
kernel::FileNameString fns(fileName);
fns.SetExtension("bmp");
QImageWriter writer(QString(fns.GetPath(0)));
writer.setFormat("bmp");
writer.write(image);
saveIni();
}
/*
* Suppression d'un logo
*/
void LogoTemplate::remove() {
kernel::FileNameString fns(fileName);
fns.SetExtension("ini");
QFile file(fns.GetPath());
file.remove();
fns.SetExtension("bmp");
QFile file2(fns.GetPath());
file2.remove();
}
/*
* Sauvegarde des paramètres du logo dans un fichier ini
*/
void LogoTemplate::saveIni()
{
kernel::FileNameString fns(fileName);
fns.SetExtension("ini");
int x1,y1,x2,y2;
pos.getCoords(&x1,&y1,&x2,&y2);
QSettings settings(QString(fns.GetPath(0)), QSettings::IniFormat);
settings.setValue("name",name);
settings.setValue("x1",QVariant(x1));
settings.setValue("y1",QVariant(y1));
settings.setValue("x2",QVariant(x2));
settings.setValue("y2",QVariant(y2));
settings.setValue("rank",QVariant(rank));
settings.setValue("version",QVariant((int)2)); // pour éventuelles évolutions futures
settings.setValue("notLogo",QVariant(notLogo));
settings.setValue("marginAfter",QVariant(marginAfter));
settings.setValue("marginBefore",QVariant(marginBefore));
}