thedesk/libthedesk/actionquickwidget.cpp
2021-04-02 22:56:12 +11:00

120 lines
3.8 KiB
C++

/****************************************
*
* INSERT-PROJECT-NAME-HERE - INSERT-GENERIC-NAME-HERE
* Copyright (C) 2020 Victor Tran
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* *************************************/
#include "actionquickwidget.h"
#include "ui_actionquickwidget.h"
#include <QPushButton>
#include <QActionEvent>
#include <QAction>
#include "private/quickwidgetcontainer.h"
#include "chunk.h"
struct ActionQuickWidgetPrivate {
Chunk* parentChunk = nullptr;
QuickWidgetContainer* parentQuickWidget = nullptr;
QMap<QAction*, QWidget*> buttons;
};
ActionQuickWidget::ActionQuickWidget(Chunk* parent) :
QWidget(parent),
ui(new Ui::ActionQuickWidget) {
ui->setupUi(this);
d = new ActionQuickWidgetPrivate();
d->parentChunk = parent;
}
ActionQuickWidget::ActionQuickWidget(QuickWidgetContainer* parent) :
QWidget(parent),
ui(new Ui::ActionQuickWidget) {
ui->setupUi(this);
d = new ActionQuickWidgetPrivate();
d->parentQuickWidget = parent;
}
ActionQuickWidget::~ActionQuickWidget() {
delete ui;
delete d;
}
void ActionQuickWidget::addAction(QAction* action) {
QWidget::addAction(action);
}
QAction* ActionQuickWidget::addAction(QString text, std::function<void ()> triggered) {
QAction* action = new QAction(text, this);
connect(action, &QAction::triggered, this, triggered);
QWidget::addAction(action);
return action;
}
QAction* ActionQuickWidget::addAction(QIcon icon, QString text, std::function<void ()> triggered) {
QAction* action = new QAction(icon, text, this);
connect(action, &QAction::triggered, this, triggered);
QWidget::addAction(action);
return action;
}
bool ActionQuickWidget::event(QEvent* event) {
QActionEvent* e = static_cast<QActionEvent*>(event);
switch (event->type()) {
case QEvent::ActionAdded: {
QAction* action = e->action();
QPushButton* button = new QPushButton();
button->setText(action->text());
button->setIcon(action->icon());
button->setEnabled(action->isEnabled());
button->setVisible(action->isVisible());
connect(button, &QPushButton::clicked, this, [ = ] {
if (d->parentChunk) {
d->parentChunk->hideQuickWidget();
} else {
d->parentQuickWidget->hideContainer();
}
action->trigger();
});
connect(action, &QAction::changed, this, [ = ] {
button->setText(action->text());
button->setIcon(action->icon());
button->setEnabled(action->isEnabled());
button->setVisible(action->isVisible());
});
d->buttons.insert(action, button);
ui->actionsLayout->addWidget(button);
break;
}
case QEvent::ActionChanged:
break;
case QEvent::ActionRemoved: {
QWidget* button = d->buttons.value(e->action());
ui->actionsLayout->removeWidget(button);
button->deleteLater();
d->buttons.remove(e->action());
break;
}
default:
break;
}
return QWidget::event(event);
}