summaryrefslogtreecommitdiff
path: root/gui/dynamicwaysentry.cc
blob: cbd5342993f9f8949c66f17d37c66f16dd2d5c37 (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
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
// Simulator for the RISC-V[ECTOR] mini-ISA
// Copyright (C) 2025 Siddarth Suresh
// Copyright (C) 2025 bdunahu

// 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 3 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 <https://www.gnu.org/licenses/>.

#include "dynamicwaysentry.h"
#include <QLabel>
#include <QLineEdit>
#include <QStringList>
#include <QVBoxLayout>
#include <QVector>
#include <QWidget>

DynamicWaysEntry::DynamicWaysEntry(QWidget *parent) : QWidget(parent)
{
	this->l = new QVBoxLayout(this);
	this->l->setAlignment(Qt::AlignTop);
	this->l->setSpacing(6);
	this->l->setContentsMargins(0, 0, 0, 0);
	this->setLayout(l);
	this->add_field();
}

QStringList DynamicWaysEntry::get_entries() const { return this->entries; }

int DynamicWaysEntry::parse_valid_way(QString t)
{
	bool s;
	int i;
	i = t.toInt(&s);
	return (s && i >= 0 && 5 > i) ? i : -1;
}

// TODO if you enter something valid and then make it invalid,
// the next box still shows
void DynamicWaysEntry::on_number_enter(const QString &t)
{
	int i;
	QLineEdit *sender_field;

	sender_field = qobject_cast<QLineEdit *>(sender());
	i = fields.indexOf(sender_field);
	entries[i] = t;

	if (i == this->fields.size() - 1 && !t.isEmpty() &&
		(this->parse_valid_way(t) >= 0) && fields.size() < 4)
		add_field();

	// TODO, unlink, don't trash everything after
	if (t.isEmpty()) {
		while (this->fields.size() > i + 1) {
			remove_last_field();
		}
		while (entries.size() > fields.size()) {
			entries.removeLast();
		}
	}
}

void DynamicWaysEntry::add_field()
{
	QLineEdit *f;

	f = new QLineEdit(this);
	f->setPlaceholderText("# ways (a power of 2)");

	this->l->addWidget(f);;
	this->fields.append(f);
	this->entries.append(QString());
	connect(
		f, &QLineEdit::textChanged, this, &DynamicWaysEntry::on_number_enter);
}

void DynamicWaysEntry::remove_last_field()
{
	QLineEdit *f;

	if (this->fields.isEmpty())
		return;

	f = this->fields.takeLast();
	this->l->removeWidget(f);
	f->deleteLater();

	if (!this->entries.isEmpty())
		entries.removeLast();
}