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