/* Shortcut.h */

/* Copyright (C) 2011-2024 Michael Lugmair (Lucio Carreras)
 *
 * This file is part of sayonara player
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef SHORTCUT_H
#define SHORTCUT_H

#include "ShortcutIdentifier.h"
#include "Utils/Pimpl.h"

#include <QShortcut>

#define ShortcutHandlerPrivate private

class QKeySequence;
class QWidget;
class Shortcut
{
		PIMPL(Shortcut)
		Shortcut();

		friend class ShortcutHandler;

	public:
		Shortcut(ShortcutIdentifier identifier, const QString& defaultShortcut);
		Shortcut(ShortcutIdentifier identifier, const QStringList& defaultShortcuts);
		Shortcut(const Shortcut& other);
		Shortcut& operator=(const Shortcut& other);
		~Shortcut();

		static Shortcut getInvalid();

		void changeShortcut(const QStringList& shortcuts);

		[[nodiscard]] QString name() const;

		[[nodiscard]] QStringList defaultShortcut() const;

		[[nodiscard]] QList<QKeySequence> sequences() const;
		[[nodiscard]] QKeySequence sequence() const;

		[[nodiscard]] const QStringList& shortcuts() const;

		[[nodiscard]] ShortcutIdentifier identifier() const;
		[[nodiscard]] QString databaseKey() const;

		[[nodiscard]] bool isValid() const;

		template<typename T>
		void connect(QWidget* parent, T func, Qt::ShortcutContext context = Qt::WindowShortcut)
		{
			QList<QShortcut*> shortcuts = initQtShortcut(parent, context);
			for(QShortcut* sc: shortcuts)
			{
				QObject::connect(sc, &QShortcut::activated, parent, func);
			}
		}

		void connect(QWidget* parent, QObject* receiver, const char* slot,
		             Qt::ShortcutContext context = Qt::WindowShortcut);

	ShortcutHandlerPrivate:
		void setQtShortcuts(const QList<QShortcut*>& qshortcuts);
		void removeQtShortcut(QShortcut* qshortcut);
		QList<QShortcut*> qtShortcuts() const;

	private:
		QList<QShortcut*> initQtShortcut(QWidget* parent, Qt::ShortcutContext context);
};

#endif // SHORTCUT_H
