-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsettingsitem.cpp
More file actions
146 lines (130 loc) · 4.8 KB
/
settingsitem.cpp
File metadata and controls
146 lines (130 loc) · 4.8 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2023 by Linwood Ferguson, licensed under GNU GPLv3
#include <QWidget>
#include <QStyleOption>
#include <QPainter>
#include <QLineEdit>
#include <QCheckBox>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include "settingsitem.h"
#include "mainwindow.h"
#include "settingswidget.h"
#include "focuswatcher.h"
#include "oursettings.h"
#include <cassert>
// This provides individual widgets composed of a prompt, a value entry portion, and a message for validation messages
/* int */ settingsItem::settingsItem(settingsWidget *sw, QWidget *parent, QString key, QString prompt, int lower, int higher) : QWidget(parent)
{
valueWidget = new QLineEdit(this);
valueWidget->setProperty("Number",true);
thisType = int_t;
settingsItemShared(sw, key, prompt);
((QLineEdit*)valueWidget)->setText(mParent->ourSettingsPtr->getSetting(key).toString());
connect(new FocusWatcher(valueWidget), &FocusWatcher::focusChanged, this, [this,lower, higher](){validateRange(lower,higher);});
}
/* uint */ settingsItem::settingsItem(settingsWidget *sw, QWidget *parent, QString key, QString prompt, unsigned int lower, unsigned int higher) : QWidget(parent)
{
valueWidget = new QLineEdit(this);
valueWidget->setProperty("Number",true);
thisType = uint_t;
settingsItemShared(sw, key, prompt);
((QLineEdit*)valueWidget)->setText(mParent->ourSettingsPtr->getSetting(key).toString());
connect(new FocusWatcher(valueWidget), &FocusWatcher::focusChanged, this, [this, lower, higher](){validateRange(lower,higher);});
}
/* string */ settingsItem::settingsItem(settingsWidget *sw, QWidget *parent, QString key, QString prompt, QString property) : QWidget(parent)
{
valueWidget = new QLineEdit(this);
valueWidget->setProperty(property.toStdString().c_str(),true);
thisType = string_t;
settingsItemShared(sw, key, prompt);
((QLineEdit*)valueWidget)->setText(mParent->ourSettingsPtr->getSetting(key).toString());
}
/* bool */ settingsItem::settingsItem(settingsWidget *sw, QWidget *parent, QString key, QString prompt) : QWidget(parent) // junk parameter just to distinguish this from string
{
valueWidget = new QCheckBox(this);
thisType = bool_t;
settingsItemShared(sw, key, prompt);
((QCheckBox*)valueWidget)->setChecked(mParent->ourSettingsPtr->getSetting(key).toBool());
}
/* shared */ void settingsItem::settingsItemShared(settingsWidget *sw, QString key, QString prompt)
{
ourParent = sw; // This will be the settings
mParent = sw->mParent; // we need a main window pointer
sw->values[key] = this; // Self-register in the map
sw->innerLayout->addWidget(this); // and putourselves in the layout
hb = new QHBoxLayout(this);
hb->setAlignment(Qt::AlignBottom);
m = new QLabel(this); // starts blank
m->setProperty("SettingMsg",true);
p = new QLabel(prompt,this);
p->setAlignment(Qt::AlignRight | Qt::AlignCenter);
hb->addWidget(p);
hb->addWidget(valueWidget);
hb->addWidget(m);
this->setObjectName(key);
m->setProperty("ErrorMessage",true);
hb->setContentsMargins(0,0,0,0);
}
settingsItem::~settingsItem()
{
}
int settingsItem::toInt()
{
assert(thisType == int_t || thisType == uint_t );
return ((QLineEdit*)valueWidget)->text().toInt();
}
unsigned int settingsItem::toUInt()
{
assert(thisType == uint_t );
return ((QLineEdit*)valueWidget)->text().toInt();
}
QString settingsItem::toString()
{
if(thisType == bool_t) return (((QCheckBox*)valueWidget)->checkState() ? "true" : "false");
else return ((QLineEdit*)valueWidget)->text();
}
bool settingsItem::toBool()
{
assert(thisType == bool_t);
return ((QCheckBox*)valueWidget)->checkState();
}
QLabel* settingsItem::msgPtr()
{
return m;
}
int settingsItem::getPromptWidth()
{
return p->fontMetrics().boundingRect(p->text()).width();
}
void settingsItem::setPromptWidth(int w)
{
p->setMinimumWidth(w);
p->setMaximumWidth(w);
}
bool settingsItem::validateRange(int bottom, int top)
{
// Validate int (or uint) field, that it is a number and is in range [bottom,top], giving message in label if not (clearing if so)
bool ok;
int newVal = ((QLineEdit*)valueWidget)->text().toInt(&ok);
if(!ok)
{
m->setText("Invalid integer number format");
valueWidget->setFocus();
return false;
}
if(newVal < bottom || newVal > top)
{
m->setText("Value must be between " + QString::number(bottom) + " and " + QString::number(top));
valueWidget->setFocus();
return false;
}
m->setText("");
return true; // Note on success we aren't affecting focus
}
void settingsItem::paintEvent(QPaintEvent *) // This is here so we can use stylesheet styling if needed
{
QStyleOption opt;
opt.initFrom(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}