]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Dialog.cpp
27af0d11d7da8f0ac8bdc58e94b01aae19bc4ce8
[lyx.git] / src / frontends / qt4 / Dialog.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 "Dialog.h"
14
15 #include "GuiView.h"
16
17 #include "Buffer.h"
18 #include "FuncRequest.h"
19 #include "FuncStatus.h"
20 #include "LyXFunc.h"
21
22 #include "support/Debug.h"
23
24 #include <string>
25
26 using namespace std;
27
28 namespace lyx {
29 namespace frontend {
30
31
32 Dialog::Dialog(GuiView & lv, std::string const & name)
33         : lyxview_(&lv), name_(name)
34 {}
35
36
37 Dialog::~Dialog()
38 {}
39
40
41 std::string const & Dialog::name() const
42 {
43         return name_;
44 }
45
46 bool Dialog::canApply() const
47 {
48         FuncRequest const fr(getLfun(), from_ascii(name_));
49         FuncStatus const fs(getStatus(fr));
50         return fs.enabled();
51 }
52
53
54 void Dialog::dispatch(FuncRequest const & fr) const
55 {
56         theLyXFunc().setLyXView(lyxview_);
57         lyx::dispatch(fr);
58 }
59
60
61 void Dialog::updateDialog() const
62 {
63         dispatch(FuncRequest(LFUN_DIALOG_UPDATE, from_ascii(name_)));
64 }
65
66
67 void Dialog::disconnect() const
68 {
69         lyxview_->disconnectDialog(name_);
70 }
71
72
73 bool Dialog::isBufferAvailable() const
74 {
75         return lyxview_->buffer() != 0;
76 }
77
78
79 bool Dialog::isBufferReadonly() const
80 {
81         if (!lyxview_->buffer())
82                 return true;
83         return lyxview_->buffer()->isReadonly();
84 }
85
86
87 std::string const Dialog::bufferFilepath() const
88 {
89         return buffer().filePath();
90 }
91
92
93 KernelDocType Dialog::docType() const
94 {
95         if (buffer().isLatex())
96                 return LATEX;
97         if (buffer().isLiterate())
98                 return LITERATE;
99
100         return DOCBOOK;
101 }
102
103
104 BufferView * Dialog::bufferview()
105 {
106         return lyxview_->view();
107 }
108
109
110 BufferView const * Dialog::bufferview() const
111 {
112         return lyxview_->view();
113 }
114
115
116 Buffer & Dialog::buffer()
117 {
118         BOOST_ASSERT(lyxview_->buffer());
119         return *lyxview_->buffer();
120 }
121
122
123 Buffer const & Dialog::buffer() const
124 {
125         BOOST_ASSERT(lyxview_->buffer());
126         return *lyxview_->buffer();
127 }
128
129
130 void Dialog::showData(string const & data)
131 {
132         if (isBufferDependent() && !isBufferAvailable())
133                 return;
134
135         if (!initialiseParams(data)) {
136                 LYXERR0("Dialog \"" << name()
137                         << "\" failed to translate the data string passed to show()");
138                 return;
139         }
140
141         showView();
142 }
143
144
145 void Dialog::apply()
146 {
147         if (isBufferDependent()) {
148                 if (!isBufferAvailable() ||
149                     (isBufferReadonly() && !canApplyToReadOnly()))
150                         return;
151         }
152
153         applyView();
154         dispatchParams();
155
156         if (disconnectOnApply() && !isClosing()) {
157                 disconnect();
158                 initialiseParams(string());
159                 updateView();
160         }
161 }
162
163
164 void Dialog::updateData(string const & data)
165 {
166         if (isBufferDependent() && !isBufferAvailable())
167                 return;
168
169         if (!initialiseParams(data)) {
170                 LYXERR0("Dialog \"" << name()
171                        << "\" could not be initialized");
172                 return;
173         }
174
175         updateView();
176 }
177
178
179 void Dialog::showView()
180 {
181         updateView();  // make sure its up-to-date
182         if (exitEarly())
183                 return;
184
185         QWidget * w = asQWidget();
186         QSize const hint = w->sizeHint();
187         if (hint.height() >= 0 && hint.width() >= 0)
188                 w->setMinimumSize(hint);
189
190         if (w->isVisible()) {
191                 w->raise();
192                 w->activateWindow();
193         } else
194                 w->show();
195
196         w->setFocus();
197 }
198
199
200 void Dialog::hideView()
201 {
202         QWidget * w = asQWidget();
203         if (!w->isVisible())
204                 return;
205         clearParams();
206         disconnect();
207         w->hide();
208 }
209
210
211 bool Dialog::isVisibleView() const
212 {
213         return asQWidget()->isVisible();
214 }
215
216
217 void Dialog::checkStatus()
218 {
219         // buffer independant dialogs are always active.
220         // This check allows us leave canApply unimplemented for some dialogs.
221         if (!isBufferDependent())
222                 return;
223
224         // deactivate the dialog if we have no buffer
225         if (!isBufferAvailable()) {
226                 enableView(false);
227                 return;
228         }
229
230         // check whether this dialog may be active
231         if (canApply()) {
232                 bool const readonly = isBufferReadonly();
233                 enableView(!readonly);
234                 // refreshReadOnly() is too generous in _enabling_ widgets
235                 // update dialog to disable disabled widgets again
236
237                 if (!readonly || canApplyToReadOnly())
238                         updateView();
239
240         } else
241                 enableView(false);
242 }
243
244 } // namespace frontend
245 } // namespace lyx