File indexing completed on 2024-05-12 05:40:51

0001 #include <algorithm>
0002 
0003 #include "helper_global.h"
0004 #include "network/networkmessage.h"
0005 // #include "utils/iohelper.h"
0006 #include <QByteArray>
0007 #include <QColor>
0008 #include <QPointF>
0009 #include <QRectF>
0010 #include <QStringList>
0011 #include <QUrl>
0012 #include <QMovie>
0013 #include <map>
0014 #include <random>
0015 #include <type_traits>
0016 #include <utility>
0017 
0018 class QObject;
0019 namespace Helper
0020 {
0021 template <typename Iterator>
0022 bool next_combination(const Iterator first, Iterator k, const Iterator last)
0023 {
0024     /* Credits: Mark Nelson http://marknelson.us */
0025     if((first == last) || (first == k) || (last == k))
0026         return false;
0027     Iterator i1= first;
0028     Iterator i2= last;
0029     ++i1;
0030     if(last == i1)
0031         return false;
0032     i1= last;
0033     --i1;
0034     i1= k;
0035     --i2;
0036     while(first != i1)
0037     {
0038         if(*--i1 < *i2)
0039         {
0040             Iterator j= k;
0041             while(!(*i1 < *j))
0042                 ++j;
0043             std::iter_swap(i1, j);
0044             ++i1;
0045             ++j;
0046             i2= k;
0047             std::rotate(i1, j, last);
0048             while(last != j)
0049             {
0050                 ++j;
0051                 ++i2;
0052             }
0053             std::rotate(k, i2, last);
0054             return true;
0055         }
0056     }
0057     std::rotate(first, k, last);
0058     return false;
0059 }
0060 
0061 class HELPER_EXPORT TestMessageSender : public MessageSenderInterface
0062 {
0063 public:
0064     TestMessageSender();
0065     virtual void sendMessage(const NetworkMessage* msg) override;
0066 
0067     QByteArray messageData() const;
0068 
0069 private:
0070     QByteArray m_msgData;
0071 };
0072 
0073 HELPER_EXPORT std::pair<bool, QStringList> testAllProperties(QObject* obj, bool setAgain= true);
0074 
0075 template <typename T>
0076 T generate(const T& min, const T& max) //=std::numeric_limits<T>::min(),
0077                                        //=std::numeric_limits<T>::max()
0078 {
0079     static std::random_device dev;
0080     static std::mt19937 rng(dev());
0081     /*
0082       using dist_t = std::conditional<std::is_floating_point_v<T>,
0083                                       std::uniform_real_distribution<T>,
0084                                       std::uniform_int_distribution<T>>;*/
0085     T res;
0086     if constexpr(std::is_integral<T>::value)
0087     {
0088         std::uniform_int_distribution<T> dist(min, max);
0089         res= dist(rng);
0090     }
0091     else if(std::is_floating_point<T>::value)
0092     {
0093         std::uniform_real_distribution<T> dist(min, max);
0094         res= dist(rng);
0095     }
0096     else
0097     {
0098         res= T{}; // useless
0099     }
0100     return res;
0101 }
0102 HELPER_EXPORT const std::map<QString, QVariant> buildRectController(bool filled, const QRectF& rect,
0103                                                                     const QPointF& pos= QPointF(0, 0));
0104 
0105 HELPER_EXPORT const std::map<QString, QVariant>
0106 buildTextController(bool border, const QString& text, const QRectF& rect, const QPointF& pos= QPointF(0, 0));
0107 
0108 HELPER_EXPORT const std::map<QString, QVariant> buildEllipseController(bool filled, qreal rx, qreal ry,
0109                                                                        const QPointF& pos= QPointF(0, 0));
0110 
0111 HELPER_EXPORT const std::map<QString, QVariant> buildImageController(const QString& path, const QRectF& rect,
0112                                                                      const QPointF& pos= QPointF(0, 0));
0113 
0114 HELPER_EXPORT const std::map<QString, QVariant> buildPenController(bool filled, const std::vector<QPointF>& points,
0115                                                                    const QPointF& pos);
0116 
0117 HELPER_EXPORT const std::map<QString, QVariant> buildPathController(bool filled, const std::vector<QPointF>& points,
0118                                                                     const QPointF& pos= QPointF(0, 0));
0119 HELPER_EXPORT const std::map<QString, QVariant> buildLineController(const QPointF& p1, const QPointF& p2,
0120                                                                     const QPointF& pos= QPointF(0, 0));
0121 HELPER_EXPORT const std::map<QString, QVariant> buildTokenController(bool isNpc, const QPointF& pos);
0122 
0123 QString randomString(int length= 10)
0124 {
0125     QString res;
0126     static QString list{"abcdefghijklmnropqrstuvwxyzABCDEFGHIJKLMOPQRSTUVWXYZ1234567890"};
0127 
0128     for(int i= 0; i < length; ++i)
0129     {
0130         res.append(list[generate<int>(0, list.size() - 1)]);
0131     }
0132 
0133     return res;
0134 }
0135 
0136 HELPER_EXPORT QString imagePath(bool isSquare= false);
0137 HELPER_EXPORT QUrl imagePathUrl(bool isSquare = false);
0138 HELPER_EXPORT QByteArray imageData(bool isSquare= false);
0139 HELPER_EXPORT QByteArray randomData(int length= 1000);
0140 HELPER_EXPORT QColor randomColor();
0141 HELPER_EXPORT QPointF randomPoint();
0142 HELPER_EXPORT QRectF randomRect();
0143 HELPER_EXPORT QObject* initWebServer(int port= 9090);
0144 HELPER_EXPORT QUrl randomUrl();
0145 HELPER_EXPORT QMovie randomMovie();
0146 
0147 template <typename T>
0148 T randomFromList(const std::vector<T>& list)
0149 {
0150     return list[generate<int>(0, list.size() - 1)];
0151 }
0152 } // namespace Helper