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

#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(QString format READ format WRITE setFormat NOTIFY formatChanged);
  Q_PROPERTY(QString 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);

  QString format() const;
  void setFormat (QString format);

  QString date() const;

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

private slots:
  void onTimeout();

private:
  bool mEnabled = true;
  Chrono::Precision mPrecision = Chrono::Seconds;
  QString mFormat = "{:%c}";
  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);
};