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