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