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