File indexing completed on 2024-04-28 15:39:47

0001 /* SPDX-FileCopyrightText: 2003-2010 Jesper K. Pedersen <blackie@kde.org>
0002 
0003    SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 #include "ShortCutManager.h"
0006 
0007 #include "ListSelect.h"
0008 
0009 #include <QDockWidget>
0010 #include <QShortcut>
0011 
0012 /**
0013  * Register the dock widget for getting a shortcut. its buddy will get the
0014  * actual focus when the shortcut is execute.
0015  */
0016 void AnnotationDialog::ShortCutManager::addDock(QDockWidget *dock, QWidget *buddy)
0017 {
0018     m_docks.append(qMakePair(dock, buddy));
0019 }
0020 
0021 void AnnotationDialog::ShortCutManager::addLabel(QLabel *label)
0022 {
0023     m_labelWidgets.append(label);
0024 }
0025 
0026 void AnnotationDialog::ShortCutManager::setupShortCuts()
0027 {
0028     for (const DockPair &pair : m_docks) {
0029         QDockWidget *dock = pair.first;
0030         QWidget *widget = pair.second;
0031         QString title = dock->windowTitle();
0032         for (int index = 0; index < title.length(); ++index) {
0033             const QChar ch = title[index].toLower();
0034             if (!m_taken.contains(ch)) {
0035                 m_taken.insert(ch);
0036                 dock->setWindowTitle(title.left(index) + QString::fromLatin1("&") + title.mid(index));
0037                 new QShortcut(QString::fromLatin1("Alt+%1").arg(ch), widget, SLOT(setFocus()));
0038                 break;
0039             }
0040         }
0041     }
0042 
0043     for (QLabel *label : m_labelWidgets) {
0044         const QString title = label->text();
0045         for (int index = 0; index < title.length(); ++index) {
0046             const QChar ch = title[index].toLower();
0047             if (!m_taken.contains(ch)) {
0048                 m_taken.insert(ch);
0049                 label->setText(title.left(index) + QString::fromLatin1("&") + title.mid(index));
0050                 break;
0051             }
0052         }
0053     }
0054 }
0055 
0056 /**
0057  * Search for & in the text, and if found register the character after the ampersand as a shortcut
0058  * This is needed as the OK and Cancel button will get a shortcut by KDE,
0059  * despite an attempt at telling it not too.
0060  */
0061 void AnnotationDialog::ShortCutManager::addTaken(const QString &text)
0062 {
0063     const int index = text.indexOf(QChar::fromLatin1('&'));
0064     if (index != -1)
0065         m_taken.insert(text[index + 1].toLower());
0066 }
0067 // vi:expandtab:tabstop=4 shiftwidth=4: