mirror of
https://github.com/arcan1s/awesome-widgets.git
synced 2025-04-24 15:37:23 +00:00
add json formatter
This commit is contained in:
parent
8dfb6b609f
commit
004a97800c
@ -26,6 +26,7 @@
|
||||
#include "awdatetimeformatter.h"
|
||||
#include "awdebug.h"
|
||||
#include "awfloatformatter.h"
|
||||
#include "awjsonformatter.h"
|
||||
#include "awlistformatter.h"
|
||||
#include "awnoformatter.h"
|
||||
#include "awscriptformatter.h"
|
||||
@ -169,6 +170,8 @@ AWFormatterHelper::defineFormatterClass(const QString stringType) const
|
||||
formatter = AWAbstractFormatter::FormatterClass::Script;
|
||||
else if (stringType == QString("String"))
|
||||
formatter = AWAbstractFormatter::FormatterClass::String;
|
||||
else if (stringType == QString("Json"))
|
||||
formatter = AWAbstractFormatter::FormatterClass::Json;
|
||||
else
|
||||
qCWarning(LOG_AW) << "Unknown formatter" << stringType;
|
||||
|
||||
@ -216,6 +219,9 @@ void AWFormatterHelper::initFormatters()
|
||||
m_formattersClasses[name]
|
||||
= new AWStringFormatter(this, filePath);
|
||||
break;
|
||||
case AWAbstractFormatter::FormatterClass::Json:
|
||||
m_formattersClasses[name] = new AWJsonFormatter(this, filePath);
|
||||
break;
|
||||
case AWAbstractFormatter::FormatterClass::NoFormat:
|
||||
m_formattersClasses[name] = new AWNoFormatter(this, filePath);
|
||||
break;
|
||||
@ -296,7 +302,8 @@ void AWFormatterHelper::doCreateItem()
|
||||
QStringList selection = QStringList()
|
||||
<< QString("NoFormat") << QString("DateTime")
|
||||
<< QString("Float") << QString("List")
|
||||
<< QString("Script") << QString("String");
|
||||
<< QString("Script") << QString("String")
|
||||
<< QString("Json");
|
||||
bool ok;
|
||||
QString select = QInputDialog::getItem(
|
||||
this, i18n("Select type"), i18n("Type:"), selection, 0, false, &ok);
|
||||
@ -319,6 +326,8 @@ void AWFormatterHelper::doCreateItem()
|
||||
return createItem<AWScriptFormatter>();
|
||||
case AWAbstractFormatter::FormatterClass::String:
|
||||
return createItem<AWStringFormatter>();
|
||||
case AWAbstractFormatter::FormatterClass::Json:
|
||||
return createItem<AWNoFormatter>();
|
||||
case AWAbstractFormatter::FormatterClass::NoFormat:
|
||||
return createItem<AWNoFormatter>();
|
||||
}
|
||||
|
@ -69,6 +69,9 @@ QString AWAbstractFormatter::strType() const
|
||||
case FormatterClass::String:
|
||||
value = QString("String");
|
||||
break;
|
||||
case FormatterClass::Json:
|
||||
value = QString("Json");
|
||||
break;
|
||||
case FormatterClass::NoFormat:
|
||||
value = QString("NoFormat");
|
||||
break;
|
||||
@ -98,6 +101,8 @@ void AWAbstractFormatter::setStrType(const QString _type)
|
||||
m_type = FormatterClass::Script;
|
||||
else if (_type == QString("String"))
|
||||
m_type = FormatterClass::String;
|
||||
else if (_type == QString("Json"))
|
||||
m_type = FormatterClass::Json;
|
||||
else
|
||||
m_type = FormatterClass::NoFormat;
|
||||
}
|
||||
|
@ -34,7 +34,8 @@ public:
|
||||
List,
|
||||
Script,
|
||||
String,
|
||||
NoFormat
|
||||
NoFormat,
|
||||
Json
|
||||
};
|
||||
|
||||
explicit AWAbstractFormatter(QWidget *parent,
|
||||
|
201
sources/awesomewidgets/awjsonformatter.cpp
Normal file
201
sources/awesomewidgets/awjsonformatter.cpp
Normal file
@ -0,0 +1,201 @@
|
||||
/***************************************************************************
|
||||
* This file is part of awesome-widgets *
|
||||
* *
|
||||
* awesome-widgets is free software: you can redistribute it and/or *
|
||||
* modify it under the terms of the GNU General Public License as *
|
||||
* published by the Free Software Foundation, either version 3 of the *
|
||||
* License, or (at your option) any later version. *
|
||||
* *
|
||||
* awesome-widgets is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with awesome-widgets. If not, see http://www.gnu.org/licenses/ *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#include "awjsonformatter.h"
|
||||
#include "ui_awjsonformatter.h"
|
||||
|
||||
#include <KI18n/KLocalizedString>
|
||||
|
||||
#include <QSettings>
|
||||
|
||||
#include "awdebug.h"
|
||||
|
||||
|
||||
AWJsonFormatter::AWJsonFormatter(QWidget *parent, const QString filePath)
|
||||
: AWAbstractFormatter(parent, filePath)
|
||||
, ui(new Ui::AWJsonFormatter)
|
||||
{
|
||||
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
||||
|
||||
if (!filePath.isEmpty())
|
||||
readConfiguration();
|
||||
ui->setupUi(this);
|
||||
translate();
|
||||
}
|
||||
|
||||
|
||||
AWJsonFormatter::~AWJsonFormatter()
|
||||
{
|
||||
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
||||
|
||||
delete ui;
|
||||
}
|
||||
|
||||
|
||||
QString AWJsonFormatter::convert(const QVariant &_value) const
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Convert value" << _value;
|
||||
|
||||
QVariant converted = _value;
|
||||
for (auto &element : m_splittedPath)
|
||||
converted = getFromJson(converted, element);
|
||||
|
||||
return converted.toString();
|
||||
}
|
||||
|
||||
|
||||
AWJsonFormatter *AWJsonFormatter::copy(const QString _fileName,
|
||||
const int _number)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "File" << _fileName << "with number" << _number;
|
||||
|
||||
AWJsonFormatter *item
|
||||
= new AWJsonFormatter(static_cast<QWidget *>(parent()), _fileName);
|
||||
AWAbstractFormatter::copyDefaults(item);
|
||||
item->setNumber(_number);
|
||||
item->setPath(path());
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
|
||||
QString AWJsonFormatter::path() const
|
||||
{
|
||||
return m_path;
|
||||
}
|
||||
|
||||
|
||||
void AWJsonFormatter::setPath(const QString _path)
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Path" << _path;
|
||||
|
||||
m_path = _path;
|
||||
initPath();
|
||||
}
|
||||
|
||||
|
||||
void AWJsonFormatter::readConfiguration()
|
||||
{
|
||||
AWAbstractFormatter::readConfiguration();
|
||||
|
||||
QSettings settings(fileName(), QSettings::IniFormat);
|
||||
|
||||
settings.beginGroup(QString("Desktop Entry"));
|
||||
setPath(settings.value(QString("X-AW-Path"), path()).toString());
|
||||
settings.endGroup();
|
||||
|
||||
bumpApi(AWEFAPI);
|
||||
}
|
||||
|
||||
|
||||
int AWJsonFormatter::showConfiguration(const QVariant args)
|
||||
{
|
||||
Q_UNUSED(args)
|
||||
|
||||
ui->lineEdit_name->setText(name());
|
||||
ui->lineEdit_comment->setText(comment());
|
||||
ui->label_typeValue->setText(QString("NoFormat"));
|
||||
ui->lineEdit_path->setText(path());
|
||||
|
||||
int ret = exec();
|
||||
if (ret != 1)
|
||||
return ret;
|
||||
setName(ui->lineEdit_name->text());
|
||||
setComment(ui->lineEdit_comment->text());
|
||||
setApiVersion(AWEFAPI);
|
||||
setStrType(ui->label_typeValue->text());
|
||||
setPath(ui->lineEdit_path->text());
|
||||
|
||||
writeConfiguration();
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
void AWJsonFormatter::writeConfiguration() const
|
||||
{
|
||||
AWAbstractFormatter::writeConfiguration();
|
||||
|
||||
QSettings settings(writtableConfig(), QSettings::IniFormat);
|
||||
qCInfo(LOG_LIB) << "Configuration file" << settings.fileName();
|
||||
|
||||
settings.beginGroup(QString("Desktop Entry"));
|
||||
settings.setValue(QString("X-AW-Path"), path());
|
||||
settings.endGroup();
|
||||
|
||||
settings.sync();
|
||||
}
|
||||
|
||||
|
||||
QVariant AWJsonFormatter::getFromJson(const QVariant &value,
|
||||
const QVariant &element) const
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Looking for element" << element << "in" << value;
|
||||
|
||||
if (element.type() == QVariant::String) {
|
||||
return getFromMap(value, element.toString());
|
||||
} else if (element.type() == QVariant::Int) {
|
||||
return getFromList(value, element.toInt());
|
||||
} else {
|
||||
qCWarning(LOG_LIB) << "Unknown type" << element.typeName();
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
QVariant AWJsonFormatter::getFromList(const QVariant &value,
|
||||
const int index) const
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Looking for index" << index << "in" << value;
|
||||
|
||||
return value.toList()[index];
|
||||
}
|
||||
|
||||
|
||||
QVariant AWJsonFormatter::getFromMap(const QVariant &value,
|
||||
const QString &key) const
|
||||
{
|
||||
qCDebug(LOG_LIB) << "Looking for key" << key << "in" << value;
|
||||
|
||||
return value.toMap()[key];
|
||||
}
|
||||
|
||||
|
||||
void AWJsonFormatter::initPath()
|
||||
{
|
||||
m_splittedPath.clear();
|
||||
QStringList splittedByDot = m_path.split(QRegExp(QString("(\\.|\\[|\\])")),
|
||||
QString::SkipEmptyParts);
|
||||
|
||||
for (auto &element : splittedByDot) {
|
||||
bool ok;
|
||||
int number = element.toInt(&ok);
|
||||
if (ok)
|
||||
m_splittedPath.append(number);
|
||||
else
|
||||
m_splittedPath.append(element);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void AWJsonFormatter::translate()
|
||||
{
|
||||
ui->label_name->setText(i18n("Name"));
|
||||
ui->label_comment->setText(i18n("Comment"));
|
||||
ui->label_type->setText(i18n("Type"));
|
||||
ui->label_path->setText(i18n("Path"));
|
||||
}
|
62
sources/awesomewidgets/awjsonformatter.h
Normal file
62
sources/awesomewidgets/awjsonformatter.h
Normal file
@ -0,0 +1,62 @@
|
||||
/***************************************************************************
|
||||
* This file is part of awesome-widgets *
|
||||
* *
|
||||
* awesome-widgets is free software: you can redistribute it and/or *
|
||||
* modify it under the terms of the GNU General Public License as *
|
||||
* published by the Free Software Foundation, either version 3 of the *
|
||||
* License, or (at your option) any later version. *
|
||||
* *
|
||||
* awesome-widgets is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with awesome-widgets. If not, see http://www.gnu.org/licenses/ *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef AWJSONFORMATTER_H
|
||||
#define AWJSONFORMATTER_H
|
||||
|
||||
#include "awabstractformatter.h"
|
||||
|
||||
|
||||
namespace Ui
|
||||
{
|
||||
class AWJsonFormatter;
|
||||
}
|
||||
|
||||
class AWJsonFormatter : public AWAbstractFormatter
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QString path READ path WRITE setPath)
|
||||
|
||||
public:
|
||||
explicit AWJsonFormatter(QWidget *parent,
|
||||
const QString filePath = QString());
|
||||
virtual ~AWJsonFormatter();
|
||||
QString convert(const QVariant &_value) const;
|
||||
AWJsonFormatter *copy(const QString _fileName, const int _number);
|
||||
// properties
|
||||
QString path() const;
|
||||
void setPath(const QString _path);
|
||||
|
||||
public slots:
|
||||
void readConfiguration();
|
||||
int showConfiguration(const QVariant args = QVariant());
|
||||
void writeConfiguration() const;
|
||||
|
||||
private:
|
||||
Ui::AWJsonFormatter *ui = nullptr;
|
||||
QVariant getFromJson(const QVariant &value, const QVariant &element) const;
|
||||
QVariant getFromList(const QVariant &value, const int index) const;
|
||||
QVariant getFromMap(const QVariant &value, const QString &key) const;
|
||||
void initPath();
|
||||
void translate();
|
||||
// properties
|
||||
QString m_path;
|
||||
QVariantList m_splittedPath;
|
||||
};
|
||||
|
||||
|
||||
#endif /* AWJSONFORMATTER_H */
|
167
sources/awesomewidgets/awjsonformatter.ui
Normal file
167
sources/awesomewidgets/awjsonformatter.ui
Normal file
@ -0,0 +1,167 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>AWJsonFormatter</class>
|
||||
<widget class="QDialog" name="AWJsonFormatter">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>420</width>
|
||||
<height>128</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Configuration</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="layout_name">
|
||||
<item>
|
||||
<widget class="QLabel" name="label_name">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Name</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="lineEdit_name"/>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="layout_comment">
|
||||
<item>
|
||||
<widget class="QLabel" name="label_comment">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Comment</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="lineEdit_comment"/>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="layout_type">
|
||||
<item>
|
||||
<widget class="QLabel" name="label_type">
|
||||
<property name="text">
|
||||
<string>Type</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="label_typeValue">
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="layout_path">
|
||||
<item>
|
||||
<widget class="QLabel" name="label_path">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Path</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="lineEdit_path"/>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="verticalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>20</width>
|
||||
<height>40</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QDialogButtonBox" name="buttonBox">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="standardButtons">
|
||||
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections>
|
||||
<connection>
|
||||
<sender>buttonBox</sender>
|
||||
<signal>accepted()</signal>
|
||||
<receiver>AWJsonFormatter</receiver>
|
||||
<slot>accept()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>248</x>
|
||||
<y>254</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>157</x>
|
||||
<y>274</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
<connection>
|
||||
<sender>buttonBox</sender>
|
||||
<signal>rejected()</signal>
|
||||
<receiver>AWJsonFormatter</receiver>
|
||||
<slot>reject()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>316</x>
|
||||
<y>260</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>286</x>
|
||||
<y>274</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
</connections>
|
||||
</ui>
|
Loading…
Reference in New Issue
Block a user