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