File indexing completed on 2024-05-12 05:39:54

0001 /***************************************************************************
0002  *  Copyright (C) 2021 by Renaud Guezennec                               *
0003  *   http://www.rolisteam.org/contact                                      *
0004  *                                                                         *
0005  *   This software 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 #ifndef UTILSHELPER_H
0021 #define UTILSHELPER_H
0022 
0023 #include <QFuture>
0024 #include <QFutureWatcher>
0025 #include <QPixmap>
0026 #include <QRect>
0027 #include <QString>
0028 #include <functional>
0029 
0030 #include "media/mediatype.h"
0031 #include "network/network_type.h"
0032 #include <core_global.h>
0033 
0034 namespace helper
0035 {
0036 namespace utils
0037 {
0038 CORE_EXPORT QString allSupportedImageFormatFilter();
0039 CORE_EXPORT QRectF computerBiggerRectInside(const QRect& rect, qreal ratio);
0040 CORE_EXPORT QPixmap roundCornerImage(const QPixmap& source, int size= 80, int radius= 8);
0041 CORE_EXPORT bool isSquareImage(const QByteArray& array);
0042 CORE_EXPORT bool hasValidCharacter(const std::vector<connection::CharacterData>& characters, bool isGameMaster);
0043 
0044 CORE_EXPORT QStringList extentionPerType(Core::ContentType type, bool save, bool wildcard= false);
0045 CORE_EXPORT QString filterForType(Core::ContentType, bool save);
0046 CORE_EXPORT QString typeToIconPath(Core::ContentType);
0047 CORE_EXPORT QString typeToString(Core::ContentType);
0048 CORE_EXPORT Core::ContentType mediaTypeToContentType(Core::MediaType type);
0049 CORE_EXPORT Core::ContentType extensionToContentType(const QString& filename);
0050 
0051 template <typename T>
0052 void setContinuation(QFuture<T> future, QObject* obj, std::function<void(T)> callback)
0053 {
0054     auto watcher= new QFutureWatcher<T>();
0055     QObject::connect(watcher, &QFutureWatcher<T>::finished, obj,
0056                      [watcher, callback]()
0057                      {
0058                          auto result= watcher->result();
0059                          callback(result);
0060                          delete watcher;
0061                      });
0062     watcher->setFuture(future);
0063 }
0064 
0065 template <typename T>
0066 void setParamIfAny(const QString& key, const std::map<QString, QVariant>& params, std::function<void(T)> setter)
0067 {
0068     auto it= params.find(key);
0069     if(params.end() != it)
0070     {
0071         setter(it->second.value<T>());
0072     }
0073 }
0074 
0075 template <typename T>
0076 void setParamIfAny(const QString& key, const QHash<QString, QVariant>& params, std::function<void(T)> setter)
0077 {
0078     auto it= params.find(key);
0079     if(params.end() != it)
0080     {
0081         setter(it.value().value<T>());
0082     }
0083 }
0084 } // namespace utils
0085 } // namespace helper
0086 
0087 #endif // UTILSHELPER_H