File indexing completed on 2024-05-12 16:39:41

0001 /* This file is part of the KDE project
0002    Copyright (C) 2007 Jarosław Staniek <staniek@kde.org>
0003 
0004    This program is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This program is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this program; see the file COPYING.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "KexiStandardAction.h"
0021 
0022 #include <KexiIcon.h>
0023 
0024 #include <KActionCollection>
0025 #include <KLocalizedString>
0026 
0027 #include <QHash>
0028 #include <QKeySequence>
0029 #include <QAction>
0030 
0031 namespace KexiStandardAction
0032 {
0033 
0034 //! @internal
0035 struct Info {
0036     StandardAction id;
0037     QKeySequence shortcut;
0038     const char* psName;
0039     const char* psText;
0040     const char* psToolTip;
0041     const char* psWhatsThis;
0042     const char* psIconName;
0043 };
0044 
0045 //! @internal
0046 static const Info g_rgActionInfo[] = {
0047     { SortAscending, QKeySequence(), "data_sort_az", I18N_NOOP("&Ascending"),
0048         I18N_NOOP("Sort data in ascending order"),
0049         I18N_NOOP("Sorts data in ascending order (from A to Z and from 0 to 9). Data from selected column is used for sorting."),
0050         koIconNameCStr("view-sort-ascending") },
0051     { SortDescending, QKeySequence(), "data_sort_za", I18N_NOOP("&Descending"),
0052       I18N_NOOP("Sort data in descending order"),
0053       I18N_NOOP("Sorts data in descending (from Z to A and from 9 to 0). Data from selected column is used for sorting."),
0054       koIconNameCStr("view-sort-descending") },
0055 
0056     { ActionNone, QKeySequence(), 0, 0, 0, 0, 0 }
0057 };
0058 
0059 //! @internal
0060 class ActionsInfoHash : public QHash<StandardAction, const Info*>
0061 {
0062 public:
0063     ActionsInfoHash() {
0064         const Info* actionInfo = g_rgActionInfo;
0065 
0066         for (; actionInfo->id != ActionNone; actionInfo++)
0067             insert(actionInfo->id, actionInfo);
0068     }
0069 };
0070 
0071 Q_GLOBAL_STATIC(ActionsInfoHash, g_rgActionInfoHash)
0072 
0073 inline const Info* infoPtr(StandardAction id)
0074 {
0075     return g_rgActionInfoHash->value(id);
0076 }
0077 
0078 //---------------------------------------------
0079 
0080 QAction *create(StandardAction id, const QObject *recvr, const char *slot, QObject *parent)
0081 {
0082     QAction *pAction = 0;
0083     const Info* pInfo = infoPtr(id);
0084 
0085     if (pInfo) {
0086         pAction = new QAction(parent);
0087         pAction->setObjectName(pInfo->psName);
0088         QKeySequence cut(pInfo->shortcut);
0089         if (!cut.isEmpty())
0090             pAction->setShortcut(cut);
0091         pAction->setText(i18n(pInfo->psText));
0092         pAction->setToolTip(i18n(pInfo->psToolTip));
0093         pAction->setWhatsThis(i18n(pInfo->psWhatsThis));
0094         if (pInfo->psIconName)
0095             pAction->setIcon(QIcon::fromTheme(QLatin1String(pInfo->psIconName)));
0096     }
0097 
0098     if (recvr && slot)
0099         QObject::connect(pAction, SIGNAL(triggered(bool)), recvr, slot);
0100 
0101     if (pAction) {
0102         KActionCollection *collection = qobject_cast<KActionCollection *>(parent);
0103         if (collection)
0104             collection->addAction(pAction->objectName(), pAction);
0105     }
0106     return pAction;
0107 }
0108 
0109 const char* name(StandardAction id)
0110 {
0111     const Info* pInfo = infoPtr(id);
0112     return (pInfo) ? pInfo->psName : 0;
0113 }
0114 
0115 #define CREATE_METHOD(methodName, enumName) \
0116     QAction *methodName(const QObject *recvr, const char *slot, QObject *parent) \
0117     { \
0118         return KexiStandardAction::create(enumName, recvr, slot, parent); \
0119     }
0120 
0121 CREATE_METHOD(sortAscending, SortAscending)
0122 CREATE_METHOD(sortDescending, SortDescending)
0123 
0124 } //KexiStandardAction