summaryrefslogtreecommitdiff
path: root/accounts/gkleen@sif/shell/quickshell-plugins/Chrono.hpp
blob: 4d06007d12c1ee8dc54de0a2a9d2a3bc2898c886 (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
#pragma once

#include <QDateTime>
#include <QObject>
#include <QTimer>

#include <qqmlintegration.h>
#include <chrono>

class Chrono : public QObject {
  Q_OBJECT;
  Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged);
  Q_PROPERTY(Chrono::Precision precision READ precision WRITE setPrecision NOTIFY precisionChanged);
  Q_PROPERTY(QDateTime date READ date NOTIFY dateChanged)
  QML_ELEMENT;

public:
  enum Precision : quint8 {
    Hours = 1,
    Minutes = 2,
    Seconds = 3,
  };
  Q_ENUM(Precision);

  explicit Chrono(QObject* parent = nullptr);

  bool enabled() const;
  void setEnabled(bool enabled);

  Chrono::Precision precision() const;
  void setPrecision(Chrono::Precision precision);

  Q_INVOKABLE QString format(const QString& fmt) const;

  QDateTime date() const;

signals:
  void enabledChanged();
  void precisionChanged();
  void dateChanged();

private slots:
  void onTimeout();

private:
  bool mEnabled = true;
  Chrono::Precision mPrecision = Chrono::Seconds;
  QTimer timer;
  std::chrono::time_point<std::chrono::system_clock> currentTime, targetTime;

  void update();
  void setTime(const std::chrono::time_point<std::chrono::system_clock>& targetTime);
  void schedule(const std::chrono::time_point<std::chrono::system_clock>& targetTime);
};