"Fossies" - the Fresh Open Source Software Archive

Member "scidavis-2.3.0/libscidavis/src/future/lib/ActionManager.cpp" (12 Jun 2020, 4814 Bytes) of package /linux/misc/scidavis-2.3.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ActionManager.cpp" see the Fossies "Dox" file reference documentation.

    1 /***************************************************************************
    2     File                 : ActionManager.cpp
    3     Project              : SciDAVis
    4     Description          : Manages QActions and their shortcuts
    5     --------------------------------------------------------------------
    6     Copyright            : (C) 2008-2009 Tilman Benkert (thzs*gmx.net)
    7                            (replace * with @ in the email addresses) 
    8                            
    9  ***************************************************************************/
   10 
   11 /***************************************************************************
   12  *                                                                         *
   13  *  This program is free software; you can redistribute it and/or modify   *
   14  *  it under the terms of the GNU General Public License as published by   *
   15  *  the Free Software Foundation; either version 2 of the License, or      *
   16  *  (at your option) any later version.                                    *
   17  *                                                                         *
   18  *  This program is distributed in the hope that it will be useful,        *
   19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
   20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
   21  *  GNU General Public License for more details.                           *
   22  *                                                                         *
   23  *   You should have received a copy of the GNU General Public License     *
   24  *   along with this program; if not, write to the Free Software           *
   25  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
   26  *   Boston, MA  02110-1301  USA                                           *
   27  *                                                                         *
   28  ***************************************************************************/
   29 
   30 #include "ActionManager.h"
   31 #include <QMutableMapIterator>
   32 
   33 ActionManager::ActionManager()
   34 {
   35 }
   36 
   37 ActionManager::~ActionManager()
   38 {
   39     QMutableMapIterator<QString, QList<QAction *> * > it(d_action_registry);
   40     while (it.hasNext())
   41         delete it.next().value();
   42 }
   43 
   44 void ActionManager::addAction(QAction * action, const QString& internal_name)
   45 {
   46     if (!action)
   47         return;
   48     if (!d_action_registry.contains(internal_name)) // new internal name
   49     {
   50         QList<QAction *> * list = new QList<QAction *>();
   51         list->append(action);
   52         d_action_registry.insert(internal_name, list);
   53         d_action_shortcuts.insert(internal_name, action->shortcuts());
   54         connect(action, SIGNAL(destroyed(QObject *)), this, SLOT(removeAction(QObject *)));
   55     }
   56     else
   57     {
   58         QList<QAction *> * list = d_action_registry.value(internal_name);
   59         if (!list->contains(action))
   60         {
   61             list->append(action);
   62             connect(action, SIGNAL(destroyed(QObject *)), this, SLOT(removeAction(QObject *)));
   63         }
   64         QList<QKeySequence> sequences = d_action_shortcuts.value(internal_name);
   65         action->setShortcuts(sequences);
   66     }
   67     d_action_texts.insert(internal_name, action->text());
   68 }
   69 
   70 QList<QKeySequence> ActionManager::shortcuts(const QString& internal_name) const
   71 {
   72     if(!d_action_shortcuts.contains(internal_name))
   73         return QList<QKeySequence>();
   74 
   75     return d_action_shortcuts.value(internal_name);
   76 }
   77 
   78 void ActionManager::setShortcuts(const QString& internal_name, const QList<QKeySequence>& sequences)
   79 {
   80     if(!d_action_registry.contains(internal_name)) // new internal name
   81     {
   82         QList<QAction *> * list = new QList<QAction *>();
   83         d_action_registry.insert(internal_name, list);
   84         d_action_shortcuts.insert(internal_name, sequences);
   85     }
   86     else
   87     {
   88         QList<QAction *> * list = d_action_registry.value(internal_name);
   89         foreach(QAction * action, *list)
   90             action->setShortcuts(sequences);
   91         d_action_shortcuts.insert(internal_name, sequences); 
   92     }
   93 }
   94 
   95 QString ActionManager::actionText(const QString& internal_name) const
   96 {
   97     if(!d_action_registry.contains(internal_name))
   98         return QString();
   99 
  100     QList<QAction *> * list = d_action_registry.value(internal_name);
  101     if (list->isEmpty())
  102     {
  103         if (d_action_texts.contains(internal_name))
  104             return d_action_texts.value(internal_name);
  105         else
  106             return internal_name;
  107     }
  108     return list->last()->text();
  109 }
  110 
  111 QList<QString> ActionManager::internalNames() const
  112 {
  113     return d_action_registry.keys();
  114 }
  115 
  116 void ActionManager::removeAction(QObject * action)
  117 {
  118     removeAction(static_cast<QAction *>(action));
  119 }
  120 
  121 void ActionManager::removeAction(QAction * action)
  122 {
  123     if(!action)
  124         return;
  125 
  126     disconnect(action, SIGNAL(destroyed(QObject *)), this, SLOT(removeAction(QObject *)));
  127     
  128     QMutableMapIterator<QString, QList<QAction *> * > it(d_action_registry);
  129     while (it.hasNext())
  130     {
  131         it.next();
  132         if (it.value()->contains(action))
  133         {
  134             d_action_texts.insert(it.key(), action->text());
  135             it.value()->removeAll(action);
  136         }
  137     }
  138 }
  139