File indexing completed on 2025-01-05 05:14:47

0001 /*
0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com>
0003 
0004 SPDX-License-Identifier: GPL-3.0-or-later
0005 */
0006 
0007 #include "abstractactions.h"
0008 
0009 #include "gitmanager.h"
0010 
0011 #include <KLocalizedString>
0012 #include <QMenu>
0013 #include <QWidget>
0014 
0015 void AbstractActions::setActionEnabled(QAction *action, bool enabled)
0016 {
0017     if (mGit->isValid()) {
0018         action->setEnabled(enabled);
0019     } else {
0020         action->setEnabled(false);
0021         mActionStatuses.insert(action, enabled);
0022     }
0023 }
0024 
0025 AbstractActions::AbstractActions(Git::Manager *git, QWidget *parent)
0026     : QObject{parent}
0027     , mGit{git}
0028     , mParent{parent}
0029 {
0030     mMenu = new QMenu(parent);
0031     connect(git, &Git::Manager::pathChanged, this, &AbstractActions::git_reloaded);
0032 }
0033 
0034 void AbstractActions::popup()
0035 {
0036     mMenu->popup(QCursor::pos());
0037 }
0038 
0039 void AbstractActions::popup(const QPoint &pos)
0040 {
0041     mMenu->popup(pos);
0042 }
0043 
0044 void AbstractActions::git_reloaded()
0045 {
0046     if (!mGit->isValid()) {
0047         for (auto &a : std::as_const(mActions))
0048             a->setEnabled(false);
0049         mActionStatuses.clear();
0050         return;
0051     }
0052 
0053     for (auto i = mActionStatuses.begin(); i != mActionStatuses.end(); ++i) {
0054         i.key()->setEnabled(i.value());
0055     }
0056 
0057     mActionStatuses.clear();
0058 }
0059 
0060 QAction *AbstractActions::createAction(const QString &text, bool enabled, bool addToMenu)
0061 {
0062     auto a = new QAction(this);
0063     a->setText(text);
0064     setActionEnabled(a, enabled);
0065     if (addToMenu)
0066         mMenu->addAction(a);
0067     mActions.append(a);
0068     return a;
0069 }
0070 
0071 #include "moc_abstractactions.cpp"