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