]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiWrap.cpp
af19e66503bc33c86f7d49b07332ecf93fb0cb01
[lyx.git] / src / frontends / qt4 / GuiWrap.cpp
1 /**
2  * \file GuiWrap.cpp
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 "GuiWrap.h"
14 #include "Qt2BC.h"
15
16 #include "LengthCombo.h"
17 #include "qt_helpers.h"
18
19 #include "insets/InsetWrap.h"
20
21 #include "support/convert.h"
22 #include "support/lstrings.h"
23
24 #include <QLineEdit>
25 #include <QCloseEvent>
26 #include <QPushButton>
27
28
29 using std::string;
30
31 namespace lyx {
32 namespace frontend {
33
34 /////////////////////////////////////////////////////////////////////
35 //
36 // GuiWrapDialog
37 //
38 /////////////////////////////////////////////////////////////////////
39
40
41 GuiWrapDialog::GuiWrapDialog(GuiWrap * form)
42         : form_(form)
43 {
44         setupUi(this);
45
46         connect(restorePB, SIGNAL(clicked()), form, SLOT(slotRestore()));
47         connect(okPB, SIGNAL(clicked()), form, SLOT(slotOK()));
48         connect(applyPB, SIGNAL(clicked()), form, SLOT(slotApply()));
49         connect(closePB, SIGNAL(clicked()), form, SLOT(slotClose()));
50
51         connect(widthED, SIGNAL(textChanged(const QString &)),
52                 this, SLOT(change_adaptor()));
53         connect(unitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
54                 this, SLOT(change_adaptor()));
55         connect(valignCO, SIGNAL(highlighted(const QString &)),
56                 this, SLOT(change_adaptor()));
57 }
58
59
60 void GuiWrapDialog::closeEvent(QCloseEvent * e)
61 {
62         form_->slotWMHide();
63         e->accept();
64 }
65
66
67 void GuiWrapDialog::change_adaptor()
68 {
69         form_->changed();
70 }
71
72 /////////////////////////////////////////////////////////////////////
73 //
74 // GuiWrap
75 //
76 /////////////////////////////////////////////////////////////////////
77
78 GuiWrap::GuiWrap(Dialog & parent)
79         : GuiView<GuiWrapDialog>(parent, _("Text Wrap Settings"))
80 {
81 }
82
83
84 void GuiWrap::build_dialog()
85 {
86         dialog_.reset(new GuiWrapDialog(this));
87
88         bcview().setRestore(dialog_->restorePB);
89         bcview().setOK(dialog_->okPB);
90         bcview().setApply(dialog_->applyPB);
91         bcview().setCancel(dialog_->closePB);
92
93         bcview().addReadOnly(dialog_->widthED);
94         bcview().addReadOnly(dialog_->unitsLC);
95         bcview().addReadOnly(dialog_->valignCO);
96 }
97
98
99 void GuiWrap::apply()
100 {
101         double const value = convert<double>(fromqstr(dialog_->widthED->text()));
102         Length::UNIT unit = dialog_->unitsLC->currentLengthItem();
103         if (dialog_->widthED->text().isEmpty())
104                 unit = Length::UNIT_NONE;
105
106         InsetWrapParams & params = controller().params();
107
108         params.width = Length(value, unit);
109
110         switch (dialog_->valignCO->currentIndex()) {
111         case 0:
112                 params.placement.erase();
113                 break;
114         case 1:
115                 params.placement = "l";
116                 break;
117         case 2:
118                 params.placement = "r";
119                 break;
120         case 3:
121                 params.placement = "p";
122                 break;
123         }
124 }
125
126
127 static string const numtostr(double val)
128 {
129         string a = convert<string>(val);
130         // FIXME: Will this test ever trigger? (Lgb)
131         if (a == "0")
132                 a.erase();
133         return a;
134 }
135
136
137 void GuiWrap::update_contents()
138 {
139         InsetWrapParams & params = controller().params();
140
141         Length len(params.width);
142         dialog_->widthED->setText(toqstr(numtostr(len.value())));
143         dialog_->unitsLC->setCurrentItem(len.unit());
144
145         int item = 0;
146         if (params.placement == "l")
147                 item = 1;
148         else if (params.placement == "r")
149                 item = 2;
150         else if (params.placement == "p")
151                 item = 3;
152
153         dialog_->valignCO->setCurrentIndex(item);
154 }
155
156 } // namespace frontend
157 } // namespace lyx
158
159
160 #include "GuiWrap_moc.cpp"