File indexing completed on 2024-05-12 04:56:35
0001 /*************************************************************************** 0002 * Copyright (C) 2005-2017 by Linuxstopmotion contributors; * 0003 * see the AUTHORS file for details. * 0004 * * 0005 * This program is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0013 * GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the * 0017 * Free Software Foundation, Inc., * 0018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0019 ***************************************************************************/ 0020 #include "modelhandler.h" 0021 0022 #include "logger.h" 0023 #include "src/presentation/frontends/qtfrontend/mainwindowgui.h" 0024 #include "src/presentation/frontends/qtfrontend/framebar/framebar.h" 0025 #include "src/domain/domainfacade.h" 0026 #include "src/technical/stringiterator.h" 0027 #include "src/technical/util.h" 0028 0029 #include <stddef.h> 0030 #include <string> 0031 #include <QChar> 0032 #include <QFileDialog> 0033 #include <QMessageBox> 0034 #include <QMimeData> 0035 #include <QProcess> 0036 #include <QPushButton> 0037 #include <QStatusBar> 0038 #include <QString> 0039 #include <QStringList> 0040 0041 ModelHandler::ModelHandler ( QObject *parent, QStatusBar *sb, FrameBar *frameBar, 0042 QString* lastVisitedDir, const char *name ) 0043 : QObject(parent), frameBar(frameBar), statusBar(sb), 0044 lastVisitedDir(lastVisitedDir) { 0045 fileDialog = NULL; 0046 removeFramesButton = NULL; 0047 setObjectName(name); 0048 } 0049 0050 0051 ModelHandler::~ModelHandler() { 0052 } 0053 0054 0055 void ModelHandler::setRemoveFramesButton(QPushButton* removeFramesButton) { 0056 this->removeFramesButton = removeFramesButton; 0057 } 0058 0059 0060 void ModelHandler::chooseFrame() { 0061 fileDialog = new QFileDialog((MainWindowGUI*)parent(), 0062 tr("Choose frames to add"), *lastVisitedDir); 0063 QStringList filters; 0064 filters << "Images (*.png *.jpg *.jpeg *.gif *.PNG *.JPG *.JPEG *.GIF)" 0065 << "Joint Photographic Ex. Gr. (*.jpg *.jpeg *.JPG *.JPEG)" 0066 << "Portable Network Graphics (*.png *.PNG)" 0067 << "GIMP native (*.xcf *.XCF)" 0068 << "Tagged Image File Format (*.tif *.TIF)" 0069 << "Windows Bitmap (*.bmp *.BMP)" 0070 << "TrueVision Targa (*.tga *.TGA)" 0071 << "Portable Anymap (*.pnm *.PNM)" 0072 << "X11 Pixmap (*.xpm *.XPM)" 0073 << "ZSoft IBM PC Paintbrush (*.pcx *.PCX)" 0074 << "CompuServe Graph. Interch. Format (*.gif *.GIF)" 0075 << "Interleaved Bitmap (*.lbm *.iff *.LBM *.IFF)" 0076 << "All files (*)"; 0077 fileDialog->setNameFilters(filters); 0078 fileDialog->setAcceptMode(QFileDialog::AcceptOpen); 0079 fileDialog->setFileMode(QFileDialog::ExistingFiles); 0080 0081 //PicturePreview* p = new PicturePreview(fileDialog); 0082 0083 QObject::connect( fileDialog, SIGNAL(filesSelected (const QStringList &)), 0084 this, SLOT(addFrames(const QStringList &)) ); 0085 0086 fileDialog->show(); 0087 } 0088 0089 0090 class StringListIterator : public StringIterator { 0091 QStringList::ConstIterator b; 0092 QStringList::ConstIterator e; 0093 std::string buffer; 0094 0095 void set() { 0096 if (!atEnd()) { 0097 buffer = b->toStdString(); 0098 buffer.c_str(); 0099 } 0100 } 0101 0102 public: 0103 StringListIterator(QStringList::ConstIterator begin, 0104 QStringList::ConstIterator end) : b(begin), e(end) { 0105 set(); 0106 } 0107 ~StringListIterator() { 0108 } 0109 int count() { 0110 int c = 0; 0111 for (QStringList::ConstIterator i(b); i != e; ++i) { 0112 ++c; 0113 } 0114 return c; 0115 } 0116 bool atEnd() const { 0117 return b == e; 0118 } 0119 const char* get() const { 0120 return &buffer[0]; 0121 } 0122 void next() { 0123 ++b; 0124 set(); 0125 } 0126 }; 0127 0128 0129 void ModelHandler::addFrames(const QStringList & fileNames) { 0130 Logger::get().logDebug("addFrames in modelhandler"); 0131 0132 // the fileDialog pointer is NULL when adding of frames is 0133 // done by drag 'n drop 0134 if ( fileDialog != NULL ) { 0135 fileDialog->hide(); 0136 *lastVisitedDir = fileDialog->directory().path(); 0137 } 0138 0139 if ( !fileNames.isEmpty() ) { 0140 StringListIterator fNames(fileNames.begin(), fileNames.end()); 0141 int scene = frameBar->getActiveScene(); 0142 int frame = frameBar->getActiveFrame() + 1; 0143 if (scene < 0) { 0144 scene = 0; 0145 } 0146 DomainFacade* facade = DomainFacade::getFacade(); 0147 int frameCount = facade->getSceneSize(scene); 0148 if (frameCount < frame) 0149 frame = frameCount; 0150 if (frame < 0) 0151 frame = 0; 0152 facade->addFrames(scene, frame, fNames); 0153 emit modelChanged(); 0154 } 0155 } 0156 0157 0158 void ModelHandler::addFrame( const QString &fileName ) { 0159 if (fileDialog != NULL) { 0160 fileDialog->hide(); 0161 *lastVisitedDir = fileDialog->directory().path(); 0162 } 0163 0164 QStringList fileNames; 0165 fileNames.push_back(fileName); 0166 this->addFrames(fileNames); 0167 } 0168 0169 0170 void ModelHandler::removeFrames() { 0171 if (removeFramesButton->isEnabled()) { 0172 int selectionFrame = frameBar->getSelectionAnchor(); 0173 int activeScene = frameBar->getActiveScene(); 0174 int activeFrame = frameBar->getActiveFrame(); 0175 int lowend = (selectionFrame < activeFrame ) ? selectionFrame : activeFrame; 0176 int highend = (selectionFrame > activeFrame ) ? selectionFrame : activeFrame; 0177 if (activeScene < 0 || lowend < 0) { 0178 return; 0179 } 0180 0181 DomainFacade::getFacade()->removeFrames(activeScene, lowend, 0182 highend - lowend + 1); 0183 statusBar->showMessage( tr("Removed the selected frame"), 2000 ); 0184 } 0185 } 0186 0187 0188 void ModelHandler::newScene() { 0189 int activeScene = frameBar->getActiveScene(); 0190 0191 if (activeScene >= 0) { 0192 DomainFacade::getFacade()->newScene(activeScene+1); 0193 } 0194 else { 0195 int numScenes = DomainFacade::getFacade()->getNumberOfScenes(); 0196 if(numScenes > 0) { 0197 DomainFacade::getFacade()->newScene(numScenes); 0198 } 0199 else { 0200 DomainFacade::getFacade()->newScene(activeScene+1); 0201 } 0202 } 0203 emit modelChanged(); 0204 } 0205 0206 0207 void ModelHandler::removeScene() { 0208 int activeScene = frameBar->getActiveScene(); 0209 if (0 <= activeScene) 0210 DomainFacade::getFacade()->removeScene(activeScene); 0211 } 0212 0213 0214 /*! 0215 \fn ModelHandler::editCurrentFrame() 0216 */ 0217 int ModelHandler::editCurrentFrame() { 0218 std::string gimpCommand; 0219 if (!Util::checkCommand(&gimpCommand, "gimp")) { 0220 QMessageBox::warning(static_cast<MainWindowGUI *>(parent()), tr("Warning"), 0221 tr("You do not have Gimp installed on your system"), 0222 QMessageBox::Ok, Qt::NoButton, Qt::NoButton); 0223 return 1; 0224 } 0225 0226 // Determine the active scene and active frame. 0227 int activeScene = frameBar->getActiveScene(); 0228 int activeFrame = frameBar->getActiveFrame(); 0229 0230 if (activeScene < 0 || activeFrame < 0) { 0231 QMessageBox::warning(static_cast<MainWindowGUI *>(parent()), tr("Warning"), 0232 tr("There is no active frame to open"), 0233 QMessageBox::Ok, Qt::NoButton, Qt::NoButton); 0234 return 1; 0235 } 0236 DomainFacade* facade = DomainFacade::getFacade(); 0237 facade->duplicateImage(activeScene, activeFrame); 0238 const char *path = facade->getImagePath(activeScene, activeFrame); 0239 0240 QStringList argList; 0241 // arg0 are the options, and arg1 is the path of the frame. 0242 // Start Gimp without splash screen. 0243 argList.append(QLatin1String("--no-splash")); 0244 argList.append(QString::fromLocal8Bit(path)); 0245 0246 QProcess process; 0247 if (!process.startDetached(QLatin1String(gimpCommand.c_str()), argList)) { 0248 QMessageBox::warning(static_cast<MainWindowGUI *>(parent()), tr("Warning"), 0249 tr("Failed to start Gimp!"), 0250 QMessageBox::Ok, Qt::NoButton, Qt::NoButton); 0251 return 1; 0252 } 0253 0254 return 0; 0255 }