]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiDialog.cpp
fix memory leaks
[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 "GuiView.h"
15 #include "support/debug.h"
16 #include "qt_helpers.h"
17
18 #include <QCloseEvent>
19 #include <QMainWindow>
20 #include <QSettings>
21 #include <QShowEvent>
22
23 using std::string;
24
25 namespace lyx {
26 namespace frontend {
27
28 GuiDialog::GuiDialog(GuiView & lv, std::string const & name)
29         : QDialog(&lv), Dialog(lv, name), is_closing_(false)
30 {}
31
32
33 GuiDialog::~GuiDialog()
34 {
35 }
36
37
38 void GuiDialog::setViewTitle(docstring const & title)
39 {
40         setWindowTitle("LyX: " + toqstr(title));
41 }
42
43
44 void GuiDialog::setButtonsValid(bool valid)
45 {
46         bc().setValid(valid);
47 }
48
49
50 void GuiDialog::slotApply()
51 {
52         apply();
53         bc().apply();
54 }
55
56
57 void GuiDialog::slotOK()
58 {
59         is_closing_ = true;
60         apply();
61         is_closing_ = false;
62         QDialog::hide();
63         bc().ok();
64 }
65
66
67 void GuiDialog::slotClose()
68 {
69         QDialog::hide();
70         bc().cancel();
71 }
72
73
74 void GuiDialog::slotRestore()
75 {
76         // Tell the controller that a request to refresh the dialog's contents
77         // has been received. It's up to the controller to supply the necessary
78         // info by calling GuiDialog::updateView().
79         updateDialog();
80         bc().restore();
81 }
82
83 void GuiDialog::checkStatus()
84 {
85         // buffer independant dialogs are always active.
86         // This check allows us leave canApply unimplemented for some dialogs.
87         if (!isBufferDependent())
88                 return;
89
90         // deactivate the dialog if we have no buffer
91         if (!isBufferAvailable()) {
92                 bc().setReadOnly(true);
93                 return;
94         }
95
96         // check whether this dialog may be active
97         if (canApply()) {
98                 bool const readonly = isBufferReadonly();
99                 bc().setReadOnly(readonly);
100                 // refreshReadOnly() is too generous in _enabling_ widgets
101                 // update dialog to disable disabled widgets again
102
103                 if (!readonly || canApplyToReadOnly())
104                         updateView();
105
106         } else {
107                 bc().setReadOnly(true);
108         }       
109 }
110
111
112 bool GuiDialog::isVisibleView() const
113 {
114         return QDialog::isVisible();
115 }
116
117
118 void GuiDialog::showView()
119 {
120         QSize const hint = sizeHint();
121         if (hint.height() >= 0 && hint.width() >= 0)
122                 setMinimumSize(hint);
123
124         updateView();  // make sure its up-to-date
125         if (exitEarly())
126                 return;
127
128         if (QWidget::isVisible()) {
129                 raise();
130                 activateWindow();
131         } else {
132                 QWidget::show();
133         }
134         setFocus();
135 }
136
137
138 void GuiDialog::hideView()
139 {
140         QDialog::hide();
141 }
142
143
144 void GuiDialog::changed()
145 {
146         if (updating_)
147                 return;
148         bc().setValid(isValid());
149 }
150
151
152 void GuiDialog::updateView()
153 {
154         setUpdatesEnabled(false);
155
156         // protect the BC from unwarranted state transitions
157         updating_ = true;
158         updateContents();
159         updating_ = false;
160
161         setUpdatesEnabled(true);
162         QDialog::update();
163 }
164
165
166 void GuiDialog::showData(string const & data)
167 {
168         if (isBufferDependent() && !isBufferAvailable())
169                 return;
170
171         if (!initialiseParams(data)) {
172                 LYXERR0("Dialog \"" << name()
173                         << "\" failed to translate the data string passed to show()");
174                 return;
175         }
176
177         bc().setReadOnly(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 (isBufferDependent() && !isBufferAvailable())
187                 return;
188
189         if (!initialiseParams(data)) {
190                 LYXERR0("Dialog \"" << name()
191                        << "\" could not be initialized");
192                 return;
193         }
194
195         bc().setReadOnly(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         clearParams();
208         hideView();
209         Dialog::disconnect();
210 }
211
212
213 void GuiDialog::apply()
214 {
215         if (isBufferDependent()) {
216                 if (!isBufferAvailable() ||
217                     (isBufferReadonly() && !canApplyToReadOnly()))
218                         return;
219         }
220
221         applyView();
222         dispatchParams();
223
224         if (disconnectOnApply() && !is_closing_) {
225                 Dialog::disconnect();
226                 initialiseParams(string());
227                 updateView();
228         }
229 }
230
231
232 void GuiDialog::showEvent(QShowEvent * e)
233 {
234         QSettings settings;
235         string key = name() + "/geometry";
236         restoreGeometry(settings.value(key.c_str()).toByteArray());
237         QDialog::showEvent(e);
238 }
239
240
241 void GuiDialog::closeEvent(QCloseEvent * e)
242 {
243         QSettings settings;
244         string key = name() + "/geometry";
245         settings.setValue(key.c_str(), saveGeometry());
246         QDialog::closeEvent(e);
247 }
248
249 } // namespace frontend
250 } // namespace lyx
251
252
253 /////////////////////////////////////////////////////////////////////
254 //
255 // Command based dialogs
256 //
257 /////////////////////////////////////////////////////////////////////
258
259 #include "FuncRequest.h"
260 #include "insets/InsetCommand.h"
261
262
263 using std::string;
264
265 namespace lyx {
266 namespace frontend {
267
268 GuiCommand::GuiCommand(GuiView & lv, string const & name)
269         : GuiDialog(lv, name), params_(insetCode(name)), lfun_name_(name)
270 {
271 }
272
273
274 bool GuiCommand::initialiseParams(string const & data)
275 {
276         // The name passed with LFUN_INSET_APPLY is also the name
277         // used to identify the mailer.
278         InsetCommandMailer::string2params(lfun_name_, data, params_);
279         return true;
280 }
281
282
283 void GuiCommand::dispatchParams()
284 {
285         if (lfun_name_.empty())
286                 return;
287
288         string const lfun = 
289                 InsetCommandMailer::params2string(lfun_name_, params_);
290         dispatch(FuncRequest(getLfun(), lfun));
291 }
292
293 } // namespace frontend
294 } // namespace lyx
295
296 #include "GuiDialog_moc.cpp"