vutils.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef VUTILS_H
  2. #define VUTILS_H
  3. #include <QString>
  4. #include <QColor>
  5. #include <QVector>
  6. #include <QPair>
  7. #include <QMessageBox>
  8. #include <QUrl>
  9. #include "vconfigmanager.h"
  10. #include "vconstants.h"
  11. class QKeyEvent;
  12. class VFile;
  13. #if !defined(V_ASSERT)
  14. #define V_ASSERT(cond) ((!(cond)) ? qt_assert(#cond, __FILE__, __LINE__) : qt_noop())
  15. #endif
  16. enum class MessageBoxType
  17. {
  18. Normal = 0,
  19. Danger = 1
  20. };
  21. struct ImageLink
  22. {
  23. enum ImageLinkType
  24. {
  25. LocalRelativeInternal = 0x1,
  26. LocalRelativeExternal = 0x2,
  27. LocalAbsolute = 0x4,
  28. Resource = 0x8,
  29. Remote = 0x10,
  30. All = 0xffff
  31. };
  32. QString m_path;
  33. ImageLinkType m_type;
  34. };
  35. class VUtils
  36. {
  37. public:
  38. VUtils();
  39. static QString readFileFromDisk(const QString &filePath);
  40. static bool writeFileToDisk(const QString &filePath, const QString &text);
  41. // Transform FFFFFF string to QRgb
  42. static QRgb QRgbFromString(const QString &str);
  43. static QString generateImageFileName(const QString &path, const QString &title,
  44. const QString &format = "png");
  45. static QString generateCopiedFileName(const QString &p_dirPath, const QString &p_fileName);
  46. static QString generateCopiedDirName(const QString &p_parentDirPath, const QString &p_dirName);
  47. static void processStyle(QString &style, const QVector<QPair<QString, QString> > &varMap);
  48. static bool isMarkdown(const QString &p_fileName);
  49. // Return the last directory name of @p_path.
  50. static inline QString directoryNameFromPath(const QString& p_path);
  51. // Return the file name of @p_path.
  52. // /home/tamlok/abc, /home/tamlok/abc/ will both return abc.
  53. static QString fileNameFromPath(const QString &p_path);
  54. // Return the base path of @p_path.
  55. // /home/tamlok/abc, /home/tamlok/abc/ will both return /home/tamlok.
  56. static QString basePathFromPath(const QString &p_path);
  57. // Fetch all the image links (including those in code blocks) in markdown file p_file.
  58. // @p_type to filter the links returned.
  59. // Need to open p_file and will close it if it is originally closed.
  60. static QVector<ImageLink> fetchImagesFromMarkdownFile(VFile *p_file,
  61. ImageLink::ImageLinkType p_type = ImageLink::All);
  62. // Create directories along the @p_path.
  63. // @p_path could be /home/tamlok/abc, /home/tamlok/abc/.
  64. static bool makePath(const QString &p_path);
  65. static ClipboardOpType opTypeInClipboard();
  66. static bool copyFile(const QString &p_srcFilePath, const QString &p_destFilePath, bool p_isCut);
  67. static bool copyDirectory(const QString &p_srcDirPath, const QString &p_destDirPath, bool p_isCut);
  68. static int showMessage(QMessageBox::Icon p_icon, const QString &p_title, const QString &p_text,
  69. const QString &p_infoText, QMessageBox::StandardButtons p_buttons,
  70. QMessageBox::StandardButton p_defaultBtn, QWidget *p_parent,
  71. MessageBoxType p_type = MessageBoxType::Normal);
  72. static const QVector<QPair<QString, QString> > &getAvailableLanguages();
  73. static bool isValidLanguage(const QString &p_lang);
  74. static bool isImageURL(const QUrl &p_url);
  75. static bool isImageURLText(const QString &p_url);
  76. static qreal calculateScaleFactor();
  77. static bool realEqual(qreal p_a, qreal p_b);
  78. static QChar keyToChar(int p_key);
  79. static QString getLocale();
  80. // Regular expression for image link.
  81. // ![image title]( http://github.com/tamlok/vnote.jpg "alt \" text" )
  82. // Captured texts (need to be trimmed):
  83. // 1. Image Alt Text (Title);
  84. // 2. Image URL;
  85. // 3. Image Optional Title with double quotes;
  86. // 4. Unused;
  87. static const QString c_imageLinkRegExp;
  88. private:
  89. // <value, name>
  90. static const QVector<QPair<QString, QString>> c_availableLanguages;
  91. };
  92. inline QString VUtils::directoryNameFromPath(const QString &p_path)
  93. {
  94. return fileNameFromPath(p_path);
  95. }
  96. #endif // VUTILS_H