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