]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiDialog.cpp
42d2109d7a4f2245c04ffbaa35af3d61f01e34cd
[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
17 #include <QCloseEvent>
18 #include <QSettings>
19 #include <QShowEvent>
20
21 using std::string;
22
23 namespace lyx {
24 namespace frontend {
25
26 GuiDialog::GuiDialog(GuiView & lv, std::string const & name)
27         : Dialog(lv), is_closing_(false), name_(name)
28 {}
29
30
31 GuiDialog::~GuiDialog()
32 {
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         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 (!isBufferDependent())
86                 return;
87
88         // deactivate the dialog if we have no buffer
89         if (!isBufferAvailable()) {
90                 bc().setReadOnly(true);
91                 return;
92         }
93
94         // check whether this dialog may be active
95         if (canApply()) {
96                 bool const readonly = isBufferReadonly();
97                 bc().setReadOnly(readonly);
98                 // refreshReadOnly() is too generous in _enabling_ widgets
99                 // update dialog to disable disabled widgets again
100
101                 if (!readonly || canApplyToReadOnly())
102                         updateView();
103
104         } else {
105                 bc().setReadOnly(true);
106         }       
107 }
108
109
110 bool GuiDialog::isVisibleView() const
111 {
112         return QDialog::isVisible();
113 }
114
115
116 void GuiDialog::showView()
117 {
118         QSize const hint = sizeHint();
119         if (hint.height() >= 0 && hint.width() >= 0)
120                 setMinimumSize(hint);
121
122         updateView();  // make sure its up-to-date
123         if (exitEarly())
124                 return;
125
126         if (QWidget::isVisible()) {
127                 raise();
128                 activateWindow();
129         } else {
130                 QWidget::show();
131         }
132         setFocus();
133 }
134
135
136 void GuiDialog::hideView()
137 {
138         QDialog::hide();
139 }
140
141
142 void GuiDialog::changed()
143 {
144         if (updating_)
145                 return;
146         bc().setValid(isValid());
147 }
148
149
150 void GuiDialog::updateView()
151 {
152         setUpdatesEnabled(false);
153
154         // protect the BC from unwarranted state transitions
155         updating_ = true;
156         updateContents();
157         updating_ = false;
158
159         setUpdatesEnabled(true);
160         QDialog::update();
161 }
162
163
164 void GuiDialog::showData(string const & data)
165 {
166         if (isBufferDependent() && !isBufferAvailable())
167                 return;
168
169         if (!initialiseParams(data)) {
170                 lyxerr << "Dialog \"" << name_
171                        << "\" failed to translate the data "
172                         "string passed to show()" << std::endl;
173                 return;
174         }
175
176         bc().setReadOnly(isBufferReadonly());
177         showView();
178         // The widgets may not be valid, so refresh the button controller
179         bc().refresh();
180 }
181
182
183 void GuiDialog::updateData(string const & data)
184 {
185         if (isBufferDependent() && !isBufferAvailable())
186                 return;
187
188         if (!initialiseParams(data)) {
189                 lyxerr << "Dialog \"" << name_
190                        << "\" could not be initialized" << std::endl;
191                 return;
192         }
193
194         bc().setReadOnly(isBufferReadonly());
195         updateView();
196         // The widgets may not be valid, so refresh the button controller
197         bc().refresh();
198 }
199
200
201 void GuiDialog::hide()
202 {
203         if (!isVisibleView())
204                 return;
205
206         clearParams();
207         hideView();
208         Dialog::disconnect(name_);
209 }
210
211
212 void GuiDialog::apply()
213 {
214         if (isBufferDependent()) {
215                 if (!isBufferAvailable() ||
216                     (isBufferReadonly() && !canApplyToReadOnly()))
217                         return;
218         }
219
220         applyView();
221         dispatchParams();
222
223         if (disconnectOnApply() && !is_closing_) {
224                 Dialog::disconnect(name_);
225                 initialiseParams(string());
226                 updateView();
227         }
228 }
229
230
231 void GuiDialog::showEvent(QShowEvent * e)
232 {
233         QSettings settings;
234         string key = name_ + "/geometry";
235         restoreGeometry(settings.value(key.c_str()).toByteArray());
236         QDialog::showEvent(e);
237 }
238
239
240 void GuiDialog::closeEvent(QCloseEvent * e)
241 {
242         QSettings settings;
243         string key = name_ + "/geometry";
244         settings.setValue(key.c_str(), saveGeometry());
245         QDialog::closeEvent(e);
246 }
247
248 } // namespace frontend
249 } // namespace lyx
250
251
252 /////////////////////////////////////////////////////////////////////
253 //
254 // Command based dialogs
255 //
256 /////////////////////////////////////////////////////////////////////
257
258 #include "FuncRequest.h"
259 #include "insets/InsetCommand.h"
260
261
262 using std::string;
263
264 namespace lyx {
265 namespace frontend {
266
267 GuiCommand::GuiCommand(GuiView & lv, string const & name)
268         : GuiDialog(lv, name), params_(insetCode(name)), lfun_name_(name)
269 {
270 }
271
272
273 bool GuiCommand::initialiseParams(string const & data)
274 {
275         // The name passed with LFUN_INSET_APPLY is also the name
276         // used to identify the mailer.
277         InsetCommandMailer::string2params(lfun_name_, data, params_);
278         return true;
279 }
280
281
282 void GuiCommand::dispatchParams()
283 {
284         if (lfun_name_.empty())
285                 return;
286
287         string const lfun = 
288                 InsetCommandMailer::params2string(lfun_name_, params_);
289         dispatch(FuncRequest(getLfun(), lfun));
290 }
291
292 } // namespace frontend
293 } // namespace lyx
294
295 #include "GuiDialog_moc.cpp"