All Classes Namespaces Functions Variables Enumerations Properties Pages
fileformat.cpp
1 /*
2 
3 Pencil2D - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2020 Matthew Chiawen Chang
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; version 2 of the License.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 */
17 
18 #include "fileformat.h"
19 #include <QDir>
20 #include <QFileInfo>
21 #include <QMap>
22 
23 bool removePFFTmpDirectory(const QString& dirName)
24 {
25  if (dirName.isEmpty())
26  {
27  return false;
28  }
29 
30  QDir dir(dirName);
31 
32  if (!dir.exists())
33  {
34  Q_ASSERT(false);
35  return false;
36  }
37 
38  bool result = dir.removeRecursively();
39  return result;
40 }
41 
42 QString uniqueString(int len)
43 {
44  static const char alphanum[] = "0123456789abcdefghijklmnopqrstuvwxyz";
45  const int alphanum_len = sizeof(alphanum);
46 
47  if (len > 128) len = 128;
48 
49  char s[128 + 1];
50  for (int i = 0; i < len; ++i)
51  {
52  s[i] = alphanum[rand() % (alphanum_len - 1)];
53  }
54  s[len] = 0;
55  return QString::fromUtf8(s);
56 }
57 
58 QString retrieveProjectNameFromTempPath(const QString& path)
59 {
60  QFileInfo info(path);
61  QString fileName = info.completeBaseName();
62 
63  QStringList tokens = fileName.split("_");
64  //qDebug() << tokens;
65  return tokens[0];
66 }
67 
68 QString detectFormatByFileNameExtension(const QString& fileName)
69 {
70  QMap<QString, QString> extensionMapping
71  {
72  { "png", "PNG" },
73  { "jpg" , "JPG" },
74  { "jpeg", "JPG" },
75  { "tif", "TIF" },
76  { "tiff", "TIF" },
77  { "bmp", "BMP" },
78  { "mp4", "MP4" },
79  { "avi", "AVI" },
80  { "gif", "GIF" },
81  { "webm", "WEBM" },
82  { "apng", "APNG" },
83  };
84 
85  QString extension = fileName.mid(fileName.lastIndexOf(".") + 1).toLower();
86  if (!fileName.contains(".") || !extensionMapping.contains(extension))
87  {
88  return QString();
89  }
90  return extensionMapping[extension];
91 }
92 
93 bool isMovieFormat(const QString& format)
94 {
95  QMap<QString, bool> formatMapping
96  {
97  { "PNG", false },
98  { "JPG", false },
99  { "TIF", false },
100  { "BMP", false },
101  { "MP4", true },
102  { "AVI", true },
103  { "GIF", true },
104  { "WEBM", true },
105  { "APNG", true },
106  };
107 
108  Q_ASSERT(formatMapping.contains(format));
109  return formatMapping[format];
110 }
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
QString fromUtf8(const char *str, int size)
bool isEmpty() const const
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QString mid(int position, int n) const const