Motiv
Marvelous OTF2 Traces Interactive Visualizer
Loading...
Searching...
No Matches
TimeInputField.cpp
1/*
2 * Marvelous OTF2 Traces Interactive Visualizer (MOTIV)
3 * Copyright (C) 2023 Florian Gallrein, Björn Gehrke
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18#include "TimeInputField.hpp"
19
20#include <QHBoxLayout>
21#include <QValidator>
22#include <utility>
23
24
25TimeInputField::TimeInputField(QString text, TimeUnit resolution, types::TraceTime initialTime, QWidget *parent)
26 : QWidget(parent), text(std::move(text)), resolution(resolution), time(0) {
27
28 this->textLabel = new QLabel(this->text, this);
29
30 this->lineEdit = new QLineEdit(QString::number(this->time.count()), this);
31 this->lineEdit->setValidator(new QDoubleValidator(this));
32
33 this->comboBox = new QComboBox(this);
34 for (const auto &unit : TIME_UNITS) {
35 this->comboBox->addItem(unit.str());
36 }
37 this->comboBox->setCurrentText(resolution.str());
38
39 auto layout = new QHBoxLayout(this);
40 layout->addWidget(this->textLabel);
41 layout->addWidget(this->lineEdit);
42 layout->addWidget(this->comboBox);
43 this->setLayout(layout);
44
45 connect(this->comboBox, &QComboBox::currentTextChanged, [this] (const auto &newValue) {
46 this->resolution = TimeUnit(newValue);
47 double newTextValue = static_cast<double>(time.count()) / this->resolution.multiplier();
48 this->lineEdit->setText(QString::number(newTextValue));
49 });
50
51 connect(this->lineEdit, &QLineEdit::returnPressed, [this] {
52 double newValue = this->lineEdit->text().toDouble();
53 auto newTotalValue = static_cast<u_int64_t>(newValue * this->resolution.multiplier());
54 this->setTime(types::TraceTime(newTotalValue));
55 });
56
57 setTime(initialTime);
58}
59
60void TimeInputField::setUpdateFunction(std::function<void(types::TraceTime)> newUpdateFunction) {
61 this->updateFunction = std::move(newUpdateFunction);
62}
63
64void TimeInputField::setTime(types::TraceTime newTime) {
65 if(newTime != time) {
66 this->time = newTime;
67 if (this->updateFunction) {
68 this->updateFunction(this->time);
69 }
70
71 double newTextValue = static_cast<double>(time.count()) / this->resolution.multiplier();
72 this->lineEdit->setText(QString::number(newTextValue));
73 }
74}
TimeInputField(QString labelText, TimeUnit timeResolution, types::TraceTime initialTime, QWidget *parent=nullptr)
void setTime(types::TraceTime newTime)
Sets the time.
void setUpdateFunction(std::function< void(types::TraceTime)>)
Represents possible units on the time scale.
Definition: TimeUnit.hpp:29
QString str() const
Definition: TimeUnit.cpp:22
double multiplier() const
Definition: TimeUnit.cpp:59