1 #include "krulertest.h"
2 
3 #include <kruler.h>
4 
5 #include <QApplication>
6 #include <QButtonGroup>
7 #include <QDoubleSpinBox>
8 #include <QGroupBox>
9 #include <QKeyEvent>
10 #include <QLabel>
11 #include <QLayout>
12 #include <QSpinBox>
13 
14 /*
15 void
16 MyCheckBox::mouseReleaseEvent(QMouseEvent *e )
17 {
18   QButton::mouseReleaseEvent(e);
19   if ();
20 }
21 */
22 
MouseWidget(QWidget * parent)23 MouseWidget::MouseWidget(QWidget *parent)
24     : QFrame(parent)
25 {
26 }
27 
mousePressEvent(QMouseEvent * e)28 void MouseWidget::mousePressEvent(QMouseEvent *e)
29 {
30     mouseButtonDown = true;
31     Q_EMIT newXPos(e->x());
32     Q_EMIT newYPos(e->y());
33 }
34 
mouseReleaseEvent(QMouseEvent *)35 void MouseWidget::mouseReleaseEvent(QMouseEvent *)
36 {
37     mouseButtonDown = false;
38 }
39 
mouseMoveEvent(QMouseEvent * e)40 void MouseWidget::mouseMoveEvent(QMouseEvent *e)
41 {
42     if (mouseButtonDown) {
43         Q_EMIT newXPos(e->x());
44         Q_EMIT newYPos(e->y());
45     }
46 }
47 
resizeEvent(QResizeEvent * r)48 void MouseWidget::resizeEvent(QResizeEvent *r)
49 {
50     Q_EMIT newWidth(r->size().width());
51     Q_EMIT newHeight(r->size().height());
52 }
53 
KRulerTest()54 KRulerTest::KRulerTest()
55     : QWidget(nullptr)
56 {
57     QVBoxLayout *topLayout = new QVBoxLayout(this);
58     mainframe = new QFrame(this);
59     topLayout->addWidget(mainframe);
60 
61     layout = new QGridLayout(mainframe);
62 
63     miniwidget = new QFrame(mainframe);
64     miniwidget->setFrameStyle(QFrame::WinPanel | QFrame::Raised);
65     bigwidget = new MouseWidget(mainframe);
66     bigwidget->setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
67 
68     //  QRect bwrect = bigwidget->frameRect();
69     //  qDebug("big rect: top%i left%i bottom%i right%i",
70     //    bwrect.top(), bwrect.left(), bwrect.bottom(), bwrect.right());
71     hruler = new KRuler(Qt::Horizontal, mainframe);
72     //  hruler->setRange( bwrect.left(), bwrect.right() );
73     hruler->setRange(0, 1000);
74     //  hruler->setOffset( bwrect.left() - bigwidget->frameRect().left() );
75     hruler->setOffset(0);
76 
77     vruler = new KRuler(Qt::Vertical, mainframe);
78     vruler->setOffset(0);
79     vruler->setRange(0, 1000);
80 
81     connect(bigwidget, &MouseWidget::newXPos, hruler, &KRuler::slotNewValue);
82     connect(bigwidget, &MouseWidget::newYPos, vruler, &KRuler::slotNewValue);
83     connect(bigwidget, &MouseWidget::newWidth, this, &KRulerTest::slotNewWidth);
84     connect(bigwidget, &MouseWidget::newHeight, this, &KRulerTest::slotNewHeight);
85 
86     layout->addWidget(miniwidget, 0, 0);
87     layout->addWidget(hruler, 0, 1);
88     layout->addWidget(vruler, 1, 0);
89     layout->addWidget(bigwidget, 1, 1);
90 
91     mouse_message = new QLabel(QStringLiteral("Press and hold mouse button\nfor pointer movement"), bigwidget);
92     mouse_message->adjustSize();
93     mouse_message->move(4, 4);
94 
95     showMarks = new QGroupBox(QStringLiteral("Show which marks ?"), bigwidget);
96     showMarks->setFixedSize(140, 160);
97     showMarks->move(330, 4);
98     showTM = new QCheckBox(QStringLiteral("show tiny marks"), showMarks);
99     showTM->adjustSize();
100     showTM->move(5, 15);
101     showTM->setChecked(true);
102     connect(showTM, &QCheckBox::toggled, this, &KRulerTest::slotSetTinyMarks);
103     showLM = new QCheckBox(QStringLiteral("show little marks"), showMarks);
104     showLM->adjustSize();
105     showLM->move(5, 35);
106     showLM->setChecked(true);
107     connect(showLM, &QCheckBox::toggled, this, &KRulerTest::slotSetLittleMarks);
108     showMM = new QCheckBox(QStringLiteral("show medium marks"), showMarks);
109     showMM->adjustSize();
110     showMM->move(5, 55);
111     showMM->setChecked(true);
112     connect(showMM, &QCheckBox::toggled, this, &KRulerTest::slotSetMediumMarks);
113     showBM = new QCheckBox(QStringLiteral("show big marks"), showMarks);
114     showBM->adjustSize();
115     showBM->move(5, 75);
116     showBM->setChecked(true);
117     connect(showBM, &QCheckBox::toggled, this, &KRulerTest::slotSetBigMarks);
118     showEM = new QCheckBox(QStringLiteral("show end marks"), showMarks);
119     showEM->adjustSize();
120     showEM->move(5, 95);
121     showEM->setChecked(true);
122     connect(showEM, &QCheckBox::toggled, this, &KRulerTest::slotSetEndMarks);
123     showPT = new QCheckBox(QStringLiteral("show ruler pointer"), showMarks);
124     showPT->adjustSize();
125     showPT->move(5, 115);
126     showPT->setChecked(true);
127     connect(showPT, &QCheckBox::toggled, this, &KRulerTest::slotSetRulerPointer);
128     fixLen = new QCheckBox(QStringLiteral("fix ruler length"), showMarks);
129     fixLen->adjustSize();
130     fixLen->move(5, 135);
131     fixLen->setChecked(true);
132     connect(fixLen, &QCheckBox::toggled, this, &KRulerTest::slotFixRulerLength);
133     connect(fixLen, &QCheckBox::toggled, this, &KRulerTest::slotCheckLength);
134 
135     lineEdit = new QGroupBox(QStringLiteral("Value of begin/end"), bigwidget);
136     lineEdit->setFixedSize(140, 80);
137     lineEdit->move(330, 4 + 160);
138     beginMark = new QSpinBox(lineEdit);
139     beginMark->setValue(0);
140     beginMark->setRange(-1000, 1000);
141     beginMark->move(5, 15);
142     beginMark->setFixedSize(beginMark->sizeHint());
143     connect(beginMark, qOverload<int>(&QSpinBox::valueChanged), hruler, &KRuler::slotNewOffset);
144     connect(beginMark, qOverload<int>(&QSpinBox::valueChanged), vruler, &KRuler::slotNewOffset);
145     endMark = new QSpinBox(lineEdit);
146     endMark->setValue(0);
147     endMark->setRange(-1000, 1000);
148     endMark->move(5, 35);
149     endMark->setFixedSize(endMark->sizeHint());
150     connect(endMark, qOverload<int>(&QSpinBox::valueChanged), hruler, &KRuler::slotEndOffset);
151     connect(endMark, qOverload<int>(&QSpinBox::valueChanged), vruler, &KRuler::slotEndOffset);
152     lengthInput = new QSpinBox(lineEdit);
153     lengthInput->setValue(0);
154     lengthInput->setRange(-1000, 1000);
155     lengthInput->move(5, 55);
156     lengthInput->setFixedSize(lengthInput->sizeHint());
157     connect(lengthInput, qOverload<int>(&QSpinBox::valueChanged), hruler, &KRuler::slotEndOffset);
158     connect(lengthInput, qOverload<int>(&QSpinBox::valueChanged), vruler, &KRuler::slotEndOffset);
159 
160     vertrot = new QGroupBox(QStringLiteral("Value of rotate translate for Vert."), bigwidget);
161     vertrot->setFixedSize(140, 80);
162     vertrot->move(330, 4 + 160 + 80 + 4);
163     transX = new QDoubleSpinBox(vertrot);
164     transX->setValue(0.0);
165     transX->setRange(-1000, 1000);
166     transX->setSingleStep(1);
167     transX->move(5, 15);
168     transX->setFixedSize(transX->sizeHint());
169     // transX->setLabel("transx", AlignLeft);
170     connect(transX, qOverload<double>(&QDoubleSpinBox::valueChanged), this, &KRulerTest::slotSetXTrans);
171     transY = new QDoubleSpinBox(vertrot);
172     transY->setValue(-12.0);
173     transY->setRange(-1000, 1000);
174     transY->setSingleStep(1);
175     transY->move(5, 35);
176     transY->setFixedSize(transY->sizeHint());
177     // transY->setLabel("transy", AlignLeft);
178     connect(transY, qOverload<double>(&QDoubleSpinBox::valueChanged), this, &KRulerTest::slotSetYTrans);
179     rotV = new QDoubleSpinBox(vertrot);
180     rotV->setValue(90.0);
181     rotV->setRange(-1000, 1000);
182     rotV->setSingleStep(1);
183     rotV->move(5, 55);
184     rotV->setFixedSize(rotV->sizeHint());
185     // rotV->setLabel("rot", AlignLeft);
186     connect(rotV, qOverload<double>(&QDoubleSpinBox::valueChanged), this, &KRulerTest::slotSetRotate);
187 
188     metricstyle = new QGroupBox(QStringLiteral("metric styles"), bigwidget);
189 
190     QButtonGroup *metricstyleButtons = new QButtonGroup(bigwidget);
191 
192     metricstyle->setFixedSize(100, 120);
193     metricstyle->move(330 - 110, 4);
194     pixelmetric = new QRadioButton(QStringLiteral("pixel"), metricstyle);
195     pixelmetric->adjustSize();
196     pixelmetric->move(5, 15);
197     metricstyleButtons->addButton(pixelmetric, (int)KRuler::Pixel);
198     inchmetric = new QRadioButton(QStringLiteral("inch"), metricstyle);
199     inchmetric->adjustSize();
200     inchmetric->move(5, 35);
201     metricstyleButtons->addButton(inchmetric, (int)KRuler::Inch);
202     mmmetric = new QRadioButton(QStringLiteral("millimeter"), metricstyle);
203     mmmetric->adjustSize();
204     mmmetric->move(5, 55);
205     metricstyleButtons->addButton(mmmetric, (int)KRuler::Millimetres);
206     cmmetric = new QRadioButton(QStringLiteral("centimeter"), metricstyle);
207     cmmetric->adjustSize();
208     cmmetric->move(5, 75);
209     metricstyleButtons->addButton(cmmetric, (int)KRuler::Centimetres);
210     mmetric = new QRadioButton(QStringLiteral("meter"), metricstyle);
211     mmetric->adjustSize();
212     mmetric->move(5, 95);
213     metricstyleButtons->addButton(mmetric, (int)KRuler::Metres);
214     connect(metricstyleButtons, &QButtonGroup::buttonClicked, this, [this, metricstyleButtons](QAbstractButton *button) {
215         slotSetMStyle(metricstyleButtons->id(button));
216     });
217 
218     slotUpdateShowMarks();
219 }
220 
~KRulerTest()221 KRulerTest::~KRulerTest()
222 {
223     delete layout;
224     delete hruler;
225     delete vruler;
226     delete miniwidget;
227     delete bigwidget;
228     delete mainframe;
229 }
230 
slotNewWidth(int width)231 void KRulerTest::slotNewWidth(int width)
232 {
233     hruler->setMaximum(width);
234 }
235 
slotNewHeight(int height)236 void KRulerTest::slotNewHeight(int height)
237 {
238     vruler->setMaximum(height);
239 }
240 
slotSetTinyMarks(bool set)241 void KRulerTest::slotSetTinyMarks(bool set)
242 {
243     hruler->setShowTinyMarks(set);
244     vruler->setShowTinyMarks(set);
245 }
246 
slotSetLittleMarks(bool set)247 void KRulerTest::slotSetLittleMarks(bool set)
248 {
249     hruler->setShowLittleMarks(set);
250     vruler->setShowLittleMarks(set);
251 }
252 
slotSetMediumMarks(bool set)253 void KRulerTest::slotSetMediumMarks(bool set)
254 {
255     hruler->setShowMediumMarks(set);
256     vruler->setShowMediumMarks(set);
257 }
258 
slotSetBigMarks(bool set)259 void KRulerTest::slotSetBigMarks(bool set)
260 {
261     hruler->setShowBigMarks(set);
262     vruler->setShowBigMarks(set);
263 }
264 
slotSetEndMarks(bool set)265 void KRulerTest::slotSetEndMarks(bool set)
266 {
267     hruler->setShowEndMarks(set);
268     vruler->setShowEndMarks(set);
269 }
270 
slotSetRulerPointer(bool set)271 void KRulerTest::slotSetRulerPointer(bool set)
272 {
273     hruler->setShowPointer(set);
274     vruler->setShowPointer(set);
275 }
276 
slotSetRulerLength(int len)277 void KRulerTest::slotSetRulerLength(int len)
278 {
279     hruler->setLength(len);
280     vruler->setLength(len);
281 }
282 
slotFixRulerLength(bool fix)283 void KRulerTest::slotFixRulerLength(bool fix)
284 {
285     hruler->setLengthFixed(fix);
286     vruler->setLengthFixed(fix);
287 }
288 
slotSetMStyle(int style)289 void KRulerTest::slotSetMStyle(int style)
290 {
291     hruler->setRulerMetricStyle((KRuler::MetricStyle)style);
292     vruler->setRulerMetricStyle((KRuler::MetricStyle)style);
293     slotUpdateShowMarks();
294 }
295 
slotUpdateShowMarks()296 void KRulerTest::slotUpdateShowMarks()
297 {
298     showTM->setChecked(hruler->showTinyMarks());
299     showLM->setChecked(hruler->showLittleMarks());
300     showMM->setChecked(hruler->showMediumMarks());
301     showBM->setChecked(hruler->showBigMarks());
302     showEM->setChecked(hruler->showEndMarks());
303 }
304 
slotCheckLength(bool fixlen)305 void KRulerTest::slotCheckLength(bool fixlen)
306 {
307     Q_UNUSED(fixlen);
308     beginMark->setValue(hruler->offset());
309     endMark->setValue(hruler->endOffset());
310     lengthInput->setValue(hruler->length());
311 }
312 
slotSetRotate(double d)313 void KRulerTest::slotSetRotate(double d)
314 {
315     Q_UNUSED(d);
316 #ifdef KRULER_ROTATE_TEST
317     vruler->rotate = d;
318     vruler->update();
319     // debug("rotate %.1f", d);
320 #endif
321 }
322 
slotSetXTrans(double d)323 void KRulerTest::slotSetXTrans(double d)
324 {
325     Q_UNUSED(d);
326 #ifdef KRULER_ROTATE_TEST
327     vruler->xtrans = d;
328     vruler->update();
329     // debug("trans x %.1f", d);
330 #endif
331 }
332 
slotSetYTrans(double d)333 void KRulerTest::slotSetYTrans(double d)
334 {
335     Q_UNUSED(d);
336 #ifdef KRULER_ROTATE_TEST
337     vruler->ytrans = d;
338     vruler->update();
339     // debug("trans y %.1f", d);
340 #endif
341 }
342 
343 /* --- MAIN -----------------------*/
main(int argc,char ** argv)344 int main(int argc, char **argv)
345 {
346     QApplication::setApplicationName(QStringLiteral("test"));
347     QApplication *testapp;
348     KRulerTest *window;
349 
350     testapp = new QApplication(argc, argv);
351     testapp->setFont(QFont(QStringLiteral("Helvetica"), 12));
352 
353     window = new KRulerTest();
354     window->setWindowTitle(QStringLiteral("KRulerTest"));
355     window->resize(800, 600);
356     window->show();
357     return testapp->exec();
358 }
359