mirror of
https://github.com/arcan1s/awesome-widgets.git
synced 2025-04-24 15:37:23 +00:00
222 lines
6.6 KiB
C++
222 lines
6.6 KiB
C++
/***************************************************************************
|
|
* 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 "abstractextitemaggregator.h"
|
|
#include "ui_abstractextitemaggregator.h"
|
|
|
|
#include <KI18n/KLocalizedString>
|
|
|
|
#include <QDir>
|
|
#include <QInputDialog>
|
|
#include <QPushButton>
|
|
#include <utility>
|
|
|
|
|
|
AbstractExtItemAggregator::AbstractExtItemAggregator(QWidget *_parent, QString _type)
|
|
: QDialog(_parent)
|
|
, ui(new Ui::AbstractExtItemAggregator)
|
|
, m_type(std::move(_type))
|
|
{
|
|
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
|
|
|
// create directory at $HOME
|
|
auto localDir = QString("%1/awesomewidgets/%2")
|
|
.arg(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation), type());
|
|
QDir localDirectory;
|
|
if (localDirectory.mkpath(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()
|
|
{
|
|
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
|
|
|
|
delete ui;
|
|
}
|
|
|
|
|
|
void AbstractExtItemAggregator::copyItem()
|
|
{
|
|
auto source = itemFromWidget();
|
|
auto fileName = getName();
|
|
auto number = uniqNumber();
|
|
auto dir = QString("%1/awesomewidgets/%2")
|
|
.arg(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation), m_type);
|
|
if ((!source) || (fileName.isEmpty())) {
|
|
qCWarning(LOG_LIB) << "Nothing to copy";
|
|
return;
|
|
}
|
|
auto filePath = QString("%1/%2").arg(dir, fileName);
|
|
|
|
auto newItem = source->copy(filePath, number);
|
|
if (newItem->showConfiguration(this, configArgs()) == 1) {
|
|
initItems();
|
|
repaintList();
|
|
}
|
|
}
|
|
|
|
|
|
void AbstractExtItemAggregator::deleteItem()
|
|
{
|
|
auto source = itemFromWidget();
|
|
if (!source) {
|
|
qCWarning(LOG_LIB) << "Nothing to delete";
|
|
return;
|
|
}
|
|
|
|
if (source->tryDelete()) {
|
|
initItems();
|
|
repaintList();
|
|
}
|
|
}
|
|
|
|
|
|
void AbstractExtItemAggregator::editItem()
|
|
{
|
|
auto source = itemFromWidget();
|
|
if (!source) {
|
|
qCWarning(LOG_LIB) << "Nothing to edit";
|
|
return;
|
|
}
|
|
|
|
if (source->showConfiguration(this, configArgs()) == 1) {
|
|
initItems();
|
|
repaintList();
|
|
}
|
|
}
|
|
|
|
|
|
QString AbstractExtItemAggregator::getName()
|
|
{
|
|
bool ok;
|
|
auto name = QInputDialog::getText(this, i18n("Enter file name"), i18n("File name"), QLineEdit::Normal, "", &ok);
|
|
if ((!ok) || (name.isEmpty()))
|
|
return "";
|
|
if (!name.endsWith(".desktop"))
|
|
name += ".desktop";
|
|
|
|
return name;
|
|
}
|
|
|
|
|
|
AbstractExtItem *AbstractExtItemAggregator::itemFromWidget() const
|
|
{
|
|
auto widgetItem = ui->listWidget->currentItem();
|
|
if (!widgetItem)
|
|
return nullptr;
|
|
|
|
AbstractExtItem *found = nullptr;
|
|
for (auto &item : items()) {
|
|
auto fileName = QFileInfo(item->fileName()).fileName();
|
|
if (fileName != widgetItem->text())
|
|
continue;
|
|
found = item;
|
|
break;
|
|
}
|
|
if (!found)
|
|
qCWarning(LOG_LIB) << "Could not find item by name" << widgetItem->text();
|
|
|
|
return found;
|
|
}
|
|
|
|
|
|
void AbstractExtItemAggregator::repaintList() const
|
|
{
|
|
ui->listWidget->clear();
|
|
for (auto &_item : items()) {
|
|
QString fileName = QFileInfo(_item->fileName()).fileName();
|
|
auto item = new QListWidgetItem(fileName, ui->listWidget);
|
|
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('\n'));
|
|
ui->listWidget->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;
|
|
}
|
|
|
|
|
|
QStringList AbstractExtItemAggregator::directories() const
|
|
{
|
|
auto dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QString("awesomewidgets/%1").arg(type()),
|
|
QStandardPaths::LocateDirectory);
|
|
|
|
return dirs;
|
|
}
|
|
|
|
|
|
QString AbstractExtItemAggregator::type() const
|
|
{
|
|
return m_type;
|
|
}
|
|
|
|
|
|
void AbstractExtItemAggregator::setConfigArgs(const QVariant &_configArgs)
|
|
{
|
|
qCDebug(LOG_LIB) << "Configuration arguments" << _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();
|
|
}
|