]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiDialog.cpp
Introducing TextClassPtr.h to minimize header dependencies.
[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 #include <QCloseEvent>
19 #include <QSettings>
20 #include <QShowEvent>
21
22 using std::string;
23
24 namespace lyx {
25 namespace frontend {
26
27 GuiDialog::GuiDialog(LyXView & lv, std::string const & name)
28         : Dialog(lv), is_closing_(false), name_(name)
29 {}
30
31
32 GuiDialog::~GuiDialog()
33 {
34 }
35
36
37 void GuiDialog::setViewTitle(docstring const & title)
38 {
39         setWindowTitle("LyX: " + toqstr(title));
40 }
41
42
43 void GuiDialog::setButtonsValid(bool valid)
44 {
45         bc().setValid(valid);
46 }
47
48
49 void GuiDialog::slotApply()
50 {
51         apply();
52         bc().apply();
53 }
54
55
56 void GuiDialog::slotOK()
57 {
58         is_closing_ = true;
59         apply();
60         is_closing_ = false;
61         QDialog::hide();
62         bc().ok();
63 }
64
65
66 void GuiDialog::slotClose()
67 {
68         QDialog::hide();
69         bc().cancel();
70 }
71
72
73 void GuiDialog::slotRestore()
74 {
75         // Tell the controller that a request to refresh the dialog's contents
76         // has been received. It's up to the controller to supply the necessary
77         // info by calling GuiDialog::updateView().
78         updateDialog(name_);
79         bc().restore();
80 }
81
82 void GuiDialog::checkStatus()
83 {
84         // buffer independant dialogs are always active.
85         // This check allows us leave canApply unimplemented for some dialogs.
86         if (!isBufferDependent())
87                 return;
88
89         // deactivate the dialog if we have no buffer
90         if (!isBufferAvailable()) {
91                 bc().setReadOnly(true);
92                 return;
93         }
94
95         // check whether this dialog may be active
96         if (canApply()) {
97                 bool const readonly = isBufferReadonly();
98                 bc().setReadOnly(readonly);
99                 // refreshReadOnly() is too generous in _enabling_ widgets
100                 // update dialog to disable disabled widgets again
101
102                 if (!readonly || canApplyToReadOnly())
103                         updateView();
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 (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 (isBufferDependent() && !isBufferAvailable())
168                 return;
169
170         if (!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(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                 lyxerr << "Dialog \"" << name_
191                        << "\" could not be initialized" << std::endl;
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(name_);
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(name_);
226                 initialiseParams(string());
227                 updateView();
228         }
229 }
230
231
232 void GuiDialog::showEvent(QShowEvent * e)
233 {
234 #if (QT_VERSION >= 0x040200)
235         QSettings settings;
236         string key = name_ + "/geometry";
237         restoreGeometry(settings.value(key.c_str()).toByteArray());
238 #endif
239         QDialog::showEvent(e);
240 }
241
242
243 void GuiDialog::closeEvent(QCloseEvent * e)
244 {
245 #if (QT_VERSION >= 0x040200)
246         QSettings settings;
247         string key = name_ + "/geometry";
248         settings.setValue(key.c_str(), saveGeometry());
249 #endif
250         QDialog::closeEvent(e);
251 }
252
253 } // namespace frontend
254 } // namespace lyx
255
256
257 /////////////////////////////////////////////////////////////////////
258 //
259 // Command based dialogs
260 //
261 /////////////////////////////////////////////////////////////////////
262
263 #include "FuncRequest.h"
264 #include "insets/InsetCommand.h"
265
266
267 using std::string;
268
269 namespace lyx {
270 namespace frontend {
271
272 GuiCommand::GuiCommand(LyXView & lv, string const & name)
273         : GuiDialog(lv, name), params_(insetCode(name)), lfun_name_(name)
274 {
275 }
276
277
278 bool GuiCommand::initialiseParams(string const & data)
279 {
280         // The name passed with LFUN_INSET_APPLY is also the name
281         // used to identify the mailer.
282         InsetCommandMailer::string2params(lfun_name_, data, params_);
283         return true;
284 }
285
286
287 void GuiCommand::dispatchParams()
288 {
289         if (lfun_name_.empty())
290                 return;
291
292         string const lfun = 
293                 InsetCommandMailer::params2string(lfun_name_, params_);
294         dispatch(FuncRequest(getLfun(), lfun));
295 }
296
297 } // namespace frontend
298 } // namespace lyx
299
300 #include "GuiDialog_moc.cpp"