|
| 1 | +/* |
| 2 | + * AxisEditWidget.cpp |
| 3 | + * |
| 4 | + * Created on: Jun 12, 2016 |
| 5 | + * Author: htallone |
| 6 | + */ |
| 7 | + |
| 8 | +#include "AxisEditWidget.h" |
| 9 | + |
| 10 | +AxisEditWidget::AxisEditWidget(QWidget* parent) : QWidget(parent){ |
| 11 | + // TODO Auto-generated constructor stub |
| 12 | + |
| 13 | +} |
| 14 | + |
| 15 | +AxisEditWidget::AxisEditWidget(QWidget* parent, QCustomPlot *qcustomplotp, int axisIndexp) |
| 16 | + : QWidget(parent) |
| 17 | +{ |
| 18 | + qcustomplot = qcustomplotp; |
| 19 | + axisIndex = axisIndexp; |
| 20 | + |
| 21 | + |
| 22 | + axisLabelNameLineEdit = new QLineEdit; |
| 23 | + axisRangeLowerLineEdit = new QLineEdit; |
| 24 | + axisRangeUpperLineEdit = new QLineEdit; |
| 25 | + axisRangeLowerLineEdit->setValidator(new QDoubleValidator()); |
| 26 | + axisRangeUpperLineEdit->setValidator(new QDoubleValidator()); |
| 27 | + |
| 28 | + axisLabelNameLineEdit->setMaximumWidth(200); |
| 29 | + axisRangeLowerLineEdit->setMaximumWidth(80); |
| 30 | + axisRangeLowerLineEdit->setAlignment(Qt::AlignCenter); |
| 31 | + axisRangeUpperLineEdit->setMaximumWidth(80); |
| 32 | + axisRangeUpperLineEdit->setAlignment(Qt::AlignCenter); |
| 33 | + connect(axisLabelNameLineEdit, SIGNAL(editingFinished()), this, SLOT(slotAxisLabelNameEditingFinished())); |
| 34 | + connect(axisRangeLowerLineEdit, SIGNAL(editingFinished()), this, SLOT(slotAxisRangeEditingFinished())); |
| 35 | + connect(axisRangeUpperLineEdit, SIGNAL(editingFinished()), this, SLOT(slotAxisRangeEditingFinished())); |
| 36 | + |
| 37 | + fontSelectDialog = new QFontDialog(this); |
| 38 | + |
| 39 | + QLabel * axisNameLabel = new QLabel("Label Name:"); |
| 40 | + QLabel * axisRangeLabel = new QLabel("Axis Range:"); |
| 41 | + QLabel * axisLabelFontLabel = new QLabel("Label Font:"); |
| 42 | + QLabel * axisTicksFontLabel = new QLabel("Ticks Font:"); |
| 43 | + |
| 44 | + QHBoxLayout *axisRangeLayoutH = new QHBoxLayout; |
| 45 | + axisRangeLayoutH->setMargin(0); |
| 46 | + QLabel * rangeSpaceTakeLabel = new QLabel(" ~ "); |
| 47 | + rangeSpaceTakeLabel->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed); |
| 48 | + axisRangeLayoutH->addWidget(axisRangeLowerLineEdit); |
| 49 | + axisRangeLayoutH->addWidget(rangeSpaceTakeLabel); |
| 50 | + axisRangeLayoutH->addWidget(axisRangeUpperLineEdit); |
| 51 | + axisRangeLayoutH->addStretch(1); |
| 52 | + |
| 53 | + QHBoxLayout *axisLabelFontLayoutH = new QHBoxLayout; |
| 54 | + QPushButton * axisLabelFontSelectButton = new QPushButton(QIcon("./icon/font-select.png"),"Select Font"); |
| 55 | + //axisLabelFontSelectButton->setFlat(true); |
| 56 | + connect(axisLabelFontSelectButton, SIGNAL(clicked()), this, SLOT(slotAxisLabelFontSelected())); |
| 57 | + axisLabelFontSelectButton->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed); |
| 58 | + axisLabelFontLayoutH->addWidget(axisLabelFontSelectButton); |
| 59 | + axisLabelFontLayoutH->addStretch(1); |
| 60 | + |
| 61 | + QHBoxLayout *axisTicksLabelFontLayoutH = new QHBoxLayout; |
| 62 | + QPushButton * axisTicksLabelFontSelectButton = new QPushButton(QIcon("./icon/font-select.png"),"Select Font"); |
| 63 | + connect(axisTicksLabelFontSelectButton, SIGNAL(clicked()), this, SLOT(slotAxisTicksLabelFontSelected())); |
| 64 | + axisTicksLabelFontSelectButton->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed); |
| 65 | + axisTicksLabelFontLayoutH->addWidget(axisTicksLabelFontSelectButton); |
| 66 | + axisTicksLabelFontLayoutH->addStretch(1); |
| 67 | + |
| 68 | + |
| 69 | + QGridLayout *axisEditLayoutG = new QGridLayout; |
| 70 | + axisEditLayoutG->addWidget(axisNameLabel, 0, 0); |
| 71 | + axisEditLayoutG->addWidget(axisLabelNameLineEdit, 0, 1); |
| 72 | + axisEditLayoutG->addWidget(axisRangeLabel, 1, 0); |
| 73 | + axisEditLayoutG->addLayout(axisRangeLayoutH, 1, 1); |
| 74 | + axisEditLayoutG->addWidget(axisLabelFontLabel, 2, 0); |
| 75 | + axisEditLayoutG->addLayout(axisLabelFontLayoutH, 2, 1); |
| 76 | + axisEditLayoutG->addWidget(axisTicksFontLabel, 3, 0); |
| 77 | + axisEditLayoutG->addLayout(axisTicksLabelFontLayoutH, 3, 1); |
| 78 | + |
| 79 | + QHBoxLayout *axisEditStretchLayoutH = new QHBoxLayout; |
| 80 | + axisEditStretchLayoutH->addLayout(axisEditLayoutG); |
| 81 | + axisEditStretchLayoutH->addStretch(1); |
| 82 | + QVBoxLayout *axisEditStretchLayoutV = new QVBoxLayout; |
| 83 | + axisEditStretchLayoutV->addLayout(axisEditStretchLayoutH); |
| 84 | + axisEditStretchLayoutV->addStretch(1); |
| 85 | + |
| 86 | + this->setLayout(axisEditStretchLayoutV); |
| 87 | + |
| 88 | + |
| 89 | + if(axisIndex == 0) |
| 90 | + { |
| 91 | + setAxisLabelName(qcustomplot->xAxis->label()); |
| 92 | + axisRangeLowerLineEdit->setText(QString::number(qcustomplot->xAxis->range().lower)); |
| 93 | + axisRangeUpperLineEdit->setText(QString::number(qcustomplot->xAxis->range().upper)); |
| 94 | + connect(qcustomplot->xAxis, SIGNAL(rangeChanged(const QCPRange &)), this, SLOT(setAxisRange(const QCPRange &))); |
| 95 | + } |
| 96 | + |
| 97 | + if(axisIndex == 1) |
| 98 | + { |
| 99 | + setAxisLabelName(qcustomplot->yAxis->label()); |
| 100 | + axisRangeLowerLineEdit->setText(QString::number(qcustomplot->yAxis->range().lower)); |
| 101 | + axisRangeUpperLineEdit->setText(QString::number(qcustomplot->yAxis->range().upper)); |
| 102 | + connect(qcustomplot->yAxis, SIGNAL(rangeChanged(const QCPRange &)), this, SLOT(setAxisRange(const QCPRange &))); |
| 103 | + } |
| 104 | + |
| 105 | +} |
| 106 | + |
| 107 | +AxisEditWidget::~AxisEditWidget() { |
| 108 | + // TODO Auto-generated destructor stub |
| 109 | +} |
| 110 | + |
| 111 | +void AxisEditWidget::changeAxisIndex(int indexp) |
| 112 | +{ |
| 113 | + axisIndex = indexp; |
| 114 | + |
| 115 | + if(axisIndex == 0) |
| 116 | + { |
| 117 | + setAxisLabelName(qcustomplot->xAxis->label()); |
| 118 | + axisRangeLowerLineEdit->setText(QString::number(qcustomplot->xAxis->range().lower)); |
| 119 | + axisRangeUpperLineEdit->setText(QString::number(qcustomplot->xAxis->range().upper)); |
| 120 | + disconnect(qcustomplot->yAxis,0,this,0); |
| 121 | + connect(qcustomplot->xAxis, SIGNAL(rangeChanged(const QCPRange &)), this, SLOT(setAxisRange(const QCPRange &))); |
| 122 | + } |
| 123 | + |
| 124 | + if(axisIndex == 1) |
| 125 | + { |
| 126 | + setAxisLabelName(qcustomplot->yAxis->label()); |
| 127 | + axisRangeLowerLineEdit->setText(QString::number(qcustomplot->yAxis->range().lower)); |
| 128 | + axisRangeUpperLineEdit->setText(QString::number(qcustomplot->yAxis->range().upper)); |
| 129 | + disconnect(qcustomplot->xAxis,0,this,0); |
| 130 | + connect(qcustomplot->yAxis, SIGNAL(rangeChanged(const QCPRange &)), this, SLOT(setAxisRange(const QCPRange &))); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | + |
| 135 | +void AxisEditWidget::setAxisLabelName(const QString & name) |
| 136 | +{ |
| 137 | + if(axisLabelNameLineEdit->text() != name) |
| 138 | + axisLabelNameLineEdit->setText(name); |
| 139 | +} |
| 140 | +void AxisEditWidget::setAxisRange(double lower, double upper) |
| 141 | +{ |
| 142 | + if(axisRangeLowerLineEdit->text().toDouble() != lower) |
| 143 | + axisRangeLowerLineEdit->setText(QString::number(lower)); |
| 144 | + |
| 145 | + if(axisRangeUpperLineEdit->text().toDouble() != upper) |
| 146 | + axisRangeUpperLineEdit->setText(QString::number(upper)); |
| 147 | +} |
| 148 | + |
| 149 | +void AxisEditWidget::setAxisRange(const QCPRange & newRange) |
| 150 | +{ |
| 151 | + setAxisRange(newRange.lower,newRange.upper); |
| 152 | +} |
| 153 | + |
| 154 | +void AxisEditWidget::slotAxisLabelNameEditingFinished() |
| 155 | +{ |
| 156 | + if((axisLabelNameLineEdit->text() == "") || (axisLabelNameLineEdit->text() == "NoName")) |
| 157 | + { |
| 158 | + if((axisIndex == 0 ) && (qcustomplot->xAxis->label() != "")) |
| 159 | + qcustomplot->xAxis->setLabel(""); |
| 160 | + else if((axisIndex == 1 ) && (qcustomplot->yAxis->label() != "")) |
| 161 | + qcustomplot->yAxis->setLabel(""); |
| 162 | + |
| 163 | + qcustomplot->replot(); |
| 164 | + |
| 165 | + } |
| 166 | + else |
| 167 | + { |
| 168 | + if((axisIndex == 0) && (axisLabelNameLineEdit->text() != qcustomplot->xAxis->label())) |
| 169 | + qcustomplot->xAxis->setLabel(axisLabelNameLineEdit->text()); |
| 170 | + else if((axisIndex == 1) && (axisLabelNameLineEdit->text() != qcustomplot->yAxis->label())) |
| 171 | + qcustomplot->yAxis->setLabel(axisLabelNameLineEdit->text()); |
| 172 | + |
| 173 | + qcustomplot->replot(); |
| 174 | + |
| 175 | + } |
| 176 | +} |
| 177 | +void AxisEditWidget::slotAxisRangeEditingFinished() |
| 178 | +{ |
| 179 | + if(((sender() == axisRangeLowerLineEdit) && (axisRangeUpperLineEdit != focusWidget())) |
| 180 | + || ((sender() == axisRangeUpperLineEdit) && (axisRangeLowerLineEdit != focusWidget()))) |
| 181 | + { |
| 182 | + double lower = axisRangeLowerLineEdit->text().toDouble(); |
| 183 | + double upper = axisRangeUpperLineEdit->text().toDouble(); |
| 184 | + |
| 185 | + if(lower<upper){ |
| 186 | + if(axisIndex == 0) |
| 187 | + { |
| 188 | + double lowerOld = qcustomplot->xAxis->range().lower; |
| 189 | + double upperOld = qcustomplot->xAxis->range().upper; |
| 190 | + if((lower!=lowerOld) || (upper!=upperOld)) |
| 191 | + qcustomplot->xAxis->setRange(lower,upper); |
| 192 | + } |
| 193 | + |
| 194 | + else if(axisIndex == 1) |
| 195 | + { |
| 196 | + double lowerOld = qcustomplot->yAxis->range().lower; |
| 197 | + double upperOld = qcustomplot->yAxis->range().upper; |
| 198 | + if((lower!=lowerOld) || (upper!=upperOld)) |
| 199 | + qcustomplot->yAxis->setRange(lower,upper); |
| 200 | + } |
| 201 | + |
| 202 | + qcustomplot->replot(); |
| 203 | + } |
| 204 | + |
| 205 | + else |
| 206 | + { |
| 207 | + QMessageBox::information(this,tr("Range input invalid"),tr("Range input invalid(lower>upper)! Please re-input.")); |
| 208 | + axisRangeLowerLineEdit->setFocus(); |
| 209 | + } |
| 210 | + |
| 211 | + |
| 212 | + } |
| 213 | + |
| 214 | +} |
| 215 | +void AxisEditWidget::slotAxisLabelFontSelected() |
| 216 | +{ |
| 217 | + QFont fontSelected; |
| 218 | + if(axisIndex == 0){ |
| 219 | + fontSelectDialog->setCurrentFont(qcustomplot->xAxis->labelFont()); |
| 220 | + if(fontSelectDialog->exec()) |
| 221 | + { |
| 222 | + fontSelected = fontSelectDialog->selectedFont(); |
| 223 | + if(fontSelected != qcustomplot->xAxis->labelFont()) |
| 224 | + { |
| 225 | + qcustomplot->xAxis->setLabelFont(fontSelected); |
| 226 | + qcustomplot->replot(); |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | + else if(axisIndex == 1) |
| 231 | + { |
| 232 | + fontSelectDialog->setCurrentFont(qcustomplot->yAxis->labelFont()); |
| 233 | + if(fontSelectDialog->exec()) |
| 234 | + { |
| 235 | + fontSelected = fontSelectDialog->selectedFont(); |
| 236 | + if(fontSelected != qcustomplot->yAxis->labelFont()) |
| 237 | + { |
| 238 | + qcustomplot->yAxis->setLabelFont(fontSelected); |
| 239 | + qcustomplot->replot(); |
| 240 | + } |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + this->setFocus(); |
| 245 | +} |
| 246 | +void AxisEditWidget::slotAxisTicksLabelFontSelected() |
| 247 | +{ |
| 248 | + QFont fontSelected; |
| 249 | + if(axisIndex == 0){ |
| 250 | + fontSelectDialog->setCurrentFont(qcustomplot->xAxis->tickLabelFont()); |
| 251 | + if(fontSelectDialog->exec()) |
| 252 | + { |
| 253 | + fontSelected = fontSelectDialog->selectedFont(); |
| 254 | + if(fontSelected != qcustomplot->xAxis->tickLabelFont()) |
| 255 | + { |
| 256 | + qcustomplot->xAxis->setTickLabelFont(fontSelected); |
| 257 | + qcustomplot->replot(); |
| 258 | + } |
| 259 | + } |
| 260 | + } |
| 261 | + else if(axisIndex == 1) |
| 262 | + { |
| 263 | + fontSelectDialog->setCurrentFont(qcustomplot->yAxis->tickLabelFont()); |
| 264 | + if(fontSelectDialog->exec()) |
| 265 | + { |
| 266 | + fontSelected = fontSelectDialog->selectedFont(); |
| 267 | + if(fontSelected != qcustomplot->yAxis->tickLabelFont()) |
| 268 | + { |
| 269 | + qcustomplot->yAxis->setTickLabelFont(fontSelected); |
| 270 | + qcustomplot->replot(); |
| 271 | + } |
| 272 | + } |
| 273 | + } |
| 274 | + |
| 275 | + this->setFocus(); |
| 276 | +} |
| 277 | + |
0 commit comments