From 476a17f1b089fedc53594a2e20d1b77a29d67ba2 Mon Sep 17 00:00:00 2001 From: hama Date: Fri, 24 Apr 2015 00:23:04 +0200 Subject: [PATCH] ReStateStorage --- gui/ReGuiValidator.cpp | 18 +---------- gui/ReGuiValidator.hpp | 3 -- gui/ReStateStorage.cpp | 68 +++++++++++++++++++++++++++++++++++++++--- gui/ReStateStorage.hpp | 17 ++++++++++- 4 files changed, 81 insertions(+), 25 deletions(-) diff --git a/gui/ReGuiValidator.cpp b/gui/ReGuiValidator.cpp index 9e9ebf0..d7a145f 100644 --- a/gui/ReGuiValidator.cpp +++ b/gui/ReGuiValidator.cpp @@ -16,7 +16,7 @@ * Constructor. */ ReGuiValidator::ReGuiValidator() : - m_statusLine(NULL), m_errors(0){ + m_errors(0){ } /** @@ -148,20 +148,4 @@ void ReGuiValidator::setInHistory(QComboBox* combo, const QString& value){ combo->removeItem(20); } } -/** - * Returns the status line. - * @return the status line - */ -QStatusBar* ReGuiValidator::statusLine() const{ - return m_statusLine; -} - -/** - * Sets the status line. - * - * @param statusLine the status line to set - */ -void ReGuiValidator::setStatusLine(QStatusBar* statusLine){ - m_statusLine = statusLine; -} diff --git a/gui/ReGuiValidator.hpp b/gui/ReGuiValidator.hpp index 4abdde1..59e6697 100644 --- a/gui/ReGuiValidator.hpp +++ b/gui/ReGuiValidator.hpp @@ -26,12 +26,9 @@ public: QString comboText(QComboBox* combo); virtual void guiError(QWidget* widget, const QString& message); void setInHistory(QComboBox* combo, const QString& value); - void setStatusLine(QStatusBar* statusLine); virtual void setStatusMessage(bool error, const QString& message) = 0; - QStatusBar* statusLine() const; protected: - QStatusBar* m_statusLine; int m_errors; }; diff --git a/gui/ReStateStorage.cpp b/gui/ReStateStorage.cpp index 95cc847..dde8322 100644 --- a/gui/ReStateStorage.cpp +++ b/gui/ReStateStorage.cpp @@ -8,20 +8,80 @@ * You also can use the license from http://www.wtfpl.net/. * The latest sources: https://github.com/republib */ - +#include "QIODevice" #include "base/rebase.hpp" #include "gui/regui.hpp" /** * Constructor. * - * @param file + * @param filename filename with path of the storage file */ -ReStateStorage::ReStateStorage(const QString& file){ - +ReStateStorage::ReStateStorage(const QString& filename) : + m_filename(filename), m_fp(NULL), m_stream(NULL), m_form(){ } +/** + * Destructor. + */ ReStateStorage::~ReStateStorage(){ + delete m_stream; + if (m_fp != NULL){ + fclose(m_fp); + m_fp = NULL; + } +} +/** + * Initializes the instance for writing the storage information. + */ +bool ReStateStorage::initForWrite(){ + if (m_fp == NULL) + m_fp = fopen(m_filename.toUtf8().constData(), "wb"); + if (m_fp != NULL) + m_stream = new QTextStream(m_fp, QIODevice::ReadOnly); + return m_stream != NULL; +} +/** + * Returns the name of the current form. + * + * @return the name of the current form + */ +QString ReStateStorage::form() const{ + return m_form; +} + +/** + * Sets the name of the current form. + * + * @param form the name of the current form + */ +void ReStateStorage::setForm(const QString& form){ + m_form = form; +} + +/** + * Stores the data of a combobox. + * + * @param combo the combobox to store + * @param name the name of the combobox + * @param withCurrentText true: the current text will be saved too + */ +void ReStateStorage::store(const QComboBox* combo, const QString& name, + bool withCurrentText){ + if (initForWrite()){ + QString fullname; + if (!m_form.isEmpty()) + fullname = m_form + "."; + fullname += name; + for (int ii = 0; ii < combo->count(); ii++){ + *m_stream << fullname << ".item:" << ii << "=" << combo->itemText(ii) + << endl; + } + if (withCurrentText){ + *m_stream << fullname << ".text=" << combo->currentText() << endl; + } + } + m_stream->flush(); } diff --git a/gui/ReStateStorage.hpp b/gui/ReStateStorage.hpp index 8a2b012..9f3837b 100644 --- a/gui/ReStateStorage.hpp +++ b/gui/ReStateStorage.hpp @@ -12,10 +12,25 @@ #ifndef GUI_RESTATESTORAGE_HPP_ #define GUI_RESTATESTORAGE_HPP_ +#include +#include + class ReStateStorage { public: - ReStateStorage(const QString& file); + ReStateStorage(const QString& filename); virtual ~ReStateStorage(); +public: + QString form() const; + bool initForWrite(); + void store(const QComboBox* combo, const QString& name, + bool withCurrentText = false); + void setForm(const QString& form); + +private: + QString m_filename; + FILE* m_fp; + QTextStream* m_stream; + QString m_form; }; #endif /* GUI_RESTATESTORAGE_HPP_ */ -- 2.39.5