File indexing completed on 2024-05-05 05:54:32

0001 /*
0002     SPDX-FileCopyrightText: 2014 Elvis Angelaccio <elvis.angelaccio@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 
0008 #ifndef MAINWINDOW_H
0009 #define MAINWINDOW_H
0010 
0011 #include "session.h"
0012 
0013 #include <KXmlGuiWindow>
0014 
0015 #include <QPointer>
0016 #include <QTimer>
0017 
0018 #include <memory>
0019 
0020 class LapModel;
0021 class SessionModel;
0022 class Stopwatch;
0023 class TimeDisplay;
0024 
0025 class KToggleAction;
0026 
0027 class QAction;
0028 class QDBusInterface;
0029 class QLabel;
0030 class QSortFilterProxyModel;
0031 class QSplitter;
0032 class QTableView;
0033 class QTextStream;
0034 class QToolButton;
0035 
0036 /**
0037  * @brief Kronometer main window.
0038  */
0039 class MainWindow : public KXmlGuiWindow
0040 {
0041     Q_OBJECT
0042 
0043 public:
0044 
0045     explicit MainWindow(SessionModel *sessionModel, QWidget *parent = nullptr, const Session& session = Session {});
0046     virtual ~MainWindow();
0047 
0048     /**
0049      * @param title The title for this window.
0050      */
0051     void setWindowTitle(const QString& title);
0052 
0053     void enableLapShortcuts(bool enable);
0054 
0055 protected:
0056 
0057     bool queryClose() override;
0058 
0059 private Q_SLOTS:
0060 
0061     /**
0062      * Stopwatch running state triggers.
0063      */
0064     void slotRunning();
0065 
0066     /**
0067      * Stopwatch paused state triggers.
0068      */
0069     void slotPaused();
0070 
0071     /**
0072      * Stopwatch inactive state triggers.
0073      */
0074     void slotInactive();
0075 
0076     /**
0077      * Slot for the system bus PrepareForSleep signal.
0078      * @param beforeSleep Whether the signal has been emitted before suspension.
0079      */
0080     void slotPrepareForSleep(bool beforeSleep);
0081 
0082     /**
0083      * Setup the settings dialog.
0084      */
0085     void slotShowSettings();
0086 
0087     /**
0088      * Write the new settings on filesystem.
0089      */
0090     void slotWriteSettings();
0091 
0092     /**
0093      * Fix lap dock appereance.
0094      */
0095     void slotUpdateLapDock();
0096 
0097     /**
0098      * Open a new MainWindow instance.
0099      */
0100     void slotNewSession();
0101 
0102     /**
0103      * Open an existing session in a new MainWindow instance.
0104      */
0105     void slotOpenSession();
0106 
0107     /**
0108      * Save current times in the current session.
0109      */
0110     void slotSaveSession();
0111 
0112     /**
0113      * Save current times as a new session.
0114      */
0115     void slotSaveSessionAs();
0116 
0117     /**
0118      * Export current lap times on a file.
0119      */
0120     void slotExportLapsAs();
0121 
0122     /**
0123      * Copy current stopwatch time to clipboard.
0124      */
0125     void slotCopyToClipboard();
0126 
0127     /**
0128      * Toggle menubar visibility.
0129      */
0130     void slotToggleMenuBar();
0131 
0132     void slotUpdateControlMenu();
0133 
0134     void slotToolBarUpdated();
0135 
0136 private:
0137 
0138     bool m_startTimerImmediately = false;
0139     Stopwatch *m_stopwatch;
0140     TimeDisplay *m_stopwatchDisplay;
0141     QTableView *m_lapView;
0142 
0143     QAction *m_startAction;
0144     QAction *m_pauseAction;
0145     QAction *m_resetAction;
0146     QAction *m_lapAction;
0147     QAction *m_exportAction;
0148     std::unique_ptr<QTimer> m_controlMenuTimer;
0149     QPointer<QToolButton> m_controlMenuButton;
0150     KToggleAction *m_toggleMenuAction;
0151 
0152     LapModel *m_lapModel;
0153     SessionModel *m_sessionModel;
0154 
0155     Session m_session;
0156 
0157     QDBusInterface *m_screensaverInterface = nullptr;
0158     quint32 m_screenInhibitCookie = 0;
0159 
0160     /**
0161      * Setup the central widget of the window.
0162      */
0163     void setupCentralWidget();
0164 
0165     /**
0166      * Setup standard and custom QActions.
0167      */
0168     void setupActions();
0169 
0170     /**
0171      * Load settings from app Config and apply them to the other objects.
0172      */
0173     void loadSettings();
0174 
0175     /**
0176      * Set the stopwatch refresh granularity.
0177      */
0178     void setupGranularity();
0179 
0180     /**
0181      * Create a session with the current stopwatch time and lap times.
0182      * @param name The name of the session to be saved.
0183      */
0184     void saveSessionAs(const QString& name);
0185 
0186     /**
0187      * Load a saved session.
0188      */
0189     void loadSession();
0190 
0191     /**
0192      * Export current lap times on a new file.
0193      * @param name The name of the file to be created.
0194      * @param mimeType The mime type of the file to be created.
0195      */
0196     void exportLapsAs(const QString& name, const QString& mimeType);
0197 
0198     /**
0199      * Write the JSON laps representation on the given object.
0200      * @param json The JSON object to be written.
0201      */
0202     void exportLapsAsJson(QJsonObject& json);
0203 
0204     /**
0205      * Write the CSV laps representation on the given stream.
0206      * @param out The stream to be written.
0207      */
0208     void exportLapsAsCsv(QTextStream& out);
0209 
0210     /**
0211      * @return Whether there is a window size saved in the kronometer config.
0212      */
0213     bool isWindowSizeSaved() const;
0214 
0215     /**
0216      * A "comment" message with timestamp, to be used in the created files.
0217      * @return The string "Created by Kronomer on <timestamp>"
0218      */
0219     QString timestampMessage();
0220 
0221     void createControlMenuButton();
0222 
0223     void deleteControlMenuButton();
0224 
0225     /**
0226      * @return true if @p action has been added to @p menu;
0227      * false if @p action or @p menu is null, or if @p action
0228      * is already on the toolBar().
0229      */
0230     bool addActionToMenu(QAction* action, QMenu* menu);
0231 
0232     void activateScreenInhibition();
0233 
0234     void disactivateScreenInhibition();
0235 
0236     Q_DISABLE_COPY(MainWindow)
0237 };
0238 
0239 
0240 #endif