/src/share/WizCommonUI.cpp

https://github.com/altairwei/WizNotePlus · C++ · 181 lines · 136 code · 22 blank · 23 comment · 20 complexity · e509c314e6f69a549e733270c0b93a21 MD5 · raw file

  1. #include "WizCommonUI.h"
  2. #include <QClipboard>
  3. #include <QApplication>
  4. #include <QUuid>
  5. #include <QDir>
  6. #include <QDebug>
  7. #include <QByteArray>
  8. #include <QSaveFile>
  9. #include <QDesktopServices>
  10. #include <QUrl>
  11. #include <QFileDialog>
  12. #include "WizMisc.h"
  13. #include "utils/WizPathResolve.h"
  14. #include "share/WizMisc.h"
  15. #include "share/WizSettings.h"
  16. WizCommonUI::WizCommonUI(QObject* parent)
  17. : QObject(parent)
  18. {
  19. }
  20. QString WizCommonUI::loadTextFromFile(const QString& strFileName)
  21. {
  22. QString strText;
  23. ::WizLoadUnicodeTextFromFile(strFileName, strText);
  24. return strText;
  25. }
  26. bool WizCommonUI::saveTextToFile(const QString &strFileName, const QString &strText, const QString &strCharset)
  27. {
  28. QString charset = strCharset.toLower();
  29. if (charset == "unicode" || charset == "utf-8") {
  30. return ::WizSaveUnicodeTextToUtf8File(strFileName, strText, false);
  31. } else if (charset == "utf-16") {
  32. return ::WizSaveUnicodeTextToUtf16File(strFileName, strText);
  33. } else {
  34. return ::WizSaveUnicodeTextToUtf8File(strFileName, strText);
  35. }
  36. }
  37. QString WizCommonUI::clipboardToImage(int hwnd, const QString& strOptions)
  38. {
  39. Q_UNUSED(hwnd);
  40. //
  41. QClipboard* clipboard = QApplication::clipboard();
  42. if (!clipboard)
  43. return CString();
  44. //
  45. //
  46. QImage image = clipboard->image();
  47. if (image.isNull())
  48. return CString();
  49. //
  50. CString strTempPath = ::WizGetCommandLineValue(strOptions, "TempPath");
  51. if (strTempPath.isEmpty())
  52. {
  53. strTempPath = Utils::WizPathResolve::tempPath();
  54. }
  55. else
  56. {
  57. ::WizPathAddBackslash(strTempPath);
  58. ::WizEnsurePathExists(strTempPath);
  59. }
  60. //
  61. CString strFileName = strTempPath + WizIntToStr(WizGetTickCount()) + ".png";
  62. if (!image.save(strFileName))
  63. return CString();
  64. //
  65. return strFileName;
  66. }
  67. QString WizCommonUI::LoadTextFromFile(const QString& strFileName)
  68. {
  69. return loadTextFromFile(strFileName);
  70. }
  71. bool WizCommonUI::SaveTextToFile(const QString &strFileName, const QString &strText, const QString &strCharset)
  72. {
  73. return saveTextToFile(strFileName, strText, strCharset);
  74. }
  75. QString WizCommonUI::ClipboardToImage(const QString& strOptions)
  76. {
  77. return clipboardToImage(0, strOptions);
  78. }
  79. QString WizCommonUI::GetSpecialFolder(const QString &bstrFolderName)
  80. {
  81. if (bstrFolderName == "TemporaryFolder") {
  82. return Utils::WizPathResolve::tempPath();
  83. } else if (bstrFolderName == "AppPath") {
  84. return Utils::WizPathResolve::appPath();
  85. } else {
  86. return "";
  87. }
  88. }
  89. QString WizCommonUI::GetATempFileName(const QString &bstrFileExt)
  90. {
  91. QString strTempFileName = QUuid::createUuid().toString() + bstrFileExt;
  92. return QDir(GetSpecialFolder("TemporaryFolder")).absoluteFilePath(strTempFileName);
  93. }
  94. /**
  95. * @brief Creates the directory path dirPath.
  96. *
  97. * The function will create all parent directories necessary to create the directory.
  98. *
  99. * @param bstrPath
  100. * @return true
  101. * @return false
  102. */
  103. bool WizCommonUI::CreateDirectory(const QString &bstrPath)
  104. {
  105. return QDir().mkpath(bstrPath);
  106. }
  107. /**
  108. * @brief Download resource to file.
  109. *
  110. * @param bstrURL
  111. * @param bstrFileName
  112. * @return true
  113. * @return false
  114. */
  115. bool WizCommonUI::URLDownloadToFile(const QString &bstrURL, const QString &bstrFileName, bool isImage)
  116. {
  117. return WizURLDownloadToFile(bstrURL, bstrFileName, isImage);
  118. }
  119. bool WizCommonUI::Base64ToFile(const QString &base64, const QString &fileName)
  120. {
  121. QByteArray buffer = QByteArray::fromBase64(base64.toUtf8());
  122. QSaveFile file(fileName);
  123. file.open(QIODevice::WriteOnly);
  124. file.write(buffer);
  125. file.commit();
  126. return true;
  127. }
  128. void WizCommonUI::OpenUrl(const QString &url)
  129. {
  130. QDesktopServices::openUrl(QUrl(url));
  131. }
  132. QString WizCommonUI::SelectWindowsFile(bool isOpen, const QString &filter)
  133. {
  134. if (isOpen) {
  135. return QFileDialog::getOpenFileName(
  136. nullptr, tr("Select File"), QDir::home().absolutePath(), filter);
  137. } else {
  138. return QFileDialog::getSaveFileName(
  139. nullptr, tr("Select File"), QDir::home().absolutePath(), filter);
  140. }
  141. }
  142. bool WizCommonUI::PathFileExists(const QString &path)
  143. {
  144. return WizPathFileExists(path);
  145. }
  146. void WizCommonUI::CopyFile(const QString &existingFile, const QString &newFileName)
  147. {
  148. QFile::copy(existingFile, newFileName);
  149. }
  150. QString WizCommonUI::GetValueFromIni(const QString &fileName, const QString &section, const QString &key)
  151. {
  152. WizSettings setting(fileName);
  153. return setting.getString(section, key);
  154. }
  155. void WizCommonUI::SetValueToIni(const QString &fileName, const QString &section, const QString &key, const QString &value)
  156. {
  157. WizSettings setting(fileName);
  158. setting.setString(section, key, value);
  159. }