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