]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiDialog.cpp
cosmetics
[lyx.git] / src / frontends / qt4 / GuiDialog.cpp
1 /**
2  * \file Dialog.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GuiDialog.h"
14 #include "debug.h"
15 #include "qt_helpers.h"
16 #include "frontends/LyXView.h"
17
18 using std::string;
19
20 namespace lyx {
21 namespace frontend {
22
23 GuiDialog::GuiDialog(LyXView & lv, std::string const & name)
24         : is_closing_(false), name_(name), controller_(0)
25 {
26         lyxview_ = &lv;
27 }
28
29
30 GuiDialog::~GuiDialog()
31 {
32         delete controller_;
33 }
34
35
36 void GuiDialog::setViewTitle(docstring const & title)
37 {
38         setWindowTitle("LyX: " + toqstr(title));
39 }
40
41
42 void GuiDialog::setButtonsValid(bool valid)
43 {
44         bc().setValid(valid);
45 }
46
47
48 void GuiDialog::slotApply()
49 {
50         apply();
51         bc().apply();
52 }
53
54
55 void GuiDialog::slotOK()
56 {
57         is_closing_ = true;
58         apply();
59         is_closing_ = false;
60         QDialog::hide();
61         bc().ok();
62 }
63
64
65 void GuiDialog::slotClose()
66 {
67         QDialog::hide();
68         bc().cancel();
69 }
70
71
72 void GuiDialog::slotRestore()
73 {
74         // Tell the controller that a request to refresh the dialog's contents
75         // has been received. It's up to the controller to supply the necessary
76         // info by calling GuiDialog::updateView().
77         controller().updateDialog(name_);
78         bc().restore();
79 }
80
81 void GuiDialog::checkStatus()
82 {
83         // buffer independant dialogs are always active.
84         // This check allows us leave canApply unimplemented for some dialogs.
85         if (!controller().isBufferDependent())
86                 return;
87
88         // deactivate the dialog if we have no buffer
89         if (!controller().isBufferAvailable()) {
90                 bc().setReadOnly(true);
91                 return;
92         }
93
94         // check whether this dialog may be active
95         if (controller().canApply()) {
96                 bool const readonly = controller().isBufferReadonly();
97                 bc().setReadOnly(readonly);
98                 // refreshReadOnly() is too generous in _enabling_ widgets
99                 // update dialog to disable disabled widgets again
100 /*
101  *      FIXME:
102                 if (!readonly || controller().canApplyToReadOnly())
103                         update();
104 */
105         } else {
106                 bc().setReadOnly(true);
107         }       
108 }
109
110
111 bool GuiDialog::isVisibleView() const
112 {
113         return QDialog::isVisible();
114 }
115
116
117 void GuiDialog::showView()
118 {
119         QSize const hint = sizeHint();
120         if (hint.height() >= 0 && hint.width() >= 0)
121                 setMinimumSize(hint);
122
123         updateView();  // make sure its up-to-date
124         if (controller().exitEarly())
125                 return;
126
127         if (QWidget::isVisible()) {
128                 raise();
129                 activateWindow();
130         } else {
131                 QWidget::show();
132         }
133         setFocus();
134 }
135
136
137 void GuiDialog::hideView()
138 {
139         QDialog::hide();
140 }
141
142
143 void GuiDialog::changed()
144 {
145         if (updating_)
146                 return;
147         bc().setValid(isValid());
148 }
149
150
151 void GuiDialog::updateView()
152 {
153         setUpdatesEnabled(false);
154
155         // protect the BC from unwarranted state transitions
156         updating_ = true;
157         updateContents();
158         updating_ = false;
159
160         setUpdatesEnabled(true);
161         QDialog::update();
162 }
163
164
165 void GuiDialog::showData(string const & data)
166 {
167         if (controller().isBufferDependent() && !controller().isBufferAvailable())
168                 return;
169
170         if (!controller().initialiseParams(data)) {
171                 lyxerr << "Dialog \"" << name_
172                        << "\" failed to translate the data "
173                         "string passed to show()" << std::endl;
174                 return;
175         }
176
177         bc().setReadOnly(controller().isBufferReadonly());
178         showView();
179         // The widgets may not be valid, so refresh the button controller
180         bc().refresh();
181 }
182
183
184 void GuiDialog::updateData(string const & data)
185 {
186         if (controller().isBufferDependent() && !controller().isBufferAvailable())
187                 return;
188
189         if (!controller().initialiseParams(data)) {
190                 lyxerr << "Dialog \"" << name_
191                        << "\" could not be initialized" << std::endl;
192                 return;
193         }
194
195         bc().setReadOnly(controller().isBufferReadonly());
196         updateView();
197         // The widgets may not be valid, so refresh the button controller
198         bc().refresh();
199 }
200
201
202 void GuiDialog::hide()
203 {
204         if (!isVisibleView())
205                 return;
206
207         controller().clearParams();
208         hideView();
209         controller().disconnect(name_);
210 }
211
212
213 void GuiDialog::apply()
214 {
215         if (controller().isBufferDependent()) {
216                 if (!controller().isBufferAvailable() ||
217                     (controller().isBufferReadonly() && !controller().canApplyToReadOnly()))
218                         return;
219         }
220
221         applyView();
222         controller().dispatchParams();
223
224         if (controller().disconnectOnApply() && !is_closing_) {
225                 controller().disconnect(name_);
226                 controller().initialiseParams(string());
227                 updateView();
228         }
229 }
230
231
232 void GuiDialog::setController(Controller * controller)
233 {
234         BOOST_ASSERT(controller);
235         BOOST_ASSERT(!controller_);
236         controller_ = controller;
237         controller_->setLyXView(*lyxview_);
238 }
239
240
241 } // namespace frontend
242 } // namespace lyx
243
244 #include "GuiDialog_moc.cpp"