mirror of
https://github.com/redis/RedisDesktopManager.git
synced 2026-10-29 03:27:04 +08:00
Fixed issue #343
This commit is contained in:
@@ -8,10 +8,14 @@ public:
|
||||
|
||||
virtual ~AbstractFormatter() {};
|
||||
|
||||
virtual void setRawValue(const QString&);
|
||||
virtual void setSource(const QString&);
|
||||
|
||||
virtual QString getFormatted() = 0;
|
||||
|
||||
virtual bool isValid() = 0;
|
||||
|
||||
virtual QString getRaw() = 0;
|
||||
|
||||
enum FormatterType
|
||||
{
|
||||
Plain = 0, Json = 1, PHPSerializer = 2
|
||||
|
||||
@@ -1,9 +1,22 @@
|
||||
#pragma once
|
||||
#include <QJsonDocument>
|
||||
#include "AbstractFormatter.h"
|
||||
|
||||
class JsonFormatter : public AbstractFormatter
|
||||
{
|
||||
public:
|
||||
JsonFormatter();
|
||||
|
||||
void setSource(const QString&);
|
||||
|
||||
QString getFormatted();
|
||||
|
||||
bool isValid();
|
||||
|
||||
QString getRaw();
|
||||
|
||||
protected:
|
||||
bool valid;
|
||||
QJsonDocument document;
|
||||
};
|
||||
|
||||
|
||||
@@ -12,6 +12,10 @@ public:
|
||||
|
||||
virtual QString getFormatted();
|
||||
|
||||
bool isValid();
|
||||
|
||||
QString getRaw();
|
||||
|
||||
private:
|
||||
|
||||
enum ValueType {
|
||||
|
||||
@@ -6,5 +6,9 @@ class PlainFormatter : public AbstractFormatter
|
||||
{
|
||||
public:
|
||||
QString getFormatted();
|
||||
|
||||
bool isValid();
|
||||
|
||||
QString getRaw();
|
||||
};
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include "PHPSerializeFormatter.h"
|
||||
#include "JsonFormatter.h"
|
||||
|
||||
void AbstractFormatter::setRawValue(const QString&r)
|
||||
void AbstractFormatter::setSource(const QString&r)
|
||||
{
|
||||
rawValue = r;
|
||||
}
|
||||
|
||||
@@ -1,16 +1,43 @@
|
||||
#include "JsonFormatter.h"
|
||||
|
||||
#include <QScopedPointer>
|
||||
#include <QJsonDocument>
|
||||
|
||||
JsonFormatter::JsonFormatter()
|
||||
: valid(false)
|
||||
{
|
||||
}
|
||||
|
||||
void JsonFormatter::setSource(const QString &src)
|
||||
{
|
||||
rawValue = src;
|
||||
|
||||
QScopedPointer<QJsonParseError> parsingError(new QJsonParseError);
|
||||
document = QJsonDocument::fromJson(rawValue.toStdString().c_str(), parsingError.data());
|
||||
valid = (parsingError->error == QJsonParseError::NoError);
|
||||
}
|
||||
|
||||
QString JsonFormatter::getFormatted()
|
||||
{
|
||||
QJsonParseError * parsingError = new QJsonParseError;
|
||||
QJsonDocument document = QJsonDocument::fromJson(rawValue.toStdString().c_str(), parsingError);
|
||||
|
||||
if (parsingError->error != QJsonParseError::NoError)
|
||||
if (!isValid())
|
||||
{
|
||||
return QString("Invalid JSON");
|
||||
}
|
||||
|
||||
return QString(document.toJson(QJsonDocument::Indented));
|
||||
}
|
||||
|
||||
bool JsonFormatter::isValid()
|
||||
{
|
||||
return valid;
|
||||
}
|
||||
|
||||
QString JsonFormatter::getRaw()
|
||||
{
|
||||
if (!isValid())
|
||||
{
|
||||
return QString("Invalid JSON");
|
||||
}
|
||||
|
||||
return QString(document.toJson(QJsonDocument::Compact));
|
||||
}
|
||||
@@ -3,6 +3,16 @@
|
||||
#include <QStringList>
|
||||
#include <QDebug>
|
||||
|
||||
bool PHPSerializeFormatter::isValid()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
QString PHPSerializeFormatter::getRaw()
|
||||
{
|
||||
return rawValue;
|
||||
}
|
||||
|
||||
PHPSerializeFormatter::PHPSerializeFormatter()
|
||||
{
|
||||
arrayItemWithValueRegex.setPatternSyntax(QRegExp::RegExp2);
|
||||
|
||||
@@ -6,3 +6,12 @@ QString PlainFormatter::getFormatted()
|
||||
return rawValue;
|
||||
}
|
||||
|
||||
bool PlainFormatter::isValid()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
QString PlainFormatter::getRaw()
|
||||
{
|
||||
return rawValue;
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "StringKeyModel.h"
|
||||
#include "PaginatedModel.h"
|
||||
#include "AbstractFormatter.h"
|
||||
#include <QMessageBox>
|
||||
|
||||
ValueTabView::ValueTabView(const QString& name, QWidget * parent)
|
||||
: controller(parent), formatter(AbstractFormatter::getFormatter()),
|
||||
@@ -246,7 +247,7 @@ void ValueTabView::onSelectedItemChanged(const QModelIndex & current, const QMod
|
||||
|
||||
singleValue->clear();
|
||||
|
||||
formatter->setRawValue(model->itemFromIndex(current)->text());
|
||||
formatter->setSource(model->itemFromIndex(current)->text());
|
||||
|
||||
singleValue->appendPlainText(formatter->getFormatted());
|
||||
|
||||
@@ -263,7 +264,7 @@ void ValueTabView::currentFormatterChanged(int index)
|
||||
|
||||
if (model->getKeyModelType() == StringKeyModel::KEY_MODEL_TYPE) {
|
||||
singleValue->clear();
|
||||
formatter->setRawValue(((StringKeyModel*)model)->getValue());
|
||||
formatter->setSource(((StringKeyModel*)model)->getValue());
|
||||
singleValue->appendPlainText(formatter->getFormatted());
|
||||
} else if (currentCell != nullptr) {
|
||||
onSelectedItemChanged(*currentCell, *currentCell);
|
||||
@@ -275,7 +276,16 @@ void ValueTabView::valueUpdate()
|
||||
if (currentCell == nullptr && model->getKeyModelType() != StringKeyModel::KEY_MODEL_TYPE)
|
||||
return;
|
||||
|
||||
emit saveChangedValue(singleValue->toPlainText(), currentCell);
|
||||
QString value = singleValue->toPlainText();
|
||||
|
||||
formatter->setSource(value);
|
||||
|
||||
if (!formatter->isValid()) {
|
||||
QMessageBox::warning(controller, "Invalid value", "Check value format");
|
||||
return;
|
||||
}
|
||||
|
||||
emit saveChangedValue(formatter->getRaw(), currentCell);
|
||||
}
|
||||
|
||||
const QModelIndex * ValueTabView::getCurrentCell()
|
||||
|
||||
Reference in New Issue
Block a user