Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Features/snippets.h"
- #include <iostream>
- Snippets::Snippets(QObject* parent)
- : QObject(parent)
- , m_Snippets(nullptr)
- {
- bool success;
- LoadSnippets(success);
- }
- void Snippets::LoadSnippets(bool& success)
- {
- success = false;
- QFile file(SNIPPETS_FILE);
- QMap<QString, QString> data = QMap<QString, QString>();
- if (file.open(QIODevice::ReadOnly)) {
- QDataStream in(&file);
- in >> data;
- file.close();
- }
- if (data.isEmpty()) {
- data.insert(tr("API Help"), tr(
- "# Full APIn"
- "# ---------------------------n"
- "# get method's have no parameters and others have onen"
- "#n"
- "# get_input - get input textbox's textn"
- "# set_input - set input textbox's textn"
- "# get_output - get output textbox's textn"
- "# set_output - get output textbox's textn"
- "# get_code - get code textbox's textn"
- "# set_code - set code textbox's textn"
- "# write_output- append to output boxn"
- "# get_apppath - get exe pathnn"
- "# API Help/Code Samplen"
- "# ---------------------------n"
- "n"
- "# get text from input boxn"
- "# parameters - nonen"
- "txt = get_input()n"
- "n"
- "# change output box's textn"
- "# parameters - stringn"
- "set_output("")n"
- "n"
- "# append to output boxn"
- "# does not add a new linen"
- "# parameters - stringn"
- "write_output("Hi You,\n")n"
- "n"
- "# get_apppath() -> get exe pathn"
- "print ("PyRun.exe is at :", get_apppath())nn"));
- }
- m_Snippets = new QMap<QString, QString>(data);
- success = true;
- }
- void Snippets::SaveSnippets(bool& success)
- {
- success = false;
- if (m_Snippets != nullptr) {
- QFile file(SNIPPETS_FILE);
- if (!file.open(QIODevice::WriteOnly)) {
- return;
- }
- QDataStream out(&file);
- out << *m_Snippets;
- file.close();
- success = true;
- }
- }
- void Snippets::AddSnippet(const QString& name, const QString& code, bool& success)
- {
- success = false;
- if (m_Snippets != nullptr) {
- success = true;
- m_Snippets->insert(name, code);
- }
- }
- void Snippets::RemoveSnippet(const QString& name, bool& success)
- {
- success = false;
- if (m_Snippets != nullptr && m_Snippets->contains(name)) {
- success = (m_Snippets->remove(name) > 0);
- }
- }
- QString Snippets::GetSnippet(const QString& name, bool& success)
- {
- success = false;
- if (m_Snippets != nullptr && m_Snippets->contains(name)) {
- success = true;
- return m_Snippets->value(name);
- }
- return QString();
- }
- QList<QString> Snippets::GetKeys(bool& success)
- {
- success = false;
- if (m_Snippets != nullptr) {
- success = true;
- return m_Snippets->keys();
- }
- return QList<QString>();
- }
- Snippets::~Snippets()
- {
- if (m_Snippets != nullptr) {
- bool success;
- SaveSnippets(success); // save on the destructor
- if (!success) {
- std::cerr << "Writing Snippets to Database on save failed" << std::endl;
- }
- delete m_Snippets;
- }
- }
- #ifndef SNIPPETS_H
- #define SNIPPETS_H
- #include <QObject>
- #include <QMap>
- #include <QList>
- #include <QIODevice>
- #include <QFile>
- #include <QApplication>
- #define SNIPPETS_FILE QApplication::applicationDirPath() + "/snippets.dat"
- class Snippets : public QObject {
- Q_OBJECT
- public:
- explicit Snippets(QObject* parent = 0);
- QString GetSnippet(const QString& name, bool& success);
- void RemoveSnippet(const QString& name, bool& success);
- void AddSnippet(const QString& name, const QString& code, bool& success);
- void SaveSnippets(bool& success);
- void LoadSnippets(bool& success);
- ~Snippets();
- QList<QString> GetKeys(bool& success);
- signals:
- public slots:
- private:
- QMap<QString, QString>* m_Snippets;
- };
- #endif // SNIPPETS_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement