aboutsummaryrefslogtreecommitdiff
path: root/src/applicationmenu.cpp
blob: b7acd57332fb2230fe59b81a9cf502e2e9beb40a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
 * This file is part of smolbote. It's copyrighted by the contributors recorded
 * in the version control history of the file, available from its original
 * location: https://neueland.iserlohn-fortress.net/cgit/smolbote
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "applicationmenu.h"
#include "browser.h"
#include "configuration.h"
#include "session/savesessiondialog.h"
#include "session/sessiondialog.h"
#include "smolbote/plugininterface.hpp"
#include <QPluginLoader>

ApplicationMenu::ApplicationMenu(Browser *app, QWidget *parent)
    : QMenu(parent)
{
    setTitle(qApp->applicationName());
    Configuration conf;

    const auto sessionPath = conf.value<QString>("session.path").value();
    auto *actionSaveSession = addAction(tr("Save Session"), this, [sessionPath]() {
        auto *sessionDialog = new SaveSessionDialog(nullptr);
        if(sessionDialog->exec() == QDialog::Accepted) {
            sessionDialog->save(sessionPath);
        }
    });
    conf.shortcut<QAction>(*actionSaveSession, "shortcuts.session.save");

    auto *actionOpenSession = addAction(tr("Open Session"), this, []() {
        auto *sessionDialog = new SessionDialog(nullptr);
        sessionDialog->exec();
    });
    conf.shortcut<QAction>(*actionOpenSession, "shortcuts.session.open");

    bottom_pluginSeparator = addSeparator();

    auto *actionAbout = addAction(tr("About"), app, &Browser::about);
    conf.shortcut<QAction>(*actionAbout, "shortcuts.window.about");

    auto *action_aboutPlugins = addAction(tr("About Plugins"), app, &Browser::aboutPlugins);
    conf.shortcut(*action_aboutPlugins, "app.shortcuts.about.plugins");

    auto *actionQuit = addAction(tr("Quit"), app, &Browser::quit);
    conf.shortcut<QAction>(*actionQuit, "shortcuts.window.quit");
}

void ApplicationMenu::addPlugin(QPluginLoader *plugin)
{
    if(top_pluginSeparator == nullptr) {
        top_pluginSeparator = insertSeparator(bottom_pluginSeparator);
    }

    const auto metadata = plugin->metaData().value("MetaData").toObject();
    auto *action = addAction(metadata.value("name").toString());
    action->setShortcut(QKeySequence::fromString(metadata.value("shortcut").toString()));

    connect(action, &QAction::triggered, this, [this, plugin]() {
        if(plugin->isLoaded()) {
            if(const auto *interface = qobject_cast<PluginInterface *>(plugin->instance())) {
                interface->createWidget(this->parentWidget())->exec();
            }
        }
    });
    insertAction(bottom_pluginSeparator, action);
}