/** * @file GlobalUtility.cpp * @brief 基础工具集合。包含数值字符串之间的转换, * 父对象子对象的查找,环境变量的获取和磁盘空间 * 的获取等功能。 * @date 2014-11-07 */ #include #include //#include // #include "Utils.h" #include "GlobalUtility.h" namespace pai { namespace gui { int Percentage(const qreal value) { return qRound(value * 100); } QString PercentageFormat(const qreal value) { return QString("%1").arg(Percentage(value)) + "%"; } QRect MapToGlobal(QWidget* pWidget, const QRect & localRect) { return QRect(pWidget->mapToGlobal(localRect.topLeft()), localRect.size()); } QRect MapFromGlobal(QWidget *pWidget, const QRect & globalRect) { return QRect(pWidget->mapFromGlobal(globalRect.topLeft()), globalRect.size()); } QString GetPaiTmpHomePath(const QString & tempPath) { QString TempPath = tempPath; QString CompName = "/.wisenergy"; if(!tempPath.startsWith("/")) { CompName += "/"; } if(!tempPath.endsWith("/")) { TempPath += "/"; } QString PaiTmpHomePath = QDir::homePath() + CompName + TempPath; QDir directory(PaiTmpHomePath); if(!directory.exists()) { directory.mkpath(PaiTmpHomePath); } return PaiTmpHomePath; } long GetDirFreeSpace(const char *pPath) { //TODO 跨平台 //struct statfs diskInfo; //statfs(pPath, &diskInfo); //return diskInfo.f_bavail * diskInfo.f_bsize; return 0; } long StringToDigitSize(QString quota) { double size = 0; if(quota.isEmpty() || (quota == ErrorTag)) { size = -1; } else { QString quotaValue = quota.section(' ', 0, 0); QString unit = quota.section(' ', 1, 1); if(unit.isEmpty()) { size = quotaValue.toDouble(); } else { if(unit == "TB") { size = quotaValue.toDouble() * TB_BYTE_COUNT; } else if(unit == "GB") { size = quotaValue.toDouble() * GB_BYTE_COUNT; } else if(unit == "MB") { size = quotaValue.toDouble() * MB_BYTE_COUNT; } else if(unit == "KB") { size = quotaValue.toDouble() * KB_BYTE_COUNT; } else { size = quotaValue.toDouble(); } } } return static_cast< long > (size); } // QString DigitSizeToString(long size, bool moreSpace) // { // if(size < 0) // { // return ErrorTag; // } // else // { // QString tmp = QString::fromStdString(pai::utils::CUtils::DigitSizeToString((long long) size)); // if(StringToDigitSize(tmp) == size) // { // return tmp; // } // else // { // bool ok; // // 将结果分成 整数,小数和单位三部分 // int integerPart = tmp.mid(0, tmp.indexOf('.')).toInt(&ok); // int decimalPart = tmp.at(tmp.indexOf('.') + 1).digitValue(); // QString unit = tmp.section(' ', 1, 1); // Q_ASSERT(ok && (decimalPart != -1)); // // 如果需要向上取值,且当转换比原值小时,增加转换值 // if((StringToDigitSize(tmp) < size) && (moreSpace)) // { // if(++decimalPart == 10) // { // decimalPart = 0; // integerPart++; // } // return QString("%1.%2 %3").arg(integerPart).arg(decimalPart).arg(unit); // } // // 如果需要向下取值,且当转换比原值大时,减小转换值 // else if((StringToDigitSize(tmp) > size) && (!moreSpace)) // { // if(--decimalPart == -1) // { // decimalPart = 9; // integerPart--; // } // return QString("%1.%2 %3").arg(integerPart).arg(decimalPart).arg(unit); // } // } // return tmp; // } // } bool CaseInsensitiveLessThan(const QString & leftString, const QString & rightString) { return leftString.toLower() < rightString.toLower(); } QString DoubleToQString(double value, int decimal) { QString strValue = QString::number(value, 'f', decimal); if(decimal > 0) { while(strValue.endsWith('0') && strValue.at(strValue.length() - 2) != '.') { strValue = strValue.remove(strValue.length() - 1, 1); } } else { strValue.append(".0"); } return strValue; } QString IntToQString(int value) { QString strValue = QString::number(value, 'f', 0); return strValue; } } }