File indexing completed on 2024-05-05 05:41:12
0001 #include "utils/iohelper.h" 0002 #include "qbuffer.h" 0003 #include "utils/networkdownloader.h" 0004 0005 #include <QByteArray> 0006 #include <QDir> 0007 #include <QFile> 0008 #include <QSaveFile> 0009 #include <QTextStream> 0010 #include <QSignalSpy> 0011 0012 0013 namespace utils 0014 { 0015 namespace IOHelper 0016 { 0017 QByteArray loadFile(const QString& filepath) 0018 { 0019 QByteArray data; 0020 if(filepath.isEmpty()) 0021 return data; 0022 QFile file(filepath); 0023 if(file.open(QIODevice::ReadOnly)) 0024 { 0025 data= file.readAll(); 0026 } 0027 return data; 0028 } 0029 0030 bool makeSurePathExist(const QDir& dir) 0031 { 0032 if(!dir.exists()) 0033 dir.mkpath(dir.path()); 0034 0035 return dir.exists(); 0036 } 0037 0038 bool makeDir(const QString& dir) 0039 { 0040 return makeSurePathExist(QDir(dir)); 0041 } 0042 0043 QString copyFile(const QString& source, const QString& destination) 0044 { 0045 QFile src(source); 0046 QFileInfo srcInfo(source); 0047 QFileInfo dest(destination); 0048 if(dest.isDir()) 0049 dest.setFile(QString("%1/%2").arg(destination, srcInfo.fileName())); 0050 0051 if(makeSurePathExist(dest.dir())) 0052 { 0053 if(src.copy(dest.absoluteFilePath())) 0054 return dest.absoluteFilePath(); 0055 } 0056 return {}; 0057 } 0058 0059 bool removeFile(const QString& source) 0060 { 0061 return QFile::remove(source); 0062 } 0063 0064 bool writeFile(const QString& path, const QByteArray& arry, bool override) 0065 { 0066 if(arry.isEmpty()) 0067 return false; 0068 0069 QFileInfo pathInfo(path); 0070 makeDir(pathInfo.absolutePath()); 0071 0072 QSaveFile file(path); 0073 if(file.open(override ? QIODevice::WriteOnly : QIODevice::Append)) 0074 { 0075 file.write(arry); 0076 file.commit(); 0077 return true; 0078 } 0079 return false; 0080 } 0081 0082 bool moveFile(const QString& source, const QString& destination) 0083 { 0084 QFile src(source); 0085 QFileInfo srcInfo(source); 0086 QFileInfo dest(destination); 0087 0088 if(dest.isDir()) 0089 dest.setFile(QString("%1/%2").arg(destination, srcInfo.fileName())); 0090 0091 bool res= false; 0092 if(makeSurePathExist(dest.dir())) 0093 { 0094 res= src.rename(dest.absoluteFilePath()); 0095 } 0096 return res; 0097 } 0098 0099 QString readTextFile(const QString& path) 0100 { 0101 if(path.isEmpty()) 0102 return {}; 0103 QFile file(path); 0104 if(!file.open(QIODevice::ReadOnly)) 0105 return {}; 0106 0107 QTextStream out(&file); 0108 return out.readAll(); 0109 } 0110 0111 QPixmap readPixmapFromFile(const QString& uri) 0112 { 0113 return QPixmap(uri); 0114 } 0115 0116 QPixmap readPixmapFromWeb(const QUrl& url) 0117 { 0118 NetworkDownloader dl(url); 0119 QSignalSpy spy(&dl, &NetworkDownloader::finished); 0120 0121 spy.wait(5000); 0122 if(spy.count() == 0) 0123 return {}; 0124 else 0125 { 0126 auto first = spy.takeFirst(); 0127 auto hasImage = first.at(1).toBool(); 0128 auto imageData = first.at(0).toByteArray(); 0129 if(hasImage) 0130 return QPixmap::fromImage(dataToImage(imageData)); 0131 } 0132 return {}; 0133 } 0134 0135 QPixmap readPixmapFromURL(const QUrl& url) 0136 { 0137 QPixmap map; 0138 if(url.isLocalFile()) 0139 { 0140 map= readPixmapFromFile(url.toLocalFile()); 0141 } 0142 else if(url.scheme() == QStringLiteral("qrc")) 0143 { 0144 map= readPixmapFromFile(url.path()); 0145 } 0146 else if(url.scheme().startsWith(QStringLiteral("http"))) 0147 { 0148 map = readPixmapFromWeb(url); 0149 } 0150 return map; 0151 } 0152 QString shortNameFromPath(const QString& path) 0153 { 0154 QFileInfo info(path); 0155 return info.baseName(); 0156 } 0157 QString absoluteToRelative(const QString& absolute, const QString& root) 0158 { 0159 QDir dir(root); 0160 QFileInfo path(absolute); 0161 if(!path.isAbsolute()) 0162 return absolute; 0163 0164 return dir.relativeFilePath(absolute); 0165 } 0166 0167 QImage dataToImage(const QByteArray& data) 0168 { 0169 QImage img; 0170 img.loadFromData(data); 0171 return img; 0172 } 0173 0174 QByteArray imageToData(const QImage &pix) 0175 { 0176 QByteArray bytes; 0177 QBuffer buffer(&bytes); 0178 buffer.open(QIODevice::WriteOnly); 0179 pix.save(&buffer, "PNG"); 0180 return bytes; 0181 } 0182 0183 } // namespace IOHelper 0184 } // namespace utils