diff --git a/sources/awesome-widget/plugin/awesomewidget.cpp b/sources/awesome-widget/plugin/awesomewidget.cpp index 6d6233b..4235ed5 100644 --- a/sources/awesome-widget/plugin/awesomewidget.cpp +++ b/sources/awesome-widget/plugin/awesomewidget.cpp @@ -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(uri, 1, 0, "AWActions"); qmlRegisterType(uri, 1, 0, "AWConfigHelper"); + qmlRegisterType(uri, 1, 0, "AWFormatterHelper"); qmlRegisterType(uri, 1, 0, "AWKeys"); } diff --git a/sources/awesome-widget/plugin/awformatterhelper.cpp b/sources/awesome-widget/plugin/awformatterhelper.cpp index cac9759..3a42a58 100644 --- a/sources/awesome-widget/plugin/awformatterhelper.cpp +++ b/sources/awesome-widget/plugin/awformatterhelper.cpp @@ -17,9 +17,11 @@ #include "awformatterhelper.h" +#include + #include +#include #include -#include #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 AWFormatterHelper::items() const +{ + QList 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(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(); + case FormatterClass::Float: + return createItem(); + case FormatterClass::Script: + return createItem(); + case FormatterClass::NoFormat: + return createItem(); + } +} + + +void AWFormatterHelper::initItems() +{ + installDirectories(); + initFormatters(); + initKeys(); +} diff --git a/sources/awesome-widget/plugin/awformatterhelper.h b/sources/awesome-widget/plugin/awformatterhelper.h index 779dd36..952452c 100644 --- a/sources/awesome-widget/plugin/awformatterhelper.h +++ b/sources/awesome-widget/plugin/awformatterhelper.h @@ -19,25 +19,28 @@ #ifndef AWFORMATTERHELPER_H #define AWFORMATTERHELPER_H -#include - -#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 items() const; private: + // methods AWFormatterHelper::FormatterClass defineFormatterClass(const QString stringType) const; void initFormatters(); @@ -45,6 +48,9 @@ private: void installDirectories(); QPair readMetadata(const QString filePath) const; + // parent methods + void doCreateItem(); + void initItems(); // properties QStringList m_directories; QString m_formatterConfig; diff --git a/sources/awesome-widget/plugin/awkeysaggregator.cpp b/sources/awesome-widget/plugin/awkeysaggregator.cpp index 0f5f0c0..735f716 100644 --- a/sources/awesome-widget/plugin/awkeysaggregator.cpp +++ b/sources/awesome-widget/plugin/awkeysaggregator.cpp @@ -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); } diff --git a/sources/awesomewidgets/abstractextitemaggregator.cpp b/sources/awesomewidgets/abstractextitemaggregator.cpp index 69113c7..122ab07 100644 --- a/sources/awesomewidgets/abstractextitemaggregator.cpp +++ b/sources/awesomewidgets/abstractextitemaggregator.cpp @@ -19,15 +19,16 @@ #include +#include #include #include #include -#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 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(button) == copyButton) return copyItem(); else if (static_cast(button) == createButton) - return createItem(); + return doCreateItem(); else if (static_cast(button) == deleteButton) return deleteItem(); else if (dialogButtons->buttonRole(button) == QDialogButtonBox::AcceptRole) diff --git a/sources/awesomewidgets/abstractextitemaggregator.h b/sources/awesomewidgets/abstractextitemaggregator.h index 54724a2..29d0cb8 100644 --- a/sources/awesomewidgets/abstractextitemaggregator.h +++ b/sources/awesomewidgets/abstractextitemaggregator.h @@ -22,19 +22,52 @@ #include #include #include +#include #include +#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 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 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; }; diff --git a/sources/awesome-widget/plugin/awabstractformatter.cpp b/sources/awesomewidgets/awabstractformatter.cpp similarity index 57% rename from sources/awesome-widget/plugin/awabstractformatter.cpp rename to sources/awesomewidgets/awabstractformatter.cpp index 302a00a..e44c240 100644 --- a/sources/awesome-widget/plugin/awabstractformatter.cpp +++ b/sources/awesomewidgets/awabstractformatter.cpp @@ -17,23 +17,16 @@ #include "awabstractformatter.h" -#include #include -#include #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(_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(); diff --git a/sources/awesome-widget/plugin/awabstractformatter.h b/sources/awesomewidgets/awabstractformatter.h similarity index 69% rename from sources/awesome-widget/plugin/awabstractformatter.h rename to sources/awesomewidgets/awabstractformatter.h index d953a25..519d5fb 100644 --- a/sources/awesome-widget/plugin/awabstractformatter.h +++ b/sources/awesomewidgets/awabstractformatter.h @@ -18,47 +18,32 @@ #ifndef AWABSTRACTFORMATTER_H #define AWABSTRACTFORMATTER_H -#include -#include +#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"); }; diff --git a/sources/awesome-widget/plugin/awdatetimeformatter.cpp b/sources/awesomewidgets/awdatetimeformatter.cpp similarity index 94% rename from sources/awesome-widget/plugin/awdatetimeformatter.cpp rename to sources/awesomewidgets/awdatetimeformatter.cpp index 6445fe3..96e28dd 100644 --- a/sources/awesome-widget/plugin/awdatetimeformatter.cpp +++ b/sources/awesomewidgets/awdatetimeformatter.cpp @@ -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(parent()), _fileName); - copyDefaults(item); + AWAbstractFormatter::copyDefaults(item); item->setFormat(format()); + item->setNumber(_number); return item; } diff --git a/sources/awesome-widget/plugin/awdatetimeformatter.h b/sources/awesomewidgets/awdatetimeformatter.h similarity index 96% rename from sources/awesome-widget/plugin/awdatetimeformatter.h rename to sources/awesomewidgets/awdatetimeformatter.h index b0b1bbc..29d2c82 100644 --- a/sources/awesome-widget/plugin/awdatetimeformatter.h +++ b/sources/awesomewidgets/awdatetimeformatter.h @@ -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); diff --git a/sources/awesome-widget/plugin/awdatetimeformatter.ui b/sources/awesomewidgets/awdatetimeformatter.ui similarity index 100% rename from sources/awesome-widget/plugin/awdatetimeformatter.ui rename to sources/awesomewidgets/awdatetimeformatter.ui diff --git a/sources/awesome-widget/plugin/awfloatformatter.cpp b/sources/awesomewidgets/awfloatformatter.cpp similarity index 97% rename from sources/awesome-widget/plugin/awfloatformatter.cpp rename to sources/awesomewidgets/awfloatformatter.cpp index ba2a18f..9b37d29 100644 --- a/sources/awesome-widget/plugin/awfloatformatter.cpp +++ b/sources/awesomewidgets/awfloatformatter.cpp @@ -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(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()); diff --git a/sources/awesome-widget/plugin/awfloatformatter.h b/sources/awesomewidgets/awfloatformatter.h similarity index 97% rename from sources/awesome-widget/plugin/awfloatformatter.h rename to sources/awesomewidgets/awfloatformatter.h index 1a9b28f..921ea70 100644 --- a/sources/awesome-widget/plugin/awfloatformatter.h +++ b/sources/awesomewidgets/awfloatformatter.h @@ -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; diff --git a/sources/awesome-widget/plugin/awfloatformatter.ui b/sources/awesomewidgets/awfloatformatter.ui similarity index 100% rename from sources/awesome-widget/plugin/awfloatformatter.ui rename to sources/awesomewidgets/awfloatformatter.ui diff --git a/sources/awesome-widget/plugin/awnoformatter.cpp b/sources/awesomewidgets/awnoformatter.cpp similarity index 92% rename from sources/awesome-widget/plugin/awnoformatter.cpp rename to sources/awesomewidgets/awnoformatter.cpp index a7903a2..7f727ad 100644 --- a/sources/awesome-widget/plugin/awnoformatter.cpp +++ b/sources/awesomewidgets/awnoformatter.cpp @@ -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(parent()), _fileName); - copyDefaults(item); + AWAbstractFormatter::copyDefaults(item); + item->setNumber(_number); return item; } diff --git a/sources/awesome-widget/plugin/awnoformatter.h b/sources/awesomewidgets/awnoformatter.h similarity index 96% rename from sources/awesome-widget/plugin/awnoformatter.h rename to sources/awesomewidgets/awnoformatter.h index 77b50c7..31121bc 100644 --- a/sources/awesome-widget/plugin/awnoformatter.h +++ b/sources/awesomewidgets/awnoformatter.h @@ -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()); diff --git a/sources/awesome-widget/plugin/awnoformatter.ui b/sources/awesomewidgets/awnoformatter.ui similarity index 100% rename from sources/awesome-widget/plugin/awnoformatter.ui rename to sources/awesomewidgets/awnoformatter.ui diff --git a/sources/awesome-widget/plugin/awscriptformatter.cpp b/sources/awesomewidgets/awscriptformatter.cpp similarity index 96% rename from sources/awesome-widget/plugin/awscriptformatter.cpp rename to sources/awesomewidgets/awscriptformatter.cpp index d2b1eaa..bc7909d 100644 --- a/sources/awesome-widget/plugin/awscriptformatter.cpp +++ b/sources/awesomewidgets/awscriptformatter.cpp @@ -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(parent()), _fileName); - copyDefaults(item); + AWAbstractFormatter::copyDefaults(item); item->setAppendCode(appendCode()); item->setCode(code()); item->setHasReturn(hasReturn()); + item->setNumber(_number); return item; } diff --git a/sources/awesome-widget/plugin/awscriptformatter.h b/sources/awesomewidgets/awscriptformatter.h similarity index 97% rename from sources/awesome-widget/plugin/awscriptformatter.h rename to sources/awesomewidgets/awscriptformatter.h index 3939917..0b8d786 100644 --- a/sources/awesome-widget/plugin/awscriptformatter.h +++ b/sources/awesomewidgets/awscriptformatter.h @@ -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; diff --git a/sources/awesome-widget/plugin/awscriptformatter.ui b/sources/awesomewidgets/awscriptformatter.ui similarity index 100% rename from sources/awesome-widget/plugin/awscriptformatter.ui rename to sources/awesomewidgets/awscriptformatter.ui diff --git a/sources/awesomewidgets/extitemaggregator.h b/sources/awesomewidgets/extitemaggregator.h index 740d018..5527d07 100644 --- a/sources/awesomewidgets/extitemaggregator.h +++ b/sources/awesomewidgets/extitemaggregator.h @@ -32,8 +32,7 @@ template 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(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(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 items() const { return m_items; }; - - int uniqNumber() const - { - QList tagList; - for (auto item : m_items) - tagList.append(item->number()); - int number = 0; - while (tagList.contains(number)) - number++; - - return number; - }; + QList items() const { return m_items; }; private: - QList m_items; + QList m_items; QList m_activeItems; - QString m_type; - QList getItems() + void doCreateItem() { return createItem(); } + + QList 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 items; + QList 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(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(item)); } }; };