vutils.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "vutils.h"
  2. #include <QFile>
  3. #include <QDebug>
  4. VUtils::VUtils()
  5. {
  6. }
  7. QString VUtils::readFileFromDisk(const QString &filePath)
  8. {
  9. QFile file(filePath);
  10. if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
  11. qWarning() << "error: fail to read file" << filePath;
  12. return QString();
  13. }
  14. QString fileText(file.readAll());
  15. file.close();
  16. qDebug() << "read file content:" << filePath;
  17. return fileText;
  18. }
  19. bool VUtils::writeFileToDisk(const QString &filePath, const QString &text)
  20. {
  21. QFile file(filePath);
  22. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  23. qWarning() << "error: fail to open file" << filePath << "to write to";
  24. return false;
  25. }
  26. QTextStream stream(&file);
  27. stream << text;
  28. file.close();
  29. qDebug() << "write file content:" << filePath;
  30. return true;
  31. }
  32. QRgb VUtils::QRgbFromString(const QString &str)
  33. {
  34. Q_ASSERT(str.length() == 6);
  35. QString rStr = str.left(2);
  36. QString gStr = str.mid(2, 2);
  37. QString bStr = str.right(2);
  38. qDebug() << rStr << gStr << bStr;
  39. bool ok, ret = true;
  40. int red = rStr.toInt(&ok, 16);
  41. ret = ret && ok;
  42. int green = gStr.toInt(&ok, 16);
  43. ret = ret && ok;
  44. int blue = bStr.toInt(&ok, 16);
  45. ret = ret && ok;
  46. if (ret) {
  47. return qRgb(red, green, blue);
  48. }
  49. qWarning() << "error: fail to construct QRgb from string" << str;
  50. return QRgb();
  51. }