rewrite formatters to ext classes

This commit is contained in:
Evgenii Alekseev 2016-05-12 20:51:21 +03:00
parent 14aab3b758
commit 326c65528d
21 changed files with 288 additions and 275 deletions

View File

@ -21,7 +21,7 @@
#include "awactions.h"
#include "awconfighelper.h"
#include "awdataengineaggregator.h"
#include "awformatterhelper.h"
#include "awkeys.h"
@ -31,5 +31,6 @@ void AWPlugin::registerTypes(const char *uri)
qmlRegisterType<AWActions>(uri, 1, 0, "AWActions");
qmlRegisterType<AWConfigHelper>(uri, 1, 0, "AWConfigHelper");
qmlRegisterType<AWFormatterHelper>(uri, 1, 0, "AWFormatterHelper");
qmlRegisterType<AWKeys>(uri, 1, 0, "AWKeys");
}

View File

@ -17,9 +17,11 @@
#include "awformatterhelper.h"
#include <KI18n/KLocalizedString>
#include <QDir>
#include <QInputDialog>
#include <QSettings>
#include <QStandardPaths>
#include "awdebug.h"
#include "awdatetimeformatter.h"
@ -28,15 +30,13 @@
#include "awscriptformatter.h"
AWFormatterHelper::AWFormatterHelper(QObject *parent)
: QObject(parent)
AWFormatterHelper::AWFormatterHelper(QWidget *parent)
: AbstractExtItemAggregator(parent, QString("formatters"))
{
qCDebug(LOG_AW) << __PRETTY_FUNCTION__;
#ifdef BUILD_FUTURE
installDirectories();
initFormatters();
initKeys();
initItems();
#endif /* BUILD_FUTURE */
}
@ -66,12 +66,30 @@ QStringList AWFormatterHelper::definedFormatters() const
}
QString AWFormatterHelper::formatterByTag(const QString tag) const
{
qCDebug(LOG_AW) << "Looking for tag" << tag;
return m_formatters.contains(tag) ? m_formatters[tag]->name() : QString();
}
QStringList AWFormatterHelper::knownFormatters() const
{
return m_formattersClasses.keys();
}
QList<AbstractExtItem *> AWFormatterHelper::items() const
{
QList<AbstractExtItem *> converted;
for (auto item : m_formattersClasses.values())
converted.append(item);
return converted;
}
AWFormatterHelper::FormatterClass
AWFormatterHelper::defineFormatterClass(const QString stringType) const
{
@ -114,19 +132,18 @@ void AWFormatterHelper::initFormatters()
switch (metadata.second) {
case FormatterClass::DateTime:
m_formattersClasses[name]
= new AWDateTimeFormatter(nullptr, filePath);
= new AWDateTimeFormatter(this, filePath);
break;
case FormatterClass::Float:
m_formattersClasses[name]
= new AWFloatFormatter(nullptr, filePath);
= new AWFloatFormatter(this, filePath);
break;
case FormatterClass::Script:
m_formattersClasses[name]
= new AWScriptFormatter(nullptr, filePath);
= new AWScriptFormatter(this, filePath);
break;
case FormatterClass::NoFormat:
m_formattersClasses[name]
= new AWNoFormatter(nullptr, filePath);
m_formattersClasses[name] = new AWNoFormatter(this, filePath);
break;
}
}
@ -185,3 +202,36 @@ AWFormatterHelper::readMetadata(const QString filePath) const
return QPair<QString, AWFormatterHelper::FormatterClass>(name, formatter);
}
void AWFormatterHelper::doCreateItem()
{
QStringList selection = QStringList()
<< QString("NoFormat") << QString("DateTime")
<< QString("Float") << QString("Script");
bool ok;
QString select = QInputDialog::getItem(
this, i18n("Select type"), i18n("Type:"), selection, 0, false, &ok);
if (!ok)
return;
FormatterClass formatter = defineFormatterClass(select);
switch (formatter) {
case FormatterClass::DateTime:
return createItem<AWDateTimeFormatter>();
case FormatterClass::Float:
return createItem<AWFloatFormatter>();
case FormatterClass::Script:
return createItem<AWScriptFormatter>();
case FormatterClass::NoFormat:
return createItem<AWNoFormatter>();
}
}
void AWFormatterHelper::initItems()
{
installDirectories();
initFormatters();
initKeys();
}

View File

@ -19,25 +19,28 @@
#ifndef AWFORMATTERHELPER_H
#define AWFORMATTERHELPER_H
#include <QObject>
#include "awabstractformatter.h"
#include "abstractextitemaggregator.h"
class AWFormatterHelper : public QObject
class AWAbstractFormatter;
class AWFormatterHelper : public AbstractExtItemAggregator
{
Q_OBJECT
public:
enum class FormatterClass { DateTime, Float, Script, NoFormat };
explicit AWFormatterHelper(QObject *parent = nullptr);
explicit AWFormatterHelper(QWidget *parent = nullptr);
virtual ~AWFormatterHelper();
QString convert(const QVariant &value, const QString name) const;
QStringList definedFormatters() const;
QStringList knownFormatters() const;
Q_INVOKABLE QStringList definedFormatters() const;
Q_INVOKABLE QString formatterByTag(const QString tag) const;
Q_INVOKABLE QStringList knownFormatters() const;
QList<AbstractExtItem *> items() const;
private:
// methods
AWFormatterHelper::FormatterClass
defineFormatterClass(const QString stringType) const;
void initFormatters();
@ -45,6 +48,9 @@ private:
void installDirectories();
QPair<QString, AWFormatterHelper::FormatterClass>
readMetadata(const QString filePath) const;
// parent methods
void doCreateItem();
void initItems();
// properties
QStringList m_directories;
QString m_formatterConfig;

View File

@ -49,7 +49,7 @@ AWKeysAggregator::AWKeysAggregator(QObject *parent)
m_formatter[QString("swaptotmb")] = FormatterType::MemMBFormat;
m_formatter[QString("swaptotgb")] = FormatterType::MemGBFormat;
m_customFormatters = new AWFormatterHelper(this);
m_customFormatters = new AWFormatterHelper(nullptr);
}

View File

@ -19,15 +19,16 @@
#include <KI18n/KLocalizedString>
#include <QFileInfo>
#include <QHBoxLayout>
#include <QInputDialog>
#include <QLineEdit>
#include "awdebug.h"
AbstractExtItemAggregator::AbstractExtItemAggregator(QWidget *parent)
AbstractExtItemAggregator::AbstractExtItemAggregator(QWidget *parent,
const QString type)
: QWidget(parent)
, m_type(type)
{
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
@ -62,6 +63,59 @@ AbstractExtItemAggregator::~AbstractExtItemAggregator()
}
void AbstractExtItemAggregator::copyItem()
{
AbstractExtItem *source = itemFromWidget();
QString fileName = getName();
int number = uniqNumber();
QString dir = QString("%1/awesomewidgets/%2")
.arg(QStandardPaths::writableLocation(
QStandardPaths::GenericDataLocation))
.arg(m_type);
if ((source == nullptr) || (fileName.isEmpty())) {
qCWarning(LOG_LIB) << "Nothing to copy";
return;
}
QString filePath = QString("%1/%2").arg(dir).arg(fileName);
AbstractExtItem *newItem = source->copy(filePath, number);
if (newItem->showConfiguration(configArgs()) == 1) {
initItems();
repaintList();
}
}
void AbstractExtItemAggregator::deleteItem()
{
AbstractExtItem *source = itemFromWidget();
if (source == nullptr) {
qCWarning(LOG_LIB) << "Nothing to delete";
return;
};
if (source->tryDelete()) {
initItems();
repaintList();
}
}
void AbstractExtItemAggregator::editItem()
{
AbstractExtItem *source = itemFromWidget();
if (source == nullptr) {
qCWarning(LOG_LIB) << "Nothing to edit";
return;
};
if (source->showConfiguration(configArgs()) == 1) {
initItems();
repaintList();
}
}
QString AbstractExtItemAggregator::getName()
{
bool ok;
@ -77,12 +131,69 @@ QString AbstractExtItemAggregator::getName()
}
AbstractExtItem *AbstractExtItemAggregator::itemFromWidget()
{
QListWidgetItem *widgetItem = widgetDialog->currentItem();
if (widgetItem == nullptr)
return nullptr;
AbstractExtItem *found = nullptr;
for (auto item : items()) {
QString fileName = QFileInfo(item->fileName()).fileName();
if (fileName != widgetItem->text())
continue;
found = item;
break;
}
if (found == nullptr)
qCWarning(LOG_LIB) << "Could not find item by name"
<< widgetItem->text();
return found;
}
void AbstractExtItemAggregator::repaintList()
{
widgetDialog->clear();
for (auto _item : items()) {
QString fileName = QFileInfo(_item->fileName()).fileName();
QListWidgetItem *item = new QListWidgetItem(fileName, widgetDialog);
QStringList tooltip;
tooltip.append(i18n("Name: %1", _item->name()));
tooltip.append(i18n("Comment: %1", _item->comment()));
tooltip.append(i18n("Identity: %1", _item->uniq()));
item->setToolTip(tooltip.join(QChar('\n')));
widgetDialog->addItem(item);
}
}
int AbstractExtItemAggregator::uniqNumber() const
{
QList<int> tagList;
for (auto item : items())
tagList.append(item->number());
int number = 0;
while (tagList.contains(number))
number++;
return number;
}
QVariant AbstractExtItemAggregator::configArgs() const
{
return m_configArgs;
}
QString AbstractExtItemAggregator::type() const
{
return m_type;
}
void AbstractExtItemAggregator::setConfigArgs(const QVariant _configArgs)
{
qCDebug(LOG_LIB) << "Configuration arguments" << _configArgs;
@ -102,7 +213,7 @@ void AbstractExtItemAggregator::editItemButtonPressed(QAbstractButton *button)
if (static_cast<QPushButton *>(button) == copyButton)
return copyItem();
else if (static_cast<QPushButton *>(button) == createButton)
return createItem();
return doCreateItem();
else if (static_cast<QPushButton *>(button) == deleteButton)
return deleteItem();
else if (dialogButtons->buttonRole(button) == QDialogButtonBox::AcceptRole)

View File

@ -22,19 +22,52 @@
#include <QDialogButtonBox>
#include <QListWidget>
#include <QPushButton>
#include <QStandardPaths>
#include <QWidget>
#include "abstractextitem.h"
#include "awdebug.h"
// additional class since QObject macro does not allow class templates
class AbstractExtItemAggregator : public QWidget
{
Q_OBJECT
Q_PROPERTY(QVariant configArgs READ configArgs WRITE setConfigArgs)
Q_PROPERTY(QVariant type READ type)
public:
explicit AbstractExtItemAggregator(QWidget *parent = nullptr);
explicit AbstractExtItemAggregator(QWidget *parent, const QString type);
virtual ~AbstractExtItemAggregator();
// methods
void copyItem();
template <class T> void createItem()
{
QString fileName = getName();
int number = uniqNumber();
QString dir = QString("%1/awesomewidgets/%2")
.arg(QStandardPaths::writableLocation(
QStandardPaths::GenericDataLocation))
.arg(m_type);
if (fileName.isEmpty()) {
qCWarning(LOG_LIB) << "Nothing to create";
return;
};
QString filePath = QString("%1/%2").arg(dir).arg(fileName);
T *newItem = new T(this, filePath);
newItem->setNumber(number);
if (newItem->showConfiguration(configArgs()) == 1) {
initItems();
repaintList();
};
};
void deleteItem();
void editItem();
QString getName();
AbstractExtItem *itemFromWidget();
void repaintList();
int uniqNumber() const;
// ui
QDialog *dialog = nullptr;
QListWidget *widgetDialog = nullptr;
@ -44,6 +77,8 @@ public:
QPushButton *deleteButton = nullptr;
// get methods
QVariant configArgs() const;
virtual QList<AbstractExtItem *> items() const = 0;
QString type() const;
// set methods
void setConfigArgs(const QVariant _configArgs);
@ -53,11 +88,10 @@ private slots:
private:
QVariant m_configArgs;
// methods
virtual void copyItem() = 0;
virtual void createItem() = 0;
virtual void deleteItem() = 0;
virtual void editItem() = 0;
QString m_type;
// ui methods
virtual void doCreateItem() = 0;
virtual void initItems() = 0;
};

View File

@ -17,23 +17,16 @@
#include "awabstractformatter.h"
#include <QDir>
#include <QSettings>
#include <QStandardPaths>
#include "awdebug.h"
AWAbstractFormatter::AWAbstractFormatter(QWidget *parent,
const QString filePath)
: QDialog(parent)
, m_fileName(filePath)
: AbstractExtItem(parent, filePath)
{
qCDebug(LOG_AW) << __PRETTY_FUNCTION__;
qCDebug(LOG_AW) << "Desktop name" << filePath;
m_name = m_fileName;
}
@ -43,44 +36,17 @@ AWAbstractFormatter::~AWAbstractFormatter()
}
void AWAbstractFormatter::copyDefaults(AWAbstractFormatter *_other) const
void AWAbstractFormatter::copyDefaults(AbstractExtItem *_other) const
{
_other->setComment(m_comment);
_other->setName(m_name);
_other->setType(m_type);
AbstractExtItem::copyDefaults(_other);
static_cast<AWAbstractFormatter *>(_other)->setType(m_type);
}
QString AWAbstractFormatter::writtableConfig() const
QString AWAbstractFormatter::uniq() const
{
QStringList paths = m_fileName.split(QChar('/'));
QString name = paths.takeLast();
QString dir = paths.takeLast();
return QString("%1/awesomewidgets/%2/%3")
.arg(QStandardPaths::writableLocation(
QStandardPaths::GenericDataLocation))
.arg(dir)
.arg(name);
}
QString AWAbstractFormatter::comment() const
{
return m_comment;
}
QString AWAbstractFormatter::fileName() const
{
return m_fileName;
}
QString AWAbstractFormatter::name() const
{
return m_name;
return QString("%1(%2)").arg(name()).arg(m_type);
}
@ -90,22 +56,6 @@ QString AWAbstractFormatter::type() const
}
void AWAbstractFormatter::setComment(const QString _comment)
{
qCDebug(LOG_AW) << "Comment" << _comment;
m_comment = _comment;
}
void AWAbstractFormatter::setName(const QString _name)
{
qCDebug(LOG_AW) << "Name" << _name;
m_name = _name;
}
void AWAbstractFormatter::setType(const QString _type)
{
qCDebug(LOG_AW) << "Type" << _type;
@ -116,34 +66,24 @@ void AWAbstractFormatter::setType(const QString _type)
void AWAbstractFormatter::readConfiguration()
{
QSettings settings(m_fileName, QSettings::IniFormat);
AbstractExtItem::readConfiguration();
QSettings settings(fileName(), QSettings::IniFormat);
settings.beginGroup(QString("Desktop Entry"));
setName(settings.value(QString("Name"), m_name).toString());
setComment(settings.value(QString("Comment"), m_comment).toString());
setType(settings.value(QString("Type"), m_type).toString());
settings.endGroup();
}
bool AWAbstractFormatter::tryDelete() const
{
bool status = QFile::remove(m_fileName);
qCInfo(LOG_AW) << "Remove file" << m_fileName << status;
return status;
}
void AWAbstractFormatter::writeConfiguration() const
{
AbstractExtItem::writeConfiguration();
QSettings settings(writtableConfig(), QSettings::IniFormat);
qCInfo(LOG_AW) << "Configuration file" << settings.fileName();
settings.beginGroup(QString("Desktop Entry"));
settings.setValue(QString("Encoding"), QString("UTF-8"));
settings.setValue(QString("Name"), m_name);
settings.setValue(QString("Comment"), m_comment);
settings.setValue(QString("Type"), m_type);
settings.endGroup();

View File

@ -18,47 +18,32 @@
#ifndef AWABSTRACTFORMATTER_H
#define AWABSTRACTFORMATTER_H
#include <QDialog>
#include <QVariant>
#include "abstractextitem.h"
class AWAbstractFormatter : public QDialog
class AWAbstractFormatter : public AbstractExtItem
{
Q_OBJECT
Q_PROPERTY(QString comment READ comment WRITE setComment)
Q_PROPERTY(QString fileName READ fileName)
Q_PROPERTY(QString name READ name WRITE setName)
Q_PROPERTY(QString type READ type WRITE setType)
public:
explicit AWAbstractFormatter(QWidget *parent = nullptr,
const QString filePath = QString());
virtual ~AWAbstractFormatter();
virtual AWAbstractFormatter *copy(const QString _fileName) = 0;
void copyDefaults(AWAbstractFormatter *_other) const;
void copyDefaults(AbstractExtItem *_other) const;
virtual QString convert(const QVariant &_value) const = 0;
QString writtableConfig() const;
QString uniq() const;
// properties
QString comment() const;
QString fileName() const;
QString name() const;
QString type() const;
void setComment(const QString _comment = QString("empty"));
void setName(const QString _name = QString("none"));
void setType(const QString _type = QString("NoFormat"));
public slots:
virtual void readConfiguration();
virtual int showConfiguration(const QVariant args = QVariant()) = 0;
bool tryDelete() const;
QVariantHash run() { return QVariantHash(); };
virtual void writeConfiguration() const;
private:
QString m_fileName;
virtual void translate() = 0;
// properties
QString m_comment = QString("empty");
QString m_name = QString("none");
QString m_type = QString("NoFormat");
};

View File

@ -70,14 +70,16 @@ QString AWDateTimeFormatter::convert(const QVariant &_value) const
}
AWDateTimeFormatter *AWDateTimeFormatter::copy(const QString _fileName)
AWDateTimeFormatter *AWDateTimeFormatter::copy(const QString _fileName,
const int _number)
{
qCDebug(LOG_LIB) << "File" << _fileName;
qCDebug(LOG_LIB) << "File" << _fileName << "with number" << _number;
AWDateTimeFormatter *item
= new AWDateTimeFormatter(static_cast<QWidget *>(parent()), _fileName);
copyDefaults(item);
AWAbstractFormatter::copyDefaults(item);
item->setFormat(format());
item->setNumber(_number);
return item;
}

View File

@ -36,7 +36,7 @@ public:
explicit AWDateTimeFormatter(const QString format, QWidget *parent);
virtual ~AWDateTimeFormatter();
QString convert(const QVariant &_value) const;
AWDateTimeFormatter *copy(const QString _fileName);
AWDateTimeFormatter *copy(const QString _fileName, const int _number);
// properties
QString format() const;
void setFormat(const QString _format);

View File

@ -77,17 +77,19 @@ QString AWFloatFormatter::convert(const QVariant &_value) const
}
AWFloatFormatter *AWFloatFormatter::copy(const QString _fileName)
AWFloatFormatter *AWFloatFormatter::copy(const QString _fileName,
const int _number)
{
qCDebug(LOG_LIB) << "File" << _fileName;
qCDebug(LOG_LIB) << "File" << _fileName << "with number" << _number;
AWFloatFormatter *item
= new AWFloatFormatter(static_cast<QWidget *>(parent()), _fileName);
copyDefaults(item);
AWAbstractFormatter::copyDefaults(item);
item->setCount(count());
item->setFormat(format());
item->setFillChar(fillChar());
item->setMultiplier(multiplier());
item->setNumber(_number);
item->setPrecision(precision());
item->setSummand(summand());

View File

@ -44,7 +44,7 @@ public:
QWidget *parent);
virtual ~AWFloatFormatter();
QString convert(const QVariant &_value) const;
AWFloatFormatter *copy(const QString _fileName);
AWFloatFormatter *copy(const QString _fileName, const int _number);
// properties
int count() const;
QChar fillChar() const;

View File

@ -63,13 +63,14 @@ QString AWNoFormatter::convert(const QVariant &_value) const
}
AWNoFormatter *AWNoFormatter::copy(const QString _fileName)
AWNoFormatter *AWNoFormatter::copy(const QString _fileName, const int _number)
{
qCDebug(LOG_LIB) << "File" << _fileName;
qCDebug(LOG_LIB) << "File" << _fileName << "with number" << _number;
AWNoFormatter *item
= new AWNoFormatter(static_cast<QWidget *>(parent()), _fileName);
copyDefaults(item);
AWAbstractFormatter::copyDefaults(item);
item->setNumber(_number);
return item;
}

View File

@ -35,7 +35,7 @@ public:
explicit AWNoFormatter(QWidget *parent);
virtual ~AWNoFormatter();
QString convert(const QVariant &_value) const;
AWNoFormatter *copy(const QString _fileName);
AWNoFormatter *copy(const QString _fileName, const int _number);
public slots:
int showConfiguration(const QVariant args = QVariant());

View File

@ -86,16 +86,18 @@ QString AWScriptFormatter::convert(const QVariant &_value) const
}
AWScriptFormatter *AWScriptFormatter::copy(const QString _fileName)
AWScriptFormatter *AWScriptFormatter::copy(const QString _fileName,
const int _number)
{
qCDebug(LOG_LIB) << "File" << _fileName;
qCDebug(LOG_LIB) << "File" << _fileName << "with number" << _number;
AWScriptFormatter *item
= new AWScriptFormatter(static_cast<QWidget *>(parent()), _fileName);
copyDefaults(item);
AWAbstractFormatter::copyDefaults(item);
item->setAppendCode(appendCode());
item->setCode(code());
item->setHasReturn(hasReturn());
item->setNumber(_number);
return item;
}

View File

@ -40,7 +40,7 @@ public:
const bool hasReturn, QWidget *parent);
virtual ~AWScriptFormatter();
QString convert(const QVariant &_value) const;
AWScriptFormatter *copy(const QString _fileName);
AWScriptFormatter *copy(const QString _fileName, const int _number);
// properties
bool appendCode() const;
QString code() const;

View File

@ -32,8 +32,7 @@ template <class T> class ExtItemAggregator : public AbstractExtItemAggregator
{
public:
explicit ExtItemAggregator(QWidget *parent, const QString type)
: AbstractExtItemAggregator(parent)
, m_type(type)
: AbstractExtItemAggregator(parent, type)
{
qSetMessagePattern(LOG_FORMAT);
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
@ -70,7 +69,7 @@ public:
for (auto item : m_items) {
if (item->tag(_type) != _tag)
continue;
found = item;
found = static_cast<T *>(item);
break;
}
if (found == nullptr)
@ -87,7 +86,7 @@ public:
for (auto item : m_items) {
if (item->number() != _number)
continue;
found = item;
found = static_cast<T *>(item);
break;
}
if (found == nullptr)
@ -96,63 +95,31 @@ public:
return found;
};
T *itemFromWidget() const
{
QListWidgetItem *widgetItem = widgetDialog->currentItem();
if (widgetItem == nullptr)
return nullptr;
T *found = nullptr;
for (auto item : m_items) {
QString fileName = QFileInfo(item->fileName()).fileName();
if (fileName != widgetItem->text())
continue;
found = item;
break;
}
if (found == nullptr)
qCWarning(LOG_LIB) << "Could not find item by name"
<< widgetItem->text();
return found;
};
QList<T *> items() const { return m_items; };
int uniqNumber() const
{
QList<int> tagList;
for (auto item : m_items)
tagList.append(item->number());
int number = 0;
while (tagList.contains(number))
number++;
return number;
};
QList<AbstractExtItem *> items() const { return m_items; };
private:
QList<T *> m_items;
QList<AbstractExtItem *> m_items;
QList<T *> m_activeItems;
QString m_type;
QList<T *> getItems()
void doCreateItem() { return createItem<T>(); }
QList<AbstractExtItem *> getItems()
{
// create directory at $HOME
QString localDir = QString("%1/awesomewidgets/%2")
.arg(QStandardPaths::writableLocation(
QStandardPaths::GenericDataLocation))
.arg(m_type);
.arg(type());
QDir localDirectory;
if (localDirectory.mkpath(localDir))
qCInfo(LOG_LIB) << "Created directory" << localDir;
QStringList dirs = QStandardPaths::locateAll(
QStandardPaths::GenericDataLocation,
QString("awesomewidgets/%1").arg(m_type),
QString("awesomewidgets/%1").arg(type()),
QStandardPaths::LocateDirectory);
QStringList names;
QList<T *> items;
QList<AbstractExtItem *> items;
for (auto dir : dirs) {
QStringList files = QDir(dir).entryList(QDir::Files, QDir::Name);
for (auto file : files) {
@ -167,9 +134,10 @@ private:
}
// sort items
std::sort(items.begin(), items.end(), [](const T *lhs, const T *rhs) {
return lhs->number() < rhs->number();
});
std::sort(items.begin(), items.end(),
[](const AbstractExtItem *lhs, const AbstractExtItem *rhs) {
return lhs->number() < rhs->number();
});
return items;
};
@ -182,96 +150,7 @@ private:
for (auto item : m_items) {
if (!item->isActive())
continue;
m_activeItems.append(item);
}
};
void repaintList()
{
widgetDialog->clear();
for (auto _item : m_items) {
QString fileName = QFileInfo(_item->fileName()).fileName();
QListWidgetItem *item
= new QListWidgetItem(fileName, widgetDialog);
QStringList tooltip;
tooltip.append(i18n("Name: %1", _item->name()));
tooltip.append(i18n("Comment: %1", _item->comment()));
tooltip.append(i18n("Identity: %1", _item->uniq()));
item->setToolTip(tooltip.join(QChar('\n')));
widgetDialog->addItem(item);
}
};
// methods
void copyItem()
{
T *source = itemFromWidget();
QString fileName = getName();
int number = uniqNumber();
QString dir = QString("%1/awesomewidgets/%2")
.arg(QStandardPaths::writableLocation(
QStandardPaths::GenericDataLocation))
.arg(m_type);
if ((source == nullptr) || (fileName.isEmpty())) {
qCWarning(LOG_LIB) << "Nothing to copy";
return;
}
QString filePath = QString("%1/%2").arg(dir).arg(fileName);
T *newItem = static_cast<T *>(source->copy(filePath, number));
if (newItem->showConfiguration(configArgs()) == 1) {
initItems();
repaintList();
}
};
void createItem()
{
QString fileName = getName();
int number = uniqNumber();
QString dir = QString("%1/awesomewidgets/%2")
.arg(QStandardPaths::writableLocation(
QStandardPaths::GenericDataLocation))
.arg(m_type);
if (fileName.isEmpty()) {
qCWarning(LOG_LIB) << "Nothing to create";
return;
};
QString filePath = QString("%1/%2").arg(dir).arg(fileName);
T *newItem = new T(this, filePath);
newItem->setNumber(number);
if (newItem->showConfiguration(configArgs()) == 1) {
initItems();
repaintList();
}
};
void deleteItem()
{
T *source = itemFromWidget();
if (source == nullptr) {
qCWarning(LOG_LIB) << "Nothing to delete";
return;
};
if (source->tryDelete()) {
initItems();
repaintList();
}
};
void editItem()
{
T *source = itemFromWidget();
if (source == nullptr) {
qCWarning(LOG_LIB) << "Nothing to edit";
return;
};
if (source->showConfiguration(configArgs()) == 1) {
initItems();
repaintList();
m_activeItems.append(static_cast<T *>(item));
}
};
};