]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiDialog.cpp
5034373cce484f218f9deb02d18178dc7d0b2fee
[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 "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                 lyxerr << "Dialog \"" << name()
173                        << "\" failed to translate the data "
174                         "string passed to show()" << std::endl;
175                 return;
176         }
177
178         bc().setReadOnly(isBufferReadonly());
179         showView();
180         // The widgets may not be valid, so refresh the button controller
181         bc().refresh();
182 }
183
184
185 void GuiDialog::updateData(string const & data)
186 {
187         if (isBufferDependent() && !isBufferAvailable())
188                 return;
189
190         if (!initialiseParams(data)) {
191                 lyxerr << "Dialog \"" << name()
192                        << "\" could not be initialized" << std::endl;
193                 return;
194         }
195
196         bc().setReadOnly(isBufferReadonly());
197         updateView();
198         // The widgets may not be valid, so refresh the button controller
199         bc().refresh();
200 }
201
202
203 void GuiDialog::hide()
204 {
205         if (!isVisibleView())
206                 return;
207
208         clearParams();
209         hideView();
210         Dialog::disconnect();
211 }
212
213
214 void GuiDialog::apply()
215 {
216         if (isBufferDependent()) {
217                 if (!isBufferAvailable() ||
218                     (isBufferReadonly() && !canApplyToReadOnly()))
219                         return;
220         }
221
222         applyView();
223         dispatchParams();
224
225         if (disconnectOnApply() && !is_closing_) {
226                 Dialog::disconnect();
227                 initialiseParams(string());
228                 updateView();
229         }
230 }
231
232
233 void GuiDialog::showEvent(QShowEvent * e)
234 {
235         QSettings settings;
236         string key = name() + "/geometry";
237         restoreGeometry(settings.value(key.c_str()).toByteArray());
238         QDialog::showEvent(e);
239 }
240
241
242 void GuiDialog::closeEvent(QCloseEvent * e)
243 {
244         QSettings settings;
245         string key = name() + "/geometry";
246         settings.setValue(key.c_str(), saveGeometry());
247         QDialog::closeEvent(e);
248 }
249
250 } // namespace frontend
251 } // namespace lyx
252
253
254 /////////////////////////////////////////////////////////////////////
255 //
256 // Command based dialogs
257 //
258 /////////////////////////////////////////////////////////////////////
259
260 #include "FuncRequest.h"
261 #include "insets/InsetCommand.h"
262
263
264 using std::string;
265
266 namespace lyx {
267 namespace frontend {
268
269 GuiCommand::GuiCommand(GuiView & lv, string const & name)
270         : GuiDialog(lv, name), params_(insetCode(name)), lfun_name_(name)
271 {
272 }
273
274
275 bool GuiCommand::initialiseParams(string const & data)
276 {
277         // The name passed with LFUN_INSET_APPLY is also the name
278         // used to identify the mailer.
279         InsetCommandMailer::string2params(lfun_name_, data, params_);
280         return true;
281 }
282
283
284 void GuiCommand::dispatchParams()
285 {
286         if (lfun_name_.empty())
287                 return;
288
289         string const lfun = 
290                 InsetCommandMailer::params2string(lfun_name_, params_);
291         dispatch(FuncRequest(getLfun(), lfun));
292 }
293
294 } // namespace frontend
295 } // namespace lyx
296
297 #include "GuiDialog_moc.cpp"