Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. #include "Features/snippets.h"
  2. #include <iostream>
  3.  
  4. Snippets::Snippets(QObject* parent)
  5. : QObject(parent)
  6. , m_Snippets(nullptr)
  7. {
  8. bool success;
  9. LoadSnippets(success);
  10. }
  11.  
  12. void Snippets::LoadSnippets(bool& success)
  13. {
  14. success = false;
  15. QFile file(SNIPPETS_FILE);
  16.  
  17. QMap<QString, QString> data = QMap<QString, QString>();
  18.  
  19. if (file.open(QIODevice::ReadOnly)) {
  20. QDataStream in(&file);
  21. in >> data;
  22. file.close();
  23. }
  24.  
  25. if (data.isEmpty()) {
  26. data.insert(tr("API Help"), tr(
  27. "# Full APIn"
  28. "# ---------------------------n"
  29. "# get method's have no parameters and others have onen"
  30. "#n"
  31. "# get_input - get input textbox's textn"
  32. "# set_input - set input textbox's textn"
  33. "# get_output - get output textbox's textn"
  34. "# set_output - get output textbox's textn"
  35. "# get_code - get code textbox's textn"
  36. "# set_code - set code textbox's textn"
  37. "# write_output- append to output boxn"
  38. "# get_apppath - get exe pathnn"
  39. "# API Help/Code Samplen"
  40. "# ---------------------------n"
  41. "n"
  42. "# get text from input boxn"
  43. "# parameters - nonen"
  44. "txt = get_input()n"
  45. "n"
  46. "# change output box's textn"
  47. "# parameters - stringn"
  48. "set_output("")n"
  49. "n"
  50. "# append to output boxn"
  51. "# does not add a new linen"
  52. "# parameters - stringn"
  53. "write_output("Hi You,\n")n"
  54. "n"
  55. "# get_apppath() -> get exe pathn"
  56. "print ("PyRun.exe is at :", get_apppath())nn"));
  57. }
  58.  
  59. m_Snippets = new QMap<QString, QString>(data);
  60.  
  61. success = true;
  62. }
  63.  
  64. void Snippets::SaveSnippets(bool& success)
  65. {
  66. success = false;
  67. if (m_Snippets != nullptr) {
  68.  
  69. QFile file(SNIPPETS_FILE);
  70. if (!file.open(QIODevice::WriteOnly)) {
  71. return;
  72. }
  73. QDataStream out(&file);
  74. out << *m_Snippets;
  75. file.close();
  76. success = true;
  77. }
  78. }
  79.  
  80. void Snippets::AddSnippet(const QString& name, const QString& code, bool& success)
  81. {
  82. success = false;
  83. if (m_Snippets != nullptr) {
  84. success = true;
  85. m_Snippets->insert(name, code);
  86. }
  87. }
  88.  
  89. void Snippets::RemoveSnippet(const QString& name, bool& success)
  90. {
  91. success = false;
  92. if (m_Snippets != nullptr && m_Snippets->contains(name)) {
  93. success = (m_Snippets->remove(name) > 0);
  94. }
  95. }
  96.  
  97. QString Snippets::GetSnippet(const QString& name, bool& success)
  98. {
  99. success = false;
  100. if (m_Snippets != nullptr && m_Snippets->contains(name)) {
  101. success = true;
  102. return m_Snippets->value(name);
  103. }
  104. return QString();
  105. }
  106.  
  107. QList<QString> Snippets::GetKeys(bool& success)
  108. {
  109. success = false;
  110. if (m_Snippets != nullptr) {
  111. success = true;
  112. return m_Snippets->keys();
  113. }
  114. return QList<QString>();
  115. }
  116.  
  117. Snippets::~Snippets()
  118. {
  119. if (m_Snippets != nullptr) {
  120. bool success;
  121. SaveSnippets(success); // save on the destructor
  122. if (!success) {
  123. std::cerr << "Writing Snippets to Database on save failed" << std::endl;
  124. }
  125. delete m_Snippets;
  126. }
  127. }
  128.  
  129. #ifndef SNIPPETS_H
  130. #define SNIPPETS_H
  131.  
  132. #include <QObject>
  133. #include <QMap>
  134. #include <QList>
  135. #include <QIODevice>
  136. #include <QFile>
  137. #include <QApplication>
  138.  
  139. #define SNIPPETS_FILE QApplication::applicationDirPath() + "/snippets.dat"
  140.  
  141. class Snippets : public QObject {
  142. Q_OBJECT
  143. public:
  144. explicit Snippets(QObject* parent = 0);
  145.  
  146. QString GetSnippet(const QString& name, bool& success);
  147. void RemoveSnippet(const QString& name, bool& success);
  148. void AddSnippet(const QString& name, const QString& code, bool& success);
  149. void SaveSnippets(bool& success);
  150. void LoadSnippets(bool& success);
  151. ~Snippets();
  152. QList<QString> GetKeys(bool& success);
  153. signals:
  154.  
  155. public slots:
  156.  
  157. private:
  158. QMap<QString, QString>* m_Snippets;
  159. };
  160.  
  161. #endif // SNIPPETS_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement