File indexing completed on 2024-05-12 17:12:40

0001 /***************************************************************************
0002  *   Copyright (C) 2017 by Renaud Guezennec                                *
0003  *   https://rolisteam.org/contact                   *
0004  *                                                                         *
0005  *   rolisteam is free software; you can redistribute it and/or modify     *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #include "shortcutvisitor.h"
0021 #include "data/shortcutmodel.h"
0022 
0023 #include <QAction>
0024 #include <QList>
0025 #include <QWidget>
0026 
0027 ///////////////////////// CPP
0028 ShortcutVisitor::ShortcutVisitor(QObject* parent) : QObject(parent), m_model(new ShortCutModel()) {}
0029 
0030 ShortcutVisitor::~ShortcutVisitor() {}
0031 
0032 QAbstractItemModel* ShortcutVisitor::getModel() const
0033 {
0034     return m_model;
0035 }
0036 
0037 bool ShortcutVisitor::registerWidget(QWidget* widget, const QString& categoryName, bool recursion)
0038 {
0039     if(m_categoriesNames.contains(widget))
0040         return false; // widget already registered
0041 
0042     m_categoriesNames.append(widget, categoryName);
0043 
0044     visit(widget, categoryName, recursion);
0045     connect(widget, &QWidget::destroyed, this, &ShortcutVisitor::objectDeleted);
0046     return true;
0047 }
0048 
0049 bool ShortcutVisitor::unregisterWidget(QWidget* widget)
0050 {
0051     if(!m_categoriesNames.contains(widget))
0052         return false;
0053 
0054     const QString& category= m_categoriesNames[widget];
0055 
0056     if(category.isEmpty())
0057         return false;
0058 
0059     disconnect(widget, &QWidget::destroyed, this, &ShortcutVisitor::objectDeleted);
0060     m_model->removeCategory(category, true);
0061     return true;
0062 }
0063 
0064 void ShortcutVisitor::objectDeleted(QObject* obj)
0065 {
0066     QWidget* widget= qobject_cast<QWidget*>(obj);
0067     if(nullptr != widget)
0068     {
0069         unregisterWidget(widget);
0070     }
0071 }
0072 
0073 void ShortcutVisitor::visit(QWidget* widget, const QString& category, bool recursion)
0074 {
0075     QList<QAction*> actions= widget->actions();
0076     if(!actions.isEmpty())
0077     {
0078         m_model->addCategory(category);
0079     }
0080     for(QAction* action : actions)
0081     {
0082         // if(!action->shortcut().isEmpty())
0083         {
0084             QString actionName= action->text();
0085             actionName.remove('&');
0086             m_model->insertShortCut(category, actionName, action->shortcut().toString(QKeySequence::NativeText));
0087         }
0088     }
0089 
0090     if(recursion)
0091     {
0092         QObjectList children= widget->children();
0093         for(QObject* obj : children)
0094         {
0095             QWidget* wd= qobject_cast<QWidget*>(obj);
0096             if(nullptr != wd)
0097             {
0098                 visit(wd, wd->objectName(), recursion);
0099             }
0100         }
0101     }
0102 }