]> Shamusworld >> Repos - architektonas/blob - src/forms/lineangleoptions.cpp
43775e41702fbf6a853f8f98fb9f8dc7ea7cf67c
[architektonas] / src / forms / lineangleoptions.cpp
1 // lineangleoptions.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // (C) 2010 Underground Software
7 //
8 // JLH = James L. Hammons <jlhamm@acm.org>
9 //
10 // Who  When        What
11 // ---  ----------  -----------------------------------------------------------
12 // JLH  06/10/2010  Created this file. :-)
13 //
14
15 #include "lineangleoptions.h"
16
17 #include "actiondrawlineangle.h"
18 #include "settings.h"
19
20 LineAngleOptions::LineAngleOptions(QToolBar * parent/*= 0*/, Qt::WindowFlags flags/*= 0*/):
21         QWidget(parent, flags),
22         lAngle(new QLabel(tr("Angle:"))),
23         leAngle(new QLineEdit(this)),
24         lLength(new QLabel(tr("Length:"))),
25         leLength(new QLineEdit(this)),
26         lSnapPoint(new QLabel(tr("Snap Point:"))),
27         cbSnapPoint(new QComboBox(this))
28 {
29         QHBoxLayout * layout = new QHBoxLayout(this);
30         layout->setContentsMargins(0, 0, 0, 0);
31
32         QStringList items;
33         items << "Start" << "Middle" << "End";
34         cbSnapPoint->addItems(items);
35
36         leAngle->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Ignored));
37         leLength->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Ignored));
38
39         layout->addWidget(lAngle);
40         layout->addWidget(leAngle);
41         layout->addWidget(lLength);
42         layout->addWidget(leLength);
43         layout->addWidget(lSnapPoint);
44         layout->addWidget(cbSnapPoint);
45
46         connect(leAngle, SIGNAL(textChanged(QString)), this, SLOT(updateAngle(QString)));
47         connect(leLength, SIGNAL(textChanged(QString)), this, SLOT(updateLength(QString)));
48         connect(cbSnapPoint, SIGNAL(activated(int)), this, SLOT(updateSnapPoint(int)));
49
50         // We need to add the widget (this thing) to the toolbar passed in. Otherwise,
51         // nothing will show up on the screen. :-)
52         if (parent)
53                 parent->addWidget(this);
54 }
55
56 LineAngleOptions::~LineAngleOptions()
57 {
58         if (action)
59         {
60                 settings.beginGroup("Draw");
61
62                 if (!action->hasFixedAngle())
63                         settings.setValue("LineAngleAngle", RS_Math::rad2deg(action->getAngle()));
64
65                 settings.setValue("LineAngleLength", action->getLength());
66                 settings.setValue("LineAngleSnapPoint", action->getSnapPoint());
67                 settings.endGroup();
68         }
69 }
70
71 void LineAngleOptions::setAction(ActionInterface * a, bool update)
72 {
73         if (a != NULL && a->rtti() == RS2::ActionDrawLineAngle)
74         {
75                 action = (ActionDrawLineAngle *)a;
76
77                 if (action->hasFixedAngle())
78                 {
79                         lAngle->hide();
80                         leAngle->hide();
81                 }
82
83                 QString sa;
84                 QString sl;
85                 int sp;
86
87                 // settings from action:
88                 if (update)
89                 {
90                         sa = QString("%1").arg(RS_Math::rad2deg(action->getAngle()));
91                         sl = QString("%1").arg(action->getLength());
92                         sp = action->getSnapPoint();
93                 }
94                 // settings from config file:
95                 else
96                 {
97                         settings.beginGroup("Draw");
98
99                         if (!action->hasFixedAngle())
100                                 sa = settings.value("LineAngleAngle", "30.0").toString();
101                         else
102                                 sa = QString("%1").arg(action->getAngle());
103
104                         sl = settings.value("LineAngleLength", "10.0").toString();
105                         sp = settings.value("LineAngleSnapPoint", 0).toInt();
106                         settings.endGroup();
107                         action->setSnapPoint(sp);
108                 }
109
110                 leAngle->setText(sa);
111                 leLength->setText(sl);
112                 cbSnapPoint->setCurrentIndex(sp);
113         }
114         else
115         {
116                 RS_DEBUG->print(RS_Debug::D_ERROR, "LineAngleOptions::setAction: wrong action type");
117                 this->action = NULL;
118         }
119 }
120
121 void LineAngleOptions::updateAngle(const QString & a)
122 {
123         if (action != NULL && !action->hasFixedAngle())
124                 action->setAngle(RS_Math::deg2rad(RS_Math::eval(a)));
125 }
126
127 void LineAngleOptions::updateLength(const QString & l)
128 {
129         if (action != NULL)
130                 action->setLength(RS_Math::eval(l));
131 }
132
133 void LineAngleOptions::updateSnapPoint(int sp)
134 {
135         if (action != NULL)
136                 action->setSnapPoint(sp);
137 }