mainwindow.cpp 5.43 KB
Newer Older
1
/*
Allen Winter's avatar
Allen Winter committed
2
  This file is part of KAddressBook.
3

4
  SPDX-FileCopyrightText: 2007 Tobias Koenig <tokoe@kde.org>
5

6
  SPDX-License-Identifier: GPL-2.0-or-later
7
8
9
10
*/

#include "mainwindow.h"
#include "mainwidget.h"
Laurent Montel's avatar
Laurent Montel committed
11
#include "settings.h"
Allen Winter's avatar
Allen Winter committed
12
#include <KActionCollection>
Laurent Montel's avatar
Laurent Montel committed
13
#include <KConfigGroup>
Allen Winter's avatar
Allen Winter committed
14
#include <KEditToolBar>
Laurent Montel's avatar
Laurent Montel committed
15
#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
16
#include <KMessageBox>
Laurent Montel's avatar
Laurent Montel committed
17
18
19
20
21
#include <KSharedConfig>
#include <KShortcutsDialog>
#include <KStandardAction>
#include <KToolBar>
#include <QAction>
Laurent Montel's avatar
Laurent Montel committed
22
#include <QMenuBar>
Laurent Montel's avatar
Laurent Montel committed
23
#include <QPointer>
Laurent Montel's avatar
Laurent Montel committed
24
#include <kxmlgui_version.h>
Laurent Montel's avatar
Laurent Montel committed
25
#ifdef WITH_KUSERFEEDBACK
Laurent Montel's avatar
Laurent Montel committed
26
#include "userfeedback/userfeedbackmanager.h"
Laurent Montel's avatar
Laurent Montel committed
27
28
29
#include <KUserFeedback/NotificationPopup>
#include <KUserFeedback/Provider>
#endif
Allen Winter's avatar
Allen Winter committed
30

31
MainWindow::MainWindow()
Laurent Montel's avatar
Laurent Montel committed
32
    : KXmlGuiWindow(nullptr)
33
{
Laurent Montel's avatar
Laurent Montel committed
34
    mMainWidget = new MainWidget(this, this);
35

Laurent Montel's avatar
Laurent Montel committed
36
    setCentralWidget(mMainWidget);
37
38


Laurent Montel's avatar
Laurent Montel committed
39
    setStandardToolBarMenuEnabled(true);
40

Laurent Montel's avatar
Laurent Montel committed
41
    toolBar()->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
42

43
    initActions();
Laurent Montel's avatar
Laurent Montel committed
44
    setupGUI(Save | Create, QStringLiteral("kaddressbookui.rc"));
45

Laurent Montel's avatar
Laurent Montel committed
46
    setAutoSaveSettings();
47

Laurent Montel's avatar
Laurent Montel committed
48
    mShowMenuBarAction->setChecked(Settings::self()->showMenuBar());
Laurent Montel's avatar
Laurent Montel committed
49
    slotToggleMenubar(true);
Laurent Montel's avatar
Laurent Montel committed
50
#ifdef WITH_KUSERFEEDBACK
51
    auto userFeedBackNotificationPopup = new KUserFeedback::NotificationPopup(this);
Laurent Montel's avatar
Laurent Montel committed
52
53
    userFeedBackNotificationPopup->setFeedbackProvider(UserFeedBackManager::self()->userFeedbackProvider());
#endif
54
55
56
57
58
59
}

MainWindow::~MainWindow()
{
}

60
MainWidget *MainWindow::mainWidget() const
61
{
62
    return mMainWidget;
63
64
}

65
66
void MainWindow::initActions()
{
Laurent Montel's avatar
Laurent Montel committed
67
    KStandardAction::quit(this, &MainWindow::close, actionCollection());
Laurent Montel's avatar
Laurent Montel committed
68
    mShowMenuBarAction = KStandardAction::showMenubar(this, &MainWindow::slotToggleMenubar, actionCollection());
Laurent Montel's avatar
Laurent Montel committed
69

Laurent Montel's avatar
Laurent Montel committed
70
71
72
73
    QAction *action = KStandardAction::keyBindings(this, &MainWindow::configureKeyBindings, actionCollection());
    action->setWhatsThis(i18nc("@info:whatsthis",
                               "You will be presented with a dialog where you can configure "
                               "the application-wide shortcuts."));
Laurent Montel's avatar
Laurent Montel committed
74
75
    KStandardAction::configureToolbars(this, &MainWindow::configureToolbars, actionCollection());
    KStandardAction::preferences(this, &MainWindow::configure, actionCollection());
76
77
78
79
80
81
82
83
84
85
86
87
    if (menuBar()) {
        mHamburgerMenu = KStandardAction::hamburgerMenu(nullptr, nullptr, actionCollection());
        mHamburgerMenu->setShowMenuBarAction(mShowMenuBarAction);
        mHamburgerMenu->setMenuBar(menuBar());
        connect(mHamburgerMenu, &KHamburgerMenu::aboutToShowMenu, this, [this]() {
            updateHamburgerMenu();
            // Immediately disconnect. We only need to run this once, but on demand.
            // NOTE: The nullptr at the end disconnects all connections between
            // q and mHamburgerMenu's aboutToShowMenu signal.
            disconnect(mHamburgerMenu, &KHamburgerMenu::aboutToShowMenu, this, nullptr);
        });
    }
88
89
90
91
92
}

void MainWindow::updateHamburgerMenu()
{
    QMenu *menu = new QMenu;
93
94
95
    menu->addAction(actionCollection()->action(QStringLiteral("akonadi_resource_create")));
    menu->addSeparator();

96
    menu->addAction(actionCollection()->action(QLatin1String(KStandardAction::name(KStandardAction::Print))));
97
    menu->addAction(actionCollection()->action(QLatin1String(KStandardAction::name(KStandardAction::PrintPreview))));
98
    menu->addSeparator();
99

100
101
    menu->addAction(actionCollection()->action(QLatin1String(KStandardAction::name(KStandardAction::Quit))));
    mHamburgerMenu->setMenu(menu);
102
103
104
105
}

void MainWindow::configure()
{
Laurent Montel's avatar
Laurent Montel committed
106
    mMainWidget->configure();
107
108
109
110
}

void MainWindow::configureKeyBindings()
{
Laurent Montel's avatar
Laurent Montel committed
111
#if KXMLGUI_VERSION < QT_VERSION_CHECK(5, 84, 0)
Laurent Montel's avatar
Laurent Montel committed
112
    if (KShortcutsDialog::configure(actionCollection(), KShortcutsEditor::LetterShortcutsAllowed, this)) {
Laurent Montel's avatar
Laurent Montel committed
113
114
        mMainWidget->updateQuickSearchText();
    }
Laurent Montel's avatar
Laurent Montel committed
115
116
117
118
119
#else
    KShortcutsDialog::showDialog(actionCollection(), KShortcutsEditor::LetterShortcutsAllowed, /*isModal*/ true, this);
    // We can't detect if we accepted or not => update quick search all the time. Perhaps in the future we will have a signal.
    mMainWidget->updateQuickSearchText();
#endif
120
}
121
122
123

void MainWindow::configureToolbars()
{
Laurent Montel's avatar
Laurent Montel committed
124
125
    KConfigGroup grp = KSharedConfig::openConfig()->group("MainWindow");
    saveMainWindowSettings(grp);
126

Laurent Montel's avatar
Laurent Montel committed
127
    QPointer<KEditToolBar> dlg = new KEditToolBar(factory());
Martin Koller's avatar
Martin Koller committed
128
    connect(dlg.data(), &KEditToolBar::newToolBarConfig, this, &MainWindow::newToolbarConfig);
Laurent Montel's avatar
Laurent Montel committed
129
130
    dlg->exec();
    delete dlg;
131
132
133
134
}

void MainWindow::newToolbarConfig()
{
Laurent Montel's avatar
Laurent Montel committed
135
    createGUI(QStringLiteral("kaddressbookui.rc"));
136

Laurent Montel's avatar
Laurent Montel committed
137
    applyMainWindowSettings(KSharedConfig::openConfig()->group("MainWindow"));
138
139
}

Laurent Montel's avatar
Laurent Montel committed
140
141
142
void MainWindow::slotToggleMenubar(bool dontShowWarning)
{
    if (menuBar()) {
Laurent Montel's avatar
Laurent Montel committed
143
        if (mShowMenuBarAction->isChecked()) {
Laurent Montel's avatar
Laurent Montel committed
144
145
            menuBar()->show();
        } else {
146
            if (!dontShowWarning && (!toolBar()->isVisible() || !toolBar()->actions().contains(mHamburgerMenu))) {
Laurent Montel's avatar
Laurent Montel committed
147
                const QString accel = mShowMenuBarAction->shortcut().toString();
Laurent Montel's avatar
Laurent Montel committed
148
149
                KMessageBox::information(this,
                                         i18n("<qt>This will hide the menu bar completely."
Laurent Montel's avatar
Laurent Montel committed
150
151
152
153
                                              " You can show it again by typing %1.</qt>",
                                              accel),
                                         i18n("Hide menu bar"),
                                         QStringLiteral("HideMenuBarWarning"));
Laurent Montel's avatar
Laurent Montel committed
154
155
156
            }
            menuBar()->hide();
        }
Laurent Montel's avatar
Laurent Montel committed
157
        Settings::self()->setShowMenuBar(mShowMenuBarAction->isChecked());
Laurent Montel's avatar
Laurent Montel committed
158
159
    }
}