]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QWrap.C
e67ab77f3542e555e2de150af839d35a91f7c33e
[lyx.git] / src / frontends / qt2 / QWrap.C
1 /**
2  * \file QWrap.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Spitzmüller
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "QWrap.h"
14 #include "QWrapDialog.h"
15 #include "Qt2BC.h"
16
17 #include "lengthcombo.h"
18 #include "qt_helpers.h"
19
20 #include "controllers/ControlWrap.h"
21
22 #include "insets/insetwrap.h"
23
24 #include "support/convert.h"
25 #include "support/lstrings.h"
26
27 #include <qlineedit.h>
28 #include <qpushbutton.h>
29
30 using lyx::support::strToDbl;
31
32 using std::string;
33
34 namespace lyx {
35 namespace frontend {
36
37 typedef QController<ControlWrap, QView<QWrapDialog> > base_class;
38
39 QWrap::QWrap(Dialog & parent)
40         : base_class(parent, _("LyX: Text Wrap Settings"))
41 {
42 }
43
44
45 void QWrap::build_dialog()
46 {
47         dialog_.reset(new QWrapDialog(this));
48
49         bcview().setRestore(dialog_->restorePB);
50         bcview().setOK(dialog_->okPB);
51         bcview().setApply(dialog_->applyPB);
52         bcview().setCancel(dialog_->closePB);
53
54         bcview().addReadOnly(dialog_->widthED);
55         bcview().addReadOnly(dialog_->unitsLC);
56         bcview().addReadOnly(dialog_->valignCO);
57 }
58
59
60 void QWrap::apply()
61 {
62         double const value = strToDbl(fromqstr(dialog_->widthED->text()));
63         LyXLength::UNIT unit = dialog_->unitsLC->currentLengthItem();
64         if (dialog_->widthED->text().isEmpty())
65                 unit = LyXLength::UNIT_NONE;
66
67         InsetWrapParams & params = controller().params();
68
69         params.width = LyXLength(value, unit);
70
71         switch (dialog_->valignCO->currentItem()) {
72         case 0:
73                 params.placement.erase();
74                 break;
75         case 1:
76                 params.placement = "l";
77                 break;
78         case 2:
79                 params.placement = "r";
80                 break;
81         case 3:
82                 params.placement = "p";
83                 break;
84         }
85 }
86
87
88 namespace {
89
90 string const numtostr(double val) {
91         string a(convert<string>(val));
92 #ifdef WITH_WARNINGS
93 #warning Will this test ever trigger? (Lgb)
94 #endif
95         if (a == "0")
96                 a.erase();
97         return a;
98 }
99
100 } // namespace anon
101
102
103 void QWrap::update_contents()
104 {
105         InsetWrapParams & params = controller().params();
106
107         LyXLength len(params.width);
108         dialog_->widthED->setText(toqstr(numtostr(len.value())));
109         dialog_->unitsLC->setCurrentItem(len.unit());
110
111         int item = 0;
112         if (params.placement == "l")
113                 item = 1;
114         else if (params.placement == "r")
115                 item = 2;
116         else if (params.placement == "p")
117                 item = 3;
118
119         dialog_->valignCO->setCurrentItem(item);
120 }
121
122 } // namespace frontend
123 } // namespace lyx