File: CharmWindow.cpp

package info (click to toggle)
charmtimetracker 1.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,340 kB
  • sloc: cpp: 19,176; xml: 284; python: 120; makefile: 14
file content (282 lines) | stat: -rw-r--r-- 6,860 bytes parent folder | download | duplicates (2)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
  CharmWindow.cpp

  This file is part of Charm, a task-based time tracking application.

  Copyright (C) 2014-2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com

  Author: Frank Osterfeld <frank.osterfeld@kdab.com>

  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 2 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/>.
*/

#include "CharmWindow.h"
#include "ApplicationCore.h"
#include "Data.h"
#include "ViewHelpers.h"
#include "WidgetUtils.h"

#include "Commands/CommandRelayCommand.h"

#include "Core/CharmCommand.h"
#include "Core/CharmConstants.h"

#include <QAction>
#include <QKeyEvent>
#include <QKeySequence>
#include <QMenuBar>
#include <QSettings>
#include <QShortcut>
#include <QToolBar>
#include <QToolButton>

CharmWindow::CharmWindow(const QString &name, QWidget *parent)
    : QMainWindow(parent)
    , m_openCharmAction(new QAction(tr("Open Charm"), this))
    , m_showAction(new QAction(this))
{
    setWindowName(name);
    connect(m_openCharmAction, SIGNAL(triggered(bool)), SLOT(showView()));
    connect(m_showAction, SIGNAL(triggered(bool)), SLOT(showView()));
    connect(this, &CharmWindow::visibilityChanged, this, &CharmWindow::handleShow);
    m_toolBar = addToolBar(QStringLiteral("Toolbar"));
    m_toolBar->setMovable(false);

    emit visibilityChanged(false);
}

void CharmWindow::stateChanged(State)
{
    switch (ApplicationCore::instance().state()) {
    case Connecting:
        setEnabled(false);
        restoreGuiState();
        configurationChanged();
        break;
    case Connected:
        configurationChanged();
        ApplicationCore::instance().createFileMenu(menuBar());
        insertEditMenu();
        ApplicationCore::instance().createWindowMenu(menuBar());
        ApplicationCore::instance().createHelpMenu(menuBar());
        setEnabled(true);
        break;
    case Disconnecting:
        setEnabled(false);
        saveGuiState();
        break;
    case ShuttingDown:
    case Dead:
    default:
        break;
    }
}

void CharmWindow::setWindowName(const QString &text)
{
    m_windowName = text;
    setWindowTitle(text);
}

QString CharmWindow::windowName() const
{
    return m_windowName;
}

void CharmWindow::setWindowIdentifier(const QString &id)
{
    m_windowIdentifier = id;
}

QString CharmWindow::windowIdentifier() const
{
    return m_windowIdentifier;
}

void CharmWindow::setWindowNumber(int number)
{
    m_windowNumber = number;
    delete m_shortcut;
    m_shortcut = new QShortcut(this);
    QKeySequence sequence(tr("Ctrl+%1").arg(number));
#ifdef Q_OS_OSX
    m_shortcut->setKey(sequence);
#endif
    m_shortcut->setContext(Qt::ApplicationShortcut);
    m_showAction->setShortcut(sequence);
    connect(m_shortcut, SIGNAL(activated()), SLOT(showHideView()));
    connect(m_shortcut, SIGNAL(activated()), SLOT(showView()));
}

int CharmWindow::windowNumber() const
{
    return m_windowNumber;
}

QToolBar *CharmWindow::toolBar() const
{
    return m_toolBar;
}

QAction *CharmWindow::openCharmAction()
{
    return m_openCharmAction;
}

QAction *CharmWindow::showAction()
{
    return m_showAction;
}

void CharmWindow::restore()
{
    show();
}

void CharmWindow::checkVisibility()
{
    const auto visibility = isVisible();

    if (m_isVisibility != visibility) {
        m_isVisibility = visibility;
        emit visibilityChanged(m_isVisibility);
    }
}

void CharmWindow::showEvent(QShowEvent *e)
{
    checkVisibility();
    QMainWindow::showEvent(e);
}

void CharmWindow::hideEvent(QHideEvent *e)
{
    checkVisibility();
    QMainWindow::hideEvent(e);
}

void CharmWindow::sendCommand(CharmCommand *cmd)
{
    cmd->prepare();
    auto relay = new CommandRelayCommand(this);
    relay->setCommand(cmd);
    emit emitCommand(relay);
}

void CharmWindow::sendCommandRollback(CharmCommand *cmd)
{
    cmd->prepare();
    auto relay = new CommandRelayCommand(this);
    relay->setCommand(cmd);
    emit emitCommandRollback(relay);
}

void CharmWindow::handleShow(bool visible)
{
    const QString text = tr("Show %1").arg(m_windowName);
    m_showAction->setText(text);
    m_showAction->setEnabled(!visible);
}

void CharmWindow::commitCommand(CharmCommand *command)
{
    command->finalize();
}

void CharmWindow::keyPressEvent(QKeyEvent *event)
{
    if (event->type() == QEvent::KeyPress) {
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
        if (keyEvent->modifiers() & Qt::ControlModifier
            && keyEvent->key() == Qt::Key_W
            && isVisible()) {
            showHideView();
            return;
        }
    }
    QMainWindow::keyPressEvent(event);
}

void CharmWindow::showView(QWidget *w)
{
    w->show();
    w->setWindowState(w->windowState() & ~Qt::WindowMinimized);
    w->raise();
    w->activateWindow();
}

bool CharmWindow::showHideView(QWidget *w)
{
    // hide or restore the view
    if (w->isVisible()) {
        w->hide();
        return false;
    } else {
        showView(w);
        return true;
    }
}

void CharmWindow::showView()
{
    showView(this);
}

void CharmWindow::showHideView()
{
    showHideView(this);
}

void CharmWindow::configurationChanged()
{
    WidgetUtils::updateToolButtonStyle(this);
}

void CharmWindow::saveGuiState()
{
    Q_ASSERT(!windowIdentifier().isEmpty());
    QSettings settings;
    settings.beginGroup(windowIdentifier());
    // save geometry
    WidgetUtils::saveGeometry(this, MetaKey_MainWindowGeometry);
    settings.setValue(MetaKey_MainWindowVisible, isVisible());
}

void CharmWindow::restoreGuiState()
{
    const QString identifier = windowIdentifier();
    Q_ASSERT(!identifier.isEmpty());
    // restore geometry
    QSettings settings;
    settings.beginGroup(identifier);
    WidgetUtils::restoreGeometry(this, MetaKey_MainWindowGeometry);
    // restore visibility
    bool visible = true;
    if (m_hideAtStartUp)
    {
        visible = false;
    }
    else
    {
        // Time Tracking Window should always be visible, except when Charm is started with --hide-at-start
        visible = (identifier == QLatin1String("window_tracking")) ? true : settings.value(
                                                                         MetaKey_MainWindowVisible).toBool();
    }
    setVisible(visible);
}

void CharmWindow::setHideAtStartup(const bool &value)
{
    m_hideAtStartUp = value;
}