File indexing completed on 2024-05-05 04:46:57
0001 #include "fmh.h" 0002 0003 #include <QDebug> 0004 #include <QFileInfo> 0005 0006 namespace FMH 0007 { 0008 const QVector<int> modelRoles(const FMH::MODEL &model) 0009 { 0010 const auto keys = model.keys(); 0011 return std::accumulate(keys.begin(), keys.end(), QVector<int>(), [](QVector<int> &res, const FMH::MODEL_KEY &key) { 0012 res.append(key); 0013 return res; 0014 }); 0015 } 0016 0017 const QString mapValue(const QVariantMap &map, const FMH::MODEL_KEY &key) 0018 { 0019 return map[FMH::MODEL_NAME[key]].toString(); 0020 } 0021 0022 const QVariantMap toMap(const FMH::MODEL &model) 0023 { 0024 QVariantMap map; 0025 const auto keys = model.keys(); 0026 for (const auto &key : keys) 0027 { 0028 map.insert(FMH::MODEL_NAME[key], model[key]); 0029 } 0030 0031 return map; 0032 } 0033 0034 const FMH::MODEL toModel(const QVariantMap &map) 0035 { 0036 FMH::MODEL model; 0037 const auto keys = map.keys(); 0038 for (const auto &key : keys) 0039 { 0040 model.insert(FMH::MODEL_NAME_KEY[key], map[key].toString()); 0041 } 0042 0043 return model; 0044 } 0045 0046 const FMH::MODEL_LIST toModelList(const QVariantList &list) 0047 { 0048 FMH::MODEL_LIST res; 0049 return std::accumulate(list.constBegin(), list.constEnd(), res, [](FMH::MODEL_LIST &res, const QVariant &item) -> FMH::MODEL_LIST { 0050 res << FMH::toModel(item.toMap()); 0051 return res; 0052 }); 0053 } 0054 0055 const QVariantList toMapList(const FMH::MODEL_LIST &list) 0056 { 0057 QVariantList res; 0058 return std::accumulate(list.constBegin(), list.constEnd(), res, [](QVariantList &res, const FMH::MODEL &item) -> QVariantList { 0059 res << FMH::toMap(item); 0060 return res; 0061 }); 0062 } 0063 0064 const FMH::MODEL filterModel(const FMH::MODEL &model, const QVector<FMH::MODEL_KEY> &keys) 0065 { 0066 FMH::MODEL res; 0067 return std::accumulate(keys.constBegin(), keys.constEnd(), res, [=](FMH::MODEL &res, const FMH::MODEL_KEY &key) -> FMH::MODEL { 0068 if (model.contains(key)) 0069 res[key] = model[key]; 0070 return res; 0071 }); 0072 } 0073 0074 const QStringList modelToList(const FMH::MODEL_LIST &list, const FMH::MODEL_KEY &key) 0075 { 0076 QStringList res; 0077 return std::accumulate(list.constBegin(), list.constEnd(), res, [key](QStringList &res, const FMH::MODEL &item) -> QStringList { 0078 if (item.contains(key)) 0079 res << item[key]; 0080 return res; 0081 }); 0082 } 0083 0084 bool isAndroid() 0085 { 0086 #if defined(Q_OS_ANDROID) 0087 return true; 0088 #else 0089 return false; 0090 #endif 0091 } 0092 0093 bool isWindows() 0094 { 0095 #if defined(Q_OS_WIN32) 0096 return true; 0097 #elif defined(Q_OS_WIN64) 0098 return true; 0099 #else 0100 return false; 0101 #endif 0102 } 0103 0104 bool isLinux() 0105 { 0106 #if (defined Q_OS_LINUX || defined Q_OS_FREEBSD) && !defined Q_OS_ANDROID 0107 return true; 0108 #else 0109 return false; 0110 #endif 0111 } 0112 0113 bool isMac() 0114 { 0115 #if defined(Q_OS_MACOS) 0116 return true; 0117 #elif defined(Q_OS_MAC) 0118 return true; 0119 #else 0120 return false; 0121 #endif 0122 } 0123 0124 bool isIOS() 0125 { 0126 #if defined(Q_OS_iOS) 0127 return true; 0128 #else 0129 return false; 0130 #endif 0131 } 0132 0133 bool fileExists(const QUrl &path) 0134 { 0135 if (!path.isLocalFile()) { 0136 qWarning() << "URL recived is not a local file" << path; 0137 return false; 0138 } 0139 return QFileInfo::exists(path.toLocalFile()); 0140 } 0141 0142 0143 }