You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
457 lines
12 KiB
457 lines
12 KiB
////////////////////////////////////////////////////////////////////////////////
|
|
/**
|
|
* \file project.cpp
|
|
* \version v1.0
|
|
* \author Ing. Dominik Malcik
|
|
*/
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include "project.h"
|
|
#include "mainwindow.h"
|
|
#include "xmlparser.h"
|
|
|
|
// Konstanty
|
|
const QString Project::PROJVERSION = QString("1.0");
|
|
const QString Project::PROJSUBDIR_IMAGES = QString("images");
|
|
const QString Project::PROJSUBDIR_LAYERS = QString("layers");
|
|
const QString Project::PROJ_FILE_EXT = QString(".mapro");
|
|
const QString Project::XML_TRUE = QString("1");
|
|
const QString Project::XML_FALSE = QString("0");
|
|
|
|
// --------------------------------------------
|
|
/**
|
|
* @brief = KONSTRUKTOR
|
|
*/
|
|
// --------------------------------------------
|
|
Project::Project(QObject *parent) : QObject(parent){
|
|
valueInit();
|
|
}
|
|
|
|
// --------------------------------------------
|
|
/**
|
|
* @brief = DESTRUKTOR
|
|
*/
|
|
// --------------------------------------------
|
|
Project::~Project()
|
|
{
|
|
}
|
|
|
|
void Project::valueInit() {
|
|
setProjectName("");
|
|
setProjectFileName("");
|
|
setProjectRootDir("");
|
|
setProjectRootDir_old("");
|
|
setImagesDir(PROJSUBDIR_IMAGES);
|
|
setLayersDir(PROJSUBDIR_LAYERS);
|
|
setFileImported(false);
|
|
std::vector<QString> imgs;
|
|
setImages(imgs);
|
|
std::vector<QString> lrs;
|
|
setLayers(lrs);
|
|
}
|
|
|
|
void Project::setProjectName (QString s) {
|
|
this->projectName = s;
|
|
}
|
|
|
|
QString Project::getProjectName (void) {
|
|
return this->projectName;
|
|
}
|
|
|
|
void Project::setProjectFileName (QString s) {
|
|
this->projectFileName = s;
|
|
}
|
|
|
|
QString Project::getProjectFileName (void) {
|
|
return this->projectFileName;
|
|
}
|
|
|
|
void Project::setProjectRootDir (QString s) {
|
|
this->projectRootDir = s;
|
|
}
|
|
|
|
QString Project::getProjectRootDir (void) {
|
|
return this->projectRootDir;
|
|
}
|
|
|
|
void Project::setProjectRootDir_old (QString s) {
|
|
this->projectRootDir_old = s;
|
|
}
|
|
|
|
QString Project::getProjectRootDir_old (void) {
|
|
return this->projectRootDir_old;
|
|
}
|
|
|
|
void Project::setImagesDir (QString s) {
|
|
this->imagesDir = s;
|
|
}
|
|
|
|
QString Project::getImagesDir (void) {
|
|
return this->imagesDir;
|
|
}
|
|
|
|
void Project::setLayersDir (QString s) {
|
|
this->layersDir = s;
|
|
}
|
|
|
|
QString Project::getLayersDir (void) {
|
|
return this->layersDir;
|
|
}
|
|
|
|
void Project::setFileImported (bool i) {
|
|
this->fileImported = i;
|
|
}
|
|
|
|
bool Project::getFileImported (void) {
|
|
return this->fileImported;
|
|
}
|
|
|
|
|
|
void Project::setImages (std::vector<QString> i) {
|
|
if (i != getImages()) {
|
|
this->images = i;
|
|
emit imagesValueChanged();
|
|
}
|
|
}
|
|
|
|
std::vector<QString> Project::getImages (void) {
|
|
return this->images;
|
|
}
|
|
|
|
void Project::addToImages (QString s) {
|
|
this->images.push_back(s);
|
|
emit imagesValueChanged();
|
|
}
|
|
|
|
QString Project::getSelectedImage (unsigned int index) {
|
|
if (images.size() > index) {
|
|
return this->images[index];
|
|
}
|
|
else {
|
|
return "ERROR";
|
|
}
|
|
}
|
|
|
|
void Project::eraseSelectedImage(unsigned int index) {
|
|
if (images.size() > index) {
|
|
std::vector<QString>::iterator it = images.begin();
|
|
it += index;
|
|
images.erase(it);
|
|
}
|
|
}
|
|
|
|
void Project::emitDeleteImported() {
|
|
emit imagesValueChanged();
|
|
}
|
|
|
|
|
|
|
|
|
|
void Project::setLayers (std::vector<QString> i) {
|
|
if (i != getLayers()) {
|
|
this->layers = i;
|
|
emit layersValueChanged();
|
|
}
|
|
}
|
|
|
|
std::vector<QString> Project::getLayers (void) {
|
|
return this->layers;
|
|
}
|
|
|
|
void Project::addToLayers (QString s) {
|
|
this->layers.push_back(s);
|
|
emit layersValueChanged();
|
|
}
|
|
|
|
QString Project::getSelectedLayer (unsigned int index) {
|
|
if (layers.size() > index) {
|
|
return this->layers[index];
|
|
}
|
|
else {
|
|
return "ERROR";
|
|
}
|
|
}
|
|
|
|
void Project::eraseSelectedLayer(unsigned int index) {
|
|
if (layers.size() > index) {
|
|
std::vector<QString>::iterator it = layers.begin();
|
|
it += index;
|
|
layers.erase(it);
|
|
}
|
|
}
|
|
|
|
void Project::emitDeleteAnalysings() {
|
|
emit layersValueChanged();
|
|
}
|
|
|
|
int Project::layersCount() {
|
|
return this->layers.size();
|
|
}
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// ----------------------------------------------------------------------------
|
|
// ----------------------------------------------------------------------------
|
|
|
|
void Project::prepareProject(QString rootDir, QString projectName, QString projectFileName,
|
|
bool fileImported, std::vector<QString> images, std::vector<QString> layers) {
|
|
|
|
setProjectRootDir_old(getProjectRootDir());
|
|
setProjectRootDir(rootDir);
|
|
setProjectName(projectName);
|
|
setProjectFileName(projectFileName);
|
|
setFileImported(fileImported);
|
|
setImagesDir(Project::PROJSUBDIR_IMAGES);
|
|
setLayersDir(Project::PROJSUBDIR_LAYERS);
|
|
setImages(images);
|
|
setLayers(layers);
|
|
}
|
|
|
|
bool Project::fileCopy(QString fileName, QString destDir, QString destFilename, QString * newFileName) {
|
|
|
|
// remembering of the filename
|
|
QFileInfo fi(fileName);
|
|
|
|
// get the number for filename (filename_number.ext)
|
|
QDir dir(destDir);
|
|
int number = dir.count();
|
|
number--;
|
|
|
|
while (1) {
|
|
QString numStr = QString::number(number);
|
|
QFile destFile(destDir + "/" + destFilename + "_" + numStr + "." + fi.completeSuffix());
|
|
|
|
if (!destFile.exists()) {
|
|
break;
|
|
}
|
|
else {
|
|
number++;
|
|
}
|
|
}
|
|
|
|
// copy the file to the project directory with a specified filename
|
|
QString numStr = QString::number(number);
|
|
QString finalDestFileString = destDir + "/" + destFilename + "_" + numStr + "." + fi.completeSuffix();
|
|
QFile sourceFile(fileName);
|
|
QFile finalDestFile(finalDestFileString);
|
|
|
|
QFileInfo finalFi(finalDestFileString);
|
|
*newFileName = finalFi.baseName() + "." + finalFi.completeSuffix();
|
|
|
|
if (sourceFile.exists()) {
|
|
if (!sourceFile.copy(finalDestFile.fileName())) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool Project::removeDir(QString dirName) {
|
|
bool result = true;
|
|
QDir dir(dirName);
|
|
|
|
if (dir.exists(dirName)) {
|
|
Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) {
|
|
if (info.isDir()) {
|
|
result = this->removeDir(info.absoluteFilePath());
|
|
}
|
|
else {
|
|
result = QFile::remove(info.absoluteFilePath());
|
|
}
|
|
|
|
if (!result) {
|
|
return result;
|
|
}
|
|
}
|
|
result = dir.rmdir(dirName);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
bool Project::saveToFile(bool removeSubDirs) {
|
|
|
|
if (getProjectRootDir() == getProjectRootDir_old()) {
|
|
removeSubDirs = false;
|
|
}
|
|
|
|
QFile mapro(getProjectRootDir() + "/" + getProjectFileName());
|
|
if (mapro.exists()) {
|
|
if (!mapro.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
|
return false;
|
|
}
|
|
}
|
|
else {
|
|
if (!mapro.open(QIODevice::ReadWrite)) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// make an entry in XML for an imported image
|
|
QString importedInt = XML_FALSE;
|
|
if (getFileImported()) {
|
|
importedInt = XML_TRUE;
|
|
}
|
|
|
|
// prepare directory structure for the project
|
|
QString projImages_dir = getProjectRootDir();
|
|
projImages_dir.append("/").append(getImagesDir());
|
|
QString projLayers_dir = getProjectRootDir();
|
|
projLayers_dir.append("/").append(getLayersDir());
|
|
|
|
if (removeSubDirs) {
|
|
if (QDir(projImages_dir).exists()) {
|
|
removeDir(projImages_dir);
|
|
}
|
|
QDir().mkpath(projImages_dir);
|
|
|
|
if (QDir(projLayers_dir).exists()) {
|
|
removeDir(projLayers_dir);
|
|
}
|
|
QDir().mkpath(projLayers_dir);
|
|
}
|
|
else {
|
|
if (!QDir(projImages_dir).exists()) {
|
|
QDir().mkpath(projImages_dir);
|
|
}
|
|
|
|
if (!QDir(projLayers_dir).exists()) {
|
|
QDir().mkpath(projLayers_dir);
|
|
}
|
|
}
|
|
|
|
// copying of the files and creating an XML entry
|
|
// BACKGROUNDs
|
|
QString importedImageFilesXML = "";
|
|
std::vector<QString> tmpB = getImages();
|
|
for (uint i = 0; i < getImages().size(); i++) {
|
|
QString scrImage = getImages()[i]; // source file
|
|
QFileInfo scrFi(scrImage);
|
|
|
|
// if the file is already in the right place, do not perform the copying
|
|
bool fileFromProjectDir = false;
|
|
if (scrImage == (scrFi.baseName() + "." + scrFi.completeSuffix())) {
|
|
scrImage = getProjectRootDir_old() + "/" + getImagesDir() + "/" + scrImage;
|
|
fileFromProjectDir = true;
|
|
}
|
|
|
|
QString destAddr = getProjectRootDir() + "/" + getImagesDir(); // destination directory
|
|
QString newFileName_part = "pict"; // renaming of the file
|
|
QString newFileName_whole; // final file name for XML entry
|
|
|
|
// for SAVE it does not copy files that already exist in the project
|
|
bool makeCopy = true;
|
|
QFile testExist(scrImage);
|
|
if (!removeSubDirs && fileFromProjectDir && testExist.exists()) {
|
|
makeCopy = false;
|
|
newFileName_whole = getImages()[i];
|
|
}
|
|
|
|
if (makeCopy && !fileCopy(scrImage, destAddr, newFileName_part, &newFileName_whole)) {
|
|
removeDir(getProjectRootDir());
|
|
return false;
|
|
}
|
|
|
|
if (makeCopy) {
|
|
tmpB[i] = newFileName_whole;
|
|
}
|
|
|
|
importedImageFilesXML += "\t\t<image>" + newFileName_whole + "</image>\n";
|
|
}
|
|
setImages(tmpB);
|
|
|
|
|
|
// copying of the files and creating an XML entry
|
|
// LAYERs
|
|
QString importedLayerFilesXML = "";
|
|
std::vector<QString> tmpL = getLayers();
|
|
for (uint i = 0; i < getLayers().size(); i++) {
|
|
|
|
QString scrImage = getLayers()[i]; // source file
|
|
QFileInfo scrFi(scrImage);
|
|
|
|
bool fileFromProjectDir = false;
|
|
if (scrImage == (scrFi.baseName() + "." + scrFi.completeSuffix())) {
|
|
scrImage = getProjectRootDir_old() + "/" + getLayersDir() + "/" + scrImage;
|
|
fileFromProjectDir = true;
|
|
}
|
|
|
|
QString destAddr = getProjectRootDir() + "/" + getLayersDir(); // destination directory
|
|
QString newFileName_part = scrFi.baseName(); // renaming of the file
|
|
QString newFileName_whole; // final file name for XML entry
|
|
|
|
// for SAVE it does not copy files that already exist in the project
|
|
bool makeCopy = true;
|
|
QFile testExist(scrImage);
|
|
if (!removeSubDirs && fileFromProjectDir && testExist.exists()) {
|
|
makeCopy = false;
|
|
newFileName_whole = getLayers()[i];
|
|
}
|
|
|
|
if (makeCopy && !fileCopy(scrImage, destAddr, newFileName_part, &newFileName_whole)) {
|
|
removeDir(getProjectRootDir());
|
|
return false;
|
|
}
|
|
|
|
if (makeCopy) {
|
|
tmpL[i] = newFileName_whole;
|
|
}
|
|
|
|
importedLayerFilesXML += "\t\t<layer>" + newFileName_whole + "</layer>\n";
|
|
}
|
|
setLayers(tmpL);
|
|
|
|
|
|
// XML format of the project file
|
|
QTextStream ts( &mapro );
|
|
ts << "<ma_project>" << endl <<
|
|
"\t<version>" + PROJVERSION + "</version>" << endl <<
|
|
"\t<name>" + getProjectName() + "</name>" << endl <<
|
|
"\t<fileImported>" + importedInt + "</fileImported>" << endl <<
|
|
"\t<imagesDir>" + getImagesDir() + "</imagesDir>" << endl <<
|
|
"\t<layersDir>" + getLayersDir() + "</layersDir>" << endl <<
|
|
"\t<images>" << endl;
|
|
if (importedImageFilesXML.length() > 0) {
|
|
ts << importedImageFilesXML;
|
|
}
|
|
ts << "\t</images>" << endl <<
|
|
"\t<layers>" << endl;
|
|
if (importedLayerFilesXML.length() > 0) {
|
|
ts << importedLayerFilesXML;
|
|
}
|
|
ts << "\t</layers>" << endl <<
|
|
"</ma_project>" << endl;
|
|
|
|
mapro.close();
|
|
return true;
|
|
}
|
|
|
|
bool Project::deleteAnalysing(int index) {
|
|
// prepare path for removing of a file
|
|
QString fileToRemove = getProjectRootDir() + "/";
|
|
fileToRemove += getLayersDir() + "/";
|
|
fileToRemove += getSelectedLayer(index);
|
|
|
|
// remove file from LAYERs
|
|
eraseSelectedLayer(index);
|
|
return QFile::remove(fileToRemove);
|
|
}
|
|
|
|
|
|
bool Project::deleteImported(int index) {
|
|
// prepare path for removing of a file
|
|
QString fileToRemove = getProjectRootDir() + "/";
|
|
fileToRemove += getImagesDir() + "/";
|
|
fileToRemove += getSelectedImage(index);
|
|
|
|
// remove file from BACKGROUNDs
|
|
eraseSelectedImage(index);
|
|
|
|
// samotne smazani souboru
|
|
return QFile::remove(fileToRemove);
|
|
}
|
|
|
|
|