mirror of
https://github.com/arcan1s/awesome-widgets.git
synced 2025-05-04 12:23:49 +00:00
do not derive from qwidget in aggregates
This commit is contained in:
parent
33a41bb6c0
commit
0a15a098a2
@ -26,9 +26,8 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
|
||||||
AbstractExtItemAggregator::AbstractExtItemAggregator(QWidget *_parent, QString _type)
|
AbstractExtItemAggregator::AbstractExtItemAggregator(QObject *_parent, QString _type)
|
||||||
: QDialog(_parent)
|
: QObject(_parent)
|
||||||
, ui(new Ui::AbstractExtItemAggregator)
|
|
||||||
, m_type(std::move(_type))
|
, m_type(std::move(_type))
|
||||||
{
|
{
|
||||||
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
||||||
@ -39,29 +38,12 @@ AbstractExtItemAggregator::AbstractExtItemAggregator(QWidget *_parent, QString _
|
|||||||
QDir localDirectory;
|
QDir localDirectory;
|
||||||
if (localDirectory.mkpath(localDir))
|
if (localDirectory.mkpath(localDir))
|
||||||
qCInfo(LOG_LIB) << "Created directory" << localDir;
|
qCInfo(LOG_LIB) << "Created directory" << localDir;
|
||||||
|
|
||||||
ui->setupUi(this);
|
|
||||||
copyButton = ui->buttonBox->addButton(i18n("Copy"), QDialogButtonBox::ActionRole);
|
|
||||||
createButton = ui->buttonBox->addButton(i18n("Create"), QDialogButtonBox::ActionRole);
|
|
||||||
deleteButton = ui->buttonBox->addButton(i18n("Remove"), QDialogButtonBox::ActionRole);
|
|
||||||
|
|
||||||
connect(ui->buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(editItemButtonPressed(QAbstractButton *)));
|
|
||||||
connect(ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
|
|
||||||
connect(ui->listWidget, SIGNAL(itemActivated(QListWidgetItem *)), this, SLOT(editItemActivated(QListWidgetItem *)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
AbstractExtItemAggregator::~AbstractExtItemAggregator()
|
void AbstractExtItemAggregator::copyItem(QListWidget *_widget)
|
||||||
{
|
{
|
||||||
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
auto source = itemFromWidget(_widget);
|
||||||
|
|
||||||
delete ui;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void AbstractExtItemAggregator::copyItem()
|
|
||||||
{
|
|
||||||
auto source = itemFromWidget();
|
|
||||||
auto fileName = getName();
|
auto fileName = getName();
|
||||||
auto number = uniqNumber();
|
auto number = uniqNumber();
|
||||||
auto dir = QString("%1/awesomewidgets/%2")
|
auto dir = QString("%1/awesomewidgets/%2")
|
||||||
@ -73,16 +55,16 @@ void AbstractExtItemAggregator::copyItem()
|
|||||||
auto filePath = QString("%1/%2").arg(dir, fileName);
|
auto filePath = QString("%1/%2").arg(dir, fileName);
|
||||||
|
|
||||||
auto newItem = source->copy(filePath, number);
|
auto newItem = source->copy(filePath, number);
|
||||||
if (newItem->showConfiguration(this, configArgs()) == 1) {
|
if (newItem->showConfiguration(nullptr, configArgs()) == 1) {
|
||||||
initItems();
|
initItems();
|
||||||
repaintList();
|
repaintList(_widget);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void AbstractExtItemAggregator::deleteItem()
|
void AbstractExtItemAggregator::deleteItem(QListWidget *_widget)
|
||||||
{
|
{
|
||||||
auto source = itemFromWidget();
|
auto source = itemFromWidget(_widget);
|
||||||
if (!source) {
|
if (!source) {
|
||||||
qCWarning(LOG_LIB) << "Nothing to delete";
|
qCWarning(LOG_LIB) << "Nothing to delete";
|
||||||
return;
|
return;
|
||||||
@ -90,30 +72,63 @@ void AbstractExtItemAggregator::deleteItem()
|
|||||||
|
|
||||||
if (source->tryDelete()) {
|
if (source->tryDelete()) {
|
||||||
initItems();
|
initItems();
|
||||||
repaintList();
|
repaintList(_widget);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void AbstractExtItemAggregator::editItem()
|
void AbstractExtItemAggregator::editItem(QListWidget *_widget)
|
||||||
{
|
{
|
||||||
auto source = itemFromWidget();
|
auto source = itemFromWidget(_widget);
|
||||||
if (!source) {
|
if (!source) {
|
||||||
qCWarning(LOG_LIB) << "Nothing to edit";
|
qCWarning(LOG_LIB) << "Nothing to edit";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (source->showConfiguration(this, configArgs()) == 1) {
|
if (source->showConfiguration(nullptr, configArgs()) == 1) {
|
||||||
initItems();
|
initItems();
|
||||||
repaintList();
|
repaintList(_widget);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int AbstractExtItemAggregator::exec()
|
||||||
|
{
|
||||||
|
auto dialog = new QDialog();
|
||||||
|
auto ui = new Ui::AbstractExtItemAggregator();
|
||||||
|
ui->setupUi(dialog);
|
||||||
|
|
||||||
|
auto copyButton = ui->buttonBox->addButton(i18n("Copy"), QDialogButtonBox::ActionRole);
|
||||||
|
auto createButton = ui->buttonBox->addButton(i18n("Create"), QDialogButtonBox::ActionRole);
|
||||||
|
auto deleteButton = ui->buttonBox->addButton(i18n("Remove"), QDialogButtonBox::ActionRole);
|
||||||
|
|
||||||
|
connect(ui->buttonBox, &QDialogButtonBox::clicked, [&](QAbstractButton *_button) {
|
||||||
|
if (dynamic_cast<QPushButton *>(_button) == copyButton)
|
||||||
|
copyItem(ui->listWidget);
|
||||||
|
else if (dynamic_cast<QPushButton *>(_button) == createButton)
|
||||||
|
doCreateItem(ui->listWidget);
|
||||||
|
else if (dynamic_cast<QPushButton *>(_button) == deleteButton)
|
||||||
|
deleteItem(ui->listWidget);
|
||||||
|
else if (ui->buttonBox->buttonRole(_button) == QDialogButtonBox::AcceptRole)
|
||||||
|
editItem(ui->listWidget);
|
||||||
|
});
|
||||||
|
connect(ui->buttonBox, &QDialogButtonBox::rejected, [dialog]() { dialog->reject(); });
|
||||||
|
connect(ui->listWidget, &QListWidget::itemActivated, [&](QListWidgetItem *) { editItem(ui->listWidget); });
|
||||||
|
|
||||||
|
repaintList(ui->listWidget);
|
||||||
|
auto ret = dialog->exec();
|
||||||
|
|
||||||
|
dialog->deleteLater();
|
||||||
|
delete ui;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
QString AbstractExtItemAggregator::getName()
|
QString AbstractExtItemAggregator::getName()
|
||||||
{
|
{
|
||||||
bool ok;
|
bool ok;
|
||||||
auto name = QInputDialog::getText(this, i18n("Enter file name"), i18n("File name"), QLineEdit::Normal, "", &ok);
|
auto name = QInputDialog::getText(nullptr, i18n("Enter file name"), i18n("File name"), QLineEdit::Normal, "", &ok);
|
||||||
if ((!ok) || (name.isEmpty()))
|
if ((!ok) || (name.isEmpty()))
|
||||||
return "";
|
return "";
|
||||||
if (!name.endsWith(".desktop"))
|
if (!name.endsWith(".desktop"))
|
||||||
@ -123,9 +138,9 @@ QString AbstractExtItemAggregator::getName()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
AbstractExtItem *AbstractExtItemAggregator::itemFromWidget() const
|
AbstractExtItem *AbstractExtItemAggregator::itemFromWidget(QListWidget *_widget) const
|
||||||
{
|
{
|
||||||
auto widgetItem = ui->listWidget->currentItem();
|
auto widgetItem = _widget->currentItem();
|
||||||
if (!widgetItem)
|
if (!widgetItem)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
@ -144,18 +159,18 @@ AbstractExtItem *AbstractExtItemAggregator::itemFromWidget() const
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void AbstractExtItemAggregator::repaintList() const
|
void AbstractExtItemAggregator::repaintList(QListWidget *_widget) const
|
||||||
{
|
{
|
||||||
ui->listWidget->clear();
|
_widget->clear();
|
||||||
for (auto &_item : items()) {
|
for (auto &_item : items()) {
|
||||||
QString fileName = QFileInfo(_item->fileName()).fileName();
|
QString fileName = QFileInfo(_item->fileName()).fileName();
|
||||||
auto item = new QListWidgetItem(fileName, ui->listWidget);
|
auto item = new QListWidgetItem(fileName, _widget);
|
||||||
QStringList tooltip;
|
QStringList tooltip;
|
||||||
tooltip.append(i18n("Name: %1", _item->name()));
|
tooltip.append(i18n("Name: %1", _item->name()));
|
||||||
tooltip.append(i18n("Comment: %1", _item->comment()));
|
tooltip.append(i18n("Comment: %1", _item->comment()));
|
||||||
tooltip.append(i18n("Identity: %1", _item->uniq()));
|
tooltip.append(i18n("Identity: %1", _item->uniq()));
|
||||||
item->setToolTip(tooltip.join('\n'));
|
item->setToolTip(tooltip.join('\n'));
|
||||||
ui->listWidget->addItem(item);
|
_widget->addItem(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -200,22 +215,3 @@ void AbstractExtItemAggregator::setConfigArgs(const QVariant &_configArgs)
|
|||||||
|
|
||||||
m_configArgs = _configArgs;
|
m_configArgs = _configArgs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void AbstractExtItemAggregator::editItemActivated(QListWidgetItem *)
|
|
||||||
{
|
|
||||||
return editItem();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void AbstractExtItemAggregator::editItemButtonPressed(QAbstractButton *_button)
|
|
||||||
{
|
|
||||||
if (dynamic_cast<QPushButton *>(_button) == copyButton)
|
|
||||||
return copyItem();
|
|
||||||
else if (dynamic_cast<QPushButton *>(_button) == createButton)
|
|
||||||
return doCreateItem();
|
|
||||||
else if (dynamic_cast<QPushButton *>(_button) == deleteButton)
|
|
||||||
return deleteItem();
|
|
||||||
else if (ui->buttonBox->buttonRole(_button) == QDialogButtonBox::AcceptRole)
|
|
||||||
return editItem();
|
|
||||||
}
|
|
||||||
|
@ -18,7 +18,6 @@
|
|||||||
#ifndef ABSTRACTEXTITEMAGGREGATOR_H
|
#ifndef ABSTRACTEXTITEMAGGREGATOR_H
|
||||||
#define ABSTRACTEXTITEMAGGREGATOR_H
|
#define ABSTRACTEXTITEMAGGREGATOR_H
|
||||||
|
|
||||||
#include <QDialog>
|
|
||||||
#include <QStandardPaths>
|
#include <QStandardPaths>
|
||||||
|
|
||||||
#include "abstractextitem.h"
|
#include "abstractextitem.h"
|
||||||
@ -26,27 +25,23 @@
|
|||||||
|
|
||||||
|
|
||||||
class QAbstractButton;
|
class QAbstractButton;
|
||||||
|
class QListWidget;
|
||||||
class QListWidgetItem;
|
class QListWidgetItem;
|
||||||
namespace Ui
|
|
||||||
{
|
|
||||||
class AbstractExtItemAggregator;
|
|
||||||
}
|
|
||||||
|
|
||||||
class AbstractExtItemAggregator : public QDialog
|
class AbstractExtItemAggregator : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
Q_PROPERTY(QVariant configArgs READ configArgs WRITE setConfigArgs)
|
Q_PROPERTY(QVariant configArgs READ configArgs WRITE setConfigArgs)
|
||||||
Q_PROPERTY(QVariant type READ type)
|
Q_PROPERTY(QVariant type READ type)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit AbstractExtItemAggregator(QWidget *_parent, QString _type);
|
explicit AbstractExtItemAggregator(QObject *_parent, QString _type);
|
||||||
~AbstractExtItemAggregator() override;
|
|
||||||
// methods
|
// methods
|
||||||
void copyItem();
|
void copyItem(QListWidget *_widget);
|
||||||
template <class T> void createItem()
|
template <class T> void createItem(QListWidget *_widget)
|
||||||
{
|
{
|
||||||
auto fileName = getName();
|
auto fileName = getName();
|
||||||
int number = uniqNumber();
|
auto number = uniqNumber();
|
||||||
auto dir = QString("%1/awesomewidgets/%2")
|
auto dir = QString("%1/awesomewidgets/%2")
|
||||||
.arg(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation), m_type);
|
.arg(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation), m_type);
|
||||||
if (fileName.isEmpty()) {
|
if (fileName.isEmpty()) {
|
||||||
@ -55,19 +50,20 @@ public:
|
|||||||
}
|
}
|
||||||
auto filePath = QString("%1/%2").arg(dir, fileName);
|
auto filePath = QString("%1/%2").arg(dir, fileName);
|
||||||
|
|
||||||
T *newItem = new T(this, filePath);
|
auto newItem = new T(this, filePath);
|
||||||
newItem->setNumber(number);
|
newItem->setNumber(number);
|
||||||
if (newItem->showConfiguration(this, configArgs()) == 1) {
|
if (newItem->showConfiguration(nullptr, configArgs()) == 1) {
|
||||||
initItems();
|
initItems();
|
||||||
repaintList();
|
repaintList(_widget);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
void deleteItem();
|
void deleteItem(QListWidget *_widget);
|
||||||
void editItem();
|
void editItem(QListWidget *_widget);
|
||||||
|
[[nodiscard]] int exec();
|
||||||
QString getName();
|
QString getName();
|
||||||
virtual void initItems() = 0;
|
virtual void initItems() = 0;
|
||||||
[[nodiscard]] AbstractExtItem *itemFromWidget() const;
|
[[nodiscard]] AbstractExtItem *itemFromWidget(QListWidget *_widget) const;
|
||||||
void repaintList() const;
|
void repaintList(QListWidget *_widget) const;
|
||||||
[[nodiscard]] int uniqNumber() const;
|
[[nodiscard]] int uniqNumber() const;
|
||||||
// get methods
|
// get methods
|
||||||
[[nodiscard]] QVariant configArgs() const;
|
[[nodiscard]] QVariant configArgs() const;
|
||||||
@ -77,21 +73,12 @@ public:
|
|||||||
// set methods
|
// set methods
|
||||||
void setConfigArgs(const QVariant &_configArgs);
|
void setConfigArgs(const QVariant &_configArgs);
|
||||||
|
|
||||||
private slots:
|
|
||||||
void editItemActivated(QListWidgetItem *);
|
|
||||||
void editItemButtonPressed(QAbstractButton *_button);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// ui
|
|
||||||
Ui::AbstractExtItemAggregator *ui = nullptr;
|
|
||||||
QPushButton *copyButton = nullptr;
|
|
||||||
QPushButton *createButton = nullptr;
|
|
||||||
QPushButton *deleteButton = nullptr;
|
|
||||||
// properties
|
// properties
|
||||||
QVariant m_configArgs;
|
QVariant m_configArgs;
|
||||||
QString m_type;
|
QString m_type;
|
||||||
// ui methods
|
// ui methods
|
||||||
virtual void doCreateItem() = 0;
|
virtual void doCreateItem(QListWidget *_widget) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@
|
|||||||
template <class T> class ExtItemAggregator : public AbstractExtItemAggregator
|
template <class T> class ExtItemAggregator : public AbstractExtItemAggregator
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit ExtItemAggregator(QWidget *_parent, const QString &_type)
|
explicit ExtItemAggregator(QObject *_parent, const QString &_type)
|
||||||
: AbstractExtItemAggregator(_parent, _type)
|
: AbstractExtItemAggregator(_parent, _type)
|
||||||
{
|
{
|
||||||
qSetMessagePattern(AWDebug::LOG_FORMAT);
|
qSetMessagePattern(AWDebug::LOG_FORMAT);
|
||||||
@ -56,8 +56,7 @@ public:
|
|||||||
|
|
||||||
void editItems()
|
void editItems()
|
||||||
{
|
{
|
||||||
repaintList();
|
auto ret = exec();
|
||||||
int ret = exec();
|
|
||||||
qCInfo(LOG_LIB) << "Dialog returns" << ret;
|
qCInfo(LOG_LIB) << "Dialog returns" << ret;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -123,7 +122,7 @@ private:
|
|||||||
QList<AbstractExtItem *> m_items;
|
QList<AbstractExtItem *> m_items;
|
||||||
QList<T *> m_activeItems;
|
QList<T *> m_activeItems;
|
||||||
|
|
||||||
void doCreateItem() override { return createItem<T>(); }
|
void doCreateItem(QListWidget *_widget) override { return createItem<T>(_widget); }
|
||||||
|
|
||||||
QList<AbstractExtItem *> getItems()
|
QList<AbstractExtItem *> getItems()
|
||||||
{
|
{
|
||||||
@ -137,7 +136,7 @@ private:
|
|||||||
if (!file.endsWith(".desktop"))
|
if (!file.endsWith(".desktop"))
|
||||||
continue;
|
continue;
|
||||||
qCInfo(LOG_LIB) << "Found file" << file << "in" << dir;
|
qCInfo(LOG_LIB) << "Found file" << file << "in" << dir;
|
||||||
QString filePath = QString("%1/%2").arg(dir).arg(file);
|
auto filePath = QString("%1/%2").arg(dir, file);
|
||||||
// check if already exists
|
// check if already exists
|
||||||
if (std::any_of(items.cbegin(), items.cend(),
|
if (std::any_of(items.cbegin(), items.cend(),
|
||||||
[&filePath](AbstractExtItem *item) { return (item->fileName() == filePath); }))
|
[&filePath](AbstractExtItem *item) { return (item->fileName() == filePath); }))
|
||||||
@ -147,8 +146,7 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// sort items
|
// sort items
|
||||||
std::sort(items.begin(), items.end(),
|
std::sort(items.begin(), items.end(), [](auto *lhs, auto *rhs) { return lhs->number() < rhs->number(); });
|
||||||
[](const AbstractExtItem *lhs, const AbstractExtItem *rhs) { return lhs->number() < rhs->number(); });
|
|
||||||
return items;
|
return items;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user