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