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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
#include "guitools.h"
//------------------------------------------------------------------------------
// QFitsSimplestButton
//------------------------------------------------------------------------------
QFitsSimplestButton::QFitsSimplestButton(QPixmap pm, QWidget *parent) :
QLabel(parent) {
setPixmap(pm);
}
QFitsSimplestButton::QFitsSimplestButton(const QString &te, QWidget *parent) :
QLabel(te, parent) {
setAlignment(Qt::AlignCenter);
}
void QFitsSimplestButton::mouseReleaseEvent(QMouseEvent *event) {
if (event->button() == Qt::LeftButton) emit clicked();
}
void QFitsSimplestButton::mouseDoubleClickEvent(QMouseEvent *e) {
emit doubleClicked();
}
dpDoubleEdit::dpDoubleEdit(QString t, QWidget *parent) : QLineEdit(t, parent) {
setValidator(new QDoubleValidator(this));
connect(this, SIGNAL(textEdited(const QString &)), SLOT(notifyChange(const QString &)));
}
void dpDoubleEdit::notifyChange(const QString &t) {
emit valueChanged(t.toDouble());
}
void dpDoubleEdit::setValue(const double &v) {
setText(QString::number(v));
}
double dpDoubleEdit::value() {
return text().toDouble();
}
dpFitEstimate::dpFitEstimate(QString t, QWidget *parent) : dpDoubleEdit(t, parent) {
minConstrain = new QLabel("min", this);
maxConstrain = new QLabel("max", this);
minConstrain->setAlignment(Qt::AlignRight | Qt::AlignBottom);
maxConstrain->setAlignment(Qt::AlignRight | Qt::AlignTop);
QFont theFont(font());
theFont.setPointSizeF(font().pointSizeF() * .6);
minConstrain->setFont(theFont);
maxConstrain->setFont(theFont);
minConstrain->setStyleSheet("color: rgba(0,0,0,128)");
maxConstrain->setStyleSheet("color: rgba(0,0,0,128)");
minConstrain->setVisible(false);
maxConstrain->setVisible(false);
parameterLock = new QLabel("", this);
parameterLock->setPixmap(QIcon(":/padlock.svg").pixmap(10, 10));
parameterLock->adjustSize();
parameterLock->hide();
popupButton = new QFitsSimplestButton(">", this);
popupButton->setCursor(Qt::ArrowCursor);
// result = new QLabel("result", this);
// error = new QLabel("error", this);
parameters = new QWidget();
fixed = new QCheckBox("fixed", parameters);
fixed->adjustSize();
fixed->move(5, 0);
constrained = new QCheckBox("constrained", parameters);
constrained->adjustSize();
constrained->move(5, fixed->height());
lowerBound = new dpDoubleEdit("", parameters);
lowerBound->adjustSize();
lowerBound->move(5, constrained->y() + constrained->height());
upperBound = new dpDoubleEdit("", parameters);
upperBound->adjustSize();
upperBound->move(lowerBound->x() + lowerBound->width(), lowerBound->y());
parameters->setFixedSize(upperBound->x() + upperBound->width() + 5, lowerBound->y() + lowerBound->height() + 5);
// parameters->adjustSize();
parameters->setWindowFlag(Qt::Popup);
// parameters->setStyleSheet("background: rgba(0, 0, 0, 240)");
// parameters->setAttribute(Qt::WA_NoSystemBackground);
// parameters->setAttribute(Qt::WA_TranslucentBackground);
parameters->hide();
connect(fixed, SIGNAL(toggled(bool)), parameterLock, SLOT(setVisible(bool)));
connect(constrained, SIGNAL(toggled(bool)), minConstrain, SLOT(setVisible(bool)));
connect(constrained, SIGNAL(toggled(bool)), maxConstrain, SLOT(setVisible(bool)));
connect(lowerBound, SIGNAL(textChanged(QString)), minConstrain, SLOT(setText(QString)));
connect(upperBound, SIGNAL(textChanged(QString)), maxConstrain, SLOT(setText(QString)));
connect(popupButton, SIGNAL(clicked()), this, SLOT(popupParameters()));
}
void dpFitEstimate::resizeEvent(QResizeEvent *e) {
popupButton->setGeometry(e->size().width() - e->size().height(), 0, e->size().height(), e->size().height());
QFont theFont(font());
while (QFontMetrics(theFont).boundingRect("0").height() > e->size().height() / 2.) theFont.setPointSizeF(theFont.pointSizeF() * .9);
minConstrain->setGeometry(0, e->size().height() / 2, e->size().width() - e->size().height(), e->size().height() / 2);
minConstrain->setFont(theFont);
maxConstrain->setGeometry(0, 0, e->size().width() - e->size().height(), e->size().height() / 2);
maxConstrain->setFont(theFont);
parameterLock->move(width() - parameterLock->width() - e->size().height(), (height()-parameterLock->height()) / 2);
// result->setGeometry(e->size().width(), 0, e->size().width(), e->size().height());
// error->setGeometry(2 * e->size().width(), 0, e->size().width(), e->size().height());
// fixed->setGeometry(e->size().width() + 5, 0, fixed->size().width(), e->size().height());
// constrained->setGeometry(0, e->size().height() + 5, e->size().width() + 5 + fixed->size().width(), constrained->size().height());
// lowerBound->setGeometry(0, constrained->y() + constrained->height() + 5, e->size().width(), e->size().height());
// upperBound->setGeometry(lowerBound->size().width() + 5, constrained->y() + constrained->height() + 5, e->size().width(), e->size().height());
// parameters->adjustSize();
}
//void dpFitEstimate::mousePressEvent(QMouseEvent *event) {
// if (event->button() == Qt::LeftButton) popupParameters();
// QLineEdit::mousePressEvent(event);
//}
void dpFitEstimate::contextMenuEvent(QContextMenuEvent *event) {
popupParameters();
// QMenu *menu = createStandardContextMenu();
// menu->addAction("specify constrains...", this, SLOT(popupParameters()));
// menu->exec(event->globalPos());
// delete menu;
}
void dpFitEstimate::popupParameters() {
parameters->move(mapToGlobal(QPoint(width(), 0)));
// parameters->move(mapToGlobal(pos()));
// estimate->setText(text());
// estimate->setFocus();
parameters->show();
}
dpFitResult::dpFitResult(QWidget *parent) : QWidget(parent) {
value = 0.;
error = 0.;
v = new QLabel("", this);
v->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
e = new QLabel("", this);
e->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
pm = new QLabel("±", this);
pm->setAlignment(Qt::AlignCenter);
pm->adjustSize();
}
void dpFitResult::adjustSize() {
QRect s = fontMetrics().boundingRect("XXXXXXXXXX±XXXXXXXXXX");
resize(s.width(), s.height());
}
void dpFitResult::resizeEvent(QResizeEvent *ev) {
int w = (ev->size().width() - pm->width()) / 2;
v->setGeometry(0, 0, w, ev->size().height());
pm->setGeometry(w, 0, pm->width(), ev->size().height());
e->setGeometry(w + pm->width(), 0, w, ev->size().height());
}
void dpFitResult::setResult(double nv, double ne) {
value = nv;
error = ne;
v->setText(QString::number(value, 'g', 5));
e->setText(QString::number(error, 'g', 4));
if (ne < 0.) {
e->hide();
pm->hide();
} else {
e->show();
pm->show();
}
}
QString dpFitResult::text() const {
return v->text() + "," + e->text();
}
QString dpFitResult::getValue() const {
return v->text();
}
QString dpFitResult::getError() const {
return e->text();
}
|