/****************************************************************************** Copyright(C):2015~2018 hzleaper FileName:$FILE_BASE$.$FILE_EXT$ Author:zhikun wu Email:zk.wu@hzleaper.com Tools:vs2010 pc on company Created:$DATE$ History:$DAY$:$MONTH$:$YEAR$ $HOUR$:$MINUTE$ *******************************************************************************/ #ifndef __Q_TP_FUNCTIONS_H #define __Q_TP_FUNCTIONS_H //#include "baseStruct.h" #include "tpGuiHeader.h" #include #include #include #include #include #include #include #include #include #include #include #include inline void TpRectMaxRB(const QRect& rect, QRect& rtMax) { if (rect.right() > rtMax.right()) { rtMax.setRight(rect.right()); } if (rect.bottom() > rtMax.bottom()) { rtMax.setBottom(rect.bottom()); } } inline QString TpExeFileToPath(QString& strExe) { QFileInfo fph(strExe); return fph.absolutePath() + QString("/"); } inline QString TpExeFileName(QString& strExe) { QFileInfo fph(strExe); if (!fph.exists()) { qWarning() << "Invalid file path : " << strExe; return QString(""); } return fph.baseName(); } inline bool TpJsonArray2Point(const QJsonArray& jsonArray, QPoint& point) { if (jsonArray.isEmpty() || jsonArray.size() < 2) { return false; } point.setX(jsonArray[0].toInt()); point.setY(jsonArray[1].toInt()); return true; } inline bool TpJsonArray2Rect(const QJsonArray& jsonArray, QRect& rect) { if (jsonArray.isEmpty() || jsonArray.size() < 4) { return false; } rect.setX(jsonArray[0].toInt()); rect.setY(jsonArray[1].toInt()); rect.setWidth(jsonArray[2].toInt()); rect.setHeight(jsonArray[3].toInt()); return true; } inline bool TpJsonArray2RectF(const QJsonArray& jsonArray, QRectF& rectF) { if (jsonArray.isEmpty() || jsonArray.size() < 4) { return false; } rectF.setX(jsonArray[0].toDouble()); rectF.setY(jsonArray[1].toDouble()); rectF.setWidth(jsonArray[2].toDouble()); rectF.setHeight(jsonArray[3].toDouble()); return true; } inline QVector TpSplitSize(int nTotals, int splits) { QVector vt(splits); int blkSize = nTotals / splits; int blkRem = nTotals % splits; int blks = 0; for (int i = 0; i < splits; ++i) { blks += blkSize; if (blkRem > 0) { blks += 1; --blkRem; } vt[i] = blks; } return vt; } inline QVector TpSplitFixedSize(int nTotals, int size, int d) { QVector vt; if (size <= 0) { size = nTotals; } int nBlk = size; if (d > 0) { nBlk = d; } else { nBlk += d; } while (nBlk < nTotals) { vt.append(nBlk); nBlk += size; } vt.append(nTotals); return vt; } #define __SPECIAL_EXE_NAME "leaper.exe" #define __SPECIAL_CODE "abcdeaxx&&88**xxdddr88**dfejjjf" inline bool TpExeCanStart(int argc, const char *argv[]) { QString sExe(argv[0]); if (sExe.endsWith(__SPECIAL_EXE_NAME, Qt::CaseInsensitive) || (argc >= 2 && 0 == strcmp(__SPECIAL_CODE, argv[1])) ) { return true; } return false; } #define __SPECIAL_FILE_FOR_UPDATED "update.leaper" inline bool TpIsUpdated(const QString& szMainPath, bool bCreate = true) { QFile file(szMainPath + __SPECIAL_FILE_FOR_UPDATED); if (file.exists()) { return false; } if (bCreate) { file.open(QFile::WriteOnly); } return true; } inline QObject* TpTopParent(QObject* p) { if (NULL != p) { while (NULL != p->parent()) { p = p->parent(); } } return p; } inline QWidget* TpTopWidget(QObject* p) { QWidget* pw = NULL; while (NULL != p) { if (p->isWidgetType()) { pw = qobject_cast(p); } p = p->parent(); } return pw; } inline void TpPathChecked(QString& szPath) { if (!szPath.endsWith("/") && !szPath.endsWith("\\")) { szPath.append("/"); } } inline QTpObjectList TpFindObjects(const QString& objName, QObject* parent) { QTpObjectList objList; if (NULL != parent) { objList.append(parent->findChildren(objName)); if (objName == parent->objectName()) { objList.append(parent); } } return objList; } inline bool TpWidgetLessThan(const QWidget* p1, const QWidget* p2) { return p1->objectName() < p2->objectName(); } #endif