]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Dialog.cpp
44b4ca5935cac2b8c996233641493a1819170600
[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
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         QSize const hint = w->sizeHint();
193         if (hint.height() >= 0 && hint.width() >= 0)
194                 w->setMinimumSize(hint);
195
196         if (w->isVisible()) {
197                 w->raise();
198                 w->activateWindow();
199         } else
200                 w->show();
201
202         w->setFocus();
203 }
204
205
206 void Dialog::hideView()
207 {
208         QWidget * w = asQWidget();
209         if (!w->isVisible())
210                 return;
211         clearParams();
212         disconnect();
213         w->hide();
214 }
215
216
217 bool Dialog::isVisibleView() const
218 {
219         return asQWidget()->isVisible();
220 }
221
222
223 void Dialog::checkStatus()
224 {
225         // buffer independant dialogs are always active.
226         // This check allows us leave canApply unimplemented for some dialogs.
227         if (!isBufferDependent())
228                 return;
229
230         // deactivate the dialog if we have no buffer
231         if (!isBufferAvailable()) {
232                 enableView(false);
233                 return;
234         }
235
236         // check whether this dialog may be active
237         if (canApply()) {
238                 bool const readonly = isBufferReadonly();
239                 enableView(!readonly);
240                 // refreshReadOnly() is too generous in _enabling_ widgets
241                 // update dialog to disable disabled widgets again
242
243                 if (!readonly || canApplyToReadOnly())
244                         updateView();
245
246         } else
247                 enableView(false);
248 }
249
250
251 QString Dialog::sessionKey() const
252 {
253         return "view-" + QString::number(lyxview_->id())
254                 + "/" + toqstr(name());
255 }
256
257
258 void Dialog::saveSession() const
259 {
260         QSettings settings;
261         settings.setValue(sessionKey() + "/geometry", asQWidget()->saveGeometry());
262 }
263
264
265 void Dialog::restoreSession()
266 {
267         QSettings settings;
268         asQWidget()->restoreGeometry(
269                 settings.value(sessionKey() + "/geometry").toByteArray());
270 }
271
272 } // namespace frontend
273 } // namespace lyx