]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiDialog.cpp
Potential bug fix...
[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                 if (!readonly || controller().canApplyToReadOnly())
102                         updateView();
103
104         } else {
105                 bc().setReadOnly(true);
106         }       
107 }
108
109
110 bool GuiDialog::isVisibleView() const
111 {
112         return QDialog::isVisible();
113 }
114
115
116 void GuiDialog::showView()
117 {
118         QSize const hint = sizeHint();
119         if (hint.height() >= 0 && hint.width() >= 0)
120                 setMinimumSize(hint);
121
122         updateView();  // make sure its up-to-date
123         if (controller().exitEarly())
124                 return;
125
126         if (QWidget::isVisible()) {
127                 raise();
128                 activateWindow();
129         } else {
130                 QWidget::show();
131         }
132         setFocus();
133 }
134
135
136 void GuiDialog::hideView()
137 {
138         QDialog::hide();
139 }
140
141
142 void GuiDialog::changed()
143 {
144         if (updating_)
145                 return;
146         bc().setValid(isValid());
147 }
148
149
150 void GuiDialog::updateView()
151 {
152         setUpdatesEnabled(false);
153
154         // protect the BC from unwarranted state transitions
155         updating_ = true;
156         updateContents();
157         updating_ = false;
158
159         setUpdatesEnabled(true);
160         QDialog::update();
161 }
162
163
164 void GuiDialog::showData(string const & data)
165 {
166         if (controller().isBufferDependent() && !controller().isBufferAvailable())
167                 return;
168
169         if (!controller().initialiseParams(data)) {
170                 lyxerr << "Dialog \"" << name_
171                        << "\" failed to translate the data "
172                         "string passed to show()" << std::endl;
173                 return;
174         }
175
176         bc().setReadOnly(controller().isBufferReadonly());
177         showView();
178         // The widgets may not be valid, so refresh the button controller
179         bc().refresh();
180 }
181
182
183 void GuiDialog::updateData(string const & data)
184 {
185         if (controller().isBufferDependent() && !controller().isBufferAvailable())
186                 return;
187
188         if (!controller().initialiseParams(data)) {
189                 lyxerr << "Dialog \"" << name_
190                        << "\" could not be initialized" << std::endl;
191                 return;
192         }
193
194         bc().setReadOnly(controller().isBufferReadonly());
195         updateView();
196         // The widgets may not be valid, so refresh the button controller
197         bc().refresh();
198 }
199
200
201 void GuiDialog::hide()
202 {
203         if (!isVisibleView())
204                 return;
205
206         controller().clearParams();
207         hideView();
208         controller().disconnect(name_);
209 }
210
211
212 void GuiDialog::apply()
213 {
214         if (controller().isBufferDependent()) {
215                 if (!controller().isBufferAvailable() ||
216                     (controller().isBufferReadonly() && !controller().canApplyToReadOnly()))
217                         return;
218         }
219
220         applyView();
221         controller().dispatchParams();
222
223         if (controller().disconnectOnApply() && !is_closing_) {
224                 controller().disconnect(name_);
225                 controller().initialiseParams(string());
226                 updateView();
227         }
228 }
229
230
231 void GuiDialog::setController(Controller * controller)
232 {
233         BOOST_ASSERT(controller);
234         BOOST_ASSERT(!controller_);
235         controller_ = controller;
236         controller_->setLyXView(*lyxview_);
237 }
238
239
240 } // namespace frontend
241 } // namespace lyx
242
243 #include "GuiDialog_moc.cpp"