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